Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 4fccd2fe 2023-03-08 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 9f7d7167 2018-04-29 stsp
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
21 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
22 80ddbec8 2018-04-29 stsp
23 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
24 31120ada 2018-04-30 stsp #include <errno.h>
25 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
26 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
27 def2f970 2021-09-28 thomas #endif
28 9f7d7167 2018-04-29 stsp #include <curses.h>
29 9f7d7167 2018-04-29 stsp #include <panel.h>
30 9f7d7167 2018-04-29 stsp #include <locale.h>
31 61266923 2020-01-14 stsp #include <signal.h>
32 9f7d7167 2018-04-29 stsp #include <stdlib.h>
33 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
34 26ed57b2 2018-05-19 stsp #include <stdio.h>
35 9f7d7167 2018-04-29 stsp #include <getopt.h>
36 9f7d7167 2018-04-29 stsp #include <string.h>
37 9f7d7167 2018-04-29 stsp #include <err.h>
38 80ddbec8 2018-04-29 stsp #include <unistd.h>
39 26ed57b2 2018-05-19 stsp #include <limits.h>
40 61e69b96 2018-05-20 stsp #include <wchar.h>
41 788c352e 2018-06-16 stsp #include <time.h>
42 84451b3e 2018-07-10 stsp #include <pthread.h>
43 5036bf37 2018-09-24 stsp #include <libgen.h>
44 60493ae3 2019-06-20 stsp #include <regex.h>
45 3da8ef85 2021-09-21 stsp #include <sched.h>
46 dd038bc6 2021-09-21 thomas.ad
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 9f7d7167 2018-04-29 stsp
62 881b2d3e 2018-04-30 stsp #ifndef MIN
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 881b2d3e 2018-04-30 stsp #endif
65 881b2d3e 2018-04-30 stsp
66 2bd27830 2018-10-22 stsp #ifndef MAX
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #endif
69 2bd27830 2018-10-22 stsp
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
72 acf52a76 2021-09-21 thomas.ad #endif
73 2bd27830 2018-10-22 stsp
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 9f7d7167 2018-04-29 stsp #endif
77 9f7d7167 2018-04-29 stsp
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
82 9f7d7167 2018-04-29 stsp };
83 9f7d7167 2018-04-29 stsp
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 9f7d7167 2018-04-29 stsp
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 9f7d7167 2018-04-29 stsp
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
103 9f7d7167 2018-04-29 stsp };
104 9f7d7167 2018-04-29 stsp
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 fc2737d5 2022-09-15 thomas TOG_VIEW_HELP
112 fc2737d5 2022-09-15 thomas };
113 fc2737d5 2022-09-15 thomas
114 fc2737d5 2022-09-15 thomas /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 fc2737d5 2022-09-15 thomas enum tog_keymap_type {
116 fc2737d5 2022-09-15 thomas TOG_KEYMAP_KEYS = -2,
117 fc2737d5 2022-09-15 thomas TOG_KEYMAP_GLOBAL,
118 fc2737d5 2022-09-15 thomas TOG_KEYMAP_DIFF,
119 fc2737d5 2022-09-15 thomas TOG_KEYMAP_LOG,
120 fc2737d5 2022-09-15 thomas TOG_KEYMAP_BLAME,
121 fc2737d5 2022-09-15 thomas TOG_KEYMAP_TREE,
122 fc2737d5 2022-09-15 thomas TOG_KEYMAP_REF,
123 fc2737d5 2022-09-15 thomas TOG_KEYMAP_HELP
124 d6b05b5b 2018-08-04 stsp };
125 c3e9aa98 2019-05-13 jcs
126 a5d43cac 2022-07-01 thomas enum tog_view_mode {
127 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
128 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
129 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
130 a5d43cac 2022-07-01 thomas };
131 a5d43cac 2022-07-01 thomas
132 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
133 a5d43cac 2022-07-01 thomas
134 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
135 d6b05b5b 2018-08-04 stsp
136 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
137 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
138 ba4f502b 2018-08-04 stsp struct got_object_id *id;
139 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
140 1a76625f 2018-10-22 stsp int idx;
141 ba4f502b 2018-08-04 stsp };
142 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 ba4f502b 2018-08-04 stsp struct commit_queue {
144 ba4f502b 2018-08-04 stsp int ncommits;
145 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
146 6d17833f 2019-11-08 stsp };
147 6d17833f 2019-11-08 stsp
148 f26dddb7 2019-11-08 stsp struct tog_color {
149 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
150 6d17833f 2019-11-08 stsp regex_t regex;
151 6d17833f 2019-11-08 stsp short colorpair;
152 15a087fe 2019-02-21 stsp };
153 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
154 11b20872 2019-11-08 stsp
155 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
157 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
158 2183bbf6 2022-01-23 thomas
159 2183bbf6 2022-01-23 thomas static const struct got_error *
160 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 2183bbf6 2022-01-23 thomas struct got_reference* re2)
162 2183bbf6 2022-01-23 thomas {
163 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
164 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
165 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
166 2183bbf6 2022-01-23 thomas
167 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
168 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
171 2183bbf6 2022-01-23 thomas *cmp = -1;
172 2183bbf6 2022-01-23 thomas return NULL;
173 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
174 2183bbf6 2022-01-23 thomas *cmp = 1;
175 2183bbf6 2022-01-23 thomas return NULL;
176 2183bbf6 2022-01-23 thomas }
177 2183bbf6 2022-01-23 thomas
178 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
179 2183bbf6 2022-01-23 thomas return NULL;
180 2183bbf6 2022-01-23 thomas }
181 51a10b52 2020-12-26 stsp
182 11b20872 2019-11-08 stsp static const struct got_error *
183 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
184 51a10b52 2020-12-26 stsp {
185 51a10b52 2020-12-26 stsp const struct got_error *err;
186 51a10b52 2020-12-26 stsp
187 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
189 3bfadbd4 2021-11-20 thomas repo);
190 51a10b52 2020-12-26 stsp if (err)
191 51a10b52 2020-12-26 stsp return err;
192 51a10b52 2020-12-26 stsp
193 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
194 51a10b52 2020-12-26 stsp repo);
195 51a10b52 2020-12-26 stsp }
196 51a10b52 2020-12-26 stsp
197 51a10b52 2020-12-26 stsp static void
198 51a10b52 2020-12-26 stsp tog_free_refs(void)
199 51a10b52 2020-12-26 stsp {
200 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
201 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
202 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
203 51a10b52 2020-12-26 stsp }
204 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
205 51a10b52 2020-12-26 stsp }
206 51a10b52 2020-12-26 stsp
207 51a10b52 2020-12-26 stsp static const struct got_error *
208 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
209 11b20872 2019-11-08 stsp int idx, short color)
210 11b20872 2019-11-08 stsp {
211 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
212 11b20872 2019-11-08 stsp struct tog_color *tc;
213 11b20872 2019-11-08 stsp int regerr = 0;
214 11b20872 2019-11-08 stsp
215 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
216 11b20872 2019-11-08 stsp return NULL;
217 11b20872 2019-11-08 stsp
218 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
219 11b20872 2019-11-08 stsp
220 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
221 11b20872 2019-11-08 stsp if (tc == NULL)
222 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
223 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
224 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
225 11b20872 2019-11-08 stsp if (regerr) {
226 11b20872 2019-11-08 stsp static char regerr_msg[512];
227 11b20872 2019-11-08 stsp static char err_msg[512];
228 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
229 11b20872 2019-11-08 stsp sizeof(regerr_msg));
230 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
231 11b20872 2019-11-08 stsp regerr_msg);
232 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
233 11b20872 2019-11-08 stsp free(tc);
234 11b20872 2019-11-08 stsp return err;
235 11b20872 2019-11-08 stsp }
236 11b20872 2019-11-08 stsp tc->colorpair = idx;
237 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
238 11b20872 2019-11-08 stsp return NULL;
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 11b20872 2019-11-08 stsp static void
242 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
247 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
248 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
249 11b20872 2019-11-08 stsp regfree(&tc->regex);
250 11b20872 2019-11-08 stsp free(tc);
251 11b20872 2019-11-08 stsp }
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 ef20f542 2022-06-26 thomas static struct tog_color *
255 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
258 11b20872 2019-11-08 stsp
259 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
260 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
261 11b20872 2019-11-08 stsp return tc;
262 11b20872 2019-11-08 stsp }
263 11b20872 2019-11-08 stsp
264 11b20872 2019-11-08 stsp return NULL;
265 11b20872 2019-11-08 stsp }
266 11b20872 2019-11-08 stsp
267 11b20872 2019-11-08 stsp static int
268 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
269 11b20872 2019-11-08 stsp {
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
273 11b20872 2019-11-08 stsp return COLOR_CYAN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
275 11b20872 2019-11-08 stsp return COLOR_YELLOW;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
277 11b20872 2019-11-08 stsp return COLOR_GREEN;
278 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
280 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
282 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
283 91b8c405 2020-01-25 stsp return COLOR_CYAN;
284 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
285 11b20872 2019-11-08 stsp return COLOR_GREEN;
286 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
287 11b20872 2019-11-08 stsp return COLOR_GREEN;
288 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
289 11b20872 2019-11-08 stsp return COLOR_CYAN;
290 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
291 11b20872 2019-11-08 stsp return COLOR_YELLOW;
292 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
293 6458efa5 2020-11-24 stsp return COLOR_GREEN;
294 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
296 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
297 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
298 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
299 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
300 11b20872 2019-11-08 stsp
301 11b20872 2019-11-08 stsp return -1;
302 11b20872 2019-11-08 stsp }
303 11b20872 2019-11-08 stsp
304 11b20872 2019-11-08 stsp static int
305 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
306 11b20872 2019-11-08 stsp {
307 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
308 11b20872 2019-11-08 stsp
309 11b20872 2019-11-08 stsp if (val == NULL)
310 11b20872 2019-11-08 stsp return default_color_value(envvar);
311 15a087fe 2019-02-21 stsp
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
313 11b20872 2019-11-08 stsp return COLOR_BLACK;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
315 11b20872 2019-11-08 stsp return COLOR_RED;
316 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
317 11b20872 2019-11-08 stsp return COLOR_GREEN;
318 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
319 11b20872 2019-11-08 stsp return COLOR_YELLOW;
320 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
321 11b20872 2019-11-08 stsp return COLOR_BLUE;
322 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
323 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
324 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
325 11b20872 2019-11-08 stsp return COLOR_CYAN;
326 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
327 11b20872 2019-11-08 stsp return COLOR_WHITE;
328 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
329 11b20872 2019-11-08 stsp return -1;
330 11b20872 2019-11-08 stsp
331 11b20872 2019-11-08 stsp return default_color_value(envvar);
332 11b20872 2019-11-08 stsp }
333 11b20872 2019-11-08 stsp
334 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
335 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
336 3dbaef42 2020-11-24 stsp const char *label1, *label2;
337 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
338 19a6a6b5 2022-07-01 thomas int fd1, fd2;
339 82c78e96 2022-08-06 thomas int lineno;
340 15a087fe 2019-02-21 stsp int first_displayed_line;
341 15a087fe 2019-02-21 stsp int last_displayed_line;
342 15a087fe 2019-02-21 stsp int eof;
343 15a087fe 2019-02-21 stsp int diff_context;
344 3dbaef42 2020-11-24 stsp int ignore_whitespace;
345 64453f7e 2020-11-21 stsp int force_text_diff;
346 15a087fe 2019-02-21 stsp struct got_repository *repo;
347 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
348 fe621944 2020-11-10 stsp size_t nlines;
349 f44b1f58 2020-02-02 tracey int matched_line;
350 f44b1f58 2020-02-02 tracey int selected_line;
351 15a087fe 2019-02-21 stsp
352 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
353 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
354 b01e7d3b 2018-08-04 stsp };
355 b01e7d3b 2018-08-04 stsp
356 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
358 1a76625f 2018-10-22 stsp
359 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
360 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
361 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
362 1a76625f 2018-10-22 stsp int commits_needed;
363 fb280deb 2021-08-30 stsp int load_all;
364 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
365 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
366 1a76625f 2018-10-22 stsp const char *in_repo_path;
367 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp struct got_repository *repo;
369 1af5eddf 2022-06-23 thomas int *pack_fds;
370 1a76625f 2018-10-22 stsp int log_complete;
371 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
372 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
373 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
374 13add988 2019-10-15 stsp int *searching;
375 13add988 2019-10-15 stsp int *search_next_done;
376 13add988 2019-10-15 stsp regex_t *regex;
377 7e8004ba 2022-09-11 thomas int *limiting;
378 7e8004ba 2022-09-11 thomas int limit_match;
379 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
380 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
381 1a76625f 2018-10-22 stsp };
382 1a76625f 2018-10-22 stsp
383 1a76625f 2018-10-22 stsp struct tog_log_view_state {
384 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
385 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
386 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
387 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
388 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
389 b01e7d3b 2018-08-04 stsp int selected;
390 b01e7d3b 2018-08-04 stsp char *in_repo_path;
391 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
392 b672a97a 2020-01-27 stsp int log_branches;
393 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
394 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
395 1a76625f 2018-10-22 stsp sig_atomic_t quit;
396 1a76625f 2018-10-22 stsp pthread_t thread;
397 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
398 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
399 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
400 11b20872 2019-11-08 stsp struct tog_colors colors;
401 f69c5a46 2022-07-19 thomas int use_committer;
402 7e8004ba 2022-09-11 thomas int limit_view;
403 7e8004ba 2022-09-11 thomas regex_t limit_regex;
404 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
405 ba4f502b 2018-08-04 stsp };
406 11b20872 2019-11-08 stsp
407 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
408 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
409 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
411 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
412 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
413 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
414 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
415 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
416 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
417 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
418 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
419 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
420 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
421 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
422 ba4f502b 2018-08-04 stsp
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
425 e9424729 2018-08-04 stsp int nlines;
426 e9424729 2018-08-04 stsp
427 e9424729 2018-08-04 stsp struct tog_view *view;
428 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
429 e9424729 2018-08-04 stsp int *quit;
430 e9424729 2018-08-04 stsp };
431 e9424729 2018-08-04 stsp
432 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
433 e9424729 2018-08-04 stsp const char *path;
434 e9424729 2018-08-04 stsp struct got_repository *repo;
435 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
436 e9424729 2018-08-04 stsp int *complete;
437 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
438 fc06ba56 2019-08-22 stsp void *cancel_arg;
439 e9424729 2018-08-04 stsp };
440 e9424729 2018-08-04 stsp
441 e9424729 2018-08-04 stsp struct tog_blame {
442 e9424729 2018-08-04 stsp FILE *f;
443 be659d10 2020-11-18 stsp off_t filesize;
444 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
445 6fcac457 2018-11-19 stsp int nlines;
446 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
447 e9424729 2018-08-04 stsp pthread_t thread;
448 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
449 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
450 e9424729 2018-08-04 stsp const char *path;
451 7cd52833 2022-06-23 thomas int *pack_fds;
452 e9424729 2018-08-04 stsp };
453 e9424729 2018-08-04 stsp
454 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
455 7cbe629d 2018-08-04 stsp int first_displayed_line;
456 7cbe629d 2018-08-04 stsp int last_displayed_line;
457 7cbe629d 2018-08-04 stsp int selected_line;
458 4fc71f3b 2022-07-12 thomas int last_diffed_line;
459 7cbe629d 2018-08-04 stsp int blame_complete;
460 e5a0f69f 2018-08-18 stsp int eof;
461 e5a0f69f 2018-08-18 stsp int done;
462 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
463 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
464 e5a0f69f 2018-08-18 stsp char *path;
465 7cbe629d 2018-08-04 stsp struct got_repository *repo;
466 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
467 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
468 e9424729 2018-08-04 stsp struct tog_blame blame;
469 6c4c42e0 2019-06-24 stsp int matched_line;
470 11b20872 2019-11-08 stsp struct tog_colors colors;
471 ad80ab7b 2018-08-04 stsp };
472 ad80ab7b 2018-08-04 stsp
473 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
474 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
475 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
476 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
478 ad80ab7b 2018-08-04 stsp int selected;
479 ad80ab7b 2018-08-04 stsp };
480 ad80ab7b 2018-08-04 stsp
481 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
482 ad80ab7b 2018-08-04 stsp
483 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
484 ad80ab7b 2018-08-04 stsp char *tree_label;
485 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
486 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
487 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
489 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
490 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
491 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
492 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
493 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
494 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
495 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
496 6458efa5 2020-11-24 stsp struct tog_colors colors;
497 6458efa5 2020-11-24 stsp };
498 6458efa5 2020-11-24 stsp
499 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
500 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
501 6458efa5 2020-11-24 stsp struct got_reference *ref;
502 6458efa5 2020-11-24 stsp int idx;
503 6458efa5 2020-11-24 stsp };
504 6458efa5 2020-11-24 stsp
505 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
506 6458efa5 2020-11-24 stsp
507 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
508 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
509 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
510 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
511 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
512 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 6458efa5 2020-11-24 stsp struct got_repository *repo;
514 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
515 bddb1296 2019-11-08 stsp struct tog_colors colors;
516 fc2737d5 2022-09-15 thomas };
517 fc2737d5 2022-09-15 thomas
518 fc2737d5 2022-09-15 thomas struct tog_help_view_state {
519 fc2737d5 2022-09-15 thomas FILE *f;
520 fc2737d5 2022-09-15 thomas off_t *line_offsets;
521 fc2737d5 2022-09-15 thomas size_t nlines;
522 fc2737d5 2022-09-15 thomas int lineno;
523 fc2737d5 2022-09-15 thomas int first_displayed_line;
524 fc2737d5 2022-09-15 thomas int last_displayed_line;
525 fc2737d5 2022-09-15 thomas int eof;
526 fc2737d5 2022-09-15 thomas int matched_line;
527 fc2737d5 2022-09-15 thomas int selected_line;
528 fc2737d5 2022-09-15 thomas int all;
529 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
530 fc2737d5 2022-09-15 thomas };
531 fc2737d5 2022-09-15 thomas
532 fc2737d5 2022-09-15 thomas #define GENERATE_HELP \
533 fc2737d5 2022-09-15 thomas KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 fc2737d5 2022-09-15 thomas KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 fc2737d5 2022-09-15 thomas KEY_("k C-p Up", "Move cursor or page up one line"), \
536 fc2737d5 2022-09-15 thomas KEY_("j C-n Down", "Move cursor or page down one line"), \
537 fc2737d5 2022-09-15 thomas KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 fc2737d5 2022-09-15 thomas KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 fc2737d5 2022-09-15 thomas KEY_("C-u u", "Scroll the view up one half page"), \
540 fc2737d5 2022-09-15 thomas KEY_("C-d d", "Scroll the view down one half page"), \
541 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
542 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
543 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
544 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
545 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
546 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
547 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
548 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
549 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
550 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
551 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
552 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
553 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
554 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
555 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
556 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
557 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
558 fc2737d5 2022-09-15 thomas \
559 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
560 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
561 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
562 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
563 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
564 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
565 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
566 fc2737d5 2022-09-15 thomas " commit"), \
567 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
568 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
569 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
570 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
571 fc2737d5 2022-09-15 thomas \
572 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
573 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
574 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
575 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
576 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
577 fc2737d5 2022-09-15 thomas " data"), \
578 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
579 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
580 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
581 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
582 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
583 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
584 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
585 fc2737d5 2022-09-15 thomas \
586 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
587 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
588 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
589 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
590 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
591 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
592 fc2737d5 2022-09-15 thomas " selected line's commit"), \
593 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
594 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
595 fc2737d5 2022-09-15 thomas \
596 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
597 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
598 fc2737d5 2022-09-15 thomas " selected file"), \
599 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
600 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
601 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
602 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
603 fc2737d5 2022-09-15 thomas \
604 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
605 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
606 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
607 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
608 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
609 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
610 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
611 fc2737d5 2022-09-15 thomas
612 fc2737d5 2022-09-15 thomas struct tog_key_map {
613 fc2737d5 2022-09-15 thomas const char *keys;
614 fc2737d5 2022-09-15 thomas const char *info;
615 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
616 7cbe629d 2018-08-04 stsp };
617 7cbe629d 2018-08-04 stsp
618 b85a3496 2023-04-14 thomas /* curses io for tog regress */
619 b85a3496 2023-04-14 thomas struct tog_io {
620 b85a3496 2023-04-14 thomas FILE *cin;
621 b85a3496 2023-04-14 thomas FILE *cout;
622 b85a3496 2023-04-14 thomas FILE *f;
623 557d3365 2023-04-14 thomas } tog_io;
624 557d3365 2023-04-14 thomas static int using_mock_io;
625 b85a3496 2023-04-14 thomas
626 b85a3496 2023-04-14 thomas #define TOG_SCREEN_DUMP "SCREENDUMP"
627 b85a3496 2023-04-14 thomas #define TOG_SCREEN_DUMP_LEN (sizeof(TOG_SCREEN_DUMP) - 1)
628 b85a3496 2023-04-14 thomas #define TOG_KEY_SCRDUMP SHRT_MIN
629 b85a3496 2023-04-14 thomas
630 669b5ffa 2018-10-07 stsp /*
631 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
632 669b5ffa 2018-10-07 stsp *
633 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
634 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
635 669b5ffa 2018-10-07 stsp * there is enough screen estate.
636 669b5ffa 2018-10-07 stsp *
637 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
638 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
639 669b5ffa 2018-10-07 stsp *
640 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
641 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
642 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
643 669b5ffa 2018-10-07 stsp *
644 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
645 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
646 669b5ffa 2018-10-07 stsp */
647 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
648 669b5ffa 2018-10-07 stsp
649 cc3c9aac 2018-08-01 stsp struct tog_view {
650 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
651 26ed57b2 2018-05-19 stsp WINDOW *window;
652 26ed57b2 2018-05-19 stsp PANEL *panel;
653 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
654 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
655 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
656 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
657 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
658 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
659 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
660 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
661 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
662 9970f7fc 2020-12-03 stsp int dying;
663 669b5ffa 2018-10-07 stsp struct tog_view *parent;
664 669b5ffa 2018-10-07 stsp struct tog_view *child;
665 5dc9f4bc 2018-08-04 stsp
666 e78dc838 2020-12-04 stsp /*
667 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
668 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
669 e78dc838 2020-12-04 stsp * between parent and child.
670 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
671 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
672 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
673 e78dc838 2020-12-04 stsp * situations.
674 e78dc838 2020-12-04 stsp */
675 e78dc838 2020-12-04 stsp int focus_child;
676 e78dc838 2020-12-04 stsp
677 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
678 5dc9f4bc 2018-08-04 stsp /* type-specific state */
679 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
680 5dc9f4bc 2018-08-04 stsp union {
681 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
682 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
683 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
684 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
685 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
686 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
687 5dc9f4bc 2018-08-04 stsp } state;
688 e5a0f69f 2018-08-18 stsp
689 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
690 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
691 e78dc838 2020-12-04 stsp struct tog_view *, int);
692 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
693 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
694 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
695 60493ae3 2019-06-20 stsp
696 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
697 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
698 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
699 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
700 c0c4acc8 2021-01-24 stsp int search_started;
701 60493ae3 2019-06-20 stsp int searching;
702 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
703 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
704 60493ae3 2019-06-20 stsp int search_next_done;
705 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
706 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
707 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
708 1803e47f 2019-06-22 stsp regex_t regex;
709 41605754 2020-11-12 stsp regmatch_t regmatch;
710 f2d06bef 2023-01-23 thomas const char *action;
711 cc3c9aac 2018-08-01 stsp };
712 cd0acaa7 2018-05-20 stsp
713 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
714 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
715 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
716 78756c87 2020-11-24 stsp struct got_repository *);
717 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
718 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
719 e78dc838 2020-12-04 stsp struct tog_view *, int);
720 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
721 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
722 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
723 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
724 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
725 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
726 e5a0f69f 2018-08-18 stsp
727 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
728 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
729 78756c87 2020-11-24 stsp const char *, const char *, int);
730 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
731 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
732 e78dc838 2020-12-04 stsp struct tog_view *, int);
733 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
734 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
735 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
736 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
737 e5a0f69f 2018-08-18 stsp
738 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
739 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
740 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
741 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
742 e78dc838 2020-12-04 stsp struct tog_view *, int);
743 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
744 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
745 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
746 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
747 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
748 e5a0f69f 2018-08-18 stsp
749 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
750 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
751 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
752 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
753 e78dc838 2020-12-04 stsp struct tog_view *, int);
754 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
755 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
756 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
757 6458efa5 2020-11-24 stsp
758 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
759 6458efa5 2020-11-24 stsp struct got_repository *);
760 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
761 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
762 e78dc838 2020-12-04 stsp struct tog_view *, int);
763 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
764 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
765 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
766 2a7d3cd7 2022-09-17 thomas
767 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
768 2a7d3cd7 2022-09-17 thomas struct tog_view *);
769 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
770 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
771 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
772 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
773 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
774 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
775 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
776 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
777 25791caa 2018-10-24 stsp
778 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
779 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
780 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
781 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
782 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
783 25791caa 2018-10-24 stsp
784 25791caa 2018-10-24 stsp static void
785 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
786 25791caa 2018-10-24 stsp {
787 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
788 83baff54 2019-08-12 stsp }
789 83baff54 2019-08-12 stsp
790 83baff54 2019-08-12 stsp static void
791 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
792 83baff54 2019-08-12 stsp {
793 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
794 25791caa 2018-10-24 stsp }
795 26ed57b2 2018-05-19 stsp
796 61266923 2020-01-14 stsp static void
797 61266923 2020-01-14 stsp tog_sigcont(int signo)
798 61266923 2020-01-14 stsp {
799 61266923 2020-01-14 stsp tog_sigcont_received = 1;
800 61266923 2020-01-14 stsp }
801 61266923 2020-01-14 stsp
802 296152d1 2022-05-31 thomas static void
803 296152d1 2022-05-31 thomas tog_sigint(int signo)
804 296152d1 2022-05-31 thomas {
805 296152d1 2022-05-31 thomas tog_sigint_received = 1;
806 296152d1 2022-05-31 thomas }
807 296152d1 2022-05-31 thomas
808 296152d1 2022-05-31 thomas static void
809 296152d1 2022-05-31 thomas tog_sigterm(int signo)
810 296152d1 2022-05-31 thomas {
811 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
812 296152d1 2022-05-31 thomas }
813 296152d1 2022-05-31 thomas
814 296152d1 2022-05-31 thomas static int
815 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
816 296152d1 2022-05-31 thomas {
817 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
818 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
819 296152d1 2022-05-31 thomas }
820 296152d1 2022-05-31 thomas
821 e5a0f69f 2018-08-18 stsp static const struct got_error *
822 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
823 ea5e7bb5 2018-08-01 stsp {
824 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
825 e5a0f69f 2018-08-18 stsp
826 669b5ffa 2018-10-07 stsp if (view->child) {
827 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
828 669b5ffa 2018-10-07 stsp view->child = NULL;
829 669b5ffa 2018-10-07 stsp }
830 e5a0f69f 2018-08-18 stsp if (view->close)
831 e5a0f69f 2018-08-18 stsp err = view->close(view);
832 ea5e7bb5 2018-08-01 stsp if (view->panel)
833 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
834 ea5e7bb5 2018-08-01 stsp if (view->window)
835 ea5e7bb5 2018-08-01 stsp delwin(view->window);
836 ea5e7bb5 2018-08-01 stsp free(view);
837 f2d749db 2022-07-12 thomas return err ? err : child_err;
838 ea5e7bb5 2018-08-01 stsp }
839 ea5e7bb5 2018-08-01 stsp
840 ea5e7bb5 2018-08-01 stsp static struct tog_view *
841 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
842 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
843 ea5e7bb5 2018-08-01 stsp {
844 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
845 ea5e7bb5 2018-08-01 stsp
846 ea5e7bb5 2018-08-01 stsp if (view == NULL)
847 ea5e7bb5 2018-08-01 stsp return NULL;
848 ea5e7bb5 2018-08-01 stsp
849 d6b05b5b 2018-08-04 stsp view->type = type;
850 f7d12f7e 2018-08-01 stsp view->lines = LINES;
851 f7d12f7e 2018-08-01 stsp view->cols = COLS;
852 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
853 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
854 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
855 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
856 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
857 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
858 96a765a8 2018-08-04 stsp view_close(view);
859 ea5e7bb5 2018-08-01 stsp return NULL;
860 ea5e7bb5 2018-08-01 stsp }
861 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
862 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
863 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
864 96a765a8 2018-08-04 stsp view_close(view);
865 ea5e7bb5 2018-08-01 stsp return NULL;
866 ea5e7bb5 2018-08-01 stsp }
867 ea5e7bb5 2018-08-01 stsp
868 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
869 ea5e7bb5 2018-08-01 stsp return view;
870 cdf1ee82 2018-08-01 stsp }
871 cdf1ee82 2018-08-01 stsp
872 0cf4efb1 2018-09-29 stsp static int
873 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
874 0cf4efb1 2018-09-29 stsp {
875 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
876 0cf4efb1 2018-09-29 stsp return 0;
877 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
878 5c60c32a 2018-10-18 stsp }
879 5c60c32a 2018-10-18 stsp
880 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
881 a5d43cac 2022-07-01 thomas static int
882 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
883 a5d43cac 2022-07-01 thomas {
884 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
885 a5d43cac 2022-07-01 thomas }
886 a5d43cac 2022-07-01 thomas
887 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
888 5c60c32a 2018-10-18 stsp
889 5c60c32a 2018-10-18 stsp static const struct got_error *
890 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
891 5c60c32a 2018-10-18 stsp {
892 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
893 5c60c32a 2018-10-18 stsp
894 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
895 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
896 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
897 53d2bdd3 2022-07-10 thomas else
898 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
899 a5d43cac 2022-07-01 thomas view->begin_x = 0;
900 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
901 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
902 53d2bdd3 2022-07-10 thomas view->cols > 119)
903 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
904 53d2bdd3 2022-07-10 thomas else
905 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
906 a5d43cac 2022-07-01 thomas view->begin_y = 0;
907 a5d43cac 2022-07-01 thomas }
908 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
909 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
910 5c60c32a 2018-10-18 stsp view->lines = LINES;
911 5c60c32a 2018-10-18 stsp view->cols = COLS;
912 5c60c32a 2018-10-18 stsp err = view_resize(view);
913 5c60c32a 2018-10-18 stsp if (err)
914 5c60c32a 2018-10-18 stsp return err;
915 5c60c32a 2018-10-18 stsp
916 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
917 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
918 a5d43cac 2022-07-01 thomas
919 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
920 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
921 5c60c32a 2018-10-18 stsp
922 5c60c32a 2018-10-18 stsp return NULL;
923 5c60c32a 2018-10-18 stsp }
924 5c60c32a 2018-10-18 stsp
925 5c60c32a 2018-10-18 stsp static const struct got_error *
926 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
927 5c60c32a 2018-10-18 stsp {
928 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
929 5c60c32a 2018-10-18 stsp
930 5c60c32a 2018-10-18 stsp view->begin_x = 0;
931 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
932 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
933 5c60c32a 2018-10-18 stsp view->ncols = COLS;
934 5c60c32a 2018-10-18 stsp view->lines = LINES;
935 5c60c32a 2018-10-18 stsp view->cols = COLS;
936 5c60c32a 2018-10-18 stsp err = view_resize(view);
937 5c60c32a 2018-10-18 stsp if (err)
938 5c60c32a 2018-10-18 stsp return err;
939 5c60c32a 2018-10-18 stsp
940 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
941 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
942 5c60c32a 2018-10-18 stsp
943 5c60c32a 2018-10-18 stsp return NULL;
944 0cf4efb1 2018-09-29 stsp }
945 0cf4efb1 2018-09-29 stsp
946 5c60c32a 2018-10-18 stsp static int
947 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
948 5c60c32a 2018-10-18 stsp {
949 5c60c32a 2018-10-18 stsp return view->parent == NULL;
950 5c60c32a 2018-10-18 stsp }
951 5c60c32a 2018-10-18 stsp
952 b65b3ea0 2022-06-23 thomas static int
953 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
954 b65b3ea0 2022-06-23 thomas {
955 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
956 e44940c3 2022-07-01 thomas }
957 e44940c3 2022-07-01 thomas
958 e44940c3 2022-07-01 thomas static int
959 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
960 e44940c3 2022-07-01 thomas {
961 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
962 b65b3ea0 2022-06-23 thomas }
963 b65b3ea0 2022-06-23 thomas
964 444d5325 2022-07-03 thomas static int
965 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
966 444d5325 2022-07-03 thomas {
967 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
968 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
969 444d5325 2022-07-03 thomas }
970 444d5325 2022-07-03 thomas
971 a5d43cac 2022-07-01 thomas static void
972 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
973 a5d43cac 2022-07-01 thomas {
974 a5d43cac 2022-07-01 thomas PANEL *panel;
975 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
976 b65b3ea0 2022-06-23 thomas
977 a5d43cac 2022-07-01 thomas if (view->parent)
978 a5d43cac 2022-07-01 thomas return view_border(view->parent);
979 a5d43cac 2022-07-01 thomas
980 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
981 a5d43cac 2022-07-01 thomas if (panel == NULL)
982 a5d43cac 2022-07-01 thomas return;
983 a5d43cac 2022-07-01 thomas
984 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
985 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
986 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
987 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
988 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
989 a5d43cac 2022-07-01 thomas else
990 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
991 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
992 a5d43cac 2022-07-01 thomas }
993 a5d43cac 2022-07-01 thomas
994 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
995 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
996 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
997 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
998 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
999 a5d43cac 2022-07-01 thomas
1000 4d8c2215 2018-08-19 stsp static const struct got_error *
1001 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
1002 f7d12f7e 2018-08-01 stsp {
1003 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
1004 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
1005 f7d12f7e 2018-08-01 stsp
1006 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
1007 a5d43cac 2022-07-01 thomas
1008 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1009 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1010 0cf4efb1 2018-09-29 stsp else
1011 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1012 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1013 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1014 0cf4efb1 2018-09-29 stsp else
1015 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1016 6d0fee91 2018-08-01 stsp
1017 4918811f 2022-07-01 thomas if (view->child) {
1018 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1019 a5d43cac 2022-07-01 thomas
1020 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1021 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1022 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1023 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1024 40236d76 2022-06-23 thomas ncols = COLS;
1025 40236d76 2022-06-23 thomas
1026 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1027 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1028 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1029 5c60c32a 2018-10-18 stsp else
1030 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1031 5c60c32a 2018-10-18 stsp } else {
1032 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1033 40236d76 2022-06-23 thomas
1034 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1035 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1036 a5d43cac 2022-07-01 thomas }
1037 a5d43cac 2022-07-01 thomas /*
1038 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1039 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1040 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1041 a5d43cac 2022-07-01 thomas */
1042 a5d43cac 2022-07-01 thomas if (hs) {
1043 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1044 a5d43cac 2022-07-01 thomas if (err)
1045 a5d43cac 2022-07-01 thomas return err;
1046 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1047 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1048 a5d43cac 2022-07-01 thomas if (err)
1049 a5d43cac 2022-07-01 thomas return err;
1050 a5d43cac 2022-07-01 thomas }
1051 a5d43cac 2022-07-01 thomas view_border(view);
1052 a5d43cac 2022-07-01 thomas update_panels();
1053 a5d43cac 2022-07-01 thomas doupdate();
1054 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1055 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1056 a5d43cac 2022-07-01 thomas }
1057 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1058 40236d76 2022-06-23 thomas ncols = COLS;
1059 669b5ffa 2018-10-07 stsp
1060 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1061 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1062 ea0bff04 2022-07-19 thomas if (err)
1063 ea0bff04 2022-07-19 thomas return err;
1064 ea0bff04 2022-07-19 thomas }
1065 ea0bff04 2022-07-19 thomas
1066 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1067 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1068 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1069 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1070 40236d76 2022-06-23 thomas wclear(view->window);
1071 40236d76 2022-06-23 thomas
1072 40236d76 2022-06-23 thomas view->nlines = nlines;
1073 40236d76 2022-06-23 thomas view->ncols = ncols;
1074 40236d76 2022-06-23 thomas view->lines = LINES;
1075 40236d76 2022-06-23 thomas view->cols = COLS;
1076 40236d76 2022-06-23 thomas
1077 5c60c32a 2018-10-18 stsp return NULL;
1078 ea0bff04 2022-07-19 thomas }
1079 ea0bff04 2022-07-19 thomas
1080 ea0bff04 2022-07-19 thomas static const struct got_error *
1081 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1082 ea0bff04 2022-07-19 thomas {
1083 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1084 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1085 3a0139e8 2022-07-22 thomas int n = 0;
1086 ea0bff04 2022-07-19 thomas
1087 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1088 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1089 3a0139e8 2022-07-22 thomas
1090 ea0bff04 2022-07-19 thomas /*
1091 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1092 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1093 ea0bff04 2022-07-19 thomas */
1094 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1095 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1096 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1097 ea0bff04 2022-07-19 thomas }
1098 ea0bff04 2022-07-19 thomas
1099 ea0bff04 2022-07-19 thomas return err;
1100 97cb21cd 2022-07-12 thomas }
1101 97cb21cd 2022-07-12 thomas
1102 97cb21cd 2022-07-12 thomas static void
1103 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1104 97cb21cd 2022-07-12 thomas {
1105 97cb21cd 2022-07-12 thomas if (n == 0)
1106 97cb21cd 2022-07-12 thomas return;
1107 97cb21cd 2022-07-12 thomas
1108 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1109 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1110 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1111 97cb21cd 2022-07-12 thomas else
1112 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1113 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1114 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1115 97cb21cd 2022-07-12 thomas view->offset -= n;
1116 97cb21cd 2022-07-12 thomas else
1117 97cb21cd 2022-07-12 thomas view->offset = 0;
1118 97cb21cd 2022-07-12 thomas }
1119 53d2bdd3 2022-07-10 thomas }
1120 53d2bdd3 2022-07-10 thomas
1121 53d2bdd3 2022-07-10 thomas static const struct got_error *
1122 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1123 53d2bdd3 2022-07-10 thomas {
1124 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1125 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1126 53d2bdd3 2022-07-10 thomas
1127 53d2bdd3 2022-07-10 thomas if (view->parent)
1128 53d2bdd3 2022-07-10 thomas v = view->parent;
1129 53d2bdd3 2022-07-10 thomas else
1130 53d2bdd3 2022-07-10 thomas v = view;
1131 53d2bdd3 2022-07-10 thomas
1132 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1133 53d2bdd3 2022-07-10 thomas return NULL;
1134 53d2bdd3 2022-07-10 thomas
1135 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1136 53d2bdd3 2022-07-10 thomas
1137 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1138 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1139 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1140 53d2bdd3 2022-07-10 thomas if (view->parent)
1141 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1142 53d2bdd3 2022-07-10 thomas else
1143 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1144 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1145 53d2bdd3 2022-07-10 thomas view->count = 0;
1146 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1147 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1148 53d2bdd3 2022-07-10 thomas view->count = 0;
1149 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1150 53d2bdd3 2022-07-10 thomas }
1151 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1152 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1153 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1154 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1155 53d2bdd3 2022-07-10 thomas if (err)
1156 53d2bdd3 2022-07-10 thomas return err;
1157 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1158 53d2bdd3 2022-07-10 thomas } else {
1159 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1160 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1161 53d2bdd3 2022-07-10 thomas if (view->parent)
1162 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1163 53d2bdd3 2022-07-10 thomas else
1164 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1165 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1166 53d2bdd3 2022-07-10 thomas view->count = 0;
1167 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1168 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1169 53d2bdd3 2022-07-10 thomas view->count = 0;
1170 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1171 53d2bdd3 2022-07-10 thomas }
1172 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1173 53d2bdd3 2022-07-10 thomas }
1174 53d2bdd3 2022-07-10 thomas
1175 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1176 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1177 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1178 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1179 53d2bdd3 2022-07-10 thomas
1180 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1181 53d2bdd3 2022-07-10 thomas if (err)
1182 53d2bdd3 2022-07-10 thomas return err;
1183 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1184 53d2bdd3 2022-07-10 thomas if (err)
1185 53d2bdd3 2022-07-10 thomas return err;
1186 53d2bdd3 2022-07-10 thomas
1187 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1188 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1189 53d2bdd3 2022-07-10 thomas if (err)
1190 53d2bdd3 2022-07-10 thomas return err;
1191 53d2bdd3 2022-07-10 thomas }
1192 53d2bdd3 2022-07-10 thomas
1193 3a0139e8 2022-07-22 thomas if (v->resize)
1194 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1195 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1196 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1197 53d2bdd3 2022-07-10 thomas
1198 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1199 53d2bdd3 2022-07-10 thomas
1200 53d2bdd3 2022-07-10 thomas return err;
1201 53d2bdd3 2022-07-10 thomas }
1202 53d2bdd3 2022-07-10 thomas
1203 53d2bdd3 2022-07-10 thomas static void
1204 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1205 53d2bdd3 2022-07-10 thomas {
1206 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1207 53d2bdd3 2022-07-10 thomas
1208 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1209 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1210 669b5ffa 2018-10-07 stsp }
1211 669b5ffa 2018-10-07 stsp
1212 669b5ffa 2018-10-07 stsp static const struct got_error *
1213 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1214 669b5ffa 2018-10-07 stsp {
1215 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1216 669b5ffa 2018-10-07 stsp
1217 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1218 669b5ffa 2018-10-07 stsp return NULL;
1219 669b5ffa 2018-10-07 stsp
1220 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1221 669b5ffa 2018-10-07 stsp view->child = NULL;
1222 669b5ffa 2018-10-07 stsp return err;
1223 669b5ffa 2018-10-07 stsp }
1224 669b5ffa 2018-10-07 stsp
1225 40236d76 2022-06-23 thomas static const struct got_error *
1226 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1227 669b5ffa 2018-10-07 stsp {
1228 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1229 53d2bdd3 2022-07-10 thomas
1230 669b5ffa 2018-10-07 stsp view->child = child;
1231 669b5ffa 2018-10-07 stsp child->parent = view;
1232 40236d76 2022-06-23 thomas
1233 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1234 53d2bdd3 2022-07-10 thomas if (err)
1235 53d2bdd3 2022-07-10 thomas return err;
1236 53d2bdd3 2022-07-10 thomas
1237 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1238 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1239 53d2bdd3 2022-07-10 thomas
1240 53d2bdd3 2022-07-10 thomas return err;
1241 bfddd0d9 2018-09-29 stsp }
1242 2a31b33b 2022-07-23 thomas
1243 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1244 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1245 2a31b33b 2022-07-23 thomas
1246 2a31b33b 2022-07-23 thomas static const struct got_error *
1247 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1248 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1249 2a31b33b 2022-07-23 thomas {
1250 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1251 2a31b33b 2022-07-23 thomas const struct got_error *err;
1252 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1253 2a31b33b 2022-07-23 thomas
1254 2a31b33b 2022-07-23 thomas *requested = NULL;
1255 2a31b33b 2022-07-23 thomas
1256 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1257 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1258 bfddd0d9 2018-09-29 stsp
1259 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1260 2a31b33b 2022-07-23 thomas if (err)
1261 2a31b33b 2022-07-23 thomas return err;
1262 2a31b33b 2022-07-23 thomas
1263 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1264 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1265 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1266 2a31b33b 2022-07-23 thomas if (err)
1267 2a31b33b 2022-07-23 thomas return err;
1268 2a31b33b 2022-07-23 thomas }
1269 2a31b33b 2022-07-23 thomas
1270 2a31b33b 2022-07-23 thomas view->focussed = 0;
1271 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1272 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1273 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1274 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1275 2a31b33b 2022-07-23 thomas
1276 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1277 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1278 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1279 2a31b33b 2022-07-23 thomas if (err)
1280 2a31b33b 2022-07-23 thomas return err;
1281 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1282 2a31b33b 2022-07-23 thomas if (err)
1283 2a31b33b 2022-07-23 thomas return err;
1284 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1285 2a31b33b 2022-07-23 thomas } else
1286 2a31b33b 2022-07-23 thomas *requested = new_view;
1287 2a31b33b 2022-07-23 thomas
1288 2a31b33b 2022-07-23 thomas return NULL;
1289 2a31b33b 2022-07-23 thomas }
1290 2a31b33b 2022-07-23 thomas
1291 34bc9ec9 2019-02-22 stsp static void
1292 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1293 25791caa 2018-10-24 stsp {
1294 25791caa 2018-10-24 stsp int cols, lines;
1295 25791caa 2018-10-24 stsp struct winsize size;
1296 25791caa 2018-10-24 stsp
1297 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1298 25791caa 2018-10-24 stsp cols = 80; /* Default */
1299 25791caa 2018-10-24 stsp lines = 24;
1300 25791caa 2018-10-24 stsp } else {
1301 25791caa 2018-10-24 stsp cols = size.ws_col;
1302 25791caa 2018-10-24 stsp lines = size.ws_row;
1303 25791caa 2018-10-24 stsp }
1304 25791caa 2018-10-24 stsp resize_term(lines, cols);
1305 2b49a8ae 2019-06-22 stsp }
1306 2b49a8ae 2019-06-22 stsp
1307 2b49a8ae 2019-06-22 stsp static const struct got_error *
1308 f54d892e 2023-02-17 thomas view_search_start(struct tog_view *view, int fast_refresh)
1309 2b49a8ae 2019-06-22 stsp {
1310 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1311 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1312 2b49a8ae 2019-06-22 stsp char pattern[1024];
1313 2b49a8ae 2019-06-22 stsp int ret;
1314 c0c4acc8 2021-01-24 stsp
1315 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1316 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1317 c0c4acc8 2021-01-24 stsp view->searching = 0;
1318 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1319 c0c4acc8 2021-01-24 stsp }
1320 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1321 2b49a8ae 2019-06-22 stsp
1322 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1323 2b49a8ae 2019-06-22 stsp return NULL;
1324 2b49a8ae 2019-06-22 stsp
1325 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1326 a5d43cac 2022-07-01 thomas v = view->child;
1327 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1328 d07291c6 2022-12-30 thomas v = view->parent;
1329 2b49a8ae 2019-06-22 stsp
1330 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1331 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1332 a5d43cac 2022-07-01 thomas
1333 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1334 2b49a8ae 2019-06-22 stsp nocbreak();
1335 2b49a8ae 2019-06-22 stsp echo();
1336 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1337 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1338 2b49a8ae 2019-06-22 stsp cbreak();
1339 2b49a8ae 2019-06-22 stsp noecho();
1340 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1341 557d3365 2023-04-14 thomas if (!fast_refresh && !using_mock_io)
1342 f54d892e 2023-02-17 thomas halfdelay(10);
1343 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1344 2b49a8ae 2019-06-22 stsp return NULL;
1345 2b49a8ae 2019-06-22 stsp
1346 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1347 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1348 7c32bd05 2019-06-22 stsp if (err) {
1349 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1350 7c32bd05 2019-06-22 stsp return err;
1351 7c32bd05 2019-06-22 stsp }
1352 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1353 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1354 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1355 2b49a8ae 2019-06-22 stsp view->search_next(view);
1356 2b49a8ae 2019-06-22 stsp }
1357 2b49a8ae 2019-06-22 stsp
1358 2b49a8ae 2019-06-22 stsp return NULL;
1359 64486692 2022-07-07 thomas }
1360 64486692 2022-07-07 thomas
1361 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1362 64486692 2022-07-07 thomas static const struct got_error *
1363 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1364 64486692 2022-07-07 thomas {
1365 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1366 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1367 64486692 2022-07-07 thomas
1368 64486692 2022-07-07 thomas if (view->parent)
1369 64486692 2022-07-07 thomas v = view->parent;
1370 64486692 2022-07-07 thomas else
1371 64486692 2022-07-07 thomas v = view;
1372 64486692 2022-07-07 thomas
1373 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1374 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1375 ddbc4d37 2022-07-12 thomas else
1376 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1377 64486692 2022-07-07 thomas
1378 ddbc4d37 2022-07-12 thomas if (!v->child)
1379 ddbc4d37 2022-07-12 thomas return NULL;
1380 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1381 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1382 ddbc4d37 2022-07-12 thomas
1383 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1384 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1385 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1386 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1387 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1388 64486692 2022-07-07 thomas
1389 ddbc4d37 2022-07-12 thomas
1390 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1391 64486692 2022-07-07 thomas v->ncols = COLS;
1392 64486692 2022-07-07 thomas v->child->ncols = COLS;
1393 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1394 64486692 2022-07-07 thomas
1395 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1396 64486692 2022-07-07 thomas if (err)
1397 64486692 2022-07-07 thomas return err;
1398 64486692 2022-07-07 thomas }
1399 64486692 2022-07-07 thomas v->child->mode = v->mode;
1400 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1401 64486692 2022-07-07 thomas v->focus_child = 1;
1402 64486692 2022-07-07 thomas
1403 64486692 2022-07-07 thomas err = view_fullscreen(v);
1404 64486692 2022-07-07 thomas if (err)
1405 64486692 2022-07-07 thomas return err;
1406 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1407 64486692 2022-07-07 thomas if (err)
1408 64486692 2022-07-07 thomas return err;
1409 64486692 2022-07-07 thomas
1410 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1411 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1412 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1413 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1414 cf1fe301 2022-07-29 thomas if (err)
1415 cf1fe301 2022-07-29 thomas return err;
1416 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1417 cf1fe301 2022-07-29 thomas if (err)
1418 cf1fe301 2022-07-29 thomas return err;
1419 ddbc4d37 2022-07-12 thomas } else {
1420 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1421 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1422 64486692 2022-07-07 thomas }
1423 f4e6231a 2022-07-22 thomas if (v->resize)
1424 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1425 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1426 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1427 64486692 2022-07-07 thomas
1428 64486692 2022-07-07 thomas return err;
1429 0cf4efb1 2018-09-29 stsp }
1430 6d0fee91 2018-08-01 stsp
1431 07b0611c 2022-06-23 thomas /*
1432 b85a3496 2023-04-14 thomas * Strip trailing whitespace from str starting at byte *n;
1433 b85a3496 2023-04-14 thomas * if *n < 0, use strlen(str). Return new str length in *n.
1434 b85a3496 2023-04-14 thomas */
1435 b85a3496 2023-04-14 thomas static void
1436 b85a3496 2023-04-14 thomas strip_trailing_ws(char *str, int *n)
1437 b85a3496 2023-04-14 thomas {
1438 b85a3496 2023-04-14 thomas size_t x = *n;
1439 b85a3496 2023-04-14 thomas
1440 b85a3496 2023-04-14 thomas if (str == NULL || *str == '\0')
1441 b85a3496 2023-04-14 thomas return;
1442 b85a3496 2023-04-14 thomas
1443 b85a3496 2023-04-14 thomas if (x < 0)
1444 b85a3496 2023-04-14 thomas x = strlen(str);
1445 b85a3496 2023-04-14 thomas
1446 b85a3496 2023-04-14 thomas while (x-- > 0 && isspace((unsigned char)str[x]))
1447 b85a3496 2023-04-14 thomas str[x] = '\0';
1448 b85a3496 2023-04-14 thomas
1449 b85a3496 2023-04-14 thomas *n = x + 1;
1450 b85a3496 2023-04-14 thomas }
1451 b85a3496 2023-04-14 thomas
1452 b85a3496 2023-04-14 thomas /*
1453 b85a3496 2023-04-14 thomas * Extract visible substring of line y from the curses screen
1454 b85a3496 2023-04-14 thomas * and strip trailing whitespace. If vline is set and locale is
1455 b85a3496 2023-04-14 thomas * UTF-8, overwrite line[vline] with '|' because the ACS_VLINE
1456 b85a3496 2023-04-14 thomas * character is written out as 'x'. Write the line to file f.
1457 b85a3496 2023-04-14 thomas */
1458 b85a3496 2023-04-14 thomas static const struct got_error *
1459 b85a3496 2023-04-14 thomas view_write_line(FILE *f, int y, int vline)
1460 b85a3496 2023-04-14 thomas {
1461 b85a3496 2023-04-14 thomas char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1462 b85a3496 2023-04-14 thomas int r, w;
1463 b85a3496 2023-04-14 thomas
1464 b85a3496 2023-04-14 thomas r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1465 b85a3496 2023-04-14 thomas if (r == ERR)
1466 b85a3496 2023-04-14 thomas return got_error_fmt(GOT_ERR_RANGE,
1467 b85a3496 2023-04-14 thomas "failed to extract line %d", y);
1468 b85a3496 2023-04-14 thomas
1469 b85a3496 2023-04-14 thomas /*
1470 b85a3496 2023-04-14 thomas * In some views, lines are padded with blanks to COLS width.
1471 b85a3496 2023-04-14 thomas * Strip them so we can diff without the -b flag when testing.
1472 b85a3496 2023-04-14 thomas */
1473 b85a3496 2023-04-14 thomas strip_trailing_ws(line, &r);
1474 b85a3496 2023-04-14 thomas
1475 b85a3496 2023-04-14 thomas if (vline > 0 && got_locale_is_utf8())
1476 b85a3496 2023-04-14 thomas line[vline] = '|';
1477 b85a3496 2023-04-14 thomas
1478 b85a3496 2023-04-14 thomas w = fprintf(f, "%s\n", line);
1479 b85a3496 2023-04-14 thomas if (w != r + 1) /* \n */
1480 b85a3496 2023-04-14 thomas return got_ferror(f, GOT_ERR_IO);
1481 b85a3496 2023-04-14 thomas
1482 b85a3496 2023-04-14 thomas return NULL;
1483 b85a3496 2023-04-14 thomas }
1484 b85a3496 2023-04-14 thomas
1485 b85a3496 2023-04-14 thomas /*
1486 b85a3496 2023-04-14 thomas * Capture the visible curses screen by writing each line to the
1487 b85a3496 2023-04-14 thomas * file at the path set via the TOG_SCR_DUMP environment variable.
1488 b85a3496 2023-04-14 thomas */
1489 b85a3496 2023-04-14 thomas static const struct got_error *
1490 b85a3496 2023-04-14 thomas screendump(struct tog_view *view)
1491 b85a3496 2023-04-14 thomas {
1492 b85a3496 2023-04-14 thomas const struct got_error *err;
1493 b85a3496 2023-04-14 thomas FILE *f = NULL;
1494 b85a3496 2023-04-14 thomas const char *path;
1495 b85a3496 2023-04-14 thomas int i;
1496 b85a3496 2023-04-14 thomas
1497 b85a3496 2023-04-14 thomas path = getenv("TOG_SCR_DUMP");
1498 b85a3496 2023-04-14 thomas if (path == NULL || *path == '\0')
1499 b85a3496 2023-04-14 thomas return got_error_msg(GOT_ERR_BAD_PATH,
1500 b85a3496 2023-04-14 thomas "TOG_SCR_DUMP path not set to capture screen dump");
1501 b85a3496 2023-04-14 thomas f = fopen(path, "wex");
1502 b85a3496 2023-04-14 thomas if (f == NULL)
1503 b85a3496 2023-04-14 thomas return got_error_from_errno_fmt("fopen: %s", path);
1504 b85a3496 2023-04-14 thomas
1505 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_x) ||
1506 b85a3496 2023-04-14 thomas (view->parent && view->begin_x)) {
1507 b85a3496 2023-04-14 thomas int ncols = view->child ? view->ncols : view->parent->ncols;
1508 b85a3496 2023-04-14 thomas
1509 b85a3496 2023-04-14 thomas /* vertical splitscreen */
1510 b85a3496 2023-04-14 thomas for (i = 0; i < view->nlines; ++i) {
1511 b85a3496 2023-04-14 thomas err = view_write_line(f, i, ncols - 1);
1512 b85a3496 2023-04-14 thomas if (err)
1513 b85a3496 2023-04-14 thomas goto done;
1514 b85a3496 2023-04-14 thomas }
1515 b85a3496 2023-04-14 thomas } else {
1516 b85a3496 2023-04-14 thomas int hline = 0;
1517 b85a3496 2023-04-14 thomas
1518 b85a3496 2023-04-14 thomas /* fullscreen or horizontal splitscreen */
1519 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_y) ||
1520 b85a3496 2023-04-14 thomas (view->parent && view->begin_y)) /* hsplit */
1521 b85a3496 2023-04-14 thomas hline = view->child ?
1522 b85a3496 2023-04-14 thomas view->child->begin_y : view->begin_y;
1523 b85a3496 2023-04-14 thomas
1524 b85a3496 2023-04-14 thomas for (i = 0; i < view->lines; i++) {
1525 b85a3496 2023-04-14 thomas if (hline && got_locale_is_utf8() && i == hline - 1) {
1526 b85a3496 2023-04-14 thomas int c;
1527 b85a3496 2023-04-14 thomas
1528 b85a3496 2023-04-14 thomas /* ACS_HLINE writes out as 'q', overwrite it */
1529 b85a3496 2023-04-14 thomas for (c = 0; c < view->cols; ++c)
1530 b85a3496 2023-04-14 thomas fputc('-', f);
1531 b85a3496 2023-04-14 thomas fputc('\n', f);
1532 b85a3496 2023-04-14 thomas continue;
1533 b85a3496 2023-04-14 thomas }
1534 b85a3496 2023-04-14 thomas
1535 b85a3496 2023-04-14 thomas err = view_write_line(f, i, 0);
1536 b85a3496 2023-04-14 thomas if (err)
1537 b85a3496 2023-04-14 thomas goto done;
1538 b85a3496 2023-04-14 thomas }
1539 b85a3496 2023-04-14 thomas }
1540 b85a3496 2023-04-14 thomas
1541 b85a3496 2023-04-14 thomas done:
1542 b85a3496 2023-04-14 thomas if (f && fclose(f) == EOF && err == NULL)
1543 b85a3496 2023-04-14 thomas err = got_ferror(f, GOT_ERR_IO);
1544 b85a3496 2023-04-14 thomas return err;
1545 b85a3496 2023-04-14 thomas }
1546 b85a3496 2023-04-14 thomas
1547 b85a3496 2023-04-14 thomas /*
1548 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1549 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1550 07b0611c 2022-06-23 thomas */
1551 07b0611c 2022-06-23 thomas static int
1552 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1553 07b0611c 2022-06-23 thomas {
1554 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1555 a5d43cac 2022-07-01 thomas int x, n = 0;
1556 07b0611c 2022-06-23 thomas
1557 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1558 a5d43cac 2022-07-01 thomas v = view->child;
1559 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1560 a5d43cac 2022-07-01 thomas v = view->parent;
1561 a5d43cac 2022-07-01 thomas
1562 07b0611c 2022-06-23 thomas view->count = 0;
1563 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1564 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1565 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1566 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1567 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1568 07b0611c 2022-06-23 thomas
1569 07b0611c 2022-06-23 thomas do {
1570 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1571 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1572 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1573 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1574 a5d43cac 2022-07-01 thomas }
1575 a5d43cac 2022-07-01 thomas
1576 07b0611c 2022-06-23 thomas /*
1577 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1578 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1579 07b0611c 2022-06-23 thomas */
1580 07b0611c 2022-06-23 thomas if (n >= 9999999)
1581 07b0611c 2022-06-23 thomas n = 9999999;
1582 07b0611c 2022-06-23 thomas else
1583 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1584 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1585 07b0611c 2022-06-23 thomas
1586 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1587 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1588 07dd3ed3 2022-08-06 thomas n = 0;
1589 07dd3ed3 2022-08-06 thomas c = 0;
1590 07dd3ed3 2022-08-06 thomas }
1591 07dd3ed3 2022-08-06 thomas
1592 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1593 07b0611c 2022-06-23 thomas view->count = n;
1594 07b0611c 2022-06-23 thomas
1595 07b0611c 2022-06-23 thomas return c;
1596 f2d06bef 2023-01-23 thomas }
1597 f2d06bef 2023-01-23 thomas
1598 f2d06bef 2023-01-23 thomas static void
1599 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1600 f2d06bef 2023-01-23 thomas {
1601 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1602 f2d06bef 2023-01-23 thomas
1603 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1604 f2d06bef 2023-01-23 thomas v = view->child;
1605 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1606 f2d06bef 2023-01-23 thomas v = view->parent;
1607 f2d06bef 2023-01-23 thomas
1608 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1609 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1610 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1611 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1612 f2d06bef 2023-01-23 thomas
1613 f2d06bef 2023-01-23 thomas /*
1614 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1615 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1616 f2d06bef 2023-01-23 thomas */
1617 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1618 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1619 f2d06bef 2023-01-23 thomas view->action = NULL;
1620 f2d06bef 2023-01-23 thomas } else
1621 f2d06bef 2023-01-23 thomas view->action = NULL;
1622 07b0611c 2022-06-23 thomas }
1623 07b0611c 2022-06-23 thomas
1624 b85a3496 2023-04-14 thomas /*
1625 b85a3496 2023-04-14 thomas * Read the next line from the test script and assign
1626 b85a3496 2023-04-14 thomas * key instruction to *ch. If at EOF, set the *done flag.
1627 b85a3496 2023-04-14 thomas */
1628 0cf4efb1 2018-09-29 stsp static const struct got_error *
1629 b85a3496 2023-04-14 thomas tog_read_script_key(FILE *script, int *ch, int *done)
1630 b85a3496 2023-04-14 thomas {
1631 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1632 b85a3496 2023-04-14 thomas char *line = NULL;
1633 b85a3496 2023-04-14 thomas size_t linesz = 0;
1634 b85a3496 2023-04-14 thomas
1635 b85a3496 2023-04-14 thomas if (getline(&line, &linesz, script) == -1) {
1636 b85a3496 2023-04-14 thomas if (feof(script)) {
1637 b85a3496 2023-04-14 thomas *done = 1;
1638 b85a3496 2023-04-14 thomas goto done;
1639 b85a3496 2023-04-14 thomas } else {
1640 b85a3496 2023-04-14 thomas err = got_ferror(script, GOT_ERR_IO);
1641 b85a3496 2023-04-14 thomas goto done;
1642 b85a3496 2023-04-14 thomas }
1643 b85a3496 2023-04-14 thomas } else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1644 b85a3496 2023-04-14 thomas *ch = KEY_ENTER;
1645 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1646 b85a3496 2023-04-14 thomas *ch = KEY_RIGHT;
1647 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1648 b85a3496 2023-04-14 thomas *ch = KEY_LEFT;
1649 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1650 b85a3496 2023-04-14 thomas *ch = KEY_DOWN;
1651 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_UP", 6) == 0)
1652 b85a3496 2023-04-14 thomas *ch = KEY_UP;
1653 b85a3496 2023-04-14 thomas else if (strncasecmp(line, TOG_SCREEN_DUMP, TOG_SCREEN_DUMP_LEN) == 0)
1654 b85a3496 2023-04-14 thomas *ch = TOG_KEY_SCRDUMP;
1655 b85a3496 2023-04-14 thomas else
1656 b85a3496 2023-04-14 thomas *ch = *line;
1657 b85a3496 2023-04-14 thomas
1658 b85a3496 2023-04-14 thomas done:
1659 b85a3496 2023-04-14 thomas free(line);
1660 b85a3496 2023-04-14 thomas return err;
1661 b85a3496 2023-04-14 thomas }
1662 b85a3496 2023-04-14 thomas
1663 b85a3496 2023-04-14 thomas static const struct got_error *
1664 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1665 557d3365 2023-04-14 thomas struct tog_view_list_head *views, int fast_refresh)
1666 e5a0f69f 2018-08-18 stsp {
1667 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1668 669b5ffa 2018-10-07 stsp struct tog_view *v;
1669 1a76625f 2018-10-22 stsp int ch, errcode;
1670 e5a0f69f 2018-08-18 stsp
1671 e5a0f69f 2018-08-18 stsp *new = NULL;
1672 f2d06bef 2023-01-23 thomas
1673 f2d06bef 2023-01-23 thomas if (view->action)
1674 f2d06bef 2023-01-23 thomas action_report(view);
1675 8f4ed634 2020-03-26 stsp
1676 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1677 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1678 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1679 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1680 07b0611c 2022-06-23 thomas view->count = 0;
1681 07b0611c 2022-06-23 thomas }
1682 e5a0f69f 2018-08-18 stsp
1683 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1684 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1685 82954512 2020-02-03 stsp if (errcode)
1686 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1687 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1688 3da8ef85 2021-09-21 stsp sched_yield();
1689 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1690 82954512 2020-02-03 stsp if (errcode)
1691 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1692 82954512 2020-02-03 stsp "pthread_mutex_lock");
1693 60493ae3 2019-06-20 stsp view->search_next(view);
1694 60493ae3 2019-06-20 stsp return NULL;
1695 60493ae3 2019-06-20 stsp }
1696 60493ae3 2019-06-20 stsp
1697 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1698 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1699 1a76625f 2018-10-22 stsp if (errcode)
1700 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1701 b85a3496 2023-04-14 thomas
1702 557d3365 2023-04-14 thomas if (using_mock_io) {
1703 557d3365 2023-04-14 thomas err = tog_read_script_key(tog_io.f, &ch, done);
1704 b85a3496 2023-04-14 thomas if (err)
1705 b85a3496 2023-04-14 thomas return err;
1706 b85a3496 2023-04-14 thomas } else if (view->count && --view->count) {
1707 634cb454 2022-07-03 thomas cbreak();
1708 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1709 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1710 b85a3496 2023-04-14 thomas /* let C-g or backspace abort unfinished count */
1711 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1712 634cb454 2022-07-03 thomas view->count = 0;
1713 634cb454 2022-07-03 thomas else
1714 634cb454 2022-07-03 thomas ch = view->ch;
1715 634cb454 2022-07-03 thomas } else {
1716 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1717 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1718 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1719 07b0611c 2022-06-23 thomas }
1720 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1721 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1722 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1723 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1724 1a76625f 2018-10-22 stsp if (errcode)
1725 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1726 25791caa 2018-10-24 stsp
1727 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1728 25791caa 2018-10-24 stsp tog_resizeterm();
1729 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1730 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1731 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1732 25791caa 2018-10-24 stsp err = view_resize(v);
1733 25791caa 2018-10-24 stsp if (err)
1734 25791caa 2018-10-24 stsp return err;
1735 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1736 25791caa 2018-10-24 stsp if (err)
1737 25791caa 2018-10-24 stsp return err;
1738 cdfcfb03 2020-12-06 stsp if (v->child) {
1739 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1740 cdfcfb03 2020-12-06 stsp if (err)
1741 cdfcfb03 2020-12-06 stsp return err;
1742 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1743 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1744 cdfcfb03 2020-12-06 stsp if (err)
1745 cdfcfb03 2020-12-06 stsp return err;
1746 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1747 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1748 53d2bdd3 2022-07-10 thomas if (err)
1749 53d2bdd3 2022-07-10 thomas return err;
1750 53d2bdd3 2022-07-10 thomas }
1751 cdfcfb03 2020-12-06 stsp }
1752 25791caa 2018-10-24 stsp }
1753 25791caa 2018-10-24 stsp }
1754 25791caa 2018-10-24 stsp
1755 e5a0f69f 2018-08-18 stsp switch (ch) {
1756 fc2737d5 2022-09-15 thomas case '?':
1757 fc2737d5 2022-09-15 thomas case 'H':
1758 fc2737d5 2022-09-15 thomas case KEY_F(1):
1759 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1760 fc2737d5 2022-09-15 thomas err = view->reset(view);
1761 fc2737d5 2022-09-15 thomas else
1762 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1763 fc2737d5 2022-09-15 thomas break;
1764 1e37a5c2 2019-05-12 jcs case '\t':
1765 07b0611c 2022-06-23 thomas view->count = 0;
1766 1e37a5c2 2019-05-12 jcs if (view->child) {
1767 e78dc838 2020-12-04 stsp view->focussed = 0;
1768 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1769 e78dc838 2020-12-04 stsp view->focus_child = 1;
1770 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1771 e78dc838 2020-12-04 stsp view->focussed = 0;
1772 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1773 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1774 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1775 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1776 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1777 3a0139e8 2022-07-22 thomas 0);
1778 a5d43cac 2022-07-01 thomas if (err)
1779 a5d43cac 2022-07-01 thomas return err;
1780 a5d43cac 2022-07-01 thomas }
1781 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1782 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1783 a5d43cac 2022-07-01 thomas if (err)
1784 a5d43cac 2022-07-01 thomas return err;
1785 a5d43cac 2022-07-01 thomas }
1786 1e37a5c2 2019-05-12 jcs }
1787 1e37a5c2 2019-05-12 jcs break;
1788 1e37a5c2 2019-05-12 jcs case 'q':
1789 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1790 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1791 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1792 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1793 a5d43cac 2022-07-01 thomas if (err)
1794 a5d43cac 2022-07-01 thomas break;
1795 a5d43cac 2022-07-01 thomas }
1796 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1797 a5d43cac 2022-07-01 thomas }
1798 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1799 9970f7fc 2020-12-03 stsp view->dying = 1;
1800 1e37a5c2 2019-05-12 jcs break;
1801 1e37a5c2 2019-05-12 jcs case 'Q':
1802 1e37a5c2 2019-05-12 jcs *done = 1;
1803 1e37a5c2 2019-05-12 jcs break;
1804 1c5e5faa 2022-06-23 thomas case 'F':
1805 07b0611c 2022-06-23 thomas view->count = 0;
1806 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1807 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1808 1e37a5c2 2019-05-12 jcs break;
1809 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1810 e78dc838 2020-12-04 stsp view->focussed = 0;
1811 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1812 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1813 53d2bdd3 2022-07-10 thomas } else {
1814 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1815 53d2bdd3 2022-07-10 thomas if (!err)
1816 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1817 53d2bdd3 2022-07-10 thomas }
1818 1e37a5c2 2019-05-12 jcs if (err)
1819 1e37a5c2 2019-05-12 jcs break;
1820 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1821 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1822 1e37a5c2 2019-05-12 jcs } else {
1823 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1824 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1825 e78dc838 2020-12-04 stsp view->focussed = 1;
1826 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1827 1e37a5c2 2019-05-12 jcs } else {
1828 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1829 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1830 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1831 53d2bdd3 2022-07-10 thomas if (!err)
1832 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1833 669b5ffa 2018-10-07 stsp }
1834 1e37a5c2 2019-05-12 jcs if (err)
1835 1e37a5c2 2019-05-12 jcs break;
1836 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1837 a5d43cac 2022-07-01 thomas }
1838 a5d43cac 2022-07-01 thomas if (err)
1839 a5d43cac 2022-07-01 thomas break;
1840 3a0139e8 2022-07-22 thomas if (view->resize) {
1841 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1842 a5d43cac 2022-07-01 thomas if (err)
1843 a5d43cac 2022-07-01 thomas break;
1844 1e37a5c2 2019-05-12 jcs }
1845 a5d43cac 2022-07-01 thomas if (view->parent)
1846 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1847 a5d43cac 2022-07-01 thomas if (!err)
1848 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1849 1e37a5c2 2019-05-12 jcs break;
1850 64486692 2022-07-07 thomas case 'S':
1851 53d2bdd3 2022-07-10 thomas view->count = 0;
1852 64486692 2022-07-07 thomas err = switch_split(view);
1853 53d2bdd3 2022-07-10 thomas break;
1854 53d2bdd3 2022-07-10 thomas case '-':
1855 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1856 64486692 2022-07-07 thomas break;
1857 53d2bdd3 2022-07-10 thomas case '+':
1858 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1859 53d2bdd3 2022-07-10 thomas break;
1860 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1861 60493ae3 2019-06-20 stsp break;
1862 60493ae3 2019-06-20 stsp case '/':
1863 07b0611c 2022-06-23 thomas view->count = 0;
1864 60493ae3 2019-06-20 stsp if (view->search_start)
1865 f54d892e 2023-02-17 thomas view_search_start(view, fast_refresh);
1866 60493ae3 2019-06-20 stsp else
1867 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1868 1e37a5c2 2019-05-12 jcs break;
1869 b1bf1435 2019-06-21 stsp case 'N':
1870 60493ae3 2019-06-20 stsp case 'n':
1871 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1872 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1873 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1874 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1875 60493ae3 2019-06-20 stsp view->search_next(view);
1876 60493ae3 2019-06-20 stsp } else
1877 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1878 adf4c9e0 2022-07-03 thomas break;
1879 adf4c9e0 2022-07-03 thomas case 'A':
1880 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1881 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1882 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1883 f2d06bef 2023-01-23 thomas } else {
1884 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1885 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1886 f2d06bef 2023-01-23 thomas }
1887 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1888 adf4c9e0 2022-07-03 thomas if (v->reset) {
1889 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1890 adf4c9e0 2022-07-03 thomas if (err)
1891 adf4c9e0 2022-07-03 thomas return err;
1892 adf4c9e0 2022-07-03 thomas }
1893 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1894 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1895 adf4c9e0 2022-07-03 thomas if (err)
1896 adf4c9e0 2022-07-03 thomas return err;
1897 adf4c9e0 2022-07-03 thomas }
1898 adf4c9e0 2022-07-03 thomas }
1899 60493ae3 2019-06-20 stsp break;
1900 b85a3496 2023-04-14 thomas case TOG_KEY_SCRDUMP:
1901 b85a3496 2023-04-14 thomas err = screendump(view);
1902 b85a3496 2023-04-14 thomas break;
1903 1e37a5c2 2019-05-12 jcs default:
1904 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1905 1e37a5c2 2019-05-12 jcs break;
1906 e5a0f69f 2018-08-18 stsp }
1907 e5a0f69f 2018-08-18 stsp
1908 e5a0f69f 2018-08-18 stsp return err;
1909 bcbd79e2 2018-08-19 stsp }
1910 bcbd79e2 2018-08-19 stsp
1911 ef20f542 2022-06-26 thomas static int
1912 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1913 a3404814 2018-09-02 stsp {
1914 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1915 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1916 669b5ffa 2018-10-07 stsp return 0;
1917 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1918 669b5ffa 2018-10-07 stsp return 0;
1919 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1920 a3404814 2018-09-02 stsp return 0;
1921 a3404814 2018-09-02 stsp
1922 669b5ffa 2018-10-07 stsp return view->focussed;
1923 a3404814 2018-09-02 stsp }
1924 a3404814 2018-09-02 stsp
1925 bcbd79e2 2018-08-19 stsp static const struct got_error *
1926 557d3365 2023-04-14 thomas tog_io_close(void)
1927 e5a0f69f 2018-08-18 stsp {
1928 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1929 b85a3496 2023-04-14 thomas
1930 557d3365 2023-04-14 thomas if (tog_io.cin && fclose(tog_io.cin) == EOF)
1931 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cin, GOT_ERR_IO);
1932 557d3365 2023-04-14 thomas if (tog_io.cout && fclose(tog_io.cout) == EOF && err == NULL)
1933 557d3365 2023-04-14 thomas err = got_ferror(tog_io.cout, GOT_ERR_IO);
1934 557d3365 2023-04-14 thomas if (tog_io.f && fclose(tog_io.f) == EOF && err == NULL)
1935 557d3365 2023-04-14 thomas err = got_ferror(tog_io.f, GOT_ERR_IO);
1936 b85a3496 2023-04-14 thomas
1937 b85a3496 2023-04-14 thomas return err;
1938 b85a3496 2023-04-14 thomas }
1939 b85a3496 2023-04-14 thomas
1940 b85a3496 2023-04-14 thomas static const struct got_error *
1941 557d3365 2023-04-14 thomas view_loop(struct tog_view *view)
1942 b85a3496 2023-04-14 thomas {
1943 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1944 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1945 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1946 64486692 2022-07-07 thomas char *mode;
1947 fd823528 2018-10-22 stsp int fast_refresh = 10;
1948 1a76625f 2018-10-22 stsp int done = 0, errcode;
1949 e5a0f69f 2018-08-18 stsp
1950 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1951 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1952 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1953 64486692 2022-07-07 thomas else
1954 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1955 64486692 2022-07-07 thomas
1956 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1957 1a76625f 2018-10-22 stsp if (errcode)
1958 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1959 1a76625f 2018-10-22 stsp
1960 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1961 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1962 e5a0f69f 2018-08-18 stsp
1963 1004088d 2018-09-29 stsp view->focussed = 1;
1964 878940b7 2018-09-29 stsp err = view->show(view);
1965 0cf4efb1 2018-09-29 stsp if (err)
1966 0cf4efb1 2018-09-29 stsp return err;
1967 0cf4efb1 2018-09-29 stsp update_panels();
1968 0cf4efb1 2018-09-29 stsp doupdate();
1969 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1970 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1971 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1972 557d3365 2023-04-14 thomas if (fast_refresh && --fast_refresh == 0 && !using_mock_io)
1973 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1974 fd823528 2018-10-22 stsp
1975 557d3365 2023-04-14 thomas err = view_input(&new_view, &done, view, &views, fast_refresh);
1976 e5a0f69f 2018-08-18 stsp if (err)
1977 e5a0f69f 2018-08-18 stsp break;
1978 6ec3d39c 2023-01-23 thomas
1979 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
1980 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
1981 6ec3d39c 2023-01-23 thomas done = 1;
1982 6ec3d39c 2023-01-23 thomas if (done) {
1983 6ec3d39c 2023-01-23 thomas struct tog_view *v;
1984 6ec3d39c 2023-01-23 thomas
1985 6ec3d39c 2023-01-23 thomas /*
1986 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
1987 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
1988 6ec3d39c 2023-01-23 thomas */
1989 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
1990 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
1991 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
1992 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
1993 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
1994 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
1995 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
1996 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
1997 6ec3d39c 2023-01-23 thomas }
1998 6ec3d39c 2023-01-23 thomas }
1999 6ec3d39c 2023-01-23 thomas doupdate();
2000 6ec3d39c 2023-01-23 thomas }
2001 6ec3d39c 2023-01-23 thomas
2002 9970f7fc 2020-12-03 stsp if (view->dying) {
2003 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2004 669b5ffa 2018-10-07 stsp
2005 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2006 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2007 9970f7fc 2020-12-03 stsp entry);
2008 e78dc838 2020-12-04 stsp else if (view->parent)
2009 669b5ffa 2018-10-07 stsp prev = view->parent;
2010 669b5ffa 2018-10-07 stsp
2011 e78dc838 2020-12-04 stsp if (view->parent) {
2012 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2013 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2014 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
2015 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
2016 40236d76 2022-06-23 thomas err = view_resize(view->parent);
2017 40236d76 2022-06-23 thomas if (err)
2018 40236d76 2022-06-23 thomas break;
2019 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
2020 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
2021 e78dc838 2020-12-04 stsp } else
2022 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2023 669b5ffa 2018-10-07 stsp
2024 9970f7fc 2020-12-03 stsp err = view_close(view);
2025 fb59748f 2020-12-05 stsp if (err)
2026 e5a0f69f 2018-08-18 stsp goto done;
2027 669b5ffa 2018-10-07 stsp
2028 e78dc838 2020-12-04 stsp view = NULL;
2029 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2030 e78dc838 2020-12-04 stsp if (v->focussed)
2031 e78dc838 2020-12-04 stsp break;
2032 0cf4efb1 2018-09-29 stsp }
2033 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2034 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2035 e78dc838 2020-12-04 stsp if (prev)
2036 e78dc838 2020-12-04 stsp view = prev;
2037 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2038 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2039 e78dc838 2020-12-04 stsp tog_view_list_head);
2040 e78dc838 2020-12-04 stsp }
2041 e78dc838 2020-12-04 stsp if (view) {
2042 e78dc838 2020-12-04 stsp if (view->focus_child) {
2043 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2044 e78dc838 2020-12-04 stsp view = view->child;
2045 e78dc838 2020-12-04 stsp } else
2046 e78dc838 2020-12-04 stsp view->focussed = 1;
2047 e78dc838 2020-12-04 stsp }
2048 e78dc838 2020-12-04 stsp }
2049 e5a0f69f 2018-08-18 stsp }
2050 bcbd79e2 2018-08-19 stsp if (new_view) {
2051 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2052 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2053 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2054 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2055 86c66b02 2018-10-18 stsp continue;
2056 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2057 86c66b02 2018-10-18 stsp err = view_close(v);
2058 86c66b02 2018-10-18 stsp if (err)
2059 86c66b02 2018-10-18 stsp goto done;
2060 86c66b02 2018-10-18 stsp break;
2061 86c66b02 2018-10-18 stsp }
2062 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2063 fed7eaa8 2018-10-24 stsp view = new_view;
2064 7cd52833 2022-06-23 thomas }
2065 6ec3d39c 2023-01-23 thomas if (view && !done) {
2066 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2067 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2068 e78dc838 2020-12-04 stsp view = view->child;
2069 e78dc838 2020-12-04 stsp } else {
2070 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2071 e78dc838 2020-12-04 stsp view = view->parent;
2072 1a76625f 2018-10-22 stsp }
2073 e78dc838 2020-12-04 stsp show_panel(view->panel);
2074 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2075 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2076 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2077 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2078 669b5ffa 2018-10-07 stsp if (err)
2079 1a76625f 2018-10-22 stsp goto done;
2080 669b5ffa 2018-10-07 stsp }
2081 669b5ffa 2018-10-07 stsp err = view->show(view);
2082 0cf4efb1 2018-09-29 stsp if (err)
2083 1a76625f 2018-10-22 stsp goto done;
2084 669b5ffa 2018-10-07 stsp if (view->child) {
2085 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2086 669b5ffa 2018-10-07 stsp if (err)
2087 1a76625f 2018-10-22 stsp goto done;
2088 669b5ffa 2018-10-07 stsp }
2089 1a76625f 2018-10-22 stsp update_panels();
2090 1a76625f 2018-10-22 stsp doupdate();
2091 0cf4efb1 2018-09-29 stsp }
2092 e5a0f69f 2018-08-18 stsp }
2093 e5a0f69f 2018-08-18 stsp done:
2094 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2095 f2d749db 2022-07-12 thomas const struct got_error *close_err;
2096 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2097 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2098 f2d749db 2022-07-12 thomas close_err = view_close(view);
2099 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
2100 f2d749db 2022-07-12 thomas err = close_err;
2101 e5a0f69f 2018-08-18 stsp }
2102 1a76625f 2018-10-22 stsp
2103 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2104 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2105 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2106 1a76625f 2018-10-22 stsp
2107 e5a0f69f 2018-08-18 stsp return err;
2108 ea5e7bb5 2018-08-01 stsp }
2109 ea5e7bb5 2018-08-01 stsp
2110 4ed7e80c 2018-05-20 stsp __dead static void
2111 9f7d7167 2018-04-29 stsp usage_log(void)
2112 9f7d7167 2018-04-29 stsp {
2113 80ddbec8 2018-04-29 stsp endwin();
2114 c70c5802 2018-08-01 stsp fprintf(stderr,
2115 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2116 9f7d7167 2018-04-29 stsp getprogname());
2117 9f7d7167 2018-04-29 stsp exit(1);
2118 80ddbec8 2018-04-29 stsp }
2119 80ddbec8 2018-04-29 stsp
2120 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2121 80ddbec8 2018-04-29 stsp static const struct got_error *
2122 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2123 963b370f 2018-05-20 stsp {
2124 00dfcb92 2018-06-11 stsp char *vis = NULL;
2125 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2126 963b370f 2018-05-20 stsp
2127 963b370f 2018-05-20 stsp *ws = NULL;
2128 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2129 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2130 00dfcb92 2018-06-11 stsp int vislen;
2131 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2132 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2133 00dfcb92 2018-06-11 stsp
2134 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2135 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2136 00dfcb92 2018-06-11 stsp if (err)
2137 00dfcb92 2018-06-11 stsp return err;
2138 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2139 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2140 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2141 a7f50699 2018-06-11 stsp goto done;
2142 a7f50699 2018-06-11 stsp }
2143 00dfcb92 2018-06-11 stsp }
2144 963b370f 2018-05-20 stsp
2145 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2146 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2147 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2148 a7f50699 2018-06-11 stsp goto done;
2149 a7f50699 2018-06-11 stsp }
2150 963b370f 2018-05-20 stsp
2151 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2152 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2153 a7f50699 2018-06-11 stsp done:
2154 00dfcb92 2018-06-11 stsp free(vis);
2155 963b370f 2018-05-20 stsp if (err) {
2156 963b370f 2018-05-20 stsp free(*ws);
2157 963b370f 2018-05-20 stsp *ws = NULL;
2158 963b370f 2018-05-20 stsp *wlen = 0;
2159 963b370f 2018-05-20 stsp }
2160 963b370f 2018-05-20 stsp return err;
2161 05171be4 2022-06-23 thomas }
2162 05171be4 2022-06-23 thomas
2163 05171be4 2022-06-23 thomas static const struct got_error *
2164 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
2165 05171be4 2022-06-23 thomas {
2166 05171be4 2022-06-23 thomas char *dst;
2167 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
2168 05171be4 2022-06-23 thomas
2169 05171be4 2022-06-23 thomas *ptr = NULL;
2170 05171be4 2022-06-23 thomas n = len = strlen(src);
2171 83316834 2022-06-23 thomas dst = malloc(n + 1);
2172 05171be4 2022-06-23 thomas if (dst == NULL)
2173 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
2174 05171be4 2022-06-23 thomas
2175 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
2176 05171be4 2022-06-23 thomas const char c = src[idx];
2177 05171be4 2022-06-23 thomas
2178 05171be4 2022-06-23 thomas if (c == '\t') {
2179 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
2180 b235ad5d 2022-06-23 thomas char *p;
2181 b235ad5d 2022-06-23 thomas
2182 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
2183 83316834 2022-06-23 thomas if (p == NULL) {
2184 83316834 2022-06-23 thomas free(dst);
2185 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
2186 83316834 2022-06-23 thomas
2187 83316834 2022-06-23 thomas }
2188 83316834 2022-06-23 thomas dst = p;
2189 05171be4 2022-06-23 thomas n += nb;
2190 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
2191 05171be4 2022-06-23 thomas sz += nb;
2192 05171be4 2022-06-23 thomas } else
2193 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2194 05171be4 2022-06-23 thomas ++idx;
2195 05171be4 2022-06-23 thomas }
2196 05171be4 2022-06-23 thomas
2197 05171be4 2022-06-23 thomas dst[sz] = '\0';
2198 05171be4 2022-06-23 thomas *ptr = dst;
2199 05171be4 2022-06-23 thomas return NULL;
2200 963b370f 2018-05-20 stsp }
2201 963b370f 2018-05-20 stsp
2202 8d208d34 2022-06-23 thomas /*
2203 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2204 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2205 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
2206 8d208d34 2022-06-23 thomas * *rcol.
2207 f91a2b48 2022-06-23 thomas */
2208 8d208d34 2022-06-23 thomas static int
2209 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2210 8d208d34 2022-06-23 thomas {
2211 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2212 f91a2b48 2022-06-23 thomas
2213 8d208d34 2022-06-23 thomas if (n == 0) {
2214 8d208d34 2022-06-23 thomas *rcol = cols;
2215 8d208d34 2022-06-23 thomas return off;
2216 8d208d34 2022-06-23 thomas }
2217 f91a2b48 2022-06-23 thomas
2218 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2219 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2220 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2221 8d208d34 2022-06-23 thomas else
2222 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2223 f91a2b48 2022-06-23 thomas
2224 8d208d34 2022-06-23 thomas if (width == -1) {
2225 8d208d34 2022-06-23 thomas width = 1;
2226 8d208d34 2022-06-23 thomas wline[i] = L'.';
2227 f91a2b48 2022-06-23 thomas }
2228 f91a2b48 2022-06-23 thomas
2229 8d208d34 2022-06-23 thomas if (cols + width > n)
2230 8d208d34 2022-06-23 thomas break;
2231 8d208d34 2022-06-23 thomas cols += width;
2232 f91a2b48 2022-06-23 thomas }
2233 f91a2b48 2022-06-23 thomas
2234 8d208d34 2022-06-23 thomas *rcol = cols;
2235 8d208d34 2022-06-23 thomas return i;
2236 f91a2b48 2022-06-23 thomas }
2237 f91a2b48 2022-06-23 thomas
2238 f91a2b48 2022-06-23 thomas /*
2239 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2240 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2241 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2242 f91a2b48 2022-06-23 thomas */
2243 f91a2b48 2022-06-23 thomas static const struct got_error *
2244 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2245 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2246 f91a2b48 2022-06-23 thomas {
2247 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2248 8d208d34 2022-06-23 thomas int cols;
2249 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2250 05171be4 2022-06-23 thomas char *exstr = NULL;
2251 963b370f 2018-05-20 stsp size_t wlen;
2252 8d208d34 2022-06-23 thomas int i, scrollx;
2253 963b370f 2018-05-20 stsp
2254 963b370f 2018-05-20 stsp *wlinep = NULL;
2255 b700b5d6 2018-07-10 stsp *widthp = 0;
2256 963b370f 2018-05-20 stsp
2257 05171be4 2022-06-23 thomas if (expand) {
2258 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2259 05171be4 2022-06-23 thomas if (err)
2260 05171be4 2022-06-23 thomas return err;
2261 05171be4 2022-06-23 thomas }
2262 05171be4 2022-06-23 thomas
2263 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2264 05171be4 2022-06-23 thomas free(exstr);
2265 963b370f 2018-05-20 stsp if (err)
2266 963b370f 2018-05-20 stsp return err;
2267 963b370f 2018-05-20 stsp
2268 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2269 f91a2b48 2022-06-23 thomas
2270 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2271 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2272 3f670bfb 2020-12-10 stsp wlen--;
2273 3f670bfb 2020-12-10 stsp }
2274 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2275 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2276 3f670bfb 2020-12-10 stsp wlen--;
2277 3f670bfb 2020-12-10 stsp }
2278 3f670bfb 2020-12-10 stsp
2279 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2280 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2281 27a741e5 2019-09-11 stsp
2282 b700b5d6 2018-07-10 stsp if (widthp)
2283 b700b5d6 2018-07-10 stsp *widthp = cols;
2284 f91a2b48 2022-06-23 thomas if (scrollxp)
2285 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2286 963b370f 2018-05-20 stsp if (err)
2287 963b370f 2018-05-20 stsp free(wline);
2288 963b370f 2018-05-20 stsp else
2289 963b370f 2018-05-20 stsp *wlinep = wline;
2290 963b370f 2018-05-20 stsp return err;
2291 963b370f 2018-05-20 stsp }
2292 963b370f 2018-05-20 stsp
2293 8b473291 2019-02-21 stsp static const struct got_error*
2294 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2295 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2296 8b473291 2019-02-21 stsp {
2297 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2298 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2299 8b473291 2019-02-21 stsp char *s;
2300 8b473291 2019-02-21 stsp const char *name;
2301 8b473291 2019-02-21 stsp
2302 8b473291 2019-02-21 stsp *refs_str = NULL;
2303 8b473291 2019-02-21 stsp
2304 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2305 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2306 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2307 52b5abe1 2019-08-13 stsp int cmp;
2308 52b5abe1 2019-08-13 stsp
2309 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2310 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2311 8b473291 2019-02-21 stsp continue;
2312 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2313 8b473291 2019-02-21 stsp name += 5;
2314 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2315 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2316 7143d404 2019-03-12 stsp continue;
2317 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2318 8b473291 2019-02-21 stsp name += 6;
2319 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2320 8b473291 2019-02-21 stsp name += 8;
2321 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2322 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2323 79cc719f 2020-04-24 stsp continue;
2324 79cc719f 2020-04-24 stsp }
2325 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2326 48cae60d 2020-09-22 stsp if (err)
2327 48cae60d 2020-09-22 stsp break;
2328 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2329 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2330 5d844a1e 2019-08-13 stsp if (err) {
2331 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2332 48cae60d 2020-09-22 stsp free(ref_id);
2333 5d844a1e 2019-08-13 stsp break;
2334 48cae60d 2020-09-22 stsp }
2335 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2336 5d844a1e 2019-08-13 stsp err = NULL;
2337 5d844a1e 2019-08-13 stsp tag = NULL;
2338 5d844a1e 2019-08-13 stsp }
2339 52b5abe1 2019-08-13 stsp }
2340 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2341 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2342 48cae60d 2020-09-22 stsp free(ref_id);
2343 52b5abe1 2019-08-13 stsp if (tag)
2344 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2345 52b5abe1 2019-08-13 stsp if (cmp != 0)
2346 52b5abe1 2019-08-13 stsp continue;
2347 8b473291 2019-02-21 stsp s = *refs_str;
2348 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2349 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2350 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2351 8b473291 2019-02-21 stsp free(s);
2352 8b473291 2019-02-21 stsp *refs_str = NULL;
2353 8b473291 2019-02-21 stsp break;
2354 8b473291 2019-02-21 stsp }
2355 8b473291 2019-02-21 stsp free(s);
2356 8b473291 2019-02-21 stsp }
2357 8b473291 2019-02-21 stsp
2358 8b473291 2019-02-21 stsp return err;
2359 8b473291 2019-02-21 stsp }
2360 8b473291 2019-02-21 stsp
2361 963b370f 2018-05-20 stsp static const struct got_error *
2362 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2363 27a741e5 2019-09-11 stsp int col_tab_align)
2364 5813d178 2019-03-09 stsp {
2365 e6b8b890 2020-12-29 naddy char *smallerthan;
2366 5813d178 2019-03-09 stsp
2367 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2368 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2369 5813d178 2019-03-09 stsp author = smallerthan + 1;
2370 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2371 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2372 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2373 5813d178 2019-03-09 stsp }
2374 5813d178 2019-03-09 stsp
2375 5813d178 2019-03-09 stsp static const struct got_error *
2376 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2377 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2378 8fdc79fe 2020-12-01 naddy int author_display_cols)
2379 80ddbec8 2018-04-29 stsp {
2380 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2381 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2382 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2383 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2384 5813d178 2019-03-09 stsp char *author = NULL;
2385 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2386 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2387 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2388 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2389 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2390 ccb26ccd 2018-11-05 stsp struct tm tm;
2391 45d799e2 2018-12-23 stsp time_t committer_time;
2392 11b20872 2019-11-08 stsp struct tog_color *tc;
2393 80ddbec8 2018-04-29 stsp
2394 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2395 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2396 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2397 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2398 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2399 b39d25c7 2018-07-10 stsp
2400 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2401 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2402 b39d25c7 2018-07-10 stsp else
2403 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2404 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2405 11b20872 2019-11-08 stsp if (tc)
2406 11b20872 2019-11-08 stsp wattr_on(view->window,
2407 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2408 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2409 11b20872 2019-11-08 stsp if (tc)
2410 11b20872 2019-11-08 stsp wattr_off(view->window,
2411 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2412 27a741e5 2019-09-11 stsp col = limit;
2413 b39d25c7 2018-07-10 stsp if (col > avail)
2414 b39d25c7 2018-07-10 stsp goto done;
2415 6570a66d 2019-11-08 stsp
2416 6570a66d 2019-11-08 stsp if (avail >= 120) {
2417 6570a66d 2019-11-08 stsp char *id_str;
2418 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2419 6570a66d 2019-11-08 stsp if (err)
2420 6570a66d 2019-11-08 stsp goto done;
2421 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2422 11b20872 2019-11-08 stsp if (tc)
2423 11b20872 2019-11-08 stsp wattr_on(view->window,
2424 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2425 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2426 11b20872 2019-11-08 stsp if (tc)
2427 11b20872 2019-11-08 stsp wattr_off(view->window,
2428 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2429 6570a66d 2019-11-08 stsp free(id_str);
2430 6570a66d 2019-11-08 stsp col += 9;
2431 6570a66d 2019-11-08 stsp if (col > avail)
2432 6570a66d 2019-11-08 stsp goto done;
2433 6570a66d 2019-11-08 stsp }
2434 b39d25c7 2018-07-10 stsp
2435 f69c5a46 2022-07-19 thomas if (s->use_committer)
2436 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2437 f69c5a46 2022-07-19 thomas else
2438 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2439 5813d178 2019-03-09 stsp if (author == NULL) {
2440 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2441 80ddbec8 2018-04-29 stsp goto done;
2442 80ddbec8 2018-04-29 stsp }
2443 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2444 bb737323 2018-05-20 stsp if (err)
2445 bb737323 2018-05-20 stsp goto done;
2446 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2447 11b20872 2019-11-08 stsp if (tc)
2448 11b20872 2019-11-08 stsp wattr_on(view->window,
2449 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2450 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2451 bb737323 2018-05-20 stsp col += author_width;
2452 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2453 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2454 bb737323 2018-05-20 stsp col++;
2455 bb737323 2018-05-20 stsp author_width++;
2456 bb737323 2018-05-20 stsp }
2457 f31d6c3b 2022-09-09 thomas if (tc)
2458 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2459 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2460 9c2eaf34 2018-05-20 stsp if (col > avail)
2461 9c2eaf34 2018-05-20 stsp goto done;
2462 80ddbec8 2018-04-29 stsp
2463 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2464 5943eee2 2019-08-13 stsp if (err)
2465 6d9fbc00 2018-04-29 stsp goto done;
2466 bb737323 2018-05-20 stsp logmsg = logmsg0;
2467 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2468 bb737323 2018-05-20 stsp logmsg++;
2469 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2470 bb737323 2018-05-20 stsp if (newline)
2471 bb737323 2018-05-20 stsp *newline = '\0';
2472 f91a2b48 2022-06-23 thomas limit = avail - col;
2473 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2474 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2475 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2476 f91a2b48 2022-06-23 thomas limit, col, 1);
2477 331b1a16 2022-06-23 thomas if (err)
2478 331b1a16 2022-06-23 thomas goto done;
2479 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2480 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2481 27a741e5 2019-09-11 stsp while (col < avail) {
2482 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2483 bb737323 2018-05-20 stsp col++;
2484 881b2d3e 2018-04-30 stsp }
2485 80ddbec8 2018-04-29 stsp done:
2486 80ddbec8 2018-04-29 stsp free(logmsg0);
2487 bb737323 2018-05-20 stsp free(wlogmsg);
2488 5813d178 2019-03-09 stsp free(author);
2489 bb737323 2018-05-20 stsp free(wauthor);
2490 80ddbec8 2018-04-29 stsp free(line);
2491 80ddbec8 2018-04-29 stsp return err;
2492 80ddbec8 2018-04-29 stsp }
2493 26ed57b2 2018-05-19 stsp
2494 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2495 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2496 899d86c2 2018-05-10 stsp struct got_object_id *id)
2497 80ddbec8 2018-04-29 stsp {
2498 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2499 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2500 80ddbec8 2018-04-29 stsp
2501 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2502 80ddbec8 2018-04-29 stsp if (entry == NULL)
2503 899d86c2 2018-05-10 stsp return NULL;
2504 99db9666 2018-05-07 stsp
2505 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2506 d68b9737 2022-09-05 thomas if (dup == NULL) {
2507 d68b9737 2022-09-05 thomas free(entry);
2508 d68b9737 2022-09-05 thomas return NULL;
2509 d68b9737 2022-09-05 thomas }
2510 d68b9737 2022-09-05 thomas
2511 d68b9737 2022-09-05 thomas entry->id = dup;
2512 99db9666 2018-05-07 stsp entry->commit = commit;
2513 899d86c2 2018-05-10 stsp return entry;
2514 99db9666 2018-05-07 stsp }
2515 80ddbec8 2018-04-29 stsp
2516 99db9666 2018-05-07 stsp static void
2517 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2518 99db9666 2018-05-07 stsp {
2519 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2520 99db9666 2018-05-07 stsp
2521 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2522 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2523 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2524 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2525 d68b9737 2022-09-05 thomas free(entry->id);
2526 99db9666 2018-05-07 stsp free(entry);
2527 99db9666 2018-05-07 stsp }
2528 99db9666 2018-05-07 stsp
2529 99db9666 2018-05-07 stsp static void
2530 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2531 99db9666 2018-05-07 stsp {
2532 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2533 99db9666 2018-05-07 stsp pop_commit(commits);
2534 c4972b91 2018-05-07 stsp }
2535 c4972b91 2018-05-07 stsp
2536 c4972b91 2018-05-07 stsp static const struct got_error *
2537 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2538 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2539 13add988 2019-10-15 stsp {
2540 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2541 13add988 2019-10-15 stsp regmatch_t regmatch;
2542 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2543 13add988 2019-10-15 stsp
2544 13add988 2019-10-15 stsp *have_match = 0;
2545 13add988 2019-10-15 stsp
2546 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2547 13add988 2019-10-15 stsp if (err)
2548 13add988 2019-10-15 stsp return err;
2549 13add988 2019-10-15 stsp
2550 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2551 13add988 2019-10-15 stsp if (err)
2552 13add988 2019-10-15 stsp goto done;
2553 13add988 2019-10-15 stsp
2554 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2555 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2556 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2557 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2558 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2559 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2560 13add988 2019-10-15 stsp *have_match = 1;
2561 13add988 2019-10-15 stsp done:
2562 13add988 2019-10-15 stsp free(id_str);
2563 13add988 2019-10-15 stsp free(logmsg);
2564 13add988 2019-10-15 stsp return err;
2565 13add988 2019-10-15 stsp }
2566 13add988 2019-10-15 stsp
2567 13add988 2019-10-15 stsp static const struct got_error *
2568 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2569 c4972b91 2018-05-07 stsp {
2570 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2571 9ba79e04 2018-06-11 stsp
2572 1a76625f 2018-10-22 stsp /*
2573 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2574 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2575 1a76625f 2018-10-22 stsp * while updating the display.
2576 1a76625f 2018-10-22 stsp */
2577 4e0d2870 2020-12-07 naddy do {
2578 7210b715 2022-09-11 thomas struct got_object_id id;
2579 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2580 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2581 7e8004ba 2022-09-11 thomas int limit_match = 0;
2582 1a76625f 2018-10-22 stsp int errcode;
2583 899d86c2 2018-05-10 stsp
2584 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2585 4e0d2870 2020-12-07 naddy NULL, NULL);
2586 7210b715 2022-09-11 thomas if (err)
2587 ecb28ae0 2018-07-16 stsp break;
2588 899d86c2 2018-05-10 stsp
2589 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2590 9ba79e04 2018-06-11 stsp if (err)
2591 9ba79e04 2018-06-11 stsp break;
2592 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2593 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2594 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2595 9ba79e04 2018-06-11 stsp break;
2596 9ba79e04 2018-06-11 stsp }
2597 93e45b7c 2018-09-24 stsp
2598 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2599 1a76625f 2018-10-22 stsp if (errcode) {
2600 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2601 13add988 2019-10-15 stsp "pthread_mutex_lock");
2602 1a76625f 2018-10-22 stsp break;
2603 1a76625f 2018-10-22 stsp }
2604 1a76625f 2018-10-22 stsp
2605 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2606 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2607 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2608 1a76625f 2018-10-22 stsp
2609 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2610 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2611 7e8004ba 2022-09-11 thomas a->limit_regex);
2612 7e8004ba 2022-09-11 thomas if (err)
2613 7e8004ba 2022-09-11 thomas break;
2614 7e8004ba 2022-09-11 thomas
2615 7e8004ba 2022-09-11 thomas if (limit_match) {
2616 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2617 7e8004ba 2022-09-11 thomas
2618 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2619 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2620 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2621 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2622 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2623 7e8004ba 2022-09-11 thomas break;
2624 7e8004ba 2022-09-11 thomas }
2625 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2626 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2627 7e8004ba 2022-09-11 thomas
2628 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2629 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2630 7e8004ba 2022-09-11 thomas matched, entry);
2631 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2632 7e8004ba 2022-09-11 thomas }
2633 7e8004ba 2022-09-11 thomas
2634 7e8004ba 2022-09-11 thomas /*
2635 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2636 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2637 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2638 7e8004ba 2022-09-11 thomas */
2639 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2640 7e8004ba 2022-09-11 thomas }
2641 7e8004ba 2022-09-11 thomas
2642 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2643 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2644 7c1452c1 2020-03-26 stsp int have_match;
2645 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2646 7c1452c1 2020-03-26 stsp if (err)
2647 7c1452c1 2020-03-26 stsp break;
2648 7e8004ba 2022-09-11 thomas
2649 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2650 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2651 7e8004ba 2022-09-11 thomas *a->search_next_done =
2652 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2653 7e8004ba 2022-09-11 thomas } else if (have_match)
2654 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2655 13add988 2019-10-15 stsp }
2656 13add988 2019-10-15 stsp
2657 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2658 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2659 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2660 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2661 7c1452c1 2020-03-26 stsp if (err)
2662 13add988 2019-10-15 stsp break;
2663 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2664 899d86c2 2018-05-10 stsp
2665 9ba79e04 2018-06-11 stsp return err;
2666 0553a4e3 2018-04-30 stsp }
2667 0553a4e3 2018-04-30 stsp
2668 2b779855 2020-12-05 naddy static void
2669 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2670 2b779855 2020-12-05 naddy {
2671 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2672 2b779855 2020-12-05 naddy int ncommits = 0;
2673 2b779855 2020-12-05 naddy
2674 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2675 2b779855 2020-12-05 naddy while (entry) {
2676 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2677 2b779855 2020-12-05 naddy s->selected_entry = entry;
2678 2b779855 2020-12-05 naddy break;
2679 2b779855 2020-12-05 naddy }
2680 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2681 2b779855 2020-12-05 naddy ncommits++;
2682 2b779855 2020-12-05 naddy }
2683 2b779855 2020-12-05 naddy }
2684 2b779855 2020-12-05 naddy
2685 0553a4e3 2018-04-30 stsp static const struct got_error *
2686 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2687 0553a4e3 2018-04-30 stsp {
2688 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2689 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2690 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2691 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2692 60493ae3 2019-06-20 stsp int width;
2693 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2694 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2695 8b473291 2019-02-21 stsp char *refs_str = NULL;
2696 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2697 11b20872 2019-11-08 stsp struct tog_color *tc;
2698 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2699 bd3f8225 2022-08-12 thomas
2700 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2701 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2702 0553a4e3 2018-04-30 stsp
2703 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2704 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2705 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2706 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2707 1a76625f 2018-10-22 stsp if (err)
2708 ecb28ae0 2018-07-16 stsp return err;
2709 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2710 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2711 d2075bf3 2020-12-25 stsp if (refs) {
2712 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2713 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2714 d2075bf3 2020-12-25 stsp if (err)
2715 d2075bf3 2020-12-25 stsp goto done;
2716 d2075bf3 2020-12-25 stsp }
2717 867c6645 2018-07-10 stsp }
2718 359bfafd 2019-02-22 stsp
2719 557d3365 2023-04-14 thomas if (s->thread_args.commits_needed == 0 && !using_mock_io)
2720 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2721 1a76625f 2018-10-22 stsp
2722 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2723 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2724 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2725 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2726 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2727 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2728 8f4ed634 2020-03-26 stsp goto done;
2729 8f4ed634 2020-03-26 stsp }
2730 8f4ed634 2020-03-26 stsp } else {
2731 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2732 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2733 f9686aa5 2020-03-27 stsp
2734 f9686aa5 2020-03-27 stsp if (view->searching) {
2735 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2736 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2737 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2738 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2739 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2740 f9686aa5 2020-03-27 stsp search_str = "searching...";
2741 f9686aa5 2020-03-27 stsp }
2742 f9686aa5 2020-03-27 stsp
2743 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2744 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2745 7e8004ba 2022-09-11 thomas
2746 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2747 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2748 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2749 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2750 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2751 8f4ed634 2020-03-26 stsp goto done;
2752 8f4ed634 2020-03-26 stsp }
2753 8b473291 2019-02-21 stsp }
2754 1a76625f 2018-10-22 stsp
2755 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2756 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2757 91198554 2022-06-23 thomas "........................................",
2758 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2759 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2760 1a76625f 2018-10-22 stsp header = NULL;
2761 1a76625f 2018-10-22 stsp goto done;
2762 1a76625f 2018-10-22 stsp }
2763 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2764 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2765 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2766 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2767 1a76625f 2018-10-22 stsp header = NULL;
2768 1a76625f 2018-10-22 stsp goto done;
2769 ecb28ae0 2018-07-16 stsp }
2770 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2771 1a76625f 2018-10-22 stsp if (err)
2772 1a76625f 2018-10-22 stsp goto done;
2773 867c6645 2018-07-10 stsp
2774 2814baeb 2018-08-01 stsp werase(view->window);
2775 867c6645 2018-07-10 stsp
2776 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2777 a3404814 2018-09-02 stsp wstandout(view->window);
2778 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2779 11b20872 2019-11-08 stsp if (tc)
2780 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2781 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2782 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2783 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2784 1a76625f 2018-10-22 stsp width++;
2785 1a76625f 2018-10-22 stsp }
2786 86f4aab9 2022-09-09 thomas if (tc)
2787 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2788 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2789 a3404814 2018-09-02 stsp wstandend(view->window);
2790 ecb28ae0 2018-07-16 stsp free(wline);
2791 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2792 1a76625f 2018-10-22 stsp goto done;
2793 0553a4e3 2018-04-30 stsp
2794 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2795 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2796 5813d178 2019-03-09 stsp ncommits = 0;
2797 05171be4 2022-06-23 thomas view->maxx = 0;
2798 5813d178 2019-03-09 stsp while (entry) {
2799 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2800 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2801 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2802 5813d178 2019-03-09 stsp int width;
2803 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2804 5813d178 2019-03-09 stsp break;
2805 f69c5a46 2022-07-19 thomas if (s->use_committer)
2806 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2807 f69c5a46 2022-07-19 thomas else
2808 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2809 5813d178 2019-03-09 stsp if (author == NULL) {
2810 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2811 5813d178 2019-03-09 stsp goto done;
2812 5813d178 2019-03-09 stsp }
2813 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2814 27a741e5 2019-09-11 stsp date_display_cols);
2815 5813d178 2019-03-09 stsp if (author_cols < width)
2816 5813d178 2019-03-09 stsp author_cols = width;
2817 5813d178 2019-03-09 stsp free(wauthor);
2818 5813d178 2019-03-09 stsp free(author);
2819 ef944b8b 2022-07-21 thomas if (err)
2820 ef944b8b 2022-07-21 thomas goto done;
2821 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2822 05171be4 2022-06-23 thomas if (err)
2823 05171be4 2022-06-23 thomas goto done;
2824 05171be4 2022-06-23 thomas msg = msg0;
2825 05171be4 2022-06-23 thomas while (*msg == '\n')
2826 05171be4 2022-06-23 thomas ++msg;
2827 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2828 331b1a16 2022-06-23 thomas *eol = '\0';
2829 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2830 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2831 331b1a16 2022-06-23 thomas if (err)
2832 331b1a16 2022-06-23 thomas goto done;
2833 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2834 05171be4 2022-06-23 thomas free(msg0);
2835 331b1a16 2022-06-23 thomas free(wmsg);
2836 7ca04879 2019-10-19 stsp ncommits++;
2837 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2838 5813d178 2019-03-09 stsp }
2839 5813d178 2019-03-09 stsp
2840 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2841 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2842 867c6645 2018-07-10 stsp ncommits = 0;
2843 899d86c2 2018-05-10 stsp while (entry) {
2844 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2845 899d86c2 2018-05-10 stsp break;
2846 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2847 2814baeb 2018-08-01 stsp wstandout(view->window);
2848 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2849 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2850 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2851 2814baeb 2018-08-01 stsp wstandend(view->window);
2852 0553a4e3 2018-04-30 stsp if (err)
2853 60493ae3 2019-06-20 stsp goto done;
2854 0553a4e3 2018-04-30 stsp ncommits++;
2855 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2856 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2857 80ddbec8 2018-04-29 stsp }
2858 80ddbec8 2018-04-29 stsp
2859 a5d43cac 2022-07-01 thomas view_border(view);
2860 1a76625f 2018-10-22 stsp done:
2861 1a76625f 2018-10-22 stsp free(id_str);
2862 8b473291 2019-02-21 stsp free(refs_str);
2863 1a76625f 2018-10-22 stsp free(ncommits_str);
2864 1a76625f 2018-10-22 stsp free(header);
2865 80ddbec8 2018-04-29 stsp return err;
2866 9f7d7167 2018-04-29 stsp }
2867 07b55e75 2018-05-10 stsp
2868 07b55e75 2018-05-10 stsp static void
2869 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2870 07b55e75 2018-05-10 stsp {
2871 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2872 07b55e75 2018-05-10 stsp int nscrolled = 0;
2873 07b55e75 2018-05-10 stsp
2874 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2875 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2876 07b55e75 2018-05-10 stsp return;
2877 9f7d7167 2018-04-29 stsp
2878 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2879 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2880 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2881 07b55e75 2018-05-10 stsp if (entry) {
2882 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2883 07b55e75 2018-05-10 stsp nscrolled++;
2884 07b55e75 2018-05-10 stsp }
2885 07b55e75 2018-05-10 stsp }
2886 aa075928 2018-05-10 stsp }
2887 aa075928 2018-05-10 stsp
2888 aa075928 2018-05-10 stsp static const struct got_error *
2889 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2890 aa075928 2018-05-10 stsp {
2891 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2892 5e224a3e 2019-02-22 stsp int errcode;
2893 8a42fca8 2019-02-22 stsp
2894 557d3365 2023-04-14 thomas if (!using_mock_io)
2895 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while loading commits */
2896 aa075928 2018-05-10 stsp
2897 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2898 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2899 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2900 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2901 7aafa0d1 2019-02-22 stsp if (errcode)
2902 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2903 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2904 7c1452c1 2020-03-26 stsp
2905 7c1452c1 2020-03-26 stsp /*
2906 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2907 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2908 7c1452c1 2020-03-26 stsp */
2909 7c1452c1 2020-03-26 stsp if (!wait)
2910 7c1452c1 2020-03-26 stsp break;
2911 7c1452c1 2020-03-26 stsp
2912 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2913 ffe38506 2020-12-01 naddy show_log_view(view);
2914 7c1452c1 2020-03-26 stsp update_panels();
2915 7c1452c1 2020-03-26 stsp doupdate();
2916 7c1452c1 2020-03-26 stsp
2917 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2918 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2919 82954512 2020-02-03 stsp if (errcode)
2920 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2921 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2922 82954512 2020-02-03 stsp
2923 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2924 ffe38506 2020-12-01 naddy show_log_view(view);
2925 7c1452c1 2020-03-26 stsp update_panels();
2926 7c1452c1 2020-03-26 stsp doupdate();
2927 5e224a3e 2019-02-22 stsp }
2928 5e224a3e 2019-02-22 stsp
2929 5e224a3e 2019-02-22 stsp return NULL;
2930 a5d43cac 2022-07-01 thomas }
2931 a5d43cac 2022-07-01 thomas
2932 a5d43cac 2022-07-01 thomas static const struct got_error *
2933 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2934 a5d43cac 2022-07-01 thomas {
2935 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2936 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2937 fe731b51 2022-07-14 thomas
2938 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2939 fe731b51 2022-07-14 thomas return NULL;
2940 a5d43cac 2022-07-01 thomas
2941 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2942 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2943 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2944 a5d43cac 2022-07-01 thomas
2945 a5d43cac 2022-07-01 thomas return err;
2946 5e224a3e 2019-02-22 stsp }
2947 5e224a3e 2019-02-22 stsp
2948 5e224a3e 2019-02-22 stsp static const struct got_error *
2949 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2950 5e224a3e 2019-02-22 stsp {
2951 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2952 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2953 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2954 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2955 5e224a3e 2019-02-22 stsp
2956 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2957 5e224a3e 2019-02-22 stsp return NULL;
2958 5e224a3e 2019-02-22 stsp
2959 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2960 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2961 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2962 08ebd0a9 2019-02-22 stsp /*
2963 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2964 08ebd0a9 2019-02-22 stsp */
2965 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2966 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2967 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2968 5e224a3e 2019-02-22 stsp if (err)
2969 5e224a3e 2019-02-22 stsp return err;
2970 7aafa0d1 2019-02-22 stsp }
2971 b295e71b 2019-02-22 stsp
2972 7aafa0d1 2019-02-22 stsp do {
2973 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2974 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2975 88048b54 2019-02-21 stsp break;
2976 88048b54 2019-02-21 stsp
2977 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2978 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
2979 aa075928 2018-05-10 stsp
2980 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2981 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2982 dd0a52c1 2018-05-20 stsp break;
2983 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2984 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2985 aa075928 2018-05-10 stsp
2986 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2987 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2988 a5d43cac 2022-07-01 thomas else
2989 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2990 a5d43cac 2022-07-01 thomas
2991 dd0a52c1 2018-05-20 stsp return err;
2992 07b55e75 2018-05-10 stsp }
2993 4a7f7875 2018-05-10 stsp
2994 cd0acaa7 2018-05-20 stsp static const struct got_error *
2995 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2996 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2997 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2998 cd0acaa7 2018-05-20 stsp {
2999 cd0acaa7 2018-05-20 stsp const struct got_error *err;
3000 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
3001 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3002 cd0acaa7 2018-05-20 stsp
3003 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3004 15a94983 2018-12-23 stsp if (diff_view == NULL)
3005 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3006 ea5e7bb5 2018-08-01 stsp
3007 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3008 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3009 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3010 e5a0f69f 2018-08-18 stsp if (err == NULL)
3011 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3012 cd0acaa7 2018-05-20 stsp return err;
3013 4a7f7875 2018-05-10 stsp }
3014 4a7f7875 2018-05-10 stsp
3015 80ddbec8 2018-04-29 stsp static const struct got_error *
3016 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3017 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3018 9343a5fb 2018-06-23 stsp {
3019 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3020 941e9f74 2019-05-21 stsp
3021 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3022 941e9f74 2019-05-21 stsp if (parent == NULL)
3023 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3024 941e9f74 2019-05-21 stsp
3025 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3026 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3027 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3028 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3029 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3030 941e9f74 2019-05-21 stsp s->tree = subtree;
3031 941e9f74 2019-05-21 stsp s->selected = 0;
3032 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3033 941e9f74 2019-05-21 stsp return NULL;
3034 941e9f74 2019-05-21 stsp }
3035 941e9f74 2019-05-21 stsp
3036 941e9f74 2019-05-21 stsp static const struct got_error *
3037 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3038 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
3039 941e9f74 2019-05-21 stsp {
3040 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3041 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3042 941e9f74 2019-05-21 stsp const char *p;
3043 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3044 9343a5fb 2018-06-23 stsp
3045 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3046 941e9f74 2019-05-21 stsp p = path;
3047 941e9f74 2019-05-21 stsp while (*p) {
3048 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3049 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3050 56e0773d 2019-11-28 stsp char *te_name;
3051 33cbf02b 2020-01-12 stsp
3052 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3053 33cbf02b 2020-01-12 stsp p++;
3054 941e9f74 2019-05-21 stsp
3055 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3056 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3057 941e9f74 2019-05-21 stsp if (slash == NULL)
3058 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3059 33cbf02b 2020-01-12 stsp else
3060 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3061 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3062 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3063 56e0773d 2019-11-28 stsp break;
3064 941e9f74 2019-05-21 stsp }
3065 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3066 56e0773d 2019-11-28 stsp if (te == NULL) {
3067 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3068 56e0773d 2019-11-28 stsp free(te_name);
3069 941e9f74 2019-05-21 stsp break;
3070 941e9f74 2019-05-21 stsp }
3071 56e0773d 2019-11-28 stsp free(te_name);
3072 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3073 941e9f74 2019-05-21 stsp
3074 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3075 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3076 b03c880f 2019-05-21 stsp
3077 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3078 941e9f74 2019-05-21 stsp if (slash)
3079 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3080 941e9f74 2019-05-21 stsp else
3081 941e9f74 2019-05-21 stsp subpath = strdup(path);
3082 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3083 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3084 941e9f74 2019-05-21 stsp break;
3085 941e9f74 2019-05-21 stsp }
3086 941e9f74 2019-05-21 stsp
3087 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
3088 941e9f74 2019-05-21 stsp subpath);
3089 941e9f74 2019-05-21 stsp if (err)
3090 941e9f74 2019-05-21 stsp break;
3091 941e9f74 2019-05-21 stsp
3092 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3093 941e9f74 2019-05-21 stsp free(tree_id);
3094 941e9f74 2019-05-21 stsp if (err)
3095 941e9f74 2019-05-21 stsp break;
3096 941e9f74 2019-05-21 stsp
3097 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3098 941e9f74 2019-05-21 stsp if (err) {
3099 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3100 941e9f74 2019-05-21 stsp break;
3101 941e9f74 2019-05-21 stsp }
3102 941e9f74 2019-05-21 stsp if (slash == NULL)
3103 941e9f74 2019-05-21 stsp break;
3104 941e9f74 2019-05-21 stsp free(subpath);
3105 941e9f74 2019-05-21 stsp subpath = NULL;
3106 941e9f74 2019-05-21 stsp p = slash;
3107 941e9f74 2019-05-21 stsp }
3108 941e9f74 2019-05-21 stsp
3109 941e9f74 2019-05-21 stsp free(subpath);
3110 1a76625f 2018-10-22 stsp return err;
3111 61266923 2020-01-14 stsp }
3112 61266923 2020-01-14 stsp
3113 61266923 2020-01-14 stsp static const struct got_error *
3114 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3115 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3116 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3117 55cccc34 2020-02-20 stsp {
3118 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3119 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3120 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3121 55cccc34 2020-02-20 stsp
3122 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3123 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3124 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3125 55cccc34 2020-02-20 stsp
3126 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3127 bc573f3b 2021-07-10 stsp if (err)
3128 55cccc34 2020-02-20 stsp return err;
3129 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3130 55cccc34 2020-02-20 stsp
3131 55cccc34 2020-02-20 stsp *new_view = tree_view;
3132 55cccc34 2020-02-20 stsp
3133 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3134 55cccc34 2020-02-20 stsp return NULL;
3135 55cccc34 2020-02-20 stsp
3136 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
3137 55cccc34 2020-02-20 stsp }
3138 55cccc34 2020-02-20 stsp
3139 55cccc34 2020-02-20 stsp static const struct got_error *
3140 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3141 61266923 2020-01-14 stsp {
3142 61266923 2020-01-14 stsp sigset_t sigset;
3143 61266923 2020-01-14 stsp int errcode;
3144 61266923 2020-01-14 stsp
3145 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3146 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3147 61266923 2020-01-14 stsp
3148 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3149 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3150 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3151 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3152 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3153 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
3154 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3155 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
3156 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3157 61266923 2020-01-14 stsp
3158 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3159 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3160 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3161 61266923 2020-01-14 stsp
3162 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3163 61266923 2020-01-14 stsp if (errcode)
3164 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3165 61266923 2020-01-14 stsp
3166 61266923 2020-01-14 stsp return NULL;
3167 1a76625f 2018-10-22 stsp }
3168 1a76625f 2018-10-22 stsp
3169 1a76625f 2018-10-22 stsp static void *
3170 1a76625f 2018-10-22 stsp log_thread(void *arg)
3171 1a76625f 2018-10-22 stsp {
3172 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3173 1a76625f 2018-10-22 stsp int errcode = 0;
3174 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3175 1a76625f 2018-10-22 stsp int done = 0;
3176 61266923 2020-01-14 stsp
3177 f2d749db 2022-07-12 thomas /*
3178 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
3179 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
3180 f2d749db 2022-07-12 thomas */
3181 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
3182 f2d749db 2022-07-12 thomas if (errcode) {
3183 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
3184 61266923 2020-01-14 stsp return (void *)err;
3185 f2d749db 2022-07-12 thomas }
3186 1a76625f 2018-10-22 stsp
3187 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
3188 f2d749db 2022-07-12 thomas if (err) {
3189 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3190 f2d749db 2022-07-12 thomas goto done;
3191 f2d749db 2022-07-12 thomas }
3192 f2d749db 2022-07-12 thomas
3193 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3194 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3195 f2d749db 2022-07-12 thomas if (errcode) {
3196 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3197 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3198 f2d749db 2022-07-12 thomas goto done;
3199 f2d749db 2022-07-12 thomas }
3200 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3201 1a76625f 2018-10-22 stsp if (err) {
3202 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3203 f2d749db 2022-07-12 thomas goto done;
3204 1a76625f 2018-10-22 stsp err = NULL;
3205 1a76625f 2018-10-22 stsp done = 1;
3206 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3207 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3208 7e8004ba 2022-09-11 thomas if (a->limit_match)
3209 7e8004ba 2022-09-11 thomas a->commits_needed--;
3210 7e8004ba 2022-09-11 thomas } else
3211 7e8004ba 2022-09-11 thomas a->commits_needed--;
3212 7e8004ba 2022-09-11 thomas }
3213 1a76625f 2018-10-22 stsp
3214 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3215 3abe8080 2019-04-10 stsp if (errcode) {
3216 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3217 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3218 f2d749db 2022-07-12 thomas goto done;
3219 3abe8080 2019-04-10 stsp } else if (*a->quit)
3220 1a76625f 2018-10-22 stsp done = 1;
3221 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3222 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3223 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3224 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3225 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3226 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3227 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3228 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3229 1a76625f 2018-10-22 stsp }
3230 1a76625f 2018-10-22 stsp
3231 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3232 7c1452c1 2020-03-26 stsp if (errcode) {
3233 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3234 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3235 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3236 f2d749db 2022-07-12 thomas goto done;
3237 7c1452c1 2020-03-26 stsp }
3238 7c1452c1 2020-03-26 stsp
3239 1a76625f 2018-10-22 stsp if (done)
3240 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3241 7c1452c1 2020-03-26 stsp else {
3242 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3243 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3244 7c1452c1 2020-03-26 stsp &tog_mutex);
3245 f2d749db 2022-07-12 thomas if (errcode) {
3246 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3247 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3248 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3249 f2d749db 2022-07-12 thomas goto done;
3250 f2d749db 2022-07-12 thomas }
3251 21355643 2020-12-06 stsp if (*a->quit)
3252 21355643 2020-12-06 stsp done = 1;
3253 7c1452c1 2020-03-26 stsp }
3254 1a76625f 2018-10-22 stsp }
3255 1a76625f 2018-10-22 stsp }
3256 3abe8080 2019-04-10 stsp a->log_complete = 1;
3257 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3258 f2d749db 2022-07-12 thomas if (errcode)
3259 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3260 f2d749db 2022-07-12 thomas done:
3261 f2d749db 2022-07-12 thomas if (err) {
3262 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3263 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3264 f2d749db 2022-07-12 thomas }
3265 1a76625f 2018-10-22 stsp return (void *)err;
3266 1a76625f 2018-10-22 stsp }
3267 1a76625f 2018-10-22 stsp
3268 1a76625f 2018-10-22 stsp static const struct got_error *
3269 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3270 1a76625f 2018-10-22 stsp {
3271 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3272 1a76625f 2018-10-22 stsp int errcode;
3273 1a76625f 2018-10-22 stsp
3274 1a76625f 2018-10-22 stsp if (s->thread) {
3275 1a76625f 2018-10-22 stsp s->quit = 1;
3276 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3277 1a76625f 2018-10-22 stsp if (errcode)
3278 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3279 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3280 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3281 1a76625f 2018-10-22 stsp if (errcode)
3282 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3283 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3284 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3285 1a76625f 2018-10-22 stsp if (errcode)
3286 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3287 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3288 1a76625f 2018-10-22 stsp if (errcode)
3289 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3290 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3291 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3292 1a76625f 2018-10-22 stsp }
3293 1a76625f 2018-10-22 stsp
3294 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3295 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3296 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3297 1af5eddf 2022-06-23 thomas }
3298 1af5eddf 2022-06-23 thomas
3299 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3300 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3301 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3302 1af5eddf 2022-06-23 thomas if (err == NULL)
3303 1af5eddf 2022-06-23 thomas err = pack_err;
3304 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3305 1a76625f 2018-10-22 stsp }
3306 1a76625f 2018-10-22 stsp
3307 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3308 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3309 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3310 1a76625f 2018-10-22 stsp }
3311 1a76625f 2018-10-22 stsp
3312 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3313 9343a5fb 2018-06-23 stsp }
3314 9343a5fb 2018-06-23 stsp
3315 9343a5fb 2018-06-23 stsp static const struct got_error *
3316 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3317 1a76625f 2018-10-22 stsp {
3318 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3319 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3320 276b94a1 2020-11-13 naddy int errcode;
3321 1a76625f 2018-10-22 stsp
3322 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3323 276b94a1 2020-11-13 naddy
3324 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3325 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3326 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3327 276b94a1 2020-11-13 naddy
3328 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3329 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3330 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3331 276b94a1 2020-11-13 naddy
3332 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3333 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3334 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3335 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3336 1a76625f 2018-10-22 stsp free(s->start_id);
3337 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3338 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3339 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3340 1a76625f 2018-10-22 stsp return err;
3341 1a76625f 2018-10-22 stsp }
3342 1a76625f 2018-10-22 stsp
3343 7e8004ba 2022-09-11 thomas /*
3344 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3345 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3346 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3347 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3348 7e8004ba 2022-09-11 thomas * works with very slight change.
3349 7e8004ba 2022-09-11 thomas */
3350 1a76625f 2018-10-22 stsp static const struct got_error *
3351 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3352 7e8004ba 2022-09-11 thomas {
3353 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3354 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3355 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3356 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3357 7e8004ba 2022-09-11 thomas char pattern[1024];
3358 7e8004ba 2022-09-11 thomas int ret;
3359 7e8004ba 2022-09-11 thomas
3360 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3361 7e8004ba 2022-09-11 thomas v = view->child;
3362 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3363 7e8004ba 2022-09-11 thomas v = view->parent;
3364 7e8004ba 2022-09-11 thomas
3365 7e8004ba 2022-09-11 thomas /* Get the pattern */
3366 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3367 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3368 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3369 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3370 7e8004ba 2022-09-11 thomas nocbreak();
3371 7e8004ba 2022-09-11 thomas echo();
3372 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3373 7e8004ba 2022-09-11 thomas cbreak();
3374 7e8004ba 2022-09-11 thomas noecho();
3375 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3376 7e8004ba 2022-09-11 thomas if (ret == ERR)
3377 7e8004ba 2022-09-11 thomas return NULL;
3378 7e8004ba 2022-09-11 thomas
3379 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3380 7e8004ba 2022-09-11 thomas /*
3381 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3382 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3383 7e8004ba 2022-09-11 thomas */
3384 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3385 7e8004ba 2022-09-11 thomas return NULL;
3386 7e8004ba 2022-09-11 thomas
3387 7e8004ba 2022-09-11 thomas /*
3388 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3389 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3390 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3391 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3392 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3393 7e8004ba 2022-09-11 thomas * the queue.
3394 7e8004ba 2022-09-11 thomas */
3395 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3396 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3397 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3398 7e8004ba 2022-09-11 thomas s->selected = 0;
3399 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3400 7e8004ba 2022-09-11 thomas
3401 7e8004ba 2022-09-11 thomas return NULL;
3402 7e8004ba 2022-09-11 thomas }
3403 7e8004ba 2022-09-11 thomas
3404 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3405 7e8004ba 2022-09-11 thomas return NULL;
3406 7e8004ba 2022-09-11 thomas
3407 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3408 7e8004ba 2022-09-11 thomas
3409 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3410 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3411 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3412 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3413 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3414 7e8004ba 2022-09-11 thomas
3415 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3416 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3417 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3418 7e8004ba 2022-09-11 thomas
3419 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3420 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3421 7e8004ba 2022-09-11 thomas int have_match = 0;
3422 7e8004ba 2022-09-11 thomas
3423 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3424 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3425 7e8004ba 2022-09-11 thomas if (err)
3426 7e8004ba 2022-09-11 thomas return err;
3427 7e8004ba 2022-09-11 thomas
3428 7e8004ba 2022-09-11 thomas if (have_match) {
3429 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3430 7e8004ba 2022-09-11 thomas
3431 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3432 7e8004ba 2022-09-11 thomas entry->id);
3433 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3434 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3435 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3436 7e8004ba 2022-09-11 thomas break;
3437 7e8004ba 2022-09-11 thomas }
3438 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3439 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3440 7e8004ba 2022-09-11 thomas
3441 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3442 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3443 7e8004ba 2022-09-11 thomas matched, entry);
3444 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3445 7e8004ba 2022-09-11 thomas }
3446 7e8004ba 2022-09-11 thomas }
3447 7e8004ba 2022-09-11 thomas
3448 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3449 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3450 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3451 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3452 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3453 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3454 7e8004ba 2022-09-11 thomas if (err)
3455 7e8004ba 2022-09-11 thomas return err;
3456 7e8004ba 2022-09-11 thomas }
3457 7e8004ba 2022-09-11 thomas
3458 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3459 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3460 7e8004ba 2022-09-11 thomas s->selected = 0;
3461 7e8004ba 2022-09-11 thomas
3462 7e8004ba 2022-09-11 thomas return NULL;
3463 7e8004ba 2022-09-11 thomas }
3464 7e8004ba 2022-09-11 thomas
3465 7e8004ba 2022-09-11 thomas static const struct got_error *
3466 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3467 60493ae3 2019-06-20 stsp {
3468 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3469 60493ae3 2019-06-20 stsp
3470 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3471 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3472 60493ae3 2019-06-20 stsp return NULL;
3473 60493ae3 2019-06-20 stsp }
3474 60493ae3 2019-06-20 stsp
3475 60493ae3 2019-06-20 stsp static const struct got_error *
3476 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3477 60493ae3 2019-06-20 stsp {
3478 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3479 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3480 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3481 60493ae3 2019-06-20 stsp
3482 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3483 f9686aa5 2020-03-27 stsp show_log_view(view);
3484 f9686aa5 2020-03-27 stsp update_panels();
3485 f9686aa5 2020-03-27 stsp doupdate();
3486 f9686aa5 2020-03-27 stsp
3487 96e2b566 2019-07-08 stsp if (s->search_entry) {
3488 13add988 2019-10-15 stsp int errcode, ch;
3489 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3490 13add988 2019-10-15 stsp if (errcode)
3491 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3492 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3493 13add988 2019-10-15 stsp ch = wgetch(view->window);
3494 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3495 13add988 2019-10-15 stsp if (errcode)
3496 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3497 13add988 2019-10-15 stsp "pthread_mutex_lock");
3498 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3499 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3500 678cbce5 2019-07-28 stsp return NULL;
3501 678cbce5 2019-07-28 stsp }
3502 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3503 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3504 96e2b566 2019-07-08 stsp else
3505 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3506 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3507 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3508 df5e841d 2022-06-23 thomas /*
3509 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3510 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3511 1f4d5162 2022-06-23 thomas * might have changed.
3512 df5e841d 2022-06-23 thomas */
3513 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3514 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3515 e7baaec8 2022-09-13 thomas else
3516 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3517 e7baaec8 2022-09-13 thomas entry);
3518 20be8d96 2019-06-21 stsp } else {
3519 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3520 20be8d96 2019-06-21 stsp }
3521 60493ae3 2019-06-20 stsp
3522 60493ae3 2019-06-20 stsp while (1) {
3523 13add988 2019-10-15 stsp int have_match = 0;
3524 13add988 2019-10-15 stsp
3525 60493ae3 2019-06-20 stsp if (entry == NULL) {
3526 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3527 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3528 f9967bca 2020-03-27 stsp view->search_next_done =
3529 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3530 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3531 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3532 f801134a 2019-06-25 stsp return NULL;
3533 60493ae3 2019-06-20 stsp }
3534 96e2b566 2019-07-08 stsp /*
3535 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3536 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3537 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3538 96e2b566 2019-07-08 stsp */
3539 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3540 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3541 60493ae3 2019-06-20 stsp }
3542 60493ae3 2019-06-20 stsp
3543 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3544 13add988 2019-10-15 stsp &view->regex);
3545 5943eee2 2019-08-13 stsp if (err)
3546 13add988 2019-10-15 stsp break;
3547 13add988 2019-10-15 stsp if (have_match) {
3548 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3549 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3550 60493ae3 2019-06-20 stsp break;
3551 60493ae3 2019-06-20 stsp }
3552 13add988 2019-10-15 stsp
3553 96e2b566 2019-07-08 stsp s->search_entry = entry;
3554 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3555 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3556 b1bf1435 2019-06-21 stsp else
3557 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3558 60493ae3 2019-06-20 stsp }
3559 60493ae3 2019-06-20 stsp
3560 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3561 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3562 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3563 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3564 60493ae3 2019-06-20 stsp if (err)
3565 60493ae3 2019-06-20 stsp return err;
3566 ead14cbe 2019-06-21 stsp cur++;
3567 ead14cbe 2019-06-21 stsp }
3568 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3569 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3570 60493ae3 2019-06-20 stsp if (err)
3571 60493ae3 2019-06-20 stsp return err;
3572 ead14cbe 2019-06-21 stsp cur--;
3573 60493ae3 2019-06-20 stsp }
3574 60493ae3 2019-06-20 stsp }
3575 60493ae3 2019-06-20 stsp
3576 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3577 96e2b566 2019-07-08 stsp
3578 60493ae3 2019-06-20 stsp return NULL;
3579 60493ae3 2019-06-20 stsp }
3580 60493ae3 2019-06-20 stsp
3581 60493ae3 2019-06-20 stsp static const struct got_error *
3582 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3583 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3584 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3585 80ddbec8 2018-04-29 stsp {
3586 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3587 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3588 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3589 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3590 1a76625f 2018-10-22 stsp int errcode;
3591 80ddbec8 2018-04-29 stsp
3592 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3593 f135c941 2020-02-20 stsp free(s->in_repo_path);
3594 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3595 51b7e1c3 2023-04-14 thomas if (s->in_repo_path == NULL) {
3596 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("strdup");
3597 51b7e1c3 2023-04-14 thomas goto done;
3598 51b7e1c3 2023-04-14 thomas }
3599 f135c941 2020-02-20 stsp }
3600 ecb28ae0 2018-07-16 stsp
3601 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3602 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3603 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3604 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3605 78756c87 2020-11-24 stsp
3606 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3607 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3608 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3609 7e8004ba 2022-09-11 thomas
3610 fb2756b9 2018-08-04 stsp s->repo = repo;
3611 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3612 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3613 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3614 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3615 9cd7cbd1 2020-12-07 stsp goto done;
3616 9cd7cbd1 2020-12-07 stsp }
3617 9cd7cbd1 2020-12-07 stsp }
3618 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3619 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3620 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3621 5036bf37 2018-09-24 stsp goto done;
3622 5036bf37 2018-09-24 stsp }
3623 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3624 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3625 e5a0f69f 2018-08-18 stsp
3626 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3627 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3628 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3629 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3630 11b20872 2019-11-08 stsp if (err)
3631 11b20872 2019-11-08 stsp goto done;
3632 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3633 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3634 11b20872 2019-11-08 stsp if (err) {
3635 11b20872 2019-11-08 stsp free_colors(&s->colors);
3636 11b20872 2019-11-08 stsp goto done;
3637 11b20872 2019-11-08 stsp }
3638 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3639 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3640 11b20872 2019-11-08 stsp if (err) {
3641 11b20872 2019-11-08 stsp free_colors(&s->colors);
3642 11b20872 2019-11-08 stsp goto done;
3643 11b20872 2019-11-08 stsp }
3644 11b20872 2019-11-08 stsp }
3645 11b20872 2019-11-08 stsp
3646 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3647 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3648 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3649 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3650 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3651 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3652 1a76625f 2018-10-22 stsp
3653 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3654 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3655 1af5eddf 2022-06-23 thomas if (err)
3656 1af5eddf 2022-06-23 thomas goto done;
3657 1af5eddf 2022-06-23 thomas }
3658 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3659 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3660 7cd52833 2022-06-23 thomas if (err)
3661 7cd52833 2022-06-23 thomas goto done;
3662 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3663 b672a97a 2020-01-27 stsp !s->log_branches);
3664 1a76625f 2018-10-22 stsp if (err)
3665 1a76625f 2018-10-22 stsp goto done;
3666 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3667 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3668 c5b78334 2020-01-12 stsp if (err)
3669 c5b78334 2020-01-12 stsp goto done;
3670 1a76625f 2018-10-22 stsp
3671 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3672 1a76625f 2018-10-22 stsp if (errcode) {
3673 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3674 1a76625f 2018-10-22 stsp goto done;
3675 1a76625f 2018-10-22 stsp }
3676 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3677 7c1452c1 2020-03-26 stsp if (errcode) {
3678 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3679 7c1452c1 2020-03-26 stsp goto done;
3680 7c1452c1 2020-03-26 stsp }
3681 1a76625f 2018-10-22 stsp
3682 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3683 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3684 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3685 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3686 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3687 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3688 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3689 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3690 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3691 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3692 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3693 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3694 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3695 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3696 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3697 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3698 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3699 ba4f502b 2018-08-04 stsp done:
3700 51b7e1c3 2023-04-14 thomas if (err) {
3701 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
3702 51b7e1c3 2023-04-14 thomas close_log_view(view);
3703 51b7e1c3 2023-04-14 thomas view_close(view);
3704 51b7e1c3 2023-04-14 thomas }
3705 ba4f502b 2018-08-04 stsp return err;
3706 ba4f502b 2018-08-04 stsp }
3707 ba4f502b 2018-08-04 stsp
3708 e5a0f69f 2018-08-18 stsp static const struct got_error *
3709 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3710 ba4f502b 2018-08-04 stsp {
3711 f2f6d207 2020-11-24 stsp const struct got_error *err;
3712 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3713 ba4f502b 2018-08-04 stsp
3714 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3715 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3716 2b380cc8 2018-10-24 stsp &s->thread_args);
3717 2b380cc8 2018-10-24 stsp if (errcode)
3718 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3719 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3720 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3721 f2f6d207 2020-11-24 stsp if (err)
3722 f2f6d207 2020-11-24 stsp return err;
3723 f2f6d207 2020-11-24 stsp }
3724 2b380cc8 2018-10-24 stsp }
3725 2b380cc8 2018-10-24 stsp
3726 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3727 b31f89ff 2022-07-01 thomas }
3728 b31f89ff 2022-07-01 thomas
3729 b31f89ff 2022-07-01 thomas static void
3730 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3731 b31f89ff 2022-07-01 thomas {
3732 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3733 b31f89ff 2022-07-01 thomas
3734 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3735 b31f89ff 2022-07-01 thomas return;
3736 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3737 7e8004ba 2022-09-11 thomas view->count = 0;
3738 b31f89ff 2022-07-01 thomas
3739 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3740 b31f89ff 2022-07-01 thomas || home)
3741 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3742 b31f89ff 2022-07-01 thomas
3743 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3744 b31f89ff 2022-07-01 thomas --s->selected;
3745 b31f89ff 2022-07-01 thomas else
3746 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3747 b31f89ff 2022-07-01 thomas
3748 b31f89ff 2022-07-01 thomas select_commit(s);
3749 b31f89ff 2022-07-01 thomas return;
3750 b31f89ff 2022-07-01 thomas }
3751 b31f89ff 2022-07-01 thomas
3752 b31f89ff 2022-07-01 thomas static const struct got_error *
3753 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3754 b31f89ff 2022-07-01 thomas {
3755 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3756 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3757 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3758 b31f89ff 2022-07-01 thomas
3759 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3760 7e8004ba 2022-09-11 thomas return NULL;
3761 7e8004ba 2022-09-11 thomas
3762 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3763 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3764 b31f89ff 2022-07-01 thomas return NULL;
3765 b31f89ff 2022-07-01 thomas
3766 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3767 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3768 b31f89ff 2022-07-01 thomas
3769 7ed048bd 2022-08-12 thomas if (!page) {
3770 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3771 b31f89ff 2022-07-01 thomas ++s->selected;
3772 b31f89ff 2022-07-01 thomas else
3773 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3774 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3775 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3776 7ed048bd 2022-08-12 thomas int n;
3777 7ed048bd 2022-08-12 thomas
3778 7ed048bd 2022-08-12 thomas s->selected = 0;
3779 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3780 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3781 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3782 7ed048bd 2022-08-12 thomas if (entry == NULL)
3783 7ed048bd 2022-08-12 thomas break;
3784 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3785 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3786 7ed048bd 2022-08-12 thomas }
3787 7ed048bd 2022-08-12 thomas if (n > 0)
3788 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3789 b31f89ff 2022-07-01 thomas } else {
3790 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3791 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3792 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3793 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3794 bd3f8225 2022-08-12 thomas else
3795 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3796 b31f89ff 2022-07-01 thomas }
3797 b31f89ff 2022-07-01 thomas if (err)
3798 b31f89ff 2022-07-01 thomas return err;
3799 b31f89ff 2022-07-01 thomas
3800 a5d43cac 2022-07-01 thomas /*
3801 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3802 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3803 a5d43cac 2022-07-01 thomas */
3804 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3805 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3806 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3807 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3808 25100026 2022-08-13 thomas }
3809 a5d43cac 2022-07-01 thomas
3810 b31f89ff 2022-07-01 thomas select_commit(s);
3811 b31f89ff 2022-07-01 thomas
3812 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3813 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3814 b31f89ff 2022-07-01 thomas view->count = 0;
3815 b31f89ff 2022-07-01 thomas
3816 b31f89ff 2022-07-01 thomas return NULL;
3817 e5a0f69f 2018-08-18 stsp }
3818 04cc582a 2018-08-01 stsp
3819 a5d43cac 2022-07-01 thomas static void
3820 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3821 a5d43cac 2022-07-01 thomas {
3822 24415785 2022-07-03 thomas *x = 0;
3823 24415785 2022-07-03 thomas *y = 0;
3824 24415785 2022-07-03 thomas
3825 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3826 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3827 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3828 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3829 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3830 53d2bdd3 2022-07-10 thomas else
3831 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3832 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3833 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3834 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3835 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3836 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3837 53d2bdd3 2022-07-10 thomas else
3838 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3839 53d2bdd3 2022-07-10 thomas }
3840 a5d43cac 2022-07-01 thomas }
3841 a5d43cac 2022-07-01 thomas
3842 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3843 e5a0f69f 2018-08-18 stsp static const struct got_error *
3844 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3845 a5d43cac 2022-07-01 thomas {
3846 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3847 a5d43cac 2022-07-01 thomas
3848 a5d43cac 2022-07-01 thomas view->nlines = y;
3849 64486692 2022-07-07 thomas view->ncols = COLS;
3850 a5d43cac 2022-07-01 thomas err = view_resize(view);
3851 a5d43cac 2022-07-01 thomas if (err)
3852 a5d43cac 2022-07-01 thomas return err;
3853 a5d43cac 2022-07-01 thomas
3854 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3855 a5d43cac 2022-07-01 thomas
3856 a5d43cac 2022-07-01 thomas return err;
3857 07dd3ed3 2022-08-06 thomas }
3858 07dd3ed3 2022-08-06 thomas
3859 07dd3ed3 2022-08-06 thomas static const struct got_error *
3860 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3861 07dd3ed3 2022-08-06 thomas {
3862 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3863 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3864 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3865 25100026 2022-08-13 thomas
3866 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3867 25100026 2022-08-13 thomas return NULL;
3868 07dd3ed3 2022-08-06 thomas
3869 07dd3ed3 2022-08-06 thomas g = view->gline;
3870 07dd3ed3 2022-08-06 thomas view->gline = 0;
3871 07dd3ed3 2022-08-06 thomas
3872 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3873 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3874 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3875 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3876 07dd3ed3 2022-08-06 thomas select_commit(s);
3877 07dd3ed3 2022-08-06 thomas return NULL;
3878 07dd3ed3 2022-08-06 thomas }
3879 07dd3ed3 2022-08-06 thomas
3880 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3881 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3882 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3883 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3884 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3885 07dd3ed3 2022-08-06 thomas if (err)
3886 07dd3ed3 2022-08-06 thomas return err;
3887 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3888 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3889 07dd3ed3 2022-08-06 thomas
3890 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3891 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3892 07dd3ed3 2022-08-06 thomas
3893 07dd3ed3 2022-08-06 thomas select_commit(s);
3894 07dd3ed3 2022-08-06 thomas return NULL;
3895 c72de8ab 2023-02-03 thomas
3896 c72de8ab 2023-02-03 thomas }
3897 c72de8ab 2023-02-03 thomas
3898 c72de8ab 2023-02-03 thomas static void
3899 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
3900 c72de8ab 2023-02-03 thomas {
3901 07dd3ed3 2022-08-06 thomas
3902 c72de8ab 2023-02-03 thomas switch (ch) {
3903 c72de8ab 2023-02-03 thomas case KEY_LEFT:
3904 c72de8ab 2023-02-03 thomas case 'h':
3905 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
3906 c72de8ab 2023-02-03 thomas if (view->x <= 0)
3907 c72de8ab 2023-02-03 thomas view->count = 0;
3908 c72de8ab 2023-02-03 thomas break;
3909 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
3910 c72de8ab 2023-02-03 thomas case 'l':
3911 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
3912 c72de8ab 2023-02-03 thomas view->x += 2;
3913 c72de8ab 2023-02-03 thomas else
3914 c72de8ab 2023-02-03 thomas view->count = 0;
3915 c72de8ab 2023-02-03 thomas break;
3916 c72de8ab 2023-02-03 thomas case '0':
3917 c72de8ab 2023-02-03 thomas view->x = 0;
3918 c72de8ab 2023-02-03 thomas break;
3919 c72de8ab 2023-02-03 thomas case '$':
3920 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3921 c72de8ab 2023-02-03 thomas view->count = 0;
3922 c72de8ab 2023-02-03 thomas break;
3923 c72de8ab 2023-02-03 thomas default:
3924 c72de8ab 2023-02-03 thomas break;
3925 c72de8ab 2023-02-03 thomas }
3926 a5d43cac 2022-07-01 thomas }
3927 a5d43cac 2022-07-01 thomas
3928 a5d43cac 2022-07-01 thomas static const struct got_error *
3929 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3930 e5a0f69f 2018-08-18 stsp {
3931 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3932 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3933 7ed048bd 2022-08-12 thomas int eos, nscroll;
3934 80ddbec8 2018-04-29 stsp
3935 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3936 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3937 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3938 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3939 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3940 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3941 fb280deb 2021-08-30 stsp }
3942 c0be8933 2022-08-12 thomas if (err)
3943 c0be8933 2022-08-12 thomas return err;
3944 528dedf3 2021-08-30 stsp }
3945 068ab281 2022-07-01 thomas
3946 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3947 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3948 068ab281 2022-07-01 thomas --eos; /* border */
3949 07dd3ed3 2022-08-06 thomas
3950 07dd3ed3 2022-08-06 thomas if (view->gline)
3951 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3952 068ab281 2022-07-01 thomas
3953 528dedf3 2021-08-30 stsp switch (ch) {
3954 7e8004ba 2022-09-11 thomas case '&':
3955 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3956 7e8004ba 2022-09-11 thomas break;
3957 1e37a5c2 2019-05-12 jcs case 'q':
3958 1e37a5c2 2019-05-12 jcs s->quit = 1;
3959 05171be4 2022-06-23 thomas break;
3960 05171be4 2022-06-23 thomas case '0':
3961 05171be4 2022-06-23 thomas case '$':
3962 05171be4 2022-06-23 thomas case KEY_RIGHT:
3963 05171be4 2022-06-23 thomas case 'l':
3964 05171be4 2022-06-23 thomas case KEY_LEFT:
3965 05171be4 2022-06-23 thomas case 'h':
3966 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
3967 05171be4 2022-06-23 thomas break;
3968 1e37a5c2 2019-05-12 jcs case 'k':
3969 1e37a5c2 2019-05-12 jcs case KEY_UP:
3970 1e37a5c2 2019-05-12 jcs case '<':
3971 1e37a5c2 2019-05-12 jcs case ',':
3972 f7140bf5 2021-10-17 thomas case CTRL('p'):
3973 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3974 912a3f79 2021-08-30 j break;
3975 912a3f79 2021-08-30 j case 'g':
3976 aa7a1117 2023-01-09 thomas case '=':
3977 912a3f79 2021-08-30 j case KEY_HOME:
3978 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3979 07b0611c 2022-06-23 thomas view->count = 0;
3980 1e37a5c2 2019-05-12 jcs break;
3981 70f17a53 2022-06-13 thomas case CTRL('u'):
3982 23427b14 2022-06-23 thomas case 'u':
3983 70f17a53 2022-06-13 thomas nscroll /= 2;
3984 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3985 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3986 a4292ac5 2019-05-12 jcs case CTRL('b'):
3987 1c5e5faa 2022-06-23 thomas case 'b':
3988 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3989 1e37a5c2 2019-05-12 jcs break;
3990 1e37a5c2 2019-05-12 jcs case 'j':
3991 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3992 1e37a5c2 2019-05-12 jcs case '>':
3993 1e37a5c2 2019-05-12 jcs case '.':
3994 f7140bf5 2021-10-17 thomas case CTRL('n'):
3995 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3996 912a3f79 2021-08-30 j break;
3997 f69c5a46 2022-07-19 thomas case '@':
3998 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3999 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
4000 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
4001 f69c5a46 2022-07-19 thomas break;
4002 912a3f79 2021-08-30 j case 'G':
4003 aa7a1117 2023-01-09 thomas case '*':
4004 912a3f79 2021-08-30 j case KEY_END: {
4005 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4006 912a3f79 2021-08-30 j * traverse them all. */
4007 07b0611c 2022-06-23 thomas view->count = 0;
4008 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
4009 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
4010 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4011 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4012 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
4013 1e37a5c2 2019-05-12 jcs break;
4014 912a3f79 2021-08-30 j }
4015 bccd1d5d 2022-06-13 thomas case CTRL('d'):
4016 23427b14 2022-06-23 thomas case 'd':
4017 70f17a53 2022-06-13 thomas nscroll /= 2;
4018 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4019 70f17a53 2022-06-13 thomas case KEY_NPAGE:
4020 1c5e5faa 2022-06-23 thomas case CTRL('f'):
4021 4c2d69cb 2022-06-23 thomas case 'f':
4022 b31f89ff 2022-07-01 thomas case ' ':
4023 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
4024 1e37a5c2 2019-05-12 jcs break;
4025 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4026 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4027 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4028 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
4029 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
4030 2b779855 2020-12-05 naddy select_commit(s);
4031 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
4032 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4033 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4034 7e8004ba 2022-09-11 thomas s->commits->ncommits;
4035 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4036 0bf7f153 2020-12-02 naddy }
4037 1e37a5c2 2019-05-12 jcs break;
4038 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4039 444d5325 2022-07-03 thomas case '\r':
4040 07b0611c 2022-06-23 thomas view->count = 0;
4041 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4042 e5a0f69f 2018-08-18 stsp break;
4043 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4044 1e37a5c2 2019-05-12 jcs break;
4045 1be4947a 2022-07-22 thomas case 'T':
4046 07b0611c 2022-06-23 thomas view->count = 0;
4047 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4048 5036bf37 2018-09-24 stsp break;
4049 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
4050 1e37a5c2 2019-05-12 jcs break;
4051 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4052 21355643 2020-12-06 stsp case CTRL('l'):
4053 21355643 2020-12-06 stsp case 'B':
4054 07b0611c 2022-06-23 thomas view->count = 0;
4055 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4056 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4057 1e37a5c2 2019-05-12 jcs break;
4058 21355643 2020-12-06 stsp err = stop_log_thread(s);
4059 74cfe85e 2020-10-20 stsp if (err)
4060 74cfe85e 2020-10-20 stsp return err;
4061 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4062 21355643 2020-12-06 stsp char *parent_path;
4063 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4064 21355643 2020-12-06 stsp if (err)
4065 21355643 2020-12-06 stsp return err;
4066 21355643 2020-12-06 stsp free(s->in_repo_path);
4067 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4068 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4069 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4070 21355643 2020-12-06 stsp struct got_object_id *start_id;
4071 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4072 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4073 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4074 466429a1 2022-11-17 thomas if (err) {
4075 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
4076 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
4077 466429a1 2022-11-17 thomas return err;
4078 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
4079 466429a1 2022-11-17 thomas free(s->head_ref_name);
4080 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
4081 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
4082 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4083 466429a1 2022-11-17 thomas &tog_refs, s->repo);
4084 466429a1 2022-11-17 thomas if (err)
4085 466429a1 2022-11-17 thomas return err;
4086 466429a1 2022-11-17 thomas }
4087 21355643 2020-12-06 stsp free(s->start_id);
4088 21355643 2020-12-06 stsp s->start_id = start_id;
4089 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4090 21355643 2020-12-06 stsp } else /* 'B' */
4091 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4092 21355643 2020-12-06 stsp
4093 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
4094 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4095 bf7e79b3 2022-06-23 thomas if (err)
4096 bf7e79b3 2022-06-23 thomas return err;
4097 bf7e79b3 2022-06-23 thomas }
4098 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
4099 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
4100 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
4101 74cfe85e 2020-10-20 stsp if (err)
4102 21355643 2020-12-06 stsp return err;
4103 51a10b52 2020-12-26 stsp tog_free_refs();
4104 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
4105 ca51c541 2020-12-07 stsp if (err)
4106 ca51c541 2020-12-07 stsp return err;
4107 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4108 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4109 d01904d4 2019-06-25 stsp if (err)
4110 d01904d4 2019-06-25 stsp return err;
4111 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4112 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4113 b672a97a 2020-01-27 stsp if (err)
4114 b672a97a 2020-01-27 stsp return err;
4115 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
4116 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
4117 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4118 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4119 21355643 2020-12-06 stsp s->selected_entry = NULL;
4120 21355643 2020-12-06 stsp s->selected = 0;
4121 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4122 21355643 2020-12-06 stsp s->quit = 0;
4123 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
4124 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
4125 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
4126 94ecf40d 2022-07-22 thomas view->offset = 0;
4127 d01904d4 2019-06-25 stsp break;
4128 1be4947a 2022-07-22 thomas case 'R':
4129 07b0611c 2022-06-23 thomas view->count = 0;
4130 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
4131 6458efa5 2020-11-24 stsp break;
4132 1e37a5c2 2019-05-12 jcs default:
4133 07b0611c 2022-06-23 thomas view->count = 0;
4134 1e37a5c2 2019-05-12 jcs break;
4135 899d86c2 2018-05-10 stsp }
4136 e5a0f69f 2018-08-18 stsp
4137 80ddbec8 2018-04-29 stsp return err;
4138 80ddbec8 2018-04-29 stsp }
4139 80ddbec8 2018-04-29 stsp
4140 4ed7e80c 2018-05-20 stsp static const struct got_error *
4141 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4142 c2db6724 2019-01-04 stsp {
4143 c2db6724 2019-01-04 stsp const struct got_error *error;
4144 c2db6724 2019-01-04 stsp
4145 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4146 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4147 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4148 37c06ea4 2019-07-15 stsp #endif
4149 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4150 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4151 c2db6724 2019-01-04 stsp
4152 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4153 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4154 c2db6724 2019-01-04 stsp
4155 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4156 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4157 c2db6724 2019-01-04 stsp
4158 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4159 c2db6724 2019-01-04 stsp if (error != NULL)
4160 c2db6724 2019-01-04 stsp return error;
4161 c2db6724 2019-01-04 stsp
4162 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4163 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4164 c2db6724 2019-01-04 stsp
4165 c2db6724 2019-01-04 stsp return NULL;
4166 c2db6724 2019-01-04 stsp }
4167 c2db6724 2019-01-04 stsp
4168 b85a3496 2023-04-14 thomas static const struct got_error *
4169 557d3365 2023-04-14 thomas init_mock_term(const char *test_script_path)
4170 b85a3496 2023-04-14 thomas {
4171 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4172 b85a3496 2023-04-14 thomas
4173 b85a3496 2023-04-14 thomas if (test_script_path == NULL || *test_script_path == '\0')
4174 b85a3496 2023-04-14 thomas return got_error_msg(GOT_ERR_IO, "GOT_TOG_TEST not defined");
4175 b85a3496 2023-04-14 thomas
4176 557d3365 2023-04-14 thomas tog_io.f = fopen(test_script_path, "re");
4177 557d3365 2023-04-14 thomas if (tog_io.f == NULL) {
4178 b85a3496 2023-04-14 thomas err = got_error_from_errno_fmt("fopen: %s",
4179 b85a3496 2023-04-14 thomas test_script_path);
4180 b85a3496 2023-04-14 thomas goto done;
4181 b85a3496 2023-04-14 thomas }
4182 b85a3496 2023-04-14 thomas
4183 b85a3496 2023-04-14 thomas /* test mode, we don't want any output */
4184 557d3365 2023-04-14 thomas tog_io.cout = fopen("/dev/null", "w+");
4185 557d3365 2023-04-14 thomas if (tog_io.cout == NULL) {
4186 b85a3496 2023-04-14 thomas err = got_error_from_errno("fopen: /dev/null");
4187 b85a3496 2023-04-14 thomas goto done;
4188 b85a3496 2023-04-14 thomas }
4189 b85a3496 2023-04-14 thomas
4190 557d3365 2023-04-14 thomas tog_io.cin = fopen("/dev/tty", "r+");
4191 557d3365 2023-04-14 thomas if (tog_io.cin == NULL) {
4192 b85a3496 2023-04-14 thomas err = got_error_from_errno("fopen: /dev/tty");
4193 b85a3496 2023-04-14 thomas goto done;
4194 b85a3496 2023-04-14 thomas }
4195 b85a3496 2023-04-14 thomas
4196 557d3365 2023-04-14 thomas if (fseeko(tog_io.f, 0L, SEEK_SET) == -1) {
4197 b85a3496 2023-04-14 thomas err = got_error_from_errno("fseeko");
4198 b85a3496 2023-04-14 thomas goto done;
4199 b85a3496 2023-04-14 thomas }
4200 b85a3496 2023-04-14 thomas
4201 1d98034b 2023-04-14 thomas /* use local TERM so we test in different environments */
4202 557d3365 2023-04-14 thomas if (newterm(NULL, tog_io.cout, tog_io.cin) == NULL)
4203 b85a3496 2023-04-14 thomas err = got_error_msg(GOT_ERR_IO,
4204 b85a3496 2023-04-14 thomas "newterm: failed to initialise curses");
4205 557d3365 2023-04-14 thomas
4206 557d3365 2023-04-14 thomas using_mock_io = 1;
4207 b85a3496 2023-04-14 thomas done:
4208 b85a3496 2023-04-14 thomas if (err)
4209 557d3365 2023-04-14 thomas tog_io_close();
4210 b85a3496 2023-04-14 thomas return err;
4211 b85a3496 2023-04-14 thomas }
4212 b85a3496 2023-04-14 thomas
4213 557d3365 2023-04-14 thomas static void
4214 557d3365 2023-04-14 thomas init_curses(void)
4215 a915003a 2019-02-05 stsp {
4216 296152d1 2022-05-31 thomas /*
4217 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
4218 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
4219 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
4220 296152d1 2022-05-31 thomas */
4221 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
4222 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
4223 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
4224 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
4225 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
4226 296152d1 2022-05-31 thomas
4227 557d3365 2023-04-14 thomas if (using_mock_io) /* In test mode we use a fake terminal */
4228 557d3365 2023-04-14 thomas return;
4229 b85a3496 2023-04-14 thomas
4230 557d3365 2023-04-14 thomas initscr();
4231 557d3365 2023-04-14 thomas
4232 a915003a 2019-02-05 stsp cbreak();
4233 557d3365 2023-04-14 thomas halfdelay(1); /* Fast refresh while initial view is loading. */
4234 a915003a 2019-02-05 stsp noecho();
4235 a915003a 2019-02-05 stsp nonl();
4236 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4237 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4238 a915003a 2019-02-05 stsp curs_set(0);
4239 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4240 6d17833f 2019-11-08 stsp start_color();
4241 6d17833f 2019-11-08 stsp use_default_colors();
4242 6d17833f 2019-11-08 stsp }
4243 b85a3496 2023-04-14 thomas
4244 557d3365 2023-04-14 thomas return;
4245 a915003a 2019-02-05 stsp }
4246 a915003a 2019-02-05 stsp
4247 c2db6724 2019-01-04 stsp static const struct got_error *
4248 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4249 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4250 f135c941 2020-02-20 stsp {
4251 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4252 f135c941 2020-02-20 stsp
4253 f135c941 2020-02-20 stsp if (argc == 0) {
4254 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4255 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4256 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4257 f135c941 2020-02-20 stsp return NULL;
4258 f135c941 2020-02-20 stsp }
4259 f135c941 2020-02-20 stsp
4260 f135c941 2020-02-20 stsp if (worktree) {
4261 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4262 bfd61697 2020-11-14 stsp char *p;
4263 f135c941 2020-02-20 stsp
4264 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4265 f135c941 2020-02-20 stsp if (err)
4266 f135c941 2020-02-20 stsp return err;
4267 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4268 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4269 bfd61697 2020-11-14 stsp p) == -1) {
4270 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4271 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4272 f135c941 2020-02-20 stsp }
4273 f135c941 2020-02-20 stsp free(p);
4274 f135c941 2020-02-20 stsp } else
4275 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4276 f135c941 2020-02-20 stsp
4277 f135c941 2020-02-20 stsp return err;
4278 f135c941 2020-02-20 stsp }
4279 f135c941 2020-02-20 stsp
4280 f135c941 2020-02-20 stsp static const struct got_error *
4281 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4282 9f7d7167 2018-04-29 stsp {
4283 1d98034b 2023-04-14 thomas const struct got_error *error;
4284 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4285 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4286 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4287 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4288 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4289 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4290 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4291 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4292 04cc582a 2018-08-01 stsp struct tog_view *view;
4293 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4294 80ddbec8 2018-04-29 stsp
4295 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4296 80ddbec8 2018-04-29 stsp switch (ch) {
4297 b672a97a 2020-01-27 stsp case 'b':
4298 b672a97a 2020-01-27 stsp log_branches = 1;
4299 b672a97a 2020-01-27 stsp break;
4300 80ddbec8 2018-04-29 stsp case 'c':
4301 80ddbec8 2018-04-29 stsp start_commit = optarg;
4302 80ddbec8 2018-04-29 stsp break;
4303 ecb28ae0 2018-07-16 stsp case 'r':
4304 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4305 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4306 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4307 9ba1d308 2019-10-21 stsp optarg);
4308 ecb28ae0 2018-07-16 stsp break;
4309 80ddbec8 2018-04-29 stsp default:
4310 17020d27 2019-03-07 stsp usage_log();
4311 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4312 80ddbec8 2018-04-29 stsp }
4313 80ddbec8 2018-04-29 stsp }
4314 80ddbec8 2018-04-29 stsp
4315 80ddbec8 2018-04-29 stsp argc -= optind;
4316 80ddbec8 2018-04-29 stsp argv += optind;
4317 80ddbec8 2018-04-29 stsp
4318 f135c941 2020-02-20 stsp if (argc > 1)
4319 f135c941 2020-02-20 stsp usage_log();
4320 963f97a1 2019-03-18 stsp
4321 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4322 7cd52833 2022-06-23 thomas if (error != NULL)
4323 7cd52833 2022-06-23 thomas goto done;
4324 7cd52833 2022-06-23 thomas
4325 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4326 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4327 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4328 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4329 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4330 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4331 c156c7a4 2020-12-18 stsp goto done;
4332 a1fbf39a 2019-08-11 stsp if (worktree)
4333 6962eb72 2020-02-20 stsp repo_path =
4334 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4335 a1fbf39a 2019-08-11 stsp else
4336 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4337 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4338 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4339 c156c7a4 2020-12-18 stsp goto done;
4340 c156c7a4 2020-12-18 stsp }
4341 ecb28ae0 2018-07-16 stsp }
4342 ecb28ae0 2018-07-16 stsp
4343 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4344 80ddbec8 2018-04-29 stsp if (error != NULL)
4345 ecb28ae0 2018-07-16 stsp goto done;
4346 80ddbec8 2018-04-29 stsp
4347 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4348 f135c941 2020-02-20 stsp repo, worktree);
4349 f135c941 2020-02-20 stsp if (error)
4350 f135c941 2020-02-20 stsp goto done;
4351 f135c941 2020-02-20 stsp
4352 557d3365 2023-04-14 thomas init_curses();
4353 f135c941 2020-02-20 stsp
4354 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4355 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4356 c02c541e 2019-03-29 stsp if (error)
4357 c02c541e 2019-03-29 stsp goto done;
4358 c02c541e 2019-03-29 stsp
4359 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4360 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4361 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4362 87670572 2020-12-26 naddy if (error)
4363 87670572 2020-12-26 naddy goto done;
4364 87670572 2020-12-26 naddy }
4365 51a10b52 2020-12-26 stsp
4366 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4367 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4368 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4369 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4370 d8f38dc4 2020-12-05 stsp if (error)
4371 d8f38dc4 2020-12-05 stsp goto done;
4372 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4373 d8f38dc4 2020-12-05 stsp } else {
4374 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4375 d8f38dc4 2020-12-05 stsp if (error == NULL)
4376 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4377 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4378 d8f38dc4 2020-12-05 stsp goto done;
4379 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4380 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4381 d8f38dc4 2020-12-05 stsp if (error)
4382 d8f38dc4 2020-12-05 stsp goto done;
4383 d8f38dc4 2020-12-05 stsp }
4384 8b473291 2019-02-21 stsp
4385 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4386 04cc582a 2018-08-01 stsp if (view == NULL) {
4387 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4388 04cc582a 2018-08-01 stsp goto done;
4389 04cc582a 2018-08-01 stsp }
4390 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4391 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4392 ba4f502b 2018-08-04 stsp if (error)
4393 ba4f502b 2018-08-04 stsp goto done;
4394 2fc00ff4 2019-08-31 stsp if (worktree) {
4395 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4396 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4397 2fc00ff4 2019-08-31 stsp worktree = NULL;
4398 2fc00ff4 2019-08-31 stsp }
4399 557d3365 2023-04-14 thomas error = view_loop(view);
4400 ecb28ae0 2018-07-16 stsp done:
4401 f135c941 2020-02-20 stsp free(in_repo_path);
4402 ecb28ae0 2018-07-16 stsp free(repo_path);
4403 ecb28ae0 2018-07-16 stsp free(cwd);
4404 899d86c2 2018-05-10 stsp free(start_id);
4405 d8f38dc4 2020-12-05 stsp free(label);
4406 d8f38dc4 2020-12-05 stsp if (ref)
4407 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4408 1d0f4054 2021-06-17 stsp if (repo) {
4409 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4410 1d0f4054 2021-06-17 stsp if (error == NULL)
4411 1d0f4054 2021-06-17 stsp error = close_err;
4412 1d0f4054 2021-06-17 stsp }
4413 ec142235 2019-03-07 stsp if (worktree)
4414 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4415 7cd52833 2022-06-23 thomas if (pack_fds) {
4416 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4417 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4418 7cd52833 2022-06-23 thomas if (error == NULL)
4419 7cd52833 2022-06-23 thomas error = pack_err;
4420 b85a3496 2023-04-14 thomas }
4421 51a10b52 2020-12-26 stsp tog_free_refs();
4422 80ddbec8 2018-04-29 stsp return error;
4423 9f7d7167 2018-04-29 stsp }
4424 9f7d7167 2018-04-29 stsp
4425 4ed7e80c 2018-05-20 stsp __dead static void
4426 9f7d7167 2018-04-29 stsp usage_diff(void)
4427 9f7d7167 2018-04-29 stsp {
4428 80ddbec8 2018-04-29 stsp endwin();
4429 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4430 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4431 9f7d7167 2018-04-29 stsp exit(1);
4432 b304db33 2018-05-20 stsp }
4433 b304db33 2018-05-20 stsp
4434 6d17833f 2019-11-08 stsp static int
4435 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4436 41605754 2020-11-12 stsp regmatch_t *regmatch)
4437 6d17833f 2019-11-08 stsp {
4438 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4439 6d17833f 2019-11-08 stsp }
4440 6d17833f 2019-11-08 stsp
4441 ef20f542 2022-06-26 thomas static struct tog_color *
4442 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4443 6d17833f 2019-11-08 stsp {
4444 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4445 6d17833f 2019-11-08 stsp
4446 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4447 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4448 6d17833f 2019-11-08 stsp return tc;
4449 6d17833f 2019-11-08 stsp }
4450 6d17833f 2019-11-08 stsp
4451 6d17833f 2019-11-08 stsp return NULL;
4452 6d17833f 2019-11-08 stsp }
4453 6d17833f 2019-11-08 stsp
4454 4ed7e80c 2018-05-20 stsp static const struct got_error *
4455 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4456 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4457 41605754 2020-11-12 stsp {
4458 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4459 666f7b10 2022-06-23 thomas char *exstr = NULL;
4460 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4461 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4462 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4463 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4464 41605754 2020-11-12 stsp
4465 41605754 2020-11-12 stsp *wtotal = 0;
4466 cbea3800 2022-06-23 thomas
4467 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4468 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4469 41605754 2020-11-12 stsp
4470 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4471 666f7b10 2022-06-23 thomas if (err)
4472 666f7b10 2022-06-23 thomas return err;
4473 666f7b10 2022-06-23 thomas
4474 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4475 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4476 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4477 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4478 666f7b10 2022-06-23 thomas goto done;
4479 666f7b10 2022-06-23 thomas }
4480 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4481 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4482 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4483 cbea3800 2022-06-23 thomas goto done;
4484 cbea3800 2022-06-23 thomas }
4485 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4486 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4487 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4488 cbea3800 2022-06-23 thomas goto done;
4489 cbea3800 2022-06-23 thomas }
4490 05171be4 2022-06-23 thomas
4491 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4492 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4493 cbea3800 2022-06-23 thomas col_tab_align, 1);
4494 cbea3800 2022-06-23 thomas if (err)
4495 cbea3800 2022-06-23 thomas goto done;
4496 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4497 05171be4 2022-06-23 thomas if (n) {
4498 cbea3800 2022-06-23 thomas free(wline);
4499 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4500 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4501 cbea3800 2022-06-23 thomas if (err)
4502 cbea3800 2022-06-23 thomas goto done;
4503 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4504 cbea3800 2022-06-23 thomas wlimit -= width;
4505 cbea3800 2022-06-23 thomas *wtotal += width;
4506 41605754 2020-11-12 stsp }
4507 41605754 2020-11-12 stsp
4508 41605754 2020-11-12 stsp if (wlimit > 0) {
4509 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4510 cbea3800 2022-06-23 thomas size_t wlen;
4511 cbea3800 2022-06-23 thomas
4512 cbea3800 2022-06-23 thomas free(wline);
4513 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4514 cbea3800 2022-06-23 thomas col_tab_align, 1);
4515 cbea3800 2022-06-23 thomas if (err)
4516 cbea3800 2022-06-23 thomas goto done;
4517 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4518 cbea3800 2022-06-23 thomas while (i < wlen) {
4519 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4520 cbea3800 2022-06-23 thomas if (width == -1) {
4521 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4522 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4523 cbea3800 2022-06-23 thomas goto done;
4524 cbea3800 2022-06-23 thomas }
4525 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4526 cbea3800 2022-06-23 thomas break;
4527 1c5e5faa 2022-06-23 thomas w += width;
4528 cbea3800 2022-06-23 thomas i++;
4529 41605754 2020-11-12 stsp }
4530 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4531 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4532 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4533 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4534 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4535 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4536 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4537 41605754 2020-11-12 stsp }
4538 41605754 2020-11-12 stsp }
4539 41605754 2020-11-12 stsp
4540 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4541 cbea3800 2022-06-23 thomas free(wline);
4542 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4543 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4544 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4545 cbea3800 2022-06-23 thomas col_tab_align, 1);
4546 cbea3800 2022-06-23 thomas if (err)
4547 cbea3800 2022-06-23 thomas goto done;
4548 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4549 cbea3800 2022-06-23 thomas } else {
4550 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4551 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4552 cbea3800 2022-06-23 thomas if (err)
4553 cbea3800 2022-06-23 thomas goto done;
4554 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4555 cbea3800 2022-06-23 thomas }
4556 cbea3800 2022-06-23 thomas *wtotal += width2;
4557 41605754 2020-11-12 stsp }
4558 cbea3800 2022-06-23 thomas done:
4559 05171be4 2022-06-23 thomas free(wline);
4560 666f7b10 2022-06-23 thomas free(exstr);
4561 cbea3800 2022-06-23 thomas free(seg0);
4562 cbea3800 2022-06-23 thomas free(seg1);
4563 cbea3800 2022-06-23 thomas free(seg2);
4564 cbea3800 2022-06-23 thomas return err;
4565 41605754 2020-11-12 stsp }
4566 07dd3ed3 2022-08-06 thomas
4567 07dd3ed3 2022-08-06 thomas static int
4568 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4569 07dd3ed3 2022-08-06 thomas {
4570 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4571 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4572 07dd3ed3 2022-08-06 thomas
4573 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4574 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4575 fc2737d5 2022-09-15 thomas
4576 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4577 fc2737d5 2022-09-15 thomas selected = first;
4578 fc2737d5 2022-09-15 thomas eof = &s->eof;
4579 fc2737d5 2022-09-15 thomas f = s->f;
4580 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4581 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4582 41605754 2020-11-12 stsp
4583 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4584 07dd3ed3 2022-08-06 thomas selected = first;
4585 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4586 07dd3ed3 2022-08-06 thomas f = s->f;
4587 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4588 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4589 07dd3ed3 2022-08-06 thomas
4590 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4591 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4592 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4593 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4594 07dd3ed3 2022-08-06 thomas } else
4595 07dd3ed3 2022-08-06 thomas return 0;
4596 07dd3ed3 2022-08-06 thomas
4597 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4598 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4599 07dd3ed3 2022-08-06 thomas return 0;
4600 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4601 07dd3ed3 2022-08-06 thomas rewind(f);
4602 07dd3ed3 2022-08-06 thomas *eof = 0;
4603 07dd3ed3 2022-08-06 thomas *first = 1;
4604 07dd3ed3 2022-08-06 thomas *lineno = 0;
4605 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4606 07dd3ed3 2022-08-06 thomas return 0;
4607 07dd3ed3 2022-08-06 thomas }
4608 07dd3ed3 2022-08-06 thomas
4609 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4610 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4611 07dd3ed3 2022-08-06 thomas view->gline = 0;
4612 07dd3ed3 2022-08-06 thomas
4613 07dd3ed3 2022-08-06 thomas return 1;
4614 07dd3ed3 2022-08-06 thomas }
4615 07dd3ed3 2022-08-06 thomas
4616 41605754 2020-11-12 stsp static const struct got_error *
4617 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4618 26ed57b2 2018-05-19 stsp {
4619 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4620 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4621 61e69b96 2018-05-20 stsp const struct got_error *err;
4622 82c78e96 2022-08-06 thomas int nprinted = 0;
4623 b304db33 2018-05-20 stsp char *line;
4624 826082fe 2020-12-10 stsp size_t linesize = 0;
4625 826082fe 2020-12-10 stsp ssize_t linelen;
4626 61e69b96 2018-05-20 stsp wchar_t *wline;
4627 e0b650dd 2018-05-20 stsp int width;
4628 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4629 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4630 fe621944 2020-11-10 stsp off_t line_offset;
4631 26ed57b2 2018-05-19 stsp
4632 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4633 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4634 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4635 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4636 fe621944 2020-11-10 stsp
4637 f7d12f7e 2018-08-01 stsp werase(view->window);
4638 a3404814 2018-09-02 stsp
4639 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4640 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4641 07dd3ed3 2022-08-06 thomas
4642 a3404814 2018-09-02 stsp if (header) {
4643 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4644 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4645 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4646 07dd3ed3 2022-08-06 thomas
4647 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4648 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4649 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4650 f91a2b48 2022-06-23 thomas 0, 0);
4651 135a2da0 2020-11-11 stsp free(line);
4652 135a2da0 2020-11-11 stsp if (err)
4653 a3404814 2018-09-02 stsp return err;
4654 a3404814 2018-09-02 stsp
4655 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4656 a3404814 2018-09-02 stsp wstandout(view->window);
4657 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4658 e54cc94a 2020-11-11 stsp free(wline);
4659 e54cc94a 2020-11-11 stsp wline = NULL;
4660 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4661 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4662 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4663 a3404814 2018-09-02 stsp wstandend(view->window);
4664 26ed57b2 2018-05-19 stsp
4665 a3404814 2018-09-02 stsp if (max_lines <= 1)
4666 a3404814 2018-09-02 stsp return NULL;
4667 a3404814 2018-09-02 stsp max_lines--;
4668 a3404814 2018-09-02 stsp }
4669 a3404814 2018-09-02 stsp
4670 89f1a395 2020-12-01 naddy s->eof = 0;
4671 05171be4 2022-06-23 thomas view->maxx = 0;
4672 826082fe 2020-12-10 stsp line = NULL;
4673 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4674 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4675 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4676 6568f0aa 2022-08-06 thomas
4677 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4678 826082fe 2020-12-10 stsp if (linelen == -1) {
4679 826082fe 2020-12-10 stsp if (feof(s->f)) {
4680 826082fe 2020-12-10 stsp s->eof = 1;
4681 826082fe 2020-12-10 stsp break;
4682 826082fe 2020-12-10 stsp }
4683 826082fe 2020-12-10 stsp free(line);
4684 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4685 61e69b96 2018-05-20 stsp }
4686 05171be4 2022-06-23 thomas
4687 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4688 07dd3ed3 2022-08-06 thomas continue;
4689 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4690 07dd3ed3 2022-08-06 thomas continue;
4691 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4692 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4693 07dd3ed3 2022-08-06 thomas
4694 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4695 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4696 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4697 cbea3800 2022-06-23 thomas if (err) {
4698 cbea3800 2022-06-23 thomas free(line);
4699 cbea3800 2022-06-23 thomas return err;
4700 cbea3800 2022-06-23 thomas }
4701 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4702 cbea3800 2022-06-23 thomas free(wline);
4703 cbea3800 2022-06-23 thomas wline = NULL;
4704 cbea3800 2022-06-23 thomas
4705 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4706 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4707 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4708 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4709 07dd3ed3 2022-08-06 thomas if (attr)
4710 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4711 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4712 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4713 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4714 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4715 41605754 2020-11-12 stsp if (err) {
4716 41605754 2020-11-12 stsp free(line);
4717 41605754 2020-11-12 stsp return err;
4718 41605754 2020-11-12 stsp }
4719 41605754 2020-11-12 stsp } else {
4720 f1c0ec19 2022-06-23 thomas int skip;
4721 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4722 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4723 f1c0ec19 2022-06-23 thomas if (err) {
4724 f1c0ec19 2022-06-23 thomas free(line);
4725 f1c0ec19 2022-06-23 thomas return err;
4726 f1c0ec19 2022-06-23 thomas }
4727 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4728 f1c0ec19 2022-06-23 thomas free(wline);
4729 41605754 2020-11-12 stsp wline = NULL;
4730 41605754 2020-11-12 stsp }
4731 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4732 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4733 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4734 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4735 07dd3ed3 2022-08-06 thomas } else {
4736 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4737 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4738 07dd3ed3 2022-08-06 thomas }
4739 07dd3ed3 2022-08-06 thomas if (attr)
4740 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4741 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4742 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4743 826082fe 2020-12-10 stsp }
4744 826082fe 2020-12-10 stsp free(line);
4745 fe621944 2020-11-10 stsp if (nprinted >= 1)
4746 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4747 89f1a395 2020-12-01 naddy (nprinted - 1);
4748 fe621944 2020-11-10 stsp else
4749 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4750 26ed57b2 2018-05-19 stsp
4751 a5d43cac 2022-07-01 thomas view_border(view);
4752 c3e9aa98 2019-05-13 jcs
4753 89f1a395 2020-12-01 naddy if (s->eof) {
4754 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4755 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4756 c3e9aa98 2019-05-13 jcs nprinted++;
4757 c3e9aa98 2019-05-13 jcs }
4758 c3e9aa98 2019-05-13 jcs
4759 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4760 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4761 c3e9aa98 2019-05-13 jcs if (err) {
4762 c3e9aa98 2019-05-13 jcs return err;
4763 c3e9aa98 2019-05-13 jcs }
4764 26ed57b2 2018-05-19 stsp
4765 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4766 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4767 e54cc94a 2020-11-11 stsp free(wline);
4768 e54cc94a 2020-11-11 stsp wline = NULL;
4769 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4770 c3e9aa98 2019-05-13 jcs }
4771 c3e9aa98 2019-05-13 jcs
4772 26ed57b2 2018-05-19 stsp return NULL;
4773 abd2672a 2018-12-23 stsp }
4774 abd2672a 2018-12-23 stsp
4775 abd2672a 2018-12-23 stsp static char *
4776 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4777 abd2672a 2018-12-23 stsp {
4778 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4779 09867e48 2019-08-13 stsp char *p, *s;
4780 09867e48 2019-08-13 stsp
4781 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4782 09867e48 2019-08-13 stsp if (tm == NULL)
4783 09867e48 2019-08-13 stsp return NULL;
4784 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4785 09867e48 2019-08-13 stsp if (s == NULL)
4786 09867e48 2019-08-13 stsp return NULL;
4787 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4788 abd2672a 2018-12-23 stsp if (p)
4789 abd2672a 2018-12-23 stsp *p = '\0';
4790 abd2672a 2018-12-23 stsp return s;
4791 9f7d7167 2018-04-29 stsp }
4792 9f7d7167 2018-04-29 stsp
4793 4ed7e80c 2018-05-20 stsp static const struct got_error *
4794 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4795 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4796 abd2672a 2018-12-23 stsp {
4797 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4798 fe621944 2020-11-10 stsp
4799 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4800 fe621944 2020-11-10 stsp if (p == NULL)
4801 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4802 82c78e96 2022-08-06 thomas *lines = p;
4803 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4804 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4805 fe621944 2020-11-10 stsp (*nlines)++;
4806 82c78e96 2022-08-06 thomas
4807 fe621944 2020-11-10 stsp return NULL;
4808 fe621944 2020-11-10 stsp }
4809 fe621944 2020-11-10 stsp
4810 fe621944 2020-11-10 stsp static const struct got_error *
4811 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4812 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
4813 be97ab03 2023-01-19 thomas {
4814 be97ab03 2023-01-19 thomas struct got_diff_line *p;
4815 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
4816 be97ab03 2023-01-19 thomas size_t i, r;
4817 be97ab03 2023-01-19 thomas
4818 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
4819 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
4820 be97ab03 2023-01-19 thomas
4821 be97ab03 2023-01-19 thomas for (;;) {
4822 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
4823 be97ab03 2023-01-19 thomas if (r == 0) {
4824 be97ab03 2023-01-19 thomas if (ferror(src))
4825 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
4826 be97ab03 2023-01-19 thomas if (feof(src))
4827 be97ab03 2023-01-19 thomas break;
4828 be97ab03 2023-01-19 thomas }
4829 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
4830 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
4831 be97ab03 2023-01-19 thomas }
4832 be97ab03 2023-01-19 thomas
4833 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
4834 9382c10a 2023-02-23 thomas return NULL;
4835 9382c10a 2023-02-23 thomas
4836 be97ab03 2023-01-19 thomas /*
4837 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
4838 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
4839 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
4840 be97ab03 2023-01-19 thomas */
4841 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
4842 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
4843 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4844 be97ab03 2023-01-19 thomas
4845 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
4846 9382c10a 2023-02-23 thomas --s_nlines;
4847 9382c10a 2023-02-23 thomas ++s_lines;
4848 9382c10a 2023-02-23 thomas }
4849 9382c10a 2023-02-23 thomas }
4850 be97ab03 2023-01-19 thomas
4851 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4852 be97ab03 2023-01-19 thomas if (p == NULL) {
4853 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
4854 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
4855 be97ab03 2023-01-19 thomas }
4856 be97ab03 2023-01-19 thomas
4857 be97ab03 2023-01-19 thomas *d_lines = p;
4858 be97ab03 2023-01-19 thomas
4859 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4860 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
4861 be97ab03 2023-01-19 thomas
4862 be97ab03 2023-01-19 thomas return NULL;
4863 be97ab03 2023-01-19 thomas }
4864 be97ab03 2023-01-19 thomas
4865 be97ab03 2023-01-19 thomas static const struct got_error *
4866 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4867 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4868 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4869 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
4870 fe621944 2020-11-10 stsp {
4871 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4872 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4873 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4874 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4875 45d799e2 2018-12-23 stsp time_t committer_time;
4876 45d799e2 2018-12-23 stsp const char *author, *committer;
4877 8b473291 2019-02-21 stsp char *refs_str = NULL;
4878 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4879 fe621944 2020-11-10 stsp off_t outoff = 0;
4880 fe621944 2020-11-10 stsp int n;
4881 abd2672a 2018-12-23 stsp
4882 8b473291 2019-02-21 stsp if (refs) {
4883 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4884 8b473291 2019-02-21 stsp if (err)
4885 8b473291 2019-02-21 stsp return err;
4886 8b473291 2019-02-21 stsp }
4887 8b473291 2019-02-21 stsp
4888 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4889 abd2672a 2018-12-23 stsp if (err)
4890 abd2672a 2018-12-23 stsp return err;
4891 abd2672a 2018-12-23 stsp
4892 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4893 15a94983 2018-12-23 stsp if (err) {
4894 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4895 15a94983 2018-12-23 stsp goto done;
4896 15a94983 2018-12-23 stsp }
4897 abd2672a 2018-12-23 stsp
4898 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4899 fe621944 2020-11-10 stsp if (err)
4900 fe621944 2020-11-10 stsp goto done;
4901 fe621944 2020-11-10 stsp
4902 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4903 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4904 fe621944 2020-11-10 stsp if (n < 0) {
4905 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4906 abd2672a 2018-12-23 stsp goto done;
4907 abd2672a 2018-12-23 stsp }
4908 fe621944 2020-11-10 stsp outoff += n;
4909 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4910 fe621944 2020-11-10 stsp if (err)
4911 fe621944 2020-11-10 stsp goto done;
4912 fe621944 2020-11-10 stsp
4913 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4914 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4915 fe621944 2020-11-10 stsp if (n < 0) {
4916 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4917 abd2672a 2018-12-23 stsp goto done;
4918 abd2672a 2018-12-23 stsp }
4919 fe621944 2020-11-10 stsp outoff += n;
4920 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4921 fe621944 2020-11-10 stsp if (err)
4922 fe621944 2020-11-10 stsp goto done;
4923 fe621944 2020-11-10 stsp
4924 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
4925 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
4926 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
4927 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
4928 fe621944 2020-11-10 stsp if (n < 0) {
4929 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4930 fe621944 2020-11-10 stsp goto done;
4931 fe621944 2020-11-10 stsp }
4932 fe621944 2020-11-10 stsp outoff += n;
4933 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4934 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
4935 fe621944 2020-11-10 stsp if (err)
4936 fe621944 2020-11-10 stsp goto done;
4937 abd2672a 2018-12-23 stsp }
4938 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
4939 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
4940 48830929 2023-01-07 thomas if (datestr) {
4941 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
4942 fe621944 2020-11-10 stsp if (n < 0) {
4943 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4944 fe621944 2020-11-10 stsp goto done;
4945 fe621944 2020-11-10 stsp }
4946 fe621944 2020-11-10 stsp outoff += n;
4947 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4948 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
4949 fe621944 2020-11-10 stsp if (err)
4950 fe621944 2020-11-10 stsp goto done;
4951 abd2672a 2018-12-23 stsp }
4952 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4953 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4954 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4955 ac4dc263 2021-09-24 thomas int pn = 1;
4956 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4957 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4958 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4959 ac4dc263 2021-09-24 thomas if (err)
4960 ac4dc263 2021-09-24 thomas goto done;
4961 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4962 ac4dc263 2021-09-24 thomas if (n < 0) {
4963 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4964 ac4dc263 2021-09-24 thomas goto done;
4965 ac4dc263 2021-09-24 thomas }
4966 ac4dc263 2021-09-24 thomas outoff += n;
4967 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4968 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4969 ac4dc263 2021-09-24 thomas if (err)
4970 ac4dc263 2021-09-24 thomas goto done;
4971 ac4dc263 2021-09-24 thomas free(id_str);
4972 ac4dc263 2021-09-24 thomas id_str = NULL;
4973 ac4dc263 2021-09-24 thomas }
4974 ac4dc263 2021-09-24 thomas }
4975 ac4dc263 2021-09-24 thomas
4976 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4977 5943eee2 2019-08-13 stsp if (err)
4978 5943eee2 2019-08-13 stsp goto done;
4979 fe621944 2020-11-10 stsp s = logmsg;
4980 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4981 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4982 fe621944 2020-11-10 stsp if (n < 0) {
4983 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4984 fe621944 2020-11-10 stsp goto done;
4985 fe621944 2020-11-10 stsp }
4986 fe621944 2020-11-10 stsp outoff += n;
4987 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4988 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4989 fe621944 2020-11-10 stsp if (err)
4990 fe621944 2020-11-10 stsp goto done;
4991 abd2672a 2018-12-23 stsp }
4992 fe621944 2020-11-10 stsp
4993 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
4994 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4995 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
4996 772fcad5 2023-01-07 thomas
4997 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4998 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4999 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
5000 fe621944 2020-11-10 stsp if (n < 0) {
5001 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5002 fe621944 2020-11-10 stsp goto done;
5003 fe621944 2020-11-10 stsp }
5004 fe621944 2020-11-10 stsp outoff += n;
5005 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5006 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
5007 fe621944 2020-11-10 stsp if (err)
5008 fe621944 2020-11-10 stsp goto done;
5009 0208f208 2020-05-05 stsp }
5010 fe621944 2020-11-10 stsp
5011 0208f208 2020-05-05 stsp fputc('\n', outfile);
5012 fe621944 2020-11-10 stsp outoff++;
5013 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5014 772fcad5 2023-01-07 thomas if (err)
5015 772fcad5 2023-01-07 thomas goto done;
5016 772fcad5 2023-01-07 thomas
5017 772fcad5 2023-01-07 thomas n = fprintf(outfile,
5018 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5019 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5020 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5021 772fcad5 2023-01-07 thomas if (n < 0) {
5022 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
5023 772fcad5 2023-01-07 thomas goto done;
5024 772fcad5 2023-01-07 thomas }
5025 772fcad5 2023-01-07 thomas outoff += n;
5026 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5027 772fcad5 2023-01-07 thomas if (err)
5028 772fcad5 2023-01-07 thomas goto done;
5029 772fcad5 2023-01-07 thomas
5030 772fcad5 2023-01-07 thomas fputc('\n', outfile);
5031 772fcad5 2023-01-07 thomas outoff++;
5032 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5033 abd2672a 2018-12-23 stsp done:
5034 abd2672a 2018-12-23 stsp free(id_str);
5035 5943eee2 2019-08-13 stsp free(logmsg);
5036 8b473291 2019-02-21 stsp free(refs_str);
5037 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5038 7510f233 2020-08-09 stsp if (err) {
5039 82c78e96 2022-08-06 thomas free(*lines);
5040 82c78e96 2022-08-06 thomas *lines = NULL;
5041 ae6a6978 2020-08-09 stsp *nlines = 0;
5042 7510f233 2020-08-09 stsp }
5043 fe621944 2020-11-10 stsp return err;
5044 abd2672a 2018-12-23 stsp }
5045 abd2672a 2018-12-23 stsp
5046 abd2672a 2018-12-23 stsp static const struct got_error *
5047 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5048 26ed57b2 2018-05-19 stsp {
5049 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5050 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
5051 15a94983 2018-12-23 stsp int obj_type;
5052 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
5053 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
5054 fe621944 2020-11-10 stsp
5055 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
5056 be97ab03 2023-01-19 thomas
5057 82c78e96 2022-08-06 thomas free(s->lines);
5058 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
5059 82c78e96 2022-08-06 thomas if (s->lines == NULL)
5060 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5061 fe621944 2020-11-10 stsp s->nlines = 0;
5062 26ed57b2 2018-05-19 stsp
5063 511a516b 2018-05-19 stsp f = got_opentemp();
5064 48ae06ee 2018-10-18 stsp if (f == NULL) {
5065 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5066 48ae06ee 2018-10-18 stsp goto done;
5067 48ae06ee 2018-10-18 stsp }
5068 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
5069 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
5070 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
5071 be97ab03 2023-01-19 thomas goto done;
5072 be97ab03 2023-01-19 thomas }
5073 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5074 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5075 fb43ecf1 2019-02-11 stsp goto done;
5076 fb43ecf1 2019-02-11 stsp }
5077 48ae06ee 2018-10-18 stsp s->f = f;
5078 26ed57b2 2018-05-19 stsp
5079 15a94983 2018-12-23 stsp if (s->id1)
5080 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5081 15a94983 2018-12-23 stsp else
5082 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5083 15a94983 2018-12-23 stsp if (err)
5084 15a94983 2018-12-23 stsp goto done;
5085 15a94983 2018-12-23 stsp
5086 15a94983 2018-12-23 stsp switch (obj_type) {
5087 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5088 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5089 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5090 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
5091 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5092 53d03f97 2023-01-10 thomas s->f);
5093 26ed57b2 2018-05-19 stsp break;
5094 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5095 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5096 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5097 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5098 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
5099 26ed57b2 2018-05-19 stsp break;
5100 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5101 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5102 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5103 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5104 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5105 be97ab03 2023-01-19 thomas size_t nlines = 0;
5106 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
5107 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
5108 be97ab03 2023-01-19 thomas &changed_paths,
5109 be97ab03 2023-01-19 thomas s->ignore_whitespace,
5110 be97ab03 2023-01-19 thomas s->force_text_diff,
5111 be97ab03 2023-01-19 thomas tog_diff_algo
5112 be97ab03 2023-01-19 thomas };
5113 abd2672a 2018-12-23 stsp
5114 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
5115 be97ab03 2023-01-19 thomas if (lines == NULL) {
5116 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
5117 be97ab03 2023-01-19 thomas goto done;
5118 be97ab03 2023-01-19 thomas }
5119 be97ab03 2023-01-19 thomas
5120 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
5121 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
5122 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5123 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5124 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5125 be97ab03 2023-01-19 thomas if (err)
5126 be97ab03 2023-01-19 thomas break;
5127 be97ab03 2023-01-19 thomas
5128 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5129 abd2672a 2018-12-23 stsp if (err)
5130 3ffacbe1 2020-02-02 tracey goto done;
5131 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5132 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5133 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5134 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
5135 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
5136 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5137 f44b1f58 2020-02-02 tracey if (err)
5138 f44b1f58 2020-02-02 tracey goto done;
5139 f44b1f58 2020-02-02 tracey } else {
5140 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5141 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5142 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5143 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
5144 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
5145 772fcad5 2023-01-07 thomas s->ignore_whitespace,
5146 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5147 f44b1f58 2020-02-02 tracey if (err)
5148 f44b1f58 2020-02-02 tracey goto done;
5149 f5404e4e 2020-02-02 tracey break;
5150 15a087fe 2019-02-21 stsp }
5151 abd2672a 2018-12-23 stsp }
5152 abd2672a 2018-12-23 stsp }
5153 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5154 abd2672a 2018-12-23 stsp
5155 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5156 be97ab03 2023-01-19 thomas lines, nlines);
5157 26ed57b2 2018-05-19 stsp break;
5158 abd2672a 2018-12-23 stsp }
5159 26ed57b2 2018-05-19 stsp default:
5160 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5161 48ae06ee 2018-10-18 stsp break;
5162 26ed57b2 2018-05-19 stsp }
5163 48ae06ee 2018-10-18 stsp done:
5164 be97ab03 2023-01-19 thomas free(lines);
5165 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5166 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5167 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5168 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5169 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
5170 48ae06ee 2018-10-18 stsp return err;
5171 48ae06ee 2018-10-18 stsp }
5172 26ed57b2 2018-05-19 stsp
5173 f5215bb9 2019-02-22 stsp static void
5174 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5175 f5215bb9 2019-02-22 stsp {
5176 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5177 f5215bb9 2019-02-22 stsp update_panels();
5178 f5215bb9 2019-02-22 stsp doupdate();
5179 f44b1f58 2020-02-02 tracey }
5180 f44b1f58 2020-02-02 tracey
5181 f44b1f58 2020-02-02 tracey static const struct got_error *
5182 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5183 f44b1f58 2020-02-02 tracey {
5184 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5185 f44b1f58 2020-02-02 tracey
5186 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5187 f44b1f58 2020-02-02 tracey return NULL;
5188 f44b1f58 2020-02-02 tracey }
5189 f44b1f58 2020-02-02 tracey
5190 2a7d3cd7 2022-09-17 thomas static void
5191 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5192 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
5193 f44b1f58 2020-02-02 tracey {
5194 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
5195 fc2737d5 2022-09-15 thomas
5196 2a7d3cd7 2022-09-17 thomas *f = s->f;
5197 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
5198 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
5199 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
5200 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
5201 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
5202 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
5203 fc2737d5 2022-09-15 thomas }
5204 fc2737d5 2022-09-15 thomas
5205 fc2737d5 2022-09-15 thomas static const struct got_error *
5206 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
5207 fc2737d5 2022-09-15 thomas {
5208 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5209 fc2737d5 2022-09-15 thomas FILE *f;
5210 f44b1f58 2020-02-02 tracey int lineno;
5211 78eecffc 2022-06-23 thomas char *line = NULL;
5212 826082fe 2020-12-10 stsp size_t linesize = 0;
5213 826082fe 2020-12-10 stsp ssize_t linelen;
5214 fc2737d5 2022-09-15 thomas off_t *line_offsets;
5215 fc2737d5 2022-09-15 thomas size_t nlines = 0;
5216 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
5217 f44b1f58 2020-02-02 tracey
5218 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
5219 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
5220 2a7d3cd7 2022-09-17 thomas "view search not supported");
5221 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5222 fc2737d5 2022-09-15 thomas &match, &selected);
5223 fc2737d5 2022-09-15 thomas
5224 f44b1f58 2020-02-02 tracey if (!view->searching) {
5225 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5226 f44b1f58 2020-02-02 tracey return NULL;
5227 f44b1f58 2020-02-02 tracey }
5228 f44b1f58 2020-02-02 tracey
5229 fc2737d5 2022-09-15 thomas if (*match) {
5230 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5231 7303e8c8 2023-04-04 thomas lineno = *first + 1;
5232 f44b1f58 2020-02-02 tracey else
5233 7303e8c8 2023-04-04 thomas lineno = *first - 1;
5234 4b3f9dac 2021-12-17 thomas } else
5235 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
5236 f44b1f58 2020-02-02 tracey
5237 f44b1f58 2020-02-02 tracey while (1) {
5238 f44b1f58 2020-02-02 tracey off_t offset;
5239 f44b1f58 2020-02-02 tracey
5240 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
5241 fc2737d5 2022-09-15 thomas if (*match == 0) {
5242 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5243 f44b1f58 2020-02-02 tracey break;
5244 f44b1f58 2020-02-02 tracey }
5245 f44b1f58 2020-02-02 tracey
5246 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5247 f44b1f58 2020-02-02 tracey lineno = 1;
5248 f44b1f58 2020-02-02 tracey else
5249 fc2737d5 2022-09-15 thomas lineno = nlines;
5250 f44b1f58 2020-02-02 tracey }
5251 f44b1f58 2020-02-02 tracey
5252 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5253 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5254 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5255 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5256 f44b1f58 2020-02-02 tracey free(line);
5257 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5258 f44b1f58 2020-02-02 tracey }
5259 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5260 78eecffc 2022-06-23 thomas if (linelen != -1) {
5261 78eecffc 2022-06-23 thomas char *exstr;
5262 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5263 78eecffc 2022-06-23 thomas if (err)
5264 78eecffc 2022-06-23 thomas break;
5265 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5266 78eecffc 2022-06-23 thomas &view->regmatch)) {
5267 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5268 fc2737d5 2022-09-15 thomas *match = lineno;
5269 78eecffc 2022-06-23 thomas free(exstr);
5270 78eecffc 2022-06-23 thomas break;
5271 78eecffc 2022-06-23 thomas }
5272 78eecffc 2022-06-23 thomas free(exstr);
5273 f44b1f58 2020-02-02 tracey }
5274 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5275 f44b1f58 2020-02-02 tracey lineno++;
5276 f44b1f58 2020-02-02 tracey else
5277 f44b1f58 2020-02-02 tracey lineno--;
5278 f44b1f58 2020-02-02 tracey }
5279 826082fe 2020-12-10 stsp free(line);
5280 f44b1f58 2020-02-02 tracey
5281 fc2737d5 2022-09-15 thomas if (*match) {
5282 fc2737d5 2022-09-15 thomas *first = *match;
5283 fc2737d5 2022-09-15 thomas *selected = 1;
5284 f44b1f58 2020-02-02 tracey }
5285 f44b1f58 2020-02-02 tracey
5286 05171be4 2022-06-23 thomas return err;
5287 f5215bb9 2019-02-22 stsp }
5288 f5215bb9 2019-02-22 stsp
5289 48ae06ee 2018-10-18 stsp static const struct got_error *
5290 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5291 a0f32f33 2022-06-13 thomas {
5292 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5293 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5294 a0f32f33 2022-06-13 thomas
5295 a0f32f33 2022-06-13 thomas free(s->id1);
5296 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5297 a0f32f33 2022-06-13 thomas free(s->id2);
5298 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5299 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5300 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5301 a0f32f33 2022-06-13 thomas s->f = NULL;
5302 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5303 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5304 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5305 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5306 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5307 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5308 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5309 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5310 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5311 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5312 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5313 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5314 82c78e96 2022-08-06 thomas free(s->lines);
5315 82c78e96 2022-08-06 thomas s->lines = NULL;
5316 a0f32f33 2022-06-13 thomas s->nlines = 0;
5317 a0f32f33 2022-06-13 thomas return err;
5318 a0f32f33 2022-06-13 thomas }
5319 a0f32f33 2022-06-13 thomas
5320 a0f32f33 2022-06-13 thomas static const struct got_error *
5321 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5322 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5323 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5324 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5325 48ae06ee 2018-10-18 stsp {
5326 48ae06ee 2018-10-18 stsp const struct got_error *err;
5327 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5328 5dc9f4bc 2018-08-04 stsp
5329 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5330 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5331 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5332 a0f32f33 2022-06-13 thomas
5333 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5334 51b7e1c3 2023-04-14 thomas int type1, type2;
5335 15a94983 2018-12-23 stsp
5336 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type1, repo, id1);
5337 51b7e1c3 2023-04-14 thomas if (err)
5338 51b7e1c3 2023-04-14 thomas goto done;
5339 51b7e1c3 2023-04-14 thomas err = got_object_get_type(&type2, repo, id2);
5340 51b7e1c3 2023-04-14 thomas if (err)
5341 51b7e1c3 2023-04-14 thomas goto done;
5342 51b7e1c3 2023-04-14 thomas
5343 51b7e1c3 2023-04-14 thomas if (type1 != type2) {
5344 51b7e1c3 2023-04-14 thomas err = got_error(GOT_ERR_OBJ_TYPE);
5345 51b7e1c3 2023-04-14 thomas goto done;
5346 51b7e1c3 2023-04-14 thomas }
5347 15a94983 2018-12-23 stsp }
5348 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5349 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5350 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5351 f44b1f58 2020-02-02 tracey s->repo = repo;
5352 f44b1f58 2020-02-02 tracey s->id1 = id1;
5353 f44b1f58 2020-02-02 tracey s->id2 = id2;
5354 3dbaef42 2020-11-24 stsp s->label1 = label1;
5355 3dbaef42 2020-11-24 stsp s->label2 = label2;
5356 48ae06ee 2018-10-18 stsp
5357 15a94983 2018-12-23 stsp if (id1) {
5358 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5359 51b7e1c3 2023-04-14 thomas if (s->id1 == NULL) {
5360 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
5361 51b7e1c3 2023-04-14 thomas goto done;
5362 51b7e1c3 2023-04-14 thomas }
5363 48ae06ee 2018-10-18 stsp } else
5364 5465d566 2020-02-01 tracey s->id1 = NULL;
5365 48ae06ee 2018-10-18 stsp
5366 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5367 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5368 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5369 a0f32f33 2022-06-13 thomas goto done;
5370 48ae06ee 2018-10-18 stsp }
5371 a0f32f33 2022-06-13 thomas
5372 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5373 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5374 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5375 9a1d7435 2022-06-23 thomas goto done;
5376 9a1d7435 2022-06-23 thomas }
5377 9a1d7435 2022-06-23 thomas
5378 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5379 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5380 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5381 a0f32f33 2022-06-13 thomas goto done;
5382 a0f32f33 2022-06-13 thomas }
5383 a0f32f33 2022-06-13 thomas
5384 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5385 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5386 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5387 19a6a6b5 2022-07-01 thomas goto done;
5388 19a6a6b5 2022-07-01 thomas }
5389 19a6a6b5 2022-07-01 thomas
5390 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5391 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5392 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5393 19a6a6b5 2022-07-01 thomas goto done;
5394 19a6a6b5 2022-07-01 thomas }
5395 19a6a6b5 2022-07-01 thomas
5396 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5397 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5398 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5399 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5400 5465d566 2020-02-01 tracey s->repo = repo;
5401 6d17833f 2019-11-08 stsp
5402 557d3365 2023-04-14 thomas if (has_colors() && getenv("TOG_COLORS") != NULL && !using_mock_io) {
5403 6568f0aa 2022-08-06 thomas int rc;
5404 6d17833f 2019-11-08 stsp
5405 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5406 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5407 6568f0aa 2022-08-06 thomas if (rc != ERR)
5408 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5409 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5410 6568f0aa 2022-08-06 thomas if (rc != ERR)
5411 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5412 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5413 6568f0aa 2022-08-06 thomas if (rc != ERR)
5414 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5415 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5416 6568f0aa 2022-08-06 thomas if (rc != ERR)
5417 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5418 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5419 6568f0aa 2022-08-06 thomas if (rc != ERR)
5420 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5421 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5422 6568f0aa 2022-08-06 thomas if (rc != ERR)
5423 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5424 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5425 6568f0aa 2022-08-06 thomas if (rc != ERR)
5426 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5427 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5428 6568f0aa 2022-08-06 thomas if (rc != ERR)
5429 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5430 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5431 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5432 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5433 a0f32f33 2022-06-13 thomas goto done;
5434 6568f0aa 2022-08-06 thomas }
5435 6d17833f 2019-11-08 stsp }
5436 5dc9f4bc 2018-08-04 stsp
5437 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5438 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5439 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5440 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5441 f5215bb9 2019-02-22 stsp
5442 5465d566 2020-02-01 tracey err = create_diff(s);
5443 48ae06ee 2018-10-18 stsp
5444 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5445 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5446 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5447 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5448 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5449 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5450 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5451 a0f32f33 2022-06-13 thomas done:
5452 51b7e1c3 2023-04-14 thomas if (err) {
5453 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
5454 51b7e1c3 2023-04-14 thomas close_diff_view(view);
5455 51b7e1c3 2023-04-14 thomas view_close(view);
5456 51b7e1c3 2023-04-14 thomas }
5457 e5a0f69f 2018-08-18 stsp return err;
5458 5dc9f4bc 2018-08-04 stsp }
5459 5dc9f4bc 2018-08-04 stsp
5460 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5461 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5462 5dc9f4bc 2018-08-04 stsp {
5463 a3404814 2018-09-02 stsp const struct got_error *err;
5464 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5465 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5466 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5467 a3404814 2018-09-02 stsp
5468 a3404814 2018-09-02 stsp if (s->id1) {
5469 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5470 a3404814 2018-09-02 stsp if (err)
5471 a3404814 2018-09-02 stsp return err;
5472 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5473 3dbaef42 2020-11-24 stsp } else
5474 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5475 3dbaef42 2020-11-24 stsp
5476 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5477 a3404814 2018-09-02 stsp if (err)
5478 a3404814 2018-09-02 stsp return err;
5479 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5480 26ed57b2 2018-05-19 stsp
5481 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5482 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5483 a3404814 2018-09-02 stsp free(id_str1);
5484 a3404814 2018-09-02 stsp free(id_str2);
5485 a3404814 2018-09-02 stsp return err;
5486 a3404814 2018-09-02 stsp }
5487 a3404814 2018-09-02 stsp free(id_str1);
5488 a3404814 2018-09-02 stsp free(id_str2);
5489 a3404814 2018-09-02 stsp
5490 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5491 267bb3b8 2021-08-01 stsp free(header);
5492 267bb3b8 2021-08-01 stsp return err;
5493 15a087fe 2019-02-21 stsp }
5494 15a087fe 2019-02-21 stsp
5495 15a087fe 2019-02-21 stsp static const struct got_error *
5496 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5497 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5498 15a087fe 2019-02-21 stsp {
5499 d7a04538 2019-02-21 stsp const struct got_error *err;
5500 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5501 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5502 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5503 15a087fe 2019-02-21 stsp
5504 15a087fe 2019-02-21 stsp free(s->id2);
5505 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5506 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5507 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5508 15a087fe 2019-02-21 stsp
5509 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5510 d7a04538 2019-02-21 stsp if (err)
5511 d7a04538 2019-02-21 stsp return err;
5512 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5513 15a087fe 2019-02-21 stsp free(s->id1);
5514 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5515 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5516 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5517 15a087fe 2019-02-21 stsp return NULL;
5518 0cf4efb1 2018-09-29 stsp }
5519 0cf4efb1 2018-09-29 stsp
5520 0cf4efb1 2018-09-29 stsp static const struct got_error *
5521 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5522 adf4c9e0 2022-07-03 thomas {
5523 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5524 adf4c9e0 2022-07-03 thomas
5525 adf4c9e0 2022-07-03 thomas view->count = 0;
5526 adf4c9e0 2022-07-03 thomas wclear(view->window);
5527 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5528 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5529 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5530 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5531 adf4c9e0 2022-07-03 thomas return create_diff(s);
5532 82c78e96 2022-08-06 thomas }
5533 82c78e96 2022-08-06 thomas
5534 82c78e96 2022-08-06 thomas static void
5535 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5536 82c78e96 2022-08-06 thomas {
5537 82c78e96 2022-08-06 thomas int start, i;
5538 82c78e96 2022-08-06 thomas
5539 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5540 82c78e96 2022-08-06 thomas
5541 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5542 82c78e96 2022-08-06 thomas if (i == 0)
5543 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5544 82c78e96 2022-08-06 thomas if (--i == start)
5545 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5546 82c78e96 2022-08-06 thomas }
5547 82c78e96 2022-08-06 thomas
5548 82c78e96 2022-08-06 thomas s->selected_line = 1;
5549 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5550 adf4c9e0 2022-07-03 thomas }
5551 adf4c9e0 2022-07-03 thomas
5552 82c78e96 2022-08-06 thomas static void
5553 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5554 82c78e96 2022-08-06 thomas {
5555 82c78e96 2022-08-06 thomas int start, i;
5556 82c78e96 2022-08-06 thomas
5557 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5558 82c78e96 2022-08-06 thomas
5559 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5560 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5561 82c78e96 2022-08-06 thomas i = 0;
5562 82c78e96 2022-08-06 thomas if (++i == start)
5563 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5564 82c78e96 2022-08-06 thomas }
5565 82c78e96 2022-08-06 thomas
5566 82c78e96 2022-08-06 thomas s->selected_line = 1;
5567 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5568 82c78e96 2022-08-06 thomas }
5569 82c78e96 2022-08-06 thomas
5570 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5571 4fc71f3b 2022-07-12 thomas int, int, int);
5572 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5573 4fc71f3b 2022-07-12 thomas int, int);
5574 4fc71f3b 2022-07-12 thomas
5575 adf4c9e0 2022-07-03 thomas static const struct got_error *
5576 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5577 e5a0f69f 2018-08-18 stsp {
5578 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5579 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5580 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5581 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5582 826082fe 2020-12-10 stsp char *line = NULL;
5583 826082fe 2020-12-10 stsp size_t linesize = 0;
5584 826082fe 2020-12-10 stsp ssize_t linelen;
5585 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5586 e5a0f69f 2018-08-18 stsp
5587 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5588 82c78e96 2022-08-06 thomas
5589 e5a0f69f 2018-08-18 stsp switch (ch) {
5590 05171be4 2022-06-23 thomas case '0':
5591 05171be4 2022-06-23 thomas case '$':
5592 05171be4 2022-06-23 thomas case KEY_RIGHT:
5593 05171be4 2022-06-23 thomas case 'l':
5594 05171be4 2022-06-23 thomas case KEY_LEFT:
5595 05171be4 2022-06-23 thomas case 'h':
5596 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5597 05171be4 2022-06-23 thomas break;
5598 64453f7e 2020-11-21 stsp case 'a':
5599 3dbaef42 2020-11-24 stsp case 'w':
5600 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5601 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5602 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5603 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5604 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5605 f2d06bef 2023-01-23 thomas }
5606 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5607 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5608 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5609 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5610 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5611 f2d06bef 2023-01-23 thomas }
5612 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5613 912a3f79 2021-08-30 j break;
5614 912a3f79 2021-08-30 j case 'g':
5615 912a3f79 2021-08-30 j case KEY_HOME:
5616 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5617 07b0611c 2022-06-23 thomas view->count = 0;
5618 64453f7e 2020-11-21 stsp break;
5619 912a3f79 2021-08-30 j case 'G':
5620 912a3f79 2021-08-30 j case KEY_END:
5621 07b0611c 2022-06-23 thomas view->count = 0;
5622 912a3f79 2021-08-30 j if (s->eof)
5623 912a3f79 2021-08-30 j break;
5624 912a3f79 2021-08-30 j
5625 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5626 912a3f79 2021-08-30 j s->eof = 1;
5627 912a3f79 2021-08-30 j break;
5628 1e37a5c2 2019-05-12 jcs case 'k':
5629 1e37a5c2 2019-05-12 jcs case KEY_UP:
5630 f7140bf5 2021-10-17 thomas case CTRL('p'):
5631 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5632 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5633 07b0611c 2022-06-23 thomas else
5634 07b0611c 2022-06-23 thomas view->count = 0;
5635 1e37a5c2 2019-05-12 jcs break;
5636 70f17a53 2022-06-13 thomas case CTRL('u'):
5637 23427b14 2022-06-23 thomas case 'u':
5638 70f17a53 2022-06-13 thomas nscroll /= 2;
5639 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5640 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5641 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5642 1c5e5faa 2022-06-23 thomas case 'b':
5643 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5644 07b0611c 2022-06-23 thomas view->count = 0;
5645 26ed57b2 2018-05-19 stsp break;
5646 07b0611c 2022-06-23 thomas }
5647 1e37a5c2 2019-05-12 jcs i = 0;
5648 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5649 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5650 1e37a5c2 2019-05-12 jcs break;
5651 1e37a5c2 2019-05-12 jcs case 'j':
5652 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5653 f7140bf5 2021-10-17 thomas case CTRL('n'):
5654 1e37a5c2 2019-05-12 jcs if (!s->eof)
5655 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5656 07b0611c 2022-06-23 thomas else
5657 07b0611c 2022-06-23 thomas view->count = 0;
5658 1e37a5c2 2019-05-12 jcs break;
5659 70f17a53 2022-06-13 thomas case CTRL('d'):
5660 23427b14 2022-06-23 thomas case 'd':
5661 70f17a53 2022-06-13 thomas nscroll /= 2;
5662 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5663 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5664 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5665 1c5e5faa 2022-06-23 thomas case 'f':
5666 1e37a5c2 2019-05-12 jcs case ' ':
5667 07b0611c 2022-06-23 thomas if (s->eof) {
5668 07b0611c 2022-06-23 thomas view->count = 0;
5669 1e37a5c2 2019-05-12 jcs break;
5670 07b0611c 2022-06-23 thomas }
5671 1e37a5c2 2019-05-12 jcs i = 0;
5672 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5673 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5674 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5675 826082fe 2020-12-10 stsp if (linelen == -1) {
5676 826082fe 2020-12-10 stsp if (feof(s->f)) {
5677 826082fe 2020-12-10 stsp s->eof = 1;
5678 826082fe 2020-12-10 stsp } else
5679 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5680 34bc9ec9 2019-02-22 stsp break;
5681 826082fe 2020-12-10 stsp }
5682 1e37a5c2 2019-05-12 jcs }
5683 826082fe 2020-12-10 stsp free(line);
5684 1e37a5c2 2019-05-12 jcs break;
5685 82c78e96 2022-08-06 thomas case '(':
5686 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5687 82c78e96 2022-08-06 thomas break;
5688 82c78e96 2022-08-06 thomas case ')':
5689 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5690 82c78e96 2022-08-06 thomas break;
5691 82c78e96 2022-08-06 thomas case '{':
5692 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5693 82c78e96 2022-08-06 thomas break;
5694 82c78e96 2022-08-06 thomas case '}':
5695 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5696 82c78e96 2022-08-06 thomas break;
5697 1e37a5c2 2019-05-12 jcs case '[':
5698 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5699 1e37a5c2 2019-05-12 jcs s->diff_context--;
5700 aa61903a 2021-12-31 thomas s->matched_line = 0;
5701 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5702 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5703 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5704 27829c9e 2020-11-21 stsp s->nlines) {
5705 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5706 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5707 27829c9e 2020-11-21 stsp }
5708 07b0611c 2022-06-23 thomas } else
5709 07b0611c 2022-06-23 thomas view->count = 0;
5710 1e37a5c2 2019-05-12 jcs break;
5711 1e37a5c2 2019-05-12 jcs case ']':
5712 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5713 1e37a5c2 2019-05-12 jcs s->diff_context++;
5714 aa61903a 2021-12-31 thomas s->matched_line = 0;
5715 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5716 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5717 07b0611c 2022-06-23 thomas } else
5718 07b0611c 2022-06-23 thomas view->count = 0;
5719 1e37a5c2 2019-05-12 jcs break;
5720 1e37a5c2 2019-05-12 jcs case '<':
5721 1e37a5c2 2019-05-12 jcs case ',':
5722 777aae21 2022-07-20 thomas case 'K':
5723 4fc71f3b 2022-07-12 thomas up = 1;
5724 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5725 4fc71f3b 2022-07-12 thomas case '>':
5726 4fc71f3b 2022-07-12 thomas case '.':
5727 777aae21 2022-07-20 thomas case 'J':
5728 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5729 07b0611c 2022-06-23 thomas view->count = 0;
5730 48ae06ee 2018-10-18 stsp break;
5731 07b0611c 2022-06-23 thomas }
5732 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5733 6524637e 2019-02-21 stsp
5734 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5735 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5736 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5737 15a087fe 2019-02-21 stsp
5738 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5739 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5740 4fc71f3b 2022-07-12 thomas if (err)
5741 4fc71f3b 2022-07-12 thomas break;
5742 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5743 15a087fe 2019-02-21 stsp
5744 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5745 4fc71f3b 2022-07-12 thomas break;
5746 15a087fe 2019-02-21 stsp
5747 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5748 4fc71f3b 2022-07-12 thomas if (err)
5749 4fc71f3b 2022-07-12 thomas break;
5750 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5751 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5752 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5753 5e224a3e 2019-02-22 stsp
5754 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5755 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5756 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5757 4fc71f3b 2022-07-12 thomas
5758 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5759 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5760 4fc71f3b 2022-07-12 thomas if (err)
5761 4fc71f3b 2022-07-12 thomas break;
5762 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5763 5a8b5076 2020-12-05 stsp
5764 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5765 4fc71f3b 2022-07-12 thomas break;
5766 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5767 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5768 4fc71f3b 2022-07-12 thomas bs->selected_line);
5769 4fc71f3b 2022-07-12 thomas if (id == NULL)
5770 4fc71f3b 2022-07-12 thomas break;
5771 15a087fe 2019-02-21 stsp
5772 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5773 4fc71f3b 2022-07-12 thomas break;
5774 15a087fe 2019-02-21 stsp
5775 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5776 4fc71f3b 2022-07-12 thomas if (err)
5777 4fc71f3b 2022-07-12 thomas break;
5778 4fc71f3b 2022-07-12 thomas }
5779 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5780 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5781 aa61903a 2021-12-31 thomas s->matched_line = 0;
5782 05171be4 2022-06-23 thomas view->x = 0;
5783 1e37a5c2 2019-05-12 jcs
5784 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5785 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5786 1e37a5c2 2019-05-12 jcs break;
5787 1e37a5c2 2019-05-12 jcs default:
5788 07b0611c 2022-06-23 thomas view->count = 0;
5789 1e37a5c2 2019-05-12 jcs break;
5790 26ed57b2 2018-05-19 stsp }
5791 e5a0f69f 2018-08-18 stsp
5792 bcbd79e2 2018-08-19 stsp return err;
5793 26ed57b2 2018-05-19 stsp }
5794 26ed57b2 2018-05-19 stsp
5795 4ed7e80c 2018-05-20 stsp static const struct got_error *
5796 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5797 9f7d7167 2018-04-29 stsp {
5798 1d98034b 2023-04-14 thomas const struct got_error *error;
5799 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5800 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5801 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5802 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5803 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5804 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5805 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5806 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5807 3dbaef42 2020-11-24 stsp const char *errstr;
5808 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5809 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5810 70ac5f84 2019-03-28 stsp
5811 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5812 26ed57b2 2018-05-19 stsp switch (ch) {
5813 64453f7e 2020-11-21 stsp case 'a':
5814 64453f7e 2020-11-21 stsp force_text_diff = 1;
5815 3dbaef42 2020-11-24 stsp break;
5816 3dbaef42 2020-11-24 stsp case 'C':
5817 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5818 3dbaef42 2020-11-24 stsp &errstr);
5819 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5820 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5821 8f666e67 2022-02-12 thomas errstr, errstr);
5822 64453f7e 2020-11-21 stsp break;
5823 09b5bff8 2020-02-23 naddy case 'r':
5824 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5825 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5826 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5827 09b5bff8 2020-02-23 naddy optarg);
5828 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5829 09b5bff8 2020-02-23 naddy break;
5830 3dbaef42 2020-11-24 stsp case 'w':
5831 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5832 3dbaef42 2020-11-24 stsp break;
5833 26ed57b2 2018-05-19 stsp default:
5834 17020d27 2019-03-07 stsp usage_diff();
5835 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5836 26ed57b2 2018-05-19 stsp }
5837 26ed57b2 2018-05-19 stsp }
5838 26ed57b2 2018-05-19 stsp
5839 26ed57b2 2018-05-19 stsp argc -= optind;
5840 26ed57b2 2018-05-19 stsp argv += optind;
5841 26ed57b2 2018-05-19 stsp
5842 26ed57b2 2018-05-19 stsp if (argc == 0) {
5843 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5844 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5845 15a94983 2018-12-23 stsp id_str1 = argv[0];
5846 15a94983 2018-12-23 stsp id_str2 = argv[1];
5847 26ed57b2 2018-05-19 stsp } else
5848 26ed57b2 2018-05-19 stsp usage_diff();
5849 eb6600df 2019-01-04 stsp
5850 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5851 7cd52833 2022-06-23 thomas if (error)
5852 7cd52833 2022-06-23 thomas goto done;
5853 7cd52833 2022-06-23 thomas
5854 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5855 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5856 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5857 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5858 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5859 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5860 c156c7a4 2020-12-18 stsp goto done;
5861 a273ac94 2020-02-23 naddy if (worktree)
5862 a273ac94 2020-02-23 naddy repo_path =
5863 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5864 a273ac94 2020-02-23 naddy else
5865 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5866 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5867 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5868 c156c7a4 2020-12-18 stsp goto done;
5869 c156c7a4 2020-12-18 stsp }
5870 a273ac94 2020-02-23 naddy }
5871 a273ac94 2020-02-23 naddy
5872 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5873 eb6600df 2019-01-04 stsp if (error)
5874 eb6600df 2019-01-04 stsp goto done;
5875 26ed57b2 2018-05-19 stsp
5876 557d3365 2023-04-14 thomas init_curses();
5877 a273ac94 2020-02-23 naddy
5878 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5879 51a10b52 2020-12-26 stsp if (error)
5880 51a10b52 2020-12-26 stsp goto done;
5881 51a10b52 2020-12-26 stsp
5882 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5883 26ed57b2 2018-05-19 stsp if (error)
5884 26ed57b2 2018-05-19 stsp goto done;
5885 26ed57b2 2018-05-19 stsp
5886 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5887 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5888 26ed57b2 2018-05-19 stsp if (error)
5889 26ed57b2 2018-05-19 stsp goto done;
5890 26ed57b2 2018-05-19 stsp
5891 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5892 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5893 26ed57b2 2018-05-19 stsp if (error)
5894 26ed57b2 2018-05-19 stsp goto done;
5895 26ed57b2 2018-05-19 stsp
5896 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5897 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5898 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5899 ea5e7bb5 2018-08-01 stsp goto done;
5900 ea5e7bb5 2018-08-01 stsp }
5901 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5902 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5903 5dc9f4bc 2018-08-04 stsp if (error)
5904 5dc9f4bc 2018-08-04 stsp goto done;
5905 557d3365 2023-04-14 thomas error = view_loop(view);
5906 26ed57b2 2018-05-19 stsp done:
5907 3dbaef42 2020-11-24 stsp free(label1);
5908 3dbaef42 2020-11-24 stsp free(label2);
5909 c02c541e 2019-03-29 stsp free(repo_path);
5910 a273ac94 2020-02-23 naddy free(cwd);
5911 1d0f4054 2021-06-17 stsp if (repo) {
5912 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5913 1d0f4054 2021-06-17 stsp if (error == NULL)
5914 1d0f4054 2021-06-17 stsp error = close_err;
5915 1d0f4054 2021-06-17 stsp }
5916 a273ac94 2020-02-23 naddy if (worktree)
5917 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5918 7cd52833 2022-06-23 thomas if (pack_fds) {
5919 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5920 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5921 7cd52833 2022-06-23 thomas if (error == NULL)
5922 7cd52833 2022-06-23 thomas error = pack_err;
5923 b85a3496 2023-04-14 thomas }
5924 51a10b52 2020-12-26 stsp tog_free_refs();
5925 26ed57b2 2018-05-19 stsp return error;
5926 9f7d7167 2018-04-29 stsp }
5927 9f7d7167 2018-04-29 stsp
5928 4ed7e80c 2018-05-20 stsp __dead static void
5929 9f7d7167 2018-04-29 stsp usage_blame(void)
5930 9f7d7167 2018-04-29 stsp {
5931 80ddbec8 2018-04-29 stsp endwin();
5932 91198554 2022-06-23 thomas fprintf(stderr,
5933 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5934 9f7d7167 2018-04-29 stsp getprogname());
5935 9f7d7167 2018-04-29 stsp exit(1);
5936 9f7d7167 2018-04-29 stsp }
5937 84451b3e 2018-07-10 stsp
5938 84451b3e 2018-07-10 stsp struct tog_blame_line {
5939 84451b3e 2018-07-10 stsp int annotated;
5940 84451b3e 2018-07-10 stsp struct got_object_id *id;
5941 84451b3e 2018-07-10 stsp };
5942 9f7d7167 2018-04-29 stsp
5943 4ed7e80c 2018-05-20 stsp static const struct got_error *
5944 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5945 84451b3e 2018-07-10 stsp {
5946 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5947 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5948 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5949 84451b3e 2018-07-10 stsp const struct got_error *err;
5950 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5951 826082fe 2020-12-10 stsp char *line = NULL;
5952 826082fe 2020-12-10 stsp size_t linesize = 0;
5953 826082fe 2020-12-10 stsp ssize_t linelen;
5954 84451b3e 2018-07-10 stsp wchar_t *wline;
5955 27a741e5 2019-09-11 stsp int width;
5956 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5957 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5958 ab089a2a 2018-07-12 stsp char *id_str;
5959 11b20872 2019-11-08 stsp struct tog_color *tc;
5960 ab089a2a 2018-07-12 stsp
5961 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5962 ab089a2a 2018-07-12 stsp if (err)
5963 ab089a2a 2018-07-12 stsp return err;
5964 84451b3e 2018-07-10 stsp
5965 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5966 f7d12f7e 2018-08-01 stsp werase(view->window);
5967 84451b3e 2018-07-10 stsp
5968 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5969 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5970 ab089a2a 2018-07-12 stsp free(id_str);
5971 ab089a2a 2018-07-12 stsp return err;
5972 ab089a2a 2018-07-12 stsp }
5973 ab089a2a 2018-07-12 stsp
5974 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5975 ab089a2a 2018-07-12 stsp free(line);
5976 2550e4c3 2018-07-13 stsp line = NULL;
5977 1cae65b4 2019-09-22 stsp if (err)
5978 1cae65b4 2019-09-22 stsp return err;
5979 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5980 a3404814 2018-09-02 stsp wstandout(view->window);
5981 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5982 11b20872 2019-11-08 stsp if (tc)
5983 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5984 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5985 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5986 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5987 11b20872 2019-11-08 stsp if (tc)
5988 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5989 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5990 a3404814 2018-09-02 stsp wstandend(view->window);
5991 2550e4c3 2018-07-13 stsp free(wline);
5992 2550e4c3 2018-07-13 stsp wline = NULL;
5993 ab089a2a 2018-07-12 stsp
5994 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5995 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5996 07dd3ed3 2022-08-06 thomas
5997 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5998 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5999 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6000 ab089a2a 2018-07-12 stsp free(id_str);
6001 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6002 ab089a2a 2018-07-12 stsp }
6003 ab089a2a 2018-07-12 stsp free(id_str);
6004 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6005 3f60a8ef 2018-07-10 stsp free(line);
6006 2550e4c3 2018-07-13 stsp line = NULL;
6007 3f60a8ef 2018-07-10 stsp if (err)
6008 3f60a8ef 2018-07-10 stsp return err;
6009 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6010 2550e4c3 2018-07-13 stsp free(wline);
6011 2550e4c3 2018-07-13 stsp wline = NULL;
6012 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6013 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6014 3f60a8ef 2018-07-10 stsp
6015 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6016 05171be4 2022-06-23 thomas view->maxx = 0;
6017 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6018 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6019 826082fe 2020-12-10 stsp if (linelen == -1) {
6020 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6021 826082fe 2020-12-10 stsp s->eof = 1;
6022 826082fe 2020-12-10 stsp break;
6023 826082fe 2020-12-10 stsp }
6024 84451b3e 2018-07-10 stsp free(line);
6025 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6026 84451b3e 2018-07-10 stsp }
6027 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6028 07dd3ed3 2022-08-06 thomas continue;
6029 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
6030 826082fe 2020-12-10 stsp continue;
6031 cbea3800 2022-06-23 thomas
6032 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
6033 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6034 cbea3800 2022-06-23 thomas if (err) {
6035 cbea3800 2022-06-23 thomas free(line);
6036 cbea3800 2022-06-23 thomas return err;
6037 cbea3800 2022-06-23 thomas }
6038 cbea3800 2022-06-23 thomas free(wline);
6039 cbea3800 2022-06-23 thomas wline = NULL;
6040 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
6041 84451b3e 2018-07-10 stsp
6042 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6043 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6044 b700b5d6 2018-07-10 stsp
6045 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6046 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6047 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6048 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6049 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
6050 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6051 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6052 8d0fe45a 2019-08-12 stsp char *id_str;
6053 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
6054 91198554 2022-06-23 thomas blame_line->id);
6055 8d0fe45a 2019-08-12 stsp if (err) {
6056 8d0fe45a 2019-08-12 stsp free(line);
6057 8d0fe45a 2019-08-12 stsp return err;
6058 8d0fe45a 2019-08-12 stsp }
6059 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6060 11b20872 2019-11-08 stsp if (tc)
6061 11b20872 2019-11-08 stsp wattr_on(view->window,
6062 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6063 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6064 11b20872 2019-11-08 stsp if (tc)
6065 11b20872 2019-11-08 stsp wattr_off(view->window,
6066 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6067 8d0fe45a 2019-08-12 stsp free(id_str);
6068 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6069 8d0fe45a 2019-08-12 stsp } else {
6070 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6071 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6072 84451b3e 2018-07-10 stsp }
6073 ee41ec32 2018-07-10 stsp } else {
6074 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6075 ee41ec32 2018-07-10 stsp prev_id = NULL;
6076 ee41ec32 2018-07-10 stsp }
6077 84451b3e 2018-07-10 stsp
6078 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6079 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6080 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6081 27a741e5 2019-09-11 stsp
6082 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6083 41605754 2020-11-12 stsp width = 9;
6084 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6085 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6086 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6087 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6088 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
6089 41605754 2020-11-12 stsp if (err) {
6090 41605754 2020-11-12 stsp free(line);
6091 41605754 2020-11-12 stsp return err;
6092 41605754 2020-11-12 stsp }
6093 41605754 2020-11-12 stsp width += 9;
6094 41605754 2020-11-12 stsp } else {
6095 923086fd 2022-06-23 thomas int skip;
6096 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
6097 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
6098 923086fd 2022-06-23 thomas if (err) {
6099 923086fd 2022-06-23 thomas free(line);
6100 923086fd 2022-06-23 thomas return err;
6101 05171be4 2022-06-23 thomas }
6102 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
6103 02bce7e2 2022-06-23 thomas width += 9;
6104 41605754 2020-11-12 stsp free(wline);
6105 41605754 2020-11-12 stsp wline = NULL;
6106 41605754 2020-11-12 stsp }
6107 41605754 2020-11-12 stsp
6108 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6109 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6110 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6111 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6112 84451b3e 2018-07-10 stsp }
6113 826082fe 2020-12-10 stsp free(line);
6114 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6115 84451b3e 2018-07-10 stsp
6116 a5d43cac 2022-07-01 thomas view_border(view);
6117 84451b3e 2018-07-10 stsp
6118 84451b3e 2018-07-10 stsp return NULL;
6119 84451b3e 2018-07-10 stsp }
6120 84451b3e 2018-07-10 stsp
6121 84451b3e 2018-07-10 stsp static const struct got_error *
6122 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
6123 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
6124 84451b3e 2018-07-10 stsp {
6125 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6126 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6127 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6128 1a76625f 2018-10-22 stsp int errcode;
6129 84451b3e 2018-07-10 stsp
6130 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6131 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6132 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6133 84451b3e 2018-07-10 stsp
6134 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6135 1a76625f 2018-10-22 stsp if (errcode)
6136 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6137 84451b3e 2018-07-10 stsp
6138 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6139 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6140 d68a0a7d 2018-07-10 stsp goto done;
6141 d68a0a7d 2018-07-10 stsp }
6142 d68a0a7d 2018-07-10 stsp
6143 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6144 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6145 d68a0a7d 2018-07-10 stsp
6146 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6147 d68a0a7d 2018-07-10 stsp if (line->annotated)
6148 d68a0a7d 2018-07-10 stsp goto done;
6149 d68a0a7d 2018-07-10 stsp
6150 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6151 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6152 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6153 84451b3e 2018-07-10 stsp goto done;
6154 84451b3e 2018-07-10 stsp }
6155 84451b3e 2018-07-10 stsp line->annotated = 1;
6156 84451b3e 2018-07-10 stsp done:
6157 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6158 1a76625f 2018-10-22 stsp if (errcode)
6159 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6160 84451b3e 2018-07-10 stsp return err;
6161 84451b3e 2018-07-10 stsp }
6162 84451b3e 2018-07-10 stsp
6163 84451b3e 2018-07-10 stsp static void *
6164 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6165 84451b3e 2018-07-10 stsp {
6166 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6167 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6168 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6169 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
6170 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
6171 00a8878e 2022-07-01 thomas
6172 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
6173 9117a7b7 2022-07-01 thomas if (fd1 == -1)
6174 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
6175 9117a7b7 2022-07-01 thomas
6176 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
6177 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
6178 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6179 9117a7b7 2022-07-01 thomas goto done;
6180 9117a7b7 2022-07-01 thomas }
6181 18430de3 2018-07-10 stsp
6182 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
6183 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
6184 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6185 9117a7b7 2022-07-01 thomas goto done;
6186 9117a7b7 2022-07-01 thomas }
6187 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
6188 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
6189 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6190 9117a7b7 2022-07-01 thomas goto done;
6191 9117a7b7 2022-07-01 thomas }
6192 9117a7b7 2022-07-01 thomas
6193 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6194 61266923 2020-01-14 stsp if (err)
6195 9117a7b7 2022-07-01 thomas goto done;
6196 61266923 2020-01-14 stsp
6197 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6198 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
6199 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6200 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6201 fc06ba56 2019-08-22 stsp err = NULL;
6202 18430de3 2018-07-10 stsp
6203 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6204 9117a7b7 2022-07-01 thomas if (errcode) {
6205 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
6206 9117a7b7 2022-07-01 thomas goto done;
6207 9117a7b7 2022-07-01 thomas }
6208 18430de3 2018-07-10 stsp
6209 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6210 1d0f4054 2021-06-17 stsp if (err == NULL)
6211 1d0f4054 2021-06-17 stsp err = close_err;
6212 c9beca56 2018-07-22 stsp ta->repo = NULL;
6213 c9beca56 2018-07-22 stsp *ta->complete = 1;
6214 18430de3 2018-07-10 stsp
6215 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6216 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6217 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6218 18430de3 2018-07-10 stsp
6219 9117a7b7 2022-07-01 thomas done:
6220 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6221 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
6222 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6223 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
6224 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
6225 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6226 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
6227 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6228 00a8878e 2022-07-01 thomas
6229 18430de3 2018-07-10 stsp return (void *)err;
6230 84451b3e 2018-07-10 stsp }
6231 84451b3e 2018-07-10 stsp
6232 245d91c1 2018-07-12 stsp static struct got_object_id *
6233 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6234 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6235 245d91c1 2018-07-12 stsp {
6236 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6237 8d0fe45a 2019-08-12 stsp
6238 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6239 8d0fe45a 2019-08-12 stsp return NULL;
6240 b880a918 2018-07-10 stsp
6241 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6242 4fc71f3b 2022-07-12 thomas if (!line->annotated)
6243 4fc71f3b 2022-07-12 thomas return NULL;
6244 4fc71f3b 2022-07-12 thomas
6245 4fc71f3b 2022-07-12 thomas return line->id;
6246 4fc71f3b 2022-07-12 thomas }
6247 4fc71f3b 2022-07-12 thomas
6248 4fc71f3b 2022-07-12 thomas static struct got_object_id *
6249 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6250 4fc71f3b 2022-07-12 thomas int lineno)
6251 4fc71f3b 2022-07-12 thomas {
6252 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
6253 4fc71f3b 2022-07-12 thomas
6254 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6255 4fc71f3b 2022-07-12 thomas return NULL;
6256 4fc71f3b 2022-07-12 thomas
6257 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6258 245d91c1 2018-07-12 stsp if (!line->annotated)
6259 245d91c1 2018-07-12 stsp return NULL;
6260 245d91c1 2018-07-12 stsp
6261 245d91c1 2018-07-12 stsp return line->id;
6262 b880a918 2018-07-10 stsp }
6263 245d91c1 2018-07-12 stsp
6264 b880a918 2018-07-10 stsp static const struct got_error *
6265 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6266 a70480e0 2018-06-23 stsp {
6267 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6268 245d91c1 2018-07-12 stsp int i;
6269 245d91c1 2018-07-12 stsp
6270 245d91c1 2018-07-12 stsp if (blame->thread) {
6271 1a76625f 2018-10-22 stsp int errcode;
6272 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6273 1a76625f 2018-10-22 stsp if (errcode)
6274 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6275 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6276 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6277 1a76625f 2018-10-22 stsp if (errcode)
6278 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6279 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6280 1a76625f 2018-10-22 stsp if (errcode)
6281 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6282 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6283 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6284 245d91c1 2018-07-12 stsp err = NULL;
6285 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6286 245d91c1 2018-07-12 stsp }
6287 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6288 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6289 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6290 1d0f4054 2021-06-17 stsp if (err == NULL)
6291 1d0f4054 2021-06-17 stsp err = close_err;
6292 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6293 245d91c1 2018-07-12 stsp }
6294 245d91c1 2018-07-12 stsp if (blame->f) {
6295 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6296 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6297 245d91c1 2018-07-12 stsp blame->f = NULL;
6298 245d91c1 2018-07-12 stsp }
6299 57670559 2018-12-23 stsp if (blame->lines) {
6300 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6301 57670559 2018-12-23 stsp free(blame->lines[i].id);
6302 57670559 2018-12-23 stsp free(blame->lines);
6303 57670559 2018-12-23 stsp blame->lines = NULL;
6304 57670559 2018-12-23 stsp }
6305 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6306 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6307 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6308 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6309 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6310 7cd52833 2022-06-23 thomas if (err == NULL)
6311 7cd52833 2022-06-23 thomas err = pack_err;
6312 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6313 7cd52833 2022-06-23 thomas }
6314 245d91c1 2018-07-12 stsp return err;
6315 245d91c1 2018-07-12 stsp }
6316 245d91c1 2018-07-12 stsp
6317 245d91c1 2018-07-12 stsp static const struct got_error *
6318 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6319 fc06ba56 2019-08-22 stsp {
6320 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6321 fc06ba56 2019-08-22 stsp int *done = arg;
6322 fc06ba56 2019-08-22 stsp int errcode;
6323 fc06ba56 2019-08-22 stsp
6324 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6325 fc06ba56 2019-08-22 stsp if (errcode)
6326 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6327 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6328 fc06ba56 2019-08-22 stsp
6329 fc06ba56 2019-08-22 stsp if (*done)
6330 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6331 fc06ba56 2019-08-22 stsp
6332 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6333 fc06ba56 2019-08-22 stsp if (errcode)
6334 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6335 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6336 fc06ba56 2019-08-22 stsp
6337 fc06ba56 2019-08-22 stsp return err;
6338 fc06ba56 2019-08-22 stsp }
6339 fc06ba56 2019-08-22 stsp
6340 fc06ba56 2019-08-22 stsp static const struct got_error *
6341 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6342 245d91c1 2018-07-12 stsp {
6343 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6344 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6345 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6346 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6347 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6348 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6349 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6350 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6351 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6352 a70480e0 2018-06-23 stsp
6353 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6354 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6355 27d434c2 2018-09-15 stsp if (err)
6356 15a94983 2018-12-23 stsp return err;
6357 f4ae6ddb 2022-07-01 thomas
6358 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6359 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6360 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6361 f4ae6ddb 2022-07-01 thomas goto done;
6362 f4ae6ddb 2022-07-01 thomas }
6363 945f9229 2022-04-16 thomas
6364 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6365 945f9229 2022-04-16 thomas if (err)
6366 945f9229 2022-04-16 thomas goto done;
6367 27d434c2 2018-09-15 stsp
6368 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6369 84451b3e 2018-07-10 stsp if (err)
6370 84451b3e 2018-07-10 stsp goto done;
6371 27d434c2 2018-09-15 stsp
6372 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6373 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6374 84451b3e 2018-07-10 stsp goto done;
6375 84451b3e 2018-07-10 stsp }
6376 a70480e0 2018-06-23 stsp
6377 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6378 a70480e0 2018-06-23 stsp if (err)
6379 a70480e0 2018-06-23 stsp goto done;
6380 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6381 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6382 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6383 84451b3e 2018-07-10 stsp goto done;
6384 84451b3e 2018-07-10 stsp }
6385 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6386 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6387 1fddf795 2021-01-20 stsp if (err)
6388 1fddf795 2021-01-20 stsp goto done;
6389 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6390 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6391 84451b3e 2018-07-10 stsp goto done;
6392 1fddf795 2021-01-20 stsp }
6393 b02560ec 2019-08-19 stsp
6394 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6395 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6396 b02560ec 2019-08-19 stsp blame->nlines--;
6397 a70480e0 2018-06-23 stsp
6398 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6399 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6400 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6401 84451b3e 2018-07-10 stsp goto done;
6402 84451b3e 2018-07-10 stsp }
6403 a70480e0 2018-06-23 stsp
6404 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6405 bd24772e 2018-07-11 stsp if (err)
6406 bd24772e 2018-07-11 stsp goto done;
6407 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6408 7cd52833 2022-06-23 thomas pack_fds);
6409 7cd52833 2022-06-23 thomas if (err)
6410 7cd52833 2022-06-23 thomas goto done;
6411 bd24772e 2018-07-11 stsp
6412 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6413 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6414 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6415 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6416 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6417 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6418 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6419 245d91c1 2018-07-12 stsp goto done;
6420 245d91c1 2018-07-12 stsp }
6421 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6422 245d91c1 2018-07-12 stsp
6423 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6424 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6425 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6426 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6427 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6428 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6429 a5388363 2020-12-01 naddy s->blame_complete = 0;
6430 f5a09613 2020-12-13 naddy
6431 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6432 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6433 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6434 f5a09613 2020-12-13 naddy s->selected_line = 1;
6435 f5a09613 2020-12-13 naddy }
6436 aa61903a 2021-12-31 thomas s->matched_line = 0;
6437 245d91c1 2018-07-12 stsp
6438 245d91c1 2018-07-12 stsp done:
6439 945f9229 2022-04-16 thomas if (commit)
6440 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6441 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6442 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6443 245d91c1 2018-07-12 stsp if (blob)
6444 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6445 27d434c2 2018-09-15 stsp free(obj_id);
6446 245d91c1 2018-07-12 stsp if (err)
6447 245d91c1 2018-07-12 stsp stop_blame(blame);
6448 245d91c1 2018-07-12 stsp return err;
6449 245d91c1 2018-07-12 stsp }
6450 245d91c1 2018-07-12 stsp
6451 245d91c1 2018-07-12 stsp static const struct got_error *
6452 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6453 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6454 245d91c1 2018-07-12 stsp {
6455 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6456 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6457 dbc6a6b6 2018-07-12 stsp
6458 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6459 245d91c1 2018-07-12 stsp
6460 c4843652 2019-08-12 stsp s->path = strdup(path);
6461 c4843652 2019-08-12 stsp if (s->path == NULL)
6462 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6463 c4843652 2019-08-12 stsp
6464 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6465 c4843652 2019-08-12 stsp if (err) {
6466 c4843652 2019-08-12 stsp free(s->path);
6467 7cbe629d 2018-08-04 stsp return err;
6468 c4843652 2019-08-12 stsp }
6469 245d91c1 2018-07-12 stsp
6470 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6471 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6472 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6473 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6474 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6475 fb2756b9 2018-08-04 stsp s->repo = repo;
6476 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6477 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6478 7cbe629d 2018-08-04 stsp
6479 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6480 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6481 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6482 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6483 11b20872 2019-11-08 stsp if (err)
6484 11b20872 2019-11-08 stsp return err;
6485 11b20872 2019-11-08 stsp }
6486 11b20872 2019-11-08 stsp
6487 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6488 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6489 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6490 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6491 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6492 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6493 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6494 e5a0f69f 2018-08-18 stsp
6495 a5388363 2020-12-01 naddy return run_blame(view);
6496 7cbe629d 2018-08-04 stsp }
6497 7cbe629d 2018-08-04 stsp
6498 e5a0f69f 2018-08-18 stsp static const struct got_error *
6499 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6500 7cbe629d 2018-08-04 stsp {
6501 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6502 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6503 7cbe629d 2018-08-04 stsp
6504 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6505 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6506 e5a0f69f 2018-08-18 stsp
6507 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6508 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6509 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6510 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6511 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6512 7cbe629d 2018-08-04 stsp }
6513 e5a0f69f 2018-08-18 stsp
6514 e5a0f69f 2018-08-18 stsp free(s->path);
6515 11b20872 2019-11-08 stsp free_colors(&s->colors);
6516 e5a0f69f 2018-08-18 stsp return err;
6517 7cbe629d 2018-08-04 stsp }
6518 7cbe629d 2018-08-04 stsp
6519 7cbe629d 2018-08-04 stsp static const struct got_error *
6520 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6521 6c4c42e0 2019-06-24 stsp {
6522 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6523 6c4c42e0 2019-06-24 stsp
6524 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6525 6c4c42e0 2019-06-24 stsp return NULL;
6526 2a7d3cd7 2022-09-17 thomas }
6527 2a7d3cd7 2022-09-17 thomas
6528 2a7d3cd7 2022-09-17 thomas static void
6529 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6530 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6531 2a7d3cd7 2022-09-17 thomas {
6532 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6533 2a7d3cd7 2022-09-17 thomas
6534 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6535 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6536 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6537 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6538 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6539 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6540 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6541 6c4c42e0 2019-06-24 stsp }
6542 6c4c42e0 2019-06-24 stsp
6543 6c4c42e0 2019-06-24 stsp static const struct got_error *
6544 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6545 7cbe629d 2018-08-04 stsp {
6546 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6547 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6548 2b380cc8 2018-10-24 stsp int errcode;
6549 2b380cc8 2018-10-24 stsp
6550 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6551 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6552 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6553 2b380cc8 2018-10-24 stsp if (errcode)
6554 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6555 51fe7530 2019-08-19 stsp
6556 557d3365 2023-04-14 thomas if (!using_mock_io)
6557 557d3365 2023-04-14 thomas halfdelay(1); /* fast refresh while annotating */
6558 2b380cc8 2018-10-24 stsp }
6559 e5a0f69f 2018-08-18 stsp
6560 557d3365 2023-04-14 thomas if (s->blame_complete && !using_mock_io)
6561 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6562 51fe7530 2019-08-19 stsp
6563 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6564 e5a0f69f 2018-08-18 stsp
6565 a5d43cac 2022-07-01 thomas view_border(view);
6566 e5a0f69f 2018-08-18 stsp return err;
6567 e5a0f69f 2018-08-18 stsp }
6568 e5a0f69f 2018-08-18 stsp
6569 e5a0f69f 2018-08-18 stsp static const struct got_error *
6570 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6571 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6572 eaeaa612 2022-07-20 thomas {
6573 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6574 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6575 eaeaa612 2022-07-20 thomas
6576 eaeaa612 2022-07-20 thomas *new_view = NULL;
6577 eaeaa612 2022-07-20 thomas
6578 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6579 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6580 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6581 eaeaa612 2022-07-20 thomas
6582 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6583 eaeaa612 2022-07-20 thomas if (err)
6584 eaeaa612 2022-07-20 thomas view_close(log_view);
6585 eaeaa612 2022-07-20 thomas else
6586 eaeaa612 2022-07-20 thomas *new_view = log_view;
6587 eaeaa612 2022-07-20 thomas
6588 eaeaa612 2022-07-20 thomas return err;
6589 eaeaa612 2022-07-20 thomas }
6590 eaeaa612 2022-07-20 thomas
6591 eaeaa612 2022-07-20 thomas static const struct got_error *
6592 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6593 e5a0f69f 2018-08-18 stsp {
6594 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6595 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6596 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6597 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6598 a5d43cac 2022-07-01 thomas
6599 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6600 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6601 a5d43cac 2022-07-01 thomas --eos; /* border */
6602 7cbe629d 2018-08-04 stsp
6603 e5a0f69f 2018-08-18 stsp switch (ch) {
6604 05171be4 2022-06-23 thomas case '0':
6605 05171be4 2022-06-23 thomas case '$':
6606 05171be4 2022-06-23 thomas case KEY_RIGHT:
6607 05171be4 2022-06-23 thomas case 'l':
6608 05171be4 2022-06-23 thomas case KEY_LEFT:
6609 05171be4 2022-06-23 thomas case 'h':
6610 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6611 05171be4 2022-06-23 thomas break;
6612 1e37a5c2 2019-05-12 jcs case 'q':
6613 1e37a5c2 2019-05-12 jcs s->done = 1;
6614 4deef56f 2021-09-02 naddy break;
6615 4deef56f 2021-09-02 naddy case 'g':
6616 4deef56f 2021-09-02 naddy case KEY_HOME:
6617 4deef56f 2021-09-02 naddy s->selected_line = 1;
6618 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6619 07b0611c 2022-06-23 thomas view->count = 0;
6620 4deef56f 2021-09-02 naddy break;
6621 4deef56f 2021-09-02 naddy case 'G':
6622 4deef56f 2021-09-02 naddy case KEY_END:
6623 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6624 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6625 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6626 4deef56f 2021-09-02 naddy } else {
6627 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6628 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6629 4deef56f 2021-09-02 naddy }
6630 07b0611c 2022-06-23 thomas view->count = 0;
6631 1e37a5c2 2019-05-12 jcs break;
6632 1e37a5c2 2019-05-12 jcs case 'k':
6633 1e37a5c2 2019-05-12 jcs case KEY_UP:
6634 f7140bf5 2021-10-17 thomas case CTRL('p'):
6635 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6636 1e37a5c2 2019-05-12 jcs s->selected_line--;
6637 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6638 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6639 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6640 07b0611c 2022-06-23 thomas else
6641 07b0611c 2022-06-23 thomas view->count = 0;
6642 1e37a5c2 2019-05-12 jcs break;
6643 70f17a53 2022-06-13 thomas case CTRL('u'):
6644 23427b14 2022-06-23 thomas case 'u':
6645 70f17a53 2022-06-13 thomas nscroll /= 2;
6646 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6647 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6648 ea025d1d 2020-02-22 naddy case CTRL('b'):
6649 1c5e5faa 2022-06-23 thomas case 'b':
6650 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6651 07b0611c 2022-06-23 thomas if (view->count > 1)
6652 07b0611c 2022-06-23 thomas nscroll += nscroll;
6653 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6654 07b0611c 2022-06-23 thomas view->count = 0;
6655 e5a0f69f 2018-08-18 stsp break;
6656 1e37a5c2 2019-05-12 jcs }
6657 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6658 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6659 1e37a5c2 2019-05-12 jcs else
6660 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6661 1e37a5c2 2019-05-12 jcs break;
6662 1e37a5c2 2019-05-12 jcs case 'j':
6663 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6664 f7140bf5 2021-10-17 thomas case CTRL('n'):
6665 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6666 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6667 1e37a5c2 2019-05-12 jcs s->selected_line++;
6668 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6669 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6670 07b0611c 2022-06-23 thomas else
6671 07b0611c 2022-06-23 thomas view->count = 0;
6672 1e37a5c2 2019-05-12 jcs break;
6673 1c5e5faa 2022-06-23 thomas case 'c':
6674 1e37a5c2 2019-05-12 jcs case 'p': {
6675 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6676 07b0611c 2022-06-23 thomas
6677 07b0611c 2022-06-23 thomas view->count = 0;
6678 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6679 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6680 1e37a5c2 2019-05-12 jcs if (id == NULL)
6681 e5a0f69f 2018-08-18 stsp break;
6682 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6683 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6684 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6685 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6686 1e37a5c2 2019-05-12 jcs int obj_type;
6687 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6688 1e37a5c2 2019-05-12 jcs s->repo, id);
6689 e5a0f69f 2018-08-18 stsp if (err)
6690 e5a0f69f 2018-08-18 stsp break;
6691 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6692 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6693 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6694 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6695 e5a0f69f 2018-08-18 stsp break;
6696 e5a0f69f 2018-08-18 stsp }
6697 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6698 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6699 ec242592 2022-04-22 thomas s->repo, &pid->id);
6700 945f9229 2022-04-16 thomas if (err)
6701 945f9229 2022-04-16 thomas break;
6702 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6703 945f9229 2022-04-16 thomas pcommit, s->path);
6704 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6705 e5a0f69f 2018-08-18 stsp if (err) {
6706 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6707 1e37a5c2 2019-05-12 jcs err = NULL;
6708 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6709 e5a0f69f 2018-08-18 stsp break;
6710 e5a0f69f 2018-08-18 stsp }
6711 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6712 1e37a5c2 2019-05-12 jcs blob_id);
6713 1e37a5c2 2019-05-12 jcs free(blob_id);
6714 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6715 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6716 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6717 e5a0f69f 2018-08-18 stsp break;
6718 1e37a5c2 2019-05-12 jcs }
6719 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6720 ec242592 2022-04-22 thomas &pid->id);
6721 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6722 1e37a5c2 2019-05-12 jcs } else {
6723 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6724 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6725 1e37a5c2 2019-05-12 jcs break;
6726 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6727 1e37a5c2 2019-05-12 jcs id);
6728 1e37a5c2 2019-05-12 jcs }
6729 1e37a5c2 2019-05-12 jcs if (err)
6730 e5a0f69f 2018-08-18 stsp break;
6731 1e37a5c2 2019-05-12 jcs s->done = 1;
6732 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6733 1e37a5c2 2019-05-12 jcs s->done = 0;
6734 1e37a5c2 2019-05-12 jcs if (thread_err)
6735 1e37a5c2 2019-05-12 jcs break;
6736 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6737 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6738 a5388363 2020-12-01 naddy err = run_blame(view);
6739 1e37a5c2 2019-05-12 jcs if (err)
6740 1e37a5c2 2019-05-12 jcs break;
6741 1e37a5c2 2019-05-12 jcs break;
6742 1e37a5c2 2019-05-12 jcs }
6743 1c5e5faa 2022-06-23 thomas case 'C': {
6744 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6745 07b0611c 2022-06-23 thomas
6746 07b0611c 2022-06-23 thomas view->count = 0;
6747 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6748 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6749 1e37a5c2 2019-05-12 jcs break;
6750 1e37a5c2 2019-05-12 jcs s->done = 1;
6751 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6752 1e37a5c2 2019-05-12 jcs s->done = 0;
6753 1e37a5c2 2019-05-12 jcs if (thread_err)
6754 1e37a5c2 2019-05-12 jcs break;
6755 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6756 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6757 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6758 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6759 a5388363 2020-12-01 naddy err = run_blame(view);
6760 1e37a5c2 2019-05-12 jcs if (err)
6761 1e37a5c2 2019-05-12 jcs break;
6762 1e37a5c2 2019-05-12 jcs break;
6763 1e37a5c2 2019-05-12 jcs }
6764 2a31b33b 2022-07-23 thomas case 'L':
6765 eaeaa612 2022-07-20 thomas view->count = 0;
6766 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6767 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6768 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6769 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6770 eaeaa612 2022-07-20 thomas break;
6771 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6772 1e37a5c2 2019-05-12 jcs case '\r': {
6773 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6774 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6775 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6776 07b0611c 2022-06-23 thomas
6777 07b0611c 2022-06-23 thomas view->count = 0;
6778 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6779 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6780 1e37a5c2 2019-05-12 jcs if (id == NULL)
6781 1e37a5c2 2019-05-12 jcs break;
6782 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6783 1e37a5c2 2019-05-12 jcs if (err)
6784 1e37a5c2 2019-05-12 jcs break;
6785 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6786 4fc71f3b 2022-07-12 thomas if (*new_view) {
6787 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6788 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6789 4fc71f3b 2022-07-12 thomas if (err)
6790 4fc71f3b 2022-07-12 thomas break;
6791 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6792 4fc71f3b 2022-07-12 thomas } else {
6793 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6794 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6795 a5d43cac 2022-07-01 thomas
6796 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6797 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6798 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6799 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6800 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6801 4fc71f3b 2022-07-12 thomas break;
6802 4fc71f3b 2022-07-12 thomas }
6803 15a94983 2018-12-23 stsp }
6804 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6805 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6806 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6807 1e37a5c2 2019-05-12 jcs if (err) {
6808 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6809 1e37a5c2 2019-05-12 jcs break;
6810 1e37a5c2 2019-05-12 jcs }
6811 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6812 4fc71f3b 2022-07-12 thomas s->selected_line;
6813 4fc71f3b 2022-07-12 thomas if (*new_view)
6814 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6815 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6816 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6817 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6818 a5d43cac 2022-07-01 thomas if (err)
6819 a5d43cac 2022-07-01 thomas break;
6820 a5d43cac 2022-07-01 thomas }
6821 a5d43cac 2022-07-01 thomas
6822 e78dc838 2020-12-04 stsp view->focussed = 0;
6823 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6824 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6825 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6826 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6827 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6828 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6829 1e37a5c2 2019-05-12 jcs if (err)
6830 34bc9ec9 2019-02-22 stsp break;
6831 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6832 40236d76 2022-06-23 thomas if (err)
6833 40236d76 2022-06-23 thomas break;
6834 e78dc838 2020-12-04 stsp view->focus_child = 1;
6835 1e37a5c2 2019-05-12 jcs } else
6836 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6837 1e37a5c2 2019-05-12 jcs if (err)
6838 e5a0f69f 2018-08-18 stsp break;
6839 1e37a5c2 2019-05-12 jcs break;
6840 1e37a5c2 2019-05-12 jcs }
6841 70f17a53 2022-06-13 thomas case CTRL('d'):
6842 23427b14 2022-06-23 thomas case 'd':
6843 70f17a53 2022-06-13 thomas nscroll /= 2;
6844 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6845 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6846 ea025d1d 2020-02-22 naddy case CTRL('f'):
6847 1c5e5faa 2022-06-23 thomas case 'f':
6848 1e37a5c2 2019-05-12 jcs case ' ':
6849 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6850 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6851 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6852 07b0611c 2022-06-23 thomas view->count = 0;
6853 e5a0f69f 2018-08-18 stsp break;
6854 1e37a5c2 2019-05-12 jcs }
6855 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6856 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6857 70f17a53 2022-06-13 thomas s->selected_line +=
6858 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6859 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6860 1e37a5c2 2019-05-12 jcs }
6861 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6862 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6863 1e37a5c2 2019-05-12 jcs else
6864 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6865 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6866 1e37a5c2 2019-05-12 jcs break;
6867 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6868 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6869 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6870 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6871 1e37a5c2 2019-05-12 jcs }
6872 1e37a5c2 2019-05-12 jcs break;
6873 1e37a5c2 2019-05-12 jcs default:
6874 07b0611c 2022-06-23 thomas view->count = 0;
6875 1e37a5c2 2019-05-12 jcs break;
6876 a70480e0 2018-06-23 stsp }
6877 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6878 adf4c9e0 2022-07-03 thomas }
6879 adf4c9e0 2022-07-03 thomas
6880 adf4c9e0 2022-07-03 thomas static const struct got_error *
6881 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6882 adf4c9e0 2022-07-03 thomas {
6883 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6884 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6885 adf4c9e0 2022-07-03 thomas
6886 adf4c9e0 2022-07-03 thomas view->count = 0;
6887 adf4c9e0 2022-07-03 thomas s->done = 1;
6888 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6889 adf4c9e0 2022-07-03 thomas s->done = 0;
6890 adf4c9e0 2022-07-03 thomas if (err)
6891 adf4c9e0 2022-07-03 thomas return err;
6892 adf4c9e0 2022-07-03 thomas return run_blame(view);
6893 a70480e0 2018-06-23 stsp }
6894 a70480e0 2018-06-23 stsp
6895 a70480e0 2018-06-23 stsp static const struct got_error *
6896 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6897 9f7d7167 2018-04-29 stsp {
6898 1d98034b 2023-04-14 thomas const struct got_error *error;
6899 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6900 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6901 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6902 0587e10c 2020-07-23 stsp char *link_target = NULL;
6903 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6904 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6905 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6906 a70480e0 2018-06-23 stsp int ch;
6907 51b7e1c3 2023-04-14 thomas struct tog_view *view = NULL;
6908 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6909 a70480e0 2018-06-23 stsp
6910 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6911 a70480e0 2018-06-23 stsp switch (ch) {
6912 a70480e0 2018-06-23 stsp case 'c':
6913 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6914 a70480e0 2018-06-23 stsp break;
6915 69069811 2018-08-02 stsp case 'r':
6916 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6917 69069811 2018-08-02 stsp if (repo_path == NULL)
6918 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6919 9ba1d308 2019-10-21 stsp optarg);
6920 69069811 2018-08-02 stsp break;
6921 a70480e0 2018-06-23 stsp default:
6922 17020d27 2019-03-07 stsp usage_blame();
6923 a70480e0 2018-06-23 stsp /* NOTREACHED */
6924 a70480e0 2018-06-23 stsp }
6925 a70480e0 2018-06-23 stsp }
6926 a70480e0 2018-06-23 stsp
6927 a70480e0 2018-06-23 stsp argc -= optind;
6928 a70480e0 2018-06-23 stsp argv += optind;
6929 a70480e0 2018-06-23 stsp
6930 f135c941 2020-02-20 stsp if (argc != 1)
6931 a70480e0 2018-06-23 stsp usage_blame();
6932 6962eb72 2020-02-20 stsp
6933 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6934 7cd52833 2022-06-23 thomas if (error != NULL)
6935 7cd52833 2022-06-23 thomas goto done;
6936 7cd52833 2022-06-23 thomas
6937 69069811 2018-08-02 stsp if (repo_path == NULL) {
6938 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6939 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6940 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6941 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6942 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6943 c156c7a4 2020-12-18 stsp goto done;
6944 f135c941 2020-02-20 stsp if (worktree)
6945 eb41ed75 2019-02-05 stsp repo_path =
6946 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6947 f135c941 2020-02-20 stsp else
6948 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6949 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6950 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6951 c156c7a4 2020-12-18 stsp goto done;
6952 c156c7a4 2020-12-18 stsp }
6953 f135c941 2020-02-20 stsp }
6954 a915003a 2019-02-05 stsp
6955 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6956 c02c541e 2019-03-29 stsp if (error != NULL)
6957 8e94dd5b 2019-01-04 stsp goto done;
6958 69069811 2018-08-02 stsp
6959 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6960 f135c941 2020-02-20 stsp worktree);
6961 c02c541e 2019-03-29 stsp if (error)
6962 92205607 2019-01-04 stsp goto done;
6963 69069811 2018-08-02 stsp
6964 557d3365 2023-04-14 thomas init_curses();
6965 f135c941 2020-02-20 stsp
6966 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6967 51a10b52 2020-12-26 stsp if (error)
6968 51a10b52 2020-12-26 stsp goto done;
6969 51a10b52 2020-12-26 stsp
6970 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6971 eb41ed75 2019-02-05 stsp if (error)
6972 69069811 2018-08-02 stsp goto done;
6973 a70480e0 2018-06-23 stsp
6974 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6975 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6976 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6977 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6978 a70480e0 2018-06-23 stsp if (error != NULL)
6979 66b4983c 2018-06-23 stsp goto done;
6980 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6981 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6982 a70480e0 2018-06-23 stsp } else {
6983 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6984 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6985 a70480e0 2018-06-23 stsp }
6986 a19e88aa 2018-06-23 stsp if (error != NULL)
6987 8b473291 2019-02-21 stsp goto done;
6988 8b473291 2019-02-21 stsp
6989 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6990 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6991 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6992 e1cd8fed 2018-08-01 stsp goto done;
6993 e1cd8fed 2018-08-01 stsp }
6994 0587e10c 2020-07-23 stsp
6995 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6996 945f9229 2022-04-16 thomas if (error)
6997 945f9229 2022-04-16 thomas goto done;
6998 945f9229 2022-04-16 thomas
6999 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7000 945f9229 2022-04-16 thomas commit, repo);
7001 7cbe629d 2018-08-04 stsp if (error)
7002 7cbe629d 2018-08-04 stsp goto done;
7003 0587e10c 2020-07-23 stsp
7004 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7005 78756c87 2020-11-24 stsp commit_id, repo);
7006 0587e10c 2020-07-23 stsp if (error)
7007 0587e10c 2020-07-23 stsp goto done;
7008 12314ad4 2019-08-31 stsp if (worktree) {
7009 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7010 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7011 12314ad4 2019-08-31 stsp worktree = NULL;
7012 12314ad4 2019-08-31 stsp }
7013 557d3365 2023-04-14 thomas error = view_loop(view);
7014 a70480e0 2018-06-23 stsp done:
7015 69069811 2018-08-02 stsp free(repo_path);
7016 f135c941 2020-02-20 stsp free(in_repo_path);
7017 0587e10c 2020-07-23 stsp free(link_target);
7018 69069811 2018-08-02 stsp free(cwd);
7019 a70480e0 2018-06-23 stsp free(commit_id);
7020 51b7e1c3 2023-04-14 thomas if (error != NULL && view != NULL) {
7021 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
7022 51b7e1c3 2023-04-14 thomas close_blame_view(view);
7023 51b7e1c3 2023-04-14 thomas view_close(view);
7024 51b7e1c3 2023-04-14 thomas }
7025 945f9229 2022-04-16 thomas if (commit)
7026 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7027 eb41ed75 2019-02-05 stsp if (worktree)
7028 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7029 1d0f4054 2021-06-17 stsp if (repo) {
7030 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7031 1d0f4054 2021-06-17 stsp if (error == NULL)
7032 1d0f4054 2021-06-17 stsp error = close_err;
7033 1d0f4054 2021-06-17 stsp }
7034 7cd52833 2022-06-23 thomas if (pack_fds) {
7035 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7036 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7037 7cd52833 2022-06-23 thomas if (error == NULL)
7038 7cd52833 2022-06-23 thomas error = pack_err;
7039 7cd52833 2022-06-23 thomas }
7040 51a10b52 2020-12-26 stsp tog_free_refs();
7041 a70480e0 2018-06-23 stsp return error;
7042 ffd1d5e5 2018-06-23 stsp }
7043 ffd1d5e5 2018-06-23 stsp
7044 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7045 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7046 ffd1d5e5 2018-06-23 stsp {
7047 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7048 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7049 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7050 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7051 86f4aab9 2022-09-09 thomas char *index = NULL;
7052 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7053 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
7054 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7055 ffd1d5e5 2018-06-23 stsp
7056 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7057 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7058 a5d43cac 2022-07-01 thomas --limit; /* border */
7059 ffd1d5e5 2018-06-23 stsp
7060 f7d12f7e 2018-08-01 stsp werase(view->window);
7061 ffd1d5e5 2018-06-23 stsp
7062 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7063 ffd1d5e5 2018-06-23 stsp return NULL;
7064 ffd1d5e5 2018-06-23 stsp
7065 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7066 f91a2b48 2022-06-23 thomas 0, 0);
7067 ffd1d5e5 2018-06-23 stsp if (err)
7068 ffd1d5e5 2018-06-23 stsp return err;
7069 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7070 a3404814 2018-09-02 stsp wstandout(view->window);
7071 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7072 11b20872 2019-11-08 stsp if (tc)
7073 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7074 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7075 86f4aab9 2022-09-09 thomas free(wline);
7076 86f4aab9 2022-09-09 thomas wline = NULL;
7077 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7078 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7079 11b20872 2019-11-08 stsp if (tc)
7080 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7081 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7082 a3404814 2018-09-02 stsp wstandend(view->window);
7083 86f4aab9 2022-09-09 thomas if (--limit <= 0)
7084 86f4aab9 2022-09-09 thomas return NULL;
7085 07dd3ed3 2022-08-06 thomas
7086 6f5f393a 2022-08-12 thomas i += s->selected;
7087 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
7088 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
7089 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
7090 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
7091 07dd3ed3 2022-08-06 thomas }
7092 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
7093 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
7094 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7095 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
7096 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7097 86f4aab9 2022-09-09 thomas free(index);
7098 ce52c690 2018-06-23 stsp if (err)
7099 ce52c690 2018-06-23 stsp return err;
7100 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7101 2550e4c3 2018-07-13 stsp free(wline);
7102 2550e4c3 2018-07-13 stsp wline = NULL;
7103 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7104 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7105 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7106 ffd1d5e5 2018-06-23 stsp return NULL;
7107 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7108 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7109 a1eca9bb 2018-06-23 stsp return NULL;
7110 ffd1d5e5 2018-06-23 stsp
7111 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7112 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7113 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7114 0cf4efb1 2018-09-29 stsp if (view->focussed)
7115 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7116 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7117 ffd1d5e5 2018-06-23 stsp }
7118 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7119 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7120 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7121 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7122 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7123 ffd1d5e5 2018-06-23 stsp return NULL;
7124 ffd1d5e5 2018-06-23 stsp n = 1;
7125 ffd1d5e5 2018-06-23 stsp } else {
7126 ffd1d5e5 2018-06-23 stsp n = 0;
7127 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7128 ffd1d5e5 2018-06-23 stsp }
7129 ffd1d5e5 2018-06-23 stsp
7130 c72de8ab 2023-02-03 thomas view->maxx = 0;
7131 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7132 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7133 848d6979 2019-08-12 stsp const char *modestr = "";
7134 56e0773d 2019-11-28 stsp mode_t mode;
7135 1d13200f 2018-07-12 stsp
7136 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7137 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7138 56e0773d 2019-11-28 stsp
7139 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7140 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7141 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7142 1d13200f 2018-07-12 stsp if (err)
7143 638f9024 2019-05-13 stsp return got_error_from_errno(
7144 230a42bd 2019-05-11 jcs "got_object_id_str");
7145 1d13200f 2018-07-12 stsp }
7146 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7147 63c5ca5d 2019-08-24 stsp modestr = "$";
7148 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7149 0d6c6ee3 2020-05-20 stsp int i;
7150 0d6c6ee3 2020-05-20 stsp
7151 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7152 d86d3b18 2020-12-01 naddy te, s->repo);
7153 0d6c6ee3 2020-05-20 stsp if (err) {
7154 0d6c6ee3 2020-05-20 stsp free(id_str);
7155 0d6c6ee3 2020-05-20 stsp return err;
7156 0d6c6ee3 2020-05-20 stsp }
7157 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
7158 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7159 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7160 0d6c6ee3 2020-05-20 stsp }
7161 848d6979 2019-08-12 stsp modestr = "@";
7162 0d6c6ee3 2020-05-20 stsp }
7163 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7164 848d6979 2019-08-12 stsp modestr = "/";
7165 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7166 848d6979 2019-08-12 stsp modestr = "*";
7167 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7168 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7169 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7170 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7171 1d13200f 2018-07-12 stsp free(id_str);
7172 0d6c6ee3 2020-05-20 stsp free(link_target);
7173 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7174 1d13200f 2018-07-12 stsp }
7175 1d13200f 2018-07-12 stsp free(id_str);
7176 0d6c6ee3 2020-05-20 stsp free(link_target);
7177 c72de8ab 2023-02-03 thomas
7178 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
7179 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7180 ffd1d5e5 2018-06-23 stsp if (err) {
7181 ffd1d5e5 2018-06-23 stsp free(line);
7182 ffd1d5e5 2018-06-23 stsp break;
7183 ffd1d5e5 2018-06-23 stsp }
7184 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
7185 c72de8ab 2023-02-03 thomas free(wline);
7186 c72de8ab 2023-02-03 thomas wline = NULL;
7187 c72de8ab 2023-02-03 thomas
7188 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
7189 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
7190 c72de8ab 2023-02-03 thomas if (err) {
7191 c72de8ab 2023-02-03 thomas free(line);
7192 c72de8ab 2023-02-03 thomas break;
7193 c72de8ab 2023-02-03 thomas }
7194 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7195 0cf4efb1 2018-09-29 stsp if (view->focussed)
7196 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7197 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7198 ffd1d5e5 2018-06-23 stsp }
7199 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7200 f26dddb7 2019-11-08 stsp if (tc)
7201 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7202 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7203 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
7204 f26dddb7 2019-11-08 stsp if (tc)
7205 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7206 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7207 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
7208 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7209 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7210 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7211 ffd1d5e5 2018-06-23 stsp free(line);
7212 2550e4c3 2018-07-13 stsp free(wline);
7213 2550e4c3 2018-07-13 stsp wline = NULL;
7214 ffd1d5e5 2018-06-23 stsp n++;
7215 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7216 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7217 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7218 ffd1d5e5 2018-06-23 stsp break;
7219 ffd1d5e5 2018-06-23 stsp }
7220 ffd1d5e5 2018-06-23 stsp
7221 ffd1d5e5 2018-06-23 stsp return err;
7222 ffd1d5e5 2018-06-23 stsp }
7223 ffd1d5e5 2018-06-23 stsp
7224 ffd1d5e5 2018-06-23 stsp static void
7225 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7226 ffd1d5e5 2018-06-23 stsp {
7227 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7228 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7229 fa86c4bf 2020-11-29 stsp int i = 0;
7230 ffd1d5e5 2018-06-23 stsp
7231 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7232 ffd1d5e5 2018-06-23 stsp return;
7233 ffd1d5e5 2018-06-23 stsp
7234 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7235 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7236 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7237 fa86c4bf 2020-11-29 stsp if (!isroot)
7238 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7239 fa86c4bf 2020-11-29 stsp break;
7240 fa86c4bf 2020-11-29 stsp }
7241 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7242 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7243 ffd1d5e5 2018-06-23 stsp }
7244 ffd1d5e5 2018-06-23 stsp }
7245 ffd1d5e5 2018-06-23 stsp
7246 a5d43cac 2022-07-01 thomas static const struct got_error *
7247 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
7248 ffd1d5e5 2018-06-23 stsp {
7249 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7250 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7251 ffd1d5e5 2018-06-23 stsp int n = 0;
7252 ffd1d5e5 2018-06-23 stsp
7253 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7254 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7255 694d3271 2020-12-01 naddy s->first_displayed_entry);
7256 694d3271 2020-12-01 naddy else
7257 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7258 56e0773d 2019-11-28 stsp
7259 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7260 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7261 07dd3ed3 2022-08-06 thomas if (last) {
7262 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7263 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7264 07dd3ed3 2022-08-06 thomas }
7265 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7266 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7267 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7268 768394f3 2019-01-24 stsp }
7269 ffd1d5e5 2018-06-23 stsp }
7270 a5d43cac 2022-07-01 thomas
7271 a5d43cac 2022-07-01 thomas return NULL;
7272 ffd1d5e5 2018-06-23 stsp }
7273 ffd1d5e5 2018-06-23 stsp
7274 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7275 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7276 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7277 ffd1d5e5 2018-06-23 stsp {
7278 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7279 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7280 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7281 ffd1d5e5 2018-06-23 stsp
7282 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7283 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7284 56e0773d 2019-11-28 stsp + 1 /* slash */;
7285 ce52c690 2018-06-23 stsp if (te)
7286 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7287 ce52c690 2018-06-23 stsp
7288 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7289 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7290 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7291 ffd1d5e5 2018-06-23 stsp
7292 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7293 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7294 d9765a41 2018-06-23 stsp while (pt) {
7295 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7296 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7297 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7298 cb2ebc8a 2018-06-23 stsp goto done;
7299 cb2ebc8a 2018-06-23 stsp }
7300 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7301 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7302 cb2ebc8a 2018-06-23 stsp goto done;
7303 cb2ebc8a 2018-06-23 stsp }
7304 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7305 ffd1d5e5 2018-06-23 stsp }
7306 ce52c690 2018-06-23 stsp if (te) {
7307 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7308 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7309 ce52c690 2018-06-23 stsp goto done;
7310 ce52c690 2018-06-23 stsp }
7311 cb2ebc8a 2018-06-23 stsp }
7312 ce52c690 2018-06-23 stsp done:
7313 ce52c690 2018-06-23 stsp if (err) {
7314 ce52c690 2018-06-23 stsp free(*path);
7315 ce52c690 2018-06-23 stsp *path = NULL;
7316 ce52c690 2018-06-23 stsp }
7317 ce52c690 2018-06-23 stsp return err;
7318 ce52c690 2018-06-23 stsp }
7319 ce52c690 2018-06-23 stsp
7320 ce52c690 2018-06-23 stsp static const struct got_error *
7321 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7322 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7323 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7324 ce52c690 2018-06-23 stsp {
7325 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7326 ce52c690 2018-06-23 stsp char *path;
7327 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7328 a0de39f3 2019-08-09 stsp
7329 a0de39f3 2019-08-09 stsp *new_view = NULL;
7330 69efd4c4 2018-07-18 stsp
7331 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7332 ce52c690 2018-06-23 stsp if (err)
7333 ce52c690 2018-06-23 stsp return err;
7334 ffd1d5e5 2018-06-23 stsp
7335 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7336 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7337 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7338 83ce39e3 2019-08-12 stsp goto done;
7339 83ce39e3 2019-08-12 stsp }
7340 cdf1ee82 2018-08-01 stsp
7341 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7342 e5a0f69f 2018-08-18 stsp if (err) {
7343 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7344 fc06ba56 2019-08-22 stsp err = NULL;
7345 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7346 e5a0f69f 2018-08-18 stsp } else
7347 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7348 83ce39e3 2019-08-12 stsp done:
7349 83ce39e3 2019-08-12 stsp free(path);
7350 69efd4c4 2018-07-18 stsp return err;
7351 69efd4c4 2018-07-18 stsp }
7352 69efd4c4 2018-07-18 stsp
7353 69efd4c4 2018-07-18 stsp static const struct got_error *
7354 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7355 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7356 69efd4c4 2018-07-18 stsp {
7357 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7358 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7359 69efd4c4 2018-07-18 stsp char *path;
7360 a0de39f3 2019-08-09 stsp
7361 a0de39f3 2019-08-09 stsp *new_view = NULL;
7362 69efd4c4 2018-07-18 stsp
7363 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7364 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7365 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7366 e5a0f69f 2018-08-18 stsp
7367 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7368 69efd4c4 2018-07-18 stsp if (err)
7369 69efd4c4 2018-07-18 stsp return err;
7370 69efd4c4 2018-07-18 stsp
7371 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7372 4e97c21c 2020-12-06 stsp path, 0);
7373 ba4f502b 2018-08-04 stsp if (err)
7374 e5a0f69f 2018-08-18 stsp view_close(log_view);
7375 e5a0f69f 2018-08-18 stsp else
7376 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7377 cb2ebc8a 2018-06-23 stsp free(path);
7378 cb2ebc8a 2018-06-23 stsp return err;
7379 ffd1d5e5 2018-06-23 stsp }
7380 ffd1d5e5 2018-06-23 stsp
7381 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7382 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7383 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7384 ffd1d5e5 2018-06-23 stsp {
7385 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7386 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7387 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7388 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7389 ffd1d5e5 2018-06-23 stsp
7390 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7391 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7392 bc573f3b 2021-07-10 stsp
7393 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7394 51b7e1c3 2023-04-14 thomas if (s->commit_id == NULL) {
7395 51b7e1c3 2023-04-14 thomas err = got_error_from_errno("got_object_id_dup");
7396 51b7e1c3 2023-04-14 thomas goto done;
7397 51b7e1c3 2023-04-14 thomas }
7398 ffd1d5e5 2018-06-23 stsp
7399 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7400 bc573f3b 2021-07-10 stsp if (err)
7401 bc573f3b 2021-07-10 stsp goto done;
7402 bc573f3b 2021-07-10 stsp
7403 bc573f3b 2021-07-10 stsp /*
7404 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7405 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7406 bc573f3b 2021-07-10 stsp * closed on demand.
7407 bc573f3b 2021-07-10 stsp */
7408 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7409 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7410 bc573f3b 2021-07-10 stsp if (err)
7411 bc573f3b 2021-07-10 stsp goto done;
7412 bc573f3b 2021-07-10 stsp s->tree = s->root;
7413 bc573f3b 2021-07-10 stsp
7414 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7415 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7416 ffd1d5e5 2018-06-23 stsp goto done;
7417 ffd1d5e5 2018-06-23 stsp
7418 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7419 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7420 ffd1d5e5 2018-06-23 stsp goto done;
7421 ffd1d5e5 2018-06-23 stsp }
7422 ffd1d5e5 2018-06-23 stsp
7423 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7424 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7425 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7426 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7427 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7428 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7429 9cd7cbd1 2020-12-07 stsp goto done;
7430 9cd7cbd1 2020-12-07 stsp }
7431 9cd7cbd1 2020-12-07 stsp }
7432 fb2756b9 2018-08-04 stsp s->repo = repo;
7433 c0b01bdb 2019-11-08 stsp
7434 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7435 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7436 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7437 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7438 c0b01bdb 2019-11-08 stsp if (err)
7439 c0b01bdb 2019-11-08 stsp goto done;
7440 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7441 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7442 bc573f3b 2021-07-10 stsp if (err)
7443 c0b01bdb 2019-11-08 stsp goto done;
7444 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7445 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7446 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7447 bc573f3b 2021-07-10 stsp if (err)
7448 c0b01bdb 2019-11-08 stsp goto done;
7449 e5a0f69f 2018-08-18 stsp
7450 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7451 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7452 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7453 bc573f3b 2021-07-10 stsp if (err)
7454 11b20872 2019-11-08 stsp goto done;
7455 11b20872 2019-11-08 stsp
7456 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7457 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7458 bc573f3b 2021-07-10 stsp if (err)
7459 c0b01bdb 2019-11-08 stsp goto done;
7460 c0b01bdb 2019-11-08 stsp }
7461 c0b01bdb 2019-11-08 stsp
7462 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7463 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7464 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7465 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7466 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7467 ad80ab7b 2018-08-04 stsp done:
7468 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7469 bc573f3b 2021-07-10 stsp if (commit)
7470 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7471 51b7e1c3 2023-04-14 thomas if (err) {
7472 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
7473 51b7e1c3 2023-04-14 thomas close_tree_view(view);
7474 51b7e1c3 2023-04-14 thomas view_close(view);
7475 51b7e1c3 2023-04-14 thomas }
7476 ad80ab7b 2018-08-04 stsp return err;
7477 ad80ab7b 2018-08-04 stsp }
7478 ad80ab7b 2018-08-04 stsp
7479 e5a0f69f 2018-08-18 stsp static const struct got_error *
7480 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7481 ad80ab7b 2018-08-04 stsp {
7482 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7483 ad80ab7b 2018-08-04 stsp
7484 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7485 fb2756b9 2018-08-04 stsp free(s->tree_label);
7486 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7487 6484ec90 2018-09-29 stsp free(s->commit_id);
7488 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7489 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7490 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7491 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7492 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7493 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7494 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7495 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7496 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7497 ad80ab7b 2018-08-04 stsp free(parent);
7498 ad80ab7b 2018-08-04 stsp
7499 ad80ab7b 2018-08-04 stsp }
7500 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7501 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7502 bc573f3b 2021-07-10 stsp if (s->root)
7503 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7504 7c32bd05 2019-06-22 stsp return NULL;
7505 7c32bd05 2019-06-22 stsp }
7506 7c32bd05 2019-06-22 stsp
7507 7c32bd05 2019-06-22 stsp static const struct got_error *
7508 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7509 7c32bd05 2019-06-22 stsp {
7510 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7511 7c32bd05 2019-06-22 stsp
7512 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7513 7c32bd05 2019-06-22 stsp return NULL;
7514 7c32bd05 2019-06-22 stsp }
7515 7c32bd05 2019-06-22 stsp
7516 7c32bd05 2019-06-22 stsp static int
7517 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7518 7c32bd05 2019-06-22 stsp {
7519 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7520 7c32bd05 2019-06-22 stsp
7521 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7522 56e0773d 2019-11-28 stsp 0) == 0;
7523 7c32bd05 2019-06-22 stsp }
7524 7c32bd05 2019-06-22 stsp
7525 7c32bd05 2019-06-22 stsp static const struct got_error *
7526 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7527 7c32bd05 2019-06-22 stsp {
7528 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7529 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7530 7c32bd05 2019-06-22 stsp
7531 7c32bd05 2019-06-22 stsp if (!view->searching) {
7532 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7533 7c32bd05 2019-06-22 stsp return NULL;
7534 7c32bd05 2019-06-22 stsp }
7535 7c32bd05 2019-06-22 stsp
7536 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7537 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7538 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7539 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7540 56e0773d 2019-11-28 stsp s->selected_entry);
7541 7c32bd05 2019-06-22 stsp else
7542 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7543 56e0773d 2019-11-28 stsp } else {
7544 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7545 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7546 56e0773d 2019-11-28 stsp else
7547 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7548 56e0773d 2019-11-28 stsp s->selected_entry);
7549 7c32bd05 2019-06-22 stsp }
7550 7c32bd05 2019-06-22 stsp } else {
7551 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7552 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7553 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7554 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7555 56e0773d 2019-11-28 stsp else
7556 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7557 7c32bd05 2019-06-22 stsp }
7558 7c32bd05 2019-06-22 stsp
7559 7c32bd05 2019-06-22 stsp while (1) {
7560 56e0773d 2019-11-28 stsp if (te == NULL) {
7561 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7562 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7563 ac66afb8 2019-06-24 stsp return NULL;
7564 ac66afb8 2019-06-24 stsp }
7565 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7566 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7567 56e0773d 2019-11-28 stsp else
7568 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7569 7c32bd05 2019-06-22 stsp }
7570 7c32bd05 2019-06-22 stsp
7571 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7572 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7573 56e0773d 2019-11-28 stsp s->matched_entry = te;
7574 7c32bd05 2019-06-22 stsp break;
7575 7c32bd05 2019-06-22 stsp }
7576 7c32bd05 2019-06-22 stsp
7577 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7578 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7579 56e0773d 2019-11-28 stsp else
7580 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7581 7c32bd05 2019-06-22 stsp }
7582 e5a0f69f 2018-08-18 stsp
7583 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7584 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7585 7c32bd05 2019-06-22 stsp s->selected = 0;
7586 7c32bd05 2019-06-22 stsp }
7587 7c32bd05 2019-06-22 stsp
7588 e5a0f69f 2018-08-18 stsp return NULL;
7589 ad80ab7b 2018-08-04 stsp }
7590 ad80ab7b 2018-08-04 stsp
7591 ad80ab7b 2018-08-04 stsp static const struct got_error *
7592 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7593 ad80ab7b 2018-08-04 stsp {
7594 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7595 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7596 e5a0f69f 2018-08-18 stsp char *parent_path;
7597 ad80ab7b 2018-08-04 stsp
7598 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7599 e5a0f69f 2018-08-18 stsp if (err)
7600 e5a0f69f 2018-08-18 stsp return err;
7601 ffd1d5e5 2018-06-23 stsp
7602 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7603 e5a0f69f 2018-08-18 stsp free(parent_path);
7604 669b5ffa 2018-10-07 stsp
7605 a5d43cac 2022-07-01 thomas view_border(view);
7606 e5a0f69f 2018-08-18 stsp return err;
7607 07dd3ed3 2022-08-06 thomas }
7608 07dd3ed3 2022-08-06 thomas
7609 07dd3ed3 2022-08-06 thomas static const struct got_error *
7610 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7611 07dd3ed3 2022-08-06 thomas {
7612 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7613 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7614 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7615 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7616 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7617 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7618 07dd3ed3 2022-08-06 thomas
7619 07dd3ed3 2022-08-06 thomas g = view->gline;
7620 07dd3ed3 2022-08-06 thomas view->gline = 0;
7621 07dd3ed3 2022-08-06 thomas
7622 07dd3ed3 2022-08-06 thomas if (g == 0)
7623 07dd3ed3 2022-08-06 thomas g = 1;
7624 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7625 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7626 07dd3ed3 2022-08-06 thomas
7627 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7628 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7629 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7630 07dd3ed3 2022-08-06 thomas
7631 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7632 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7633 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7634 07dd3ed3 2022-08-06 thomas }
7635 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7636 07dd3ed3 2022-08-06 thomas last += off;
7637 07dd3ed3 2022-08-06 thomas
7638 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7639 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7640 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7641 07dd3ed3 2022-08-06 thomas }
7642 07dd3ed3 2022-08-06 thomas
7643 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7644 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7645 07dd3ed3 2022-08-06 thomas i += off;
7646 07dd3ed3 2022-08-06 thomas }
7647 07dd3ed3 2022-08-06 thomas
7648 07dd3ed3 2022-08-06 thomas if (i < g) {
7649 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7650 07dd3ed3 2022-08-06 thomas if (err)
7651 07dd3ed3 2022-08-06 thomas return err;
7652 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7653 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7654 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7655 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7656 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7657 07dd3ed3 2022-08-06 thomas first += off;
7658 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7659 07dd3ed3 2022-08-06 thomas }
7660 07dd3ed3 2022-08-06 thomas } else if (i > g)
7661 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7662 07dd3ed3 2022-08-06 thomas
7663 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7664 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7665 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7666 07dd3ed3 2022-08-06 thomas
7667 07dd3ed3 2022-08-06 thomas return NULL;
7668 e5a0f69f 2018-08-18 stsp }
7669 ce52c690 2018-06-23 stsp
7670 e5a0f69f 2018-08-18 stsp static const struct got_error *
7671 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7672 e5a0f69f 2018-08-18 stsp {
7673 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7674 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7675 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7676 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7677 ffd1d5e5 2018-06-23 stsp
7678 07dd3ed3 2022-08-06 thomas if (view->gline)
7679 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7680 07dd3ed3 2022-08-06 thomas
7681 e5a0f69f 2018-08-18 stsp switch (ch) {
7682 c72de8ab 2023-02-03 thomas case '0':
7683 c72de8ab 2023-02-03 thomas case '$':
7684 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
7685 c72de8ab 2023-02-03 thomas case 'l':
7686 c72de8ab 2023-02-03 thomas case KEY_LEFT:
7687 c72de8ab 2023-02-03 thomas case 'h':
7688 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
7689 c72de8ab 2023-02-03 thomas break;
7690 1e37a5c2 2019-05-12 jcs case 'i':
7691 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7692 07b0611c 2022-06-23 thomas view->count = 0;
7693 1e37a5c2 2019-05-12 jcs break;
7694 1be4947a 2022-07-22 thomas case 'L':
7695 07b0611c 2022-06-23 thomas view->count = 0;
7696 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7697 ffd1d5e5 2018-06-23 stsp break;
7698 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7699 152c1c93 2020-11-29 stsp break;
7700 1be4947a 2022-07-22 thomas case 'R':
7701 07b0611c 2022-06-23 thomas view->count = 0;
7702 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7703 e4526bf5 2021-09-03 naddy break;
7704 e4526bf5 2021-09-03 naddy case 'g':
7705 aa7a1117 2023-01-09 thomas case '=':
7706 e4526bf5 2021-09-03 naddy case KEY_HOME:
7707 e4526bf5 2021-09-03 naddy s->selected = 0;
7708 07b0611c 2022-06-23 thomas view->count = 0;
7709 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7710 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7711 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7712 e4526bf5 2021-09-03 naddy else
7713 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7714 1e37a5c2 2019-05-12 jcs break;
7715 e4526bf5 2021-09-03 naddy case 'G':
7716 aa7a1117 2023-01-09 thomas case '*':
7717 a5d43cac 2022-07-01 thomas case KEY_END: {
7718 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7719 a5d43cac 2022-07-01 thomas
7720 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7721 a5d43cac 2022-07-01 thomas --eos; /* border */
7722 e4526bf5 2021-09-03 naddy s->selected = 0;
7723 07b0611c 2022-06-23 thomas view->count = 0;
7724 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7725 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7726 e4526bf5 2021-09-03 naddy if (te == NULL) {
7727 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7728 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7729 e4526bf5 2021-09-03 naddy n++;
7730 e4526bf5 2021-09-03 naddy }
7731 e4526bf5 2021-09-03 naddy break;
7732 e4526bf5 2021-09-03 naddy }
7733 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7734 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7735 e4526bf5 2021-09-03 naddy }
7736 e4526bf5 2021-09-03 naddy if (n > 0)
7737 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7738 e4526bf5 2021-09-03 naddy break;
7739 a5d43cac 2022-07-01 thomas }
7740 1e37a5c2 2019-05-12 jcs case 'k':
7741 1e37a5c2 2019-05-12 jcs case KEY_UP:
7742 f7140bf5 2021-10-17 thomas case CTRL('p'):
7743 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7744 1e37a5c2 2019-05-12 jcs s->selected--;
7745 fa86c4bf 2020-11-29 stsp break;
7746 1e37a5c2 2019-05-12 jcs }
7747 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7748 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7749 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7750 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7751 07b0611c 2022-06-23 thomas view->count = 0;
7752 1e37a5c2 2019-05-12 jcs break;
7753 70f17a53 2022-06-13 thomas case CTRL('u'):
7754 23427b14 2022-06-23 thomas case 'u':
7755 70f17a53 2022-06-13 thomas nscroll /= 2;
7756 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7757 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7758 ea025d1d 2020-02-22 naddy case CTRL('b'):
7759 1c5e5faa 2022-06-23 thomas case 'b':
7760 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7761 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7762 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7763 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7764 fa86c4bf 2020-11-29 stsp } else {
7765 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7766 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7767 fa86c4bf 2020-11-29 stsp }
7768 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7769 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7770 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7771 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7772 07b0611c 2022-06-23 thomas view->count = 0;
7773 1e37a5c2 2019-05-12 jcs break;
7774 1e37a5c2 2019-05-12 jcs case 'j':
7775 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7776 f7140bf5 2021-10-17 thomas case CTRL('n'):
7777 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7778 1e37a5c2 2019-05-12 jcs s->selected++;
7779 1e37a5c2 2019-05-12 jcs break;
7780 1e37a5c2 2019-05-12 jcs }
7781 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7782 07b0611c 2022-06-23 thomas == NULL) {
7783 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7784 07b0611c 2022-06-23 thomas view->count = 0;
7785 1e37a5c2 2019-05-12 jcs break;
7786 07b0611c 2022-06-23 thomas }
7787 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7788 1e37a5c2 2019-05-12 jcs break;
7789 70f17a53 2022-06-13 thomas case CTRL('d'):
7790 23427b14 2022-06-23 thomas case 'd':
7791 70f17a53 2022-06-13 thomas nscroll /= 2;
7792 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7793 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7794 ea025d1d 2020-02-22 naddy case CTRL('f'):
7795 1c5e5faa 2022-06-23 thomas case 'f':
7796 4c2d69cb 2022-06-23 thomas case ' ':
7797 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7798 1e37a5c2 2019-05-12 jcs == NULL) {
7799 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7800 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7801 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7802 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7803 07b0611c 2022-06-23 thomas else
7804 07b0611c 2022-06-23 thomas view->count = 0;
7805 1e37a5c2 2019-05-12 jcs break;
7806 1e37a5c2 2019-05-12 jcs }
7807 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7808 1e37a5c2 2019-05-12 jcs break;
7809 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7810 1e37a5c2 2019-05-12 jcs case '\r':
7811 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7812 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7813 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7814 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7815 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7816 07b0611c 2022-06-23 thomas view->count = 0;
7817 1e37a5c2 2019-05-12 jcs break;
7818 07b0611c 2022-06-23 thomas }
7819 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7820 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7821 1e37a5c2 2019-05-12 jcs entry);
7822 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7823 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7824 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7825 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7826 1e37a5c2 2019-05-12 jcs s->selected_entry =
7827 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7828 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7829 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7830 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7831 a5d43cac 2022-07-01 thomas if (err)
7832 a5d43cac 2022-07-01 thomas break;
7833 a5d43cac 2022-07-01 thomas }
7834 1e37a5c2 2019-05-12 jcs free(parent);
7835 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7836 56e0773d 2019-11-28 stsp s->selected_entry))) {
7837 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7838 07b0611c 2022-06-23 thomas view->count = 0;
7839 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7840 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7841 1e37a5c2 2019-05-12 jcs if (err)
7842 1e37a5c2 2019-05-12 jcs break;
7843 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7844 941e9f74 2019-05-21 stsp if (err) {
7845 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7846 1e37a5c2 2019-05-12 jcs break;
7847 1e37a5c2 2019-05-12 jcs }
7848 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7849 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7850 1e37a5c2 2019-05-12 jcs break;
7851 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7852 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7853 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7854 07b0611c 2022-06-23 thomas view->count = 0;
7855 1e37a5c2 2019-05-12 jcs break;
7856 1e37a5c2 2019-05-12 jcs default:
7857 07b0611c 2022-06-23 thomas view->count = 0;
7858 1e37a5c2 2019-05-12 jcs break;
7859 ffd1d5e5 2018-06-23 stsp }
7860 e5a0f69f 2018-08-18 stsp
7861 ffd1d5e5 2018-06-23 stsp return err;
7862 9f7d7167 2018-04-29 stsp }
7863 9f7d7167 2018-04-29 stsp
7864 ffd1d5e5 2018-06-23 stsp __dead static void
7865 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7866 ffd1d5e5 2018-06-23 stsp {
7867 ffd1d5e5 2018-06-23 stsp endwin();
7868 91198554 2022-06-23 thomas fprintf(stderr,
7869 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7870 ffd1d5e5 2018-06-23 stsp getprogname());
7871 ffd1d5e5 2018-06-23 stsp exit(1);
7872 ffd1d5e5 2018-06-23 stsp }
7873 ffd1d5e5 2018-06-23 stsp
7874 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7875 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7876 ffd1d5e5 2018-06-23 stsp {
7877 1d98034b 2023-04-14 thomas const struct got_error *error;
7878 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7879 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7880 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7881 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7882 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7883 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7884 4e97c21c 2020-12-06 stsp char *label = NULL;
7885 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7886 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7887 ffd1d5e5 2018-06-23 stsp int ch;
7888 5221c383 2018-08-01 stsp struct tog_view *view;
7889 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7890 70ac5f84 2019-03-28 stsp
7891 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7892 ffd1d5e5 2018-06-23 stsp switch (ch) {
7893 ffd1d5e5 2018-06-23 stsp case 'c':
7894 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7895 74283ab8 2020-02-07 stsp break;
7896 74283ab8 2020-02-07 stsp case 'r':
7897 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7898 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7899 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7900 74283ab8 2020-02-07 stsp optarg);
7901 ffd1d5e5 2018-06-23 stsp break;
7902 ffd1d5e5 2018-06-23 stsp default:
7903 e99e2d15 2020-11-24 naddy usage_tree();
7904 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7905 ffd1d5e5 2018-06-23 stsp }
7906 ffd1d5e5 2018-06-23 stsp }
7907 ffd1d5e5 2018-06-23 stsp
7908 ffd1d5e5 2018-06-23 stsp argc -= optind;
7909 ffd1d5e5 2018-06-23 stsp argv += optind;
7910 ffd1d5e5 2018-06-23 stsp
7911 55cccc34 2020-02-20 stsp if (argc > 1)
7912 e99e2d15 2020-11-24 naddy usage_tree();
7913 7cd52833 2022-06-23 thomas
7914 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7915 7cd52833 2022-06-23 thomas if (error != NULL)
7916 7cd52833 2022-06-23 thomas goto done;
7917 74283ab8 2020-02-07 stsp
7918 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7919 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7920 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7921 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7922 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7923 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7924 c156c7a4 2020-12-18 stsp goto done;
7925 55cccc34 2020-02-20 stsp if (worktree)
7926 52185f70 2019-02-05 stsp repo_path =
7927 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7928 55cccc34 2020-02-20 stsp else
7929 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7930 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7931 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7932 c156c7a4 2020-12-18 stsp goto done;
7933 c156c7a4 2020-12-18 stsp }
7934 55cccc34 2020-02-20 stsp }
7935 a915003a 2019-02-05 stsp
7936 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7937 c02c541e 2019-03-29 stsp if (error != NULL)
7938 52185f70 2019-02-05 stsp goto done;
7939 d188b9a6 2019-01-04 stsp
7940 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7941 55cccc34 2020-02-20 stsp repo, worktree);
7942 55cccc34 2020-02-20 stsp if (error)
7943 55cccc34 2020-02-20 stsp goto done;
7944 55cccc34 2020-02-20 stsp
7945 557d3365 2023-04-14 thomas init_curses();
7946 55cccc34 2020-02-20 stsp
7947 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7948 c02c541e 2019-03-29 stsp if (error)
7949 52185f70 2019-02-05 stsp goto done;
7950 ffd1d5e5 2018-06-23 stsp
7951 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7952 51a10b52 2020-12-26 stsp if (error)
7953 51a10b52 2020-12-26 stsp goto done;
7954 51a10b52 2020-12-26 stsp
7955 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7956 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7957 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7958 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7959 4e97c21c 2020-12-06 stsp if (error)
7960 4e97c21c 2020-12-06 stsp goto done;
7961 4e97c21c 2020-12-06 stsp head_ref_name = label;
7962 4e97c21c 2020-12-06 stsp } else {
7963 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7964 4e97c21c 2020-12-06 stsp if (error == NULL)
7965 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7966 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7967 4e97c21c 2020-12-06 stsp goto done;
7968 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7969 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7970 4e97c21c 2020-12-06 stsp if (error)
7971 4e97c21c 2020-12-06 stsp goto done;
7972 4e97c21c 2020-12-06 stsp }
7973 ffd1d5e5 2018-06-23 stsp
7974 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7975 945f9229 2022-04-16 thomas if (error)
7976 945f9229 2022-04-16 thomas goto done;
7977 945f9229 2022-04-16 thomas
7978 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7979 5221c383 2018-08-01 stsp if (view == NULL) {
7980 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7981 5221c383 2018-08-01 stsp goto done;
7982 5221c383 2018-08-01 stsp }
7983 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7984 ad80ab7b 2018-08-04 stsp if (error)
7985 ad80ab7b 2018-08-04 stsp goto done;
7986 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7987 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7988 d91faf3b 2020-12-01 naddy in_repo_path);
7989 55cccc34 2020-02-20 stsp if (error)
7990 55cccc34 2020-02-20 stsp goto done;
7991 55cccc34 2020-02-20 stsp }
7992 55cccc34 2020-02-20 stsp
7993 55cccc34 2020-02-20 stsp if (worktree) {
7994 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7995 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7996 55cccc34 2020-02-20 stsp worktree = NULL;
7997 55cccc34 2020-02-20 stsp }
7998 557d3365 2023-04-14 thomas error = view_loop(view);
7999 ffd1d5e5 2018-06-23 stsp done:
8000 52185f70 2019-02-05 stsp free(repo_path);
8001 e4a0e26d 2020-02-20 stsp free(cwd);
8002 ffd1d5e5 2018-06-23 stsp free(commit_id);
8003 4e97c21c 2020-12-06 stsp free(label);
8004 486cd271 2020-12-06 stsp if (ref)
8005 486cd271 2020-12-06 stsp got_ref_close(ref);
8006 1d0f4054 2021-06-17 stsp if (repo) {
8007 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8008 1d0f4054 2021-06-17 stsp if (error == NULL)
8009 1d0f4054 2021-06-17 stsp error = close_err;
8010 1d0f4054 2021-06-17 stsp }
8011 7cd52833 2022-06-23 thomas if (pack_fds) {
8012 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8013 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8014 7cd52833 2022-06-23 thomas if (error == NULL)
8015 7cd52833 2022-06-23 thomas error = pack_err;
8016 b85a3496 2023-04-14 thomas }
8017 51a10b52 2020-12-26 stsp tog_free_refs();
8018 ffd1d5e5 2018-06-23 stsp return error;
8019 6458efa5 2020-11-24 stsp }
8020 6458efa5 2020-11-24 stsp
8021 6458efa5 2020-11-24 stsp static const struct got_error *
8022 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8023 6458efa5 2020-11-24 stsp {
8024 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8025 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8026 6458efa5 2020-11-24 stsp
8027 6458efa5 2020-11-24 stsp s->nrefs = 0;
8028 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8029 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
8030 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
8031 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
8032 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
8033 6458efa5 2020-11-24 stsp continue;
8034 6458efa5 2020-11-24 stsp
8035 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8036 6458efa5 2020-11-24 stsp if (re == NULL)
8037 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8038 6458efa5 2020-11-24 stsp
8039 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8040 8924d611 2020-12-26 stsp if (re->ref == NULL)
8041 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8042 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8043 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8044 6458efa5 2020-11-24 stsp }
8045 6458efa5 2020-11-24 stsp
8046 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8047 6458efa5 2020-11-24 stsp return NULL;
8048 6458efa5 2020-11-24 stsp }
8049 6458efa5 2020-11-24 stsp
8050 ef20f542 2022-06-26 thomas static void
8051 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8052 6458efa5 2020-11-24 stsp {
8053 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8054 6458efa5 2020-11-24 stsp
8055 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8056 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8057 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8058 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8059 6458efa5 2020-11-24 stsp free(re);
8060 6458efa5 2020-11-24 stsp }
8061 6458efa5 2020-11-24 stsp }
8062 6458efa5 2020-11-24 stsp
8063 6458efa5 2020-11-24 stsp static const struct got_error *
8064 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8065 6458efa5 2020-11-24 stsp {
8066 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8067 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8068 6458efa5 2020-11-24 stsp
8069 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8070 6458efa5 2020-11-24 stsp s->repo = repo;
8071 6458efa5 2020-11-24 stsp
8072 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8073 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8074 6458efa5 2020-11-24 stsp
8075 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8076 6458efa5 2020-11-24 stsp if (err)
8077 51b7e1c3 2023-04-14 thomas goto done;
8078 34ba6917 2020-11-30 stsp
8079 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8080 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8081 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8082 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8083 6458efa5 2020-11-24 stsp if (err)
8084 6458efa5 2020-11-24 stsp goto done;
8085 6458efa5 2020-11-24 stsp
8086 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8087 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8088 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8089 6458efa5 2020-11-24 stsp if (err)
8090 6458efa5 2020-11-24 stsp goto done;
8091 6458efa5 2020-11-24 stsp
8092 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8093 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8094 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8095 2183bbf6 2022-01-23 thomas if (err)
8096 2183bbf6 2022-01-23 thomas goto done;
8097 2183bbf6 2022-01-23 thomas
8098 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
8099 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
8100 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
8101 6458efa5 2020-11-24 stsp if (err)
8102 6458efa5 2020-11-24 stsp goto done;
8103 6458efa5 2020-11-24 stsp }
8104 6458efa5 2020-11-24 stsp
8105 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8106 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8107 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8108 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8109 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8110 6458efa5 2020-11-24 stsp done:
8111 51b7e1c3 2023-04-14 thomas if (err) {
8112 51b7e1c3 2023-04-14 thomas if (view->close == NULL)
8113 51b7e1c3 2023-04-14 thomas close_ref_view(view);
8114 51b7e1c3 2023-04-14 thomas view_close(view);
8115 51b7e1c3 2023-04-14 thomas }
8116 6458efa5 2020-11-24 stsp return err;
8117 6458efa5 2020-11-24 stsp }
8118 6458efa5 2020-11-24 stsp
8119 6458efa5 2020-11-24 stsp static const struct got_error *
8120 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8121 6458efa5 2020-11-24 stsp {
8122 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8123 6458efa5 2020-11-24 stsp
8124 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8125 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8126 6458efa5 2020-11-24 stsp
8127 6458efa5 2020-11-24 stsp return NULL;
8128 9f7d7167 2018-04-29 stsp }
8129 ce5b7c56 2019-07-09 stsp
8130 6458efa5 2020-11-24 stsp static const struct got_error *
8131 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8132 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8133 6458efa5 2020-11-24 stsp {
8134 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8135 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8136 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8137 6458efa5 2020-11-24 stsp int obj_type;
8138 6458efa5 2020-11-24 stsp
8139 c42c9805 2020-11-24 stsp *commit_id = NULL;
8140 6458efa5 2020-11-24 stsp
8141 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8142 6458efa5 2020-11-24 stsp if (err)
8143 6458efa5 2020-11-24 stsp return err;
8144 6458efa5 2020-11-24 stsp
8145 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8146 6458efa5 2020-11-24 stsp if (err)
8147 6458efa5 2020-11-24 stsp goto done;
8148 6458efa5 2020-11-24 stsp
8149 6458efa5 2020-11-24 stsp switch (obj_type) {
8150 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8151 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8152 6458efa5 2020-11-24 stsp break;
8153 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8154 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8155 6458efa5 2020-11-24 stsp if (err)
8156 6458efa5 2020-11-24 stsp goto done;
8157 c42c9805 2020-11-24 stsp free(obj_id);
8158 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8159 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8160 c42c9805 2020-11-24 stsp if (err)
8161 6458efa5 2020-11-24 stsp goto done;
8162 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8163 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8164 c42c9805 2020-11-24 stsp goto done;
8165 c42c9805 2020-11-24 stsp }
8166 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8167 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8168 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8169 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8170 c42c9805 2020-11-24 stsp goto done;
8171 c42c9805 2020-11-24 stsp }
8172 6458efa5 2020-11-24 stsp break;
8173 6458efa5 2020-11-24 stsp default:
8174 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8175 c42c9805 2020-11-24 stsp break;
8176 6458efa5 2020-11-24 stsp }
8177 6458efa5 2020-11-24 stsp
8178 c42c9805 2020-11-24 stsp done:
8179 c42c9805 2020-11-24 stsp if (tag)
8180 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8181 c42c9805 2020-11-24 stsp if (err) {
8182 c42c9805 2020-11-24 stsp free(*commit_id);
8183 c42c9805 2020-11-24 stsp *commit_id = NULL;
8184 c42c9805 2020-11-24 stsp }
8185 c42c9805 2020-11-24 stsp return err;
8186 c42c9805 2020-11-24 stsp }
8187 c42c9805 2020-11-24 stsp
8188 c42c9805 2020-11-24 stsp static const struct got_error *
8189 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8190 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8191 c42c9805 2020-11-24 stsp {
8192 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8193 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8194 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8195 c42c9805 2020-11-24 stsp
8196 c42c9805 2020-11-24 stsp *new_view = NULL;
8197 c42c9805 2020-11-24 stsp
8198 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8199 c42c9805 2020-11-24 stsp if (err) {
8200 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8201 c42c9805 2020-11-24 stsp return err;
8202 c42c9805 2020-11-24 stsp else
8203 c42c9805 2020-11-24 stsp return NULL;
8204 c42c9805 2020-11-24 stsp }
8205 c42c9805 2020-11-24 stsp
8206 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8207 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8208 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8209 6458efa5 2020-11-24 stsp goto done;
8210 6458efa5 2020-11-24 stsp }
8211 6458efa5 2020-11-24 stsp
8212 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8213 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
8214 6458efa5 2020-11-24 stsp done:
8215 6458efa5 2020-11-24 stsp if (err)
8216 6458efa5 2020-11-24 stsp view_close(log_view);
8217 6458efa5 2020-11-24 stsp else
8218 6458efa5 2020-11-24 stsp *new_view = log_view;
8219 c42c9805 2020-11-24 stsp free(commit_id);
8220 6458efa5 2020-11-24 stsp return err;
8221 6458efa5 2020-11-24 stsp }
8222 6458efa5 2020-11-24 stsp
8223 ce5b7c56 2019-07-09 stsp static void
8224 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8225 6458efa5 2020-11-24 stsp {
8226 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8227 34ba6917 2020-11-30 stsp int i = 0;
8228 6458efa5 2020-11-24 stsp
8229 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8230 6458efa5 2020-11-24 stsp return;
8231 6458efa5 2020-11-24 stsp
8232 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8233 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8234 34ba6917 2020-11-30 stsp if (re == NULL)
8235 34ba6917 2020-11-30 stsp break;
8236 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8237 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8238 6458efa5 2020-11-24 stsp }
8239 6458efa5 2020-11-24 stsp }
8240 6458efa5 2020-11-24 stsp
8241 a5d43cac 2022-07-01 thomas static const struct got_error *
8242 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
8243 6458efa5 2020-11-24 stsp {
8244 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8245 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8246 6458efa5 2020-11-24 stsp int n = 0;
8247 6458efa5 2020-11-24 stsp
8248 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8249 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8250 6458efa5 2020-11-24 stsp else
8251 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8252 6458efa5 2020-11-24 stsp
8253 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8254 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
8255 07dd3ed3 2022-08-06 thomas if (last) {
8256 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
8257 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
8258 07dd3ed3 2022-08-06 thomas }
8259 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8260 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8261 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8262 6458efa5 2020-11-24 stsp }
8263 6458efa5 2020-11-24 stsp }
8264 a5d43cac 2022-07-01 thomas
8265 a5d43cac 2022-07-01 thomas return NULL;
8266 6458efa5 2020-11-24 stsp }
8267 6458efa5 2020-11-24 stsp
8268 6458efa5 2020-11-24 stsp static const struct got_error *
8269 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8270 6458efa5 2020-11-24 stsp {
8271 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8272 6458efa5 2020-11-24 stsp
8273 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8274 6458efa5 2020-11-24 stsp return NULL;
8275 6458efa5 2020-11-24 stsp }
8276 6458efa5 2020-11-24 stsp
8277 6458efa5 2020-11-24 stsp static int
8278 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8279 6458efa5 2020-11-24 stsp {
8280 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8281 6458efa5 2020-11-24 stsp
8282 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
8283 6458efa5 2020-11-24 stsp 0) == 0;
8284 6458efa5 2020-11-24 stsp }
8285 6458efa5 2020-11-24 stsp
8286 6458efa5 2020-11-24 stsp static const struct got_error *
8287 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8288 6458efa5 2020-11-24 stsp {
8289 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8290 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8291 6458efa5 2020-11-24 stsp
8292 6458efa5 2020-11-24 stsp if (!view->searching) {
8293 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8294 6458efa5 2020-11-24 stsp return NULL;
8295 6458efa5 2020-11-24 stsp }
8296 6458efa5 2020-11-24 stsp
8297 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8298 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8299 6458efa5 2020-11-24 stsp if (s->selected_entry)
8300 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8301 6458efa5 2020-11-24 stsp else
8302 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8303 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8304 6458efa5 2020-11-24 stsp } else {
8305 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8306 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8307 6458efa5 2020-11-24 stsp else
8308 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8309 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8310 6458efa5 2020-11-24 stsp }
8311 6458efa5 2020-11-24 stsp } else {
8312 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8313 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8314 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8315 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8316 6458efa5 2020-11-24 stsp else
8317 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8318 6458efa5 2020-11-24 stsp }
8319 6458efa5 2020-11-24 stsp
8320 6458efa5 2020-11-24 stsp while (1) {
8321 6458efa5 2020-11-24 stsp if (re == NULL) {
8322 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8323 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8324 6458efa5 2020-11-24 stsp return NULL;
8325 6458efa5 2020-11-24 stsp }
8326 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8327 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8328 6458efa5 2020-11-24 stsp else
8329 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8330 6458efa5 2020-11-24 stsp }
8331 6458efa5 2020-11-24 stsp
8332 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8333 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8334 6458efa5 2020-11-24 stsp s->matched_entry = re;
8335 6458efa5 2020-11-24 stsp break;
8336 6458efa5 2020-11-24 stsp }
8337 6458efa5 2020-11-24 stsp
8338 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8339 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8340 6458efa5 2020-11-24 stsp else
8341 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8342 6458efa5 2020-11-24 stsp }
8343 6458efa5 2020-11-24 stsp
8344 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8345 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8346 6458efa5 2020-11-24 stsp s->selected = 0;
8347 6458efa5 2020-11-24 stsp }
8348 6458efa5 2020-11-24 stsp
8349 6458efa5 2020-11-24 stsp return NULL;
8350 6458efa5 2020-11-24 stsp }
8351 6458efa5 2020-11-24 stsp
8352 6458efa5 2020-11-24 stsp static const struct got_error *
8353 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8354 6458efa5 2020-11-24 stsp {
8355 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8356 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8357 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8358 6458efa5 2020-11-24 stsp char *line = NULL;
8359 6458efa5 2020-11-24 stsp wchar_t *wline;
8360 6458efa5 2020-11-24 stsp struct tog_color *tc;
8361 66a70b97 2023-02-03 thomas int width, n, scrollx;
8362 6458efa5 2020-11-24 stsp int limit = view->nlines;
8363 6458efa5 2020-11-24 stsp
8364 6458efa5 2020-11-24 stsp werase(view->window);
8365 6458efa5 2020-11-24 stsp
8366 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8367 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8368 a5d43cac 2022-07-01 thomas --limit; /* border */
8369 6458efa5 2020-11-24 stsp
8370 6458efa5 2020-11-24 stsp if (limit == 0)
8371 6458efa5 2020-11-24 stsp return NULL;
8372 6458efa5 2020-11-24 stsp
8373 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8374 6458efa5 2020-11-24 stsp
8375 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8376 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8377 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8378 6458efa5 2020-11-24 stsp
8379 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8380 6458efa5 2020-11-24 stsp if (err) {
8381 6458efa5 2020-11-24 stsp free(line);
8382 6458efa5 2020-11-24 stsp return err;
8383 6458efa5 2020-11-24 stsp }
8384 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8385 6458efa5 2020-11-24 stsp wstandout(view->window);
8386 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8387 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8388 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8389 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8390 6458efa5 2020-11-24 stsp wstandend(view->window);
8391 6458efa5 2020-11-24 stsp free(wline);
8392 6458efa5 2020-11-24 stsp wline = NULL;
8393 6458efa5 2020-11-24 stsp free(line);
8394 6458efa5 2020-11-24 stsp line = NULL;
8395 6458efa5 2020-11-24 stsp if (--limit <= 0)
8396 6458efa5 2020-11-24 stsp return NULL;
8397 6458efa5 2020-11-24 stsp
8398 6458efa5 2020-11-24 stsp n = 0;
8399 66a70b97 2023-02-03 thomas view->maxx = 0;
8400 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8401 6458efa5 2020-11-24 stsp char *line = NULL;
8402 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8403 6458efa5 2020-11-24 stsp
8404 84227eb1 2022-06-23 thomas if (s->show_date) {
8405 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8406 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8407 84227eb1 2022-06-23 thomas struct got_object_id *id;
8408 84227eb1 2022-06-23 thomas struct tm tm;
8409 84227eb1 2022-06-23 thomas time_t t;
8410 84227eb1 2022-06-23 thomas
8411 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8412 84227eb1 2022-06-23 thomas if (err)
8413 84227eb1 2022-06-23 thomas return err;
8414 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8415 84227eb1 2022-06-23 thomas if (err) {
8416 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8417 84227eb1 2022-06-23 thomas free(id);
8418 84227eb1 2022-06-23 thomas return err;
8419 84227eb1 2022-06-23 thomas }
8420 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8421 84227eb1 2022-06-23 thomas id);
8422 84227eb1 2022-06-23 thomas if (err) {
8423 84227eb1 2022-06-23 thomas free(id);
8424 84227eb1 2022-06-23 thomas return err;
8425 84227eb1 2022-06-23 thomas }
8426 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8427 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8428 84227eb1 2022-06-23 thomas } else {
8429 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8430 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8431 84227eb1 2022-06-23 thomas }
8432 84227eb1 2022-06-23 thomas free(id);
8433 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8434 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8435 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8436 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8437 84227eb1 2022-06-23 thomas }
8438 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8439 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8440 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8441 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8442 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8443 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8444 6458efa5 2020-11-24 stsp struct got_object_id *id;
8445 6458efa5 2020-11-24 stsp char *id_str;
8446 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8447 6458efa5 2020-11-24 stsp if (err)
8448 6458efa5 2020-11-24 stsp return err;
8449 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8450 6458efa5 2020-11-24 stsp if (err) {
8451 6458efa5 2020-11-24 stsp free(id);
8452 6458efa5 2020-11-24 stsp return err;
8453 6458efa5 2020-11-24 stsp }
8454 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8455 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8456 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8457 6458efa5 2020-11-24 stsp free(id);
8458 6458efa5 2020-11-24 stsp free(id_str);
8459 6458efa5 2020-11-24 stsp return err;
8460 6458efa5 2020-11-24 stsp }
8461 6458efa5 2020-11-24 stsp free(id);
8462 6458efa5 2020-11-24 stsp free(id_str);
8463 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8464 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8465 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8466 6458efa5 2020-11-24 stsp
8467 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8468 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8469 6458efa5 2020-11-24 stsp if (err) {
8470 6458efa5 2020-11-24 stsp free(line);
8471 6458efa5 2020-11-24 stsp return err;
8472 6458efa5 2020-11-24 stsp }
8473 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8474 66a70b97 2023-02-03 thomas free(wline);
8475 66a70b97 2023-02-03 thomas wline = NULL;
8476 66a70b97 2023-02-03 thomas
8477 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8478 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8479 66a70b97 2023-02-03 thomas if (err) {
8480 66a70b97 2023-02-03 thomas free(line);
8481 66a70b97 2023-02-03 thomas return err;
8482 66a70b97 2023-02-03 thomas }
8483 6458efa5 2020-11-24 stsp if (n == s->selected) {
8484 6458efa5 2020-11-24 stsp if (view->focussed)
8485 6458efa5 2020-11-24 stsp wstandout(view->window);
8486 6458efa5 2020-11-24 stsp s->selected_entry = re;
8487 6458efa5 2020-11-24 stsp }
8488 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8489 6458efa5 2020-11-24 stsp if (tc)
8490 6458efa5 2020-11-24 stsp wattr_on(view->window,
8491 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8492 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8493 6458efa5 2020-11-24 stsp if (tc)
8494 6458efa5 2020-11-24 stsp wattr_off(view->window,
8495 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8496 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8497 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8498 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8499 6458efa5 2020-11-24 stsp wstandend(view->window);
8500 6458efa5 2020-11-24 stsp free(line);
8501 6458efa5 2020-11-24 stsp free(wline);
8502 6458efa5 2020-11-24 stsp wline = NULL;
8503 6458efa5 2020-11-24 stsp n++;
8504 6458efa5 2020-11-24 stsp s->ndisplayed++;
8505 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8506 6458efa5 2020-11-24 stsp
8507 6458efa5 2020-11-24 stsp limit--;
8508 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8509 6458efa5 2020-11-24 stsp }
8510 6458efa5 2020-11-24 stsp
8511 a5d43cac 2022-07-01 thomas view_border(view);
8512 6458efa5 2020-11-24 stsp return err;
8513 6458efa5 2020-11-24 stsp }
8514 6458efa5 2020-11-24 stsp
8515 6458efa5 2020-11-24 stsp static const struct got_error *
8516 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8517 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8518 c42c9805 2020-11-24 stsp {
8519 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8520 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8521 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8522 c42c9805 2020-11-24 stsp
8523 c42c9805 2020-11-24 stsp *new_view = NULL;
8524 c42c9805 2020-11-24 stsp
8525 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8526 c42c9805 2020-11-24 stsp if (err) {
8527 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8528 c42c9805 2020-11-24 stsp return err;
8529 c42c9805 2020-11-24 stsp else
8530 c42c9805 2020-11-24 stsp return NULL;
8531 c42c9805 2020-11-24 stsp }
8532 c42c9805 2020-11-24 stsp
8533 c42c9805 2020-11-24 stsp
8534 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8535 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8536 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8537 c42c9805 2020-11-24 stsp goto done;
8538 c42c9805 2020-11-24 stsp }
8539 c42c9805 2020-11-24 stsp
8540 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8541 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8542 c42c9805 2020-11-24 stsp if (err)
8543 c42c9805 2020-11-24 stsp goto done;
8544 c42c9805 2020-11-24 stsp
8545 c42c9805 2020-11-24 stsp *new_view = tree_view;
8546 c42c9805 2020-11-24 stsp done:
8547 c42c9805 2020-11-24 stsp free(commit_id);
8548 c42c9805 2020-11-24 stsp return err;
8549 07dd3ed3 2022-08-06 thomas }
8550 07dd3ed3 2022-08-06 thomas
8551 07dd3ed3 2022-08-06 thomas static const struct got_error *
8552 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8553 07dd3ed3 2022-08-06 thomas {
8554 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8555 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8556 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8557 07dd3ed3 2022-08-06 thomas
8558 07dd3ed3 2022-08-06 thomas g = view->gline;
8559 07dd3ed3 2022-08-06 thomas view->gline = 0;
8560 07dd3ed3 2022-08-06 thomas
8561 07dd3ed3 2022-08-06 thomas if (g == 0)
8562 07dd3ed3 2022-08-06 thomas g = 1;
8563 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8564 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8565 07dd3ed3 2022-08-06 thomas
8566 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8567 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8568 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8569 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8570 07dd3ed3 2022-08-06 thomas return NULL;
8571 07dd3ed3 2022-08-06 thomas }
8572 07dd3ed3 2022-08-06 thomas
8573 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8574 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8575 07dd3ed3 2022-08-06 thomas if (err)
8576 07dd3ed3 2022-08-06 thomas return err;
8577 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8578 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8579 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8580 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8581 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8582 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8583 07dd3ed3 2022-08-06 thomas
8584 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8585 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8586 07dd3ed3 2022-08-06 thomas
8587 07dd3ed3 2022-08-06 thomas return NULL;
8588 07dd3ed3 2022-08-06 thomas
8589 c42c9805 2020-11-24 stsp }
8590 07dd3ed3 2022-08-06 thomas
8591 c42c9805 2020-11-24 stsp static const struct got_error *
8592 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8593 6458efa5 2020-11-24 stsp {
8594 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8595 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8596 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8597 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8598 6458efa5 2020-11-24 stsp
8599 07dd3ed3 2022-08-06 thomas if (view->gline)
8600 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8601 07dd3ed3 2022-08-06 thomas
8602 6458efa5 2020-11-24 stsp switch (ch) {
8603 66a70b97 2023-02-03 thomas case '0':
8604 66a70b97 2023-02-03 thomas case '$':
8605 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8606 66a70b97 2023-02-03 thomas case 'l':
8607 66a70b97 2023-02-03 thomas case KEY_LEFT:
8608 66a70b97 2023-02-03 thomas case 'h':
8609 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8610 66a70b97 2023-02-03 thomas break;
8611 6458efa5 2020-11-24 stsp case 'i':
8612 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8613 07b0611c 2022-06-23 thomas view->count = 0;
8614 3bfadbd4 2021-11-20 thomas break;
8615 84227eb1 2022-06-23 thomas case 'm':
8616 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8617 07b0611c 2022-06-23 thomas view->count = 0;
8618 84227eb1 2022-06-23 thomas break;
8619 98182bd0 2021-11-20 thomas case 'o':
8620 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8621 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8622 07b0611c 2022-06-23 thomas view->count = 0;
8623 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8624 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8625 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8626 c591440f 2021-11-20 thomas if (err)
8627 c591440f 2021-11-20 thomas break;
8628 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8629 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8630 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8631 3bfadbd4 2021-11-20 thomas if (err)
8632 3bfadbd4 2021-11-20 thomas break;
8633 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8634 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8635 6458efa5 2020-11-24 stsp break;
8636 6458efa5 2020-11-24 stsp case KEY_ENTER:
8637 6458efa5 2020-11-24 stsp case '\r':
8638 07b0611c 2022-06-23 thomas view->count = 0;
8639 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8640 a5d43cac 2022-07-01 thomas break;
8641 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8642 6458efa5 2020-11-24 stsp break;
8643 1be4947a 2022-07-22 thomas case 'T':
8644 07b0611c 2022-06-23 thomas view->count = 0;
8645 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8646 c42c9805 2020-11-24 stsp break;
8647 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8648 c42c9805 2020-11-24 stsp break;
8649 e4526bf5 2021-09-03 naddy case 'g':
8650 aa7a1117 2023-01-09 thomas case '=':
8651 e4526bf5 2021-09-03 naddy case KEY_HOME:
8652 e4526bf5 2021-09-03 naddy s->selected = 0;
8653 07b0611c 2022-06-23 thomas view->count = 0;
8654 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8655 e4526bf5 2021-09-03 naddy break;
8656 e4526bf5 2021-09-03 naddy case 'G':
8657 aa7a1117 2023-01-09 thomas case '*':
8658 a5d43cac 2022-07-01 thomas case KEY_END: {
8659 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8660 a5d43cac 2022-07-01 thomas
8661 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8662 a5d43cac 2022-07-01 thomas --eos; /* border */
8663 e4526bf5 2021-09-03 naddy s->selected = 0;
8664 07b0611c 2022-06-23 thomas view->count = 0;
8665 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8666 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8667 e4526bf5 2021-09-03 naddy if (re == NULL)
8668 e4526bf5 2021-09-03 naddy break;
8669 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8670 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8671 e4526bf5 2021-09-03 naddy }
8672 e4526bf5 2021-09-03 naddy if (n > 0)
8673 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8674 e4526bf5 2021-09-03 naddy break;
8675 a5d43cac 2022-07-01 thomas }
8676 6458efa5 2020-11-24 stsp case 'k':
8677 6458efa5 2020-11-24 stsp case KEY_UP:
8678 f7140bf5 2021-10-17 thomas case CTRL('p'):
8679 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8680 6458efa5 2020-11-24 stsp s->selected--;
8681 6458efa5 2020-11-24 stsp break;
8682 34ba6917 2020-11-30 stsp }
8683 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8684 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8685 07b0611c 2022-06-23 thomas view->count = 0;
8686 6458efa5 2020-11-24 stsp break;
8687 70f17a53 2022-06-13 thomas case CTRL('u'):
8688 23427b14 2022-06-23 thomas case 'u':
8689 70f17a53 2022-06-13 thomas nscroll /= 2;
8690 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8691 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8692 6458efa5 2020-11-24 stsp case CTRL('b'):
8693 1c5e5faa 2022-06-23 thomas case 'b':
8694 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8695 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8696 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8697 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8698 07b0611c 2022-06-23 thomas view->count = 0;
8699 6458efa5 2020-11-24 stsp break;
8700 6458efa5 2020-11-24 stsp case 'j':
8701 6458efa5 2020-11-24 stsp case KEY_DOWN:
8702 f7140bf5 2021-10-17 thomas case CTRL('n'):
8703 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8704 6458efa5 2020-11-24 stsp s->selected++;
8705 6458efa5 2020-11-24 stsp break;
8706 6458efa5 2020-11-24 stsp }
8707 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8708 6458efa5 2020-11-24 stsp /* can't scroll any further */
8709 07b0611c 2022-06-23 thomas view->count = 0;
8710 6458efa5 2020-11-24 stsp break;
8711 07b0611c 2022-06-23 thomas }
8712 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8713 6458efa5 2020-11-24 stsp break;
8714 70f17a53 2022-06-13 thomas case CTRL('d'):
8715 23427b14 2022-06-23 thomas case 'd':
8716 70f17a53 2022-06-13 thomas nscroll /= 2;
8717 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8718 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8719 6458efa5 2020-11-24 stsp case CTRL('f'):
8720 1c5e5faa 2022-06-23 thomas case 'f':
8721 4c2d69cb 2022-06-23 thomas case ' ':
8722 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8723 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8724 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8725 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8726 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8727 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8728 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8729 07b0611c 2022-06-23 thomas view->count = 0;
8730 6458efa5 2020-11-24 stsp break;
8731 6458efa5 2020-11-24 stsp }
8732 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8733 6458efa5 2020-11-24 stsp break;
8734 6458efa5 2020-11-24 stsp case CTRL('l'):
8735 07b0611c 2022-06-23 thomas view->count = 0;
8736 8924d611 2020-12-26 stsp tog_free_refs();
8737 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8738 8924d611 2020-12-26 stsp if (err)
8739 8924d611 2020-12-26 stsp break;
8740 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8741 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8742 6458efa5 2020-11-24 stsp break;
8743 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8744 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8745 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8746 6458efa5 2020-11-24 stsp break;
8747 6458efa5 2020-11-24 stsp default:
8748 07b0611c 2022-06-23 thomas view->count = 0;
8749 6458efa5 2020-11-24 stsp break;
8750 6458efa5 2020-11-24 stsp }
8751 6458efa5 2020-11-24 stsp
8752 6458efa5 2020-11-24 stsp return err;
8753 6458efa5 2020-11-24 stsp }
8754 6458efa5 2020-11-24 stsp
8755 6458efa5 2020-11-24 stsp __dead static void
8756 6458efa5 2020-11-24 stsp usage_ref(void)
8757 6458efa5 2020-11-24 stsp {
8758 6458efa5 2020-11-24 stsp endwin();
8759 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8760 6458efa5 2020-11-24 stsp getprogname());
8761 6458efa5 2020-11-24 stsp exit(1);
8762 6458efa5 2020-11-24 stsp }
8763 6458efa5 2020-11-24 stsp
8764 6458efa5 2020-11-24 stsp static const struct got_error *
8765 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8766 6458efa5 2020-11-24 stsp {
8767 1d98034b 2023-04-14 thomas const struct got_error *error;
8768 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8769 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8770 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8771 6458efa5 2020-11-24 stsp int ch;
8772 6458efa5 2020-11-24 stsp struct tog_view *view;
8773 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8774 6458efa5 2020-11-24 stsp
8775 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8776 6458efa5 2020-11-24 stsp switch (ch) {
8777 6458efa5 2020-11-24 stsp case 'r':
8778 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8779 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8780 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8781 6458efa5 2020-11-24 stsp optarg);
8782 6458efa5 2020-11-24 stsp break;
8783 6458efa5 2020-11-24 stsp default:
8784 e99e2d15 2020-11-24 naddy usage_ref();
8785 6458efa5 2020-11-24 stsp /* NOTREACHED */
8786 6458efa5 2020-11-24 stsp }
8787 6458efa5 2020-11-24 stsp }
8788 6458efa5 2020-11-24 stsp
8789 6458efa5 2020-11-24 stsp argc -= optind;
8790 6458efa5 2020-11-24 stsp argv += optind;
8791 6458efa5 2020-11-24 stsp
8792 6458efa5 2020-11-24 stsp if (argc > 1)
8793 e99e2d15 2020-11-24 naddy usage_ref();
8794 7cd52833 2022-06-23 thomas
8795 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8796 7cd52833 2022-06-23 thomas if (error != NULL)
8797 7cd52833 2022-06-23 thomas goto done;
8798 6458efa5 2020-11-24 stsp
8799 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8800 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8801 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8802 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8803 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8804 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8805 c156c7a4 2020-12-18 stsp goto done;
8806 6458efa5 2020-11-24 stsp if (worktree)
8807 6458efa5 2020-11-24 stsp repo_path =
8808 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8809 6458efa5 2020-11-24 stsp else
8810 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8811 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8812 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8813 c156c7a4 2020-12-18 stsp goto done;
8814 c156c7a4 2020-12-18 stsp }
8815 6458efa5 2020-11-24 stsp }
8816 6458efa5 2020-11-24 stsp
8817 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8818 6458efa5 2020-11-24 stsp if (error != NULL)
8819 6458efa5 2020-11-24 stsp goto done;
8820 6458efa5 2020-11-24 stsp
8821 557d3365 2023-04-14 thomas init_curses();
8822 6458efa5 2020-11-24 stsp
8823 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8824 51a10b52 2020-12-26 stsp if (error)
8825 51a10b52 2020-12-26 stsp goto done;
8826 51a10b52 2020-12-26 stsp
8827 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8828 6458efa5 2020-11-24 stsp if (error)
8829 6458efa5 2020-11-24 stsp goto done;
8830 6458efa5 2020-11-24 stsp
8831 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8832 6458efa5 2020-11-24 stsp if (view == NULL) {
8833 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8834 6458efa5 2020-11-24 stsp goto done;
8835 6458efa5 2020-11-24 stsp }
8836 6458efa5 2020-11-24 stsp
8837 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8838 6458efa5 2020-11-24 stsp if (error)
8839 6458efa5 2020-11-24 stsp goto done;
8840 6458efa5 2020-11-24 stsp
8841 6458efa5 2020-11-24 stsp if (worktree) {
8842 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8843 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8844 6458efa5 2020-11-24 stsp worktree = NULL;
8845 6458efa5 2020-11-24 stsp }
8846 557d3365 2023-04-14 thomas error = view_loop(view);
8847 6458efa5 2020-11-24 stsp done:
8848 6458efa5 2020-11-24 stsp free(repo_path);
8849 6458efa5 2020-11-24 stsp free(cwd);
8850 1d0f4054 2021-06-17 stsp if (repo) {
8851 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8852 1d0f4054 2021-06-17 stsp if (close_err)
8853 1d0f4054 2021-06-17 stsp error = close_err;
8854 1d0f4054 2021-06-17 stsp }
8855 7cd52833 2022-06-23 thomas if (pack_fds) {
8856 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8857 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8858 7cd52833 2022-06-23 thomas if (error == NULL)
8859 7cd52833 2022-06-23 thomas error = pack_err;
8860 b85a3496 2023-04-14 thomas }
8861 51a10b52 2020-12-26 stsp tog_free_refs();
8862 6458efa5 2020-11-24 stsp return error;
8863 6458efa5 2020-11-24 stsp }
8864 6458efa5 2020-11-24 stsp
8865 fc2737d5 2022-09-15 thomas static const struct got_error*
8866 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8867 fc2737d5 2022-09-15 thomas const char *str)
8868 fc2737d5 2022-09-15 thomas {
8869 fc2737d5 2022-09-15 thomas size_t len;
8870 fc2737d5 2022-09-15 thomas
8871 fc2737d5 2022-09-15 thomas if (win == NULL)
8872 fc2737d5 2022-09-15 thomas win = stdscr;
8873 fc2737d5 2022-09-15 thomas
8874 fc2737d5 2022-09-15 thomas len = strlen(str);
8875 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8876 fc2737d5 2022-09-15 thomas
8877 fc2737d5 2022-09-15 thomas if (focus)
8878 fc2737d5 2022-09-15 thomas wstandout(win);
8879 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8880 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8881 fc2737d5 2022-09-15 thomas if (focus)
8882 fc2737d5 2022-09-15 thomas wstandend(win);
8883 fc2737d5 2022-09-15 thomas
8884 fc2737d5 2022-09-15 thomas return NULL;
8885 fc2737d5 2022-09-15 thomas }
8886 fc2737d5 2022-09-15 thomas
8887 2a31b33b 2022-07-23 thomas static const struct got_error *
8888 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8889 fc2737d5 2022-09-15 thomas {
8890 fc2737d5 2022-09-15 thomas off_t *p;
8891 fc2737d5 2022-09-15 thomas
8892 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8893 fc2737d5 2022-09-15 thomas if (p == NULL) {
8894 fc2737d5 2022-09-15 thomas free(*line_offsets);
8895 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8896 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8897 fc2737d5 2022-09-15 thomas }
8898 fc2737d5 2022-09-15 thomas
8899 fc2737d5 2022-09-15 thomas *line_offsets = p;
8900 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8901 fc2737d5 2022-09-15 thomas ++(*nlines);
8902 fc2737d5 2022-09-15 thomas return NULL;
8903 fc2737d5 2022-09-15 thomas }
8904 fc2737d5 2022-09-15 thomas
8905 fc2737d5 2022-09-15 thomas static const struct got_error *
8906 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8907 fc2737d5 2022-09-15 thomas {
8908 fc2737d5 2022-09-15 thomas *ret = 0;
8909 fc2737d5 2022-09-15 thomas
8910 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8911 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8912 fc2737d5 2022-09-15 thomas size_t len = 1;
8913 fc2737d5 2022-09-15 thomas
8914 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8915 fc2737d5 2022-09-15 thomas continue;
8916 fc2737d5 2022-09-15 thomas
8917 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8918 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8919 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8920 fc2737d5 2022-09-15 thomas
8921 fc2737d5 2022-09-15 thomas len += strlen(t);
8922 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8923 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8924 fc2737d5 2022-09-15 thomas free(t0);
8925 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8926 fc2737d5 2022-09-15 thomas }
8927 fc2737d5 2022-09-15 thomas
8928 fc2737d5 2022-09-15 thomas return NULL;
8929 fc2737d5 2022-09-15 thomas }
8930 fc2737d5 2022-09-15 thomas
8931 fc2737d5 2022-09-15 thomas /*
8932 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8933 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8934 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8935 fc2737d5 2022-09-15 thomas */
8936 fc2737d5 2022-09-15 thomas static const struct got_error *
8937 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8938 fc2737d5 2022-09-15 thomas {
8939 fc2737d5 2022-09-15 thomas int n, len = width;
8940 fc2737d5 2022-09-15 thomas
8941 fc2737d5 2022-09-15 thomas if (km->keys) {
8942 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8943 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8944 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8945 30e77f6a 2022-09-18 thomas };
8946 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8947 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8948 fc2737d5 2022-09-15 thomas
8949 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8950 fc2737d5 2022-09-15 thomas
8951 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8952 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8953 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8954 fc2737d5 2022-09-15 thomas
8955 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8956 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8957 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8958 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8959 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8960 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8961 fc2737d5 2022-09-15 thomas if (n < 0) {
8962 fc2737d5 2022-09-15 thomas free(t0);
8963 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8964 fc2737d5 2022-09-15 thomas }
8965 fc2737d5 2022-09-15 thomas first = 0;
8966 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8967 fc2737d5 2022-09-15 thomas *off += n;
8968 fc2737d5 2022-09-15 thomas }
8969 fc2737d5 2022-09-15 thomas free(t0);
8970 fc2737d5 2022-09-15 thomas }
8971 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8972 fc2737d5 2022-09-15 thomas if (n < 0)
8973 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8974 fc2737d5 2022-09-15 thomas *off += n;
8975 fc2737d5 2022-09-15 thomas
8976 fc2737d5 2022-09-15 thomas return NULL;
8977 fc2737d5 2022-09-15 thomas }
8978 fc2737d5 2022-09-15 thomas
8979 fc2737d5 2022-09-15 thomas static const struct got_error *
8980 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
8981 fc2737d5 2022-09-15 thomas {
8982 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8983 fc2737d5 2022-09-15 thomas off_t off = 0;
8984 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
8985 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
8986 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
8987 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8988 fc2737d5 2022-09-15 thomas GENERATE_HELP
8989 fc2737d5 2022-09-15 thomas #undef KEYMAP_
8990 fc2737d5 2022-09-15 thomas #undef KEY_
8991 fc2737d5 2022-09-15 thomas };
8992 fc2737d5 2022-09-15 thomas
8993 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8994 fc2737d5 2022-09-15 thomas if (err)
8995 fc2737d5 2022-09-15 thomas return err;
8996 fc2737d5 2022-09-15 thomas
8997 fc2737d5 2022-09-15 thomas n = nitems(km);
8998 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
8999 fc2737d5 2022-09-15 thomas if (err)
9000 fc2737d5 2022-09-15 thomas return err;
9001 fc2737d5 2022-09-15 thomas
9002 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
9003 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
9004 fc2737d5 2022-09-15 thomas show = s->all;
9005 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
9006 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
9007 fc2737d5 2022-09-15 thomas show = 1;
9008 fc2737d5 2022-09-15 thomas }
9009 fc2737d5 2022-09-15 thomas if (show) {
9010 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
9011 fc2737d5 2022-09-15 thomas if (err)
9012 fc2737d5 2022-09-15 thomas return err;
9013 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9014 fc2737d5 2022-09-15 thomas if (err)
9015 fc2737d5 2022-09-15 thomas return err;
9016 fc2737d5 2022-09-15 thomas }
9017 fc2737d5 2022-09-15 thomas }
9018 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
9019 fc2737d5 2022-09-15 thomas ++off;
9020 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9021 fc2737d5 2022-09-15 thomas return err;
9022 fc2737d5 2022-09-15 thomas }
9023 fc2737d5 2022-09-15 thomas
9024 fc2737d5 2022-09-15 thomas static const struct got_error *
9025 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
9026 fc2737d5 2022-09-15 thomas {
9027 fc2737d5 2022-09-15 thomas FILE *f;
9028 fc2737d5 2022-09-15 thomas const struct got_error *err;
9029 fc2737d5 2022-09-15 thomas
9030 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9031 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9032 fc2737d5 2022-09-15 thomas s->nlines = 0;
9033 fc2737d5 2022-09-15 thomas
9034 fc2737d5 2022-09-15 thomas f = got_opentemp();
9035 fc2737d5 2022-09-15 thomas if (f == NULL)
9036 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
9037 fc2737d5 2022-09-15 thomas s->f = f;
9038 fc2737d5 2022-09-15 thomas
9039 fc2737d5 2022-09-15 thomas err = format_help(s);
9040 fc2737d5 2022-09-15 thomas if (err)
9041 fc2737d5 2022-09-15 thomas return err;
9042 fc2737d5 2022-09-15 thomas
9043 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
9044 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
9045 fc2737d5 2022-09-15 thomas
9046 fc2737d5 2022-09-15 thomas return NULL;
9047 fc2737d5 2022-09-15 thomas }
9048 fc2737d5 2022-09-15 thomas
9049 fc2737d5 2022-09-15 thomas static const struct got_error *
9050 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
9051 fc2737d5 2022-09-15 thomas {
9052 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
9053 fc2737d5 2022-09-15 thomas return NULL;
9054 fc2737d5 2022-09-15 thomas }
9055 fc2737d5 2022-09-15 thomas
9056 2a7d3cd7 2022-09-17 thomas static void
9057 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9058 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
9059 2a7d3cd7 2022-09-17 thomas {
9060 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
9061 2a7d3cd7 2022-09-17 thomas
9062 2a7d3cd7 2022-09-17 thomas *f = s->f;
9063 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
9064 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
9065 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
9066 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
9067 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
9068 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
9069 2a7d3cd7 2022-09-17 thomas }
9070 2a7d3cd7 2022-09-17 thomas
9071 fc2737d5 2022-09-15 thomas static const struct got_error *
9072 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
9073 fc2737d5 2022-09-15 thomas {
9074 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9075 fc2737d5 2022-09-15 thomas const struct got_error *err;
9076 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
9077 fc2737d5 2022-09-15 thomas wchar_t *wline;
9078 fc2737d5 2022-09-15 thomas char *line;
9079 fc2737d5 2022-09-15 thomas ssize_t linelen;
9080 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9081 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
9082 fc2737d5 2022-09-15 thomas int eos = view->nlines;
9083 fc2737d5 2022-09-15 thomas
9084 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9085 fc2737d5 2022-09-15 thomas --eos; /* account for border */
9086 fc2737d5 2022-09-15 thomas
9087 fc2737d5 2022-09-15 thomas s->lineno = 0;
9088 fc2737d5 2022-09-15 thomas rewind(s->f);
9089 fc2737d5 2022-09-15 thomas werase(view->window);
9090 fc2737d5 2022-09-15 thomas
9091 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
9092 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
9093 fc2737d5 2022-09-15 thomas
9094 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
9095 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
9096 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
9097 fc2737d5 2022-09-15 thomas if (err)
9098 fc2737d5 2022-09-15 thomas return err;
9099 fc2737d5 2022-09-15 thomas if (eos <= 1)
9100 fc2737d5 2022-09-15 thomas return NULL;
9101 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
9102 fc2737d5 2022-09-15 thomas eos -= 2;
9103 fc2737d5 2022-09-15 thomas
9104 fc2737d5 2022-09-15 thomas s->eof = 0;
9105 fc2737d5 2022-09-15 thomas view->maxx = 0;
9106 fc2737d5 2022-09-15 thomas line = NULL;
9107 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
9108 fc2737d5 2022-09-15 thomas attr_t attr = 0;
9109 fc2737d5 2022-09-15 thomas
9110 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9111 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9112 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
9113 fc2737d5 2022-09-15 thomas free(line);
9114 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
9115 fc2737d5 2022-09-15 thomas }
9116 fc2737d5 2022-09-15 thomas s->eof = 1;
9117 fc2737d5 2022-09-15 thomas break;
9118 fc2737d5 2022-09-15 thomas }
9119 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
9120 fc2737d5 2022-09-15 thomas continue;
9121 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9122 fc2737d5 2022-09-15 thomas continue;
9123 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
9124 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
9125 fc2737d5 2022-09-15 thomas
9126 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9127 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
9128 fc2737d5 2022-09-15 thomas if (err) {
9129 fc2737d5 2022-09-15 thomas free(line);
9130 fc2737d5 2022-09-15 thomas return err;
9131 fc2737d5 2022-09-15 thomas }
9132 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
9133 fc2737d5 2022-09-15 thomas free(wline);
9134 fc2737d5 2022-09-15 thomas wline = NULL;
9135 fc2737d5 2022-09-15 thomas
9136 fc2737d5 2022-09-15 thomas if (attr)
9137 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
9138 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
9139 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9140 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
9141 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
9142 fc2737d5 2022-09-15 thomas if (err) {
9143 fc2737d5 2022-09-15 thomas free(line);
9144 fc2737d5 2022-09-15 thomas return err;
9145 fc2737d5 2022-09-15 thomas }
9146 fc2737d5 2022-09-15 thomas } else {
9147 fc2737d5 2022-09-15 thomas int skip;
9148 fc2737d5 2022-09-15 thomas
9149 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
9150 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
9151 fc2737d5 2022-09-15 thomas if (err) {
9152 fc2737d5 2022-09-15 thomas free(line);
9153 fc2737d5 2022-09-15 thomas return err;
9154 fc2737d5 2022-09-15 thomas }
9155 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
9156 fc2737d5 2022-09-15 thomas free(wline);
9157 fc2737d5 2022-09-15 thomas wline = NULL;
9158 fc2737d5 2022-09-15 thomas }
9159 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
9160 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
9161 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
9162 fc2737d5 2022-09-15 thomas } else {
9163 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
9164 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
9165 fc2737d5 2022-09-15 thomas }
9166 fc2737d5 2022-09-15 thomas if (attr)
9167 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
9168 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
9169 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
9170 fc2737d5 2022-09-15 thomas }
9171 fc2737d5 2022-09-15 thomas free(line);
9172 fc2737d5 2022-09-15 thomas if (nprinted > 0)
9173 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9174 fc2737d5 2022-09-15 thomas else
9175 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
9176 fc2737d5 2022-09-15 thomas
9177 fc2737d5 2022-09-15 thomas view_border(view);
9178 fc2737d5 2022-09-15 thomas
9179 fc2737d5 2022-09-15 thomas if (s->eof) {
9180 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
9181 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
9182 fc2737d5 2022-09-15 thomas view->ncols - 1);
9183 fc2737d5 2022-09-15 thomas if (rc == ERR)
9184 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9185 fc2737d5 2022-09-15 thomas } else {
9186 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
9187 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
9188 fc2737d5 2022-09-15 thomas wstandout(view->window);
9189 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
9190 fc2737d5 2022-09-15 thomas view->ncols - 1);
9191 fc2737d5 2022-09-15 thomas if (rc == ERR)
9192 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9193 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
9194 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
9195 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
9196 fc2737d5 2022-09-15 thomas if (rc == ERR)
9197 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
9198 fc2737d5 2022-09-15 thomas }
9199 fc2737d5 2022-09-15 thomas wstandend(view->window);
9200 fc2737d5 2022-09-15 thomas }
9201 fc2737d5 2022-09-15 thomas
9202 fc2737d5 2022-09-15 thomas return NULL;
9203 fc2737d5 2022-09-15 thomas }
9204 fc2737d5 2022-09-15 thomas
9205 fc2737d5 2022-09-15 thomas static const struct got_error *
9206 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9207 fc2737d5 2022-09-15 thomas {
9208 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9209 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9210 fc2737d5 2022-09-15 thomas char *line = NULL;
9211 fc2737d5 2022-09-15 thomas ssize_t linelen;
9212 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9213 fc2737d5 2022-09-15 thomas int eos, nscroll;
9214 fc2737d5 2022-09-15 thomas
9215 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
9216 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9217 fc2737d5 2022-09-15 thomas --eos; /* border */
9218 fc2737d5 2022-09-15 thomas
9219 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
9220 fc2737d5 2022-09-15 thomas
9221 fc2737d5 2022-09-15 thomas switch (ch) {
9222 fc2737d5 2022-09-15 thomas case '0':
9223 fc2737d5 2022-09-15 thomas case '$':
9224 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
9225 fc2737d5 2022-09-15 thomas case 'l':
9226 fc2737d5 2022-09-15 thomas case KEY_LEFT:
9227 fc2737d5 2022-09-15 thomas case 'h':
9228 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
9229 fc2737d5 2022-09-15 thomas break;
9230 fc2737d5 2022-09-15 thomas case 'g':
9231 fc2737d5 2022-09-15 thomas case KEY_HOME:
9232 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9233 fc2737d5 2022-09-15 thomas view->count = 0;
9234 fc2737d5 2022-09-15 thomas break;
9235 fc2737d5 2022-09-15 thomas case 'G':
9236 fc2737d5 2022-09-15 thomas case KEY_END:
9237 fc2737d5 2022-09-15 thomas view->count = 0;
9238 fc2737d5 2022-09-15 thomas if (s->eof)
9239 fc2737d5 2022-09-15 thomas break;
9240 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
9241 fc2737d5 2022-09-15 thomas s->eof = 1;
9242 fc2737d5 2022-09-15 thomas break;
9243 fc2737d5 2022-09-15 thomas case 'k':
9244 fc2737d5 2022-09-15 thomas case KEY_UP:
9245 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
9246 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
9247 fc2737d5 2022-09-15 thomas else
9248 fc2737d5 2022-09-15 thomas view->count = 0;
9249 fc2737d5 2022-09-15 thomas break;
9250 fc2737d5 2022-09-15 thomas case CTRL('u'):
9251 fc2737d5 2022-09-15 thomas case 'u':
9252 fc2737d5 2022-09-15 thomas nscroll /= 2;
9253 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9254 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
9255 fc2737d5 2022-09-15 thomas case CTRL('b'):
9256 fc2737d5 2022-09-15 thomas case 'b':
9257 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
9258 fc2737d5 2022-09-15 thomas view->count = 0;
9259 fc2737d5 2022-09-15 thomas break;
9260 fc2737d5 2022-09-15 thomas }
9261 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
9262 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
9263 fc2737d5 2022-09-15 thomas break;
9264 fc2737d5 2022-09-15 thomas case 'j':
9265 fc2737d5 2022-09-15 thomas case KEY_DOWN:
9266 fc2737d5 2022-09-15 thomas case CTRL('n'):
9267 fc2737d5 2022-09-15 thomas if (!s->eof)
9268 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9269 fc2737d5 2022-09-15 thomas else
9270 fc2737d5 2022-09-15 thomas view->count = 0;
9271 fc2737d5 2022-09-15 thomas break;
9272 fc2737d5 2022-09-15 thomas case CTRL('d'):
9273 fc2737d5 2022-09-15 thomas case 'd':
9274 fc2737d5 2022-09-15 thomas nscroll /= 2;
9275 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9276 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9277 fc2737d5 2022-09-15 thomas case CTRL('f'):
9278 fc2737d5 2022-09-15 thomas case 'f':
9279 fc2737d5 2022-09-15 thomas case ' ':
9280 fc2737d5 2022-09-15 thomas if (s->eof) {
9281 fc2737d5 2022-09-15 thomas view->count = 0;
9282 fc2737d5 2022-09-15 thomas break;
9283 fc2737d5 2022-09-15 thomas }
9284 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9285 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9286 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9287 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9288 fc2737d5 2022-09-15 thomas if (feof(s->f))
9289 fc2737d5 2022-09-15 thomas s->eof = 1;
9290 fc2737d5 2022-09-15 thomas else
9291 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9292 fc2737d5 2022-09-15 thomas break;
9293 fc2737d5 2022-09-15 thomas }
9294 fc2737d5 2022-09-15 thomas }
9295 fc2737d5 2022-09-15 thomas free(line);
9296 fc2737d5 2022-09-15 thomas break;
9297 fc2737d5 2022-09-15 thomas default:
9298 fc2737d5 2022-09-15 thomas view->count = 0;
9299 fc2737d5 2022-09-15 thomas break;
9300 fc2737d5 2022-09-15 thomas }
9301 fc2737d5 2022-09-15 thomas
9302 fc2737d5 2022-09-15 thomas return err;
9303 fc2737d5 2022-09-15 thomas }
9304 fc2737d5 2022-09-15 thomas
9305 fc2737d5 2022-09-15 thomas static const struct got_error *
9306 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9307 fc2737d5 2022-09-15 thomas {
9308 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9309 fc2737d5 2022-09-15 thomas
9310 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9311 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9312 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9313 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9314 fc2737d5 2022-09-15 thomas
9315 fc2737d5 2022-09-15 thomas return NULL;
9316 fc2737d5 2022-09-15 thomas }
9317 fc2737d5 2022-09-15 thomas
9318 fc2737d5 2022-09-15 thomas static const struct got_error *
9319 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9320 fc2737d5 2022-09-15 thomas {
9321 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9322 fc2737d5 2022-09-15 thomas
9323 fc2737d5 2022-09-15 thomas
9324 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9325 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9326 fc2737d5 2022-09-15 thomas
9327 fc2737d5 2022-09-15 thomas wclear(view->window);
9328 fc2737d5 2022-09-15 thomas view->count = 0;
9329 fc2737d5 2022-09-15 thomas view->x = 0;
9330 fc2737d5 2022-09-15 thomas s->all = !s->all;
9331 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9332 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9333 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9334 fc2737d5 2022-09-15 thomas
9335 fc2737d5 2022-09-15 thomas return create_help(s);
9336 fc2737d5 2022-09-15 thomas }
9337 fc2737d5 2022-09-15 thomas
9338 fc2737d5 2022-09-15 thomas static const struct got_error *
9339 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9340 fc2737d5 2022-09-15 thomas {
9341 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9342 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9343 fc2737d5 2022-09-15 thomas
9344 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9345 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9346 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9347 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9348 fc2737d5 2022-09-15 thomas
9349 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9350 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9351 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9352 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9353 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9354 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9355 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9356 fc2737d5 2022-09-15 thomas
9357 fc2737d5 2022-09-15 thomas err = create_help(s);
9358 fc2737d5 2022-09-15 thomas return err;
9359 fc2737d5 2022-09-15 thomas }
9360 fc2737d5 2022-09-15 thomas
9361 fc2737d5 2022-09-15 thomas static const struct got_error *
9362 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9363 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9364 2a31b33b 2022-07-23 thomas {
9365 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9366 2a31b33b 2022-07-23 thomas
9367 2a31b33b 2022-07-23 thomas *new_view = NULL;
9368 2a31b33b 2022-07-23 thomas
9369 2a31b33b 2022-07-23 thomas switch (request) {
9370 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9371 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9372 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9373 2a31b33b 2022-07-23 thomas
9374 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9375 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9376 2a31b33b 2022-07-23 thomas view, s->repo);
9377 2a31b33b 2022-07-23 thomas } else
9378 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9379 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9380 2a31b33b 2022-07-23 thomas break;
9381 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9382 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9383 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9384 2a31b33b 2022-07-23 thomas
9385 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9386 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9387 2a31b33b 2022-07-23 thomas s->repo);
9388 2a31b33b 2022-07-23 thomas } else
9389 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9390 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9391 2a31b33b 2022-07-23 thomas break;
9392 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9393 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9394 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9395 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9396 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9397 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9398 2a31b33b 2022-07-23 thomas &view->state.tree);
9399 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9400 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9401 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9402 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9403 2a31b33b 2022-07-23 thomas else
9404 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9405 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9406 2a31b33b 2022-07-23 thomas break;
9407 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9408 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9409 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9410 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9411 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9412 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9413 2a31b33b 2022-07-23 thomas view->state.log.repo);
9414 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9415 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9416 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9417 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9418 2a31b33b 2022-07-23 thomas else
9419 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9420 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9421 2a31b33b 2022-07-23 thomas break;
9422 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9423 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9424 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9425 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9426 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9427 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9428 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9429 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9430 2a31b33b 2022-07-23 thomas else
9431 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9432 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9433 2a31b33b 2022-07-23 thomas if (err)
9434 2a31b33b 2022-07-23 thomas view_close(*new_view);
9435 2a31b33b 2022-07-23 thomas break;
9436 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9437 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9438 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9439 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9440 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9441 fc2737d5 2022-09-15 thomas if (err)
9442 fc2737d5 2022-09-15 thomas view_close(*new_view);
9443 fc2737d5 2022-09-15 thomas break;
9444 2a31b33b 2022-07-23 thomas default:
9445 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9446 2a31b33b 2022-07-23 thomas }
9447 2a31b33b 2022-07-23 thomas
9448 2a31b33b 2022-07-23 thomas return err;
9449 2a31b33b 2022-07-23 thomas }
9450 2a31b33b 2022-07-23 thomas
9451 a5d43cac 2022-07-01 thomas /*
9452 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9453 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9454 a5d43cac 2022-07-01 thomas */
9455 6458efa5 2020-11-24 stsp static void
9456 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9457 a5d43cac 2022-07-01 thomas {
9458 a5d43cac 2022-07-01 thomas switch (view->type) {
9459 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9460 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9461 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9462 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9463 a5d43cac 2022-07-01 thomas 1);
9464 a5d43cac 2022-07-01 thomas break;
9465 a5d43cac 2022-07-01 thomas }
9466 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9467 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9468 a5d43cac 2022-07-01 thomas else
9469 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9470 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9471 a5d43cac 2022-07-01 thomas break;
9472 a5d43cac 2022-07-01 thomas }
9473 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9474 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9475 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9476 a5d43cac 2022-07-01 thomas break;
9477 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9478 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9479 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9480 a5d43cac 2022-07-01 thomas break;
9481 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9482 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9483 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9484 a5d43cac 2022-07-01 thomas break;
9485 a5d43cac 2022-07-01 thomas default:
9486 a5d43cac 2022-07-01 thomas break;
9487 a5d43cac 2022-07-01 thomas }
9488 a5d43cac 2022-07-01 thomas
9489 a5d43cac 2022-07-01 thomas view->offset = 0;
9490 a5d43cac 2022-07-01 thomas }
9491 a5d43cac 2022-07-01 thomas
9492 a5d43cac 2022-07-01 thomas /*
9493 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9494 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9495 a5d43cac 2022-07-01 thomas */
9496 a5d43cac 2022-07-01 thomas static const struct got_error *
9497 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9498 a5d43cac 2022-07-01 thomas {
9499 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9500 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9501 a5d43cac 2022-07-01 thomas int *selected = NULL;
9502 a5d43cac 2022-07-01 thomas int header, offset;
9503 a5d43cac 2022-07-01 thomas
9504 a5d43cac 2022-07-01 thomas switch (view->type) {
9505 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9506 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9507 a5d43cac 2022-07-01 thomas header = 3;
9508 a5d43cac 2022-07-01 thomas scrolld = NULL;
9509 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9510 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9511 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9512 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9513 a5d43cac 2022-07-01 thomas view->offset = offset;
9514 a5d43cac 2022-07-01 thomas }
9515 a5d43cac 2022-07-01 thomas break;
9516 a5d43cac 2022-07-01 thomas }
9517 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9518 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9519 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9520 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9521 a5d43cac 2022-07-01 thomas selected = &s->selected;
9522 a5d43cac 2022-07-01 thomas break;
9523 a5d43cac 2022-07-01 thomas }
9524 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9525 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9526 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9527 a5d43cac 2022-07-01 thomas header = 3;
9528 a5d43cac 2022-07-01 thomas selected = &s->selected;
9529 a5d43cac 2022-07-01 thomas break;
9530 a5d43cac 2022-07-01 thomas }
9531 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9532 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9533 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9534 a5d43cac 2022-07-01 thomas header = 5;
9535 a5d43cac 2022-07-01 thomas selected = &s->selected;
9536 a5d43cac 2022-07-01 thomas break;
9537 a5d43cac 2022-07-01 thomas }
9538 a5d43cac 2022-07-01 thomas default:
9539 a5d43cac 2022-07-01 thomas selected = NULL;
9540 a5d43cac 2022-07-01 thomas scrolld = NULL;
9541 a5d43cac 2022-07-01 thomas header = 0;
9542 a5d43cac 2022-07-01 thomas break;
9543 a5d43cac 2022-07-01 thomas }
9544 a5d43cac 2022-07-01 thomas
9545 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9546 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9547 a5d43cac 2022-07-01 thomas view->offset = offset;
9548 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9549 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9550 a5d43cac 2022-07-01 thomas *selected -= offset;
9551 a5d43cac 2022-07-01 thomas }
9552 a5d43cac 2022-07-01 thomas }
9553 a5d43cac 2022-07-01 thomas
9554 a5d43cac 2022-07-01 thomas return err;
9555 a5d43cac 2022-07-01 thomas }
9556 a5d43cac 2022-07-01 thomas
9557 a5d43cac 2022-07-01 thomas static void
9558 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9559 ce5b7c56 2019-07-09 stsp {
9560 6059809a 2020-12-17 stsp size_t i;
9561 9f7d7167 2018-04-29 stsp
9562 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9563 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9564 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9565 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9566 ce5b7c56 2019-07-09 stsp }
9567 6879ba42 2020-10-01 naddy fputc('\n', fp);
9568 ce5b7c56 2019-07-09 stsp }
9569 ce5b7c56 2019-07-09 stsp
9570 4ed7e80c 2018-05-20 stsp __dead static void
9571 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9572 9f7d7167 2018-04-29 stsp {
9573 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9574 6879ba42 2020-10-01 naddy
9575 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9576 6879ba42 2020-10-01 naddy getprogname());
9577 6879ba42 2020-10-01 naddy if (hflag) {
9578 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9579 6879ba42 2020-10-01 naddy list_commands(fp);
9580 ee85c5e8 2020-02-29 stsp }
9581 6879ba42 2020-10-01 naddy exit(status);
9582 9f7d7167 2018-04-29 stsp }
9583 9f7d7167 2018-04-29 stsp
9584 c2301be8 2018-04-30 stsp static char **
9585 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9586 c2301be8 2018-04-30 stsp {
9587 ee85c5e8 2020-02-29 stsp va_list ap;
9588 c2301be8 2018-04-30 stsp char **argv;
9589 ee85c5e8 2020-02-29 stsp int i;
9590 c2301be8 2018-04-30 stsp
9591 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9592 ee85c5e8 2020-02-29 stsp
9593 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9594 c2301be8 2018-04-30 stsp if (argv == NULL)
9595 c2301be8 2018-04-30 stsp err(1, "calloc");
9596 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9597 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9598 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9599 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9600 c2301be8 2018-04-30 stsp }
9601 c2301be8 2018-04-30 stsp
9602 ee85c5e8 2020-02-29 stsp va_end(ap);
9603 c2301be8 2018-04-30 stsp return argv;
9604 ee85c5e8 2020-02-29 stsp }
9605 ee85c5e8 2020-02-29 stsp
9606 ee85c5e8 2020-02-29 stsp /*
9607 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9608 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9609 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9610 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9611 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9612 ee85c5e8 2020-02-29 stsp */
9613 ee85c5e8 2020-02-29 stsp static const struct got_error *
9614 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9615 ee85c5e8 2020-02-29 stsp {
9616 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9617 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9618 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9619 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9620 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9621 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9622 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9623 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9624 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9625 84de9106 2020-12-26 stsp
9626 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9627 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9628 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9629 ee85c5e8 2020-02-29 stsp
9630 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9631 7cd52833 2022-06-23 thomas if (error != NULL)
9632 7cd52833 2022-06-23 thomas goto done;
9633 7cd52833 2022-06-23 thomas
9634 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9635 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9636 ee85c5e8 2020-02-29 stsp goto done;
9637 ee85c5e8 2020-02-29 stsp
9638 ee85c5e8 2020-02-29 stsp if (worktree)
9639 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9640 ee85c5e8 2020-02-29 stsp else
9641 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9642 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9643 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9644 ee85c5e8 2020-02-29 stsp goto done;
9645 ee85c5e8 2020-02-29 stsp }
9646 ee85c5e8 2020-02-29 stsp
9647 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9648 ee85c5e8 2020-02-29 stsp if (error != NULL)
9649 ee85c5e8 2020-02-29 stsp goto done;
9650 ee85c5e8 2020-02-29 stsp
9651 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9652 ee85c5e8 2020-02-29 stsp repo, worktree);
9653 ee85c5e8 2020-02-29 stsp if (error)
9654 ee85c5e8 2020-02-29 stsp goto done;
9655 ee85c5e8 2020-02-29 stsp
9656 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9657 84de9106 2020-12-26 stsp if (error)
9658 84de9106 2020-12-26 stsp goto done;
9659 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9660 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9661 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9662 ee85c5e8 2020-02-29 stsp if (error)
9663 ee85c5e8 2020-02-29 stsp goto done;
9664 ee85c5e8 2020-02-29 stsp
9665 ee85c5e8 2020-02-29 stsp if (worktree) {
9666 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9667 ee85c5e8 2020-02-29 stsp worktree = NULL;
9668 ee85c5e8 2020-02-29 stsp }
9669 ee85c5e8 2020-02-29 stsp
9670 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9671 945f9229 2022-04-16 thomas if (error)
9672 945f9229 2022-04-16 thomas goto done;
9673 945f9229 2022-04-16 thomas
9674 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9675 ee85c5e8 2020-02-29 stsp if (error) {
9676 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9677 ee85c5e8 2020-02-29 stsp goto done;
9678 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9679 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9680 6879ba42 2020-10-01 naddy usage(1, 1);
9681 ee85c5e8 2020-02-29 stsp /* not reached */
9682 ee85c5e8 2020-02-29 stsp }
9683 ee85c5e8 2020-02-29 stsp
9684 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9685 ee85c5e8 2020-02-29 stsp if (error)
9686 ee85c5e8 2020-02-29 stsp goto done;
9687 ee85c5e8 2020-02-29 stsp
9688 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9689 ee85c5e8 2020-02-29 stsp argc = 4;
9690 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9691 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9692 ee85c5e8 2020-02-29 stsp done:
9693 1d0f4054 2021-06-17 stsp if (repo) {
9694 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9695 1d0f4054 2021-06-17 stsp if (error == NULL)
9696 1d0f4054 2021-06-17 stsp error = close_err;
9697 1d0f4054 2021-06-17 stsp }
9698 945f9229 2022-04-16 thomas if (commit)
9699 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9700 ee85c5e8 2020-02-29 stsp if (worktree)
9701 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9702 7cd52833 2022-06-23 thomas if (pack_fds) {
9703 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9704 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9705 7cd52833 2022-06-23 thomas if (error == NULL)
9706 7cd52833 2022-06-23 thomas error = pack_err;
9707 7cd52833 2022-06-23 thomas }
9708 ee85c5e8 2020-02-29 stsp free(id);
9709 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9710 ee85c5e8 2020-02-29 stsp free(commit_id);
9711 ee85c5e8 2020-02-29 stsp free(cwd);
9712 ee85c5e8 2020-02-29 stsp free(repo_path);
9713 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9714 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9715 ee85c5e8 2020-02-29 stsp int i;
9716 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9717 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9718 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9719 ee85c5e8 2020-02-29 stsp }
9720 87670572 2020-12-26 naddy tog_free_refs();
9721 ee85c5e8 2020-02-29 stsp return error;
9722 c2301be8 2018-04-30 stsp }
9723 c2301be8 2018-04-30 stsp
9724 9f7d7167 2018-04-29 stsp int
9725 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9726 9f7d7167 2018-04-29 stsp {
9727 1d98034b 2023-04-14 thomas const struct got_error *io_err, *error = NULL;
9728 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9729 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9730 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9731 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9732 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9733 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9734 83cd27f8 2020-01-13 stsp };
9735 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9736 557d3365 2023-04-14 thomas const char *test_script_path;
9737 a0abeae5 2023-02-17 thomas
9738 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
9739 a0abeae5 2023-02-17 thomas
9740 557d3365 2023-04-14 thomas /*
9741 557d3365 2023-04-14 thomas * Test mode init must happen before pledge() because "tty" will
9742 557d3365 2023-04-14 thomas * not allow TTY-related ioctls to occur via regular files.
9743 557d3365 2023-04-14 thomas */
9744 557d3365 2023-04-14 thomas test_script_path = getenv("GOT_TOG_TEST");
9745 557d3365 2023-04-14 thomas if (test_script_path != NULL) {
9746 557d3365 2023-04-14 thomas error = init_mock_term(test_script_path);
9747 557d3365 2023-04-14 thomas if (error) {
9748 557d3365 2023-04-14 thomas fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9749 557d3365 2023-04-14 thomas return 1;
9750 557d3365 2023-04-14 thomas }
9751 557d3365 2023-04-14 thomas }
9752 557d3365 2023-04-14 thomas
9753 557d3365 2023-04-14 thomas #if !defined(PROFILE)
9754 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9755 a0abeae5 2023-02-17 thomas NULL) == -1)
9756 a0abeae5 2023-02-17 thomas err(1, "pledge");
9757 a0abeae5 2023-02-17 thomas #endif
9758 9f7d7167 2018-04-29 stsp
9759 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9760 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9761 cacf1690 2022-09-06 thomas
9762 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9763 9f7d7167 2018-04-29 stsp switch (ch) {
9764 9f7d7167 2018-04-29 stsp case 'h':
9765 9f7d7167 2018-04-29 stsp hflag = 1;
9766 9f7d7167 2018-04-29 stsp break;
9767 53ccebc2 2019-07-30 stsp case 'V':
9768 53ccebc2 2019-07-30 stsp Vflag = 1;
9769 53ccebc2 2019-07-30 stsp break;
9770 9f7d7167 2018-04-29 stsp default:
9771 6879ba42 2020-10-01 naddy usage(hflag, 1);
9772 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9773 9f7d7167 2018-04-29 stsp }
9774 9f7d7167 2018-04-29 stsp }
9775 9f7d7167 2018-04-29 stsp
9776 9f7d7167 2018-04-29 stsp argc -= optind;
9777 9f7d7167 2018-04-29 stsp argv += optind;
9778 9814e6a3 2020-09-27 naddy optind = 1;
9779 c2301be8 2018-04-30 stsp optreset = 1;
9780 9f7d7167 2018-04-29 stsp
9781 53ccebc2 2019-07-30 stsp if (Vflag) {
9782 53ccebc2 2019-07-30 stsp got_version_print_str();
9783 6879ba42 2020-10-01 naddy return 0;
9784 53ccebc2 2019-07-30 stsp }
9785 4010e238 2020-12-04 stsp
9786 c2301be8 2018-04-30 stsp if (argc == 0) {
9787 f29d3e89 2018-06-23 stsp if (hflag)
9788 6879ba42 2020-10-01 naddy usage(hflag, 0);
9789 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9790 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9791 c2301be8 2018-04-30 stsp argc = 1;
9792 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9793 c2301be8 2018-04-30 stsp } else {
9794 6059809a 2020-12-17 stsp size_t i;
9795 9f7d7167 2018-04-29 stsp
9796 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9797 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9798 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9799 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9800 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9801 9f7d7167 2018-04-29 stsp break;
9802 9f7d7167 2018-04-29 stsp }
9803 9f7d7167 2018-04-29 stsp }
9804 ee85c5e8 2020-02-29 stsp }
9805 3642c4c6 2019-07-09 stsp
9806 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9807 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9808 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9809 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9810 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9811 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9812 adf4c9e0 2022-07-03 thomas }
9813 adf4c9e0 2022-07-03 thomas
9814 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9815 ee85c5e8 2020-02-29 stsp if (argc != 1)
9816 6879ba42 2020-10-01 naddy usage(0, 1);
9817 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9818 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9819 ee85c5e8 2020-02-29 stsp } else {
9820 ee85c5e8 2020-02-29 stsp if (hflag)
9821 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9822 ee85c5e8 2020-02-29 stsp else
9823 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9824 9f7d7167 2018-04-29 stsp }
9825 9f7d7167 2018-04-29 stsp
9826 1d98034b 2023-04-14 thomas if (using_mock_io) {
9827 1d98034b 2023-04-14 thomas io_err = tog_io_close();
9828 1d98034b 2023-04-14 thomas if (error == NULL)
9829 1d98034b 2023-04-14 thomas error = io_err;
9830 1d98034b 2023-04-14 thomas }
9831 9f7d7167 2018-04-29 stsp endwin();
9832 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9833 a2f4a359 2020-02-28 stsp int i;
9834 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9835 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9836 a2f4a359 2020-02-28 stsp free(cmd_argv);
9837 a2f4a359 2020-02-28 stsp }
9838 a2f4a359 2020-02-28 stsp
9839 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9840 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9841 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9842 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9843 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9844 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9845 9f7d7167 2018-04-29 stsp return 0;
9846 9f7d7167 2018-04-29 stsp }