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 b85a3496 2023-04-14 thomas };
624 b85a3496 2023-04-14 thomas
625 b85a3496 2023-04-14 thomas #define TOG_SCREEN_DUMP "SCREENDUMP"
626 b85a3496 2023-04-14 thomas #define TOG_SCREEN_DUMP_LEN (sizeof(TOG_SCREEN_DUMP) - 1)
627 b85a3496 2023-04-14 thomas #define TOG_KEY_SCRDUMP SHRT_MIN
628 b85a3496 2023-04-14 thomas
629 669b5ffa 2018-10-07 stsp /*
630 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
631 669b5ffa 2018-10-07 stsp *
632 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
633 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
634 669b5ffa 2018-10-07 stsp * there is enough screen estate.
635 669b5ffa 2018-10-07 stsp *
636 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
637 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
638 669b5ffa 2018-10-07 stsp *
639 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
640 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
641 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
642 669b5ffa 2018-10-07 stsp *
643 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
644 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
645 669b5ffa 2018-10-07 stsp */
646 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
647 669b5ffa 2018-10-07 stsp
648 cc3c9aac 2018-08-01 stsp struct tog_view {
649 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
650 26ed57b2 2018-05-19 stsp WINDOW *window;
651 26ed57b2 2018-05-19 stsp PANEL *panel;
652 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
653 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
654 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
655 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
656 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
657 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
658 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
659 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
660 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
661 9970f7fc 2020-12-03 stsp int dying;
662 669b5ffa 2018-10-07 stsp struct tog_view *parent;
663 669b5ffa 2018-10-07 stsp struct tog_view *child;
664 5dc9f4bc 2018-08-04 stsp
665 e78dc838 2020-12-04 stsp /*
666 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
667 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
668 e78dc838 2020-12-04 stsp * between parent and child.
669 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
670 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
671 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
672 e78dc838 2020-12-04 stsp * situations.
673 e78dc838 2020-12-04 stsp */
674 e78dc838 2020-12-04 stsp int focus_child;
675 e78dc838 2020-12-04 stsp
676 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
677 5dc9f4bc 2018-08-04 stsp /* type-specific state */
678 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
679 5dc9f4bc 2018-08-04 stsp union {
680 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
681 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
682 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
683 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
684 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
685 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
686 5dc9f4bc 2018-08-04 stsp } state;
687 e5a0f69f 2018-08-18 stsp
688 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
689 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
690 e78dc838 2020-12-04 stsp struct tog_view *, int);
691 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
692 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
693 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
694 60493ae3 2019-06-20 stsp
695 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
696 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
697 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
698 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
699 c0c4acc8 2021-01-24 stsp int search_started;
700 60493ae3 2019-06-20 stsp int searching;
701 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
702 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
703 60493ae3 2019-06-20 stsp int search_next_done;
704 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
705 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
706 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
707 1803e47f 2019-06-22 stsp regex_t regex;
708 41605754 2020-11-12 stsp regmatch_t regmatch;
709 f2d06bef 2023-01-23 thomas const char *action;
710 cc3c9aac 2018-08-01 stsp };
711 cd0acaa7 2018-05-20 stsp
712 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
713 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
714 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
715 78756c87 2020-11-24 stsp struct got_repository *);
716 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
717 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
718 e78dc838 2020-12-04 stsp struct tog_view *, int);
719 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
720 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
721 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
722 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
723 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
724 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
725 e5a0f69f 2018-08-18 stsp
726 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
727 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
728 78756c87 2020-11-24 stsp const char *, const char *, int);
729 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
730 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
731 e78dc838 2020-12-04 stsp struct tog_view *, int);
732 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
733 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
734 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
735 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
736 e5a0f69f 2018-08-18 stsp
737 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
738 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
739 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
740 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
741 e78dc838 2020-12-04 stsp struct tog_view *, int);
742 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
743 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
744 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
745 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
746 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
747 e5a0f69f 2018-08-18 stsp
748 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
749 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
750 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
751 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
752 e78dc838 2020-12-04 stsp struct tog_view *, int);
753 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
754 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
755 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
756 6458efa5 2020-11-24 stsp
757 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
758 6458efa5 2020-11-24 stsp struct got_repository *);
759 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
760 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
761 e78dc838 2020-12-04 stsp struct tog_view *, int);
762 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
763 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
764 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
765 2a7d3cd7 2022-09-17 thomas
766 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
767 2a7d3cd7 2022-09-17 thomas struct tog_view *);
768 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
769 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
770 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
771 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
772 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
773 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
774 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
775 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
776 25791caa 2018-10-24 stsp
777 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
778 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
779 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
780 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
781 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
782 25791caa 2018-10-24 stsp
783 25791caa 2018-10-24 stsp static void
784 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
785 25791caa 2018-10-24 stsp {
786 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
787 83baff54 2019-08-12 stsp }
788 83baff54 2019-08-12 stsp
789 83baff54 2019-08-12 stsp static void
790 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
791 83baff54 2019-08-12 stsp {
792 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
793 25791caa 2018-10-24 stsp }
794 26ed57b2 2018-05-19 stsp
795 61266923 2020-01-14 stsp static void
796 61266923 2020-01-14 stsp tog_sigcont(int signo)
797 61266923 2020-01-14 stsp {
798 61266923 2020-01-14 stsp tog_sigcont_received = 1;
799 61266923 2020-01-14 stsp }
800 61266923 2020-01-14 stsp
801 296152d1 2022-05-31 thomas static void
802 296152d1 2022-05-31 thomas tog_sigint(int signo)
803 296152d1 2022-05-31 thomas {
804 296152d1 2022-05-31 thomas tog_sigint_received = 1;
805 296152d1 2022-05-31 thomas }
806 296152d1 2022-05-31 thomas
807 296152d1 2022-05-31 thomas static void
808 296152d1 2022-05-31 thomas tog_sigterm(int signo)
809 296152d1 2022-05-31 thomas {
810 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
811 296152d1 2022-05-31 thomas }
812 296152d1 2022-05-31 thomas
813 296152d1 2022-05-31 thomas static int
814 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
815 296152d1 2022-05-31 thomas {
816 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
817 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
818 296152d1 2022-05-31 thomas }
819 296152d1 2022-05-31 thomas
820 e5a0f69f 2018-08-18 stsp static const struct got_error *
821 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
822 ea5e7bb5 2018-08-01 stsp {
823 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
824 e5a0f69f 2018-08-18 stsp
825 669b5ffa 2018-10-07 stsp if (view->child) {
826 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
827 669b5ffa 2018-10-07 stsp view->child = NULL;
828 669b5ffa 2018-10-07 stsp }
829 e5a0f69f 2018-08-18 stsp if (view->close)
830 e5a0f69f 2018-08-18 stsp err = view->close(view);
831 ea5e7bb5 2018-08-01 stsp if (view->panel)
832 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
833 ea5e7bb5 2018-08-01 stsp if (view->window)
834 ea5e7bb5 2018-08-01 stsp delwin(view->window);
835 ea5e7bb5 2018-08-01 stsp free(view);
836 f2d749db 2022-07-12 thomas return err ? err : child_err;
837 ea5e7bb5 2018-08-01 stsp }
838 ea5e7bb5 2018-08-01 stsp
839 ea5e7bb5 2018-08-01 stsp static struct tog_view *
840 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
841 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
842 ea5e7bb5 2018-08-01 stsp {
843 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
844 ea5e7bb5 2018-08-01 stsp
845 ea5e7bb5 2018-08-01 stsp if (view == NULL)
846 ea5e7bb5 2018-08-01 stsp return NULL;
847 ea5e7bb5 2018-08-01 stsp
848 d6b05b5b 2018-08-04 stsp view->type = type;
849 f7d12f7e 2018-08-01 stsp view->lines = LINES;
850 f7d12f7e 2018-08-01 stsp view->cols = COLS;
851 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
852 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
853 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
854 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
855 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
856 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
857 96a765a8 2018-08-04 stsp view_close(view);
858 ea5e7bb5 2018-08-01 stsp return NULL;
859 ea5e7bb5 2018-08-01 stsp }
860 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
861 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
862 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
863 96a765a8 2018-08-04 stsp view_close(view);
864 ea5e7bb5 2018-08-01 stsp return NULL;
865 ea5e7bb5 2018-08-01 stsp }
866 ea5e7bb5 2018-08-01 stsp
867 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
868 ea5e7bb5 2018-08-01 stsp return view;
869 cdf1ee82 2018-08-01 stsp }
870 cdf1ee82 2018-08-01 stsp
871 0cf4efb1 2018-09-29 stsp static int
872 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
873 0cf4efb1 2018-09-29 stsp {
874 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
875 0cf4efb1 2018-09-29 stsp return 0;
876 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
877 5c60c32a 2018-10-18 stsp }
878 5c60c32a 2018-10-18 stsp
879 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
880 a5d43cac 2022-07-01 thomas static int
881 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
882 a5d43cac 2022-07-01 thomas {
883 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
884 a5d43cac 2022-07-01 thomas }
885 a5d43cac 2022-07-01 thomas
886 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
887 5c60c32a 2018-10-18 stsp
888 5c60c32a 2018-10-18 stsp static const struct got_error *
889 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
890 5c60c32a 2018-10-18 stsp {
891 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
892 5c60c32a 2018-10-18 stsp
893 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
894 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
895 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
896 53d2bdd3 2022-07-10 thomas else
897 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
898 a5d43cac 2022-07-01 thomas view->begin_x = 0;
899 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
900 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
901 53d2bdd3 2022-07-10 thomas view->cols > 119)
902 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
903 53d2bdd3 2022-07-10 thomas else
904 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
905 a5d43cac 2022-07-01 thomas view->begin_y = 0;
906 a5d43cac 2022-07-01 thomas }
907 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
908 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
909 5c60c32a 2018-10-18 stsp view->lines = LINES;
910 5c60c32a 2018-10-18 stsp view->cols = COLS;
911 5c60c32a 2018-10-18 stsp err = view_resize(view);
912 5c60c32a 2018-10-18 stsp if (err)
913 5c60c32a 2018-10-18 stsp return err;
914 5c60c32a 2018-10-18 stsp
915 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
916 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
917 a5d43cac 2022-07-01 thomas
918 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
919 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
920 5c60c32a 2018-10-18 stsp
921 5c60c32a 2018-10-18 stsp return NULL;
922 5c60c32a 2018-10-18 stsp }
923 5c60c32a 2018-10-18 stsp
924 5c60c32a 2018-10-18 stsp static const struct got_error *
925 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
926 5c60c32a 2018-10-18 stsp {
927 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
928 5c60c32a 2018-10-18 stsp
929 5c60c32a 2018-10-18 stsp view->begin_x = 0;
930 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
931 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
932 5c60c32a 2018-10-18 stsp view->ncols = COLS;
933 5c60c32a 2018-10-18 stsp view->lines = LINES;
934 5c60c32a 2018-10-18 stsp view->cols = COLS;
935 5c60c32a 2018-10-18 stsp err = view_resize(view);
936 5c60c32a 2018-10-18 stsp if (err)
937 5c60c32a 2018-10-18 stsp return err;
938 5c60c32a 2018-10-18 stsp
939 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
940 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
941 5c60c32a 2018-10-18 stsp
942 5c60c32a 2018-10-18 stsp return NULL;
943 0cf4efb1 2018-09-29 stsp }
944 0cf4efb1 2018-09-29 stsp
945 5c60c32a 2018-10-18 stsp static int
946 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
947 5c60c32a 2018-10-18 stsp {
948 5c60c32a 2018-10-18 stsp return view->parent == NULL;
949 5c60c32a 2018-10-18 stsp }
950 5c60c32a 2018-10-18 stsp
951 b65b3ea0 2022-06-23 thomas static int
952 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
953 b65b3ea0 2022-06-23 thomas {
954 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
955 e44940c3 2022-07-01 thomas }
956 e44940c3 2022-07-01 thomas
957 e44940c3 2022-07-01 thomas static int
958 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
959 e44940c3 2022-07-01 thomas {
960 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
961 b65b3ea0 2022-06-23 thomas }
962 b65b3ea0 2022-06-23 thomas
963 444d5325 2022-07-03 thomas static int
964 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
965 444d5325 2022-07-03 thomas {
966 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
967 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
968 444d5325 2022-07-03 thomas }
969 444d5325 2022-07-03 thomas
970 a5d43cac 2022-07-01 thomas static void
971 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
972 a5d43cac 2022-07-01 thomas {
973 a5d43cac 2022-07-01 thomas PANEL *panel;
974 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
975 b65b3ea0 2022-06-23 thomas
976 a5d43cac 2022-07-01 thomas if (view->parent)
977 a5d43cac 2022-07-01 thomas return view_border(view->parent);
978 a5d43cac 2022-07-01 thomas
979 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
980 a5d43cac 2022-07-01 thomas if (panel == NULL)
981 a5d43cac 2022-07-01 thomas return;
982 a5d43cac 2022-07-01 thomas
983 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
984 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
985 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
986 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
987 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
988 a5d43cac 2022-07-01 thomas else
989 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
990 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
991 a5d43cac 2022-07-01 thomas }
992 a5d43cac 2022-07-01 thomas
993 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
994 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
995 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
996 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
997 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
998 a5d43cac 2022-07-01 thomas
999 4d8c2215 2018-08-19 stsp static const struct got_error *
1000 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
1001 f7d12f7e 2018-08-01 stsp {
1002 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
1003 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
1004 f7d12f7e 2018-08-01 stsp
1005 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
1006 a5d43cac 2022-07-01 thomas
1007 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
1008 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
1009 0cf4efb1 2018-09-29 stsp else
1010 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1011 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1012 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1013 0cf4efb1 2018-09-29 stsp else
1014 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1015 6d0fee91 2018-08-01 stsp
1016 4918811f 2022-07-01 thomas if (view->child) {
1017 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1018 a5d43cac 2022-07-01 thomas
1019 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1020 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1021 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1022 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1023 40236d76 2022-06-23 thomas ncols = COLS;
1024 40236d76 2022-06-23 thomas
1025 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1026 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1027 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1028 5c60c32a 2018-10-18 stsp else
1029 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1030 5c60c32a 2018-10-18 stsp } else {
1031 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1032 40236d76 2022-06-23 thomas
1033 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1034 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1035 a5d43cac 2022-07-01 thomas }
1036 a5d43cac 2022-07-01 thomas /*
1037 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1038 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1039 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1040 a5d43cac 2022-07-01 thomas */
1041 a5d43cac 2022-07-01 thomas if (hs) {
1042 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1043 a5d43cac 2022-07-01 thomas if (err)
1044 a5d43cac 2022-07-01 thomas return err;
1045 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1046 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1047 a5d43cac 2022-07-01 thomas if (err)
1048 a5d43cac 2022-07-01 thomas return err;
1049 a5d43cac 2022-07-01 thomas }
1050 a5d43cac 2022-07-01 thomas view_border(view);
1051 a5d43cac 2022-07-01 thomas update_panels();
1052 a5d43cac 2022-07-01 thomas doupdate();
1053 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1054 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1055 a5d43cac 2022-07-01 thomas }
1056 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1057 40236d76 2022-06-23 thomas ncols = COLS;
1058 669b5ffa 2018-10-07 stsp
1059 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1060 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1061 ea0bff04 2022-07-19 thomas if (err)
1062 ea0bff04 2022-07-19 thomas return err;
1063 ea0bff04 2022-07-19 thomas }
1064 ea0bff04 2022-07-19 thomas
1065 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1066 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1067 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1068 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1069 40236d76 2022-06-23 thomas wclear(view->window);
1070 40236d76 2022-06-23 thomas
1071 40236d76 2022-06-23 thomas view->nlines = nlines;
1072 40236d76 2022-06-23 thomas view->ncols = ncols;
1073 40236d76 2022-06-23 thomas view->lines = LINES;
1074 40236d76 2022-06-23 thomas view->cols = COLS;
1075 40236d76 2022-06-23 thomas
1076 5c60c32a 2018-10-18 stsp return NULL;
1077 ea0bff04 2022-07-19 thomas }
1078 ea0bff04 2022-07-19 thomas
1079 ea0bff04 2022-07-19 thomas static const struct got_error *
1080 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1081 ea0bff04 2022-07-19 thomas {
1082 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1083 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1084 3a0139e8 2022-07-22 thomas int n = 0;
1085 ea0bff04 2022-07-19 thomas
1086 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1087 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1088 3a0139e8 2022-07-22 thomas
1089 ea0bff04 2022-07-19 thomas /*
1090 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1091 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1092 ea0bff04 2022-07-19 thomas */
1093 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1094 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1095 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1096 ea0bff04 2022-07-19 thomas }
1097 ea0bff04 2022-07-19 thomas
1098 ea0bff04 2022-07-19 thomas return err;
1099 97cb21cd 2022-07-12 thomas }
1100 97cb21cd 2022-07-12 thomas
1101 97cb21cd 2022-07-12 thomas static void
1102 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1103 97cb21cd 2022-07-12 thomas {
1104 97cb21cd 2022-07-12 thomas if (n == 0)
1105 97cb21cd 2022-07-12 thomas return;
1106 97cb21cd 2022-07-12 thomas
1107 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1108 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1109 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1110 97cb21cd 2022-07-12 thomas else
1111 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1112 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1113 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1114 97cb21cd 2022-07-12 thomas view->offset -= n;
1115 97cb21cd 2022-07-12 thomas else
1116 97cb21cd 2022-07-12 thomas view->offset = 0;
1117 97cb21cd 2022-07-12 thomas }
1118 53d2bdd3 2022-07-10 thomas }
1119 53d2bdd3 2022-07-10 thomas
1120 53d2bdd3 2022-07-10 thomas static const struct got_error *
1121 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1122 53d2bdd3 2022-07-10 thomas {
1123 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1124 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1125 53d2bdd3 2022-07-10 thomas
1126 53d2bdd3 2022-07-10 thomas if (view->parent)
1127 53d2bdd3 2022-07-10 thomas v = view->parent;
1128 53d2bdd3 2022-07-10 thomas else
1129 53d2bdd3 2022-07-10 thomas v = view;
1130 53d2bdd3 2022-07-10 thomas
1131 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1132 53d2bdd3 2022-07-10 thomas return NULL;
1133 53d2bdd3 2022-07-10 thomas
1134 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1135 53d2bdd3 2022-07-10 thomas
1136 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1137 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1138 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1139 53d2bdd3 2022-07-10 thomas if (view->parent)
1140 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1141 53d2bdd3 2022-07-10 thomas else
1142 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1143 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1144 53d2bdd3 2022-07-10 thomas view->count = 0;
1145 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1146 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1147 53d2bdd3 2022-07-10 thomas view->count = 0;
1148 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1149 53d2bdd3 2022-07-10 thomas }
1150 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1151 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1152 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1153 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1154 53d2bdd3 2022-07-10 thomas if (err)
1155 53d2bdd3 2022-07-10 thomas return err;
1156 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1157 53d2bdd3 2022-07-10 thomas } else {
1158 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1159 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1160 53d2bdd3 2022-07-10 thomas if (view->parent)
1161 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1162 53d2bdd3 2022-07-10 thomas else
1163 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1164 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1165 53d2bdd3 2022-07-10 thomas view->count = 0;
1166 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1167 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1168 53d2bdd3 2022-07-10 thomas view->count = 0;
1169 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1170 53d2bdd3 2022-07-10 thomas }
1171 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1172 53d2bdd3 2022-07-10 thomas }
1173 53d2bdd3 2022-07-10 thomas
1174 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1175 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1176 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1177 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1178 53d2bdd3 2022-07-10 thomas
1179 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1180 53d2bdd3 2022-07-10 thomas if (err)
1181 53d2bdd3 2022-07-10 thomas return err;
1182 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1183 53d2bdd3 2022-07-10 thomas if (err)
1184 53d2bdd3 2022-07-10 thomas return err;
1185 53d2bdd3 2022-07-10 thomas
1186 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1187 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1188 53d2bdd3 2022-07-10 thomas if (err)
1189 53d2bdd3 2022-07-10 thomas return err;
1190 53d2bdd3 2022-07-10 thomas }
1191 53d2bdd3 2022-07-10 thomas
1192 3a0139e8 2022-07-22 thomas if (v->resize)
1193 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1194 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1195 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1196 53d2bdd3 2022-07-10 thomas
1197 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1198 53d2bdd3 2022-07-10 thomas
1199 53d2bdd3 2022-07-10 thomas return err;
1200 53d2bdd3 2022-07-10 thomas }
1201 53d2bdd3 2022-07-10 thomas
1202 53d2bdd3 2022-07-10 thomas static void
1203 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1204 53d2bdd3 2022-07-10 thomas {
1205 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1206 53d2bdd3 2022-07-10 thomas
1207 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1208 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1209 669b5ffa 2018-10-07 stsp }
1210 669b5ffa 2018-10-07 stsp
1211 669b5ffa 2018-10-07 stsp static const struct got_error *
1212 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1213 669b5ffa 2018-10-07 stsp {
1214 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1215 669b5ffa 2018-10-07 stsp
1216 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1217 669b5ffa 2018-10-07 stsp return NULL;
1218 669b5ffa 2018-10-07 stsp
1219 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1220 669b5ffa 2018-10-07 stsp view->child = NULL;
1221 669b5ffa 2018-10-07 stsp return err;
1222 669b5ffa 2018-10-07 stsp }
1223 669b5ffa 2018-10-07 stsp
1224 40236d76 2022-06-23 thomas static const struct got_error *
1225 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1226 669b5ffa 2018-10-07 stsp {
1227 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1228 53d2bdd3 2022-07-10 thomas
1229 669b5ffa 2018-10-07 stsp view->child = child;
1230 669b5ffa 2018-10-07 stsp child->parent = view;
1231 40236d76 2022-06-23 thomas
1232 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1233 53d2bdd3 2022-07-10 thomas if (err)
1234 53d2bdd3 2022-07-10 thomas return err;
1235 53d2bdd3 2022-07-10 thomas
1236 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1237 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1238 53d2bdd3 2022-07-10 thomas
1239 53d2bdd3 2022-07-10 thomas return err;
1240 bfddd0d9 2018-09-29 stsp }
1241 2a31b33b 2022-07-23 thomas
1242 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1243 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1244 2a31b33b 2022-07-23 thomas
1245 2a31b33b 2022-07-23 thomas static const struct got_error *
1246 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1247 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1248 2a31b33b 2022-07-23 thomas {
1249 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1250 2a31b33b 2022-07-23 thomas const struct got_error *err;
1251 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1252 2a31b33b 2022-07-23 thomas
1253 2a31b33b 2022-07-23 thomas *requested = NULL;
1254 2a31b33b 2022-07-23 thomas
1255 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1256 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1257 bfddd0d9 2018-09-29 stsp
1258 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1259 2a31b33b 2022-07-23 thomas if (err)
1260 2a31b33b 2022-07-23 thomas return err;
1261 2a31b33b 2022-07-23 thomas
1262 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1263 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1264 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1265 2a31b33b 2022-07-23 thomas if (err)
1266 2a31b33b 2022-07-23 thomas return err;
1267 2a31b33b 2022-07-23 thomas }
1268 2a31b33b 2022-07-23 thomas
1269 2a31b33b 2022-07-23 thomas view->focussed = 0;
1270 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1271 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1272 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1273 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1274 2a31b33b 2022-07-23 thomas
1275 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1276 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1277 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1278 2a31b33b 2022-07-23 thomas if (err)
1279 2a31b33b 2022-07-23 thomas return err;
1280 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1281 2a31b33b 2022-07-23 thomas if (err)
1282 2a31b33b 2022-07-23 thomas return err;
1283 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1284 2a31b33b 2022-07-23 thomas } else
1285 2a31b33b 2022-07-23 thomas *requested = new_view;
1286 2a31b33b 2022-07-23 thomas
1287 2a31b33b 2022-07-23 thomas return NULL;
1288 2a31b33b 2022-07-23 thomas }
1289 2a31b33b 2022-07-23 thomas
1290 34bc9ec9 2019-02-22 stsp static void
1291 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1292 25791caa 2018-10-24 stsp {
1293 25791caa 2018-10-24 stsp int cols, lines;
1294 25791caa 2018-10-24 stsp struct winsize size;
1295 25791caa 2018-10-24 stsp
1296 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1297 25791caa 2018-10-24 stsp cols = 80; /* Default */
1298 25791caa 2018-10-24 stsp lines = 24;
1299 25791caa 2018-10-24 stsp } else {
1300 25791caa 2018-10-24 stsp cols = size.ws_col;
1301 25791caa 2018-10-24 stsp lines = size.ws_row;
1302 25791caa 2018-10-24 stsp }
1303 25791caa 2018-10-24 stsp resize_term(lines, cols);
1304 2b49a8ae 2019-06-22 stsp }
1305 2b49a8ae 2019-06-22 stsp
1306 2b49a8ae 2019-06-22 stsp static const struct got_error *
1307 f54d892e 2023-02-17 thomas view_search_start(struct tog_view *view, int fast_refresh)
1308 2b49a8ae 2019-06-22 stsp {
1309 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1310 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1311 2b49a8ae 2019-06-22 stsp char pattern[1024];
1312 2b49a8ae 2019-06-22 stsp int ret;
1313 c0c4acc8 2021-01-24 stsp
1314 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1315 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1316 c0c4acc8 2021-01-24 stsp view->searching = 0;
1317 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1318 c0c4acc8 2021-01-24 stsp }
1319 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1320 2b49a8ae 2019-06-22 stsp
1321 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1322 2b49a8ae 2019-06-22 stsp return NULL;
1323 2b49a8ae 2019-06-22 stsp
1324 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1325 a5d43cac 2022-07-01 thomas v = view->child;
1326 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1327 d07291c6 2022-12-30 thomas v = view->parent;
1328 2b49a8ae 2019-06-22 stsp
1329 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1330 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1331 a5d43cac 2022-07-01 thomas
1332 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1333 2b49a8ae 2019-06-22 stsp nocbreak();
1334 2b49a8ae 2019-06-22 stsp echo();
1335 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1336 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1337 2b49a8ae 2019-06-22 stsp cbreak();
1338 2b49a8ae 2019-06-22 stsp noecho();
1339 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1340 f54d892e 2023-02-17 thomas if (!fast_refresh)
1341 f54d892e 2023-02-17 thomas halfdelay(10);
1342 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1343 2b49a8ae 2019-06-22 stsp return NULL;
1344 2b49a8ae 2019-06-22 stsp
1345 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1346 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1347 7c32bd05 2019-06-22 stsp if (err) {
1348 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1349 7c32bd05 2019-06-22 stsp return err;
1350 7c32bd05 2019-06-22 stsp }
1351 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1352 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1353 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1354 2b49a8ae 2019-06-22 stsp view->search_next(view);
1355 2b49a8ae 2019-06-22 stsp }
1356 2b49a8ae 2019-06-22 stsp
1357 2b49a8ae 2019-06-22 stsp return NULL;
1358 64486692 2022-07-07 thomas }
1359 64486692 2022-07-07 thomas
1360 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1361 64486692 2022-07-07 thomas static const struct got_error *
1362 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1363 64486692 2022-07-07 thomas {
1364 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1365 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1366 64486692 2022-07-07 thomas
1367 64486692 2022-07-07 thomas if (view->parent)
1368 64486692 2022-07-07 thomas v = view->parent;
1369 64486692 2022-07-07 thomas else
1370 64486692 2022-07-07 thomas v = view;
1371 64486692 2022-07-07 thomas
1372 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1373 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1374 ddbc4d37 2022-07-12 thomas else
1375 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1376 64486692 2022-07-07 thomas
1377 ddbc4d37 2022-07-12 thomas if (!v->child)
1378 ddbc4d37 2022-07-12 thomas return NULL;
1379 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1380 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1381 ddbc4d37 2022-07-12 thomas
1382 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1383 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1384 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1385 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1386 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1387 64486692 2022-07-07 thomas
1388 ddbc4d37 2022-07-12 thomas
1389 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1390 64486692 2022-07-07 thomas v->ncols = COLS;
1391 64486692 2022-07-07 thomas v->child->ncols = COLS;
1392 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1393 64486692 2022-07-07 thomas
1394 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1395 64486692 2022-07-07 thomas if (err)
1396 64486692 2022-07-07 thomas return err;
1397 64486692 2022-07-07 thomas }
1398 64486692 2022-07-07 thomas v->child->mode = v->mode;
1399 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1400 64486692 2022-07-07 thomas v->focus_child = 1;
1401 64486692 2022-07-07 thomas
1402 64486692 2022-07-07 thomas err = view_fullscreen(v);
1403 64486692 2022-07-07 thomas if (err)
1404 64486692 2022-07-07 thomas return err;
1405 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1406 64486692 2022-07-07 thomas if (err)
1407 64486692 2022-07-07 thomas return err;
1408 64486692 2022-07-07 thomas
1409 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1410 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1411 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1412 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1413 cf1fe301 2022-07-29 thomas if (err)
1414 cf1fe301 2022-07-29 thomas return err;
1415 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1416 cf1fe301 2022-07-29 thomas if (err)
1417 cf1fe301 2022-07-29 thomas return err;
1418 ddbc4d37 2022-07-12 thomas } else {
1419 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1420 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1421 64486692 2022-07-07 thomas }
1422 f4e6231a 2022-07-22 thomas if (v->resize)
1423 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1424 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1425 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1426 64486692 2022-07-07 thomas
1427 64486692 2022-07-07 thomas return err;
1428 0cf4efb1 2018-09-29 stsp }
1429 6d0fee91 2018-08-01 stsp
1430 07b0611c 2022-06-23 thomas /*
1431 b85a3496 2023-04-14 thomas * Strip trailing whitespace from str starting at byte *n;
1432 b85a3496 2023-04-14 thomas * if *n < 0, use strlen(str). Return new str length in *n.
1433 b85a3496 2023-04-14 thomas */
1434 b85a3496 2023-04-14 thomas static void
1435 b85a3496 2023-04-14 thomas strip_trailing_ws(char *str, int *n)
1436 b85a3496 2023-04-14 thomas {
1437 b85a3496 2023-04-14 thomas size_t x = *n;
1438 b85a3496 2023-04-14 thomas
1439 b85a3496 2023-04-14 thomas if (str == NULL || *str == '\0')
1440 b85a3496 2023-04-14 thomas return;
1441 b85a3496 2023-04-14 thomas
1442 b85a3496 2023-04-14 thomas if (x < 0)
1443 b85a3496 2023-04-14 thomas x = strlen(str);
1444 b85a3496 2023-04-14 thomas
1445 b85a3496 2023-04-14 thomas while (x-- > 0 && isspace((unsigned char)str[x]))
1446 b85a3496 2023-04-14 thomas str[x] = '\0';
1447 b85a3496 2023-04-14 thomas
1448 b85a3496 2023-04-14 thomas *n = x + 1;
1449 b85a3496 2023-04-14 thomas }
1450 b85a3496 2023-04-14 thomas
1451 b85a3496 2023-04-14 thomas /*
1452 b85a3496 2023-04-14 thomas * Extract visible substring of line y from the curses screen
1453 b85a3496 2023-04-14 thomas * and strip trailing whitespace. If vline is set and locale is
1454 b85a3496 2023-04-14 thomas * UTF-8, overwrite line[vline] with '|' because the ACS_VLINE
1455 b85a3496 2023-04-14 thomas * character is written out as 'x'. Write the line to file f.
1456 b85a3496 2023-04-14 thomas */
1457 b85a3496 2023-04-14 thomas static const struct got_error *
1458 b85a3496 2023-04-14 thomas view_write_line(FILE *f, int y, int vline)
1459 b85a3496 2023-04-14 thomas {
1460 b85a3496 2023-04-14 thomas char line[COLS * MB_LEN_MAX]; /* allow for multibyte chars */
1461 b85a3496 2023-04-14 thomas int r, w;
1462 b85a3496 2023-04-14 thomas
1463 b85a3496 2023-04-14 thomas r = mvwinnstr(curscr, y, 0, line, sizeof(line));
1464 b85a3496 2023-04-14 thomas if (r == ERR)
1465 b85a3496 2023-04-14 thomas return got_error_fmt(GOT_ERR_RANGE,
1466 b85a3496 2023-04-14 thomas "failed to extract line %d", y);
1467 b85a3496 2023-04-14 thomas
1468 b85a3496 2023-04-14 thomas /*
1469 b85a3496 2023-04-14 thomas * In some views, lines are padded with blanks to COLS width.
1470 b85a3496 2023-04-14 thomas * Strip them so we can diff without the -b flag when testing.
1471 b85a3496 2023-04-14 thomas */
1472 b85a3496 2023-04-14 thomas strip_trailing_ws(line, &r);
1473 b85a3496 2023-04-14 thomas
1474 b85a3496 2023-04-14 thomas if (vline > 0 && got_locale_is_utf8())
1475 b85a3496 2023-04-14 thomas line[vline] = '|';
1476 b85a3496 2023-04-14 thomas
1477 b85a3496 2023-04-14 thomas w = fprintf(f, "%s\n", line);
1478 b85a3496 2023-04-14 thomas if (w != r + 1) /* \n */
1479 b85a3496 2023-04-14 thomas return got_ferror(f, GOT_ERR_IO);
1480 b85a3496 2023-04-14 thomas
1481 b85a3496 2023-04-14 thomas return NULL;
1482 b85a3496 2023-04-14 thomas }
1483 b85a3496 2023-04-14 thomas
1484 b85a3496 2023-04-14 thomas /*
1485 b85a3496 2023-04-14 thomas * Capture the visible curses screen by writing each line to the
1486 b85a3496 2023-04-14 thomas * file at the path set via the TOG_SCR_DUMP environment variable.
1487 b85a3496 2023-04-14 thomas */
1488 b85a3496 2023-04-14 thomas static const struct got_error *
1489 b85a3496 2023-04-14 thomas screendump(struct tog_view *view)
1490 b85a3496 2023-04-14 thomas {
1491 b85a3496 2023-04-14 thomas const struct got_error *err;
1492 b85a3496 2023-04-14 thomas FILE *f = NULL;
1493 b85a3496 2023-04-14 thomas const char *path;
1494 b85a3496 2023-04-14 thomas int i;
1495 b85a3496 2023-04-14 thomas
1496 b85a3496 2023-04-14 thomas path = getenv("TOG_SCR_DUMP");
1497 b85a3496 2023-04-14 thomas if (path == NULL || *path == '\0')
1498 b85a3496 2023-04-14 thomas return got_error_msg(GOT_ERR_BAD_PATH,
1499 b85a3496 2023-04-14 thomas "TOG_SCR_DUMP path not set to capture screen dump");
1500 b85a3496 2023-04-14 thomas f = fopen(path, "wex");
1501 b85a3496 2023-04-14 thomas if (f == NULL)
1502 b85a3496 2023-04-14 thomas return got_error_from_errno_fmt("fopen: %s", path);
1503 b85a3496 2023-04-14 thomas
1504 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_x) ||
1505 b85a3496 2023-04-14 thomas (view->parent && view->begin_x)) {
1506 b85a3496 2023-04-14 thomas int ncols = view->child ? view->ncols : view->parent->ncols;
1507 b85a3496 2023-04-14 thomas
1508 b85a3496 2023-04-14 thomas /* vertical splitscreen */
1509 b85a3496 2023-04-14 thomas for (i = 0; i < view->nlines; ++i) {
1510 b85a3496 2023-04-14 thomas err = view_write_line(f, i, ncols - 1);
1511 b85a3496 2023-04-14 thomas if (err)
1512 b85a3496 2023-04-14 thomas goto done;
1513 b85a3496 2023-04-14 thomas }
1514 b85a3496 2023-04-14 thomas } else {
1515 b85a3496 2023-04-14 thomas int hline = 0;
1516 b85a3496 2023-04-14 thomas
1517 b85a3496 2023-04-14 thomas /* fullscreen or horizontal splitscreen */
1518 b85a3496 2023-04-14 thomas if ((view->child && view->child->begin_y) ||
1519 b85a3496 2023-04-14 thomas (view->parent && view->begin_y)) /* hsplit */
1520 b85a3496 2023-04-14 thomas hline = view->child ?
1521 b85a3496 2023-04-14 thomas view->child->begin_y : view->begin_y;
1522 b85a3496 2023-04-14 thomas
1523 b85a3496 2023-04-14 thomas for (i = 0; i < view->lines; i++) {
1524 b85a3496 2023-04-14 thomas if (hline && got_locale_is_utf8() && i == hline - 1) {
1525 b85a3496 2023-04-14 thomas int c;
1526 b85a3496 2023-04-14 thomas
1527 b85a3496 2023-04-14 thomas /* ACS_HLINE writes out as 'q', overwrite it */
1528 b85a3496 2023-04-14 thomas for (c = 0; c < view->cols; ++c)
1529 b85a3496 2023-04-14 thomas fputc('-', f);
1530 b85a3496 2023-04-14 thomas fputc('\n', f);
1531 b85a3496 2023-04-14 thomas continue;
1532 b85a3496 2023-04-14 thomas }
1533 b85a3496 2023-04-14 thomas
1534 b85a3496 2023-04-14 thomas err = view_write_line(f, i, 0);
1535 b85a3496 2023-04-14 thomas if (err)
1536 b85a3496 2023-04-14 thomas goto done;
1537 b85a3496 2023-04-14 thomas }
1538 b85a3496 2023-04-14 thomas }
1539 b85a3496 2023-04-14 thomas
1540 b85a3496 2023-04-14 thomas done:
1541 b85a3496 2023-04-14 thomas if (f && fclose(f) == EOF && err == NULL)
1542 b85a3496 2023-04-14 thomas err = got_ferror(f, GOT_ERR_IO);
1543 b85a3496 2023-04-14 thomas return err;
1544 b85a3496 2023-04-14 thomas }
1545 b85a3496 2023-04-14 thomas
1546 b85a3496 2023-04-14 thomas /*
1547 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1548 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1549 07b0611c 2022-06-23 thomas */
1550 07b0611c 2022-06-23 thomas static int
1551 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1552 07b0611c 2022-06-23 thomas {
1553 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1554 a5d43cac 2022-07-01 thomas int x, n = 0;
1555 07b0611c 2022-06-23 thomas
1556 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1557 a5d43cac 2022-07-01 thomas v = view->child;
1558 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1559 a5d43cac 2022-07-01 thomas v = view->parent;
1560 a5d43cac 2022-07-01 thomas
1561 07b0611c 2022-06-23 thomas view->count = 0;
1562 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1563 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1564 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1565 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1566 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1567 07b0611c 2022-06-23 thomas
1568 07b0611c 2022-06-23 thomas do {
1569 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1570 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1571 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1572 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1573 a5d43cac 2022-07-01 thomas }
1574 a5d43cac 2022-07-01 thomas
1575 07b0611c 2022-06-23 thomas /*
1576 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1577 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1578 07b0611c 2022-06-23 thomas */
1579 07b0611c 2022-06-23 thomas if (n >= 9999999)
1580 07b0611c 2022-06-23 thomas n = 9999999;
1581 07b0611c 2022-06-23 thomas else
1582 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1583 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1584 07b0611c 2022-06-23 thomas
1585 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1586 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1587 07dd3ed3 2022-08-06 thomas n = 0;
1588 07dd3ed3 2022-08-06 thomas c = 0;
1589 07dd3ed3 2022-08-06 thomas }
1590 07dd3ed3 2022-08-06 thomas
1591 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1592 07b0611c 2022-06-23 thomas view->count = n;
1593 07b0611c 2022-06-23 thomas
1594 07b0611c 2022-06-23 thomas return c;
1595 f2d06bef 2023-01-23 thomas }
1596 f2d06bef 2023-01-23 thomas
1597 f2d06bef 2023-01-23 thomas static void
1598 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1599 f2d06bef 2023-01-23 thomas {
1600 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1601 f2d06bef 2023-01-23 thomas
1602 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1603 f2d06bef 2023-01-23 thomas v = view->child;
1604 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1605 f2d06bef 2023-01-23 thomas v = view->parent;
1606 f2d06bef 2023-01-23 thomas
1607 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1608 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1609 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1610 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1611 f2d06bef 2023-01-23 thomas
1612 f2d06bef 2023-01-23 thomas /*
1613 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1614 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1615 f2d06bef 2023-01-23 thomas */
1616 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1617 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1618 f2d06bef 2023-01-23 thomas view->action = NULL;
1619 f2d06bef 2023-01-23 thomas } else
1620 f2d06bef 2023-01-23 thomas view->action = NULL;
1621 07b0611c 2022-06-23 thomas }
1622 07b0611c 2022-06-23 thomas
1623 b85a3496 2023-04-14 thomas /*
1624 b85a3496 2023-04-14 thomas * Read the next line from the test script and assign
1625 b85a3496 2023-04-14 thomas * key instruction to *ch. If at EOF, set the *done flag.
1626 b85a3496 2023-04-14 thomas */
1627 0cf4efb1 2018-09-29 stsp static const struct got_error *
1628 b85a3496 2023-04-14 thomas tog_read_script_key(FILE *script, int *ch, int *done)
1629 b85a3496 2023-04-14 thomas {
1630 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1631 b85a3496 2023-04-14 thomas char *line = NULL;
1632 b85a3496 2023-04-14 thomas size_t linesz = 0;
1633 b85a3496 2023-04-14 thomas
1634 b85a3496 2023-04-14 thomas if (getline(&line, &linesz, script) == -1) {
1635 b85a3496 2023-04-14 thomas if (feof(script)) {
1636 b85a3496 2023-04-14 thomas *done = 1;
1637 b85a3496 2023-04-14 thomas goto done;
1638 b85a3496 2023-04-14 thomas } else {
1639 b85a3496 2023-04-14 thomas err = got_ferror(script, GOT_ERR_IO);
1640 b85a3496 2023-04-14 thomas goto done;
1641 b85a3496 2023-04-14 thomas }
1642 b85a3496 2023-04-14 thomas } else if (strncasecmp(line, "KEY_ENTER", 9) == 0)
1643 b85a3496 2023-04-14 thomas *ch = KEY_ENTER;
1644 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_RIGHT", 9) == 0)
1645 b85a3496 2023-04-14 thomas *ch = KEY_RIGHT;
1646 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_LEFT", 8) == 0)
1647 b85a3496 2023-04-14 thomas *ch = KEY_LEFT;
1648 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_DOWN", 8) == 0)
1649 b85a3496 2023-04-14 thomas *ch = KEY_DOWN;
1650 b85a3496 2023-04-14 thomas else if (strncasecmp(line, "KEY_UP", 6) == 0)
1651 b85a3496 2023-04-14 thomas *ch = KEY_UP;
1652 b85a3496 2023-04-14 thomas else if (strncasecmp(line, TOG_SCREEN_DUMP, TOG_SCREEN_DUMP_LEN) == 0)
1653 b85a3496 2023-04-14 thomas *ch = TOG_KEY_SCRDUMP;
1654 b85a3496 2023-04-14 thomas else
1655 b85a3496 2023-04-14 thomas *ch = *line;
1656 b85a3496 2023-04-14 thomas
1657 b85a3496 2023-04-14 thomas done:
1658 b85a3496 2023-04-14 thomas free(line);
1659 b85a3496 2023-04-14 thomas return err;
1660 b85a3496 2023-04-14 thomas }
1661 b85a3496 2023-04-14 thomas
1662 b85a3496 2023-04-14 thomas static const struct got_error *
1663 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1664 b85a3496 2023-04-14 thomas struct tog_view_list_head *views, struct tog_io *tog_io, int fast_refresh)
1665 e5a0f69f 2018-08-18 stsp {
1666 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1667 669b5ffa 2018-10-07 stsp struct tog_view *v;
1668 1a76625f 2018-10-22 stsp int ch, errcode;
1669 e5a0f69f 2018-08-18 stsp
1670 e5a0f69f 2018-08-18 stsp *new = NULL;
1671 f2d06bef 2023-01-23 thomas
1672 f2d06bef 2023-01-23 thomas if (view->action)
1673 f2d06bef 2023-01-23 thomas action_report(view);
1674 8f4ed634 2020-03-26 stsp
1675 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1676 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1677 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1678 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1679 07b0611c 2022-06-23 thomas view->count = 0;
1680 07b0611c 2022-06-23 thomas }
1681 e5a0f69f 2018-08-18 stsp
1682 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1683 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1684 82954512 2020-02-03 stsp if (errcode)
1685 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1686 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1687 3da8ef85 2021-09-21 stsp sched_yield();
1688 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1689 82954512 2020-02-03 stsp if (errcode)
1690 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1691 82954512 2020-02-03 stsp "pthread_mutex_lock");
1692 60493ae3 2019-06-20 stsp view->search_next(view);
1693 60493ae3 2019-06-20 stsp return NULL;
1694 60493ae3 2019-06-20 stsp }
1695 60493ae3 2019-06-20 stsp
1696 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1697 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1698 1a76625f 2018-10-22 stsp if (errcode)
1699 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1700 b85a3496 2023-04-14 thomas
1701 b85a3496 2023-04-14 thomas if (tog_io && tog_io->f) {
1702 b85a3496 2023-04-14 thomas err = tog_read_script_key(tog_io->f, &ch, done);
1703 b85a3496 2023-04-14 thomas if (err)
1704 b85a3496 2023-04-14 thomas return err;
1705 b85a3496 2023-04-14 thomas } else if (view->count && --view->count) {
1706 634cb454 2022-07-03 thomas cbreak();
1707 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1708 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1709 b85a3496 2023-04-14 thomas /* let C-g or backspace abort unfinished count */
1710 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1711 634cb454 2022-07-03 thomas view->count = 0;
1712 634cb454 2022-07-03 thomas else
1713 634cb454 2022-07-03 thomas ch = view->ch;
1714 634cb454 2022-07-03 thomas } else {
1715 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1716 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1717 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1718 07b0611c 2022-06-23 thomas }
1719 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1720 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1721 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1722 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1723 1a76625f 2018-10-22 stsp if (errcode)
1724 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1725 25791caa 2018-10-24 stsp
1726 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1727 25791caa 2018-10-24 stsp tog_resizeterm();
1728 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1729 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1730 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1731 25791caa 2018-10-24 stsp err = view_resize(v);
1732 25791caa 2018-10-24 stsp if (err)
1733 25791caa 2018-10-24 stsp return err;
1734 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1735 25791caa 2018-10-24 stsp if (err)
1736 25791caa 2018-10-24 stsp return err;
1737 cdfcfb03 2020-12-06 stsp if (v->child) {
1738 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1739 cdfcfb03 2020-12-06 stsp if (err)
1740 cdfcfb03 2020-12-06 stsp return err;
1741 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1742 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1743 cdfcfb03 2020-12-06 stsp if (err)
1744 cdfcfb03 2020-12-06 stsp return err;
1745 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1746 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1747 53d2bdd3 2022-07-10 thomas if (err)
1748 53d2bdd3 2022-07-10 thomas return err;
1749 53d2bdd3 2022-07-10 thomas }
1750 cdfcfb03 2020-12-06 stsp }
1751 25791caa 2018-10-24 stsp }
1752 25791caa 2018-10-24 stsp }
1753 25791caa 2018-10-24 stsp
1754 e5a0f69f 2018-08-18 stsp switch (ch) {
1755 fc2737d5 2022-09-15 thomas case '?':
1756 fc2737d5 2022-09-15 thomas case 'H':
1757 fc2737d5 2022-09-15 thomas case KEY_F(1):
1758 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1759 fc2737d5 2022-09-15 thomas err = view->reset(view);
1760 fc2737d5 2022-09-15 thomas else
1761 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1762 fc2737d5 2022-09-15 thomas break;
1763 1e37a5c2 2019-05-12 jcs case '\t':
1764 07b0611c 2022-06-23 thomas view->count = 0;
1765 1e37a5c2 2019-05-12 jcs if (view->child) {
1766 e78dc838 2020-12-04 stsp view->focussed = 0;
1767 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1768 e78dc838 2020-12-04 stsp view->focus_child = 1;
1769 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1770 e78dc838 2020-12-04 stsp view->focussed = 0;
1771 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1772 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1773 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1774 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1775 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1776 3a0139e8 2022-07-22 thomas 0);
1777 a5d43cac 2022-07-01 thomas if (err)
1778 a5d43cac 2022-07-01 thomas return err;
1779 a5d43cac 2022-07-01 thomas }
1780 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1781 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1782 a5d43cac 2022-07-01 thomas if (err)
1783 a5d43cac 2022-07-01 thomas return err;
1784 a5d43cac 2022-07-01 thomas }
1785 1e37a5c2 2019-05-12 jcs }
1786 1e37a5c2 2019-05-12 jcs break;
1787 1e37a5c2 2019-05-12 jcs case 'q':
1788 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1789 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1790 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1791 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1792 a5d43cac 2022-07-01 thomas if (err)
1793 a5d43cac 2022-07-01 thomas break;
1794 a5d43cac 2022-07-01 thomas }
1795 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1796 a5d43cac 2022-07-01 thomas }
1797 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1798 9970f7fc 2020-12-03 stsp view->dying = 1;
1799 1e37a5c2 2019-05-12 jcs break;
1800 1e37a5c2 2019-05-12 jcs case 'Q':
1801 1e37a5c2 2019-05-12 jcs *done = 1;
1802 1e37a5c2 2019-05-12 jcs break;
1803 1c5e5faa 2022-06-23 thomas case 'F':
1804 07b0611c 2022-06-23 thomas view->count = 0;
1805 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1806 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1807 1e37a5c2 2019-05-12 jcs break;
1808 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1809 e78dc838 2020-12-04 stsp view->focussed = 0;
1810 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1811 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1812 53d2bdd3 2022-07-10 thomas } else {
1813 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1814 53d2bdd3 2022-07-10 thomas if (!err)
1815 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1816 53d2bdd3 2022-07-10 thomas }
1817 1e37a5c2 2019-05-12 jcs if (err)
1818 1e37a5c2 2019-05-12 jcs break;
1819 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1820 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1821 1e37a5c2 2019-05-12 jcs } else {
1822 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1823 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1824 e78dc838 2020-12-04 stsp view->focussed = 1;
1825 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1826 1e37a5c2 2019-05-12 jcs } else {
1827 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1828 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1829 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1830 53d2bdd3 2022-07-10 thomas if (!err)
1831 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1832 669b5ffa 2018-10-07 stsp }
1833 1e37a5c2 2019-05-12 jcs if (err)
1834 1e37a5c2 2019-05-12 jcs break;
1835 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1836 a5d43cac 2022-07-01 thomas }
1837 a5d43cac 2022-07-01 thomas if (err)
1838 a5d43cac 2022-07-01 thomas break;
1839 3a0139e8 2022-07-22 thomas if (view->resize) {
1840 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1841 a5d43cac 2022-07-01 thomas if (err)
1842 a5d43cac 2022-07-01 thomas break;
1843 1e37a5c2 2019-05-12 jcs }
1844 a5d43cac 2022-07-01 thomas if (view->parent)
1845 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1846 a5d43cac 2022-07-01 thomas if (!err)
1847 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1848 1e37a5c2 2019-05-12 jcs break;
1849 64486692 2022-07-07 thomas case 'S':
1850 53d2bdd3 2022-07-10 thomas view->count = 0;
1851 64486692 2022-07-07 thomas err = switch_split(view);
1852 53d2bdd3 2022-07-10 thomas break;
1853 53d2bdd3 2022-07-10 thomas case '-':
1854 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1855 64486692 2022-07-07 thomas break;
1856 53d2bdd3 2022-07-10 thomas case '+':
1857 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1858 53d2bdd3 2022-07-10 thomas break;
1859 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1860 60493ae3 2019-06-20 stsp break;
1861 60493ae3 2019-06-20 stsp case '/':
1862 07b0611c 2022-06-23 thomas view->count = 0;
1863 60493ae3 2019-06-20 stsp if (view->search_start)
1864 f54d892e 2023-02-17 thomas view_search_start(view, fast_refresh);
1865 60493ae3 2019-06-20 stsp else
1866 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1867 1e37a5c2 2019-05-12 jcs break;
1868 b1bf1435 2019-06-21 stsp case 'N':
1869 60493ae3 2019-06-20 stsp case 'n':
1870 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1871 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1872 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1873 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1874 60493ae3 2019-06-20 stsp view->search_next(view);
1875 60493ae3 2019-06-20 stsp } else
1876 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1877 adf4c9e0 2022-07-03 thomas break;
1878 adf4c9e0 2022-07-03 thomas case 'A':
1879 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1880 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1881 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1882 f2d06bef 2023-01-23 thomas } else {
1883 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1884 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1885 f2d06bef 2023-01-23 thomas }
1886 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1887 adf4c9e0 2022-07-03 thomas if (v->reset) {
1888 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1889 adf4c9e0 2022-07-03 thomas if (err)
1890 adf4c9e0 2022-07-03 thomas return err;
1891 adf4c9e0 2022-07-03 thomas }
1892 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1893 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1894 adf4c9e0 2022-07-03 thomas if (err)
1895 adf4c9e0 2022-07-03 thomas return err;
1896 adf4c9e0 2022-07-03 thomas }
1897 adf4c9e0 2022-07-03 thomas }
1898 60493ae3 2019-06-20 stsp break;
1899 b85a3496 2023-04-14 thomas case TOG_KEY_SCRDUMP:
1900 b85a3496 2023-04-14 thomas err = screendump(view);
1901 b85a3496 2023-04-14 thomas break;
1902 1e37a5c2 2019-05-12 jcs default:
1903 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1904 1e37a5c2 2019-05-12 jcs break;
1905 e5a0f69f 2018-08-18 stsp }
1906 e5a0f69f 2018-08-18 stsp
1907 e5a0f69f 2018-08-18 stsp return err;
1908 bcbd79e2 2018-08-19 stsp }
1909 bcbd79e2 2018-08-19 stsp
1910 ef20f542 2022-06-26 thomas static int
1911 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1912 a3404814 2018-09-02 stsp {
1913 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1914 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1915 669b5ffa 2018-10-07 stsp return 0;
1916 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1917 669b5ffa 2018-10-07 stsp return 0;
1918 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1919 a3404814 2018-09-02 stsp return 0;
1920 a3404814 2018-09-02 stsp
1921 669b5ffa 2018-10-07 stsp return view->focussed;
1922 a3404814 2018-09-02 stsp }
1923 a3404814 2018-09-02 stsp
1924 bcbd79e2 2018-08-19 stsp static const struct got_error *
1925 b85a3496 2023-04-14 thomas tog_io_close(struct tog_io *tog_io)
1926 e5a0f69f 2018-08-18 stsp {
1927 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1928 b85a3496 2023-04-14 thomas
1929 b85a3496 2023-04-14 thomas if (tog_io->cin && fclose(tog_io->cin) == EOF)
1930 b85a3496 2023-04-14 thomas err = got_ferror(tog_io->cin, GOT_ERR_IO);
1931 b85a3496 2023-04-14 thomas if (tog_io->cout && fclose(tog_io->cout) == EOF && err == NULL)
1932 b85a3496 2023-04-14 thomas err = got_ferror(tog_io->cout, GOT_ERR_IO);
1933 b85a3496 2023-04-14 thomas if (tog_io->f && fclose(tog_io->f) == EOF && err == NULL)
1934 b85a3496 2023-04-14 thomas err = got_ferror(tog_io->f, GOT_ERR_IO);
1935 b85a3496 2023-04-14 thomas free(tog_io);
1936 b85a3496 2023-04-14 thomas tog_io = NULL;
1937 b85a3496 2023-04-14 thomas
1938 b85a3496 2023-04-14 thomas return err;
1939 b85a3496 2023-04-14 thomas }
1940 b85a3496 2023-04-14 thomas
1941 b85a3496 2023-04-14 thomas static const struct got_error *
1942 b85a3496 2023-04-14 thomas view_loop(struct tog_view *view, struct tog_io *tog_io)
1943 b85a3496 2023-04-14 thomas {
1944 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
1945 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1946 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1947 64486692 2022-07-07 thomas char *mode;
1948 fd823528 2018-10-22 stsp int fast_refresh = 10;
1949 1a76625f 2018-10-22 stsp int done = 0, errcode;
1950 e5a0f69f 2018-08-18 stsp
1951 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1952 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1953 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1954 64486692 2022-07-07 thomas else
1955 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1956 64486692 2022-07-07 thomas
1957 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1958 1a76625f 2018-10-22 stsp if (errcode)
1959 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1960 1a76625f 2018-10-22 stsp
1961 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1962 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1963 e5a0f69f 2018-08-18 stsp
1964 1004088d 2018-09-29 stsp view->focussed = 1;
1965 878940b7 2018-09-29 stsp err = view->show(view);
1966 0cf4efb1 2018-09-29 stsp if (err)
1967 0cf4efb1 2018-09-29 stsp return err;
1968 0cf4efb1 2018-09-29 stsp update_panels();
1969 0cf4efb1 2018-09-29 stsp doupdate();
1970 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1971 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1972 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1973 b185e033 2023-01-23 thomas if (fast_refresh && --fast_refresh == 0)
1974 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1975 fd823528 2018-10-22 stsp
1976 b85a3496 2023-04-14 thomas err = view_input(&new_view, &done, view, &views, tog_io,
1977 b85a3496 2023-04-14 thomas fast_refresh);
1978 e5a0f69f 2018-08-18 stsp if (err)
1979 e5a0f69f 2018-08-18 stsp break;
1980 6ec3d39c 2023-01-23 thomas
1981 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
1982 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
1983 6ec3d39c 2023-01-23 thomas done = 1;
1984 6ec3d39c 2023-01-23 thomas if (done) {
1985 6ec3d39c 2023-01-23 thomas struct tog_view *v;
1986 6ec3d39c 2023-01-23 thomas
1987 6ec3d39c 2023-01-23 thomas /*
1988 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
1989 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
1990 6ec3d39c 2023-01-23 thomas */
1991 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
1992 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
1993 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
1994 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
1995 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
1996 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
1997 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
1998 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
1999 6ec3d39c 2023-01-23 thomas }
2000 6ec3d39c 2023-01-23 thomas }
2001 6ec3d39c 2023-01-23 thomas doupdate();
2002 6ec3d39c 2023-01-23 thomas }
2003 6ec3d39c 2023-01-23 thomas
2004 9970f7fc 2020-12-03 stsp if (view->dying) {
2005 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
2006 669b5ffa 2018-10-07 stsp
2007 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
2008 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
2009 9970f7fc 2020-12-03 stsp entry);
2010 e78dc838 2020-12-04 stsp else if (view->parent)
2011 669b5ffa 2018-10-07 stsp prev = view->parent;
2012 669b5ffa 2018-10-07 stsp
2013 e78dc838 2020-12-04 stsp if (view->parent) {
2014 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
2015 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
2016 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
2017 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
2018 40236d76 2022-06-23 thomas err = view_resize(view->parent);
2019 40236d76 2022-06-23 thomas if (err)
2020 40236d76 2022-06-23 thomas break;
2021 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
2022 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
2023 e78dc838 2020-12-04 stsp } else
2024 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
2025 669b5ffa 2018-10-07 stsp
2026 9970f7fc 2020-12-03 stsp err = view_close(view);
2027 fb59748f 2020-12-05 stsp if (err)
2028 e5a0f69f 2018-08-18 stsp goto done;
2029 669b5ffa 2018-10-07 stsp
2030 e78dc838 2020-12-04 stsp view = NULL;
2031 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
2032 e78dc838 2020-12-04 stsp if (v->focussed)
2033 e78dc838 2020-12-04 stsp break;
2034 0cf4efb1 2018-09-29 stsp }
2035 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
2036 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
2037 e78dc838 2020-12-04 stsp if (prev)
2038 e78dc838 2020-12-04 stsp view = prev;
2039 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
2040 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
2041 e78dc838 2020-12-04 stsp tog_view_list_head);
2042 e78dc838 2020-12-04 stsp }
2043 e78dc838 2020-12-04 stsp if (view) {
2044 e78dc838 2020-12-04 stsp if (view->focus_child) {
2045 e78dc838 2020-12-04 stsp view->child->focussed = 1;
2046 e78dc838 2020-12-04 stsp view = view->child;
2047 e78dc838 2020-12-04 stsp } else
2048 e78dc838 2020-12-04 stsp view->focussed = 1;
2049 e78dc838 2020-12-04 stsp }
2050 e78dc838 2020-12-04 stsp }
2051 e5a0f69f 2018-08-18 stsp }
2052 bcbd79e2 2018-08-19 stsp if (new_view) {
2053 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
2054 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
2055 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
2056 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
2057 86c66b02 2018-10-18 stsp continue;
2058 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
2059 86c66b02 2018-10-18 stsp err = view_close(v);
2060 86c66b02 2018-10-18 stsp if (err)
2061 86c66b02 2018-10-18 stsp goto done;
2062 86c66b02 2018-10-18 stsp break;
2063 86c66b02 2018-10-18 stsp }
2064 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
2065 fed7eaa8 2018-10-24 stsp view = new_view;
2066 7cd52833 2022-06-23 thomas }
2067 6ec3d39c 2023-01-23 thomas if (view && !done) {
2068 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
2069 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
2070 e78dc838 2020-12-04 stsp view = view->child;
2071 e78dc838 2020-12-04 stsp } else {
2072 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
2073 e78dc838 2020-12-04 stsp view = view->parent;
2074 1a76625f 2018-10-22 stsp }
2075 e78dc838 2020-12-04 stsp show_panel(view->panel);
2076 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
2077 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
2078 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
2079 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
2080 669b5ffa 2018-10-07 stsp if (err)
2081 1a76625f 2018-10-22 stsp goto done;
2082 669b5ffa 2018-10-07 stsp }
2083 669b5ffa 2018-10-07 stsp err = view->show(view);
2084 0cf4efb1 2018-09-29 stsp if (err)
2085 1a76625f 2018-10-22 stsp goto done;
2086 669b5ffa 2018-10-07 stsp if (view->child) {
2087 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
2088 669b5ffa 2018-10-07 stsp if (err)
2089 1a76625f 2018-10-22 stsp goto done;
2090 669b5ffa 2018-10-07 stsp }
2091 1a76625f 2018-10-22 stsp update_panels();
2092 1a76625f 2018-10-22 stsp doupdate();
2093 0cf4efb1 2018-09-29 stsp }
2094 e5a0f69f 2018-08-18 stsp }
2095 e5a0f69f 2018-08-18 stsp done:
2096 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
2097 f2d749db 2022-07-12 thomas const struct got_error *close_err;
2098 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
2099 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
2100 f2d749db 2022-07-12 thomas close_err = view_close(view);
2101 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
2102 f2d749db 2022-07-12 thomas err = close_err;
2103 e5a0f69f 2018-08-18 stsp }
2104 1a76625f 2018-10-22 stsp
2105 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2106 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
2107 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2108 1a76625f 2018-10-22 stsp
2109 e5a0f69f 2018-08-18 stsp return err;
2110 ea5e7bb5 2018-08-01 stsp }
2111 ea5e7bb5 2018-08-01 stsp
2112 4ed7e80c 2018-05-20 stsp __dead static void
2113 9f7d7167 2018-04-29 stsp usage_log(void)
2114 9f7d7167 2018-04-29 stsp {
2115 80ddbec8 2018-04-29 stsp endwin();
2116 c70c5802 2018-08-01 stsp fprintf(stderr,
2117 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
2118 9f7d7167 2018-04-29 stsp getprogname());
2119 9f7d7167 2018-04-29 stsp exit(1);
2120 80ddbec8 2018-04-29 stsp }
2121 80ddbec8 2018-04-29 stsp
2122 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
2123 80ddbec8 2018-04-29 stsp static const struct got_error *
2124 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
2125 963b370f 2018-05-20 stsp {
2126 00dfcb92 2018-06-11 stsp char *vis = NULL;
2127 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2128 963b370f 2018-05-20 stsp
2129 963b370f 2018-05-20 stsp *ws = NULL;
2130 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
2131 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
2132 00dfcb92 2018-06-11 stsp int vislen;
2133 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
2134 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
2135 00dfcb92 2018-06-11 stsp
2136 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
2137 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
2138 00dfcb92 2018-06-11 stsp if (err)
2139 00dfcb92 2018-06-11 stsp return err;
2140 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
2141 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
2142 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
2143 a7f50699 2018-06-11 stsp goto done;
2144 a7f50699 2018-06-11 stsp }
2145 00dfcb92 2018-06-11 stsp }
2146 963b370f 2018-05-20 stsp
2147 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
2148 a7f50699 2018-06-11 stsp if (*ws == NULL) {
2149 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2150 a7f50699 2018-06-11 stsp goto done;
2151 a7f50699 2018-06-11 stsp }
2152 963b370f 2018-05-20 stsp
2153 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
2154 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
2155 a7f50699 2018-06-11 stsp done:
2156 00dfcb92 2018-06-11 stsp free(vis);
2157 963b370f 2018-05-20 stsp if (err) {
2158 963b370f 2018-05-20 stsp free(*ws);
2159 963b370f 2018-05-20 stsp *ws = NULL;
2160 963b370f 2018-05-20 stsp *wlen = 0;
2161 963b370f 2018-05-20 stsp }
2162 963b370f 2018-05-20 stsp return err;
2163 05171be4 2022-06-23 thomas }
2164 05171be4 2022-06-23 thomas
2165 05171be4 2022-06-23 thomas static const struct got_error *
2166 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
2167 05171be4 2022-06-23 thomas {
2168 05171be4 2022-06-23 thomas char *dst;
2169 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
2170 05171be4 2022-06-23 thomas
2171 05171be4 2022-06-23 thomas *ptr = NULL;
2172 05171be4 2022-06-23 thomas n = len = strlen(src);
2173 83316834 2022-06-23 thomas dst = malloc(n + 1);
2174 05171be4 2022-06-23 thomas if (dst == NULL)
2175 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
2176 05171be4 2022-06-23 thomas
2177 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
2178 05171be4 2022-06-23 thomas const char c = src[idx];
2179 05171be4 2022-06-23 thomas
2180 05171be4 2022-06-23 thomas if (c == '\t') {
2181 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
2182 b235ad5d 2022-06-23 thomas char *p;
2183 b235ad5d 2022-06-23 thomas
2184 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
2185 83316834 2022-06-23 thomas if (p == NULL) {
2186 83316834 2022-06-23 thomas free(dst);
2187 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
2188 83316834 2022-06-23 thomas
2189 83316834 2022-06-23 thomas }
2190 83316834 2022-06-23 thomas dst = p;
2191 05171be4 2022-06-23 thomas n += nb;
2192 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
2193 05171be4 2022-06-23 thomas sz += nb;
2194 05171be4 2022-06-23 thomas } else
2195 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2196 05171be4 2022-06-23 thomas ++idx;
2197 05171be4 2022-06-23 thomas }
2198 05171be4 2022-06-23 thomas
2199 05171be4 2022-06-23 thomas dst[sz] = '\0';
2200 05171be4 2022-06-23 thomas *ptr = dst;
2201 05171be4 2022-06-23 thomas return NULL;
2202 963b370f 2018-05-20 stsp }
2203 963b370f 2018-05-20 stsp
2204 8d208d34 2022-06-23 thomas /*
2205 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2206 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2207 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
2208 8d208d34 2022-06-23 thomas * *rcol.
2209 f91a2b48 2022-06-23 thomas */
2210 8d208d34 2022-06-23 thomas static int
2211 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2212 8d208d34 2022-06-23 thomas {
2213 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2214 f91a2b48 2022-06-23 thomas
2215 8d208d34 2022-06-23 thomas if (n == 0) {
2216 8d208d34 2022-06-23 thomas *rcol = cols;
2217 8d208d34 2022-06-23 thomas return off;
2218 8d208d34 2022-06-23 thomas }
2219 f91a2b48 2022-06-23 thomas
2220 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2221 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2222 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2223 8d208d34 2022-06-23 thomas else
2224 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2225 f91a2b48 2022-06-23 thomas
2226 8d208d34 2022-06-23 thomas if (width == -1) {
2227 8d208d34 2022-06-23 thomas width = 1;
2228 8d208d34 2022-06-23 thomas wline[i] = L'.';
2229 f91a2b48 2022-06-23 thomas }
2230 f91a2b48 2022-06-23 thomas
2231 8d208d34 2022-06-23 thomas if (cols + width > n)
2232 8d208d34 2022-06-23 thomas break;
2233 8d208d34 2022-06-23 thomas cols += width;
2234 f91a2b48 2022-06-23 thomas }
2235 f91a2b48 2022-06-23 thomas
2236 8d208d34 2022-06-23 thomas *rcol = cols;
2237 8d208d34 2022-06-23 thomas return i;
2238 f91a2b48 2022-06-23 thomas }
2239 f91a2b48 2022-06-23 thomas
2240 f91a2b48 2022-06-23 thomas /*
2241 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2242 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2243 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2244 f91a2b48 2022-06-23 thomas */
2245 f91a2b48 2022-06-23 thomas static const struct got_error *
2246 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2247 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2248 f91a2b48 2022-06-23 thomas {
2249 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2250 8d208d34 2022-06-23 thomas int cols;
2251 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2252 05171be4 2022-06-23 thomas char *exstr = NULL;
2253 963b370f 2018-05-20 stsp size_t wlen;
2254 8d208d34 2022-06-23 thomas int i, scrollx;
2255 963b370f 2018-05-20 stsp
2256 963b370f 2018-05-20 stsp *wlinep = NULL;
2257 b700b5d6 2018-07-10 stsp *widthp = 0;
2258 963b370f 2018-05-20 stsp
2259 05171be4 2022-06-23 thomas if (expand) {
2260 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2261 05171be4 2022-06-23 thomas if (err)
2262 05171be4 2022-06-23 thomas return err;
2263 05171be4 2022-06-23 thomas }
2264 05171be4 2022-06-23 thomas
2265 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2266 05171be4 2022-06-23 thomas free(exstr);
2267 963b370f 2018-05-20 stsp if (err)
2268 963b370f 2018-05-20 stsp return err;
2269 963b370f 2018-05-20 stsp
2270 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2271 f91a2b48 2022-06-23 thomas
2272 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2273 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2274 3f670bfb 2020-12-10 stsp wlen--;
2275 3f670bfb 2020-12-10 stsp }
2276 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2277 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2278 3f670bfb 2020-12-10 stsp wlen--;
2279 3f670bfb 2020-12-10 stsp }
2280 3f670bfb 2020-12-10 stsp
2281 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2282 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2283 27a741e5 2019-09-11 stsp
2284 b700b5d6 2018-07-10 stsp if (widthp)
2285 b700b5d6 2018-07-10 stsp *widthp = cols;
2286 f91a2b48 2022-06-23 thomas if (scrollxp)
2287 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2288 963b370f 2018-05-20 stsp if (err)
2289 963b370f 2018-05-20 stsp free(wline);
2290 963b370f 2018-05-20 stsp else
2291 963b370f 2018-05-20 stsp *wlinep = wline;
2292 963b370f 2018-05-20 stsp return err;
2293 963b370f 2018-05-20 stsp }
2294 963b370f 2018-05-20 stsp
2295 8b473291 2019-02-21 stsp static const struct got_error*
2296 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2297 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2298 8b473291 2019-02-21 stsp {
2299 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2300 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2301 8b473291 2019-02-21 stsp char *s;
2302 8b473291 2019-02-21 stsp const char *name;
2303 8b473291 2019-02-21 stsp
2304 8b473291 2019-02-21 stsp *refs_str = NULL;
2305 8b473291 2019-02-21 stsp
2306 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2307 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2308 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2309 52b5abe1 2019-08-13 stsp int cmp;
2310 52b5abe1 2019-08-13 stsp
2311 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2312 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2313 8b473291 2019-02-21 stsp continue;
2314 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2315 8b473291 2019-02-21 stsp name += 5;
2316 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2317 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2318 7143d404 2019-03-12 stsp continue;
2319 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2320 8b473291 2019-02-21 stsp name += 6;
2321 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2322 8b473291 2019-02-21 stsp name += 8;
2323 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2324 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2325 79cc719f 2020-04-24 stsp continue;
2326 79cc719f 2020-04-24 stsp }
2327 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2328 48cae60d 2020-09-22 stsp if (err)
2329 48cae60d 2020-09-22 stsp break;
2330 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2331 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2332 5d844a1e 2019-08-13 stsp if (err) {
2333 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2334 48cae60d 2020-09-22 stsp free(ref_id);
2335 5d844a1e 2019-08-13 stsp break;
2336 48cae60d 2020-09-22 stsp }
2337 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2338 5d844a1e 2019-08-13 stsp err = NULL;
2339 5d844a1e 2019-08-13 stsp tag = NULL;
2340 5d844a1e 2019-08-13 stsp }
2341 52b5abe1 2019-08-13 stsp }
2342 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2343 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2344 48cae60d 2020-09-22 stsp free(ref_id);
2345 52b5abe1 2019-08-13 stsp if (tag)
2346 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2347 52b5abe1 2019-08-13 stsp if (cmp != 0)
2348 52b5abe1 2019-08-13 stsp continue;
2349 8b473291 2019-02-21 stsp s = *refs_str;
2350 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2351 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2352 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2353 8b473291 2019-02-21 stsp free(s);
2354 8b473291 2019-02-21 stsp *refs_str = NULL;
2355 8b473291 2019-02-21 stsp break;
2356 8b473291 2019-02-21 stsp }
2357 8b473291 2019-02-21 stsp free(s);
2358 8b473291 2019-02-21 stsp }
2359 8b473291 2019-02-21 stsp
2360 8b473291 2019-02-21 stsp return err;
2361 8b473291 2019-02-21 stsp }
2362 8b473291 2019-02-21 stsp
2363 963b370f 2018-05-20 stsp static const struct got_error *
2364 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2365 27a741e5 2019-09-11 stsp int col_tab_align)
2366 5813d178 2019-03-09 stsp {
2367 e6b8b890 2020-12-29 naddy char *smallerthan;
2368 5813d178 2019-03-09 stsp
2369 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2370 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2371 5813d178 2019-03-09 stsp author = smallerthan + 1;
2372 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2373 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2374 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2375 5813d178 2019-03-09 stsp }
2376 5813d178 2019-03-09 stsp
2377 5813d178 2019-03-09 stsp static const struct got_error *
2378 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2379 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2380 8fdc79fe 2020-12-01 naddy int author_display_cols)
2381 80ddbec8 2018-04-29 stsp {
2382 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2383 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2384 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2385 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2386 5813d178 2019-03-09 stsp char *author = NULL;
2387 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2388 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2389 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2390 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2391 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2392 ccb26ccd 2018-11-05 stsp struct tm tm;
2393 45d799e2 2018-12-23 stsp time_t committer_time;
2394 11b20872 2019-11-08 stsp struct tog_color *tc;
2395 80ddbec8 2018-04-29 stsp
2396 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2397 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2398 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2399 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2400 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2401 b39d25c7 2018-07-10 stsp
2402 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2403 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2404 b39d25c7 2018-07-10 stsp else
2405 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2406 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2407 11b20872 2019-11-08 stsp if (tc)
2408 11b20872 2019-11-08 stsp wattr_on(view->window,
2409 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2410 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2411 11b20872 2019-11-08 stsp if (tc)
2412 11b20872 2019-11-08 stsp wattr_off(view->window,
2413 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2414 27a741e5 2019-09-11 stsp col = limit;
2415 b39d25c7 2018-07-10 stsp if (col > avail)
2416 b39d25c7 2018-07-10 stsp goto done;
2417 6570a66d 2019-11-08 stsp
2418 6570a66d 2019-11-08 stsp if (avail >= 120) {
2419 6570a66d 2019-11-08 stsp char *id_str;
2420 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2421 6570a66d 2019-11-08 stsp if (err)
2422 6570a66d 2019-11-08 stsp goto done;
2423 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2424 11b20872 2019-11-08 stsp if (tc)
2425 11b20872 2019-11-08 stsp wattr_on(view->window,
2426 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2427 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2428 11b20872 2019-11-08 stsp if (tc)
2429 11b20872 2019-11-08 stsp wattr_off(view->window,
2430 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2431 6570a66d 2019-11-08 stsp free(id_str);
2432 6570a66d 2019-11-08 stsp col += 9;
2433 6570a66d 2019-11-08 stsp if (col > avail)
2434 6570a66d 2019-11-08 stsp goto done;
2435 6570a66d 2019-11-08 stsp }
2436 b39d25c7 2018-07-10 stsp
2437 f69c5a46 2022-07-19 thomas if (s->use_committer)
2438 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2439 f69c5a46 2022-07-19 thomas else
2440 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2441 5813d178 2019-03-09 stsp if (author == NULL) {
2442 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2443 80ddbec8 2018-04-29 stsp goto done;
2444 80ddbec8 2018-04-29 stsp }
2445 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2446 bb737323 2018-05-20 stsp if (err)
2447 bb737323 2018-05-20 stsp goto done;
2448 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2449 11b20872 2019-11-08 stsp if (tc)
2450 11b20872 2019-11-08 stsp wattr_on(view->window,
2451 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2452 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2453 bb737323 2018-05-20 stsp col += author_width;
2454 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2455 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2456 bb737323 2018-05-20 stsp col++;
2457 bb737323 2018-05-20 stsp author_width++;
2458 bb737323 2018-05-20 stsp }
2459 f31d6c3b 2022-09-09 thomas if (tc)
2460 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2461 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2462 9c2eaf34 2018-05-20 stsp if (col > avail)
2463 9c2eaf34 2018-05-20 stsp goto done;
2464 80ddbec8 2018-04-29 stsp
2465 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2466 5943eee2 2019-08-13 stsp if (err)
2467 6d9fbc00 2018-04-29 stsp goto done;
2468 bb737323 2018-05-20 stsp logmsg = logmsg0;
2469 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2470 bb737323 2018-05-20 stsp logmsg++;
2471 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2472 bb737323 2018-05-20 stsp if (newline)
2473 bb737323 2018-05-20 stsp *newline = '\0';
2474 f91a2b48 2022-06-23 thomas limit = avail - col;
2475 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2476 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2477 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2478 f91a2b48 2022-06-23 thomas limit, col, 1);
2479 331b1a16 2022-06-23 thomas if (err)
2480 331b1a16 2022-06-23 thomas goto done;
2481 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2482 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2483 27a741e5 2019-09-11 stsp while (col < avail) {
2484 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2485 bb737323 2018-05-20 stsp col++;
2486 881b2d3e 2018-04-30 stsp }
2487 80ddbec8 2018-04-29 stsp done:
2488 80ddbec8 2018-04-29 stsp free(logmsg0);
2489 bb737323 2018-05-20 stsp free(wlogmsg);
2490 5813d178 2019-03-09 stsp free(author);
2491 bb737323 2018-05-20 stsp free(wauthor);
2492 80ddbec8 2018-04-29 stsp free(line);
2493 80ddbec8 2018-04-29 stsp return err;
2494 80ddbec8 2018-04-29 stsp }
2495 26ed57b2 2018-05-19 stsp
2496 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2497 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2498 899d86c2 2018-05-10 stsp struct got_object_id *id)
2499 80ddbec8 2018-04-29 stsp {
2500 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2501 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2502 80ddbec8 2018-04-29 stsp
2503 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2504 80ddbec8 2018-04-29 stsp if (entry == NULL)
2505 899d86c2 2018-05-10 stsp return NULL;
2506 99db9666 2018-05-07 stsp
2507 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2508 d68b9737 2022-09-05 thomas if (dup == NULL) {
2509 d68b9737 2022-09-05 thomas free(entry);
2510 d68b9737 2022-09-05 thomas return NULL;
2511 d68b9737 2022-09-05 thomas }
2512 d68b9737 2022-09-05 thomas
2513 d68b9737 2022-09-05 thomas entry->id = dup;
2514 99db9666 2018-05-07 stsp entry->commit = commit;
2515 899d86c2 2018-05-10 stsp return entry;
2516 99db9666 2018-05-07 stsp }
2517 80ddbec8 2018-04-29 stsp
2518 99db9666 2018-05-07 stsp static void
2519 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2520 99db9666 2018-05-07 stsp {
2521 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2522 99db9666 2018-05-07 stsp
2523 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2524 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2525 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2526 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2527 d68b9737 2022-09-05 thomas free(entry->id);
2528 99db9666 2018-05-07 stsp free(entry);
2529 99db9666 2018-05-07 stsp }
2530 99db9666 2018-05-07 stsp
2531 99db9666 2018-05-07 stsp static void
2532 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2533 99db9666 2018-05-07 stsp {
2534 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2535 99db9666 2018-05-07 stsp pop_commit(commits);
2536 c4972b91 2018-05-07 stsp }
2537 c4972b91 2018-05-07 stsp
2538 c4972b91 2018-05-07 stsp static const struct got_error *
2539 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2540 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2541 13add988 2019-10-15 stsp {
2542 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2543 13add988 2019-10-15 stsp regmatch_t regmatch;
2544 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2545 13add988 2019-10-15 stsp
2546 13add988 2019-10-15 stsp *have_match = 0;
2547 13add988 2019-10-15 stsp
2548 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2549 13add988 2019-10-15 stsp if (err)
2550 13add988 2019-10-15 stsp return err;
2551 13add988 2019-10-15 stsp
2552 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2553 13add988 2019-10-15 stsp if (err)
2554 13add988 2019-10-15 stsp goto done;
2555 13add988 2019-10-15 stsp
2556 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2557 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2558 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2559 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2560 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2561 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2562 13add988 2019-10-15 stsp *have_match = 1;
2563 13add988 2019-10-15 stsp done:
2564 13add988 2019-10-15 stsp free(id_str);
2565 13add988 2019-10-15 stsp free(logmsg);
2566 13add988 2019-10-15 stsp return err;
2567 13add988 2019-10-15 stsp }
2568 13add988 2019-10-15 stsp
2569 13add988 2019-10-15 stsp static const struct got_error *
2570 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2571 c4972b91 2018-05-07 stsp {
2572 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2573 9ba79e04 2018-06-11 stsp
2574 1a76625f 2018-10-22 stsp /*
2575 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2576 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2577 1a76625f 2018-10-22 stsp * while updating the display.
2578 1a76625f 2018-10-22 stsp */
2579 4e0d2870 2020-12-07 naddy do {
2580 7210b715 2022-09-11 thomas struct got_object_id id;
2581 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2582 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2583 7e8004ba 2022-09-11 thomas int limit_match = 0;
2584 1a76625f 2018-10-22 stsp int errcode;
2585 899d86c2 2018-05-10 stsp
2586 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2587 4e0d2870 2020-12-07 naddy NULL, NULL);
2588 7210b715 2022-09-11 thomas if (err)
2589 ecb28ae0 2018-07-16 stsp break;
2590 899d86c2 2018-05-10 stsp
2591 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2592 9ba79e04 2018-06-11 stsp if (err)
2593 9ba79e04 2018-06-11 stsp break;
2594 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2595 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2596 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2597 9ba79e04 2018-06-11 stsp break;
2598 9ba79e04 2018-06-11 stsp }
2599 93e45b7c 2018-09-24 stsp
2600 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2601 1a76625f 2018-10-22 stsp if (errcode) {
2602 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2603 13add988 2019-10-15 stsp "pthread_mutex_lock");
2604 1a76625f 2018-10-22 stsp break;
2605 1a76625f 2018-10-22 stsp }
2606 1a76625f 2018-10-22 stsp
2607 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2608 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2609 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2610 1a76625f 2018-10-22 stsp
2611 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2612 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2613 7e8004ba 2022-09-11 thomas a->limit_regex);
2614 7e8004ba 2022-09-11 thomas if (err)
2615 7e8004ba 2022-09-11 thomas break;
2616 7e8004ba 2022-09-11 thomas
2617 7e8004ba 2022-09-11 thomas if (limit_match) {
2618 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2619 7e8004ba 2022-09-11 thomas
2620 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2621 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2622 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2623 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2624 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2625 7e8004ba 2022-09-11 thomas break;
2626 7e8004ba 2022-09-11 thomas }
2627 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2628 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2629 7e8004ba 2022-09-11 thomas
2630 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2631 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2632 7e8004ba 2022-09-11 thomas matched, entry);
2633 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2634 7e8004ba 2022-09-11 thomas }
2635 7e8004ba 2022-09-11 thomas
2636 7e8004ba 2022-09-11 thomas /*
2637 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2638 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2639 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2640 7e8004ba 2022-09-11 thomas */
2641 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2642 7e8004ba 2022-09-11 thomas }
2643 7e8004ba 2022-09-11 thomas
2644 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2645 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2646 7c1452c1 2020-03-26 stsp int have_match;
2647 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2648 7c1452c1 2020-03-26 stsp if (err)
2649 7c1452c1 2020-03-26 stsp break;
2650 7e8004ba 2022-09-11 thomas
2651 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2652 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2653 7e8004ba 2022-09-11 thomas *a->search_next_done =
2654 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2655 7e8004ba 2022-09-11 thomas } else if (have_match)
2656 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2657 13add988 2019-10-15 stsp }
2658 13add988 2019-10-15 stsp
2659 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2660 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2661 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2662 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2663 7c1452c1 2020-03-26 stsp if (err)
2664 13add988 2019-10-15 stsp break;
2665 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2666 899d86c2 2018-05-10 stsp
2667 9ba79e04 2018-06-11 stsp return err;
2668 0553a4e3 2018-04-30 stsp }
2669 0553a4e3 2018-04-30 stsp
2670 2b779855 2020-12-05 naddy static void
2671 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2672 2b779855 2020-12-05 naddy {
2673 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2674 2b779855 2020-12-05 naddy int ncommits = 0;
2675 2b779855 2020-12-05 naddy
2676 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2677 2b779855 2020-12-05 naddy while (entry) {
2678 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2679 2b779855 2020-12-05 naddy s->selected_entry = entry;
2680 2b779855 2020-12-05 naddy break;
2681 2b779855 2020-12-05 naddy }
2682 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2683 2b779855 2020-12-05 naddy ncommits++;
2684 2b779855 2020-12-05 naddy }
2685 2b779855 2020-12-05 naddy }
2686 2b779855 2020-12-05 naddy
2687 0553a4e3 2018-04-30 stsp static const struct got_error *
2688 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2689 0553a4e3 2018-04-30 stsp {
2690 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2691 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2692 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2693 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2694 60493ae3 2019-06-20 stsp int width;
2695 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2696 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2697 8b473291 2019-02-21 stsp char *refs_str = NULL;
2698 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2699 11b20872 2019-11-08 stsp struct tog_color *tc;
2700 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2701 bd3f8225 2022-08-12 thomas
2702 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2703 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2704 0553a4e3 2018-04-30 stsp
2705 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2706 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2707 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2708 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2709 1a76625f 2018-10-22 stsp if (err)
2710 ecb28ae0 2018-07-16 stsp return err;
2711 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2712 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2713 d2075bf3 2020-12-25 stsp if (refs) {
2714 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2715 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2716 d2075bf3 2020-12-25 stsp if (err)
2717 d2075bf3 2020-12-25 stsp goto done;
2718 d2075bf3 2020-12-25 stsp }
2719 867c6645 2018-07-10 stsp }
2720 359bfafd 2019-02-22 stsp
2721 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2722 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2723 1a76625f 2018-10-22 stsp
2724 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2725 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2726 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2727 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2728 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2729 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2730 8f4ed634 2020-03-26 stsp goto done;
2731 8f4ed634 2020-03-26 stsp }
2732 8f4ed634 2020-03-26 stsp } else {
2733 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2734 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2735 f9686aa5 2020-03-27 stsp
2736 f9686aa5 2020-03-27 stsp if (view->searching) {
2737 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2738 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2739 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2740 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2741 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2742 f9686aa5 2020-03-27 stsp search_str = "searching...";
2743 f9686aa5 2020-03-27 stsp }
2744 f9686aa5 2020-03-27 stsp
2745 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2746 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2747 7e8004ba 2022-09-11 thomas
2748 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2749 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2750 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2751 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2752 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2753 8f4ed634 2020-03-26 stsp goto done;
2754 8f4ed634 2020-03-26 stsp }
2755 8b473291 2019-02-21 stsp }
2756 1a76625f 2018-10-22 stsp
2757 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2758 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2759 91198554 2022-06-23 thomas "........................................",
2760 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2761 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2762 1a76625f 2018-10-22 stsp header = NULL;
2763 1a76625f 2018-10-22 stsp goto done;
2764 1a76625f 2018-10-22 stsp }
2765 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2766 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2767 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2768 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2769 1a76625f 2018-10-22 stsp header = NULL;
2770 1a76625f 2018-10-22 stsp goto done;
2771 ecb28ae0 2018-07-16 stsp }
2772 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2773 1a76625f 2018-10-22 stsp if (err)
2774 1a76625f 2018-10-22 stsp goto done;
2775 867c6645 2018-07-10 stsp
2776 2814baeb 2018-08-01 stsp werase(view->window);
2777 867c6645 2018-07-10 stsp
2778 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2779 a3404814 2018-09-02 stsp wstandout(view->window);
2780 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2781 11b20872 2019-11-08 stsp if (tc)
2782 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2783 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2784 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2785 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2786 1a76625f 2018-10-22 stsp width++;
2787 1a76625f 2018-10-22 stsp }
2788 86f4aab9 2022-09-09 thomas if (tc)
2789 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2790 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2791 a3404814 2018-09-02 stsp wstandend(view->window);
2792 ecb28ae0 2018-07-16 stsp free(wline);
2793 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2794 1a76625f 2018-10-22 stsp goto done;
2795 0553a4e3 2018-04-30 stsp
2796 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2797 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2798 5813d178 2019-03-09 stsp ncommits = 0;
2799 05171be4 2022-06-23 thomas view->maxx = 0;
2800 5813d178 2019-03-09 stsp while (entry) {
2801 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2802 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2803 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2804 5813d178 2019-03-09 stsp int width;
2805 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2806 5813d178 2019-03-09 stsp break;
2807 f69c5a46 2022-07-19 thomas if (s->use_committer)
2808 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2809 f69c5a46 2022-07-19 thomas else
2810 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2811 5813d178 2019-03-09 stsp if (author == NULL) {
2812 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2813 5813d178 2019-03-09 stsp goto done;
2814 5813d178 2019-03-09 stsp }
2815 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2816 27a741e5 2019-09-11 stsp date_display_cols);
2817 5813d178 2019-03-09 stsp if (author_cols < width)
2818 5813d178 2019-03-09 stsp author_cols = width;
2819 5813d178 2019-03-09 stsp free(wauthor);
2820 5813d178 2019-03-09 stsp free(author);
2821 ef944b8b 2022-07-21 thomas if (err)
2822 ef944b8b 2022-07-21 thomas goto done;
2823 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2824 05171be4 2022-06-23 thomas if (err)
2825 05171be4 2022-06-23 thomas goto done;
2826 05171be4 2022-06-23 thomas msg = msg0;
2827 05171be4 2022-06-23 thomas while (*msg == '\n')
2828 05171be4 2022-06-23 thomas ++msg;
2829 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2830 331b1a16 2022-06-23 thomas *eol = '\0';
2831 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2832 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2833 331b1a16 2022-06-23 thomas if (err)
2834 331b1a16 2022-06-23 thomas goto done;
2835 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2836 05171be4 2022-06-23 thomas free(msg0);
2837 331b1a16 2022-06-23 thomas free(wmsg);
2838 7ca04879 2019-10-19 stsp ncommits++;
2839 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2840 5813d178 2019-03-09 stsp }
2841 5813d178 2019-03-09 stsp
2842 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2843 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2844 867c6645 2018-07-10 stsp ncommits = 0;
2845 899d86c2 2018-05-10 stsp while (entry) {
2846 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2847 899d86c2 2018-05-10 stsp break;
2848 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2849 2814baeb 2018-08-01 stsp wstandout(view->window);
2850 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2851 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2852 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2853 2814baeb 2018-08-01 stsp wstandend(view->window);
2854 0553a4e3 2018-04-30 stsp if (err)
2855 60493ae3 2019-06-20 stsp goto done;
2856 0553a4e3 2018-04-30 stsp ncommits++;
2857 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2858 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2859 80ddbec8 2018-04-29 stsp }
2860 80ddbec8 2018-04-29 stsp
2861 a5d43cac 2022-07-01 thomas view_border(view);
2862 1a76625f 2018-10-22 stsp done:
2863 1a76625f 2018-10-22 stsp free(id_str);
2864 8b473291 2019-02-21 stsp free(refs_str);
2865 1a76625f 2018-10-22 stsp free(ncommits_str);
2866 1a76625f 2018-10-22 stsp free(header);
2867 80ddbec8 2018-04-29 stsp return err;
2868 9f7d7167 2018-04-29 stsp }
2869 07b55e75 2018-05-10 stsp
2870 07b55e75 2018-05-10 stsp static void
2871 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2872 07b55e75 2018-05-10 stsp {
2873 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2874 07b55e75 2018-05-10 stsp int nscrolled = 0;
2875 07b55e75 2018-05-10 stsp
2876 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2877 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2878 07b55e75 2018-05-10 stsp return;
2879 9f7d7167 2018-04-29 stsp
2880 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2881 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2882 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2883 07b55e75 2018-05-10 stsp if (entry) {
2884 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2885 07b55e75 2018-05-10 stsp nscrolled++;
2886 07b55e75 2018-05-10 stsp }
2887 07b55e75 2018-05-10 stsp }
2888 aa075928 2018-05-10 stsp }
2889 aa075928 2018-05-10 stsp
2890 aa075928 2018-05-10 stsp static const struct got_error *
2891 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2892 aa075928 2018-05-10 stsp {
2893 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2894 5e224a3e 2019-02-22 stsp int errcode;
2895 8a42fca8 2019-02-22 stsp
2896 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2897 aa075928 2018-05-10 stsp
2898 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2899 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2900 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2901 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2902 7aafa0d1 2019-02-22 stsp if (errcode)
2903 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2904 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2905 7c1452c1 2020-03-26 stsp
2906 7c1452c1 2020-03-26 stsp /*
2907 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2908 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2909 7c1452c1 2020-03-26 stsp */
2910 7c1452c1 2020-03-26 stsp if (!wait)
2911 7c1452c1 2020-03-26 stsp break;
2912 7c1452c1 2020-03-26 stsp
2913 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2914 ffe38506 2020-12-01 naddy show_log_view(view);
2915 7c1452c1 2020-03-26 stsp update_panels();
2916 7c1452c1 2020-03-26 stsp doupdate();
2917 7c1452c1 2020-03-26 stsp
2918 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2919 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2920 82954512 2020-02-03 stsp if (errcode)
2921 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2922 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2923 82954512 2020-02-03 stsp
2924 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2925 ffe38506 2020-12-01 naddy show_log_view(view);
2926 7c1452c1 2020-03-26 stsp update_panels();
2927 7c1452c1 2020-03-26 stsp doupdate();
2928 5e224a3e 2019-02-22 stsp }
2929 5e224a3e 2019-02-22 stsp
2930 5e224a3e 2019-02-22 stsp return NULL;
2931 a5d43cac 2022-07-01 thomas }
2932 a5d43cac 2022-07-01 thomas
2933 a5d43cac 2022-07-01 thomas static const struct got_error *
2934 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2935 a5d43cac 2022-07-01 thomas {
2936 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2937 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2938 fe731b51 2022-07-14 thomas
2939 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2940 fe731b51 2022-07-14 thomas return NULL;
2941 a5d43cac 2022-07-01 thomas
2942 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2943 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2944 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2945 a5d43cac 2022-07-01 thomas
2946 a5d43cac 2022-07-01 thomas return err;
2947 5e224a3e 2019-02-22 stsp }
2948 5e224a3e 2019-02-22 stsp
2949 5e224a3e 2019-02-22 stsp static const struct got_error *
2950 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2951 5e224a3e 2019-02-22 stsp {
2952 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2953 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2954 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2955 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2956 5e224a3e 2019-02-22 stsp
2957 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2958 5e224a3e 2019-02-22 stsp return NULL;
2959 5e224a3e 2019-02-22 stsp
2960 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2961 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2962 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2963 08ebd0a9 2019-02-22 stsp /*
2964 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2965 08ebd0a9 2019-02-22 stsp */
2966 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2967 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2968 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2969 5e224a3e 2019-02-22 stsp if (err)
2970 5e224a3e 2019-02-22 stsp return err;
2971 7aafa0d1 2019-02-22 stsp }
2972 b295e71b 2019-02-22 stsp
2973 7aafa0d1 2019-02-22 stsp do {
2974 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2975 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2976 88048b54 2019-02-21 stsp break;
2977 88048b54 2019-02-21 stsp
2978 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2979 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
2980 aa075928 2018-05-10 stsp
2981 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2982 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2983 dd0a52c1 2018-05-20 stsp break;
2984 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2985 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2986 aa075928 2018-05-10 stsp
2987 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2988 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2989 a5d43cac 2022-07-01 thomas else
2990 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2991 a5d43cac 2022-07-01 thomas
2992 dd0a52c1 2018-05-20 stsp return err;
2993 07b55e75 2018-05-10 stsp }
2994 4a7f7875 2018-05-10 stsp
2995 cd0acaa7 2018-05-20 stsp static const struct got_error *
2996 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2997 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2998 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2999 cd0acaa7 2018-05-20 stsp {
3000 cd0acaa7 2018-05-20 stsp const struct got_error *err;
3001 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
3002 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
3003 cd0acaa7 2018-05-20 stsp
3004 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
3005 15a94983 2018-12-23 stsp if (diff_view == NULL)
3006 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3007 ea5e7bb5 2018-08-01 stsp
3008 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3009 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
3010 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
3011 e5a0f69f 2018-08-18 stsp if (err == NULL)
3012 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
3013 cd0acaa7 2018-05-20 stsp return err;
3014 4a7f7875 2018-05-10 stsp }
3015 4a7f7875 2018-05-10 stsp
3016 80ddbec8 2018-04-29 stsp static const struct got_error *
3017 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
3018 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
3019 9343a5fb 2018-06-23 stsp {
3020 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
3021 941e9f74 2019-05-21 stsp
3022 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
3023 941e9f74 2019-05-21 stsp if (parent == NULL)
3024 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
3025 941e9f74 2019-05-21 stsp
3026 941e9f74 2019-05-21 stsp parent->tree = s->tree;
3027 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
3028 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
3029 941e9f74 2019-05-21 stsp parent->selected = s->selected;
3030 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3031 941e9f74 2019-05-21 stsp s->tree = subtree;
3032 941e9f74 2019-05-21 stsp s->selected = 0;
3033 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
3034 941e9f74 2019-05-21 stsp return NULL;
3035 941e9f74 2019-05-21 stsp }
3036 941e9f74 2019-05-21 stsp
3037 941e9f74 2019-05-21 stsp static const struct got_error *
3038 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
3039 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
3040 941e9f74 2019-05-21 stsp {
3041 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
3042 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
3043 941e9f74 2019-05-21 stsp const char *p;
3044 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
3045 9343a5fb 2018-06-23 stsp
3046 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
3047 941e9f74 2019-05-21 stsp p = path;
3048 941e9f74 2019-05-21 stsp while (*p) {
3049 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3050 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
3051 56e0773d 2019-11-28 stsp char *te_name;
3052 33cbf02b 2020-01-12 stsp
3053 33cbf02b 2020-01-12 stsp while (p[0] == '/')
3054 33cbf02b 2020-01-12 stsp p++;
3055 941e9f74 2019-05-21 stsp
3056 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
3057 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3058 941e9f74 2019-05-21 stsp if (slash == NULL)
3059 33cbf02b 2020-01-12 stsp te_name = strdup(p);
3060 33cbf02b 2020-01-12 stsp else
3061 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
3062 56e0773d 2019-11-28 stsp if (te_name == NULL) {
3063 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
3064 56e0773d 2019-11-28 stsp break;
3065 941e9f74 2019-05-21 stsp }
3066 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
3067 56e0773d 2019-11-28 stsp if (te == NULL) {
3068 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
3069 56e0773d 2019-11-28 stsp free(te_name);
3070 941e9f74 2019-05-21 stsp break;
3071 941e9f74 2019-05-21 stsp }
3072 56e0773d 2019-11-28 stsp free(te_name);
3073 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
3074 941e9f74 2019-05-21 stsp
3075 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
3076 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
3077 b03c880f 2019-05-21 stsp
3078 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
3079 941e9f74 2019-05-21 stsp if (slash)
3080 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
3081 941e9f74 2019-05-21 stsp else
3082 941e9f74 2019-05-21 stsp subpath = strdup(path);
3083 941e9f74 2019-05-21 stsp if (subpath == NULL) {
3084 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
3085 941e9f74 2019-05-21 stsp break;
3086 941e9f74 2019-05-21 stsp }
3087 941e9f74 2019-05-21 stsp
3088 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
3089 941e9f74 2019-05-21 stsp subpath);
3090 941e9f74 2019-05-21 stsp if (err)
3091 941e9f74 2019-05-21 stsp break;
3092 941e9f74 2019-05-21 stsp
3093 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
3094 941e9f74 2019-05-21 stsp free(tree_id);
3095 941e9f74 2019-05-21 stsp if (err)
3096 941e9f74 2019-05-21 stsp break;
3097 941e9f74 2019-05-21 stsp
3098 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
3099 941e9f74 2019-05-21 stsp if (err) {
3100 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
3101 941e9f74 2019-05-21 stsp break;
3102 941e9f74 2019-05-21 stsp }
3103 941e9f74 2019-05-21 stsp if (slash == NULL)
3104 941e9f74 2019-05-21 stsp break;
3105 941e9f74 2019-05-21 stsp free(subpath);
3106 941e9f74 2019-05-21 stsp subpath = NULL;
3107 941e9f74 2019-05-21 stsp p = slash;
3108 941e9f74 2019-05-21 stsp }
3109 941e9f74 2019-05-21 stsp
3110 941e9f74 2019-05-21 stsp free(subpath);
3111 1a76625f 2018-10-22 stsp return err;
3112 61266923 2020-01-14 stsp }
3113 61266923 2020-01-14 stsp
3114 61266923 2020-01-14 stsp static const struct got_error *
3115 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
3116 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
3117 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
3118 55cccc34 2020-02-20 stsp {
3119 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
3120 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
3121 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
3122 55cccc34 2020-02-20 stsp
3123 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
3124 55cccc34 2020-02-20 stsp if (tree_view == NULL)
3125 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
3126 55cccc34 2020-02-20 stsp
3127 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
3128 bc573f3b 2021-07-10 stsp if (err)
3129 55cccc34 2020-02-20 stsp return err;
3130 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
3131 55cccc34 2020-02-20 stsp
3132 55cccc34 2020-02-20 stsp *new_view = tree_view;
3133 55cccc34 2020-02-20 stsp
3134 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
3135 55cccc34 2020-02-20 stsp return NULL;
3136 55cccc34 2020-02-20 stsp
3137 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
3138 55cccc34 2020-02-20 stsp }
3139 55cccc34 2020-02-20 stsp
3140 55cccc34 2020-02-20 stsp static const struct got_error *
3141 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
3142 61266923 2020-01-14 stsp {
3143 61266923 2020-01-14 stsp sigset_t sigset;
3144 61266923 2020-01-14 stsp int errcode;
3145 61266923 2020-01-14 stsp
3146 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
3147 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
3148 61266923 2020-01-14 stsp
3149 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
3150 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
3151 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3152 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
3153 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3154 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
3155 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3156 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
3157 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
3158 61266923 2020-01-14 stsp
3159 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
3160 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
3161 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
3162 61266923 2020-01-14 stsp
3163 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
3164 61266923 2020-01-14 stsp if (errcode)
3165 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
3166 61266923 2020-01-14 stsp
3167 61266923 2020-01-14 stsp return NULL;
3168 1a76625f 2018-10-22 stsp }
3169 1a76625f 2018-10-22 stsp
3170 1a76625f 2018-10-22 stsp static void *
3171 1a76625f 2018-10-22 stsp log_thread(void *arg)
3172 1a76625f 2018-10-22 stsp {
3173 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3174 1a76625f 2018-10-22 stsp int errcode = 0;
3175 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
3176 1a76625f 2018-10-22 stsp int done = 0;
3177 61266923 2020-01-14 stsp
3178 f2d749db 2022-07-12 thomas /*
3179 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
3180 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
3181 f2d749db 2022-07-12 thomas */
3182 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
3183 f2d749db 2022-07-12 thomas if (errcode) {
3184 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
3185 61266923 2020-01-14 stsp return (void *)err;
3186 f2d749db 2022-07-12 thomas }
3187 1a76625f 2018-10-22 stsp
3188 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
3189 f2d749db 2022-07-12 thomas if (err) {
3190 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3191 f2d749db 2022-07-12 thomas goto done;
3192 f2d749db 2022-07-12 thomas }
3193 f2d749db 2022-07-12 thomas
3194 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3195 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3196 f2d749db 2022-07-12 thomas if (errcode) {
3197 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3198 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3199 f2d749db 2022-07-12 thomas goto done;
3200 f2d749db 2022-07-12 thomas }
3201 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3202 1a76625f 2018-10-22 stsp if (err) {
3203 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3204 f2d749db 2022-07-12 thomas goto done;
3205 1a76625f 2018-10-22 stsp err = NULL;
3206 1a76625f 2018-10-22 stsp done = 1;
3207 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3208 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3209 7e8004ba 2022-09-11 thomas if (a->limit_match)
3210 7e8004ba 2022-09-11 thomas a->commits_needed--;
3211 7e8004ba 2022-09-11 thomas } else
3212 7e8004ba 2022-09-11 thomas a->commits_needed--;
3213 7e8004ba 2022-09-11 thomas }
3214 1a76625f 2018-10-22 stsp
3215 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3216 3abe8080 2019-04-10 stsp if (errcode) {
3217 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3218 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3219 f2d749db 2022-07-12 thomas goto done;
3220 3abe8080 2019-04-10 stsp } else if (*a->quit)
3221 1a76625f 2018-10-22 stsp done = 1;
3222 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3223 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3224 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3225 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3226 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3227 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3228 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3229 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3230 1a76625f 2018-10-22 stsp }
3231 1a76625f 2018-10-22 stsp
3232 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3233 7c1452c1 2020-03-26 stsp if (errcode) {
3234 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3235 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3236 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3237 f2d749db 2022-07-12 thomas goto done;
3238 7c1452c1 2020-03-26 stsp }
3239 7c1452c1 2020-03-26 stsp
3240 1a76625f 2018-10-22 stsp if (done)
3241 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3242 7c1452c1 2020-03-26 stsp else {
3243 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3244 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3245 7c1452c1 2020-03-26 stsp &tog_mutex);
3246 f2d749db 2022-07-12 thomas if (errcode) {
3247 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3248 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3249 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3250 f2d749db 2022-07-12 thomas goto done;
3251 f2d749db 2022-07-12 thomas }
3252 21355643 2020-12-06 stsp if (*a->quit)
3253 21355643 2020-12-06 stsp done = 1;
3254 7c1452c1 2020-03-26 stsp }
3255 1a76625f 2018-10-22 stsp }
3256 1a76625f 2018-10-22 stsp }
3257 3abe8080 2019-04-10 stsp a->log_complete = 1;
3258 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3259 f2d749db 2022-07-12 thomas if (errcode)
3260 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3261 f2d749db 2022-07-12 thomas done:
3262 f2d749db 2022-07-12 thomas if (err) {
3263 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3264 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3265 f2d749db 2022-07-12 thomas }
3266 1a76625f 2018-10-22 stsp return (void *)err;
3267 1a76625f 2018-10-22 stsp }
3268 1a76625f 2018-10-22 stsp
3269 1a76625f 2018-10-22 stsp static const struct got_error *
3270 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3271 1a76625f 2018-10-22 stsp {
3272 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3273 1a76625f 2018-10-22 stsp int errcode;
3274 1a76625f 2018-10-22 stsp
3275 1a76625f 2018-10-22 stsp if (s->thread) {
3276 1a76625f 2018-10-22 stsp s->quit = 1;
3277 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3278 1a76625f 2018-10-22 stsp if (errcode)
3279 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3280 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3281 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3282 1a76625f 2018-10-22 stsp if (errcode)
3283 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3284 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3285 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3286 1a76625f 2018-10-22 stsp if (errcode)
3287 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3288 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3289 1a76625f 2018-10-22 stsp if (errcode)
3290 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3291 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3292 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3293 1a76625f 2018-10-22 stsp }
3294 1a76625f 2018-10-22 stsp
3295 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3296 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3297 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3298 1af5eddf 2022-06-23 thomas }
3299 1af5eddf 2022-06-23 thomas
3300 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3301 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3302 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3303 1af5eddf 2022-06-23 thomas if (err == NULL)
3304 1af5eddf 2022-06-23 thomas err = pack_err;
3305 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3306 1a76625f 2018-10-22 stsp }
3307 1a76625f 2018-10-22 stsp
3308 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3309 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3310 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3311 1a76625f 2018-10-22 stsp }
3312 1a76625f 2018-10-22 stsp
3313 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3314 9343a5fb 2018-06-23 stsp }
3315 9343a5fb 2018-06-23 stsp
3316 9343a5fb 2018-06-23 stsp static const struct got_error *
3317 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3318 1a76625f 2018-10-22 stsp {
3319 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3320 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3321 276b94a1 2020-11-13 naddy int errcode;
3322 1a76625f 2018-10-22 stsp
3323 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3324 276b94a1 2020-11-13 naddy
3325 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3326 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3327 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3328 276b94a1 2020-11-13 naddy
3329 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3330 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3331 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3332 276b94a1 2020-11-13 naddy
3333 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3334 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3335 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3336 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3337 1a76625f 2018-10-22 stsp free(s->start_id);
3338 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3339 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3340 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3341 1a76625f 2018-10-22 stsp return err;
3342 1a76625f 2018-10-22 stsp }
3343 1a76625f 2018-10-22 stsp
3344 7e8004ba 2022-09-11 thomas /*
3345 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3346 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3347 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3348 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3349 7e8004ba 2022-09-11 thomas * works with very slight change.
3350 7e8004ba 2022-09-11 thomas */
3351 1a76625f 2018-10-22 stsp static const struct got_error *
3352 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3353 7e8004ba 2022-09-11 thomas {
3354 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3355 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3356 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3357 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3358 7e8004ba 2022-09-11 thomas char pattern[1024];
3359 7e8004ba 2022-09-11 thomas int ret;
3360 7e8004ba 2022-09-11 thomas
3361 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3362 7e8004ba 2022-09-11 thomas v = view->child;
3363 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3364 7e8004ba 2022-09-11 thomas v = view->parent;
3365 7e8004ba 2022-09-11 thomas
3366 7e8004ba 2022-09-11 thomas /* Get the pattern */
3367 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3368 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3369 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3370 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3371 7e8004ba 2022-09-11 thomas nocbreak();
3372 7e8004ba 2022-09-11 thomas echo();
3373 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3374 7e8004ba 2022-09-11 thomas cbreak();
3375 7e8004ba 2022-09-11 thomas noecho();
3376 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3377 7e8004ba 2022-09-11 thomas if (ret == ERR)
3378 7e8004ba 2022-09-11 thomas return NULL;
3379 7e8004ba 2022-09-11 thomas
3380 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3381 7e8004ba 2022-09-11 thomas /*
3382 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3383 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3384 7e8004ba 2022-09-11 thomas */
3385 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3386 7e8004ba 2022-09-11 thomas return NULL;
3387 7e8004ba 2022-09-11 thomas
3388 7e8004ba 2022-09-11 thomas /*
3389 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3390 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3391 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3392 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3393 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3394 7e8004ba 2022-09-11 thomas * the queue.
3395 7e8004ba 2022-09-11 thomas */
3396 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3397 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3398 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3399 7e8004ba 2022-09-11 thomas s->selected = 0;
3400 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3401 7e8004ba 2022-09-11 thomas
3402 7e8004ba 2022-09-11 thomas return NULL;
3403 7e8004ba 2022-09-11 thomas }
3404 7e8004ba 2022-09-11 thomas
3405 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3406 7e8004ba 2022-09-11 thomas return NULL;
3407 7e8004ba 2022-09-11 thomas
3408 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3409 7e8004ba 2022-09-11 thomas
3410 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3411 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3412 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3413 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3414 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3415 7e8004ba 2022-09-11 thomas
3416 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3417 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3418 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3419 7e8004ba 2022-09-11 thomas
3420 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3421 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3422 7e8004ba 2022-09-11 thomas int have_match = 0;
3423 7e8004ba 2022-09-11 thomas
3424 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3425 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3426 7e8004ba 2022-09-11 thomas if (err)
3427 7e8004ba 2022-09-11 thomas return err;
3428 7e8004ba 2022-09-11 thomas
3429 7e8004ba 2022-09-11 thomas if (have_match) {
3430 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3431 7e8004ba 2022-09-11 thomas
3432 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3433 7e8004ba 2022-09-11 thomas entry->id);
3434 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3435 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3436 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3437 7e8004ba 2022-09-11 thomas break;
3438 7e8004ba 2022-09-11 thomas }
3439 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3440 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3441 7e8004ba 2022-09-11 thomas
3442 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3443 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3444 7e8004ba 2022-09-11 thomas matched, entry);
3445 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3446 7e8004ba 2022-09-11 thomas }
3447 7e8004ba 2022-09-11 thomas }
3448 7e8004ba 2022-09-11 thomas
3449 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3450 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3451 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3452 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3453 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3454 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3455 7e8004ba 2022-09-11 thomas if (err)
3456 7e8004ba 2022-09-11 thomas return err;
3457 7e8004ba 2022-09-11 thomas }
3458 7e8004ba 2022-09-11 thomas
3459 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3460 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3461 7e8004ba 2022-09-11 thomas s->selected = 0;
3462 7e8004ba 2022-09-11 thomas
3463 7e8004ba 2022-09-11 thomas return NULL;
3464 7e8004ba 2022-09-11 thomas }
3465 7e8004ba 2022-09-11 thomas
3466 7e8004ba 2022-09-11 thomas static const struct got_error *
3467 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3468 60493ae3 2019-06-20 stsp {
3469 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3470 60493ae3 2019-06-20 stsp
3471 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3472 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3473 60493ae3 2019-06-20 stsp return NULL;
3474 60493ae3 2019-06-20 stsp }
3475 60493ae3 2019-06-20 stsp
3476 60493ae3 2019-06-20 stsp static const struct got_error *
3477 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3478 60493ae3 2019-06-20 stsp {
3479 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3480 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3481 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3482 60493ae3 2019-06-20 stsp
3483 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3484 f9686aa5 2020-03-27 stsp show_log_view(view);
3485 f9686aa5 2020-03-27 stsp update_panels();
3486 f9686aa5 2020-03-27 stsp doupdate();
3487 f9686aa5 2020-03-27 stsp
3488 96e2b566 2019-07-08 stsp if (s->search_entry) {
3489 13add988 2019-10-15 stsp int errcode, ch;
3490 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3491 13add988 2019-10-15 stsp if (errcode)
3492 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3493 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3494 13add988 2019-10-15 stsp ch = wgetch(view->window);
3495 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3496 13add988 2019-10-15 stsp if (errcode)
3497 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3498 13add988 2019-10-15 stsp "pthread_mutex_lock");
3499 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3500 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3501 678cbce5 2019-07-28 stsp return NULL;
3502 678cbce5 2019-07-28 stsp }
3503 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3504 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3505 96e2b566 2019-07-08 stsp else
3506 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3507 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3508 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3509 df5e841d 2022-06-23 thomas /*
3510 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3511 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3512 1f4d5162 2022-06-23 thomas * might have changed.
3513 df5e841d 2022-06-23 thomas */
3514 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3515 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3516 e7baaec8 2022-09-13 thomas else
3517 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3518 e7baaec8 2022-09-13 thomas entry);
3519 20be8d96 2019-06-21 stsp } else {
3520 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3521 20be8d96 2019-06-21 stsp }
3522 60493ae3 2019-06-20 stsp
3523 60493ae3 2019-06-20 stsp while (1) {
3524 13add988 2019-10-15 stsp int have_match = 0;
3525 13add988 2019-10-15 stsp
3526 60493ae3 2019-06-20 stsp if (entry == NULL) {
3527 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3528 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3529 f9967bca 2020-03-27 stsp view->search_next_done =
3530 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3531 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3532 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3533 f801134a 2019-06-25 stsp return NULL;
3534 60493ae3 2019-06-20 stsp }
3535 96e2b566 2019-07-08 stsp /*
3536 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3537 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3538 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3539 96e2b566 2019-07-08 stsp */
3540 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3541 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3542 60493ae3 2019-06-20 stsp }
3543 60493ae3 2019-06-20 stsp
3544 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3545 13add988 2019-10-15 stsp &view->regex);
3546 5943eee2 2019-08-13 stsp if (err)
3547 13add988 2019-10-15 stsp break;
3548 13add988 2019-10-15 stsp if (have_match) {
3549 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3550 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3551 60493ae3 2019-06-20 stsp break;
3552 60493ae3 2019-06-20 stsp }
3553 13add988 2019-10-15 stsp
3554 96e2b566 2019-07-08 stsp s->search_entry = entry;
3555 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3556 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3557 b1bf1435 2019-06-21 stsp else
3558 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3559 60493ae3 2019-06-20 stsp }
3560 60493ae3 2019-06-20 stsp
3561 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3562 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3563 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3564 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3565 60493ae3 2019-06-20 stsp if (err)
3566 60493ae3 2019-06-20 stsp return err;
3567 ead14cbe 2019-06-21 stsp cur++;
3568 ead14cbe 2019-06-21 stsp }
3569 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3570 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3571 60493ae3 2019-06-20 stsp if (err)
3572 60493ae3 2019-06-20 stsp return err;
3573 ead14cbe 2019-06-21 stsp cur--;
3574 60493ae3 2019-06-20 stsp }
3575 60493ae3 2019-06-20 stsp }
3576 60493ae3 2019-06-20 stsp
3577 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3578 96e2b566 2019-07-08 stsp
3579 60493ae3 2019-06-20 stsp return NULL;
3580 60493ae3 2019-06-20 stsp }
3581 60493ae3 2019-06-20 stsp
3582 60493ae3 2019-06-20 stsp static const struct got_error *
3583 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3584 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3585 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3586 80ddbec8 2018-04-29 stsp {
3587 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3588 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3589 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3590 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3591 1a76625f 2018-10-22 stsp int errcode;
3592 80ddbec8 2018-04-29 stsp
3593 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3594 f135c941 2020-02-20 stsp free(s->in_repo_path);
3595 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3596 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3597 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3598 f135c941 2020-02-20 stsp }
3599 ecb28ae0 2018-07-16 stsp
3600 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3601 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3602 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3603 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3604 78756c87 2020-11-24 stsp
3605 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3606 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3607 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3608 7e8004ba 2022-09-11 thomas
3609 fb2756b9 2018-08-04 stsp s->repo = repo;
3610 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3611 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3612 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3613 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3614 9cd7cbd1 2020-12-07 stsp goto done;
3615 9cd7cbd1 2020-12-07 stsp }
3616 9cd7cbd1 2020-12-07 stsp }
3617 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3618 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3619 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3620 5036bf37 2018-09-24 stsp goto done;
3621 5036bf37 2018-09-24 stsp }
3622 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3623 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3624 e5a0f69f 2018-08-18 stsp
3625 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3626 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3627 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3628 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3629 11b20872 2019-11-08 stsp if (err)
3630 11b20872 2019-11-08 stsp goto done;
3631 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3632 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3633 11b20872 2019-11-08 stsp if (err) {
3634 11b20872 2019-11-08 stsp free_colors(&s->colors);
3635 11b20872 2019-11-08 stsp goto done;
3636 11b20872 2019-11-08 stsp }
3637 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3638 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3639 11b20872 2019-11-08 stsp if (err) {
3640 11b20872 2019-11-08 stsp free_colors(&s->colors);
3641 11b20872 2019-11-08 stsp goto done;
3642 11b20872 2019-11-08 stsp }
3643 11b20872 2019-11-08 stsp }
3644 11b20872 2019-11-08 stsp
3645 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3646 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3647 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3648 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3649 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3650 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3651 1a76625f 2018-10-22 stsp
3652 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3653 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3654 1af5eddf 2022-06-23 thomas if (err)
3655 1af5eddf 2022-06-23 thomas goto done;
3656 1af5eddf 2022-06-23 thomas }
3657 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3658 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3659 7cd52833 2022-06-23 thomas if (err)
3660 7cd52833 2022-06-23 thomas goto done;
3661 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3662 b672a97a 2020-01-27 stsp !s->log_branches);
3663 1a76625f 2018-10-22 stsp if (err)
3664 1a76625f 2018-10-22 stsp goto done;
3665 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3666 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3667 c5b78334 2020-01-12 stsp if (err)
3668 c5b78334 2020-01-12 stsp goto done;
3669 1a76625f 2018-10-22 stsp
3670 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3671 1a76625f 2018-10-22 stsp if (errcode) {
3672 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3673 1a76625f 2018-10-22 stsp goto done;
3674 1a76625f 2018-10-22 stsp }
3675 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3676 7c1452c1 2020-03-26 stsp if (errcode) {
3677 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3678 7c1452c1 2020-03-26 stsp goto done;
3679 7c1452c1 2020-03-26 stsp }
3680 1a76625f 2018-10-22 stsp
3681 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3682 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3683 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3684 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3685 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3686 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3687 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3688 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3689 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3690 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3691 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3692 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3693 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3694 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3695 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3696 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3697 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3698 ba4f502b 2018-08-04 stsp done:
3699 1a76625f 2018-10-22 stsp if (err)
3700 1a76625f 2018-10-22 stsp close_log_view(view);
3701 ba4f502b 2018-08-04 stsp return err;
3702 ba4f502b 2018-08-04 stsp }
3703 ba4f502b 2018-08-04 stsp
3704 e5a0f69f 2018-08-18 stsp static const struct got_error *
3705 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3706 ba4f502b 2018-08-04 stsp {
3707 f2f6d207 2020-11-24 stsp const struct got_error *err;
3708 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3709 ba4f502b 2018-08-04 stsp
3710 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3711 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3712 2b380cc8 2018-10-24 stsp &s->thread_args);
3713 2b380cc8 2018-10-24 stsp if (errcode)
3714 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3715 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3716 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3717 f2f6d207 2020-11-24 stsp if (err)
3718 f2f6d207 2020-11-24 stsp return err;
3719 f2f6d207 2020-11-24 stsp }
3720 2b380cc8 2018-10-24 stsp }
3721 2b380cc8 2018-10-24 stsp
3722 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3723 b31f89ff 2022-07-01 thomas }
3724 b31f89ff 2022-07-01 thomas
3725 b31f89ff 2022-07-01 thomas static void
3726 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3727 b31f89ff 2022-07-01 thomas {
3728 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3729 b31f89ff 2022-07-01 thomas
3730 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3731 b31f89ff 2022-07-01 thomas return;
3732 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3733 7e8004ba 2022-09-11 thomas view->count = 0;
3734 b31f89ff 2022-07-01 thomas
3735 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3736 b31f89ff 2022-07-01 thomas || home)
3737 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3738 b31f89ff 2022-07-01 thomas
3739 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3740 b31f89ff 2022-07-01 thomas --s->selected;
3741 b31f89ff 2022-07-01 thomas else
3742 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3743 b31f89ff 2022-07-01 thomas
3744 b31f89ff 2022-07-01 thomas select_commit(s);
3745 b31f89ff 2022-07-01 thomas return;
3746 b31f89ff 2022-07-01 thomas }
3747 b31f89ff 2022-07-01 thomas
3748 b31f89ff 2022-07-01 thomas static const struct got_error *
3749 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3750 b31f89ff 2022-07-01 thomas {
3751 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3752 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3753 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3754 b31f89ff 2022-07-01 thomas
3755 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3756 7e8004ba 2022-09-11 thomas return NULL;
3757 7e8004ba 2022-09-11 thomas
3758 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3759 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3760 b31f89ff 2022-07-01 thomas return NULL;
3761 b31f89ff 2022-07-01 thomas
3762 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3763 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3764 b31f89ff 2022-07-01 thomas
3765 7ed048bd 2022-08-12 thomas if (!page) {
3766 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3767 b31f89ff 2022-07-01 thomas ++s->selected;
3768 b31f89ff 2022-07-01 thomas else
3769 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3770 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3771 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3772 7ed048bd 2022-08-12 thomas int n;
3773 7ed048bd 2022-08-12 thomas
3774 7ed048bd 2022-08-12 thomas s->selected = 0;
3775 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3776 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3777 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3778 7ed048bd 2022-08-12 thomas if (entry == NULL)
3779 7ed048bd 2022-08-12 thomas break;
3780 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3781 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3782 7ed048bd 2022-08-12 thomas }
3783 7ed048bd 2022-08-12 thomas if (n > 0)
3784 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3785 b31f89ff 2022-07-01 thomas } else {
3786 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3787 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3788 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3789 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3790 bd3f8225 2022-08-12 thomas else
3791 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3792 b31f89ff 2022-07-01 thomas }
3793 b31f89ff 2022-07-01 thomas if (err)
3794 b31f89ff 2022-07-01 thomas return err;
3795 b31f89ff 2022-07-01 thomas
3796 a5d43cac 2022-07-01 thomas /*
3797 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3798 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3799 a5d43cac 2022-07-01 thomas */
3800 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3801 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3802 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3803 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3804 25100026 2022-08-13 thomas }
3805 a5d43cac 2022-07-01 thomas
3806 b31f89ff 2022-07-01 thomas select_commit(s);
3807 b31f89ff 2022-07-01 thomas
3808 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3809 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3810 b31f89ff 2022-07-01 thomas view->count = 0;
3811 b31f89ff 2022-07-01 thomas
3812 b31f89ff 2022-07-01 thomas return NULL;
3813 e5a0f69f 2018-08-18 stsp }
3814 04cc582a 2018-08-01 stsp
3815 a5d43cac 2022-07-01 thomas static void
3816 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3817 a5d43cac 2022-07-01 thomas {
3818 24415785 2022-07-03 thomas *x = 0;
3819 24415785 2022-07-03 thomas *y = 0;
3820 24415785 2022-07-03 thomas
3821 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3822 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3823 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3824 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3825 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3826 53d2bdd3 2022-07-10 thomas else
3827 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3828 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3829 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3830 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3831 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3832 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3833 53d2bdd3 2022-07-10 thomas else
3834 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3835 53d2bdd3 2022-07-10 thomas }
3836 a5d43cac 2022-07-01 thomas }
3837 a5d43cac 2022-07-01 thomas
3838 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3839 e5a0f69f 2018-08-18 stsp static const struct got_error *
3840 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3841 a5d43cac 2022-07-01 thomas {
3842 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3843 a5d43cac 2022-07-01 thomas
3844 a5d43cac 2022-07-01 thomas view->nlines = y;
3845 64486692 2022-07-07 thomas view->ncols = COLS;
3846 a5d43cac 2022-07-01 thomas err = view_resize(view);
3847 a5d43cac 2022-07-01 thomas if (err)
3848 a5d43cac 2022-07-01 thomas return err;
3849 a5d43cac 2022-07-01 thomas
3850 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3851 a5d43cac 2022-07-01 thomas
3852 a5d43cac 2022-07-01 thomas return err;
3853 07dd3ed3 2022-08-06 thomas }
3854 07dd3ed3 2022-08-06 thomas
3855 07dd3ed3 2022-08-06 thomas static const struct got_error *
3856 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3857 07dd3ed3 2022-08-06 thomas {
3858 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3859 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3860 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3861 25100026 2022-08-13 thomas
3862 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3863 25100026 2022-08-13 thomas return NULL;
3864 07dd3ed3 2022-08-06 thomas
3865 07dd3ed3 2022-08-06 thomas g = view->gline;
3866 07dd3ed3 2022-08-06 thomas view->gline = 0;
3867 07dd3ed3 2022-08-06 thomas
3868 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3869 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3870 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3871 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3872 07dd3ed3 2022-08-06 thomas select_commit(s);
3873 07dd3ed3 2022-08-06 thomas return NULL;
3874 07dd3ed3 2022-08-06 thomas }
3875 07dd3ed3 2022-08-06 thomas
3876 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3877 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3878 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3879 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3880 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3881 07dd3ed3 2022-08-06 thomas if (err)
3882 07dd3ed3 2022-08-06 thomas return err;
3883 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3884 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3885 07dd3ed3 2022-08-06 thomas
3886 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3887 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3888 07dd3ed3 2022-08-06 thomas
3889 07dd3ed3 2022-08-06 thomas select_commit(s);
3890 07dd3ed3 2022-08-06 thomas return NULL;
3891 c72de8ab 2023-02-03 thomas
3892 c72de8ab 2023-02-03 thomas }
3893 c72de8ab 2023-02-03 thomas
3894 c72de8ab 2023-02-03 thomas static void
3895 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
3896 c72de8ab 2023-02-03 thomas {
3897 07dd3ed3 2022-08-06 thomas
3898 c72de8ab 2023-02-03 thomas switch (ch) {
3899 c72de8ab 2023-02-03 thomas case KEY_LEFT:
3900 c72de8ab 2023-02-03 thomas case 'h':
3901 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
3902 c72de8ab 2023-02-03 thomas if (view->x <= 0)
3903 c72de8ab 2023-02-03 thomas view->count = 0;
3904 c72de8ab 2023-02-03 thomas break;
3905 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
3906 c72de8ab 2023-02-03 thomas case 'l':
3907 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
3908 c72de8ab 2023-02-03 thomas view->x += 2;
3909 c72de8ab 2023-02-03 thomas else
3910 c72de8ab 2023-02-03 thomas view->count = 0;
3911 c72de8ab 2023-02-03 thomas break;
3912 c72de8ab 2023-02-03 thomas case '0':
3913 c72de8ab 2023-02-03 thomas view->x = 0;
3914 c72de8ab 2023-02-03 thomas break;
3915 c72de8ab 2023-02-03 thomas case '$':
3916 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3917 c72de8ab 2023-02-03 thomas view->count = 0;
3918 c72de8ab 2023-02-03 thomas break;
3919 c72de8ab 2023-02-03 thomas default:
3920 c72de8ab 2023-02-03 thomas break;
3921 c72de8ab 2023-02-03 thomas }
3922 a5d43cac 2022-07-01 thomas }
3923 a5d43cac 2022-07-01 thomas
3924 a5d43cac 2022-07-01 thomas static const struct got_error *
3925 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3926 e5a0f69f 2018-08-18 stsp {
3927 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3928 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3929 7ed048bd 2022-08-12 thomas int eos, nscroll;
3930 80ddbec8 2018-04-29 stsp
3931 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3932 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3933 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3934 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3935 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3936 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3937 fb280deb 2021-08-30 stsp }
3938 c0be8933 2022-08-12 thomas if (err)
3939 c0be8933 2022-08-12 thomas return err;
3940 528dedf3 2021-08-30 stsp }
3941 068ab281 2022-07-01 thomas
3942 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3943 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3944 068ab281 2022-07-01 thomas --eos; /* border */
3945 07dd3ed3 2022-08-06 thomas
3946 07dd3ed3 2022-08-06 thomas if (view->gline)
3947 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3948 068ab281 2022-07-01 thomas
3949 528dedf3 2021-08-30 stsp switch (ch) {
3950 7e8004ba 2022-09-11 thomas case '&':
3951 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3952 7e8004ba 2022-09-11 thomas break;
3953 1e37a5c2 2019-05-12 jcs case 'q':
3954 1e37a5c2 2019-05-12 jcs s->quit = 1;
3955 05171be4 2022-06-23 thomas break;
3956 05171be4 2022-06-23 thomas case '0':
3957 05171be4 2022-06-23 thomas case '$':
3958 05171be4 2022-06-23 thomas case KEY_RIGHT:
3959 05171be4 2022-06-23 thomas case 'l':
3960 05171be4 2022-06-23 thomas case KEY_LEFT:
3961 05171be4 2022-06-23 thomas case 'h':
3962 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
3963 05171be4 2022-06-23 thomas break;
3964 1e37a5c2 2019-05-12 jcs case 'k':
3965 1e37a5c2 2019-05-12 jcs case KEY_UP:
3966 1e37a5c2 2019-05-12 jcs case '<':
3967 1e37a5c2 2019-05-12 jcs case ',':
3968 f7140bf5 2021-10-17 thomas case CTRL('p'):
3969 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3970 912a3f79 2021-08-30 j break;
3971 912a3f79 2021-08-30 j case 'g':
3972 aa7a1117 2023-01-09 thomas case '=':
3973 912a3f79 2021-08-30 j case KEY_HOME:
3974 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3975 07b0611c 2022-06-23 thomas view->count = 0;
3976 1e37a5c2 2019-05-12 jcs break;
3977 70f17a53 2022-06-13 thomas case CTRL('u'):
3978 23427b14 2022-06-23 thomas case 'u':
3979 70f17a53 2022-06-13 thomas nscroll /= 2;
3980 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3981 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3982 a4292ac5 2019-05-12 jcs case CTRL('b'):
3983 1c5e5faa 2022-06-23 thomas case 'b':
3984 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3985 1e37a5c2 2019-05-12 jcs break;
3986 1e37a5c2 2019-05-12 jcs case 'j':
3987 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3988 1e37a5c2 2019-05-12 jcs case '>':
3989 1e37a5c2 2019-05-12 jcs case '.':
3990 f7140bf5 2021-10-17 thomas case CTRL('n'):
3991 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3992 912a3f79 2021-08-30 j break;
3993 f69c5a46 2022-07-19 thomas case '@':
3994 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3995 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
3996 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
3997 f69c5a46 2022-07-19 thomas break;
3998 912a3f79 2021-08-30 j case 'G':
3999 aa7a1117 2023-01-09 thomas case '*':
4000 912a3f79 2021-08-30 j case KEY_END: {
4001 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
4002 912a3f79 2021-08-30 j * traverse them all. */
4003 07b0611c 2022-06-23 thomas view->count = 0;
4004 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
4005 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
4006 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
4007 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
4008 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
4009 1e37a5c2 2019-05-12 jcs break;
4010 912a3f79 2021-08-30 j }
4011 bccd1d5d 2022-06-13 thomas case CTRL('d'):
4012 23427b14 2022-06-23 thomas case 'd':
4013 70f17a53 2022-06-13 thomas nscroll /= 2;
4014 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4015 70f17a53 2022-06-13 thomas case KEY_NPAGE:
4016 1c5e5faa 2022-06-23 thomas case CTRL('f'):
4017 4c2d69cb 2022-06-23 thomas case 'f':
4018 b31f89ff 2022-07-01 thomas case ' ':
4019 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
4020 1e37a5c2 2019-05-12 jcs break;
4021 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4022 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
4023 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
4024 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
4025 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
4026 2b779855 2020-12-05 naddy select_commit(s);
4027 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
4028 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
4029 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
4030 7e8004ba 2022-09-11 thomas s->commits->ncommits;
4031 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
4032 0bf7f153 2020-12-02 naddy }
4033 1e37a5c2 2019-05-12 jcs break;
4034 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4035 444d5325 2022-07-03 thomas case '\r':
4036 07b0611c 2022-06-23 thomas view->count = 0;
4037 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4038 e5a0f69f 2018-08-18 stsp break;
4039 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
4040 1e37a5c2 2019-05-12 jcs break;
4041 1be4947a 2022-07-22 thomas case 'T':
4042 07b0611c 2022-06-23 thomas view->count = 0;
4043 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
4044 5036bf37 2018-09-24 stsp break;
4045 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
4046 1e37a5c2 2019-05-12 jcs break;
4047 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4048 21355643 2020-12-06 stsp case CTRL('l'):
4049 21355643 2020-12-06 stsp case 'B':
4050 07b0611c 2022-06-23 thomas view->count = 0;
4051 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
4052 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
4053 1e37a5c2 2019-05-12 jcs break;
4054 21355643 2020-12-06 stsp err = stop_log_thread(s);
4055 74cfe85e 2020-10-20 stsp if (err)
4056 74cfe85e 2020-10-20 stsp return err;
4057 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
4058 21355643 2020-12-06 stsp char *parent_path;
4059 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
4060 21355643 2020-12-06 stsp if (err)
4061 21355643 2020-12-06 stsp return err;
4062 21355643 2020-12-06 stsp free(s->in_repo_path);
4063 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
4064 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
4065 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
4066 21355643 2020-12-06 stsp struct got_object_id *start_id;
4067 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
4068 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
4069 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
4070 466429a1 2022-11-17 thomas if (err) {
4071 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
4072 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
4073 466429a1 2022-11-17 thomas return err;
4074 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
4075 466429a1 2022-11-17 thomas free(s->head_ref_name);
4076 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
4077 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
4078 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
4079 466429a1 2022-11-17 thomas &tog_refs, s->repo);
4080 466429a1 2022-11-17 thomas if (err)
4081 466429a1 2022-11-17 thomas return err;
4082 466429a1 2022-11-17 thomas }
4083 21355643 2020-12-06 stsp free(s->start_id);
4084 21355643 2020-12-06 stsp s->start_id = start_id;
4085 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
4086 21355643 2020-12-06 stsp } else /* 'B' */
4087 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
4088 21355643 2020-12-06 stsp
4089 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
4090 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
4091 bf7e79b3 2022-06-23 thomas if (err)
4092 bf7e79b3 2022-06-23 thomas return err;
4093 bf7e79b3 2022-06-23 thomas }
4094 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
4095 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
4096 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
4097 74cfe85e 2020-10-20 stsp if (err)
4098 21355643 2020-12-06 stsp return err;
4099 51a10b52 2020-12-26 stsp tog_free_refs();
4100 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
4101 ca51c541 2020-12-07 stsp if (err)
4102 ca51c541 2020-12-07 stsp return err;
4103 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
4104 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
4105 d01904d4 2019-06-25 stsp if (err)
4106 d01904d4 2019-06-25 stsp return err;
4107 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
4108 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
4109 b672a97a 2020-01-27 stsp if (err)
4110 b672a97a 2020-01-27 stsp return err;
4111 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
4112 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
4113 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
4114 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
4115 21355643 2020-12-06 stsp s->selected_entry = NULL;
4116 21355643 2020-12-06 stsp s->selected = 0;
4117 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
4118 21355643 2020-12-06 stsp s->quit = 0;
4119 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
4120 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
4121 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
4122 94ecf40d 2022-07-22 thomas view->offset = 0;
4123 d01904d4 2019-06-25 stsp break;
4124 1be4947a 2022-07-22 thomas case 'R':
4125 07b0611c 2022-06-23 thomas view->count = 0;
4126 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
4127 6458efa5 2020-11-24 stsp break;
4128 1e37a5c2 2019-05-12 jcs default:
4129 07b0611c 2022-06-23 thomas view->count = 0;
4130 1e37a5c2 2019-05-12 jcs break;
4131 899d86c2 2018-05-10 stsp }
4132 e5a0f69f 2018-08-18 stsp
4133 80ddbec8 2018-04-29 stsp return err;
4134 80ddbec8 2018-04-29 stsp }
4135 80ddbec8 2018-04-29 stsp
4136 4ed7e80c 2018-05-20 stsp static const struct got_error *
4137 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
4138 c2db6724 2019-01-04 stsp {
4139 c2db6724 2019-01-04 stsp const struct got_error *error;
4140 c2db6724 2019-01-04 stsp
4141 37c06ea4 2019-07-15 stsp #ifdef PROFILE
4142 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
4143 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
4144 37c06ea4 2019-07-15 stsp #endif
4145 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
4146 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
4147 c2db6724 2019-01-04 stsp
4148 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
4149 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
4150 c2db6724 2019-01-04 stsp
4151 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
4152 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
4153 c2db6724 2019-01-04 stsp
4154 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
4155 c2db6724 2019-01-04 stsp if (error != NULL)
4156 c2db6724 2019-01-04 stsp return error;
4157 c2db6724 2019-01-04 stsp
4158 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
4159 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
4160 c2db6724 2019-01-04 stsp
4161 c2db6724 2019-01-04 stsp return NULL;
4162 c2db6724 2019-01-04 stsp }
4163 c2db6724 2019-01-04 stsp
4164 b85a3496 2023-04-14 thomas static const struct got_error *
4165 b85a3496 2023-04-14 thomas init_mock_term(struct tog_io **tog_io, const char *test_script_path)
4166 b85a3496 2023-04-14 thomas {
4167 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4168 b85a3496 2023-04-14 thomas struct tog_io *io;
4169 b85a3496 2023-04-14 thomas
4170 b85a3496 2023-04-14 thomas if (*tog_io)
4171 b85a3496 2023-04-14 thomas *tog_io = 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 b85a3496 2023-04-14 thomas io = calloc(1, sizeof(*io));
4177 b85a3496 2023-04-14 thomas if (io == NULL)
4178 b85a3496 2023-04-14 thomas return got_error_from_errno("calloc");
4179 b85a3496 2023-04-14 thomas
4180 b85a3496 2023-04-14 thomas io->f = fopen(test_script_path, "re");
4181 b85a3496 2023-04-14 thomas if (io->f == NULL) {
4182 b85a3496 2023-04-14 thomas err = got_error_from_errno_fmt("fopen: %s",
4183 b85a3496 2023-04-14 thomas test_script_path);
4184 b85a3496 2023-04-14 thomas goto done;
4185 b85a3496 2023-04-14 thomas }
4186 b85a3496 2023-04-14 thomas
4187 b85a3496 2023-04-14 thomas /* test mode, we don't want any output */
4188 b85a3496 2023-04-14 thomas io->cout = fopen("/dev/null", "w+");
4189 b85a3496 2023-04-14 thomas if (io->cout == NULL) {
4190 b85a3496 2023-04-14 thomas err = got_error_from_errno("fopen: /dev/null");
4191 b85a3496 2023-04-14 thomas goto done;
4192 b85a3496 2023-04-14 thomas }
4193 b85a3496 2023-04-14 thomas
4194 b85a3496 2023-04-14 thomas io->cin = fopen("/dev/tty", "r+");
4195 b85a3496 2023-04-14 thomas if (io->cin == NULL) {
4196 b85a3496 2023-04-14 thomas err = got_error_from_errno("fopen: /dev/tty");
4197 b85a3496 2023-04-14 thomas goto done;
4198 b85a3496 2023-04-14 thomas }
4199 b85a3496 2023-04-14 thomas
4200 b85a3496 2023-04-14 thomas if (fseeko(io->f, 0L, SEEK_SET) == -1) {
4201 b85a3496 2023-04-14 thomas err = got_error_from_errno("fseeko");
4202 b85a3496 2023-04-14 thomas goto done;
4203 b85a3496 2023-04-14 thomas }
4204 b85a3496 2023-04-14 thomas
4205 b85a3496 2023-04-14 thomas /*
4206 b85a3496 2023-04-14 thomas * XXX Perhaps we should define "xterm" as the terminal
4207 b85a3496 2023-04-14 thomas * type for standardised testing instead of using $TERM?
4208 b85a3496 2023-04-14 thomas */
4209 b85a3496 2023-04-14 thomas if (newterm(NULL, io->cout, io->cin) == NULL)
4210 b85a3496 2023-04-14 thomas err = got_error_msg(GOT_ERR_IO,
4211 b85a3496 2023-04-14 thomas "newterm: failed to initialise curses");
4212 b85a3496 2023-04-14 thomas done:
4213 b85a3496 2023-04-14 thomas if (err)
4214 b85a3496 2023-04-14 thomas tog_io_close(io);
4215 b85a3496 2023-04-14 thomas else
4216 b85a3496 2023-04-14 thomas *tog_io = io;
4217 b85a3496 2023-04-14 thomas return err;
4218 b85a3496 2023-04-14 thomas }
4219 b85a3496 2023-04-14 thomas
4220 b85a3496 2023-04-14 thomas static const struct got_error *
4221 b85a3496 2023-04-14 thomas init_curses(struct tog_io **tog_io)
4222 a915003a 2019-02-05 stsp {
4223 b85a3496 2023-04-14 thomas const struct got_error *err = NULL;
4224 b85a3496 2023-04-14 thomas const char *test_script_path;
4225 b85a3496 2023-04-14 thomas
4226 296152d1 2022-05-31 thomas /*
4227 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
4228 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
4229 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
4230 296152d1 2022-05-31 thomas */
4231 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
4232 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
4233 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
4234 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
4235 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
4236 296152d1 2022-05-31 thomas
4237 b85a3496 2023-04-14 thomas test_script_path = getenv("GOT_TOG_TEST");
4238 b85a3496 2023-04-14 thomas if (test_script_path != NULL) {
4239 b85a3496 2023-04-14 thomas err = init_mock_term(tog_io, test_script_path);
4240 b85a3496 2023-04-14 thomas if (err)
4241 b85a3496 2023-04-14 thomas return err;
4242 b85a3496 2023-04-14 thomas } else
4243 b85a3496 2023-04-14 thomas initscr();
4244 b85a3496 2023-04-14 thomas
4245 a915003a 2019-02-05 stsp cbreak();
4246 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
4247 a915003a 2019-02-05 stsp noecho();
4248 a915003a 2019-02-05 stsp nonl();
4249 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
4250 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
4251 a915003a 2019-02-05 stsp curs_set(0);
4252 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
4253 6d17833f 2019-11-08 stsp start_color();
4254 6d17833f 2019-11-08 stsp use_default_colors();
4255 6d17833f 2019-11-08 stsp }
4256 b85a3496 2023-04-14 thomas
4257 b85a3496 2023-04-14 thomas return NULL;
4258 a915003a 2019-02-05 stsp }
4259 a915003a 2019-02-05 stsp
4260 c2db6724 2019-01-04 stsp static const struct got_error *
4261 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4262 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4263 f135c941 2020-02-20 stsp {
4264 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4265 f135c941 2020-02-20 stsp
4266 f135c941 2020-02-20 stsp if (argc == 0) {
4267 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4268 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4269 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4270 f135c941 2020-02-20 stsp return NULL;
4271 f135c941 2020-02-20 stsp }
4272 f135c941 2020-02-20 stsp
4273 f135c941 2020-02-20 stsp if (worktree) {
4274 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4275 bfd61697 2020-11-14 stsp char *p;
4276 f135c941 2020-02-20 stsp
4277 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4278 f135c941 2020-02-20 stsp if (err)
4279 f135c941 2020-02-20 stsp return err;
4280 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4281 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4282 bfd61697 2020-11-14 stsp p) == -1) {
4283 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4284 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4285 f135c941 2020-02-20 stsp }
4286 f135c941 2020-02-20 stsp free(p);
4287 f135c941 2020-02-20 stsp } else
4288 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4289 f135c941 2020-02-20 stsp
4290 f135c941 2020-02-20 stsp return err;
4291 f135c941 2020-02-20 stsp }
4292 f135c941 2020-02-20 stsp
4293 f135c941 2020-02-20 stsp static const struct got_error *
4294 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4295 9f7d7167 2018-04-29 stsp {
4296 b85a3496 2023-04-14 thomas const struct got_error *io_err, *error;
4297 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4298 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4299 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4300 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4301 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4302 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4303 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4304 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4305 04cc582a 2018-08-01 stsp struct tog_view *view;
4306 b85a3496 2023-04-14 thomas struct tog_io *tog_io = NULL;
4307 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4308 80ddbec8 2018-04-29 stsp
4309 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4310 80ddbec8 2018-04-29 stsp switch (ch) {
4311 b672a97a 2020-01-27 stsp case 'b':
4312 b672a97a 2020-01-27 stsp log_branches = 1;
4313 b672a97a 2020-01-27 stsp break;
4314 80ddbec8 2018-04-29 stsp case 'c':
4315 80ddbec8 2018-04-29 stsp start_commit = optarg;
4316 80ddbec8 2018-04-29 stsp break;
4317 ecb28ae0 2018-07-16 stsp case 'r':
4318 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4319 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4320 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4321 9ba1d308 2019-10-21 stsp optarg);
4322 ecb28ae0 2018-07-16 stsp break;
4323 80ddbec8 2018-04-29 stsp default:
4324 17020d27 2019-03-07 stsp usage_log();
4325 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4326 80ddbec8 2018-04-29 stsp }
4327 80ddbec8 2018-04-29 stsp }
4328 80ddbec8 2018-04-29 stsp
4329 80ddbec8 2018-04-29 stsp argc -= optind;
4330 80ddbec8 2018-04-29 stsp argv += optind;
4331 80ddbec8 2018-04-29 stsp
4332 f135c941 2020-02-20 stsp if (argc > 1)
4333 f135c941 2020-02-20 stsp usage_log();
4334 963f97a1 2019-03-18 stsp
4335 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4336 7cd52833 2022-06-23 thomas if (error != NULL)
4337 7cd52833 2022-06-23 thomas goto done;
4338 7cd52833 2022-06-23 thomas
4339 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4340 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4341 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4342 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4343 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4344 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4345 c156c7a4 2020-12-18 stsp goto done;
4346 a1fbf39a 2019-08-11 stsp if (worktree)
4347 6962eb72 2020-02-20 stsp repo_path =
4348 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4349 a1fbf39a 2019-08-11 stsp else
4350 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4351 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4352 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4353 c156c7a4 2020-12-18 stsp goto done;
4354 c156c7a4 2020-12-18 stsp }
4355 ecb28ae0 2018-07-16 stsp }
4356 ecb28ae0 2018-07-16 stsp
4357 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4358 80ddbec8 2018-04-29 stsp if (error != NULL)
4359 ecb28ae0 2018-07-16 stsp goto done;
4360 80ddbec8 2018-04-29 stsp
4361 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4362 f135c941 2020-02-20 stsp repo, worktree);
4363 f135c941 2020-02-20 stsp if (error)
4364 f135c941 2020-02-20 stsp goto done;
4365 f135c941 2020-02-20 stsp
4366 b85a3496 2023-04-14 thomas error = init_curses(&tog_io);
4367 b85a3496 2023-04-14 thomas if (error)
4368 b85a3496 2023-04-14 thomas goto done;
4369 f135c941 2020-02-20 stsp
4370 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4371 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4372 c02c541e 2019-03-29 stsp if (error)
4373 c02c541e 2019-03-29 stsp goto done;
4374 c02c541e 2019-03-29 stsp
4375 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4376 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4377 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4378 87670572 2020-12-26 naddy if (error)
4379 87670572 2020-12-26 naddy goto done;
4380 87670572 2020-12-26 naddy }
4381 51a10b52 2020-12-26 stsp
4382 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4383 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4384 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4385 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4386 d8f38dc4 2020-12-05 stsp if (error)
4387 d8f38dc4 2020-12-05 stsp goto done;
4388 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4389 d8f38dc4 2020-12-05 stsp } else {
4390 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4391 d8f38dc4 2020-12-05 stsp if (error == NULL)
4392 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4393 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4394 d8f38dc4 2020-12-05 stsp goto done;
4395 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4396 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4397 d8f38dc4 2020-12-05 stsp if (error)
4398 d8f38dc4 2020-12-05 stsp goto done;
4399 d8f38dc4 2020-12-05 stsp }
4400 8b473291 2019-02-21 stsp
4401 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4402 04cc582a 2018-08-01 stsp if (view == NULL) {
4403 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4404 04cc582a 2018-08-01 stsp goto done;
4405 04cc582a 2018-08-01 stsp }
4406 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4407 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4408 ba4f502b 2018-08-04 stsp if (error)
4409 ba4f502b 2018-08-04 stsp goto done;
4410 2fc00ff4 2019-08-31 stsp if (worktree) {
4411 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4412 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4413 2fc00ff4 2019-08-31 stsp worktree = NULL;
4414 2fc00ff4 2019-08-31 stsp }
4415 b85a3496 2023-04-14 thomas error = view_loop(view, tog_io);
4416 ecb28ae0 2018-07-16 stsp done:
4417 f135c941 2020-02-20 stsp free(in_repo_path);
4418 ecb28ae0 2018-07-16 stsp free(repo_path);
4419 ecb28ae0 2018-07-16 stsp free(cwd);
4420 899d86c2 2018-05-10 stsp free(start_id);
4421 d8f38dc4 2020-12-05 stsp free(label);
4422 d8f38dc4 2020-12-05 stsp if (ref)
4423 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4424 1d0f4054 2021-06-17 stsp if (repo) {
4425 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4426 1d0f4054 2021-06-17 stsp if (error == NULL)
4427 1d0f4054 2021-06-17 stsp error = close_err;
4428 1d0f4054 2021-06-17 stsp }
4429 ec142235 2019-03-07 stsp if (worktree)
4430 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4431 7cd52833 2022-06-23 thomas if (pack_fds) {
4432 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4433 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4434 7cd52833 2022-06-23 thomas if (error == NULL)
4435 7cd52833 2022-06-23 thomas error = pack_err;
4436 b85a3496 2023-04-14 thomas }
4437 b85a3496 2023-04-14 thomas if (tog_io != NULL) {
4438 b85a3496 2023-04-14 thomas io_err = tog_io_close(tog_io);
4439 b85a3496 2023-04-14 thomas if (error == NULL)
4440 b85a3496 2023-04-14 thomas error = io_err;
4441 7cd52833 2022-06-23 thomas }
4442 51a10b52 2020-12-26 stsp tog_free_refs();
4443 80ddbec8 2018-04-29 stsp return error;
4444 9f7d7167 2018-04-29 stsp }
4445 9f7d7167 2018-04-29 stsp
4446 4ed7e80c 2018-05-20 stsp __dead static void
4447 9f7d7167 2018-04-29 stsp usage_diff(void)
4448 9f7d7167 2018-04-29 stsp {
4449 80ddbec8 2018-04-29 stsp endwin();
4450 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4451 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4452 9f7d7167 2018-04-29 stsp exit(1);
4453 b304db33 2018-05-20 stsp }
4454 b304db33 2018-05-20 stsp
4455 6d17833f 2019-11-08 stsp static int
4456 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4457 41605754 2020-11-12 stsp regmatch_t *regmatch)
4458 6d17833f 2019-11-08 stsp {
4459 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4460 6d17833f 2019-11-08 stsp }
4461 6d17833f 2019-11-08 stsp
4462 ef20f542 2022-06-26 thomas static struct tog_color *
4463 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4464 6d17833f 2019-11-08 stsp {
4465 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4466 6d17833f 2019-11-08 stsp
4467 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4468 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4469 6d17833f 2019-11-08 stsp return tc;
4470 6d17833f 2019-11-08 stsp }
4471 6d17833f 2019-11-08 stsp
4472 6d17833f 2019-11-08 stsp return NULL;
4473 6d17833f 2019-11-08 stsp }
4474 6d17833f 2019-11-08 stsp
4475 4ed7e80c 2018-05-20 stsp static const struct got_error *
4476 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4477 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4478 41605754 2020-11-12 stsp {
4479 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4480 666f7b10 2022-06-23 thomas char *exstr = NULL;
4481 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4482 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4483 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4484 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4485 41605754 2020-11-12 stsp
4486 41605754 2020-11-12 stsp *wtotal = 0;
4487 cbea3800 2022-06-23 thomas
4488 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4489 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4490 41605754 2020-11-12 stsp
4491 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4492 666f7b10 2022-06-23 thomas if (err)
4493 666f7b10 2022-06-23 thomas return err;
4494 666f7b10 2022-06-23 thomas
4495 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4496 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4497 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4498 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4499 666f7b10 2022-06-23 thomas goto done;
4500 666f7b10 2022-06-23 thomas }
4501 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4502 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4503 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4504 cbea3800 2022-06-23 thomas goto done;
4505 cbea3800 2022-06-23 thomas }
4506 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4507 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4508 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4509 cbea3800 2022-06-23 thomas goto done;
4510 cbea3800 2022-06-23 thomas }
4511 05171be4 2022-06-23 thomas
4512 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4513 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 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 n = MAX(width0 - skipcol, 0);
4518 05171be4 2022-06-23 thomas if (n) {
4519 cbea3800 2022-06-23 thomas free(wline);
4520 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4521 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4522 cbea3800 2022-06-23 thomas if (err)
4523 cbea3800 2022-06-23 thomas goto done;
4524 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4525 cbea3800 2022-06-23 thomas wlimit -= width;
4526 cbea3800 2022-06-23 thomas *wtotal += width;
4527 41605754 2020-11-12 stsp }
4528 41605754 2020-11-12 stsp
4529 41605754 2020-11-12 stsp if (wlimit > 0) {
4530 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4531 cbea3800 2022-06-23 thomas size_t wlen;
4532 cbea3800 2022-06-23 thomas
4533 cbea3800 2022-06-23 thomas free(wline);
4534 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4535 cbea3800 2022-06-23 thomas col_tab_align, 1);
4536 cbea3800 2022-06-23 thomas if (err)
4537 cbea3800 2022-06-23 thomas goto done;
4538 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4539 cbea3800 2022-06-23 thomas while (i < wlen) {
4540 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4541 cbea3800 2022-06-23 thomas if (width == -1) {
4542 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4543 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4544 cbea3800 2022-06-23 thomas goto done;
4545 cbea3800 2022-06-23 thomas }
4546 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4547 cbea3800 2022-06-23 thomas break;
4548 1c5e5faa 2022-06-23 thomas w += width;
4549 cbea3800 2022-06-23 thomas i++;
4550 41605754 2020-11-12 stsp }
4551 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4552 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4553 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4554 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4555 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4556 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4557 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4558 41605754 2020-11-12 stsp }
4559 41605754 2020-11-12 stsp }
4560 41605754 2020-11-12 stsp
4561 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4562 cbea3800 2022-06-23 thomas free(wline);
4563 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4564 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4565 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4566 cbea3800 2022-06-23 thomas col_tab_align, 1);
4567 cbea3800 2022-06-23 thomas if (err)
4568 cbea3800 2022-06-23 thomas goto done;
4569 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4570 cbea3800 2022-06-23 thomas } else {
4571 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4572 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4573 cbea3800 2022-06-23 thomas if (err)
4574 cbea3800 2022-06-23 thomas goto done;
4575 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4576 cbea3800 2022-06-23 thomas }
4577 cbea3800 2022-06-23 thomas *wtotal += width2;
4578 41605754 2020-11-12 stsp }
4579 cbea3800 2022-06-23 thomas done:
4580 05171be4 2022-06-23 thomas free(wline);
4581 666f7b10 2022-06-23 thomas free(exstr);
4582 cbea3800 2022-06-23 thomas free(seg0);
4583 cbea3800 2022-06-23 thomas free(seg1);
4584 cbea3800 2022-06-23 thomas free(seg2);
4585 cbea3800 2022-06-23 thomas return err;
4586 41605754 2020-11-12 stsp }
4587 07dd3ed3 2022-08-06 thomas
4588 07dd3ed3 2022-08-06 thomas static int
4589 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4590 07dd3ed3 2022-08-06 thomas {
4591 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4592 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4593 07dd3ed3 2022-08-06 thomas
4594 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4595 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4596 fc2737d5 2022-09-15 thomas
4597 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4598 fc2737d5 2022-09-15 thomas selected = first;
4599 fc2737d5 2022-09-15 thomas eof = &s->eof;
4600 fc2737d5 2022-09-15 thomas f = s->f;
4601 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4602 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4603 41605754 2020-11-12 stsp
4604 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4605 07dd3ed3 2022-08-06 thomas selected = first;
4606 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4607 07dd3ed3 2022-08-06 thomas f = s->f;
4608 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4609 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4610 07dd3ed3 2022-08-06 thomas
4611 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4612 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4613 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4614 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4615 07dd3ed3 2022-08-06 thomas } else
4616 07dd3ed3 2022-08-06 thomas return 0;
4617 07dd3ed3 2022-08-06 thomas
4618 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4619 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4620 07dd3ed3 2022-08-06 thomas return 0;
4621 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4622 07dd3ed3 2022-08-06 thomas rewind(f);
4623 07dd3ed3 2022-08-06 thomas *eof = 0;
4624 07dd3ed3 2022-08-06 thomas *first = 1;
4625 07dd3ed3 2022-08-06 thomas *lineno = 0;
4626 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4627 07dd3ed3 2022-08-06 thomas return 0;
4628 07dd3ed3 2022-08-06 thomas }
4629 07dd3ed3 2022-08-06 thomas
4630 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4631 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4632 07dd3ed3 2022-08-06 thomas view->gline = 0;
4633 07dd3ed3 2022-08-06 thomas
4634 07dd3ed3 2022-08-06 thomas return 1;
4635 07dd3ed3 2022-08-06 thomas }
4636 07dd3ed3 2022-08-06 thomas
4637 41605754 2020-11-12 stsp static const struct got_error *
4638 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4639 26ed57b2 2018-05-19 stsp {
4640 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4641 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4642 61e69b96 2018-05-20 stsp const struct got_error *err;
4643 82c78e96 2022-08-06 thomas int nprinted = 0;
4644 b304db33 2018-05-20 stsp char *line;
4645 826082fe 2020-12-10 stsp size_t linesize = 0;
4646 826082fe 2020-12-10 stsp ssize_t linelen;
4647 61e69b96 2018-05-20 stsp wchar_t *wline;
4648 e0b650dd 2018-05-20 stsp int width;
4649 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4650 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4651 fe621944 2020-11-10 stsp off_t line_offset;
4652 26ed57b2 2018-05-19 stsp
4653 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4654 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4655 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4656 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4657 fe621944 2020-11-10 stsp
4658 f7d12f7e 2018-08-01 stsp werase(view->window);
4659 a3404814 2018-09-02 stsp
4660 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4661 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4662 07dd3ed3 2022-08-06 thomas
4663 a3404814 2018-09-02 stsp if (header) {
4664 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4665 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4666 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4667 07dd3ed3 2022-08-06 thomas
4668 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4669 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4670 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4671 f91a2b48 2022-06-23 thomas 0, 0);
4672 135a2da0 2020-11-11 stsp free(line);
4673 135a2da0 2020-11-11 stsp if (err)
4674 a3404814 2018-09-02 stsp return err;
4675 a3404814 2018-09-02 stsp
4676 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4677 a3404814 2018-09-02 stsp wstandout(view->window);
4678 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4679 e54cc94a 2020-11-11 stsp free(wline);
4680 e54cc94a 2020-11-11 stsp wline = NULL;
4681 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4682 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4683 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4684 a3404814 2018-09-02 stsp wstandend(view->window);
4685 26ed57b2 2018-05-19 stsp
4686 a3404814 2018-09-02 stsp if (max_lines <= 1)
4687 a3404814 2018-09-02 stsp return NULL;
4688 a3404814 2018-09-02 stsp max_lines--;
4689 a3404814 2018-09-02 stsp }
4690 a3404814 2018-09-02 stsp
4691 89f1a395 2020-12-01 naddy s->eof = 0;
4692 05171be4 2022-06-23 thomas view->maxx = 0;
4693 826082fe 2020-12-10 stsp line = NULL;
4694 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4695 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4696 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4697 6568f0aa 2022-08-06 thomas
4698 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4699 826082fe 2020-12-10 stsp if (linelen == -1) {
4700 826082fe 2020-12-10 stsp if (feof(s->f)) {
4701 826082fe 2020-12-10 stsp s->eof = 1;
4702 826082fe 2020-12-10 stsp break;
4703 826082fe 2020-12-10 stsp }
4704 826082fe 2020-12-10 stsp free(line);
4705 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4706 61e69b96 2018-05-20 stsp }
4707 05171be4 2022-06-23 thomas
4708 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4709 07dd3ed3 2022-08-06 thomas continue;
4710 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4711 07dd3ed3 2022-08-06 thomas continue;
4712 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4713 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4714 07dd3ed3 2022-08-06 thomas
4715 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4716 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4717 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4718 cbea3800 2022-06-23 thomas if (err) {
4719 cbea3800 2022-06-23 thomas free(line);
4720 cbea3800 2022-06-23 thomas return err;
4721 cbea3800 2022-06-23 thomas }
4722 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4723 cbea3800 2022-06-23 thomas free(wline);
4724 cbea3800 2022-06-23 thomas wline = NULL;
4725 cbea3800 2022-06-23 thomas
4726 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4727 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4728 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4729 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4730 07dd3ed3 2022-08-06 thomas if (attr)
4731 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4732 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4733 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4734 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4735 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4736 41605754 2020-11-12 stsp if (err) {
4737 41605754 2020-11-12 stsp free(line);
4738 41605754 2020-11-12 stsp return err;
4739 41605754 2020-11-12 stsp }
4740 41605754 2020-11-12 stsp } else {
4741 f1c0ec19 2022-06-23 thomas int skip;
4742 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4743 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4744 f1c0ec19 2022-06-23 thomas if (err) {
4745 f1c0ec19 2022-06-23 thomas free(line);
4746 f1c0ec19 2022-06-23 thomas return err;
4747 f1c0ec19 2022-06-23 thomas }
4748 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4749 f1c0ec19 2022-06-23 thomas free(wline);
4750 41605754 2020-11-12 stsp wline = NULL;
4751 41605754 2020-11-12 stsp }
4752 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4753 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4754 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4755 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4756 07dd3ed3 2022-08-06 thomas } else {
4757 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4758 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4759 07dd3ed3 2022-08-06 thomas }
4760 07dd3ed3 2022-08-06 thomas if (attr)
4761 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4762 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4763 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4764 826082fe 2020-12-10 stsp }
4765 826082fe 2020-12-10 stsp free(line);
4766 fe621944 2020-11-10 stsp if (nprinted >= 1)
4767 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4768 89f1a395 2020-12-01 naddy (nprinted - 1);
4769 fe621944 2020-11-10 stsp else
4770 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4771 26ed57b2 2018-05-19 stsp
4772 a5d43cac 2022-07-01 thomas view_border(view);
4773 c3e9aa98 2019-05-13 jcs
4774 89f1a395 2020-12-01 naddy if (s->eof) {
4775 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4776 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4777 c3e9aa98 2019-05-13 jcs nprinted++;
4778 c3e9aa98 2019-05-13 jcs }
4779 c3e9aa98 2019-05-13 jcs
4780 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4781 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4782 c3e9aa98 2019-05-13 jcs if (err) {
4783 c3e9aa98 2019-05-13 jcs return err;
4784 c3e9aa98 2019-05-13 jcs }
4785 26ed57b2 2018-05-19 stsp
4786 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4787 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4788 e54cc94a 2020-11-11 stsp free(wline);
4789 e54cc94a 2020-11-11 stsp wline = NULL;
4790 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4791 c3e9aa98 2019-05-13 jcs }
4792 c3e9aa98 2019-05-13 jcs
4793 26ed57b2 2018-05-19 stsp return NULL;
4794 abd2672a 2018-12-23 stsp }
4795 abd2672a 2018-12-23 stsp
4796 abd2672a 2018-12-23 stsp static char *
4797 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4798 abd2672a 2018-12-23 stsp {
4799 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4800 09867e48 2019-08-13 stsp char *p, *s;
4801 09867e48 2019-08-13 stsp
4802 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4803 09867e48 2019-08-13 stsp if (tm == NULL)
4804 09867e48 2019-08-13 stsp return NULL;
4805 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4806 09867e48 2019-08-13 stsp if (s == NULL)
4807 09867e48 2019-08-13 stsp return NULL;
4808 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4809 abd2672a 2018-12-23 stsp if (p)
4810 abd2672a 2018-12-23 stsp *p = '\0';
4811 abd2672a 2018-12-23 stsp return s;
4812 9f7d7167 2018-04-29 stsp }
4813 9f7d7167 2018-04-29 stsp
4814 4ed7e80c 2018-05-20 stsp static const struct got_error *
4815 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4816 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4817 abd2672a 2018-12-23 stsp {
4818 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4819 fe621944 2020-11-10 stsp
4820 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4821 fe621944 2020-11-10 stsp if (p == NULL)
4822 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4823 82c78e96 2022-08-06 thomas *lines = p;
4824 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4825 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4826 fe621944 2020-11-10 stsp (*nlines)++;
4827 82c78e96 2022-08-06 thomas
4828 fe621944 2020-11-10 stsp return NULL;
4829 fe621944 2020-11-10 stsp }
4830 fe621944 2020-11-10 stsp
4831 fe621944 2020-11-10 stsp static const struct got_error *
4832 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4833 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
4834 be97ab03 2023-01-19 thomas {
4835 be97ab03 2023-01-19 thomas struct got_diff_line *p;
4836 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
4837 be97ab03 2023-01-19 thomas size_t i, r;
4838 be97ab03 2023-01-19 thomas
4839 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
4840 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
4841 be97ab03 2023-01-19 thomas
4842 be97ab03 2023-01-19 thomas for (;;) {
4843 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
4844 be97ab03 2023-01-19 thomas if (r == 0) {
4845 be97ab03 2023-01-19 thomas if (ferror(src))
4846 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
4847 be97ab03 2023-01-19 thomas if (feof(src))
4848 be97ab03 2023-01-19 thomas break;
4849 be97ab03 2023-01-19 thomas }
4850 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
4851 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
4852 be97ab03 2023-01-19 thomas }
4853 be97ab03 2023-01-19 thomas
4854 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
4855 9382c10a 2023-02-23 thomas return NULL;
4856 9382c10a 2023-02-23 thomas
4857 be97ab03 2023-01-19 thomas /*
4858 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
4859 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
4860 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
4861 be97ab03 2023-01-19 thomas */
4862 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
4863 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
4864 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4865 be97ab03 2023-01-19 thomas
4866 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
4867 9382c10a 2023-02-23 thomas --s_nlines;
4868 9382c10a 2023-02-23 thomas ++s_lines;
4869 9382c10a 2023-02-23 thomas }
4870 9382c10a 2023-02-23 thomas }
4871 be97ab03 2023-01-19 thomas
4872 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4873 be97ab03 2023-01-19 thomas if (p == NULL) {
4874 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
4875 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
4876 be97ab03 2023-01-19 thomas }
4877 be97ab03 2023-01-19 thomas
4878 be97ab03 2023-01-19 thomas *d_lines = p;
4879 be97ab03 2023-01-19 thomas
4880 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4881 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
4882 be97ab03 2023-01-19 thomas
4883 be97ab03 2023-01-19 thomas return NULL;
4884 be97ab03 2023-01-19 thomas }
4885 be97ab03 2023-01-19 thomas
4886 be97ab03 2023-01-19 thomas static const struct got_error *
4887 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4888 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4889 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4890 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
4891 fe621944 2020-11-10 stsp {
4892 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4893 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4894 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4895 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4896 45d799e2 2018-12-23 stsp time_t committer_time;
4897 45d799e2 2018-12-23 stsp const char *author, *committer;
4898 8b473291 2019-02-21 stsp char *refs_str = NULL;
4899 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4900 fe621944 2020-11-10 stsp off_t outoff = 0;
4901 fe621944 2020-11-10 stsp int n;
4902 abd2672a 2018-12-23 stsp
4903 8b473291 2019-02-21 stsp if (refs) {
4904 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4905 8b473291 2019-02-21 stsp if (err)
4906 8b473291 2019-02-21 stsp return err;
4907 8b473291 2019-02-21 stsp }
4908 8b473291 2019-02-21 stsp
4909 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4910 abd2672a 2018-12-23 stsp if (err)
4911 abd2672a 2018-12-23 stsp return err;
4912 abd2672a 2018-12-23 stsp
4913 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4914 15a94983 2018-12-23 stsp if (err) {
4915 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4916 15a94983 2018-12-23 stsp goto done;
4917 15a94983 2018-12-23 stsp }
4918 abd2672a 2018-12-23 stsp
4919 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4920 fe621944 2020-11-10 stsp if (err)
4921 fe621944 2020-11-10 stsp goto done;
4922 fe621944 2020-11-10 stsp
4923 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4924 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4925 fe621944 2020-11-10 stsp if (n < 0) {
4926 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4927 abd2672a 2018-12-23 stsp goto done;
4928 abd2672a 2018-12-23 stsp }
4929 fe621944 2020-11-10 stsp outoff += n;
4930 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4931 fe621944 2020-11-10 stsp if (err)
4932 fe621944 2020-11-10 stsp goto done;
4933 fe621944 2020-11-10 stsp
4934 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4935 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4936 fe621944 2020-11-10 stsp if (n < 0) {
4937 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4938 abd2672a 2018-12-23 stsp goto done;
4939 abd2672a 2018-12-23 stsp }
4940 fe621944 2020-11-10 stsp outoff += n;
4941 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4942 fe621944 2020-11-10 stsp if (err)
4943 fe621944 2020-11-10 stsp goto done;
4944 fe621944 2020-11-10 stsp
4945 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
4946 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
4947 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
4948 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
4949 fe621944 2020-11-10 stsp if (n < 0) {
4950 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4951 fe621944 2020-11-10 stsp goto done;
4952 fe621944 2020-11-10 stsp }
4953 fe621944 2020-11-10 stsp outoff += n;
4954 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4955 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
4956 fe621944 2020-11-10 stsp if (err)
4957 fe621944 2020-11-10 stsp goto done;
4958 abd2672a 2018-12-23 stsp }
4959 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
4960 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
4961 48830929 2023-01-07 thomas if (datestr) {
4962 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
4963 fe621944 2020-11-10 stsp if (n < 0) {
4964 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4965 fe621944 2020-11-10 stsp goto done;
4966 fe621944 2020-11-10 stsp }
4967 fe621944 2020-11-10 stsp outoff += n;
4968 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4969 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
4970 fe621944 2020-11-10 stsp if (err)
4971 fe621944 2020-11-10 stsp goto done;
4972 abd2672a 2018-12-23 stsp }
4973 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4974 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4975 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4976 ac4dc263 2021-09-24 thomas int pn = 1;
4977 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4978 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4979 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4980 ac4dc263 2021-09-24 thomas if (err)
4981 ac4dc263 2021-09-24 thomas goto done;
4982 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4983 ac4dc263 2021-09-24 thomas if (n < 0) {
4984 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4985 ac4dc263 2021-09-24 thomas goto done;
4986 ac4dc263 2021-09-24 thomas }
4987 ac4dc263 2021-09-24 thomas outoff += n;
4988 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4989 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4990 ac4dc263 2021-09-24 thomas if (err)
4991 ac4dc263 2021-09-24 thomas goto done;
4992 ac4dc263 2021-09-24 thomas free(id_str);
4993 ac4dc263 2021-09-24 thomas id_str = NULL;
4994 ac4dc263 2021-09-24 thomas }
4995 ac4dc263 2021-09-24 thomas }
4996 ac4dc263 2021-09-24 thomas
4997 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4998 5943eee2 2019-08-13 stsp if (err)
4999 5943eee2 2019-08-13 stsp goto done;
5000 fe621944 2020-11-10 stsp s = logmsg;
5001 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
5002 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
5003 fe621944 2020-11-10 stsp if (n < 0) {
5004 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5005 fe621944 2020-11-10 stsp goto done;
5006 fe621944 2020-11-10 stsp }
5007 fe621944 2020-11-10 stsp outoff += n;
5008 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5009 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
5010 fe621944 2020-11-10 stsp if (err)
5011 fe621944 2020-11-10 stsp goto done;
5012 abd2672a 2018-12-23 stsp }
5013 fe621944 2020-11-10 stsp
5014 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
5015 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
5016 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
5017 772fcad5 2023-01-07 thomas
5018 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
5019 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
5020 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
5021 fe621944 2020-11-10 stsp if (n < 0) {
5022 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
5023 fe621944 2020-11-10 stsp goto done;
5024 fe621944 2020-11-10 stsp }
5025 fe621944 2020-11-10 stsp outoff += n;
5026 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
5027 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
5028 fe621944 2020-11-10 stsp if (err)
5029 fe621944 2020-11-10 stsp goto done;
5030 0208f208 2020-05-05 stsp }
5031 fe621944 2020-11-10 stsp
5032 0208f208 2020-05-05 stsp fputc('\n', outfile);
5033 fe621944 2020-11-10 stsp outoff++;
5034 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5035 772fcad5 2023-01-07 thomas if (err)
5036 772fcad5 2023-01-07 thomas goto done;
5037 772fcad5 2023-01-07 thomas
5038 772fcad5 2023-01-07 thomas n = fprintf(outfile,
5039 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
5040 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
5041 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
5042 772fcad5 2023-01-07 thomas if (n < 0) {
5043 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
5044 772fcad5 2023-01-07 thomas goto done;
5045 772fcad5 2023-01-07 thomas }
5046 772fcad5 2023-01-07 thomas outoff += n;
5047 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5048 772fcad5 2023-01-07 thomas if (err)
5049 772fcad5 2023-01-07 thomas goto done;
5050 772fcad5 2023-01-07 thomas
5051 772fcad5 2023-01-07 thomas fputc('\n', outfile);
5052 772fcad5 2023-01-07 thomas outoff++;
5053 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
5054 abd2672a 2018-12-23 stsp done:
5055 abd2672a 2018-12-23 stsp free(id_str);
5056 5943eee2 2019-08-13 stsp free(logmsg);
5057 8b473291 2019-02-21 stsp free(refs_str);
5058 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5059 7510f233 2020-08-09 stsp if (err) {
5060 82c78e96 2022-08-06 thomas free(*lines);
5061 82c78e96 2022-08-06 thomas *lines = NULL;
5062 ae6a6978 2020-08-09 stsp *nlines = 0;
5063 7510f233 2020-08-09 stsp }
5064 fe621944 2020-11-10 stsp return err;
5065 abd2672a 2018-12-23 stsp }
5066 abd2672a 2018-12-23 stsp
5067 abd2672a 2018-12-23 stsp static const struct got_error *
5068 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
5069 26ed57b2 2018-05-19 stsp {
5070 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
5071 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
5072 15a94983 2018-12-23 stsp int obj_type;
5073 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
5074 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
5075 fe621944 2020-11-10 stsp
5076 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
5077 be97ab03 2023-01-19 thomas
5078 82c78e96 2022-08-06 thomas free(s->lines);
5079 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
5080 82c78e96 2022-08-06 thomas if (s->lines == NULL)
5081 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
5082 fe621944 2020-11-10 stsp s->nlines = 0;
5083 26ed57b2 2018-05-19 stsp
5084 511a516b 2018-05-19 stsp f = got_opentemp();
5085 48ae06ee 2018-10-18 stsp if (f == NULL) {
5086 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5087 48ae06ee 2018-10-18 stsp goto done;
5088 48ae06ee 2018-10-18 stsp }
5089 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
5090 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
5091 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
5092 be97ab03 2023-01-19 thomas goto done;
5093 be97ab03 2023-01-19 thomas }
5094 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
5095 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5096 fb43ecf1 2019-02-11 stsp goto done;
5097 fb43ecf1 2019-02-11 stsp }
5098 48ae06ee 2018-10-18 stsp s->f = f;
5099 26ed57b2 2018-05-19 stsp
5100 15a94983 2018-12-23 stsp if (s->id1)
5101 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
5102 15a94983 2018-12-23 stsp else
5103 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
5104 15a94983 2018-12-23 stsp if (err)
5105 15a94983 2018-12-23 stsp goto done;
5106 15a94983 2018-12-23 stsp
5107 15a94983 2018-12-23 stsp switch (obj_type) {
5108 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
5109 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
5110 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
5111 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
5112 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
5113 53d03f97 2023-01-10 thomas s->f);
5114 26ed57b2 2018-05-19 stsp break;
5115 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
5116 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
5117 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
5118 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5119 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
5120 26ed57b2 2018-05-19 stsp break;
5121 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
5122 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
5123 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
5124 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
5125 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
5126 be97ab03 2023-01-19 thomas size_t nlines = 0;
5127 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
5128 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
5129 be97ab03 2023-01-19 thomas &changed_paths,
5130 be97ab03 2023-01-19 thomas s->ignore_whitespace,
5131 be97ab03 2023-01-19 thomas s->force_text_diff,
5132 be97ab03 2023-01-19 thomas tog_diff_algo
5133 be97ab03 2023-01-19 thomas };
5134 abd2672a 2018-12-23 stsp
5135 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
5136 be97ab03 2023-01-19 thomas if (lines == NULL) {
5137 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
5138 be97ab03 2023-01-19 thomas goto done;
5139 be97ab03 2023-01-19 thomas }
5140 be97ab03 2023-01-19 thomas
5141 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
5142 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
5143 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
5144 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
5145 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
5146 be97ab03 2023-01-19 thomas if (err)
5147 be97ab03 2023-01-19 thomas break;
5148 be97ab03 2023-01-19 thomas
5149 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
5150 abd2672a 2018-12-23 stsp if (err)
5151 3ffacbe1 2020-02-02 tracey goto done;
5152 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
5153 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
5154 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
5155 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
5156 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
5157 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5158 f44b1f58 2020-02-02 tracey if (err)
5159 f44b1f58 2020-02-02 tracey goto done;
5160 f44b1f58 2020-02-02 tracey } else {
5161 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
5162 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
5163 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
5164 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
5165 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
5166 772fcad5 2023-01-07 thomas s->ignore_whitespace,
5167 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
5168 f44b1f58 2020-02-02 tracey if (err)
5169 f44b1f58 2020-02-02 tracey goto done;
5170 f5404e4e 2020-02-02 tracey break;
5171 15a087fe 2019-02-21 stsp }
5172 abd2672a 2018-12-23 stsp }
5173 abd2672a 2018-12-23 stsp }
5174 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
5175 abd2672a 2018-12-23 stsp
5176 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
5177 be97ab03 2023-01-19 thomas lines, nlines);
5178 26ed57b2 2018-05-19 stsp break;
5179 abd2672a 2018-12-23 stsp }
5180 26ed57b2 2018-05-19 stsp default:
5181 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5182 48ae06ee 2018-10-18 stsp break;
5183 26ed57b2 2018-05-19 stsp }
5184 48ae06ee 2018-10-18 stsp done:
5185 be97ab03 2023-01-19 thomas free(lines);
5186 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
5187 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
5188 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
5189 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
5190 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
5191 48ae06ee 2018-10-18 stsp return err;
5192 48ae06ee 2018-10-18 stsp }
5193 26ed57b2 2018-05-19 stsp
5194 f5215bb9 2019-02-22 stsp static void
5195 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
5196 f5215bb9 2019-02-22 stsp {
5197 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
5198 f5215bb9 2019-02-22 stsp update_panels();
5199 f5215bb9 2019-02-22 stsp doupdate();
5200 f44b1f58 2020-02-02 tracey }
5201 f44b1f58 2020-02-02 tracey
5202 f44b1f58 2020-02-02 tracey static const struct got_error *
5203 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
5204 f44b1f58 2020-02-02 tracey {
5205 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
5206 f44b1f58 2020-02-02 tracey
5207 f44b1f58 2020-02-02 tracey s->matched_line = 0;
5208 f44b1f58 2020-02-02 tracey return NULL;
5209 f44b1f58 2020-02-02 tracey }
5210 f44b1f58 2020-02-02 tracey
5211 2a7d3cd7 2022-09-17 thomas static void
5212 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
5213 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
5214 f44b1f58 2020-02-02 tracey {
5215 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
5216 fc2737d5 2022-09-15 thomas
5217 2a7d3cd7 2022-09-17 thomas *f = s->f;
5218 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
5219 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
5220 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
5221 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
5222 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
5223 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
5224 fc2737d5 2022-09-15 thomas }
5225 fc2737d5 2022-09-15 thomas
5226 fc2737d5 2022-09-15 thomas static const struct got_error *
5227 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
5228 fc2737d5 2022-09-15 thomas {
5229 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5230 fc2737d5 2022-09-15 thomas FILE *f;
5231 f44b1f58 2020-02-02 tracey int lineno;
5232 78eecffc 2022-06-23 thomas char *line = NULL;
5233 826082fe 2020-12-10 stsp size_t linesize = 0;
5234 826082fe 2020-12-10 stsp ssize_t linelen;
5235 fc2737d5 2022-09-15 thomas off_t *line_offsets;
5236 fc2737d5 2022-09-15 thomas size_t nlines = 0;
5237 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
5238 f44b1f58 2020-02-02 tracey
5239 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
5240 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
5241 2a7d3cd7 2022-09-17 thomas "view search not supported");
5242 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
5243 fc2737d5 2022-09-15 thomas &match, &selected);
5244 fc2737d5 2022-09-15 thomas
5245 f44b1f58 2020-02-02 tracey if (!view->searching) {
5246 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5247 f44b1f58 2020-02-02 tracey return NULL;
5248 f44b1f58 2020-02-02 tracey }
5249 f44b1f58 2020-02-02 tracey
5250 fc2737d5 2022-09-15 thomas if (*match) {
5251 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5252 7303e8c8 2023-04-04 thomas lineno = *first + 1;
5253 f44b1f58 2020-02-02 tracey else
5254 7303e8c8 2023-04-04 thomas lineno = *first - 1;
5255 4b3f9dac 2021-12-17 thomas } else
5256 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
5257 f44b1f58 2020-02-02 tracey
5258 f44b1f58 2020-02-02 tracey while (1) {
5259 f44b1f58 2020-02-02 tracey off_t offset;
5260 f44b1f58 2020-02-02 tracey
5261 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
5262 fc2737d5 2022-09-15 thomas if (*match == 0) {
5263 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5264 f44b1f58 2020-02-02 tracey break;
5265 f44b1f58 2020-02-02 tracey }
5266 f44b1f58 2020-02-02 tracey
5267 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5268 f44b1f58 2020-02-02 tracey lineno = 1;
5269 f44b1f58 2020-02-02 tracey else
5270 fc2737d5 2022-09-15 thomas lineno = nlines;
5271 f44b1f58 2020-02-02 tracey }
5272 f44b1f58 2020-02-02 tracey
5273 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5274 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5275 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5276 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5277 f44b1f58 2020-02-02 tracey free(line);
5278 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5279 f44b1f58 2020-02-02 tracey }
5280 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5281 78eecffc 2022-06-23 thomas if (linelen != -1) {
5282 78eecffc 2022-06-23 thomas char *exstr;
5283 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5284 78eecffc 2022-06-23 thomas if (err)
5285 78eecffc 2022-06-23 thomas break;
5286 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5287 78eecffc 2022-06-23 thomas &view->regmatch)) {
5288 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5289 fc2737d5 2022-09-15 thomas *match = lineno;
5290 78eecffc 2022-06-23 thomas free(exstr);
5291 78eecffc 2022-06-23 thomas break;
5292 78eecffc 2022-06-23 thomas }
5293 78eecffc 2022-06-23 thomas free(exstr);
5294 f44b1f58 2020-02-02 tracey }
5295 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5296 f44b1f58 2020-02-02 tracey lineno++;
5297 f44b1f58 2020-02-02 tracey else
5298 f44b1f58 2020-02-02 tracey lineno--;
5299 f44b1f58 2020-02-02 tracey }
5300 826082fe 2020-12-10 stsp free(line);
5301 f44b1f58 2020-02-02 tracey
5302 fc2737d5 2022-09-15 thomas if (*match) {
5303 fc2737d5 2022-09-15 thomas *first = *match;
5304 fc2737d5 2022-09-15 thomas *selected = 1;
5305 f44b1f58 2020-02-02 tracey }
5306 f44b1f58 2020-02-02 tracey
5307 05171be4 2022-06-23 thomas return err;
5308 f5215bb9 2019-02-22 stsp }
5309 f5215bb9 2019-02-22 stsp
5310 48ae06ee 2018-10-18 stsp static const struct got_error *
5311 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5312 a0f32f33 2022-06-13 thomas {
5313 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5314 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5315 a0f32f33 2022-06-13 thomas
5316 a0f32f33 2022-06-13 thomas free(s->id1);
5317 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5318 a0f32f33 2022-06-13 thomas free(s->id2);
5319 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5320 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5321 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5322 a0f32f33 2022-06-13 thomas s->f = NULL;
5323 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5324 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5325 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5326 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5327 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5328 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5329 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5330 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5331 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5332 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5333 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5334 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5335 82c78e96 2022-08-06 thomas free(s->lines);
5336 82c78e96 2022-08-06 thomas s->lines = NULL;
5337 a0f32f33 2022-06-13 thomas s->nlines = 0;
5338 a0f32f33 2022-06-13 thomas return err;
5339 a0f32f33 2022-06-13 thomas }
5340 a0f32f33 2022-06-13 thomas
5341 a0f32f33 2022-06-13 thomas static const struct got_error *
5342 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5343 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5344 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5345 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5346 48ae06ee 2018-10-18 stsp {
5347 48ae06ee 2018-10-18 stsp const struct got_error *err;
5348 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5349 5dc9f4bc 2018-08-04 stsp
5350 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5351 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5352 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5353 a0f32f33 2022-06-13 thomas
5354 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5355 15a94983 2018-12-23 stsp int type1, type2;
5356 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5357 15a94983 2018-12-23 stsp if (err)
5358 15a94983 2018-12-23 stsp return err;
5359 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5360 15a94983 2018-12-23 stsp if (err)
5361 15a94983 2018-12-23 stsp return err;
5362 15a94983 2018-12-23 stsp
5363 15a94983 2018-12-23 stsp if (type1 != type2)
5364 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5365 15a94983 2018-12-23 stsp }
5366 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5367 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5368 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5369 f44b1f58 2020-02-02 tracey s->repo = repo;
5370 f44b1f58 2020-02-02 tracey s->id1 = id1;
5371 f44b1f58 2020-02-02 tracey s->id2 = id2;
5372 3dbaef42 2020-11-24 stsp s->label1 = label1;
5373 3dbaef42 2020-11-24 stsp s->label2 = label2;
5374 48ae06ee 2018-10-18 stsp
5375 15a94983 2018-12-23 stsp if (id1) {
5376 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5377 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5378 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5379 48ae06ee 2018-10-18 stsp } else
5380 5465d566 2020-02-01 tracey s->id1 = NULL;
5381 48ae06ee 2018-10-18 stsp
5382 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5383 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5384 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5385 a0f32f33 2022-06-13 thomas goto done;
5386 48ae06ee 2018-10-18 stsp }
5387 a0f32f33 2022-06-13 thomas
5388 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5389 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5390 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5391 9a1d7435 2022-06-23 thomas goto done;
5392 9a1d7435 2022-06-23 thomas }
5393 9a1d7435 2022-06-23 thomas
5394 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5395 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5396 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5397 a0f32f33 2022-06-13 thomas goto done;
5398 a0f32f33 2022-06-13 thomas }
5399 a0f32f33 2022-06-13 thomas
5400 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5401 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5402 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5403 19a6a6b5 2022-07-01 thomas goto done;
5404 19a6a6b5 2022-07-01 thomas }
5405 19a6a6b5 2022-07-01 thomas
5406 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5407 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5408 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5409 19a6a6b5 2022-07-01 thomas goto done;
5410 19a6a6b5 2022-07-01 thomas }
5411 19a6a6b5 2022-07-01 thomas
5412 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5413 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5414 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5415 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5416 5465d566 2020-02-01 tracey s->repo = repo;
5417 6d17833f 2019-11-08 stsp
5418 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5419 6568f0aa 2022-08-06 thomas int rc;
5420 6d17833f 2019-11-08 stsp
5421 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5422 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5423 6568f0aa 2022-08-06 thomas if (rc != ERR)
5424 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5425 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5426 6568f0aa 2022-08-06 thomas if (rc != ERR)
5427 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5428 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5429 6568f0aa 2022-08-06 thomas if (rc != ERR)
5430 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5431 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5432 6568f0aa 2022-08-06 thomas if (rc != ERR)
5433 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5434 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5435 6568f0aa 2022-08-06 thomas if (rc != ERR)
5436 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5437 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5438 6568f0aa 2022-08-06 thomas if (rc != ERR)
5439 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5440 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5441 6568f0aa 2022-08-06 thomas if (rc != ERR)
5442 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5443 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5444 6568f0aa 2022-08-06 thomas if (rc != ERR)
5445 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5446 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5447 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5448 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5449 a0f32f33 2022-06-13 thomas goto done;
5450 6568f0aa 2022-08-06 thomas }
5451 6d17833f 2019-11-08 stsp }
5452 5dc9f4bc 2018-08-04 stsp
5453 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5454 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5455 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5456 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5457 f5215bb9 2019-02-22 stsp
5458 5465d566 2020-02-01 tracey err = create_diff(s);
5459 48ae06ee 2018-10-18 stsp
5460 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5461 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5462 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5463 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5464 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5465 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5466 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5467 a0f32f33 2022-06-13 thomas done:
5468 a0f32f33 2022-06-13 thomas if (err)
5469 a0f32f33 2022-06-13 thomas close_diff_view(view);
5470 e5a0f69f 2018-08-18 stsp return err;
5471 5dc9f4bc 2018-08-04 stsp }
5472 5dc9f4bc 2018-08-04 stsp
5473 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5474 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5475 5dc9f4bc 2018-08-04 stsp {
5476 a3404814 2018-09-02 stsp const struct got_error *err;
5477 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5478 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5479 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5480 a3404814 2018-09-02 stsp
5481 a3404814 2018-09-02 stsp if (s->id1) {
5482 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5483 a3404814 2018-09-02 stsp if (err)
5484 a3404814 2018-09-02 stsp return err;
5485 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5486 3dbaef42 2020-11-24 stsp } else
5487 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5488 3dbaef42 2020-11-24 stsp
5489 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5490 a3404814 2018-09-02 stsp if (err)
5491 a3404814 2018-09-02 stsp return err;
5492 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5493 26ed57b2 2018-05-19 stsp
5494 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5495 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5496 a3404814 2018-09-02 stsp free(id_str1);
5497 a3404814 2018-09-02 stsp free(id_str2);
5498 a3404814 2018-09-02 stsp return err;
5499 a3404814 2018-09-02 stsp }
5500 a3404814 2018-09-02 stsp free(id_str1);
5501 a3404814 2018-09-02 stsp free(id_str2);
5502 a3404814 2018-09-02 stsp
5503 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5504 267bb3b8 2021-08-01 stsp free(header);
5505 267bb3b8 2021-08-01 stsp return err;
5506 15a087fe 2019-02-21 stsp }
5507 15a087fe 2019-02-21 stsp
5508 15a087fe 2019-02-21 stsp static const struct got_error *
5509 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5510 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5511 15a087fe 2019-02-21 stsp {
5512 d7a04538 2019-02-21 stsp const struct got_error *err;
5513 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5514 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5515 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5516 15a087fe 2019-02-21 stsp
5517 15a087fe 2019-02-21 stsp free(s->id2);
5518 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5519 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5520 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5521 15a087fe 2019-02-21 stsp
5522 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5523 d7a04538 2019-02-21 stsp if (err)
5524 d7a04538 2019-02-21 stsp return err;
5525 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5526 15a087fe 2019-02-21 stsp free(s->id1);
5527 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5528 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5529 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5530 15a087fe 2019-02-21 stsp return NULL;
5531 0cf4efb1 2018-09-29 stsp }
5532 0cf4efb1 2018-09-29 stsp
5533 0cf4efb1 2018-09-29 stsp static const struct got_error *
5534 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5535 adf4c9e0 2022-07-03 thomas {
5536 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5537 adf4c9e0 2022-07-03 thomas
5538 adf4c9e0 2022-07-03 thomas view->count = 0;
5539 adf4c9e0 2022-07-03 thomas wclear(view->window);
5540 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5541 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5542 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5543 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5544 adf4c9e0 2022-07-03 thomas return create_diff(s);
5545 82c78e96 2022-08-06 thomas }
5546 82c78e96 2022-08-06 thomas
5547 82c78e96 2022-08-06 thomas static void
5548 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5549 82c78e96 2022-08-06 thomas {
5550 82c78e96 2022-08-06 thomas int start, i;
5551 82c78e96 2022-08-06 thomas
5552 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5553 82c78e96 2022-08-06 thomas
5554 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5555 82c78e96 2022-08-06 thomas if (i == 0)
5556 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5557 82c78e96 2022-08-06 thomas if (--i == start)
5558 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5559 82c78e96 2022-08-06 thomas }
5560 82c78e96 2022-08-06 thomas
5561 82c78e96 2022-08-06 thomas s->selected_line = 1;
5562 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5563 adf4c9e0 2022-07-03 thomas }
5564 adf4c9e0 2022-07-03 thomas
5565 82c78e96 2022-08-06 thomas static void
5566 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5567 82c78e96 2022-08-06 thomas {
5568 82c78e96 2022-08-06 thomas int start, i;
5569 82c78e96 2022-08-06 thomas
5570 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5571 82c78e96 2022-08-06 thomas
5572 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5573 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5574 82c78e96 2022-08-06 thomas i = 0;
5575 82c78e96 2022-08-06 thomas if (++i == start)
5576 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5577 82c78e96 2022-08-06 thomas }
5578 82c78e96 2022-08-06 thomas
5579 82c78e96 2022-08-06 thomas s->selected_line = 1;
5580 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5581 82c78e96 2022-08-06 thomas }
5582 82c78e96 2022-08-06 thomas
5583 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5584 4fc71f3b 2022-07-12 thomas int, int, int);
5585 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5586 4fc71f3b 2022-07-12 thomas int, int);
5587 4fc71f3b 2022-07-12 thomas
5588 adf4c9e0 2022-07-03 thomas static const struct got_error *
5589 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5590 e5a0f69f 2018-08-18 stsp {
5591 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5592 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5593 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5594 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5595 826082fe 2020-12-10 stsp char *line = NULL;
5596 826082fe 2020-12-10 stsp size_t linesize = 0;
5597 826082fe 2020-12-10 stsp ssize_t linelen;
5598 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5599 e5a0f69f 2018-08-18 stsp
5600 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5601 82c78e96 2022-08-06 thomas
5602 e5a0f69f 2018-08-18 stsp switch (ch) {
5603 05171be4 2022-06-23 thomas case '0':
5604 05171be4 2022-06-23 thomas case '$':
5605 05171be4 2022-06-23 thomas case KEY_RIGHT:
5606 05171be4 2022-06-23 thomas case 'l':
5607 05171be4 2022-06-23 thomas case KEY_LEFT:
5608 05171be4 2022-06-23 thomas case 'h':
5609 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5610 05171be4 2022-06-23 thomas break;
5611 64453f7e 2020-11-21 stsp case 'a':
5612 3dbaef42 2020-11-24 stsp case 'w':
5613 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5614 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5615 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5616 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5617 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5618 f2d06bef 2023-01-23 thomas }
5619 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5620 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5621 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5622 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5623 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5624 f2d06bef 2023-01-23 thomas }
5625 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5626 912a3f79 2021-08-30 j break;
5627 912a3f79 2021-08-30 j case 'g':
5628 912a3f79 2021-08-30 j case KEY_HOME:
5629 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5630 07b0611c 2022-06-23 thomas view->count = 0;
5631 64453f7e 2020-11-21 stsp break;
5632 912a3f79 2021-08-30 j case 'G':
5633 912a3f79 2021-08-30 j case KEY_END:
5634 07b0611c 2022-06-23 thomas view->count = 0;
5635 912a3f79 2021-08-30 j if (s->eof)
5636 912a3f79 2021-08-30 j break;
5637 912a3f79 2021-08-30 j
5638 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5639 912a3f79 2021-08-30 j s->eof = 1;
5640 912a3f79 2021-08-30 j break;
5641 1e37a5c2 2019-05-12 jcs case 'k':
5642 1e37a5c2 2019-05-12 jcs case KEY_UP:
5643 f7140bf5 2021-10-17 thomas case CTRL('p'):
5644 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5645 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5646 07b0611c 2022-06-23 thomas else
5647 07b0611c 2022-06-23 thomas view->count = 0;
5648 1e37a5c2 2019-05-12 jcs break;
5649 70f17a53 2022-06-13 thomas case CTRL('u'):
5650 23427b14 2022-06-23 thomas case 'u':
5651 70f17a53 2022-06-13 thomas nscroll /= 2;
5652 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5653 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5654 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5655 1c5e5faa 2022-06-23 thomas case 'b':
5656 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5657 07b0611c 2022-06-23 thomas view->count = 0;
5658 26ed57b2 2018-05-19 stsp break;
5659 07b0611c 2022-06-23 thomas }
5660 1e37a5c2 2019-05-12 jcs i = 0;
5661 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5662 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5663 1e37a5c2 2019-05-12 jcs break;
5664 1e37a5c2 2019-05-12 jcs case 'j':
5665 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5666 f7140bf5 2021-10-17 thomas case CTRL('n'):
5667 1e37a5c2 2019-05-12 jcs if (!s->eof)
5668 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5669 07b0611c 2022-06-23 thomas else
5670 07b0611c 2022-06-23 thomas view->count = 0;
5671 1e37a5c2 2019-05-12 jcs break;
5672 70f17a53 2022-06-13 thomas case CTRL('d'):
5673 23427b14 2022-06-23 thomas case 'd':
5674 70f17a53 2022-06-13 thomas nscroll /= 2;
5675 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5676 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5677 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5678 1c5e5faa 2022-06-23 thomas case 'f':
5679 1e37a5c2 2019-05-12 jcs case ' ':
5680 07b0611c 2022-06-23 thomas if (s->eof) {
5681 07b0611c 2022-06-23 thomas view->count = 0;
5682 1e37a5c2 2019-05-12 jcs break;
5683 07b0611c 2022-06-23 thomas }
5684 1e37a5c2 2019-05-12 jcs i = 0;
5685 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5686 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5687 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5688 826082fe 2020-12-10 stsp if (linelen == -1) {
5689 826082fe 2020-12-10 stsp if (feof(s->f)) {
5690 826082fe 2020-12-10 stsp s->eof = 1;
5691 826082fe 2020-12-10 stsp } else
5692 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5693 34bc9ec9 2019-02-22 stsp break;
5694 826082fe 2020-12-10 stsp }
5695 1e37a5c2 2019-05-12 jcs }
5696 826082fe 2020-12-10 stsp free(line);
5697 1e37a5c2 2019-05-12 jcs break;
5698 82c78e96 2022-08-06 thomas case '(':
5699 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5700 82c78e96 2022-08-06 thomas break;
5701 82c78e96 2022-08-06 thomas case ')':
5702 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5703 82c78e96 2022-08-06 thomas break;
5704 82c78e96 2022-08-06 thomas case '{':
5705 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5706 82c78e96 2022-08-06 thomas break;
5707 82c78e96 2022-08-06 thomas case '}':
5708 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5709 82c78e96 2022-08-06 thomas break;
5710 1e37a5c2 2019-05-12 jcs case '[':
5711 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5712 1e37a5c2 2019-05-12 jcs s->diff_context--;
5713 aa61903a 2021-12-31 thomas s->matched_line = 0;
5714 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5715 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5716 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5717 27829c9e 2020-11-21 stsp s->nlines) {
5718 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5719 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5720 27829c9e 2020-11-21 stsp }
5721 07b0611c 2022-06-23 thomas } else
5722 07b0611c 2022-06-23 thomas view->count = 0;
5723 1e37a5c2 2019-05-12 jcs break;
5724 1e37a5c2 2019-05-12 jcs case ']':
5725 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5726 1e37a5c2 2019-05-12 jcs s->diff_context++;
5727 aa61903a 2021-12-31 thomas s->matched_line = 0;
5728 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5729 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5730 07b0611c 2022-06-23 thomas } else
5731 07b0611c 2022-06-23 thomas view->count = 0;
5732 1e37a5c2 2019-05-12 jcs break;
5733 1e37a5c2 2019-05-12 jcs case '<':
5734 1e37a5c2 2019-05-12 jcs case ',':
5735 777aae21 2022-07-20 thomas case 'K':
5736 4fc71f3b 2022-07-12 thomas up = 1;
5737 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5738 4fc71f3b 2022-07-12 thomas case '>':
5739 4fc71f3b 2022-07-12 thomas case '.':
5740 777aae21 2022-07-20 thomas case 'J':
5741 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5742 07b0611c 2022-06-23 thomas view->count = 0;
5743 48ae06ee 2018-10-18 stsp break;
5744 07b0611c 2022-06-23 thomas }
5745 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5746 6524637e 2019-02-21 stsp
5747 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5748 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5749 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5750 15a087fe 2019-02-21 stsp
5751 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5752 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5753 4fc71f3b 2022-07-12 thomas if (err)
5754 4fc71f3b 2022-07-12 thomas break;
5755 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5756 15a087fe 2019-02-21 stsp
5757 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5758 4fc71f3b 2022-07-12 thomas break;
5759 15a087fe 2019-02-21 stsp
5760 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5761 4fc71f3b 2022-07-12 thomas if (err)
5762 4fc71f3b 2022-07-12 thomas break;
5763 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5764 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5765 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5766 5e224a3e 2019-02-22 stsp
5767 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5768 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5769 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5770 4fc71f3b 2022-07-12 thomas
5771 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5772 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5773 4fc71f3b 2022-07-12 thomas if (err)
5774 4fc71f3b 2022-07-12 thomas break;
5775 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5776 5a8b5076 2020-12-05 stsp
5777 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5778 4fc71f3b 2022-07-12 thomas break;
5779 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5780 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5781 4fc71f3b 2022-07-12 thomas bs->selected_line);
5782 4fc71f3b 2022-07-12 thomas if (id == NULL)
5783 4fc71f3b 2022-07-12 thomas break;
5784 15a087fe 2019-02-21 stsp
5785 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5786 4fc71f3b 2022-07-12 thomas break;
5787 15a087fe 2019-02-21 stsp
5788 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5789 4fc71f3b 2022-07-12 thomas if (err)
5790 4fc71f3b 2022-07-12 thomas break;
5791 4fc71f3b 2022-07-12 thomas }
5792 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5793 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5794 aa61903a 2021-12-31 thomas s->matched_line = 0;
5795 05171be4 2022-06-23 thomas view->x = 0;
5796 1e37a5c2 2019-05-12 jcs
5797 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5798 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5799 1e37a5c2 2019-05-12 jcs break;
5800 1e37a5c2 2019-05-12 jcs default:
5801 07b0611c 2022-06-23 thomas view->count = 0;
5802 1e37a5c2 2019-05-12 jcs break;
5803 26ed57b2 2018-05-19 stsp }
5804 e5a0f69f 2018-08-18 stsp
5805 bcbd79e2 2018-08-19 stsp return err;
5806 26ed57b2 2018-05-19 stsp }
5807 26ed57b2 2018-05-19 stsp
5808 4ed7e80c 2018-05-20 stsp static const struct got_error *
5809 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5810 9f7d7167 2018-04-29 stsp {
5811 b85a3496 2023-04-14 thomas const struct got_error *io_err, *error;
5812 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5813 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5814 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5815 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5816 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5817 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5818 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5819 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5820 3dbaef42 2020-11-24 stsp const char *errstr;
5821 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5822 b85a3496 2023-04-14 thomas struct tog_io *tog_io = NULL;
5823 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5824 70ac5f84 2019-03-28 stsp
5825 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5826 26ed57b2 2018-05-19 stsp switch (ch) {
5827 64453f7e 2020-11-21 stsp case 'a':
5828 64453f7e 2020-11-21 stsp force_text_diff = 1;
5829 3dbaef42 2020-11-24 stsp break;
5830 3dbaef42 2020-11-24 stsp case 'C':
5831 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5832 3dbaef42 2020-11-24 stsp &errstr);
5833 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5834 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5835 8f666e67 2022-02-12 thomas errstr, errstr);
5836 64453f7e 2020-11-21 stsp break;
5837 09b5bff8 2020-02-23 naddy case 'r':
5838 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5839 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5840 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5841 09b5bff8 2020-02-23 naddy optarg);
5842 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5843 09b5bff8 2020-02-23 naddy break;
5844 3dbaef42 2020-11-24 stsp case 'w':
5845 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5846 3dbaef42 2020-11-24 stsp break;
5847 26ed57b2 2018-05-19 stsp default:
5848 17020d27 2019-03-07 stsp usage_diff();
5849 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5850 26ed57b2 2018-05-19 stsp }
5851 26ed57b2 2018-05-19 stsp }
5852 26ed57b2 2018-05-19 stsp
5853 26ed57b2 2018-05-19 stsp argc -= optind;
5854 26ed57b2 2018-05-19 stsp argv += optind;
5855 26ed57b2 2018-05-19 stsp
5856 26ed57b2 2018-05-19 stsp if (argc == 0) {
5857 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5858 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5859 15a94983 2018-12-23 stsp id_str1 = argv[0];
5860 15a94983 2018-12-23 stsp id_str2 = argv[1];
5861 26ed57b2 2018-05-19 stsp } else
5862 26ed57b2 2018-05-19 stsp usage_diff();
5863 eb6600df 2019-01-04 stsp
5864 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5865 7cd52833 2022-06-23 thomas if (error)
5866 7cd52833 2022-06-23 thomas goto done;
5867 7cd52833 2022-06-23 thomas
5868 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5869 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5870 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5871 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5872 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5873 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5874 c156c7a4 2020-12-18 stsp goto done;
5875 a273ac94 2020-02-23 naddy if (worktree)
5876 a273ac94 2020-02-23 naddy repo_path =
5877 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5878 a273ac94 2020-02-23 naddy else
5879 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5880 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5881 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5882 c156c7a4 2020-12-18 stsp goto done;
5883 c156c7a4 2020-12-18 stsp }
5884 a273ac94 2020-02-23 naddy }
5885 a273ac94 2020-02-23 naddy
5886 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5887 eb6600df 2019-01-04 stsp if (error)
5888 eb6600df 2019-01-04 stsp goto done;
5889 26ed57b2 2018-05-19 stsp
5890 b85a3496 2023-04-14 thomas error = init_curses(&tog_io);
5891 b85a3496 2023-04-14 thomas if (error)
5892 b85a3496 2023-04-14 thomas goto done;
5893 a273ac94 2020-02-23 naddy
5894 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5895 51a10b52 2020-12-26 stsp if (error)
5896 51a10b52 2020-12-26 stsp goto done;
5897 51a10b52 2020-12-26 stsp
5898 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5899 26ed57b2 2018-05-19 stsp if (error)
5900 26ed57b2 2018-05-19 stsp goto done;
5901 26ed57b2 2018-05-19 stsp
5902 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5903 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5904 26ed57b2 2018-05-19 stsp if (error)
5905 26ed57b2 2018-05-19 stsp goto done;
5906 26ed57b2 2018-05-19 stsp
5907 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5908 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5909 26ed57b2 2018-05-19 stsp if (error)
5910 26ed57b2 2018-05-19 stsp goto done;
5911 26ed57b2 2018-05-19 stsp
5912 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5913 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5914 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5915 ea5e7bb5 2018-08-01 stsp goto done;
5916 ea5e7bb5 2018-08-01 stsp }
5917 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5918 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5919 5dc9f4bc 2018-08-04 stsp if (error)
5920 5dc9f4bc 2018-08-04 stsp goto done;
5921 b85a3496 2023-04-14 thomas error = view_loop(view, tog_io);
5922 26ed57b2 2018-05-19 stsp done:
5923 3dbaef42 2020-11-24 stsp free(label1);
5924 3dbaef42 2020-11-24 stsp free(label2);
5925 c02c541e 2019-03-29 stsp free(repo_path);
5926 a273ac94 2020-02-23 naddy free(cwd);
5927 1d0f4054 2021-06-17 stsp if (repo) {
5928 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5929 1d0f4054 2021-06-17 stsp if (error == NULL)
5930 1d0f4054 2021-06-17 stsp error = close_err;
5931 1d0f4054 2021-06-17 stsp }
5932 a273ac94 2020-02-23 naddy if (worktree)
5933 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5934 7cd52833 2022-06-23 thomas if (pack_fds) {
5935 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5936 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5937 7cd52833 2022-06-23 thomas if (error == NULL)
5938 7cd52833 2022-06-23 thomas error = pack_err;
5939 b85a3496 2023-04-14 thomas }
5940 b85a3496 2023-04-14 thomas if (tog_io != NULL) {
5941 b85a3496 2023-04-14 thomas io_err = tog_io_close(tog_io);
5942 b85a3496 2023-04-14 thomas if (error == NULL)
5943 b85a3496 2023-04-14 thomas error = io_err;
5944 7cd52833 2022-06-23 thomas }
5945 51a10b52 2020-12-26 stsp tog_free_refs();
5946 26ed57b2 2018-05-19 stsp return error;
5947 9f7d7167 2018-04-29 stsp }
5948 9f7d7167 2018-04-29 stsp
5949 4ed7e80c 2018-05-20 stsp __dead static void
5950 9f7d7167 2018-04-29 stsp usage_blame(void)
5951 9f7d7167 2018-04-29 stsp {
5952 80ddbec8 2018-04-29 stsp endwin();
5953 91198554 2022-06-23 thomas fprintf(stderr,
5954 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5955 9f7d7167 2018-04-29 stsp getprogname());
5956 9f7d7167 2018-04-29 stsp exit(1);
5957 9f7d7167 2018-04-29 stsp }
5958 84451b3e 2018-07-10 stsp
5959 84451b3e 2018-07-10 stsp struct tog_blame_line {
5960 84451b3e 2018-07-10 stsp int annotated;
5961 84451b3e 2018-07-10 stsp struct got_object_id *id;
5962 84451b3e 2018-07-10 stsp };
5963 9f7d7167 2018-04-29 stsp
5964 4ed7e80c 2018-05-20 stsp static const struct got_error *
5965 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5966 84451b3e 2018-07-10 stsp {
5967 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5968 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5969 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5970 84451b3e 2018-07-10 stsp const struct got_error *err;
5971 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5972 826082fe 2020-12-10 stsp char *line = NULL;
5973 826082fe 2020-12-10 stsp size_t linesize = 0;
5974 826082fe 2020-12-10 stsp ssize_t linelen;
5975 84451b3e 2018-07-10 stsp wchar_t *wline;
5976 27a741e5 2019-09-11 stsp int width;
5977 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5978 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5979 ab089a2a 2018-07-12 stsp char *id_str;
5980 11b20872 2019-11-08 stsp struct tog_color *tc;
5981 ab089a2a 2018-07-12 stsp
5982 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5983 ab089a2a 2018-07-12 stsp if (err)
5984 ab089a2a 2018-07-12 stsp return err;
5985 84451b3e 2018-07-10 stsp
5986 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5987 f7d12f7e 2018-08-01 stsp werase(view->window);
5988 84451b3e 2018-07-10 stsp
5989 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5990 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5991 ab089a2a 2018-07-12 stsp free(id_str);
5992 ab089a2a 2018-07-12 stsp return err;
5993 ab089a2a 2018-07-12 stsp }
5994 ab089a2a 2018-07-12 stsp
5995 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5996 ab089a2a 2018-07-12 stsp free(line);
5997 2550e4c3 2018-07-13 stsp line = NULL;
5998 1cae65b4 2019-09-22 stsp if (err)
5999 1cae65b4 2019-09-22 stsp return err;
6000 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6001 a3404814 2018-09-02 stsp wstandout(view->window);
6002 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6003 11b20872 2019-11-08 stsp if (tc)
6004 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6005 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6006 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6007 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6008 11b20872 2019-11-08 stsp if (tc)
6009 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6010 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6011 a3404814 2018-09-02 stsp wstandend(view->window);
6012 2550e4c3 2018-07-13 stsp free(wline);
6013 2550e4c3 2018-07-13 stsp wline = NULL;
6014 ab089a2a 2018-07-12 stsp
6015 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
6016 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
6017 07dd3ed3 2022-08-06 thomas
6018 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
6019 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
6020 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
6021 ab089a2a 2018-07-12 stsp free(id_str);
6022 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6023 ab089a2a 2018-07-12 stsp }
6024 ab089a2a 2018-07-12 stsp free(id_str);
6025 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6026 3f60a8ef 2018-07-10 stsp free(line);
6027 2550e4c3 2018-07-13 stsp line = NULL;
6028 3f60a8ef 2018-07-10 stsp if (err)
6029 3f60a8ef 2018-07-10 stsp return err;
6030 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6031 2550e4c3 2018-07-13 stsp free(wline);
6032 2550e4c3 2018-07-13 stsp wline = NULL;
6033 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6034 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6035 3f60a8ef 2018-07-10 stsp
6036 4f7c3e5e 2020-12-01 naddy s->eof = 0;
6037 05171be4 2022-06-23 thomas view->maxx = 0;
6038 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
6039 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
6040 826082fe 2020-12-10 stsp if (linelen == -1) {
6041 826082fe 2020-12-10 stsp if (feof(blame->f)) {
6042 826082fe 2020-12-10 stsp s->eof = 1;
6043 826082fe 2020-12-10 stsp break;
6044 826082fe 2020-12-10 stsp }
6045 84451b3e 2018-07-10 stsp free(line);
6046 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
6047 84451b3e 2018-07-10 stsp }
6048 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
6049 07dd3ed3 2022-08-06 thomas continue;
6050 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
6051 826082fe 2020-12-10 stsp continue;
6052 cbea3800 2022-06-23 thomas
6053 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
6054 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
6055 cbea3800 2022-06-23 thomas if (err) {
6056 cbea3800 2022-06-23 thomas free(line);
6057 cbea3800 2022-06-23 thomas return err;
6058 cbea3800 2022-06-23 thomas }
6059 cbea3800 2022-06-23 thomas free(wline);
6060 cbea3800 2022-06-23 thomas wline = NULL;
6061 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
6062 84451b3e 2018-07-10 stsp
6063 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6064 f7d12f7e 2018-08-01 stsp wstandout(view->window);
6065 b700b5d6 2018-07-10 stsp
6066 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
6067 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
6068 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
6069 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
6070 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
6071 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6072 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
6073 8d0fe45a 2019-08-12 stsp char *id_str;
6074 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
6075 91198554 2022-06-23 thomas blame_line->id);
6076 8d0fe45a 2019-08-12 stsp if (err) {
6077 8d0fe45a 2019-08-12 stsp free(line);
6078 8d0fe45a 2019-08-12 stsp return err;
6079 8d0fe45a 2019-08-12 stsp }
6080 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6081 11b20872 2019-11-08 stsp if (tc)
6082 11b20872 2019-11-08 stsp wattr_on(view->window,
6083 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6084 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
6085 11b20872 2019-11-08 stsp if (tc)
6086 11b20872 2019-11-08 stsp wattr_off(view->window,
6087 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6088 8d0fe45a 2019-08-12 stsp free(id_str);
6089 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
6090 8d0fe45a 2019-08-12 stsp } else {
6091 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6092 8d0fe45a 2019-08-12 stsp prev_id = NULL;
6093 84451b3e 2018-07-10 stsp }
6094 ee41ec32 2018-07-10 stsp } else {
6095 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
6096 ee41ec32 2018-07-10 stsp prev_id = NULL;
6097 ee41ec32 2018-07-10 stsp }
6098 84451b3e 2018-07-10 stsp
6099 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
6100 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6101 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
6102 27a741e5 2019-09-11 stsp
6103 41605754 2020-11-12 stsp if (view->ncols <= 9) {
6104 41605754 2020-11-12 stsp width = 9;
6105 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
6106 4f7c3e5e 2020-12-01 naddy s->matched_line &&
6107 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
6108 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
6109 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
6110 41605754 2020-11-12 stsp if (err) {
6111 41605754 2020-11-12 stsp free(line);
6112 41605754 2020-11-12 stsp return err;
6113 41605754 2020-11-12 stsp }
6114 41605754 2020-11-12 stsp width += 9;
6115 41605754 2020-11-12 stsp } else {
6116 923086fd 2022-06-23 thomas int skip;
6117 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
6118 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
6119 923086fd 2022-06-23 thomas if (err) {
6120 923086fd 2022-06-23 thomas free(line);
6121 923086fd 2022-06-23 thomas return err;
6122 05171be4 2022-06-23 thomas }
6123 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
6124 02bce7e2 2022-06-23 thomas width += 9;
6125 41605754 2020-11-12 stsp free(wline);
6126 41605754 2020-11-12 stsp wline = NULL;
6127 41605754 2020-11-12 stsp }
6128 41605754 2020-11-12 stsp
6129 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
6130 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
6131 84451b3e 2018-07-10 stsp if (++nprinted == 1)
6132 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
6133 84451b3e 2018-07-10 stsp }
6134 826082fe 2020-12-10 stsp free(line);
6135 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
6136 84451b3e 2018-07-10 stsp
6137 a5d43cac 2022-07-01 thomas view_border(view);
6138 84451b3e 2018-07-10 stsp
6139 84451b3e 2018-07-10 stsp return NULL;
6140 84451b3e 2018-07-10 stsp }
6141 84451b3e 2018-07-10 stsp
6142 84451b3e 2018-07-10 stsp static const struct got_error *
6143 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
6144 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
6145 84451b3e 2018-07-10 stsp {
6146 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
6147 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
6148 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
6149 1a76625f 2018-10-22 stsp int errcode;
6150 84451b3e 2018-07-10 stsp
6151 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
6152 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
6153 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
6154 84451b3e 2018-07-10 stsp
6155 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6156 1a76625f 2018-10-22 stsp if (errcode)
6157 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
6158 84451b3e 2018-07-10 stsp
6159 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
6160 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
6161 d68a0a7d 2018-07-10 stsp goto done;
6162 d68a0a7d 2018-07-10 stsp }
6163 d68a0a7d 2018-07-10 stsp
6164 d68a0a7d 2018-07-10 stsp if (lineno == -1)
6165 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
6166 d68a0a7d 2018-07-10 stsp
6167 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
6168 d68a0a7d 2018-07-10 stsp if (line->annotated)
6169 d68a0a7d 2018-07-10 stsp goto done;
6170 d68a0a7d 2018-07-10 stsp
6171 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
6172 84451b3e 2018-07-10 stsp if (line->id == NULL) {
6173 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6174 84451b3e 2018-07-10 stsp goto done;
6175 84451b3e 2018-07-10 stsp }
6176 84451b3e 2018-07-10 stsp line->annotated = 1;
6177 84451b3e 2018-07-10 stsp done:
6178 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6179 1a76625f 2018-10-22 stsp if (errcode)
6180 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6181 84451b3e 2018-07-10 stsp return err;
6182 84451b3e 2018-07-10 stsp }
6183 84451b3e 2018-07-10 stsp
6184 84451b3e 2018-07-10 stsp static void *
6185 84451b3e 2018-07-10 stsp blame_thread(void *arg)
6186 84451b3e 2018-07-10 stsp {
6187 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
6188 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
6189 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
6190 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
6191 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
6192 00a8878e 2022-07-01 thomas
6193 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
6194 9117a7b7 2022-07-01 thomas if (fd1 == -1)
6195 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
6196 9117a7b7 2022-07-01 thomas
6197 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
6198 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
6199 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6200 9117a7b7 2022-07-01 thomas goto done;
6201 9117a7b7 2022-07-01 thomas }
6202 18430de3 2018-07-10 stsp
6203 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
6204 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
6205 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6206 9117a7b7 2022-07-01 thomas goto done;
6207 9117a7b7 2022-07-01 thomas }
6208 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
6209 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
6210 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
6211 9117a7b7 2022-07-01 thomas goto done;
6212 9117a7b7 2022-07-01 thomas }
6213 9117a7b7 2022-07-01 thomas
6214 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
6215 61266923 2020-01-14 stsp if (err)
6216 9117a7b7 2022-07-01 thomas goto done;
6217 61266923 2020-01-14 stsp
6218 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
6219 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
6220 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
6221 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
6222 fc06ba56 2019-08-22 stsp err = NULL;
6223 18430de3 2018-07-10 stsp
6224 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6225 9117a7b7 2022-07-01 thomas if (errcode) {
6226 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
6227 9117a7b7 2022-07-01 thomas goto done;
6228 9117a7b7 2022-07-01 thomas }
6229 18430de3 2018-07-10 stsp
6230 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
6231 1d0f4054 2021-06-17 stsp if (err == NULL)
6232 1d0f4054 2021-06-17 stsp err = close_err;
6233 c9beca56 2018-07-22 stsp ta->repo = NULL;
6234 c9beca56 2018-07-22 stsp *ta->complete = 1;
6235 18430de3 2018-07-10 stsp
6236 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6237 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
6238 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
6239 18430de3 2018-07-10 stsp
6240 9117a7b7 2022-07-01 thomas done:
6241 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
6242 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
6243 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
6244 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
6245 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
6246 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6247 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
6248 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
6249 00a8878e 2022-07-01 thomas
6250 18430de3 2018-07-10 stsp return (void *)err;
6251 84451b3e 2018-07-10 stsp }
6252 84451b3e 2018-07-10 stsp
6253 245d91c1 2018-07-12 stsp static struct got_object_id *
6254 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
6255 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
6256 245d91c1 2018-07-12 stsp {
6257 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
6258 8d0fe45a 2019-08-12 stsp
6259 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
6260 8d0fe45a 2019-08-12 stsp return NULL;
6261 b880a918 2018-07-10 stsp
6262 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
6263 4fc71f3b 2022-07-12 thomas if (!line->annotated)
6264 4fc71f3b 2022-07-12 thomas return NULL;
6265 4fc71f3b 2022-07-12 thomas
6266 4fc71f3b 2022-07-12 thomas return line->id;
6267 4fc71f3b 2022-07-12 thomas }
6268 4fc71f3b 2022-07-12 thomas
6269 4fc71f3b 2022-07-12 thomas static struct got_object_id *
6270 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
6271 4fc71f3b 2022-07-12 thomas int lineno)
6272 4fc71f3b 2022-07-12 thomas {
6273 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
6274 4fc71f3b 2022-07-12 thomas
6275 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6276 4fc71f3b 2022-07-12 thomas return NULL;
6277 4fc71f3b 2022-07-12 thomas
6278 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6279 245d91c1 2018-07-12 stsp if (!line->annotated)
6280 245d91c1 2018-07-12 stsp return NULL;
6281 245d91c1 2018-07-12 stsp
6282 245d91c1 2018-07-12 stsp return line->id;
6283 b880a918 2018-07-10 stsp }
6284 245d91c1 2018-07-12 stsp
6285 b880a918 2018-07-10 stsp static const struct got_error *
6286 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6287 a70480e0 2018-06-23 stsp {
6288 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6289 245d91c1 2018-07-12 stsp int i;
6290 245d91c1 2018-07-12 stsp
6291 245d91c1 2018-07-12 stsp if (blame->thread) {
6292 1a76625f 2018-10-22 stsp int errcode;
6293 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6294 1a76625f 2018-10-22 stsp if (errcode)
6295 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6296 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6297 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6298 1a76625f 2018-10-22 stsp if (errcode)
6299 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6300 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6301 1a76625f 2018-10-22 stsp if (errcode)
6302 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6303 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6304 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6305 245d91c1 2018-07-12 stsp err = NULL;
6306 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6307 245d91c1 2018-07-12 stsp }
6308 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6309 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6310 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6311 1d0f4054 2021-06-17 stsp if (err == NULL)
6312 1d0f4054 2021-06-17 stsp err = close_err;
6313 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6314 245d91c1 2018-07-12 stsp }
6315 245d91c1 2018-07-12 stsp if (blame->f) {
6316 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6317 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6318 245d91c1 2018-07-12 stsp blame->f = NULL;
6319 245d91c1 2018-07-12 stsp }
6320 57670559 2018-12-23 stsp if (blame->lines) {
6321 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6322 57670559 2018-12-23 stsp free(blame->lines[i].id);
6323 57670559 2018-12-23 stsp free(blame->lines);
6324 57670559 2018-12-23 stsp blame->lines = NULL;
6325 57670559 2018-12-23 stsp }
6326 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6327 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6328 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6329 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6330 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6331 7cd52833 2022-06-23 thomas if (err == NULL)
6332 7cd52833 2022-06-23 thomas err = pack_err;
6333 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6334 7cd52833 2022-06-23 thomas }
6335 245d91c1 2018-07-12 stsp return err;
6336 245d91c1 2018-07-12 stsp }
6337 245d91c1 2018-07-12 stsp
6338 245d91c1 2018-07-12 stsp static const struct got_error *
6339 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6340 fc06ba56 2019-08-22 stsp {
6341 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6342 fc06ba56 2019-08-22 stsp int *done = arg;
6343 fc06ba56 2019-08-22 stsp int errcode;
6344 fc06ba56 2019-08-22 stsp
6345 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6346 fc06ba56 2019-08-22 stsp if (errcode)
6347 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6348 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6349 fc06ba56 2019-08-22 stsp
6350 fc06ba56 2019-08-22 stsp if (*done)
6351 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6352 fc06ba56 2019-08-22 stsp
6353 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6354 fc06ba56 2019-08-22 stsp if (errcode)
6355 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6356 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6357 fc06ba56 2019-08-22 stsp
6358 fc06ba56 2019-08-22 stsp return err;
6359 fc06ba56 2019-08-22 stsp }
6360 fc06ba56 2019-08-22 stsp
6361 fc06ba56 2019-08-22 stsp static const struct got_error *
6362 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6363 245d91c1 2018-07-12 stsp {
6364 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6365 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6366 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6367 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6368 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6369 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6370 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6371 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6372 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6373 a70480e0 2018-06-23 stsp
6374 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6375 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6376 27d434c2 2018-09-15 stsp if (err)
6377 15a94983 2018-12-23 stsp return err;
6378 f4ae6ddb 2022-07-01 thomas
6379 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6380 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6381 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6382 f4ae6ddb 2022-07-01 thomas goto done;
6383 f4ae6ddb 2022-07-01 thomas }
6384 945f9229 2022-04-16 thomas
6385 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6386 945f9229 2022-04-16 thomas if (err)
6387 945f9229 2022-04-16 thomas goto done;
6388 27d434c2 2018-09-15 stsp
6389 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6390 84451b3e 2018-07-10 stsp if (err)
6391 84451b3e 2018-07-10 stsp goto done;
6392 27d434c2 2018-09-15 stsp
6393 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6394 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6395 84451b3e 2018-07-10 stsp goto done;
6396 84451b3e 2018-07-10 stsp }
6397 a70480e0 2018-06-23 stsp
6398 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6399 a70480e0 2018-06-23 stsp if (err)
6400 a70480e0 2018-06-23 stsp goto done;
6401 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6402 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6403 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6404 84451b3e 2018-07-10 stsp goto done;
6405 84451b3e 2018-07-10 stsp }
6406 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6407 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6408 1fddf795 2021-01-20 stsp if (err)
6409 1fddf795 2021-01-20 stsp goto done;
6410 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6411 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6412 84451b3e 2018-07-10 stsp goto done;
6413 1fddf795 2021-01-20 stsp }
6414 b02560ec 2019-08-19 stsp
6415 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6416 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6417 b02560ec 2019-08-19 stsp blame->nlines--;
6418 a70480e0 2018-06-23 stsp
6419 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6420 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6421 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6422 84451b3e 2018-07-10 stsp goto done;
6423 84451b3e 2018-07-10 stsp }
6424 a70480e0 2018-06-23 stsp
6425 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6426 bd24772e 2018-07-11 stsp if (err)
6427 bd24772e 2018-07-11 stsp goto done;
6428 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6429 7cd52833 2022-06-23 thomas pack_fds);
6430 7cd52833 2022-06-23 thomas if (err)
6431 7cd52833 2022-06-23 thomas goto done;
6432 bd24772e 2018-07-11 stsp
6433 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6434 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6435 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6436 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6437 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6438 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6439 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6440 245d91c1 2018-07-12 stsp goto done;
6441 245d91c1 2018-07-12 stsp }
6442 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6443 245d91c1 2018-07-12 stsp
6444 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6445 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6446 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6447 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6448 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6449 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6450 a5388363 2020-12-01 naddy s->blame_complete = 0;
6451 f5a09613 2020-12-13 naddy
6452 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6453 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6454 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6455 f5a09613 2020-12-13 naddy s->selected_line = 1;
6456 f5a09613 2020-12-13 naddy }
6457 aa61903a 2021-12-31 thomas s->matched_line = 0;
6458 245d91c1 2018-07-12 stsp
6459 245d91c1 2018-07-12 stsp done:
6460 945f9229 2022-04-16 thomas if (commit)
6461 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6462 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6463 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6464 245d91c1 2018-07-12 stsp if (blob)
6465 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6466 27d434c2 2018-09-15 stsp free(obj_id);
6467 245d91c1 2018-07-12 stsp if (err)
6468 245d91c1 2018-07-12 stsp stop_blame(blame);
6469 245d91c1 2018-07-12 stsp return err;
6470 245d91c1 2018-07-12 stsp }
6471 245d91c1 2018-07-12 stsp
6472 245d91c1 2018-07-12 stsp static const struct got_error *
6473 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6474 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6475 245d91c1 2018-07-12 stsp {
6476 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6477 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6478 dbc6a6b6 2018-07-12 stsp
6479 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6480 245d91c1 2018-07-12 stsp
6481 c4843652 2019-08-12 stsp s->path = strdup(path);
6482 c4843652 2019-08-12 stsp if (s->path == NULL)
6483 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6484 c4843652 2019-08-12 stsp
6485 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6486 c4843652 2019-08-12 stsp if (err) {
6487 c4843652 2019-08-12 stsp free(s->path);
6488 7cbe629d 2018-08-04 stsp return err;
6489 c4843652 2019-08-12 stsp }
6490 245d91c1 2018-07-12 stsp
6491 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6492 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6493 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6494 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6495 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6496 fb2756b9 2018-08-04 stsp s->repo = repo;
6497 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6498 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6499 7cbe629d 2018-08-04 stsp
6500 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6501 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6502 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6503 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6504 11b20872 2019-11-08 stsp if (err)
6505 11b20872 2019-11-08 stsp return err;
6506 11b20872 2019-11-08 stsp }
6507 11b20872 2019-11-08 stsp
6508 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6509 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6510 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6511 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6512 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6513 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6514 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6515 e5a0f69f 2018-08-18 stsp
6516 a5388363 2020-12-01 naddy return run_blame(view);
6517 7cbe629d 2018-08-04 stsp }
6518 7cbe629d 2018-08-04 stsp
6519 e5a0f69f 2018-08-18 stsp static const struct got_error *
6520 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6521 7cbe629d 2018-08-04 stsp {
6522 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6523 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6524 7cbe629d 2018-08-04 stsp
6525 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6526 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6527 e5a0f69f 2018-08-18 stsp
6528 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6529 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6530 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6531 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6532 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6533 7cbe629d 2018-08-04 stsp }
6534 e5a0f69f 2018-08-18 stsp
6535 e5a0f69f 2018-08-18 stsp free(s->path);
6536 11b20872 2019-11-08 stsp free_colors(&s->colors);
6537 e5a0f69f 2018-08-18 stsp return err;
6538 7cbe629d 2018-08-04 stsp }
6539 7cbe629d 2018-08-04 stsp
6540 7cbe629d 2018-08-04 stsp static const struct got_error *
6541 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6542 6c4c42e0 2019-06-24 stsp {
6543 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6544 6c4c42e0 2019-06-24 stsp
6545 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6546 6c4c42e0 2019-06-24 stsp return NULL;
6547 2a7d3cd7 2022-09-17 thomas }
6548 2a7d3cd7 2022-09-17 thomas
6549 2a7d3cd7 2022-09-17 thomas static void
6550 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6551 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6552 2a7d3cd7 2022-09-17 thomas {
6553 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6554 2a7d3cd7 2022-09-17 thomas
6555 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6556 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6557 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6558 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6559 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6560 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6561 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6562 6c4c42e0 2019-06-24 stsp }
6563 6c4c42e0 2019-06-24 stsp
6564 6c4c42e0 2019-06-24 stsp static const struct got_error *
6565 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6566 7cbe629d 2018-08-04 stsp {
6567 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6568 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6569 2b380cc8 2018-10-24 stsp int errcode;
6570 2b380cc8 2018-10-24 stsp
6571 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6572 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6573 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6574 2b380cc8 2018-10-24 stsp if (errcode)
6575 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6576 51fe7530 2019-08-19 stsp
6577 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6578 2b380cc8 2018-10-24 stsp }
6579 e5a0f69f 2018-08-18 stsp
6580 51fe7530 2019-08-19 stsp if (s->blame_complete)
6581 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6582 51fe7530 2019-08-19 stsp
6583 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6584 e5a0f69f 2018-08-18 stsp
6585 a5d43cac 2022-07-01 thomas view_border(view);
6586 e5a0f69f 2018-08-18 stsp return err;
6587 e5a0f69f 2018-08-18 stsp }
6588 e5a0f69f 2018-08-18 stsp
6589 e5a0f69f 2018-08-18 stsp static const struct got_error *
6590 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6591 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6592 eaeaa612 2022-07-20 thomas {
6593 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6594 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6595 eaeaa612 2022-07-20 thomas
6596 eaeaa612 2022-07-20 thomas *new_view = NULL;
6597 eaeaa612 2022-07-20 thomas
6598 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6599 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6600 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6601 eaeaa612 2022-07-20 thomas
6602 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6603 eaeaa612 2022-07-20 thomas if (err)
6604 eaeaa612 2022-07-20 thomas view_close(log_view);
6605 eaeaa612 2022-07-20 thomas else
6606 eaeaa612 2022-07-20 thomas *new_view = log_view;
6607 eaeaa612 2022-07-20 thomas
6608 eaeaa612 2022-07-20 thomas return err;
6609 eaeaa612 2022-07-20 thomas }
6610 eaeaa612 2022-07-20 thomas
6611 eaeaa612 2022-07-20 thomas static const struct got_error *
6612 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6613 e5a0f69f 2018-08-18 stsp {
6614 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6615 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6616 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6617 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6618 a5d43cac 2022-07-01 thomas
6619 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6620 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6621 a5d43cac 2022-07-01 thomas --eos; /* border */
6622 7cbe629d 2018-08-04 stsp
6623 e5a0f69f 2018-08-18 stsp switch (ch) {
6624 05171be4 2022-06-23 thomas case '0':
6625 05171be4 2022-06-23 thomas case '$':
6626 05171be4 2022-06-23 thomas case KEY_RIGHT:
6627 05171be4 2022-06-23 thomas case 'l':
6628 05171be4 2022-06-23 thomas case KEY_LEFT:
6629 05171be4 2022-06-23 thomas case 'h':
6630 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6631 05171be4 2022-06-23 thomas break;
6632 1e37a5c2 2019-05-12 jcs case 'q':
6633 1e37a5c2 2019-05-12 jcs s->done = 1;
6634 4deef56f 2021-09-02 naddy break;
6635 4deef56f 2021-09-02 naddy case 'g':
6636 4deef56f 2021-09-02 naddy case KEY_HOME:
6637 4deef56f 2021-09-02 naddy s->selected_line = 1;
6638 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6639 07b0611c 2022-06-23 thomas view->count = 0;
6640 4deef56f 2021-09-02 naddy break;
6641 4deef56f 2021-09-02 naddy case 'G':
6642 4deef56f 2021-09-02 naddy case KEY_END:
6643 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6644 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6645 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6646 4deef56f 2021-09-02 naddy } else {
6647 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6648 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6649 4deef56f 2021-09-02 naddy }
6650 07b0611c 2022-06-23 thomas view->count = 0;
6651 1e37a5c2 2019-05-12 jcs break;
6652 1e37a5c2 2019-05-12 jcs case 'k':
6653 1e37a5c2 2019-05-12 jcs case KEY_UP:
6654 f7140bf5 2021-10-17 thomas case CTRL('p'):
6655 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6656 1e37a5c2 2019-05-12 jcs s->selected_line--;
6657 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6658 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6659 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6660 07b0611c 2022-06-23 thomas else
6661 07b0611c 2022-06-23 thomas view->count = 0;
6662 1e37a5c2 2019-05-12 jcs break;
6663 70f17a53 2022-06-13 thomas case CTRL('u'):
6664 23427b14 2022-06-23 thomas case 'u':
6665 70f17a53 2022-06-13 thomas nscroll /= 2;
6666 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6667 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6668 ea025d1d 2020-02-22 naddy case CTRL('b'):
6669 1c5e5faa 2022-06-23 thomas case 'b':
6670 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6671 07b0611c 2022-06-23 thomas if (view->count > 1)
6672 07b0611c 2022-06-23 thomas nscroll += nscroll;
6673 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6674 07b0611c 2022-06-23 thomas view->count = 0;
6675 e5a0f69f 2018-08-18 stsp break;
6676 1e37a5c2 2019-05-12 jcs }
6677 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6678 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6679 1e37a5c2 2019-05-12 jcs else
6680 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6681 1e37a5c2 2019-05-12 jcs break;
6682 1e37a5c2 2019-05-12 jcs case 'j':
6683 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6684 f7140bf5 2021-10-17 thomas case CTRL('n'):
6685 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6686 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6687 1e37a5c2 2019-05-12 jcs s->selected_line++;
6688 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6689 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6690 07b0611c 2022-06-23 thomas else
6691 07b0611c 2022-06-23 thomas view->count = 0;
6692 1e37a5c2 2019-05-12 jcs break;
6693 1c5e5faa 2022-06-23 thomas case 'c':
6694 1e37a5c2 2019-05-12 jcs case 'p': {
6695 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6696 07b0611c 2022-06-23 thomas
6697 07b0611c 2022-06-23 thomas view->count = 0;
6698 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6699 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6700 1e37a5c2 2019-05-12 jcs if (id == NULL)
6701 e5a0f69f 2018-08-18 stsp break;
6702 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6703 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6704 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6705 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6706 1e37a5c2 2019-05-12 jcs int obj_type;
6707 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6708 1e37a5c2 2019-05-12 jcs s->repo, id);
6709 e5a0f69f 2018-08-18 stsp if (err)
6710 e5a0f69f 2018-08-18 stsp break;
6711 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6712 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6713 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6714 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6715 e5a0f69f 2018-08-18 stsp break;
6716 e5a0f69f 2018-08-18 stsp }
6717 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6718 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6719 ec242592 2022-04-22 thomas s->repo, &pid->id);
6720 945f9229 2022-04-16 thomas if (err)
6721 945f9229 2022-04-16 thomas break;
6722 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6723 945f9229 2022-04-16 thomas pcommit, s->path);
6724 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6725 e5a0f69f 2018-08-18 stsp if (err) {
6726 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6727 1e37a5c2 2019-05-12 jcs err = NULL;
6728 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6729 e5a0f69f 2018-08-18 stsp break;
6730 e5a0f69f 2018-08-18 stsp }
6731 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6732 1e37a5c2 2019-05-12 jcs blob_id);
6733 1e37a5c2 2019-05-12 jcs free(blob_id);
6734 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6735 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6736 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6737 e5a0f69f 2018-08-18 stsp break;
6738 1e37a5c2 2019-05-12 jcs }
6739 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6740 ec242592 2022-04-22 thomas &pid->id);
6741 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6742 1e37a5c2 2019-05-12 jcs } else {
6743 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6744 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6745 1e37a5c2 2019-05-12 jcs break;
6746 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6747 1e37a5c2 2019-05-12 jcs id);
6748 1e37a5c2 2019-05-12 jcs }
6749 1e37a5c2 2019-05-12 jcs if (err)
6750 e5a0f69f 2018-08-18 stsp break;
6751 1e37a5c2 2019-05-12 jcs s->done = 1;
6752 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6753 1e37a5c2 2019-05-12 jcs s->done = 0;
6754 1e37a5c2 2019-05-12 jcs if (thread_err)
6755 1e37a5c2 2019-05-12 jcs break;
6756 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6757 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6758 a5388363 2020-12-01 naddy err = run_blame(view);
6759 1e37a5c2 2019-05-12 jcs if (err)
6760 1e37a5c2 2019-05-12 jcs break;
6761 1e37a5c2 2019-05-12 jcs break;
6762 1e37a5c2 2019-05-12 jcs }
6763 1c5e5faa 2022-06-23 thomas case 'C': {
6764 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6765 07b0611c 2022-06-23 thomas
6766 07b0611c 2022-06-23 thomas view->count = 0;
6767 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6768 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6769 1e37a5c2 2019-05-12 jcs break;
6770 1e37a5c2 2019-05-12 jcs s->done = 1;
6771 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6772 1e37a5c2 2019-05-12 jcs s->done = 0;
6773 1e37a5c2 2019-05-12 jcs if (thread_err)
6774 1e37a5c2 2019-05-12 jcs break;
6775 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6776 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6777 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6778 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6779 a5388363 2020-12-01 naddy err = run_blame(view);
6780 1e37a5c2 2019-05-12 jcs if (err)
6781 1e37a5c2 2019-05-12 jcs break;
6782 1e37a5c2 2019-05-12 jcs break;
6783 1e37a5c2 2019-05-12 jcs }
6784 2a31b33b 2022-07-23 thomas case 'L':
6785 eaeaa612 2022-07-20 thomas view->count = 0;
6786 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6787 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6788 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6789 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6790 eaeaa612 2022-07-20 thomas break;
6791 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6792 1e37a5c2 2019-05-12 jcs case '\r': {
6793 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6794 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6795 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6796 07b0611c 2022-06-23 thomas
6797 07b0611c 2022-06-23 thomas view->count = 0;
6798 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6799 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6800 1e37a5c2 2019-05-12 jcs if (id == NULL)
6801 1e37a5c2 2019-05-12 jcs break;
6802 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6803 1e37a5c2 2019-05-12 jcs if (err)
6804 1e37a5c2 2019-05-12 jcs break;
6805 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6806 4fc71f3b 2022-07-12 thomas if (*new_view) {
6807 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6808 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6809 4fc71f3b 2022-07-12 thomas if (err)
6810 4fc71f3b 2022-07-12 thomas break;
6811 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6812 4fc71f3b 2022-07-12 thomas } else {
6813 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6814 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6815 a5d43cac 2022-07-01 thomas
6816 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6817 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6818 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6819 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6820 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6821 4fc71f3b 2022-07-12 thomas break;
6822 4fc71f3b 2022-07-12 thomas }
6823 15a94983 2018-12-23 stsp }
6824 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6825 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6826 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6827 1e37a5c2 2019-05-12 jcs if (err) {
6828 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6829 1e37a5c2 2019-05-12 jcs break;
6830 1e37a5c2 2019-05-12 jcs }
6831 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6832 4fc71f3b 2022-07-12 thomas s->selected_line;
6833 4fc71f3b 2022-07-12 thomas if (*new_view)
6834 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6835 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6836 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6837 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6838 a5d43cac 2022-07-01 thomas if (err)
6839 a5d43cac 2022-07-01 thomas break;
6840 a5d43cac 2022-07-01 thomas }
6841 a5d43cac 2022-07-01 thomas
6842 e78dc838 2020-12-04 stsp view->focussed = 0;
6843 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6844 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6845 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6846 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6847 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6848 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6849 1e37a5c2 2019-05-12 jcs if (err)
6850 34bc9ec9 2019-02-22 stsp break;
6851 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6852 40236d76 2022-06-23 thomas if (err)
6853 40236d76 2022-06-23 thomas break;
6854 e78dc838 2020-12-04 stsp view->focus_child = 1;
6855 1e37a5c2 2019-05-12 jcs } else
6856 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6857 1e37a5c2 2019-05-12 jcs if (err)
6858 e5a0f69f 2018-08-18 stsp break;
6859 1e37a5c2 2019-05-12 jcs break;
6860 1e37a5c2 2019-05-12 jcs }
6861 70f17a53 2022-06-13 thomas case CTRL('d'):
6862 23427b14 2022-06-23 thomas case 'd':
6863 70f17a53 2022-06-13 thomas nscroll /= 2;
6864 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6865 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6866 ea025d1d 2020-02-22 naddy case CTRL('f'):
6867 1c5e5faa 2022-06-23 thomas case 'f':
6868 1e37a5c2 2019-05-12 jcs case ' ':
6869 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6870 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6871 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6872 07b0611c 2022-06-23 thomas view->count = 0;
6873 e5a0f69f 2018-08-18 stsp break;
6874 1e37a5c2 2019-05-12 jcs }
6875 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6876 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6877 70f17a53 2022-06-13 thomas s->selected_line +=
6878 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6879 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6880 1e37a5c2 2019-05-12 jcs }
6881 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6882 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6883 1e37a5c2 2019-05-12 jcs else
6884 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6885 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6886 1e37a5c2 2019-05-12 jcs break;
6887 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6888 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6889 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6890 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6891 1e37a5c2 2019-05-12 jcs }
6892 1e37a5c2 2019-05-12 jcs break;
6893 1e37a5c2 2019-05-12 jcs default:
6894 07b0611c 2022-06-23 thomas view->count = 0;
6895 1e37a5c2 2019-05-12 jcs break;
6896 a70480e0 2018-06-23 stsp }
6897 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6898 adf4c9e0 2022-07-03 thomas }
6899 adf4c9e0 2022-07-03 thomas
6900 adf4c9e0 2022-07-03 thomas static const struct got_error *
6901 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6902 adf4c9e0 2022-07-03 thomas {
6903 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6904 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6905 adf4c9e0 2022-07-03 thomas
6906 adf4c9e0 2022-07-03 thomas view->count = 0;
6907 adf4c9e0 2022-07-03 thomas s->done = 1;
6908 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6909 adf4c9e0 2022-07-03 thomas s->done = 0;
6910 adf4c9e0 2022-07-03 thomas if (err)
6911 adf4c9e0 2022-07-03 thomas return err;
6912 adf4c9e0 2022-07-03 thomas return run_blame(view);
6913 a70480e0 2018-06-23 stsp }
6914 a70480e0 2018-06-23 stsp
6915 a70480e0 2018-06-23 stsp static const struct got_error *
6916 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6917 9f7d7167 2018-04-29 stsp {
6918 b85a3496 2023-04-14 thomas const struct got_error *io_err, *error;
6919 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6920 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6921 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6922 0587e10c 2020-07-23 stsp char *link_target = NULL;
6923 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6924 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6925 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6926 a70480e0 2018-06-23 stsp int ch;
6927 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6928 b85a3496 2023-04-14 thomas struct tog_io *tog_io = NULL;
6929 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6930 a70480e0 2018-06-23 stsp
6931 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6932 a70480e0 2018-06-23 stsp switch (ch) {
6933 a70480e0 2018-06-23 stsp case 'c':
6934 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6935 a70480e0 2018-06-23 stsp break;
6936 69069811 2018-08-02 stsp case 'r':
6937 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6938 69069811 2018-08-02 stsp if (repo_path == NULL)
6939 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6940 9ba1d308 2019-10-21 stsp optarg);
6941 69069811 2018-08-02 stsp break;
6942 a70480e0 2018-06-23 stsp default:
6943 17020d27 2019-03-07 stsp usage_blame();
6944 a70480e0 2018-06-23 stsp /* NOTREACHED */
6945 a70480e0 2018-06-23 stsp }
6946 a70480e0 2018-06-23 stsp }
6947 a70480e0 2018-06-23 stsp
6948 a70480e0 2018-06-23 stsp argc -= optind;
6949 a70480e0 2018-06-23 stsp argv += optind;
6950 a70480e0 2018-06-23 stsp
6951 f135c941 2020-02-20 stsp if (argc != 1)
6952 a70480e0 2018-06-23 stsp usage_blame();
6953 6962eb72 2020-02-20 stsp
6954 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6955 7cd52833 2022-06-23 thomas if (error != NULL)
6956 7cd52833 2022-06-23 thomas goto done;
6957 7cd52833 2022-06-23 thomas
6958 69069811 2018-08-02 stsp if (repo_path == NULL) {
6959 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6960 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6961 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6962 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6963 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6964 c156c7a4 2020-12-18 stsp goto done;
6965 f135c941 2020-02-20 stsp if (worktree)
6966 eb41ed75 2019-02-05 stsp repo_path =
6967 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6968 f135c941 2020-02-20 stsp else
6969 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6970 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6971 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6972 c156c7a4 2020-12-18 stsp goto done;
6973 c156c7a4 2020-12-18 stsp }
6974 f135c941 2020-02-20 stsp }
6975 a915003a 2019-02-05 stsp
6976 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6977 c02c541e 2019-03-29 stsp if (error != NULL)
6978 8e94dd5b 2019-01-04 stsp goto done;
6979 69069811 2018-08-02 stsp
6980 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6981 f135c941 2020-02-20 stsp worktree);
6982 c02c541e 2019-03-29 stsp if (error)
6983 92205607 2019-01-04 stsp goto done;
6984 69069811 2018-08-02 stsp
6985 b85a3496 2023-04-14 thomas error = init_curses(&tog_io);
6986 b85a3496 2023-04-14 thomas if (error)
6987 b85a3496 2023-04-14 thomas goto done;
6988 f135c941 2020-02-20 stsp
6989 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6990 51a10b52 2020-12-26 stsp if (error)
6991 51a10b52 2020-12-26 stsp goto done;
6992 51a10b52 2020-12-26 stsp
6993 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6994 eb41ed75 2019-02-05 stsp if (error)
6995 69069811 2018-08-02 stsp goto done;
6996 a70480e0 2018-06-23 stsp
6997 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6998 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6999 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
7000 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
7001 a70480e0 2018-06-23 stsp if (error != NULL)
7002 66b4983c 2018-06-23 stsp goto done;
7003 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
7004 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
7005 a70480e0 2018-06-23 stsp } else {
7006 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7007 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7008 a70480e0 2018-06-23 stsp }
7009 a19e88aa 2018-06-23 stsp if (error != NULL)
7010 8b473291 2019-02-21 stsp goto done;
7011 8b473291 2019-02-21 stsp
7012 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
7013 e1cd8fed 2018-08-01 stsp if (view == NULL) {
7014 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7015 e1cd8fed 2018-08-01 stsp goto done;
7016 e1cd8fed 2018-08-01 stsp }
7017 0587e10c 2020-07-23 stsp
7018 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7019 945f9229 2022-04-16 thomas if (error)
7020 945f9229 2022-04-16 thomas goto done;
7021 945f9229 2022-04-16 thomas
7022 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
7023 945f9229 2022-04-16 thomas commit, repo);
7024 7cbe629d 2018-08-04 stsp if (error)
7025 7cbe629d 2018-08-04 stsp goto done;
7026 0587e10c 2020-07-23 stsp
7027 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
7028 78756c87 2020-11-24 stsp commit_id, repo);
7029 0587e10c 2020-07-23 stsp if (error)
7030 0587e10c 2020-07-23 stsp goto done;
7031 12314ad4 2019-08-31 stsp if (worktree) {
7032 12314ad4 2019-08-31 stsp /* Release work tree lock. */
7033 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
7034 12314ad4 2019-08-31 stsp worktree = NULL;
7035 12314ad4 2019-08-31 stsp }
7036 b85a3496 2023-04-14 thomas error = view_loop(view, tog_io);
7037 a70480e0 2018-06-23 stsp done:
7038 69069811 2018-08-02 stsp free(repo_path);
7039 f135c941 2020-02-20 stsp free(in_repo_path);
7040 0587e10c 2020-07-23 stsp free(link_target);
7041 69069811 2018-08-02 stsp free(cwd);
7042 a70480e0 2018-06-23 stsp free(commit_id);
7043 945f9229 2022-04-16 thomas if (commit)
7044 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7045 eb41ed75 2019-02-05 stsp if (worktree)
7046 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
7047 1d0f4054 2021-06-17 stsp if (repo) {
7048 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7049 1d0f4054 2021-06-17 stsp if (error == NULL)
7050 1d0f4054 2021-06-17 stsp error = close_err;
7051 1d0f4054 2021-06-17 stsp }
7052 7cd52833 2022-06-23 thomas if (pack_fds) {
7053 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7054 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7055 7cd52833 2022-06-23 thomas if (error == NULL)
7056 7cd52833 2022-06-23 thomas error = pack_err;
7057 7cd52833 2022-06-23 thomas }
7058 b85a3496 2023-04-14 thomas if (tog_io != NULL) {
7059 b85a3496 2023-04-14 thomas io_err = tog_io_close(tog_io);
7060 b85a3496 2023-04-14 thomas if (error == NULL)
7061 b85a3496 2023-04-14 thomas error = io_err;
7062 b85a3496 2023-04-14 thomas }
7063 51a10b52 2020-12-26 stsp tog_free_refs();
7064 a70480e0 2018-06-23 stsp return error;
7065 ffd1d5e5 2018-06-23 stsp }
7066 ffd1d5e5 2018-06-23 stsp
7067 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7068 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
7069 ffd1d5e5 2018-06-23 stsp {
7070 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
7071 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7072 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
7073 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
7074 86f4aab9 2022-09-09 thomas char *index = NULL;
7075 f26dddb7 2019-11-08 stsp struct tog_color *tc;
7076 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
7077 d86d3b18 2020-12-01 naddy int limit = view->nlines;
7078 ffd1d5e5 2018-06-23 stsp
7079 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
7080 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7081 a5d43cac 2022-07-01 thomas --limit; /* border */
7082 ffd1d5e5 2018-06-23 stsp
7083 f7d12f7e 2018-08-01 stsp werase(view->window);
7084 ffd1d5e5 2018-06-23 stsp
7085 ffd1d5e5 2018-06-23 stsp if (limit == 0)
7086 ffd1d5e5 2018-06-23 stsp return NULL;
7087 ffd1d5e5 2018-06-23 stsp
7088 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
7089 f91a2b48 2022-06-23 thomas 0, 0);
7090 ffd1d5e5 2018-06-23 stsp if (err)
7091 ffd1d5e5 2018-06-23 stsp return err;
7092 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7093 a3404814 2018-09-02 stsp wstandout(view->window);
7094 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
7095 11b20872 2019-11-08 stsp if (tc)
7096 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
7097 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7098 86f4aab9 2022-09-09 thomas free(wline);
7099 86f4aab9 2022-09-09 thomas wline = NULL;
7100 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7101 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7102 11b20872 2019-11-08 stsp if (tc)
7103 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
7104 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
7105 a3404814 2018-09-02 stsp wstandend(view->window);
7106 86f4aab9 2022-09-09 thomas if (--limit <= 0)
7107 86f4aab9 2022-09-09 thomas return NULL;
7108 07dd3ed3 2022-08-06 thomas
7109 6f5f393a 2022-08-12 thomas i += s->selected;
7110 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
7111 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
7112 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
7113 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
7114 07dd3ed3 2022-08-06 thomas }
7115 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
7116 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
7117 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
7118 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
7119 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
7120 86f4aab9 2022-09-09 thomas free(index);
7121 ce52c690 2018-06-23 stsp if (err)
7122 ce52c690 2018-06-23 stsp return err;
7123 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
7124 2550e4c3 2018-07-13 stsp free(wline);
7125 2550e4c3 2018-07-13 stsp wline = NULL;
7126 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
7127 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7128 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7129 ffd1d5e5 2018-06-23 stsp return NULL;
7130 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7131 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
7132 a1eca9bb 2018-06-23 stsp return NULL;
7133 ffd1d5e5 2018-06-23 stsp
7134 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
7135 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
7136 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
7137 0cf4efb1 2018-09-29 stsp if (view->focussed)
7138 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7139 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
7140 ffd1d5e5 2018-06-23 stsp }
7141 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
7142 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
7143 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7144 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7145 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7146 ffd1d5e5 2018-06-23 stsp return NULL;
7147 ffd1d5e5 2018-06-23 stsp n = 1;
7148 ffd1d5e5 2018-06-23 stsp } else {
7149 ffd1d5e5 2018-06-23 stsp n = 0;
7150 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
7151 ffd1d5e5 2018-06-23 stsp }
7152 ffd1d5e5 2018-06-23 stsp
7153 c72de8ab 2023-02-03 thomas view->maxx = 0;
7154 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
7155 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
7156 848d6979 2019-08-12 stsp const char *modestr = "";
7157 56e0773d 2019-11-28 stsp mode_t mode;
7158 1d13200f 2018-07-12 stsp
7159 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
7160 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
7161 56e0773d 2019-11-28 stsp
7162 d86d3b18 2020-12-01 naddy if (s->show_ids) {
7163 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
7164 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
7165 1d13200f 2018-07-12 stsp if (err)
7166 638f9024 2019-05-13 stsp return got_error_from_errno(
7167 230a42bd 2019-05-11 jcs "got_object_id_str");
7168 1d13200f 2018-07-12 stsp }
7169 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
7170 63c5ca5d 2019-08-24 stsp modestr = "$";
7171 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
7172 0d6c6ee3 2020-05-20 stsp int i;
7173 0d6c6ee3 2020-05-20 stsp
7174 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
7175 d86d3b18 2020-12-01 naddy te, s->repo);
7176 0d6c6ee3 2020-05-20 stsp if (err) {
7177 0d6c6ee3 2020-05-20 stsp free(id_str);
7178 0d6c6ee3 2020-05-20 stsp return err;
7179 0d6c6ee3 2020-05-20 stsp }
7180 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
7181 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
7182 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
7183 0d6c6ee3 2020-05-20 stsp }
7184 848d6979 2019-08-12 stsp modestr = "@";
7185 0d6c6ee3 2020-05-20 stsp }
7186 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
7187 848d6979 2019-08-12 stsp modestr = "/";
7188 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
7189 848d6979 2019-08-12 stsp modestr = "*";
7190 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
7191 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
7192 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
7193 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
7194 1d13200f 2018-07-12 stsp free(id_str);
7195 0d6c6ee3 2020-05-20 stsp free(link_target);
7196 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
7197 1d13200f 2018-07-12 stsp }
7198 1d13200f 2018-07-12 stsp free(id_str);
7199 0d6c6ee3 2020-05-20 stsp free(link_target);
7200 c72de8ab 2023-02-03 thomas
7201 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
7202 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
7203 ffd1d5e5 2018-06-23 stsp if (err) {
7204 ffd1d5e5 2018-06-23 stsp free(line);
7205 ffd1d5e5 2018-06-23 stsp break;
7206 ffd1d5e5 2018-06-23 stsp }
7207 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
7208 c72de8ab 2023-02-03 thomas free(wline);
7209 c72de8ab 2023-02-03 thomas wline = NULL;
7210 c72de8ab 2023-02-03 thomas
7211 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
7212 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
7213 c72de8ab 2023-02-03 thomas if (err) {
7214 c72de8ab 2023-02-03 thomas free(line);
7215 c72de8ab 2023-02-03 thomas break;
7216 c72de8ab 2023-02-03 thomas }
7217 d86d3b18 2020-12-01 naddy if (n == s->selected) {
7218 0cf4efb1 2018-09-29 stsp if (view->focussed)
7219 0cf4efb1 2018-09-29 stsp wstandout(view->window);
7220 d86d3b18 2020-12-01 naddy s->selected_entry = te;
7221 ffd1d5e5 2018-06-23 stsp }
7222 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
7223 f26dddb7 2019-11-08 stsp if (tc)
7224 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
7225 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7226 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
7227 f26dddb7 2019-11-08 stsp if (tc)
7228 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
7229 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
7230 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
7231 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
7232 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
7233 f7d12f7e 2018-08-01 stsp wstandend(view->window);
7234 ffd1d5e5 2018-06-23 stsp free(line);
7235 2550e4c3 2018-07-13 stsp free(wline);
7236 2550e4c3 2018-07-13 stsp wline = NULL;
7237 ffd1d5e5 2018-06-23 stsp n++;
7238 d86d3b18 2020-12-01 naddy s->ndisplayed++;
7239 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
7240 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
7241 ffd1d5e5 2018-06-23 stsp break;
7242 ffd1d5e5 2018-06-23 stsp }
7243 ffd1d5e5 2018-06-23 stsp
7244 ffd1d5e5 2018-06-23 stsp return err;
7245 ffd1d5e5 2018-06-23 stsp }
7246 ffd1d5e5 2018-06-23 stsp
7247 ffd1d5e5 2018-06-23 stsp static void
7248 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
7249 ffd1d5e5 2018-06-23 stsp {
7250 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7251 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
7252 fa86c4bf 2020-11-29 stsp int i = 0;
7253 ffd1d5e5 2018-06-23 stsp
7254 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
7255 ffd1d5e5 2018-06-23 stsp return;
7256 ffd1d5e5 2018-06-23 stsp
7257 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
7258 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
7259 fa86c4bf 2020-11-29 stsp if (te == NULL) {
7260 fa86c4bf 2020-11-29 stsp if (!isroot)
7261 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
7262 fa86c4bf 2020-11-29 stsp break;
7263 fa86c4bf 2020-11-29 stsp }
7264 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
7265 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
7266 ffd1d5e5 2018-06-23 stsp }
7267 ffd1d5e5 2018-06-23 stsp }
7268 ffd1d5e5 2018-06-23 stsp
7269 a5d43cac 2022-07-01 thomas static const struct got_error *
7270 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
7271 ffd1d5e5 2018-06-23 stsp {
7272 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7273 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
7274 ffd1d5e5 2018-06-23 stsp int n = 0;
7275 ffd1d5e5 2018-06-23 stsp
7276 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7277 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
7278 694d3271 2020-12-01 naddy s->first_displayed_entry);
7279 694d3271 2020-12-01 naddy else
7280 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
7281 56e0773d 2019-11-28 stsp
7282 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7283 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7284 07dd3ed3 2022-08-06 thomas if (last) {
7285 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7286 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7287 07dd3ed3 2022-08-06 thomas }
7288 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7289 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7290 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7291 768394f3 2019-01-24 stsp }
7292 ffd1d5e5 2018-06-23 stsp }
7293 a5d43cac 2022-07-01 thomas
7294 a5d43cac 2022-07-01 thomas return NULL;
7295 ffd1d5e5 2018-06-23 stsp }
7296 ffd1d5e5 2018-06-23 stsp
7297 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7298 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7299 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7300 ffd1d5e5 2018-06-23 stsp {
7301 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7302 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7303 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7304 ffd1d5e5 2018-06-23 stsp
7305 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7306 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7307 56e0773d 2019-11-28 stsp + 1 /* slash */;
7308 ce52c690 2018-06-23 stsp if (te)
7309 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7310 ce52c690 2018-06-23 stsp
7311 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7312 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7313 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7314 ffd1d5e5 2018-06-23 stsp
7315 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7316 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7317 d9765a41 2018-06-23 stsp while (pt) {
7318 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7319 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7320 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7321 cb2ebc8a 2018-06-23 stsp goto done;
7322 cb2ebc8a 2018-06-23 stsp }
7323 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7324 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7325 cb2ebc8a 2018-06-23 stsp goto done;
7326 cb2ebc8a 2018-06-23 stsp }
7327 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7328 ffd1d5e5 2018-06-23 stsp }
7329 ce52c690 2018-06-23 stsp if (te) {
7330 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7331 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7332 ce52c690 2018-06-23 stsp goto done;
7333 ce52c690 2018-06-23 stsp }
7334 cb2ebc8a 2018-06-23 stsp }
7335 ce52c690 2018-06-23 stsp done:
7336 ce52c690 2018-06-23 stsp if (err) {
7337 ce52c690 2018-06-23 stsp free(*path);
7338 ce52c690 2018-06-23 stsp *path = NULL;
7339 ce52c690 2018-06-23 stsp }
7340 ce52c690 2018-06-23 stsp return err;
7341 ce52c690 2018-06-23 stsp }
7342 ce52c690 2018-06-23 stsp
7343 ce52c690 2018-06-23 stsp static const struct got_error *
7344 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7345 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7346 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7347 ce52c690 2018-06-23 stsp {
7348 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7349 ce52c690 2018-06-23 stsp char *path;
7350 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7351 a0de39f3 2019-08-09 stsp
7352 a0de39f3 2019-08-09 stsp *new_view = NULL;
7353 69efd4c4 2018-07-18 stsp
7354 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7355 ce52c690 2018-06-23 stsp if (err)
7356 ce52c690 2018-06-23 stsp return err;
7357 ffd1d5e5 2018-06-23 stsp
7358 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7359 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7360 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7361 83ce39e3 2019-08-12 stsp goto done;
7362 83ce39e3 2019-08-12 stsp }
7363 cdf1ee82 2018-08-01 stsp
7364 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7365 e5a0f69f 2018-08-18 stsp if (err) {
7366 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7367 fc06ba56 2019-08-22 stsp err = NULL;
7368 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7369 e5a0f69f 2018-08-18 stsp } else
7370 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7371 83ce39e3 2019-08-12 stsp done:
7372 83ce39e3 2019-08-12 stsp free(path);
7373 69efd4c4 2018-07-18 stsp return err;
7374 69efd4c4 2018-07-18 stsp }
7375 69efd4c4 2018-07-18 stsp
7376 69efd4c4 2018-07-18 stsp static const struct got_error *
7377 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7378 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7379 69efd4c4 2018-07-18 stsp {
7380 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7381 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7382 69efd4c4 2018-07-18 stsp char *path;
7383 a0de39f3 2019-08-09 stsp
7384 a0de39f3 2019-08-09 stsp *new_view = NULL;
7385 69efd4c4 2018-07-18 stsp
7386 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7387 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7388 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7389 e5a0f69f 2018-08-18 stsp
7390 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7391 69efd4c4 2018-07-18 stsp if (err)
7392 69efd4c4 2018-07-18 stsp return err;
7393 69efd4c4 2018-07-18 stsp
7394 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7395 4e97c21c 2020-12-06 stsp path, 0);
7396 ba4f502b 2018-08-04 stsp if (err)
7397 e5a0f69f 2018-08-18 stsp view_close(log_view);
7398 e5a0f69f 2018-08-18 stsp else
7399 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7400 cb2ebc8a 2018-06-23 stsp free(path);
7401 cb2ebc8a 2018-06-23 stsp return err;
7402 ffd1d5e5 2018-06-23 stsp }
7403 ffd1d5e5 2018-06-23 stsp
7404 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7405 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7406 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7407 ffd1d5e5 2018-06-23 stsp {
7408 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7409 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7410 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7411 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7412 ffd1d5e5 2018-06-23 stsp
7413 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7414 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7415 bc573f3b 2021-07-10 stsp
7416 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7417 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7418 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7419 ffd1d5e5 2018-06-23 stsp
7420 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7421 bc573f3b 2021-07-10 stsp if (err)
7422 bc573f3b 2021-07-10 stsp goto done;
7423 bc573f3b 2021-07-10 stsp
7424 bc573f3b 2021-07-10 stsp /*
7425 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7426 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7427 bc573f3b 2021-07-10 stsp * closed on demand.
7428 bc573f3b 2021-07-10 stsp */
7429 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7430 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7431 bc573f3b 2021-07-10 stsp if (err)
7432 bc573f3b 2021-07-10 stsp goto done;
7433 bc573f3b 2021-07-10 stsp s->tree = s->root;
7434 bc573f3b 2021-07-10 stsp
7435 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7436 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7437 ffd1d5e5 2018-06-23 stsp goto done;
7438 ffd1d5e5 2018-06-23 stsp
7439 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7440 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7441 ffd1d5e5 2018-06-23 stsp goto done;
7442 ffd1d5e5 2018-06-23 stsp }
7443 ffd1d5e5 2018-06-23 stsp
7444 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7445 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7446 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7447 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7448 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7449 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7450 9cd7cbd1 2020-12-07 stsp goto done;
7451 9cd7cbd1 2020-12-07 stsp }
7452 9cd7cbd1 2020-12-07 stsp }
7453 fb2756b9 2018-08-04 stsp s->repo = repo;
7454 c0b01bdb 2019-11-08 stsp
7455 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7456 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7457 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7458 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7459 c0b01bdb 2019-11-08 stsp if (err)
7460 c0b01bdb 2019-11-08 stsp goto done;
7461 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7462 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7463 bc573f3b 2021-07-10 stsp if (err)
7464 c0b01bdb 2019-11-08 stsp goto done;
7465 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7466 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7467 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7468 bc573f3b 2021-07-10 stsp if (err)
7469 c0b01bdb 2019-11-08 stsp goto done;
7470 e5a0f69f 2018-08-18 stsp
7471 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7472 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7473 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7474 bc573f3b 2021-07-10 stsp if (err)
7475 11b20872 2019-11-08 stsp goto done;
7476 11b20872 2019-11-08 stsp
7477 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7478 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7479 bc573f3b 2021-07-10 stsp if (err)
7480 c0b01bdb 2019-11-08 stsp goto done;
7481 c0b01bdb 2019-11-08 stsp }
7482 c0b01bdb 2019-11-08 stsp
7483 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7484 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7485 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7486 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7487 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7488 ad80ab7b 2018-08-04 stsp done:
7489 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7490 bc573f3b 2021-07-10 stsp if (commit)
7491 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7492 bc573f3b 2021-07-10 stsp if (err)
7493 bc573f3b 2021-07-10 stsp close_tree_view(view);
7494 ad80ab7b 2018-08-04 stsp return err;
7495 ad80ab7b 2018-08-04 stsp }
7496 ad80ab7b 2018-08-04 stsp
7497 e5a0f69f 2018-08-18 stsp static const struct got_error *
7498 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7499 ad80ab7b 2018-08-04 stsp {
7500 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7501 ad80ab7b 2018-08-04 stsp
7502 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7503 fb2756b9 2018-08-04 stsp free(s->tree_label);
7504 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7505 6484ec90 2018-09-29 stsp free(s->commit_id);
7506 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7507 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7508 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7509 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7510 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7511 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7512 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7513 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7514 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7515 ad80ab7b 2018-08-04 stsp free(parent);
7516 ad80ab7b 2018-08-04 stsp
7517 ad80ab7b 2018-08-04 stsp }
7518 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7519 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7520 bc573f3b 2021-07-10 stsp if (s->root)
7521 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7522 7c32bd05 2019-06-22 stsp return NULL;
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_start_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 7c32bd05 2019-06-22 stsp
7530 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7531 7c32bd05 2019-06-22 stsp return NULL;
7532 7c32bd05 2019-06-22 stsp }
7533 7c32bd05 2019-06-22 stsp
7534 7c32bd05 2019-06-22 stsp static int
7535 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7536 7c32bd05 2019-06-22 stsp {
7537 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7538 7c32bd05 2019-06-22 stsp
7539 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7540 56e0773d 2019-11-28 stsp 0) == 0;
7541 7c32bd05 2019-06-22 stsp }
7542 7c32bd05 2019-06-22 stsp
7543 7c32bd05 2019-06-22 stsp static const struct got_error *
7544 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7545 7c32bd05 2019-06-22 stsp {
7546 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7547 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7548 7c32bd05 2019-06-22 stsp
7549 7c32bd05 2019-06-22 stsp if (!view->searching) {
7550 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7551 7c32bd05 2019-06-22 stsp return NULL;
7552 7c32bd05 2019-06-22 stsp }
7553 7c32bd05 2019-06-22 stsp
7554 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7555 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7556 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7557 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7558 56e0773d 2019-11-28 stsp s->selected_entry);
7559 7c32bd05 2019-06-22 stsp else
7560 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7561 56e0773d 2019-11-28 stsp } else {
7562 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7563 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7564 56e0773d 2019-11-28 stsp else
7565 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7566 56e0773d 2019-11-28 stsp s->selected_entry);
7567 7c32bd05 2019-06-22 stsp }
7568 7c32bd05 2019-06-22 stsp } else {
7569 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7570 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7571 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7572 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7573 56e0773d 2019-11-28 stsp else
7574 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7575 7c32bd05 2019-06-22 stsp }
7576 7c32bd05 2019-06-22 stsp
7577 7c32bd05 2019-06-22 stsp while (1) {
7578 56e0773d 2019-11-28 stsp if (te == NULL) {
7579 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7580 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7581 ac66afb8 2019-06-24 stsp return NULL;
7582 ac66afb8 2019-06-24 stsp }
7583 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7584 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7585 56e0773d 2019-11-28 stsp else
7586 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7587 7c32bd05 2019-06-22 stsp }
7588 7c32bd05 2019-06-22 stsp
7589 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7590 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7591 56e0773d 2019-11-28 stsp s->matched_entry = te;
7592 7c32bd05 2019-06-22 stsp break;
7593 7c32bd05 2019-06-22 stsp }
7594 7c32bd05 2019-06-22 stsp
7595 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7596 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7597 56e0773d 2019-11-28 stsp else
7598 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7599 7c32bd05 2019-06-22 stsp }
7600 e5a0f69f 2018-08-18 stsp
7601 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7602 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7603 7c32bd05 2019-06-22 stsp s->selected = 0;
7604 7c32bd05 2019-06-22 stsp }
7605 7c32bd05 2019-06-22 stsp
7606 e5a0f69f 2018-08-18 stsp return NULL;
7607 ad80ab7b 2018-08-04 stsp }
7608 ad80ab7b 2018-08-04 stsp
7609 ad80ab7b 2018-08-04 stsp static const struct got_error *
7610 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7611 ad80ab7b 2018-08-04 stsp {
7612 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7613 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7614 e5a0f69f 2018-08-18 stsp char *parent_path;
7615 ad80ab7b 2018-08-04 stsp
7616 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7617 e5a0f69f 2018-08-18 stsp if (err)
7618 e5a0f69f 2018-08-18 stsp return err;
7619 ffd1d5e5 2018-06-23 stsp
7620 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7621 e5a0f69f 2018-08-18 stsp free(parent_path);
7622 669b5ffa 2018-10-07 stsp
7623 a5d43cac 2022-07-01 thomas view_border(view);
7624 e5a0f69f 2018-08-18 stsp return err;
7625 07dd3ed3 2022-08-06 thomas }
7626 07dd3ed3 2022-08-06 thomas
7627 07dd3ed3 2022-08-06 thomas static const struct got_error *
7628 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7629 07dd3ed3 2022-08-06 thomas {
7630 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7631 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7632 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7633 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7634 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7635 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7636 07dd3ed3 2022-08-06 thomas
7637 07dd3ed3 2022-08-06 thomas g = view->gline;
7638 07dd3ed3 2022-08-06 thomas view->gline = 0;
7639 07dd3ed3 2022-08-06 thomas
7640 07dd3ed3 2022-08-06 thomas if (g == 0)
7641 07dd3ed3 2022-08-06 thomas g = 1;
7642 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7643 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7644 07dd3ed3 2022-08-06 thomas
7645 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7646 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7647 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7648 07dd3ed3 2022-08-06 thomas
7649 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7650 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7651 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7652 07dd3ed3 2022-08-06 thomas }
7653 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7654 07dd3ed3 2022-08-06 thomas last += off;
7655 07dd3ed3 2022-08-06 thomas
7656 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7657 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7658 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7659 07dd3ed3 2022-08-06 thomas }
7660 07dd3ed3 2022-08-06 thomas
7661 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7662 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7663 07dd3ed3 2022-08-06 thomas i += off;
7664 07dd3ed3 2022-08-06 thomas }
7665 07dd3ed3 2022-08-06 thomas
7666 07dd3ed3 2022-08-06 thomas if (i < g) {
7667 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7668 07dd3ed3 2022-08-06 thomas if (err)
7669 07dd3ed3 2022-08-06 thomas return err;
7670 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7671 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7672 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7673 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7674 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7675 07dd3ed3 2022-08-06 thomas first += off;
7676 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7677 07dd3ed3 2022-08-06 thomas }
7678 07dd3ed3 2022-08-06 thomas } else if (i > g)
7679 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7680 07dd3ed3 2022-08-06 thomas
7681 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7682 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7683 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7684 07dd3ed3 2022-08-06 thomas
7685 07dd3ed3 2022-08-06 thomas return NULL;
7686 e5a0f69f 2018-08-18 stsp }
7687 ce52c690 2018-06-23 stsp
7688 e5a0f69f 2018-08-18 stsp static const struct got_error *
7689 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7690 e5a0f69f 2018-08-18 stsp {
7691 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7692 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7693 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7694 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7695 ffd1d5e5 2018-06-23 stsp
7696 07dd3ed3 2022-08-06 thomas if (view->gline)
7697 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7698 07dd3ed3 2022-08-06 thomas
7699 e5a0f69f 2018-08-18 stsp switch (ch) {
7700 c72de8ab 2023-02-03 thomas case '0':
7701 c72de8ab 2023-02-03 thomas case '$':
7702 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
7703 c72de8ab 2023-02-03 thomas case 'l':
7704 c72de8ab 2023-02-03 thomas case KEY_LEFT:
7705 c72de8ab 2023-02-03 thomas case 'h':
7706 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
7707 c72de8ab 2023-02-03 thomas break;
7708 1e37a5c2 2019-05-12 jcs case 'i':
7709 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7710 07b0611c 2022-06-23 thomas view->count = 0;
7711 1e37a5c2 2019-05-12 jcs break;
7712 1be4947a 2022-07-22 thomas case 'L':
7713 07b0611c 2022-06-23 thomas view->count = 0;
7714 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7715 ffd1d5e5 2018-06-23 stsp break;
7716 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7717 152c1c93 2020-11-29 stsp break;
7718 1be4947a 2022-07-22 thomas case 'R':
7719 07b0611c 2022-06-23 thomas view->count = 0;
7720 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7721 e4526bf5 2021-09-03 naddy break;
7722 e4526bf5 2021-09-03 naddy case 'g':
7723 aa7a1117 2023-01-09 thomas case '=':
7724 e4526bf5 2021-09-03 naddy case KEY_HOME:
7725 e4526bf5 2021-09-03 naddy s->selected = 0;
7726 07b0611c 2022-06-23 thomas view->count = 0;
7727 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7728 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7729 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7730 e4526bf5 2021-09-03 naddy else
7731 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7732 1e37a5c2 2019-05-12 jcs break;
7733 e4526bf5 2021-09-03 naddy case 'G':
7734 aa7a1117 2023-01-09 thomas case '*':
7735 a5d43cac 2022-07-01 thomas case KEY_END: {
7736 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7737 a5d43cac 2022-07-01 thomas
7738 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7739 a5d43cac 2022-07-01 thomas --eos; /* border */
7740 e4526bf5 2021-09-03 naddy s->selected = 0;
7741 07b0611c 2022-06-23 thomas view->count = 0;
7742 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7743 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7744 e4526bf5 2021-09-03 naddy if (te == NULL) {
7745 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7746 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7747 e4526bf5 2021-09-03 naddy n++;
7748 e4526bf5 2021-09-03 naddy }
7749 e4526bf5 2021-09-03 naddy break;
7750 e4526bf5 2021-09-03 naddy }
7751 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7752 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7753 e4526bf5 2021-09-03 naddy }
7754 e4526bf5 2021-09-03 naddy if (n > 0)
7755 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7756 e4526bf5 2021-09-03 naddy break;
7757 a5d43cac 2022-07-01 thomas }
7758 1e37a5c2 2019-05-12 jcs case 'k':
7759 1e37a5c2 2019-05-12 jcs case KEY_UP:
7760 f7140bf5 2021-10-17 thomas case CTRL('p'):
7761 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7762 1e37a5c2 2019-05-12 jcs s->selected--;
7763 fa86c4bf 2020-11-29 stsp break;
7764 1e37a5c2 2019-05-12 jcs }
7765 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7766 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7767 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7768 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7769 07b0611c 2022-06-23 thomas view->count = 0;
7770 1e37a5c2 2019-05-12 jcs break;
7771 70f17a53 2022-06-13 thomas case CTRL('u'):
7772 23427b14 2022-06-23 thomas case 'u':
7773 70f17a53 2022-06-13 thomas nscroll /= 2;
7774 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7775 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7776 ea025d1d 2020-02-22 naddy case CTRL('b'):
7777 1c5e5faa 2022-06-23 thomas case 'b':
7778 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7779 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7780 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7781 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7782 fa86c4bf 2020-11-29 stsp } else {
7783 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7784 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7785 fa86c4bf 2020-11-29 stsp }
7786 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7787 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7788 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7789 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7790 07b0611c 2022-06-23 thomas view->count = 0;
7791 1e37a5c2 2019-05-12 jcs break;
7792 1e37a5c2 2019-05-12 jcs case 'j':
7793 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7794 f7140bf5 2021-10-17 thomas case CTRL('n'):
7795 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7796 1e37a5c2 2019-05-12 jcs s->selected++;
7797 1e37a5c2 2019-05-12 jcs break;
7798 1e37a5c2 2019-05-12 jcs }
7799 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7800 07b0611c 2022-06-23 thomas == NULL) {
7801 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7802 07b0611c 2022-06-23 thomas view->count = 0;
7803 1e37a5c2 2019-05-12 jcs break;
7804 07b0611c 2022-06-23 thomas }
7805 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7806 1e37a5c2 2019-05-12 jcs break;
7807 70f17a53 2022-06-13 thomas case CTRL('d'):
7808 23427b14 2022-06-23 thomas case 'd':
7809 70f17a53 2022-06-13 thomas nscroll /= 2;
7810 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7811 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7812 ea025d1d 2020-02-22 naddy case CTRL('f'):
7813 1c5e5faa 2022-06-23 thomas case 'f':
7814 4c2d69cb 2022-06-23 thomas case ' ':
7815 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7816 1e37a5c2 2019-05-12 jcs == NULL) {
7817 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7818 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7819 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7820 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7821 07b0611c 2022-06-23 thomas else
7822 07b0611c 2022-06-23 thomas view->count = 0;
7823 1e37a5c2 2019-05-12 jcs break;
7824 1e37a5c2 2019-05-12 jcs }
7825 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7826 1e37a5c2 2019-05-12 jcs break;
7827 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7828 1e37a5c2 2019-05-12 jcs case '\r':
7829 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7830 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7831 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7832 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7833 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7834 07b0611c 2022-06-23 thomas view->count = 0;
7835 1e37a5c2 2019-05-12 jcs break;
7836 07b0611c 2022-06-23 thomas }
7837 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7838 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7839 1e37a5c2 2019-05-12 jcs entry);
7840 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7841 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7842 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7843 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7844 1e37a5c2 2019-05-12 jcs s->selected_entry =
7845 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7846 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7847 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7848 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7849 a5d43cac 2022-07-01 thomas if (err)
7850 a5d43cac 2022-07-01 thomas break;
7851 a5d43cac 2022-07-01 thomas }
7852 1e37a5c2 2019-05-12 jcs free(parent);
7853 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7854 56e0773d 2019-11-28 stsp s->selected_entry))) {
7855 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7856 07b0611c 2022-06-23 thomas view->count = 0;
7857 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7858 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7859 1e37a5c2 2019-05-12 jcs if (err)
7860 1e37a5c2 2019-05-12 jcs break;
7861 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7862 941e9f74 2019-05-21 stsp if (err) {
7863 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7864 1e37a5c2 2019-05-12 jcs break;
7865 1e37a5c2 2019-05-12 jcs }
7866 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7867 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7868 1e37a5c2 2019-05-12 jcs break;
7869 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7870 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7871 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7872 07b0611c 2022-06-23 thomas view->count = 0;
7873 1e37a5c2 2019-05-12 jcs break;
7874 1e37a5c2 2019-05-12 jcs default:
7875 07b0611c 2022-06-23 thomas view->count = 0;
7876 1e37a5c2 2019-05-12 jcs break;
7877 ffd1d5e5 2018-06-23 stsp }
7878 e5a0f69f 2018-08-18 stsp
7879 ffd1d5e5 2018-06-23 stsp return err;
7880 9f7d7167 2018-04-29 stsp }
7881 9f7d7167 2018-04-29 stsp
7882 ffd1d5e5 2018-06-23 stsp __dead static void
7883 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7884 ffd1d5e5 2018-06-23 stsp {
7885 ffd1d5e5 2018-06-23 stsp endwin();
7886 91198554 2022-06-23 thomas fprintf(stderr,
7887 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7888 ffd1d5e5 2018-06-23 stsp getprogname());
7889 ffd1d5e5 2018-06-23 stsp exit(1);
7890 ffd1d5e5 2018-06-23 stsp }
7891 ffd1d5e5 2018-06-23 stsp
7892 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7893 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7894 ffd1d5e5 2018-06-23 stsp {
7895 b85a3496 2023-04-14 thomas const struct got_error *io_err, *error;
7896 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7897 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7898 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7899 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7900 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7901 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7902 4e97c21c 2020-12-06 stsp char *label = NULL;
7903 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7904 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7905 ffd1d5e5 2018-06-23 stsp int ch;
7906 5221c383 2018-08-01 stsp struct tog_view *view;
7907 b85a3496 2023-04-14 thomas struct tog_io *tog_io = NULL;
7908 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7909 70ac5f84 2019-03-28 stsp
7910 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7911 ffd1d5e5 2018-06-23 stsp switch (ch) {
7912 ffd1d5e5 2018-06-23 stsp case 'c':
7913 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7914 74283ab8 2020-02-07 stsp break;
7915 74283ab8 2020-02-07 stsp case 'r':
7916 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7917 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7918 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7919 74283ab8 2020-02-07 stsp optarg);
7920 ffd1d5e5 2018-06-23 stsp break;
7921 ffd1d5e5 2018-06-23 stsp default:
7922 e99e2d15 2020-11-24 naddy usage_tree();
7923 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7924 ffd1d5e5 2018-06-23 stsp }
7925 ffd1d5e5 2018-06-23 stsp }
7926 ffd1d5e5 2018-06-23 stsp
7927 ffd1d5e5 2018-06-23 stsp argc -= optind;
7928 ffd1d5e5 2018-06-23 stsp argv += optind;
7929 ffd1d5e5 2018-06-23 stsp
7930 55cccc34 2020-02-20 stsp if (argc > 1)
7931 e99e2d15 2020-11-24 naddy usage_tree();
7932 7cd52833 2022-06-23 thomas
7933 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7934 7cd52833 2022-06-23 thomas if (error != NULL)
7935 7cd52833 2022-06-23 thomas goto done;
7936 74283ab8 2020-02-07 stsp
7937 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7938 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7939 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7940 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7941 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7942 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7943 c156c7a4 2020-12-18 stsp goto done;
7944 55cccc34 2020-02-20 stsp if (worktree)
7945 52185f70 2019-02-05 stsp repo_path =
7946 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7947 55cccc34 2020-02-20 stsp else
7948 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7949 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7950 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7951 c156c7a4 2020-12-18 stsp goto done;
7952 c156c7a4 2020-12-18 stsp }
7953 55cccc34 2020-02-20 stsp }
7954 a915003a 2019-02-05 stsp
7955 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7956 c02c541e 2019-03-29 stsp if (error != NULL)
7957 52185f70 2019-02-05 stsp goto done;
7958 d188b9a6 2019-01-04 stsp
7959 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7960 55cccc34 2020-02-20 stsp repo, worktree);
7961 55cccc34 2020-02-20 stsp if (error)
7962 55cccc34 2020-02-20 stsp goto done;
7963 55cccc34 2020-02-20 stsp
7964 b85a3496 2023-04-14 thomas error = init_curses(&tog_io);
7965 b85a3496 2023-04-14 thomas if (error)
7966 b85a3496 2023-04-14 thomas goto done;
7967 55cccc34 2020-02-20 stsp
7968 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7969 c02c541e 2019-03-29 stsp if (error)
7970 52185f70 2019-02-05 stsp goto done;
7971 ffd1d5e5 2018-06-23 stsp
7972 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7973 51a10b52 2020-12-26 stsp if (error)
7974 51a10b52 2020-12-26 stsp goto done;
7975 51a10b52 2020-12-26 stsp
7976 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7977 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7978 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7979 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7980 4e97c21c 2020-12-06 stsp if (error)
7981 4e97c21c 2020-12-06 stsp goto done;
7982 4e97c21c 2020-12-06 stsp head_ref_name = label;
7983 4e97c21c 2020-12-06 stsp } else {
7984 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7985 4e97c21c 2020-12-06 stsp if (error == NULL)
7986 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7987 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7988 4e97c21c 2020-12-06 stsp goto done;
7989 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7990 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7991 4e97c21c 2020-12-06 stsp if (error)
7992 4e97c21c 2020-12-06 stsp goto done;
7993 4e97c21c 2020-12-06 stsp }
7994 ffd1d5e5 2018-06-23 stsp
7995 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7996 945f9229 2022-04-16 thomas if (error)
7997 945f9229 2022-04-16 thomas goto done;
7998 945f9229 2022-04-16 thomas
7999 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
8000 5221c383 2018-08-01 stsp if (view == NULL) {
8001 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
8002 5221c383 2018-08-01 stsp goto done;
8003 5221c383 2018-08-01 stsp }
8004 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
8005 ad80ab7b 2018-08-04 stsp if (error)
8006 ad80ab7b 2018-08-04 stsp goto done;
8007 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
8008 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
8009 d91faf3b 2020-12-01 naddy in_repo_path);
8010 55cccc34 2020-02-20 stsp if (error)
8011 55cccc34 2020-02-20 stsp goto done;
8012 55cccc34 2020-02-20 stsp }
8013 55cccc34 2020-02-20 stsp
8014 55cccc34 2020-02-20 stsp if (worktree) {
8015 55cccc34 2020-02-20 stsp /* Release work tree lock. */
8016 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
8017 55cccc34 2020-02-20 stsp worktree = NULL;
8018 55cccc34 2020-02-20 stsp }
8019 b85a3496 2023-04-14 thomas error = view_loop(view, tog_io);
8020 ffd1d5e5 2018-06-23 stsp done:
8021 52185f70 2019-02-05 stsp free(repo_path);
8022 e4a0e26d 2020-02-20 stsp free(cwd);
8023 ffd1d5e5 2018-06-23 stsp free(commit_id);
8024 4e97c21c 2020-12-06 stsp free(label);
8025 486cd271 2020-12-06 stsp if (ref)
8026 486cd271 2020-12-06 stsp got_ref_close(ref);
8027 1d0f4054 2021-06-17 stsp if (repo) {
8028 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8029 1d0f4054 2021-06-17 stsp if (error == NULL)
8030 1d0f4054 2021-06-17 stsp error = close_err;
8031 1d0f4054 2021-06-17 stsp }
8032 7cd52833 2022-06-23 thomas if (pack_fds) {
8033 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8034 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8035 7cd52833 2022-06-23 thomas if (error == NULL)
8036 7cd52833 2022-06-23 thomas error = pack_err;
8037 b85a3496 2023-04-14 thomas }
8038 b85a3496 2023-04-14 thomas if (tog_io != NULL) {
8039 b85a3496 2023-04-14 thomas io_err = tog_io_close(tog_io);
8040 b85a3496 2023-04-14 thomas if (error == NULL)
8041 b85a3496 2023-04-14 thomas error = io_err;
8042 7cd52833 2022-06-23 thomas }
8043 51a10b52 2020-12-26 stsp tog_free_refs();
8044 ffd1d5e5 2018-06-23 stsp return error;
8045 6458efa5 2020-11-24 stsp }
8046 6458efa5 2020-11-24 stsp
8047 6458efa5 2020-11-24 stsp static const struct got_error *
8048 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
8049 6458efa5 2020-11-24 stsp {
8050 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
8051 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8052 6458efa5 2020-11-24 stsp
8053 6458efa5 2020-11-24 stsp s->nrefs = 0;
8054 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
8055 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
8056 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
8057 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
8058 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
8059 6458efa5 2020-11-24 stsp continue;
8060 6458efa5 2020-11-24 stsp
8061 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
8062 6458efa5 2020-11-24 stsp if (re == NULL)
8063 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
8064 6458efa5 2020-11-24 stsp
8065 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
8066 8924d611 2020-12-26 stsp if (re->ref == NULL)
8067 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
8068 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
8069 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
8070 6458efa5 2020-11-24 stsp }
8071 6458efa5 2020-11-24 stsp
8072 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8073 6458efa5 2020-11-24 stsp return NULL;
8074 6458efa5 2020-11-24 stsp }
8075 6458efa5 2020-11-24 stsp
8076 ef20f542 2022-06-26 thomas static void
8077 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
8078 6458efa5 2020-11-24 stsp {
8079 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8080 6458efa5 2020-11-24 stsp
8081 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
8082 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8083 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
8084 8924d611 2020-12-26 stsp got_ref_close(re->ref);
8085 6458efa5 2020-11-24 stsp free(re);
8086 6458efa5 2020-11-24 stsp }
8087 6458efa5 2020-11-24 stsp }
8088 6458efa5 2020-11-24 stsp
8089 6458efa5 2020-11-24 stsp static const struct got_error *
8090 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
8091 6458efa5 2020-11-24 stsp {
8092 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8093 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8094 6458efa5 2020-11-24 stsp
8095 6458efa5 2020-11-24 stsp s->selected_entry = 0;
8096 6458efa5 2020-11-24 stsp s->repo = repo;
8097 6458efa5 2020-11-24 stsp
8098 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
8099 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
8100 6458efa5 2020-11-24 stsp
8101 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8102 6458efa5 2020-11-24 stsp if (err)
8103 6458efa5 2020-11-24 stsp return err;
8104 34ba6917 2020-11-30 stsp
8105 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
8106 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
8107 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
8108 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
8109 6458efa5 2020-11-24 stsp if (err)
8110 6458efa5 2020-11-24 stsp goto done;
8111 6458efa5 2020-11-24 stsp
8112 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
8113 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
8114 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
8115 6458efa5 2020-11-24 stsp if (err)
8116 6458efa5 2020-11-24 stsp goto done;
8117 6458efa5 2020-11-24 stsp
8118 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
8119 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
8120 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
8121 2183bbf6 2022-01-23 thomas if (err)
8122 2183bbf6 2022-01-23 thomas goto done;
8123 2183bbf6 2022-01-23 thomas
8124 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
8125 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
8126 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
8127 6458efa5 2020-11-24 stsp if (err)
8128 6458efa5 2020-11-24 stsp goto done;
8129 6458efa5 2020-11-24 stsp }
8130 6458efa5 2020-11-24 stsp
8131 6458efa5 2020-11-24 stsp view->show = show_ref_view;
8132 6458efa5 2020-11-24 stsp view->input = input_ref_view;
8133 6458efa5 2020-11-24 stsp view->close = close_ref_view;
8134 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
8135 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
8136 6458efa5 2020-11-24 stsp done:
8137 6458efa5 2020-11-24 stsp if (err)
8138 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8139 6458efa5 2020-11-24 stsp return err;
8140 6458efa5 2020-11-24 stsp }
8141 6458efa5 2020-11-24 stsp
8142 6458efa5 2020-11-24 stsp static const struct got_error *
8143 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
8144 6458efa5 2020-11-24 stsp {
8145 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8146 6458efa5 2020-11-24 stsp
8147 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8148 6458efa5 2020-11-24 stsp free_colors(&s->colors);
8149 6458efa5 2020-11-24 stsp
8150 6458efa5 2020-11-24 stsp return NULL;
8151 9f7d7167 2018-04-29 stsp }
8152 ce5b7c56 2019-07-09 stsp
8153 6458efa5 2020-11-24 stsp static const struct got_error *
8154 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
8155 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8156 6458efa5 2020-11-24 stsp {
8157 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8158 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
8159 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
8160 6458efa5 2020-11-24 stsp int obj_type;
8161 6458efa5 2020-11-24 stsp
8162 c42c9805 2020-11-24 stsp *commit_id = NULL;
8163 6458efa5 2020-11-24 stsp
8164 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
8165 6458efa5 2020-11-24 stsp if (err)
8166 6458efa5 2020-11-24 stsp return err;
8167 6458efa5 2020-11-24 stsp
8168 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
8169 6458efa5 2020-11-24 stsp if (err)
8170 6458efa5 2020-11-24 stsp goto done;
8171 6458efa5 2020-11-24 stsp
8172 6458efa5 2020-11-24 stsp switch (obj_type) {
8173 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
8174 c42c9805 2020-11-24 stsp *commit_id = obj_id;
8175 6458efa5 2020-11-24 stsp break;
8176 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
8177 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
8178 6458efa5 2020-11-24 stsp if (err)
8179 6458efa5 2020-11-24 stsp goto done;
8180 c42c9805 2020-11-24 stsp free(obj_id);
8181 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
8182 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8183 c42c9805 2020-11-24 stsp if (err)
8184 6458efa5 2020-11-24 stsp goto done;
8185 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
8186 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8187 c42c9805 2020-11-24 stsp goto done;
8188 c42c9805 2020-11-24 stsp }
8189 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
8190 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
8191 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
8192 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
8193 c42c9805 2020-11-24 stsp goto done;
8194 c42c9805 2020-11-24 stsp }
8195 6458efa5 2020-11-24 stsp break;
8196 6458efa5 2020-11-24 stsp default:
8197 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
8198 c42c9805 2020-11-24 stsp break;
8199 6458efa5 2020-11-24 stsp }
8200 6458efa5 2020-11-24 stsp
8201 c42c9805 2020-11-24 stsp done:
8202 c42c9805 2020-11-24 stsp if (tag)
8203 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
8204 c42c9805 2020-11-24 stsp if (err) {
8205 c42c9805 2020-11-24 stsp free(*commit_id);
8206 c42c9805 2020-11-24 stsp *commit_id = NULL;
8207 c42c9805 2020-11-24 stsp }
8208 c42c9805 2020-11-24 stsp return err;
8209 c42c9805 2020-11-24 stsp }
8210 c42c9805 2020-11-24 stsp
8211 c42c9805 2020-11-24 stsp static const struct got_error *
8212 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
8213 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8214 c42c9805 2020-11-24 stsp {
8215 c42c9805 2020-11-24 stsp struct tog_view *log_view;
8216 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8217 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
8218 c42c9805 2020-11-24 stsp
8219 c42c9805 2020-11-24 stsp *new_view = NULL;
8220 c42c9805 2020-11-24 stsp
8221 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8222 c42c9805 2020-11-24 stsp if (err) {
8223 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8224 c42c9805 2020-11-24 stsp return err;
8225 c42c9805 2020-11-24 stsp else
8226 c42c9805 2020-11-24 stsp return NULL;
8227 c42c9805 2020-11-24 stsp }
8228 c42c9805 2020-11-24 stsp
8229 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
8230 6458efa5 2020-11-24 stsp if (log_view == NULL) {
8231 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
8232 6458efa5 2020-11-24 stsp goto done;
8233 6458efa5 2020-11-24 stsp }
8234 6458efa5 2020-11-24 stsp
8235 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
8236 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
8237 6458efa5 2020-11-24 stsp done:
8238 6458efa5 2020-11-24 stsp if (err)
8239 6458efa5 2020-11-24 stsp view_close(log_view);
8240 6458efa5 2020-11-24 stsp else
8241 6458efa5 2020-11-24 stsp *new_view = log_view;
8242 c42c9805 2020-11-24 stsp free(commit_id);
8243 6458efa5 2020-11-24 stsp return err;
8244 6458efa5 2020-11-24 stsp }
8245 6458efa5 2020-11-24 stsp
8246 ce5b7c56 2019-07-09 stsp static void
8247 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
8248 6458efa5 2020-11-24 stsp {
8249 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
8250 34ba6917 2020-11-30 stsp int i = 0;
8251 6458efa5 2020-11-24 stsp
8252 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8253 6458efa5 2020-11-24 stsp return;
8254 6458efa5 2020-11-24 stsp
8255 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
8256 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
8257 34ba6917 2020-11-30 stsp if (re == NULL)
8258 34ba6917 2020-11-30 stsp break;
8259 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
8260 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8261 6458efa5 2020-11-24 stsp }
8262 6458efa5 2020-11-24 stsp }
8263 6458efa5 2020-11-24 stsp
8264 a5d43cac 2022-07-01 thomas static const struct got_error *
8265 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
8266 6458efa5 2020-11-24 stsp {
8267 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8268 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
8269 6458efa5 2020-11-24 stsp int n = 0;
8270 6458efa5 2020-11-24 stsp
8271 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
8272 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
8273 6458efa5 2020-11-24 stsp else
8274 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
8275 6458efa5 2020-11-24 stsp
8276 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
8277 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
8278 07dd3ed3 2022-08-06 thomas if (last) {
8279 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
8280 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
8281 07dd3ed3 2022-08-06 thomas }
8282 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
8283 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
8284 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
8285 6458efa5 2020-11-24 stsp }
8286 6458efa5 2020-11-24 stsp }
8287 a5d43cac 2022-07-01 thomas
8288 a5d43cac 2022-07-01 thomas return NULL;
8289 6458efa5 2020-11-24 stsp }
8290 6458efa5 2020-11-24 stsp
8291 6458efa5 2020-11-24 stsp static const struct got_error *
8292 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8293 6458efa5 2020-11-24 stsp {
8294 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8295 6458efa5 2020-11-24 stsp
8296 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8297 6458efa5 2020-11-24 stsp return NULL;
8298 6458efa5 2020-11-24 stsp }
8299 6458efa5 2020-11-24 stsp
8300 6458efa5 2020-11-24 stsp static int
8301 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8302 6458efa5 2020-11-24 stsp {
8303 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8304 6458efa5 2020-11-24 stsp
8305 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
8306 6458efa5 2020-11-24 stsp 0) == 0;
8307 6458efa5 2020-11-24 stsp }
8308 6458efa5 2020-11-24 stsp
8309 6458efa5 2020-11-24 stsp static const struct got_error *
8310 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8311 6458efa5 2020-11-24 stsp {
8312 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8313 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8314 6458efa5 2020-11-24 stsp
8315 6458efa5 2020-11-24 stsp if (!view->searching) {
8316 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8317 6458efa5 2020-11-24 stsp return NULL;
8318 6458efa5 2020-11-24 stsp }
8319 6458efa5 2020-11-24 stsp
8320 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8321 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8322 6458efa5 2020-11-24 stsp if (s->selected_entry)
8323 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8324 6458efa5 2020-11-24 stsp else
8325 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8326 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8327 6458efa5 2020-11-24 stsp } else {
8328 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8329 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8330 6458efa5 2020-11-24 stsp else
8331 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8332 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8333 6458efa5 2020-11-24 stsp }
8334 6458efa5 2020-11-24 stsp } else {
8335 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8336 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8337 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8338 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8339 6458efa5 2020-11-24 stsp else
8340 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8341 6458efa5 2020-11-24 stsp }
8342 6458efa5 2020-11-24 stsp
8343 6458efa5 2020-11-24 stsp while (1) {
8344 6458efa5 2020-11-24 stsp if (re == NULL) {
8345 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8346 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8347 6458efa5 2020-11-24 stsp return NULL;
8348 6458efa5 2020-11-24 stsp }
8349 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8350 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8351 6458efa5 2020-11-24 stsp else
8352 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8353 6458efa5 2020-11-24 stsp }
8354 6458efa5 2020-11-24 stsp
8355 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8356 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8357 6458efa5 2020-11-24 stsp s->matched_entry = re;
8358 6458efa5 2020-11-24 stsp break;
8359 6458efa5 2020-11-24 stsp }
8360 6458efa5 2020-11-24 stsp
8361 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8362 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8363 6458efa5 2020-11-24 stsp else
8364 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8365 6458efa5 2020-11-24 stsp }
8366 6458efa5 2020-11-24 stsp
8367 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8368 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8369 6458efa5 2020-11-24 stsp s->selected = 0;
8370 6458efa5 2020-11-24 stsp }
8371 6458efa5 2020-11-24 stsp
8372 6458efa5 2020-11-24 stsp return NULL;
8373 6458efa5 2020-11-24 stsp }
8374 6458efa5 2020-11-24 stsp
8375 6458efa5 2020-11-24 stsp static const struct got_error *
8376 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8377 6458efa5 2020-11-24 stsp {
8378 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8379 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8380 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8381 6458efa5 2020-11-24 stsp char *line = NULL;
8382 6458efa5 2020-11-24 stsp wchar_t *wline;
8383 6458efa5 2020-11-24 stsp struct tog_color *tc;
8384 66a70b97 2023-02-03 thomas int width, n, scrollx;
8385 6458efa5 2020-11-24 stsp int limit = view->nlines;
8386 6458efa5 2020-11-24 stsp
8387 6458efa5 2020-11-24 stsp werase(view->window);
8388 6458efa5 2020-11-24 stsp
8389 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8390 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8391 a5d43cac 2022-07-01 thomas --limit; /* border */
8392 6458efa5 2020-11-24 stsp
8393 6458efa5 2020-11-24 stsp if (limit == 0)
8394 6458efa5 2020-11-24 stsp return NULL;
8395 6458efa5 2020-11-24 stsp
8396 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8397 6458efa5 2020-11-24 stsp
8398 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8399 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8400 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8401 6458efa5 2020-11-24 stsp
8402 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8403 6458efa5 2020-11-24 stsp if (err) {
8404 6458efa5 2020-11-24 stsp free(line);
8405 6458efa5 2020-11-24 stsp return err;
8406 6458efa5 2020-11-24 stsp }
8407 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8408 6458efa5 2020-11-24 stsp wstandout(view->window);
8409 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8410 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8411 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8412 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8413 6458efa5 2020-11-24 stsp wstandend(view->window);
8414 6458efa5 2020-11-24 stsp free(wline);
8415 6458efa5 2020-11-24 stsp wline = NULL;
8416 6458efa5 2020-11-24 stsp free(line);
8417 6458efa5 2020-11-24 stsp line = NULL;
8418 6458efa5 2020-11-24 stsp if (--limit <= 0)
8419 6458efa5 2020-11-24 stsp return NULL;
8420 6458efa5 2020-11-24 stsp
8421 6458efa5 2020-11-24 stsp n = 0;
8422 66a70b97 2023-02-03 thomas view->maxx = 0;
8423 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8424 6458efa5 2020-11-24 stsp char *line = NULL;
8425 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8426 6458efa5 2020-11-24 stsp
8427 84227eb1 2022-06-23 thomas if (s->show_date) {
8428 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8429 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8430 84227eb1 2022-06-23 thomas struct got_object_id *id;
8431 84227eb1 2022-06-23 thomas struct tm tm;
8432 84227eb1 2022-06-23 thomas time_t t;
8433 84227eb1 2022-06-23 thomas
8434 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8435 84227eb1 2022-06-23 thomas if (err)
8436 84227eb1 2022-06-23 thomas return err;
8437 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8438 84227eb1 2022-06-23 thomas if (err) {
8439 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8440 84227eb1 2022-06-23 thomas free(id);
8441 84227eb1 2022-06-23 thomas return err;
8442 84227eb1 2022-06-23 thomas }
8443 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8444 84227eb1 2022-06-23 thomas id);
8445 84227eb1 2022-06-23 thomas if (err) {
8446 84227eb1 2022-06-23 thomas free(id);
8447 84227eb1 2022-06-23 thomas return err;
8448 84227eb1 2022-06-23 thomas }
8449 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8450 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8451 84227eb1 2022-06-23 thomas } else {
8452 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8453 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8454 84227eb1 2022-06-23 thomas }
8455 84227eb1 2022-06-23 thomas free(id);
8456 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8457 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8458 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8459 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8460 84227eb1 2022-06-23 thomas }
8461 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8462 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8463 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8464 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8465 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8466 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8467 6458efa5 2020-11-24 stsp struct got_object_id *id;
8468 6458efa5 2020-11-24 stsp char *id_str;
8469 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8470 6458efa5 2020-11-24 stsp if (err)
8471 6458efa5 2020-11-24 stsp return err;
8472 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8473 6458efa5 2020-11-24 stsp if (err) {
8474 6458efa5 2020-11-24 stsp free(id);
8475 6458efa5 2020-11-24 stsp return err;
8476 6458efa5 2020-11-24 stsp }
8477 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8478 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8479 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8480 6458efa5 2020-11-24 stsp free(id);
8481 6458efa5 2020-11-24 stsp free(id_str);
8482 6458efa5 2020-11-24 stsp return err;
8483 6458efa5 2020-11-24 stsp }
8484 6458efa5 2020-11-24 stsp free(id);
8485 6458efa5 2020-11-24 stsp free(id_str);
8486 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8487 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8488 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8489 6458efa5 2020-11-24 stsp
8490 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8491 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8492 6458efa5 2020-11-24 stsp if (err) {
8493 6458efa5 2020-11-24 stsp free(line);
8494 6458efa5 2020-11-24 stsp return err;
8495 6458efa5 2020-11-24 stsp }
8496 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8497 66a70b97 2023-02-03 thomas free(wline);
8498 66a70b97 2023-02-03 thomas wline = NULL;
8499 66a70b97 2023-02-03 thomas
8500 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8501 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8502 66a70b97 2023-02-03 thomas if (err) {
8503 66a70b97 2023-02-03 thomas free(line);
8504 66a70b97 2023-02-03 thomas return err;
8505 66a70b97 2023-02-03 thomas }
8506 6458efa5 2020-11-24 stsp if (n == s->selected) {
8507 6458efa5 2020-11-24 stsp if (view->focussed)
8508 6458efa5 2020-11-24 stsp wstandout(view->window);
8509 6458efa5 2020-11-24 stsp s->selected_entry = re;
8510 6458efa5 2020-11-24 stsp }
8511 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8512 6458efa5 2020-11-24 stsp if (tc)
8513 6458efa5 2020-11-24 stsp wattr_on(view->window,
8514 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8515 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8516 6458efa5 2020-11-24 stsp if (tc)
8517 6458efa5 2020-11-24 stsp wattr_off(view->window,
8518 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8519 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8520 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8521 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8522 6458efa5 2020-11-24 stsp wstandend(view->window);
8523 6458efa5 2020-11-24 stsp free(line);
8524 6458efa5 2020-11-24 stsp free(wline);
8525 6458efa5 2020-11-24 stsp wline = NULL;
8526 6458efa5 2020-11-24 stsp n++;
8527 6458efa5 2020-11-24 stsp s->ndisplayed++;
8528 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8529 6458efa5 2020-11-24 stsp
8530 6458efa5 2020-11-24 stsp limit--;
8531 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8532 6458efa5 2020-11-24 stsp }
8533 6458efa5 2020-11-24 stsp
8534 a5d43cac 2022-07-01 thomas view_border(view);
8535 6458efa5 2020-11-24 stsp return err;
8536 6458efa5 2020-11-24 stsp }
8537 6458efa5 2020-11-24 stsp
8538 6458efa5 2020-11-24 stsp static const struct got_error *
8539 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8540 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8541 c42c9805 2020-11-24 stsp {
8542 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8543 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8544 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8545 c42c9805 2020-11-24 stsp
8546 c42c9805 2020-11-24 stsp *new_view = NULL;
8547 c42c9805 2020-11-24 stsp
8548 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8549 c42c9805 2020-11-24 stsp if (err) {
8550 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8551 c42c9805 2020-11-24 stsp return err;
8552 c42c9805 2020-11-24 stsp else
8553 c42c9805 2020-11-24 stsp return NULL;
8554 c42c9805 2020-11-24 stsp }
8555 c42c9805 2020-11-24 stsp
8556 c42c9805 2020-11-24 stsp
8557 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8558 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8559 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8560 c42c9805 2020-11-24 stsp goto done;
8561 c42c9805 2020-11-24 stsp }
8562 c42c9805 2020-11-24 stsp
8563 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8564 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8565 c42c9805 2020-11-24 stsp if (err)
8566 c42c9805 2020-11-24 stsp goto done;
8567 c42c9805 2020-11-24 stsp
8568 c42c9805 2020-11-24 stsp *new_view = tree_view;
8569 c42c9805 2020-11-24 stsp done:
8570 c42c9805 2020-11-24 stsp free(commit_id);
8571 c42c9805 2020-11-24 stsp return err;
8572 07dd3ed3 2022-08-06 thomas }
8573 07dd3ed3 2022-08-06 thomas
8574 07dd3ed3 2022-08-06 thomas static const struct got_error *
8575 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8576 07dd3ed3 2022-08-06 thomas {
8577 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8578 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8579 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8580 07dd3ed3 2022-08-06 thomas
8581 07dd3ed3 2022-08-06 thomas g = view->gline;
8582 07dd3ed3 2022-08-06 thomas view->gline = 0;
8583 07dd3ed3 2022-08-06 thomas
8584 07dd3ed3 2022-08-06 thomas if (g == 0)
8585 07dd3ed3 2022-08-06 thomas g = 1;
8586 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8587 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8588 07dd3ed3 2022-08-06 thomas
8589 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8590 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8591 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8592 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8593 07dd3ed3 2022-08-06 thomas return NULL;
8594 07dd3ed3 2022-08-06 thomas }
8595 07dd3ed3 2022-08-06 thomas
8596 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8597 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8598 07dd3ed3 2022-08-06 thomas if (err)
8599 07dd3ed3 2022-08-06 thomas return err;
8600 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8601 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8602 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8603 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8604 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8605 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8606 07dd3ed3 2022-08-06 thomas
8607 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8608 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8609 07dd3ed3 2022-08-06 thomas
8610 07dd3ed3 2022-08-06 thomas return NULL;
8611 07dd3ed3 2022-08-06 thomas
8612 c42c9805 2020-11-24 stsp }
8613 07dd3ed3 2022-08-06 thomas
8614 c42c9805 2020-11-24 stsp static const struct got_error *
8615 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8616 6458efa5 2020-11-24 stsp {
8617 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8618 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8619 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8620 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8621 6458efa5 2020-11-24 stsp
8622 07dd3ed3 2022-08-06 thomas if (view->gline)
8623 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8624 07dd3ed3 2022-08-06 thomas
8625 6458efa5 2020-11-24 stsp switch (ch) {
8626 66a70b97 2023-02-03 thomas case '0':
8627 66a70b97 2023-02-03 thomas case '$':
8628 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8629 66a70b97 2023-02-03 thomas case 'l':
8630 66a70b97 2023-02-03 thomas case KEY_LEFT:
8631 66a70b97 2023-02-03 thomas case 'h':
8632 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8633 66a70b97 2023-02-03 thomas break;
8634 6458efa5 2020-11-24 stsp case 'i':
8635 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8636 07b0611c 2022-06-23 thomas view->count = 0;
8637 3bfadbd4 2021-11-20 thomas break;
8638 84227eb1 2022-06-23 thomas case 'm':
8639 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8640 07b0611c 2022-06-23 thomas view->count = 0;
8641 84227eb1 2022-06-23 thomas break;
8642 98182bd0 2021-11-20 thomas case 'o':
8643 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8644 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8645 07b0611c 2022-06-23 thomas view->count = 0;
8646 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8647 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8648 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8649 c591440f 2021-11-20 thomas if (err)
8650 c591440f 2021-11-20 thomas break;
8651 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8652 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8653 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8654 3bfadbd4 2021-11-20 thomas if (err)
8655 3bfadbd4 2021-11-20 thomas break;
8656 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8657 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8658 6458efa5 2020-11-24 stsp break;
8659 6458efa5 2020-11-24 stsp case KEY_ENTER:
8660 6458efa5 2020-11-24 stsp case '\r':
8661 07b0611c 2022-06-23 thomas view->count = 0;
8662 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8663 a5d43cac 2022-07-01 thomas break;
8664 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8665 6458efa5 2020-11-24 stsp break;
8666 1be4947a 2022-07-22 thomas case 'T':
8667 07b0611c 2022-06-23 thomas view->count = 0;
8668 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8669 c42c9805 2020-11-24 stsp break;
8670 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8671 c42c9805 2020-11-24 stsp break;
8672 e4526bf5 2021-09-03 naddy case 'g':
8673 aa7a1117 2023-01-09 thomas case '=':
8674 e4526bf5 2021-09-03 naddy case KEY_HOME:
8675 e4526bf5 2021-09-03 naddy s->selected = 0;
8676 07b0611c 2022-06-23 thomas view->count = 0;
8677 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8678 e4526bf5 2021-09-03 naddy break;
8679 e4526bf5 2021-09-03 naddy case 'G':
8680 aa7a1117 2023-01-09 thomas case '*':
8681 a5d43cac 2022-07-01 thomas case KEY_END: {
8682 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8683 a5d43cac 2022-07-01 thomas
8684 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8685 a5d43cac 2022-07-01 thomas --eos; /* border */
8686 e4526bf5 2021-09-03 naddy s->selected = 0;
8687 07b0611c 2022-06-23 thomas view->count = 0;
8688 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8689 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8690 e4526bf5 2021-09-03 naddy if (re == NULL)
8691 e4526bf5 2021-09-03 naddy break;
8692 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8693 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8694 e4526bf5 2021-09-03 naddy }
8695 e4526bf5 2021-09-03 naddy if (n > 0)
8696 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8697 e4526bf5 2021-09-03 naddy break;
8698 a5d43cac 2022-07-01 thomas }
8699 6458efa5 2020-11-24 stsp case 'k':
8700 6458efa5 2020-11-24 stsp case KEY_UP:
8701 f7140bf5 2021-10-17 thomas case CTRL('p'):
8702 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8703 6458efa5 2020-11-24 stsp s->selected--;
8704 6458efa5 2020-11-24 stsp break;
8705 34ba6917 2020-11-30 stsp }
8706 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8707 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8708 07b0611c 2022-06-23 thomas view->count = 0;
8709 6458efa5 2020-11-24 stsp break;
8710 70f17a53 2022-06-13 thomas case CTRL('u'):
8711 23427b14 2022-06-23 thomas case 'u':
8712 70f17a53 2022-06-13 thomas nscroll /= 2;
8713 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8714 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8715 6458efa5 2020-11-24 stsp case CTRL('b'):
8716 1c5e5faa 2022-06-23 thomas case 'b':
8717 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8718 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8719 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8720 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8721 07b0611c 2022-06-23 thomas view->count = 0;
8722 6458efa5 2020-11-24 stsp break;
8723 6458efa5 2020-11-24 stsp case 'j':
8724 6458efa5 2020-11-24 stsp case KEY_DOWN:
8725 f7140bf5 2021-10-17 thomas case CTRL('n'):
8726 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8727 6458efa5 2020-11-24 stsp s->selected++;
8728 6458efa5 2020-11-24 stsp break;
8729 6458efa5 2020-11-24 stsp }
8730 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8731 6458efa5 2020-11-24 stsp /* can't scroll any further */
8732 07b0611c 2022-06-23 thomas view->count = 0;
8733 6458efa5 2020-11-24 stsp break;
8734 07b0611c 2022-06-23 thomas }
8735 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8736 6458efa5 2020-11-24 stsp break;
8737 70f17a53 2022-06-13 thomas case CTRL('d'):
8738 23427b14 2022-06-23 thomas case 'd':
8739 70f17a53 2022-06-13 thomas nscroll /= 2;
8740 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8741 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8742 6458efa5 2020-11-24 stsp case CTRL('f'):
8743 1c5e5faa 2022-06-23 thomas case 'f':
8744 4c2d69cb 2022-06-23 thomas case ' ':
8745 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8746 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8747 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8748 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8749 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8750 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8751 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8752 07b0611c 2022-06-23 thomas view->count = 0;
8753 6458efa5 2020-11-24 stsp break;
8754 6458efa5 2020-11-24 stsp }
8755 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8756 6458efa5 2020-11-24 stsp break;
8757 6458efa5 2020-11-24 stsp case CTRL('l'):
8758 07b0611c 2022-06-23 thomas view->count = 0;
8759 8924d611 2020-12-26 stsp tog_free_refs();
8760 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8761 8924d611 2020-12-26 stsp if (err)
8762 8924d611 2020-12-26 stsp break;
8763 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8764 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8765 6458efa5 2020-11-24 stsp break;
8766 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8767 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8768 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8769 6458efa5 2020-11-24 stsp break;
8770 6458efa5 2020-11-24 stsp default:
8771 07b0611c 2022-06-23 thomas view->count = 0;
8772 6458efa5 2020-11-24 stsp break;
8773 6458efa5 2020-11-24 stsp }
8774 6458efa5 2020-11-24 stsp
8775 6458efa5 2020-11-24 stsp return err;
8776 6458efa5 2020-11-24 stsp }
8777 6458efa5 2020-11-24 stsp
8778 6458efa5 2020-11-24 stsp __dead static void
8779 6458efa5 2020-11-24 stsp usage_ref(void)
8780 6458efa5 2020-11-24 stsp {
8781 6458efa5 2020-11-24 stsp endwin();
8782 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8783 6458efa5 2020-11-24 stsp getprogname());
8784 6458efa5 2020-11-24 stsp exit(1);
8785 6458efa5 2020-11-24 stsp }
8786 6458efa5 2020-11-24 stsp
8787 6458efa5 2020-11-24 stsp static const struct got_error *
8788 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8789 6458efa5 2020-11-24 stsp {
8790 b85a3496 2023-04-14 thomas const struct got_error *io_err, *error;
8791 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8792 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8793 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8794 6458efa5 2020-11-24 stsp int ch;
8795 6458efa5 2020-11-24 stsp struct tog_view *view;
8796 b85a3496 2023-04-14 thomas struct tog_io *tog_io = NULL;
8797 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8798 6458efa5 2020-11-24 stsp
8799 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8800 6458efa5 2020-11-24 stsp switch (ch) {
8801 6458efa5 2020-11-24 stsp case 'r':
8802 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8803 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8804 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8805 6458efa5 2020-11-24 stsp optarg);
8806 6458efa5 2020-11-24 stsp break;
8807 6458efa5 2020-11-24 stsp default:
8808 e99e2d15 2020-11-24 naddy usage_ref();
8809 6458efa5 2020-11-24 stsp /* NOTREACHED */
8810 6458efa5 2020-11-24 stsp }
8811 6458efa5 2020-11-24 stsp }
8812 6458efa5 2020-11-24 stsp
8813 6458efa5 2020-11-24 stsp argc -= optind;
8814 6458efa5 2020-11-24 stsp argv += optind;
8815 6458efa5 2020-11-24 stsp
8816 6458efa5 2020-11-24 stsp if (argc > 1)
8817 e99e2d15 2020-11-24 naddy usage_ref();
8818 7cd52833 2022-06-23 thomas
8819 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8820 7cd52833 2022-06-23 thomas if (error != NULL)
8821 7cd52833 2022-06-23 thomas goto done;
8822 6458efa5 2020-11-24 stsp
8823 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8824 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8825 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8826 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8827 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8828 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8829 c156c7a4 2020-12-18 stsp goto done;
8830 6458efa5 2020-11-24 stsp if (worktree)
8831 6458efa5 2020-11-24 stsp repo_path =
8832 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8833 6458efa5 2020-11-24 stsp else
8834 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8835 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8836 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8837 c156c7a4 2020-12-18 stsp goto done;
8838 c156c7a4 2020-12-18 stsp }
8839 6458efa5 2020-11-24 stsp }
8840 6458efa5 2020-11-24 stsp
8841 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8842 6458efa5 2020-11-24 stsp if (error != NULL)
8843 6458efa5 2020-11-24 stsp goto done;
8844 6458efa5 2020-11-24 stsp
8845 b85a3496 2023-04-14 thomas error = init_curses(&tog_io);
8846 b85a3496 2023-04-14 thomas if (error)
8847 b85a3496 2023-04-14 thomas goto done;
8848 6458efa5 2020-11-24 stsp
8849 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8850 51a10b52 2020-12-26 stsp if (error)
8851 51a10b52 2020-12-26 stsp goto done;
8852 51a10b52 2020-12-26 stsp
8853 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8854 6458efa5 2020-11-24 stsp if (error)
8855 6458efa5 2020-11-24 stsp goto done;
8856 6458efa5 2020-11-24 stsp
8857 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8858 6458efa5 2020-11-24 stsp if (view == NULL) {
8859 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8860 6458efa5 2020-11-24 stsp goto done;
8861 6458efa5 2020-11-24 stsp }
8862 6458efa5 2020-11-24 stsp
8863 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8864 6458efa5 2020-11-24 stsp if (error)
8865 6458efa5 2020-11-24 stsp goto done;
8866 6458efa5 2020-11-24 stsp
8867 6458efa5 2020-11-24 stsp if (worktree) {
8868 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8869 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8870 6458efa5 2020-11-24 stsp worktree = NULL;
8871 6458efa5 2020-11-24 stsp }
8872 b85a3496 2023-04-14 thomas error = view_loop(view, tog_io);
8873 6458efa5 2020-11-24 stsp done:
8874 6458efa5 2020-11-24 stsp free(repo_path);
8875 6458efa5 2020-11-24 stsp free(cwd);
8876 1d0f4054 2021-06-17 stsp if (repo) {
8877 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8878 1d0f4054 2021-06-17 stsp if (close_err)
8879 1d0f4054 2021-06-17 stsp error = close_err;
8880 1d0f4054 2021-06-17 stsp }
8881 7cd52833 2022-06-23 thomas if (pack_fds) {
8882 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8883 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8884 7cd52833 2022-06-23 thomas if (error == NULL)
8885 7cd52833 2022-06-23 thomas error = pack_err;
8886 b85a3496 2023-04-14 thomas }
8887 b85a3496 2023-04-14 thomas if (tog_io != NULL) {
8888 b85a3496 2023-04-14 thomas io_err = tog_io_close(tog_io);
8889 b85a3496 2023-04-14 thomas if (error == NULL)
8890 b85a3496 2023-04-14 thomas error = io_err;
8891 7cd52833 2022-06-23 thomas }
8892 51a10b52 2020-12-26 stsp tog_free_refs();
8893 6458efa5 2020-11-24 stsp return error;
8894 6458efa5 2020-11-24 stsp }
8895 6458efa5 2020-11-24 stsp
8896 fc2737d5 2022-09-15 thomas static const struct got_error*
8897 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8898 fc2737d5 2022-09-15 thomas const char *str)
8899 fc2737d5 2022-09-15 thomas {
8900 fc2737d5 2022-09-15 thomas size_t len;
8901 fc2737d5 2022-09-15 thomas
8902 fc2737d5 2022-09-15 thomas if (win == NULL)
8903 fc2737d5 2022-09-15 thomas win = stdscr;
8904 fc2737d5 2022-09-15 thomas
8905 fc2737d5 2022-09-15 thomas len = strlen(str);
8906 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8907 fc2737d5 2022-09-15 thomas
8908 fc2737d5 2022-09-15 thomas if (focus)
8909 fc2737d5 2022-09-15 thomas wstandout(win);
8910 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8911 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8912 fc2737d5 2022-09-15 thomas if (focus)
8913 fc2737d5 2022-09-15 thomas wstandend(win);
8914 fc2737d5 2022-09-15 thomas
8915 fc2737d5 2022-09-15 thomas return NULL;
8916 fc2737d5 2022-09-15 thomas }
8917 fc2737d5 2022-09-15 thomas
8918 2a31b33b 2022-07-23 thomas static const struct got_error *
8919 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8920 fc2737d5 2022-09-15 thomas {
8921 fc2737d5 2022-09-15 thomas off_t *p;
8922 fc2737d5 2022-09-15 thomas
8923 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8924 fc2737d5 2022-09-15 thomas if (p == NULL) {
8925 fc2737d5 2022-09-15 thomas free(*line_offsets);
8926 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8927 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8928 fc2737d5 2022-09-15 thomas }
8929 fc2737d5 2022-09-15 thomas
8930 fc2737d5 2022-09-15 thomas *line_offsets = p;
8931 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8932 fc2737d5 2022-09-15 thomas ++(*nlines);
8933 fc2737d5 2022-09-15 thomas return NULL;
8934 fc2737d5 2022-09-15 thomas }
8935 fc2737d5 2022-09-15 thomas
8936 fc2737d5 2022-09-15 thomas static const struct got_error *
8937 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8938 fc2737d5 2022-09-15 thomas {
8939 fc2737d5 2022-09-15 thomas *ret = 0;
8940 fc2737d5 2022-09-15 thomas
8941 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8942 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8943 fc2737d5 2022-09-15 thomas size_t len = 1;
8944 fc2737d5 2022-09-15 thomas
8945 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8946 fc2737d5 2022-09-15 thomas continue;
8947 fc2737d5 2022-09-15 thomas
8948 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8949 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8950 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8951 fc2737d5 2022-09-15 thomas
8952 fc2737d5 2022-09-15 thomas len += strlen(t);
8953 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8954 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8955 fc2737d5 2022-09-15 thomas free(t0);
8956 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8957 fc2737d5 2022-09-15 thomas }
8958 fc2737d5 2022-09-15 thomas
8959 fc2737d5 2022-09-15 thomas return NULL;
8960 fc2737d5 2022-09-15 thomas }
8961 fc2737d5 2022-09-15 thomas
8962 fc2737d5 2022-09-15 thomas /*
8963 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8964 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8965 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8966 fc2737d5 2022-09-15 thomas */
8967 fc2737d5 2022-09-15 thomas static const struct got_error *
8968 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8969 fc2737d5 2022-09-15 thomas {
8970 fc2737d5 2022-09-15 thomas int n, len = width;
8971 fc2737d5 2022-09-15 thomas
8972 fc2737d5 2022-09-15 thomas if (km->keys) {
8973 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8974 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8975 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8976 30e77f6a 2022-09-18 thomas };
8977 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8978 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8979 fc2737d5 2022-09-15 thomas
8980 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8981 fc2737d5 2022-09-15 thomas
8982 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8983 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8984 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8985 fc2737d5 2022-09-15 thomas
8986 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8987 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8988 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8989 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8990 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8991 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8992 fc2737d5 2022-09-15 thomas if (n < 0) {
8993 fc2737d5 2022-09-15 thomas free(t0);
8994 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8995 fc2737d5 2022-09-15 thomas }
8996 fc2737d5 2022-09-15 thomas first = 0;
8997 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8998 fc2737d5 2022-09-15 thomas *off += n;
8999 fc2737d5 2022-09-15 thomas }
9000 fc2737d5 2022-09-15 thomas free(t0);
9001 fc2737d5 2022-09-15 thomas }
9002 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
9003 fc2737d5 2022-09-15 thomas if (n < 0)
9004 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
9005 fc2737d5 2022-09-15 thomas *off += n;
9006 fc2737d5 2022-09-15 thomas
9007 fc2737d5 2022-09-15 thomas return NULL;
9008 fc2737d5 2022-09-15 thomas }
9009 fc2737d5 2022-09-15 thomas
9010 fc2737d5 2022-09-15 thomas static const struct got_error *
9011 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
9012 fc2737d5 2022-09-15 thomas {
9013 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9014 fc2737d5 2022-09-15 thomas off_t off = 0;
9015 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
9016 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
9017 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
9018 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
9019 fc2737d5 2022-09-15 thomas GENERATE_HELP
9020 fc2737d5 2022-09-15 thomas #undef KEYMAP_
9021 fc2737d5 2022-09-15 thomas #undef KEY_
9022 fc2737d5 2022-09-15 thomas };
9023 fc2737d5 2022-09-15 thomas
9024 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
9025 fc2737d5 2022-09-15 thomas if (err)
9026 fc2737d5 2022-09-15 thomas return err;
9027 fc2737d5 2022-09-15 thomas
9028 fc2737d5 2022-09-15 thomas n = nitems(km);
9029 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
9030 fc2737d5 2022-09-15 thomas if (err)
9031 fc2737d5 2022-09-15 thomas return err;
9032 fc2737d5 2022-09-15 thomas
9033 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
9034 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
9035 fc2737d5 2022-09-15 thomas show = s->all;
9036 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
9037 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
9038 fc2737d5 2022-09-15 thomas show = 1;
9039 fc2737d5 2022-09-15 thomas }
9040 fc2737d5 2022-09-15 thomas if (show) {
9041 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
9042 fc2737d5 2022-09-15 thomas if (err)
9043 fc2737d5 2022-09-15 thomas return err;
9044 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9045 fc2737d5 2022-09-15 thomas if (err)
9046 fc2737d5 2022-09-15 thomas return err;
9047 fc2737d5 2022-09-15 thomas }
9048 fc2737d5 2022-09-15 thomas }
9049 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
9050 fc2737d5 2022-09-15 thomas ++off;
9051 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
9052 fc2737d5 2022-09-15 thomas return err;
9053 fc2737d5 2022-09-15 thomas }
9054 fc2737d5 2022-09-15 thomas
9055 fc2737d5 2022-09-15 thomas static const struct got_error *
9056 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
9057 fc2737d5 2022-09-15 thomas {
9058 fc2737d5 2022-09-15 thomas FILE *f;
9059 fc2737d5 2022-09-15 thomas const struct got_error *err;
9060 fc2737d5 2022-09-15 thomas
9061 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9062 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9063 fc2737d5 2022-09-15 thomas s->nlines = 0;
9064 fc2737d5 2022-09-15 thomas
9065 fc2737d5 2022-09-15 thomas f = got_opentemp();
9066 fc2737d5 2022-09-15 thomas if (f == NULL)
9067 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
9068 fc2737d5 2022-09-15 thomas s->f = f;
9069 fc2737d5 2022-09-15 thomas
9070 fc2737d5 2022-09-15 thomas err = format_help(s);
9071 fc2737d5 2022-09-15 thomas if (err)
9072 fc2737d5 2022-09-15 thomas return err;
9073 fc2737d5 2022-09-15 thomas
9074 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
9075 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
9076 fc2737d5 2022-09-15 thomas
9077 fc2737d5 2022-09-15 thomas return NULL;
9078 fc2737d5 2022-09-15 thomas }
9079 fc2737d5 2022-09-15 thomas
9080 fc2737d5 2022-09-15 thomas static const struct got_error *
9081 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
9082 fc2737d5 2022-09-15 thomas {
9083 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
9084 fc2737d5 2022-09-15 thomas return NULL;
9085 fc2737d5 2022-09-15 thomas }
9086 fc2737d5 2022-09-15 thomas
9087 2a7d3cd7 2022-09-17 thomas static void
9088 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
9089 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
9090 2a7d3cd7 2022-09-17 thomas {
9091 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
9092 2a7d3cd7 2022-09-17 thomas
9093 2a7d3cd7 2022-09-17 thomas *f = s->f;
9094 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
9095 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
9096 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
9097 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
9098 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
9099 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
9100 2a7d3cd7 2022-09-17 thomas }
9101 2a7d3cd7 2022-09-17 thomas
9102 fc2737d5 2022-09-15 thomas static const struct got_error *
9103 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
9104 fc2737d5 2022-09-15 thomas {
9105 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9106 fc2737d5 2022-09-15 thomas const struct got_error *err;
9107 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
9108 fc2737d5 2022-09-15 thomas wchar_t *wline;
9109 fc2737d5 2022-09-15 thomas char *line;
9110 fc2737d5 2022-09-15 thomas ssize_t linelen;
9111 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9112 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
9113 fc2737d5 2022-09-15 thomas int eos = view->nlines;
9114 fc2737d5 2022-09-15 thomas
9115 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9116 fc2737d5 2022-09-15 thomas --eos; /* account for border */
9117 fc2737d5 2022-09-15 thomas
9118 fc2737d5 2022-09-15 thomas s->lineno = 0;
9119 fc2737d5 2022-09-15 thomas rewind(s->f);
9120 fc2737d5 2022-09-15 thomas werase(view->window);
9121 fc2737d5 2022-09-15 thomas
9122 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
9123 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
9124 fc2737d5 2022-09-15 thomas
9125 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
9126 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
9127 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
9128 fc2737d5 2022-09-15 thomas if (err)
9129 fc2737d5 2022-09-15 thomas return err;
9130 fc2737d5 2022-09-15 thomas if (eos <= 1)
9131 fc2737d5 2022-09-15 thomas return NULL;
9132 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
9133 fc2737d5 2022-09-15 thomas eos -= 2;
9134 fc2737d5 2022-09-15 thomas
9135 fc2737d5 2022-09-15 thomas s->eof = 0;
9136 fc2737d5 2022-09-15 thomas view->maxx = 0;
9137 fc2737d5 2022-09-15 thomas line = NULL;
9138 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
9139 fc2737d5 2022-09-15 thomas attr_t attr = 0;
9140 fc2737d5 2022-09-15 thomas
9141 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9142 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9143 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
9144 fc2737d5 2022-09-15 thomas free(line);
9145 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
9146 fc2737d5 2022-09-15 thomas }
9147 fc2737d5 2022-09-15 thomas s->eof = 1;
9148 fc2737d5 2022-09-15 thomas break;
9149 fc2737d5 2022-09-15 thomas }
9150 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
9151 fc2737d5 2022-09-15 thomas continue;
9152 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
9153 fc2737d5 2022-09-15 thomas continue;
9154 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
9155 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
9156 fc2737d5 2022-09-15 thomas
9157 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
9158 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
9159 fc2737d5 2022-09-15 thomas if (err) {
9160 fc2737d5 2022-09-15 thomas free(line);
9161 fc2737d5 2022-09-15 thomas return err;
9162 fc2737d5 2022-09-15 thomas }
9163 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
9164 fc2737d5 2022-09-15 thomas free(wline);
9165 fc2737d5 2022-09-15 thomas wline = NULL;
9166 fc2737d5 2022-09-15 thomas
9167 fc2737d5 2022-09-15 thomas if (attr)
9168 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
9169 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
9170 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
9171 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
9172 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
9173 fc2737d5 2022-09-15 thomas if (err) {
9174 fc2737d5 2022-09-15 thomas free(line);
9175 fc2737d5 2022-09-15 thomas return err;
9176 fc2737d5 2022-09-15 thomas }
9177 fc2737d5 2022-09-15 thomas } else {
9178 fc2737d5 2022-09-15 thomas int skip;
9179 fc2737d5 2022-09-15 thomas
9180 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
9181 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
9182 fc2737d5 2022-09-15 thomas if (err) {
9183 fc2737d5 2022-09-15 thomas free(line);
9184 fc2737d5 2022-09-15 thomas return err;
9185 fc2737d5 2022-09-15 thomas }
9186 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
9187 fc2737d5 2022-09-15 thomas free(wline);
9188 fc2737d5 2022-09-15 thomas wline = NULL;
9189 fc2737d5 2022-09-15 thomas }
9190 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
9191 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
9192 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
9193 fc2737d5 2022-09-15 thomas } else {
9194 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
9195 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
9196 fc2737d5 2022-09-15 thomas }
9197 fc2737d5 2022-09-15 thomas if (attr)
9198 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
9199 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
9200 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
9201 fc2737d5 2022-09-15 thomas }
9202 fc2737d5 2022-09-15 thomas free(line);
9203 fc2737d5 2022-09-15 thomas if (nprinted > 0)
9204 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
9205 fc2737d5 2022-09-15 thomas else
9206 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
9207 fc2737d5 2022-09-15 thomas
9208 fc2737d5 2022-09-15 thomas view_border(view);
9209 fc2737d5 2022-09-15 thomas
9210 fc2737d5 2022-09-15 thomas if (s->eof) {
9211 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
9212 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
9213 fc2737d5 2022-09-15 thomas view->ncols - 1);
9214 fc2737d5 2022-09-15 thomas if (rc == ERR)
9215 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9216 fc2737d5 2022-09-15 thomas } else {
9217 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
9218 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
9219 fc2737d5 2022-09-15 thomas wstandout(view->window);
9220 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
9221 fc2737d5 2022-09-15 thomas view->ncols - 1);
9222 fc2737d5 2022-09-15 thomas if (rc == ERR)
9223 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
9224 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
9225 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
9226 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
9227 fc2737d5 2022-09-15 thomas if (rc == ERR)
9228 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
9229 fc2737d5 2022-09-15 thomas }
9230 fc2737d5 2022-09-15 thomas wstandend(view->window);
9231 fc2737d5 2022-09-15 thomas }
9232 fc2737d5 2022-09-15 thomas
9233 fc2737d5 2022-09-15 thomas return NULL;
9234 fc2737d5 2022-09-15 thomas }
9235 fc2737d5 2022-09-15 thomas
9236 fc2737d5 2022-09-15 thomas static const struct got_error *
9237 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
9238 fc2737d5 2022-09-15 thomas {
9239 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9240 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9241 fc2737d5 2022-09-15 thomas char *line = NULL;
9242 fc2737d5 2022-09-15 thomas ssize_t linelen;
9243 fc2737d5 2022-09-15 thomas size_t linesz = 0;
9244 fc2737d5 2022-09-15 thomas int eos, nscroll;
9245 fc2737d5 2022-09-15 thomas
9246 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
9247 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
9248 fc2737d5 2022-09-15 thomas --eos; /* border */
9249 fc2737d5 2022-09-15 thomas
9250 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
9251 fc2737d5 2022-09-15 thomas
9252 fc2737d5 2022-09-15 thomas switch (ch) {
9253 fc2737d5 2022-09-15 thomas case '0':
9254 fc2737d5 2022-09-15 thomas case '$':
9255 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
9256 fc2737d5 2022-09-15 thomas case 'l':
9257 fc2737d5 2022-09-15 thomas case KEY_LEFT:
9258 fc2737d5 2022-09-15 thomas case 'h':
9259 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
9260 fc2737d5 2022-09-15 thomas break;
9261 fc2737d5 2022-09-15 thomas case 'g':
9262 fc2737d5 2022-09-15 thomas case KEY_HOME:
9263 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9264 fc2737d5 2022-09-15 thomas view->count = 0;
9265 fc2737d5 2022-09-15 thomas break;
9266 fc2737d5 2022-09-15 thomas case 'G':
9267 fc2737d5 2022-09-15 thomas case KEY_END:
9268 fc2737d5 2022-09-15 thomas view->count = 0;
9269 fc2737d5 2022-09-15 thomas if (s->eof)
9270 fc2737d5 2022-09-15 thomas break;
9271 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
9272 fc2737d5 2022-09-15 thomas s->eof = 1;
9273 fc2737d5 2022-09-15 thomas break;
9274 fc2737d5 2022-09-15 thomas case 'k':
9275 fc2737d5 2022-09-15 thomas case KEY_UP:
9276 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
9277 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
9278 fc2737d5 2022-09-15 thomas else
9279 fc2737d5 2022-09-15 thomas view->count = 0;
9280 fc2737d5 2022-09-15 thomas break;
9281 fc2737d5 2022-09-15 thomas case CTRL('u'):
9282 fc2737d5 2022-09-15 thomas case 'u':
9283 fc2737d5 2022-09-15 thomas nscroll /= 2;
9284 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9285 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
9286 fc2737d5 2022-09-15 thomas case CTRL('b'):
9287 fc2737d5 2022-09-15 thomas case 'b':
9288 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
9289 fc2737d5 2022-09-15 thomas view->count = 0;
9290 fc2737d5 2022-09-15 thomas break;
9291 fc2737d5 2022-09-15 thomas }
9292 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
9293 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
9294 fc2737d5 2022-09-15 thomas break;
9295 fc2737d5 2022-09-15 thomas case 'j':
9296 fc2737d5 2022-09-15 thomas case KEY_DOWN:
9297 fc2737d5 2022-09-15 thomas case CTRL('n'):
9298 fc2737d5 2022-09-15 thomas if (!s->eof)
9299 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9300 fc2737d5 2022-09-15 thomas else
9301 fc2737d5 2022-09-15 thomas view->count = 0;
9302 fc2737d5 2022-09-15 thomas break;
9303 fc2737d5 2022-09-15 thomas case CTRL('d'):
9304 fc2737d5 2022-09-15 thomas case 'd':
9305 fc2737d5 2022-09-15 thomas nscroll /= 2;
9306 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9307 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9308 fc2737d5 2022-09-15 thomas case CTRL('f'):
9309 fc2737d5 2022-09-15 thomas case 'f':
9310 fc2737d5 2022-09-15 thomas case ' ':
9311 fc2737d5 2022-09-15 thomas if (s->eof) {
9312 fc2737d5 2022-09-15 thomas view->count = 0;
9313 fc2737d5 2022-09-15 thomas break;
9314 fc2737d5 2022-09-15 thomas }
9315 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9316 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9317 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9318 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9319 fc2737d5 2022-09-15 thomas if (feof(s->f))
9320 fc2737d5 2022-09-15 thomas s->eof = 1;
9321 fc2737d5 2022-09-15 thomas else
9322 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9323 fc2737d5 2022-09-15 thomas break;
9324 fc2737d5 2022-09-15 thomas }
9325 fc2737d5 2022-09-15 thomas }
9326 fc2737d5 2022-09-15 thomas free(line);
9327 fc2737d5 2022-09-15 thomas break;
9328 fc2737d5 2022-09-15 thomas default:
9329 fc2737d5 2022-09-15 thomas view->count = 0;
9330 fc2737d5 2022-09-15 thomas break;
9331 fc2737d5 2022-09-15 thomas }
9332 fc2737d5 2022-09-15 thomas
9333 fc2737d5 2022-09-15 thomas return err;
9334 fc2737d5 2022-09-15 thomas }
9335 fc2737d5 2022-09-15 thomas
9336 fc2737d5 2022-09-15 thomas static const struct got_error *
9337 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9338 fc2737d5 2022-09-15 thomas {
9339 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9340 fc2737d5 2022-09-15 thomas
9341 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9342 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9343 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9344 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9345 fc2737d5 2022-09-15 thomas
9346 fc2737d5 2022-09-15 thomas return NULL;
9347 fc2737d5 2022-09-15 thomas }
9348 fc2737d5 2022-09-15 thomas
9349 fc2737d5 2022-09-15 thomas static const struct got_error *
9350 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9351 fc2737d5 2022-09-15 thomas {
9352 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9353 fc2737d5 2022-09-15 thomas
9354 fc2737d5 2022-09-15 thomas
9355 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9356 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9357 fc2737d5 2022-09-15 thomas
9358 fc2737d5 2022-09-15 thomas wclear(view->window);
9359 fc2737d5 2022-09-15 thomas view->count = 0;
9360 fc2737d5 2022-09-15 thomas view->x = 0;
9361 fc2737d5 2022-09-15 thomas s->all = !s->all;
9362 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9363 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9364 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9365 fc2737d5 2022-09-15 thomas
9366 fc2737d5 2022-09-15 thomas return create_help(s);
9367 fc2737d5 2022-09-15 thomas }
9368 fc2737d5 2022-09-15 thomas
9369 fc2737d5 2022-09-15 thomas static const struct got_error *
9370 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9371 fc2737d5 2022-09-15 thomas {
9372 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9373 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9374 fc2737d5 2022-09-15 thomas
9375 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9376 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9377 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9378 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9379 fc2737d5 2022-09-15 thomas
9380 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9381 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9382 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9383 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9384 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9385 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9386 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9387 fc2737d5 2022-09-15 thomas
9388 fc2737d5 2022-09-15 thomas err = create_help(s);
9389 fc2737d5 2022-09-15 thomas return err;
9390 fc2737d5 2022-09-15 thomas }
9391 fc2737d5 2022-09-15 thomas
9392 fc2737d5 2022-09-15 thomas static const struct got_error *
9393 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9394 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9395 2a31b33b 2022-07-23 thomas {
9396 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9397 2a31b33b 2022-07-23 thomas
9398 2a31b33b 2022-07-23 thomas *new_view = NULL;
9399 2a31b33b 2022-07-23 thomas
9400 2a31b33b 2022-07-23 thomas switch (request) {
9401 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9402 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9403 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9404 2a31b33b 2022-07-23 thomas
9405 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9406 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9407 2a31b33b 2022-07-23 thomas view, s->repo);
9408 2a31b33b 2022-07-23 thomas } else
9409 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9410 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9411 2a31b33b 2022-07-23 thomas break;
9412 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9413 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9414 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9415 2a31b33b 2022-07-23 thomas
9416 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9417 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9418 2a31b33b 2022-07-23 thomas s->repo);
9419 2a31b33b 2022-07-23 thomas } else
9420 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9421 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9422 2a31b33b 2022-07-23 thomas break;
9423 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9424 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9425 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9426 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9427 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9428 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9429 2a31b33b 2022-07-23 thomas &view->state.tree);
9430 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9431 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9432 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9433 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9434 2a31b33b 2022-07-23 thomas else
9435 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9436 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9437 2a31b33b 2022-07-23 thomas break;
9438 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9439 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9440 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9441 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9442 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9443 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9444 2a31b33b 2022-07-23 thomas view->state.log.repo);
9445 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9446 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9447 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9448 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9449 2a31b33b 2022-07-23 thomas else
9450 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9451 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9452 2a31b33b 2022-07-23 thomas break;
9453 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9454 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9455 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9456 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9457 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9458 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9459 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9460 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9461 2a31b33b 2022-07-23 thomas else
9462 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9463 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9464 2a31b33b 2022-07-23 thomas if (err)
9465 2a31b33b 2022-07-23 thomas view_close(*new_view);
9466 2a31b33b 2022-07-23 thomas break;
9467 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9468 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9469 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9470 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9471 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9472 fc2737d5 2022-09-15 thomas if (err)
9473 fc2737d5 2022-09-15 thomas view_close(*new_view);
9474 fc2737d5 2022-09-15 thomas break;
9475 2a31b33b 2022-07-23 thomas default:
9476 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9477 2a31b33b 2022-07-23 thomas }
9478 2a31b33b 2022-07-23 thomas
9479 2a31b33b 2022-07-23 thomas return err;
9480 2a31b33b 2022-07-23 thomas }
9481 2a31b33b 2022-07-23 thomas
9482 a5d43cac 2022-07-01 thomas /*
9483 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9484 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9485 a5d43cac 2022-07-01 thomas */
9486 6458efa5 2020-11-24 stsp static void
9487 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9488 a5d43cac 2022-07-01 thomas {
9489 a5d43cac 2022-07-01 thomas switch (view->type) {
9490 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9491 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9492 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9493 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9494 a5d43cac 2022-07-01 thomas 1);
9495 a5d43cac 2022-07-01 thomas break;
9496 a5d43cac 2022-07-01 thomas }
9497 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9498 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9499 a5d43cac 2022-07-01 thomas else
9500 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9501 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9502 a5d43cac 2022-07-01 thomas break;
9503 a5d43cac 2022-07-01 thomas }
9504 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9505 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9506 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9507 a5d43cac 2022-07-01 thomas break;
9508 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9509 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9510 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9511 a5d43cac 2022-07-01 thomas break;
9512 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9513 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9514 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9515 a5d43cac 2022-07-01 thomas break;
9516 a5d43cac 2022-07-01 thomas default:
9517 a5d43cac 2022-07-01 thomas break;
9518 a5d43cac 2022-07-01 thomas }
9519 a5d43cac 2022-07-01 thomas
9520 a5d43cac 2022-07-01 thomas view->offset = 0;
9521 a5d43cac 2022-07-01 thomas }
9522 a5d43cac 2022-07-01 thomas
9523 a5d43cac 2022-07-01 thomas /*
9524 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9525 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9526 a5d43cac 2022-07-01 thomas */
9527 a5d43cac 2022-07-01 thomas static const struct got_error *
9528 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9529 a5d43cac 2022-07-01 thomas {
9530 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9531 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9532 a5d43cac 2022-07-01 thomas int *selected = NULL;
9533 a5d43cac 2022-07-01 thomas int header, offset;
9534 a5d43cac 2022-07-01 thomas
9535 a5d43cac 2022-07-01 thomas switch (view->type) {
9536 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9537 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9538 a5d43cac 2022-07-01 thomas header = 3;
9539 a5d43cac 2022-07-01 thomas scrolld = NULL;
9540 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9541 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9542 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9543 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9544 a5d43cac 2022-07-01 thomas view->offset = offset;
9545 a5d43cac 2022-07-01 thomas }
9546 a5d43cac 2022-07-01 thomas break;
9547 a5d43cac 2022-07-01 thomas }
9548 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9549 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9550 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9551 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9552 a5d43cac 2022-07-01 thomas selected = &s->selected;
9553 a5d43cac 2022-07-01 thomas break;
9554 a5d43cac 2022-07-01 thomas }
9555 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9556 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9557 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9558 a5d43cac 2022-07-01 thomas header = 3;
9559 a5d43cac 2022-07-01 thomas selected = &s->selected;
9560 a5d43cac 2022-07-01 thomas break;
9561 a5d43cac 2022-07-01 thomas }
9562 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9563 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9564 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9565 a5d43cac 2022-07-01 thomas header = 5;
9566 a5d43cac 2022-07-01 thomas selected = &s->selected;
9567 a5d43cac 2022-07-01 thomas break;
9568 a5d43cac 2022-07-01 thomas }
9569 a5d43cac 2022-07-01 thomas default:
9570 a5d43cac 2022-07-01 thomas selected = NULL;
9571 a5d43cac 2022-07-01 thomas scrolld = NULL;
9572 a5d43cac 2022-07-01 thomas header = 0;
9573 a5d43cac 2022-07-01 thomas break;
9574 a5d43cac 2022-07-01 thomas }
9575 a5d43cac 2022-07-01 thomas
9576 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9577 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9578 a5d43cac 2022-07-01 thomas view->offset = offset;
9579 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9580 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9581 a5d43cac 2022-07-01 thomas *selected -= offset;
9582 a5d43cac 2022-07-01 thomas }
9583 a5d43cac 2022-07-01 thomas }
9584 a5d43cac 2022-07-01 thomas
9585 a5d43cac 2022-07-01 thomas return err;
9586 a5d43cac 2022-07-01 thomas }
9587 a5d43cac 2022-07-01 thomas
9588 a5d43cac 2022-07-01 thomas static void
9589 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9590 ce5b7c56 2019-07-09 stsp {
9591 6059809a 2020-12-17 stsp size_t i;
9592 9f7d7167 2018-04-29 stsp
9593 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9594 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9595 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9596 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9597 ce5b7c56 2019-07-09 stsp }
9598 6879ba42 2020-10-01 naddy fputc('\n', fp);
9599 ce5b7c56 2019-07-09 stsp }
9600 ce5b7c56 2019-07-09 stsp
9601 4ed7e80c 2018-05-20 stsp __dead static void
9602 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9603 9f7d7167 2018-04-29 stsp {
9604 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9605 6879ba42 2020-10-01 naddy
9606 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9607 6879ba42 2020-10-01 naddy getprogname());
9608 6879ba42 2020-10-01 naddy if (hflag) {
9609 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9610 6879ba42 2020-10-01 naddy list_commands(fp);
9611 ee85c5e8 2020-02-29 stsp }
9612 6879ba42 2020-10-01 naddy exit(status);
9613 9f7d7167 2018-04-29 stsp }
9614 9f7d7167 2018-04-29 stsp
9615 c2301be8 2018-04-30 stsp static char **
9616 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9617 c2301be8 2018-04-30 stsp {
9618 ee85c5e8 2020-02-29 stsp va_list ap;
9619 c2301be8 2018-04-30 stsp char **argv;
9620 ee85c5e8 2020-02-29 stsp int i;
9621 c2301be8 2018-04-30 stsp
9622 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9623 ee85c5e8 2020-02-29 stsp
9624 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9625 c2301be8 2018-04-30 stsp if (argv == NULL)
9626 c2301be8 2018-04-30 stsp err(1, "calloc");
9627 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9628 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9629 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9630 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9631 c2301be8 2018-04-30 stsp }
9632 c2301be8 2018-04-30 stsp
9633 ee85c5e8 2020-02-29 stsp va_end(ap);
9634 c2301be8 2018-04-30 stsp return argv;
9635 ee85c5e8 2020-02-29 stsp }
9636 ee85c5e8 2020-02-29 stsp
9637 ee85c5e8 2020-02-29 stsp /*
9638 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9639 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9640 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9641 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9642 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9643 ee85c5e8 2020-02-29 stsp */
9644 ee85c5e8 2020-02-29 stsp static const struct got_error *
9645 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9646 ee85c5e8 2020-02-29 stsp {
9647 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9648 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9649 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9650 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9651 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9652 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9653 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9654 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9655 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9656 84de9106 2020-12-26 stsp
9657 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9658 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9659 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9660 ee85c5e8 2020-02-29 stsp
9661 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9662 7cd52833 2022-06-23 thomas if (error != NULL)
9663 7cd52833 2022-06-23 thomas goto done;
9664 7cd52833 2022-06-23 thomas
9665 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9666 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9667 ee85c5e8 2020-02-29 stsp goto done;
9668 ee85c5e8 2020-02-29 stsp
9669 ee85c5e8 2020-02-29 stsp if (worktree)
9670 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9671 ee85c5e8 2020-02-29 stsp else
9672 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9673 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9674 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9675 ee85c5e8 2020-02-29 stsp goto done;
9676 ee85c5e8 2020-02-29 stsp }
9677 ee85c5e8 2020-02-29 stsp
9678 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9679 ee85c5e8 2020-02-29 stsp if (error != NULL)
9680 ee85c5e8 2020-02-29 stsp goto done;
9681 ee85c5e8 2020-02-29 stsp
9682 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9683 ee85c5e8 2020-02-29 stsp repo, worktree);
9684 ee85c5e8 2020-02-29 stsp if (error)
9685 ee85c5e8 2020-02-29 stsp goto done;
9686 ee85c5e8 2020-02-29 stsp
9687 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9688 84de9106 2020-12-26 stsp if (error)
9689 84de9106 2020-12-26 stsp goto done;
9690 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9691 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9692 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9693 ee85c5e8 2020-02-29 stsp if (error)
9694 ee85c5e8 2020-02-29 stsp goto done;
9695 ee85c5e8 2020-02-29 stsp
9696 ee85c5e8 2020-02-29 stsp if (worktree) {
9697 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9698 ee85c5e8 2020-02-29 stsp worktree = NULL;
9699 ee85c5e8 2020-02-29 stsp }
9700 ee85c5e8 2020-02-29 stsp
9701 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9702 945f9229 2022-04-16 thomas if (error)
9703 945f9229 2022-04-16 thomas goto done;
9704 945f9229 2022-04-16 thomas
9705 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9706 ee85c5e8 2020-02-29 stsp if (error) {
9707 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9708 ee85c5e8 2020-02-29 stsp goto done;
9709 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9710 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9711 6879ba42 2020-10-01 naddy usage(1, 1);
9712 ee85c5e8 2020-02-29 stsp /* not reached */
9713 ee85c5e8 2020-02-29 stsp }
9714 ee85c5e8 2020-02-29 stsp
9715 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9716 ee85c5e8 2020-02-29 stsp if (error)
9717 ee85c5e8 2020-02-29 stsp goto done;
9718 ee85c5e8 2020-02-29 stsp
9719 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9720 ee85c5e8 2020-02-29 stsp argc = 4;
9721 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9722 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9723 ee85c5e8 2020-02-29 stsp done:
9724 1d0f4054 2021-06-17 stsp if (repo) {
9725 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9726 1d0f4054 2021-06-17 stsp if (error == NULL)
9727 1d0f4054 2021-06-17 stsp error = close_err;
9728 1d0f4054 2021-06-17 stsp }
9729 945f9229 2022-04-16 thomas if (commit)
9730 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9731 ee85c5e8 2020-02-29 stsp if (worktree)
9732 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9733 7cd52833 2022-06-23 thomas if (pack_fds) {
9734 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9735 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9736 7cd52833 2022-06-23 thomas if (error == NULL)
9737 7cd52833 2022-06-23 thomas error = pack_err;
9738 7cd52833 2022-06-23 thomas }
9739 ee85c5e8 2020-02-29 stsp free(id);
9740 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9741 ee85c5e8 2020-02-29 stsp free(commit_id);
9742 ee85c5e8 2020-02-29 stsp free(cwd);
9743 ee85c5e8 2020-02-29 stsp free(repo_path);
9744 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9745 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9746 ee85c5e8 2020-02-29 stsp int i;
9747 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9748 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9749 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9750 ee85c5e8 2020-02-29 stsp }
9751 87670572 2020-12-26 naddy tog_free_refs();
9752 ee85c5e8 2020-02-29 stsp return error;
9753 c2301be8 2018-04-30 stsp }
9754 c2301be8 2018-04-30 stsp
9755 9f7d7167 2018-04-29 stsp int
9756 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9757 9f7d7167 2018-04-29 stsp {
9758 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9759 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9760 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9761 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9762 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9763 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9764 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9765 83cd27f8 2020-01-13 stsp };
9766 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9767 a0abeae5 2023-02-17 thomas
9768 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
9769 a0abeae5 2023-02-17 thomas
9770 b85a3496 2023-04-14 thomas #if !defined(PROFILE) && !defined(TOG_REGRESS)
9771 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9772 a0abeae5 2023-02-17 thomas NULL) == -1)
9773 a0abeae5 2023-02-17 thomas err(1, "pledge");
9774 a0abeae5 2023-02-17 thomas #endif
9775 9f7d7167 2018-04-29 stsp
9776 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9777 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9778 cacf1690 2022-09-06 thomas
9779 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9780 9f7d7167 2018-04-29 stsp switch (ch) {
9781 9f7d7167 2018-04-29 stsp case 'h':
9782 9f7d7167 2018-04-29 stsp hflag = 1;
9783 9f7d7167 2018-04-29 stsp break;
9784 53ccebc2 2019-07-30 stsp case 'V':
9785 53ccebc2 2019-07-30 stsp Vflag = 1;
9786 53ccebc2 2019-07-30 stsp break;
9787 9f7d7167 2018-04-29 stsp default:
9788 6879ba42 2020-10-01 naddy usage(hflag, 1);
9789 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9790 9f7d7167 2018-04-29 stsp }
9791 9f7d7167 2018-04-29 stsp }
9792 9f7d7167 2018-04-29 stsp
9793 9f7d7167 2018-04-29 stsp argc -= optind;
9794 9f7d7167 2018-04-29 stsp argv += optind;
9795 9814e6a3 2020-09-27 naddy optind = 1;
9796 c2301be8 2018-04-30 stsp optreset = 1;
9797 9f7d7167 2018-04-29 stsp
9798 53ccebc2 2019-07-30 stsp if (Vflag) {
9799 53ccebc2 2019-07-30 stsp got_version_print_str();
9800 6879ba42 2020-10-01 naddy return 0;
9801 53ccebc2 2019-07-30 stsp }
9802 4010e238 2020-12-04 stsp
9803 c2301be8 2018-04-30 stsp if (argc == 0) {
9804 f29d3e89 2018-06-23 stsp if (hflag)
9805 6879ba42 2020-10-01 naddy usage(hflag, 0);
9806 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9807 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9808 c2301be8 2018-04-30 stsp argc = 1;
9809 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9810 c2301be8 2018-04-30 stsp } else {
9811 6059809a 2020-12-17 stsp size_t i;
9812 9f7d7167 2018-04-29 stsp
9813 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9814 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9815 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9816 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9817 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9818 9f7d7167 2018-04-29 stsp break;
9819 9f7d7167 2018-04-29 stsp }
9820 9f7d7167 2018-04-29 stsp }
9821 ee85c5e8 2020-02-29 stsp }
9822 3642c4c6 2019-07-09 stsp
9823 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9824 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9825 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9826 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9827 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9828 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9829 adf4c9e0 2022-07-03 thomas }
9830 adf4c9e0 2022-07-03 thomas
9831 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9832 ee85c5e8 2020-02-29 stsp if (argc != 1)
9833 6879ba42 2020-10-01 naddy usage(0, 1);
9834 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9835 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9836 ee85c5e8 2020-02-29 stsp } else {
9837 ee85c5e8 2020-02-29 stsp if (hflag)
9838 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9839 ee85c5e8 2020-02-29 stsp else
9840 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9841 9f7d7167 2018-04-29 stsp }
9842 9f7d7167 2018-04-29 stsp
9843 9f7d7167 2018-04-29 stsp endwin();
9844 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9845 a2f4a359 2020-02-28 stsp int i;
9846 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9847 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9848 a2f4a359 2020-02-28 stsp free(cmd_argv);
9849 a2f4a359 2020-02-28 stsp }
9850 a2f4a359 2020-02-28 stsp
9851 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9852 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9853 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9854 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9855 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9856 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9857 9f7d7167 2018-04-29 stsp return 0;
9858 9f7d7167 2018-04-29 stsp }