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 669b5ffa 2018-10-07 stsp /*
619 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
620 669b5ffa 2018-10-07 stsp *
621 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
622 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
623 669b5ffa 2018-10-07 stsp * there is enough screen estate.
624 669b5ffa 2018-10-07 stsp *
625 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
626 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
627 669b5ffa 2018-10-07 stsp *
628 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
629 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
630 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
631 669b5ffa 2018-10-07 stsp *
632 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
633 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
634 669b5ffa 2018-10-07 stsp */
635 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
636 669b5ffa 2018-10-07 stsp
637 cc3c9aac 2018-08-01 stsp struct tog_view {
638 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
639 26ed57b2 2018-05-19 stsp WINDOW *window;
640 26ed57b2 2018-05-19 stsp PANEL *panel;
641 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
642 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
643 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
644 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
645 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
646 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
647 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
648 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
649 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
650 9970f7fc 2020-12-03 stsp int dying;
651 669b5ffa 2018-10-07 stsp struct tog_view *parent;
652 669b5ffa 2018-10-07 stsp struct tog_view *child;
653 5dc9f4bc 2018-08-04 stsp
654 e78dc838 2020-12-04 stsp /*
655 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
656 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
657 e78dc838 2020-12-04 stsp * between parent and child.
658 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
659 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
660 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
661 e78dc838 2020-12-04 stsp * situations.
662 e78dc838 2020-12-04 stsp */
663 e78dc838 2020-12-04 stsp int focus_child;
664 e78dc838 2020-12-04 stsp
665 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
666 5dc9f4bc 2018-08-04 stsp /* type-specific state */
667 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
668 5dc9f4bc 2018-08-04 stsp union {
669 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
670 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
671 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
672 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
673 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
674 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
675 5dc9f4bc 2018-08-04 stsp } state;
676 e5a0f69f 2018-08-18 stsp
677 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
678 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
679 e78dc838 2020-12-04 stsp struct tog_view *, int);
680 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
681 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
682 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
683 60493ae3 2019-06-20 stsp
684 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
685 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
686 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
687 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
688 c0c4acc8 2021-01-24 stsp int search_started;
689 60493ae3 2019-06-20 stsp int searching;
690 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
691 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
692 60493ae3 2019-06-20 stsp int search_next_done;
693 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
694 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
695 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
696 1803e47f 2019-06-22 stsp regex_t regex;
697 41605754 2020-11-12 stsp regmatch_t regmatch;
698 f2d06bef 2023-01-23 thomas const char *action;
699 cc3c9aac 2018-08-01 stsp };
700 cd0acaa7 2018-05-20 stsp
701 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
702 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
703 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
704 78756c87 2020-11-24 stsp struct got_repository *);
705 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
706 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
707 e78dc838 2020-12-04 stsp struct tog_view *, int);
708 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
709 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
710 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
711 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
712 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
713 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
714 e5a0f69f 2018-08-18 stsp
715 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
716 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
717 78756c87 2020-11-24 stsp const char *, const char *, int);
718 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
719 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
720 e78dc838 2020-12-04 stsp struct tog_view *, int);
721 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
722 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
723 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
724 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
725 e5a0f69f 2018-08-18 stsp
726 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
727 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
728 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
729 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
730 e78dc838 2020-12-04 stsp struct tog_view *, int);
731 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
732 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
733 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
734 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
735 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
736 e5a0f69f 2018-08-18 stsp
737 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
738 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
739 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
740 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
741 e78dc838 2020-12-04 stsp struct tog_view *, int);
742 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
743 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
744 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
745 6458efa5 2020-11-24 stsp
746 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
747 6458efa5 2020-11-24 stsp struct got_repository *);
748 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
749 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
750 e78dc838 2020-12-04 stsp struct tog_view *, int);
751 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
752 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
753 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
754 2a7d3cd7 2022-09-17 thomas
755 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
756 2a7d3cd7 2022-09-17 thomas struct tog_view *);
757 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
758 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
759 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
760 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
761 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
762 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
763 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
764 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
765 25791caa 2018-10-24 stsp
766 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
767 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
768 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
769 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
770 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
771 25791caa 2018-10-24 stsp
772 25791caa 2018-10-24 stsp static void
773 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
774 25791caa 2018-10-24 stsp {
775 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
776 83baff54 2019-08-12 stsp }
777 83baff54 2019-08-12 stsp
778 83baff54 2019-08-12 stsp static void
779 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
780 83baff54 2019-08-12 stsp {
781 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
782 25791caa 2018-10-24 stsp }
783 26ed57b2 2018-05-19 stsp
784 61266923 2020-01-14 stsp static void
785 61266923 2020-01-14 stsp tog_sigcont(int signo)
786 61266923 2020-01-14 stsp {
787 61266923 2020-01-14 stsp tog_sigcont_received = 1;
788 61266923 2020-01-14 stsp }
789 61266923 2020-01-14 stsp
790 296152d1 2022-05-31 thomas static void
791 296152d1 2022-05-31 thomas tog_sigint(int signo)
792 296152d1 2022-05-31 thomas {
793 296152d1 2022-05-31 thomas tog_sigint_received = 1;
794 296152d1 2022-05-31 thomas }
795 296152d1 2022-05-31 thomas
796 296152d1 2022-05-31 thomas static void
797 296152d1 2022-05-31 thomas tog_sigterm(int signo)
798 296152d1 2022-05-31 thomas {
799 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
800 296152d1 2022-05-31 thomas }
801 296152d1 2022-05-31 thomas
802 296152d1 2022-05-31 thomas static int
803 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
804 296152d1 2022-05-31 thomas {
805 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
806 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
807 296152d1 2022-05-31 thomas }
808 296152d1 2022-05-31 thomas
809 e5a0f69f 2018-08-18 stsp static const struct got_error *
810 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
811 ea5e7bb5 2018-08-01 stsp {
812 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
813 e5a0f69f 2018-08-18 stsp
814 669b5ffa 2018-10-07 stsp if (view->child) {
815 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
816 669b5ffa 2018-10-07 stsp view->child = NULL;
817 669b5ffa 2018-10-07 stsp }
818 e5a0f69f 2018-08-18 stsp if (view->close)
819 e5a0f69f 2018-08-18 stsp err = view->close(view);
820 ea5e7bb5 2018-08-01 stsp if (view->panel)
821 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
822 ea5e7bb5 2018-08-01 stsp if (view->window)
823 ea5e7bb5 2018-08-01 stsp delwin(view->window);
824 ea5e7bb5 2018-08-01 stsp free(view);
825 f2d749db 2022-07-12 thomas return err ? err : child_err;
826 ea5e7bb5 2018-08-01 stsp }
827 ea5e7bb5 2018-08-01 stsp
828 ea5e7bb5 2018-08-01 stsp static struct tog_view *
829 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
830 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
831 ea5e7bb5 2018-08-01 stsp {
832 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
833 ea5e7bb5 2018-08-01 stsp
834 ea5e7bb5 2018-08-01 stsp if (view == NULL)
835 ea5e7bb5 2018-08-01 stsp return NULL;
836 ea5e7bb5 2018-08-01 stsp
837 d6b05b5b 2018-08-04 stsp view->type = type;
838 f7d12f7e 2018-08-01 stsp view->lines = LINES;
839 f7d12f7e 2018-08-01 stsp view->cols = COLS;
840 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
841 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
842 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
843 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
844 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
845 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
846 96a765a8 2018-08-04 stsp view_close(view);
847 ea5e7bb5 2018-08-01 stsp return NULL;
848 ea5e7bb5 2018-08-01 stsp }
849 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
850 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
851 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
852 96a765a8 2018-08-04 stsp view_close(view);
853 ea5e7bb5 2018-08-01 stsp return NULL;
854 ea5e7bb5 2018-08-01 stsp }
855 ea5e7bb5 2018-08-01 stsp
856 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
857 ea5e7bb5 2018-08-01 stsp return view;
858 cdf1ee82 2018-08-01 stsp }
859 cdf1ee82 2018-08-01 stsp
860 0cf4efb1 2018-09-29 stsp static int
861 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
862 0cf4efb1 2018-09-29 stsp {
863 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
864 0cf4efb1 2018-09-29 stsp return 0;
865 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
866 5c60c32a 2018-10-18 stsp }
867 5c60c32a 2018-10-18 stsp
868 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
869 a5d43cac 2022-07-01 thomas static int
870 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
871 a5d43cac 2022-07-01 thomas {
872 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
873 a5d43cac 2022-07-01 thomas }
874 a5d43cac 2022-07-01 thomas
875 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
876 5c60c32a 2018-10-18 stsp
877 5c60c32a 2018-10-18 stsp static const struct got_error *
878 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
879 5c60c32a 2018-10-18 stsp {
880 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
881 5c60c32a 2018-10-18 stsp
882 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
883 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
884 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
885 53d2bdd3 2022-07-10 thomas else
886 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
887 a5d43cac 2022-07-01 thomas view->begin_x = 0;
888 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
889 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
890 53d2bdd3 2022-07-10 thomas view->cols > 119)
891 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
892 53d2bdd3 2022-07-10 thomas else
893 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
894 a5d43cac 2022-07-01 thomas view->begin_y = 0;
895 a5d43cac 2022-07-01 thomas }
896 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
897 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
898 5c60c32a 2018-10-18 stsp view->lines = LINES;
899 5c60c32a 2018-10-18 stsp view->cols = COLS;
900 5c60c32a 2018-10-18 stsp err = view_resize(view);
901 5c60c32a 2018-10-18 stsp if (err)
902 5c60c32a 2018-10-18 stsp return err;
903 5c60c32a 2018-10-18 stsp
904 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
905 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
906 a5d43cac 2022-07-01 thomas
907 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
908 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
909 5c60c32a 2018-10-18 stsp
910 5c60c32a 2018-10-18 stsp return NULL;
911 5c60c32a 2018-10-18 stsp }
912 5c60c32a 2018-10-18 stsp
913 5c60c32a 2018-10-18 stsp static const struct got_error *
914 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
915 5c60c32a 2018-10-18 stsp {
916 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
917 5c60c32a 2018-10-18 stsp
918 5c60c32a 2018-10-18 stsp view->begin_x = 0;
919 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
920 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
921 5c60c32a 2018-10-18 stsp view->ncols = COLS;
922 5c60c32a 2018-10-18 stsp view->lines = LINES;
923 5c60c32a 2018-10-18 stsp view->cols = COLS;
924 5c60c32a 2018-10-18 stsp err = view_resize(view);
925 5c60c32a 2018-10-18 stsp if (err)
926 5c60c32a 2018-10-18 stsp return err;
927 5c60c32a 2018-10-18 stsp
928 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
929 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
930 5c60c32a 2018-10-18 stsp
931 5c60c32a 2018-10-18 stsp return NULL;
932 0cf4efb1 2018-09-29 stsp }
933 0cf4efb1 2018-09-29 stsp
934 5c60c32a 2018-10-18 stsp static int
935 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
936 5c60c32a 2018-10-18 stsp {
937 5c60c32a 2018-10-18 stsp return view->parent == NULL;
938 5c60c32a 2018-10-18 stsp }
939 5c60c32a 2018-10-18 stsp
940 b65b3ea0 2022-06-23 thomas static int
941 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
942 b65b3ea0 2022-06-23 thomas {
943 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
944 e44940c3 2022-07-01 thomas }
945 e44940c3 2022-07-01 thomas
946 e44940c3 2022-07-01 thomas static int
947 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
948 e44940c3 2022-07-01 thomas {
949 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
950 b65b3ea0 2022-06-23 thomas }
951 b65b3ea0 2022-06-23 thomas
952 444d5325 2022-07-03 thomas static int
953 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
954 444d5325 2022-07-03 thomas {
955 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
956 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
957 444d5325 2022-07-03 thomas }
958 444d5325 2022-07-03 thomas
959 a5d43cac 2022-07-01 thomas static void
960 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
961 a5d43cac 2022-07-01 thomas {
962 a5d43cac 2022-07-01 thomas PANEL *panel;
963 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
964 b65b3ea0 2022-06-23 thomas
965 a5d43cac 2022-07-01 thomas if (view->parent)
966 a5d43cac 2022-07-01 thomas return view_border(view->parent);
967 a5d43cac 2022-07-01 thomas
968 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
969 a5d43cac 2022-07-01 thomas if (panel == NULL)
970 a5d43cac 2022-07-01 thomas return;
971 a5d43cac 2022-07-01 thomas
972 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
973 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
974 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
975 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
976 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
977 a5d43cac 2022-07-01 thomas else
978 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
979 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
980 a5d43cac 2022-07-01 thomas }
981 a5d43cac 2022-07-01 thomas
982 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
983 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
984 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
985 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
986 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
987 a5d43cac 2022-07-01 thomas
988 4d8c2215 2018-08-19 stsp static const struct got_error *
989 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
990 f7d12f7e 2018-08-01 stsp {
991 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
992 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
993 f7d12f7e 2018-08-01 stsp
994 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
995 a5d43cac 2022-07-01 thomas
996 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
997 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
998 0cf4efb1 2018-09-29 stsp else
999 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1000 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1001 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1002 0cf4efb1 2018-09-29 stsp else
1003 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1004 6d0fee91 2018-08-01 stsp
1005 4918811f 2022-07-01 thomas if (view->child) {
1006 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1007 a5d43cac 2022-07-01 thomas
1008 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1009 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1010 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1011 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1012 40236d76 2022-06-23 thomas ncols = COLS;
1013 40236d76 2022-06-23 thomas
1014 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1015 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1016 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1017 5c60c32a 2018-10-18 stsp else
1018 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1019 5c60c32a 2018-10-18 stsp } else {
1020 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1021 40236d76 2022-06-23 thomas
1022 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1023 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1024 a5d43cac 2022-07-01 thomas }
1025 a5d43cac 2022-07-01 thomas /*
1026 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1027 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1028 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1029 a5d43cac 2022-07-01 thomas */
1030 a5d43cac 2022-07-01 thomas if (hs) {
1031 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1032 a5d43cac 2022-07-01 thomas if (err)
1033 a5d43cac 2022-07-01 thomas return err;
1034 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1035 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1036 a5d43cac 2022-07-01 thomas if (err)
1037 a5d43cac 2022-07-01 thomas return err;
1038 a5d43cac 2022-07-01 thomas }
1039 a5d43cac 2022-07-01 thomas view_border(view);
1040 a5d43cac 2022-07-01 thomas update_panels();
1041 a5d43cac 2022-07-01 thomas doupdate();
1042 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1043 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1044 a5d43cac 2022-07-01 thomas }
1045 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1046 40236d76 2022-06-23 thomas ncols = COLS;
1047 669b5ffa 2018-10-07 stsp
1048 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1049 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1050 ea0bff04 2022-07-19 thomas if (err)
1051 ea0bff04 2022-07-19 thomas return err;
1052 ea0bff04 2022-07-19 thomas }
1053 ea0bff04 2022-07-19 thomas
1054 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1055 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1056 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1057 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1058 40236d76 2022-06-23 thomas wclear(view->window);
1059 40236d76 2022-06-23 thomas
1060 40236d76 2022-06-23 thomas view->nlines = nlines;
1061 40236d76 2022-06-23 thomas view->ncols = ncols;
1062 40236d76 2022-06-23 thomas view->lines = LINES;
1063 40236d76 2022-06-23 thomas view->cols = COLS;
1064 40236d76 2022-06-23 thomas
1065 5c60c32a 2018-10-18 stsp return NULL;
1066 ea0bff04 2022-07-19 thomas }
1067 ea0bff04 2022-07-19 thomas
1068 ea0bff04 2022-07-19 thomas static const struct got_error *
1069 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1070 ea0bff04 2022-07-19 thomas {
1071 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1072 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1073 3a0139e8 2022-07-22 thomas int n = 0;
1074 ea0bff04 2022-07-19 thomas
1075 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1076 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1077 3a0139e8 2022-07-22 thomas
1078 ea0bff04 2022-07-19 thomas /*
1079 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1080 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1081 ea0bff04 2022-07-19 thomas */
1082 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1083 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1084 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1085 ea0bff04 2022-07-19 thomas }
1086 ea0bff04 2022-07-19 thomas
1087 ea0bff04 2022-07-19 thomas return err;
1088 97cb21cd 2022-07-12 thomas }
1089 97cb21cd 2022-07-12 thomas
1090 97cb21cd 2022-07-12 thomas static void
1091 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1092 97cb21cd 2022-07-12 thomas {
1093 97cb21cd 2022-07-12 thomas if (n == 0)
1094 97cb21cd 2022-07-12 thomas return;
1095 97cb21cd 2022-07-12 thomas
1096 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1097 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1098 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1099 97cb21cd 2022-07-12 thomas else
1100 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1101 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1102 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1103 97cb21cd 2022-07-12 thomas view->offset -= n;
1104 97cb21cd 2022-07-12 thomas else
1105 97cb21cd 2022-07-12 thomas view->offset = 0;
1106 97cb21cd 2022-07-12 thomas }
1107 53d2bdd3 2022-07-10 thomas }
1108 53d2bdd3 2022-07-10 thomas
1109 53d2bdd3 2022-07-10 thomas static const struct got_error *
1110 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1111 53d2bdd3 2022-07-10 thomas {
1112 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1113 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1114 53d2bdd3 2022-07-10 thomas
1115 53d2bdd3 2022-07-10 thomas if (view->parent)
1116 53d2bdd3 2022-07-10 thomas v = view->parent;
1117 53d2bdd3 2022-07-10 thomas else
1118 53d2bdd3 2022-07-10 thomas v = view;
1119 53d2bdd3 2022-07-10 thomas
1120 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1121 53d2bdd3 2022-07-10 thomas return NULL;
1122 53d2bdd3 2022-07-10 thomas
1123 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1124 53d2bdd3 2022-07-10 thomas
1125 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1126 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1127 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1128 53d2bdd3 2022-07-10 thomas if (view->parent)
1129 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1130 53d2bdd3 2022-07-10 thomas else
1131 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1132 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1133 53d2bdd3 2022-07-10 thomas view->count = 0;
1134 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1135 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1136 53d2bdd3 2022-07-10 thomas view->count = 0;
1137 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1138 53d2bdd3 2022-07-10 thomas }
1139 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1140 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1141 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1142 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1143 53d2bdd3 2022-07-10 thomas if (err)
1144 53d2bdd3 2022-07-10 thomas return err;
1145 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1146 53d2bdd3 2022-07-10 thomas } else {
1147 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1148 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1149 53d2bdd3 2022-07-10 thomas if (view->parent)
1150 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1151 53d2bdd3 2022-07-10 thomas else
1152 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1153 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1154 53d2bdd3 2022-07-10 thomas view->count = 0;
1155 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1156 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1157 53d2bdd3 2022-07-10 thomas view->count = 0;
1158 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1159 53d2bdd3 2022-07-10 thomas }
1160 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1161 53d2bdd3 2022-07-10 thomas }
1162 53d2bdd3 2022-07-10 thomas
1163 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1164 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1165 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1166 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1167 53d2bdd3 2022-07-10 thomas
1168 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1169 53d2bdd3 2022-07-10 thomas if (err)
1170 53d2bdd3 2022-07-10 thomas return err;
1171 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1172 53d2bdd3 2022-07-10 thomas if (err)
1173 53d2bdd3 2022-07-10 thomas return err;
1174 53d2bdd3 2022-07-10 thomas
1175 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1176 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1177 53d2bdd3 2022-07-10 thomas if (err)
1178 53d2bdd3 2022-07-10 thomas return err;
1179 53d2bdd3 2022-07-10 thomas }
1180 53d2bdd3 2022-07-10 thomas
1181 3a0139e8 2022-07-22 thomas if (v->resize)
1182 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1183 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1184 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1185 53d2bdd3 2022-07-10 thomas
1186 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1187 53d2bdd3 2022-07-10 thomas
1188 53d2bdd3 2022-07-10 thomas return err;
1189 53d2bdd3 2022-07-10 thomas }
1190 53d2bdd3 2022-07-10 thomas
1191 53d2bdd3 2022-07-10 thomas static void
1192 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1193 53d2bdd3 2022-07-10 thomas {
1194 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1195 53d2bdd3 2022-07-10 thomas
1196 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1197 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1198 669b5ffa 2018-10-07 stsp }
1199 669b5ffa 2018-10-07 stsp
1200 669b5ffa 2018-10-07 stsp static const struct got_error *
1201 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1202 669b5ffa 2018-10-07 stsp {
1203 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1204 669b5ffa 2018-10-07 stsp
1205 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1206 669b5ffa 2018-10-07 stsp return NULL;
1207 669b5ffa 2018-10-07 stsp
1208 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1209 669b5ffa 2018-10-07 stsp view->child = NULL;
1210 669b5ffa 2018-10-07 stsp return err;
1211 669b5ffa 2018-10-07 stsp }
1212 669b5ffa 2018-10-07 stsp
1213 40236d76 2022-06-23 thomas static const struct got_error *
1214 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1215 669b5ffa 2018-10-07 stsp {
1216 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1217 53d2bdd3 2022-07-10 thomas
1218 669b5ffa 2018-10-07 stsp view->child = child;
1219 669b5ffa 2018-10-07 stsp child->parent = view;
1220 40236d76 2022-06-23 thomas
1221 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1222 53d2bdd3 2022-07-10 thomas if (err)
1223 53d2bdd3 2022-07-10 thomas return err;
1224 53d2bdd3 2022-07-10 thomas
1225 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1226 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1227 53d2bdd3 2022-07-10 thomas
1228 53d2bdd3 2022-07-10 thomas return err;
1229 bfddd0d9 2018-09-29 stsp }
1230 2a31b33b 2022-07-23 thomas
1231 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1232 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1233 2a31b33b 2022-07-23 thomas
1234 2a31b33b 2022-07-23 thomas static const struct got_error *
1235 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1236 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1237 2a31b33b 2022-07-23 thomas {
1238 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1239 2a31b33b 2022-07-23 thomas const struct got_error *err;
1240 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1241 2a31b33b 2022-07-23 thomas
1242 2a31b33b 2022-07-23 thomas *requested = NULL;
1243 2a31b33b 2022-07-23 thomas
1244 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1245 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1246 bfddd0d9 2018-09-29 stsp
1247 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1248 2a31b33b 2022-07-23 thomas if (err)
1249 2a31b33b 2022-07-23 thomas return err;
1250 2a31b33b 2022-07-23 thomas
1251 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1252 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1253 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1254 2a31b33b 2022-07-23 thomas if (err)
1255 2a31b33b 2022-07-23 thomas return err;
1256 2a31b33b 2022-07-23 thomas }
1257 2a31b33b 2022-07-23 thomas
1258 2a31b33b 2022-07-23 thomas view->focussed = 0;
1259 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1260 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1261 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1262 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1263 2a31b33b 2022-07-23 thomas
1264 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1265 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1266 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1267 2a31b33b 2022-07-23 thomas if (err)
1268 2a31b33b 2022-07-23 thomas return err;
1269 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1270 2a31b33b 2022-07-23 thomas if (err)
1271 2a31b33b 2022-07-23 thomas return err;
1272 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1273 2a31b33b 2022-07-23 thomas } else
1274 2a31b33b 2022-07-23 thomas *requested = new_view;
1275 2a31b33b 2022-07-23 thomas
1276 2a31b33b 2022-07-23 thomas return NULL;
1277 2a31b33b 2022-07-23 thomas }
1278 2a31b33b 2022-07-23 thomas
1279 34bc9ec9 2019-02-22 stsp static void
1280 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1281 25791caa 2018-10-24 stsp {
1282 25791caa 2018-10-24 stsp int cols, lines;
1283 25791caa 2018-10-24 stsp struct winsize size;
1284 25791caa 2018-10-24 stsp
1285 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1286 25791caa 2018-10-24 stsp cols = 80; /* Default */
1287 25791caa 2018-10-24 stsp lines = 24;
1288 25791caa 2018-10-24 stsp } else {
1289 25791caa 2018-10-24 stsp cols = size.ws_col;
1290 25791caa 2018-10-24 stsp lines = size.ws_row;
1291 25791caa 2018-10-24 stsp }
1292 25791caa 2018-10-24 stsp resize_term(lines, cols);
1293 2b49a8ae 2019-06-22 stsp }
1294 2b49a8ae 2019-06-22 stsp
1295 2b49a8ae 2019-06-22 stsp static const struct got_error *
1296 f54d892e 2023-02-17 thomas view_search_start(struct tog_view *view, int fast_refresh)
1297 2b49a8ae 2019-06-22 stsp {
1298 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1299 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1300 2b49a8ae 2019-06-22 stsp char pattern[1024];
1301 2b49a8ae 2019-06-22 stsp int ret;
1302 c0c4acc8 2021-01-24 stsp
1303 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1304 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1305 c0c4acc8 2021-01-24 stsp view->searching = 0;
1306 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1307 c0c4acc8 2021-01-24 stsp }
1308 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1309 2b49a8ae 2019-06-22 stsp
1310 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1311 2b49a8ae 2019-06-22 stsp return NULL;
1312 2b49a8ae 2019-06-22 stsp
1313 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1314 a5d43cac 2022-07-01 thomas v = view->child;
1315 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1316 d07291c6 2022-12-30 thomas v = view->parent;
1317 2b49a8ae 2019-06-22 stsp
1318 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1319 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1320 a5d43cac 2022-07-01 thomas
1321 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1322 2b49a8ae 2019-06-22 stsp nocbreak();
1323 2b49a8ae 2019-06-22 stsp echo();
1324 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1325 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1326 2b49a8ae 2019-06-22 stsp cbreak();
1327 2b49a8ae 2019-06-22 stsp noecho();
1328 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1329 f54d892e 2023-02-17 thomas if (!fast_refresh)
1330 f54d892e 2023-02-17 thomas halfdelay(10);
1331 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1332 2b49a8ae 2019-06-22 stsp return NULL;
1333 2b49a8ae 2019-06-22 stsp
1334 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1335 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1336 7c32bd05 2019-06-22 stsp if (err) {
1337 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1338 7c32bd05 2019-06-22 stsp return err;
1339 7c32bd05 2019-06-22 stsp }
1340 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1341 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1342 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1343 2b49a8ae 2019-06-22 stsp view->search_next(view);
1344 2b49a8ae 2019-06-22 stsp }
1345 2b49a8ae 2019-06-22 stsp
1346 2b49a8ae 2019-06-22 stsp return NULL;
1347 64486692 2022-07-07 thomas }
1348 64486692 2022-07-07 thomas
1349 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1350 64486692 2022-07-07 thomas static const struct got_error *
1351 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1352 64486692 2022-07-07 thomas {
1353 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1354 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1355 64486692 2022-07-07 thomas
1356 64486692 2022-07-07 thomas if (view->parent)
1357 64486692 2022-07-07 thomas v = view->parent;
1358 64486692 2022-07-07 thomas else
1359 64486692 2022-07-07 thomas v = view;
1360 64486692 2022-07-07 thomas
1361 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1362 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1363 ddbc4d37 2022-07-12 thomas else
1364 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1365 64486692 2022-07-07 thomas
1366 ddbc4d37 2022-07-12 thomas if (!v->child)
1367 ddbc4d37 2022-07-12 thomas return NULL;
1368 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1369 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1370 ddbc4d37 2022-07-12 thomas
1371 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1372 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1373 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1374 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1375 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1376 64486692 2022-07-07 thomas
1377 ddbc4d37 2022-07-12 thomas
1378 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1379 64486692 2022-07-07 thomas v->ncols = COLS;
1380 64486692 2022-07-07 thomas v->child->ncols = COLS;
1381 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1382 64486692 2022-07-07 thomas
1383 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1384 64486692 2022-07-07 thomas if (err)
1385 64486692 2022-07-07 thomas return err;
1386 64486692 2022-07-07 thomas }
1387 64486692 2022-07-07 thomas v->child->mode = v->mode;
1388 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1389 64486692 2022-07-07 thomas v->focus_child = 1;
1390 64486692 2022-07-07 thomas
1391 64486692 2022-07-07 thomas err = view_fullscreen(v);
1392 64486692 2022-07-07 thomas if (err)
1393 64486692 2022-07-07 thomas return err;
1394 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1395 64486692 2022-07-07 thomas if (err)
1396 64486692 2022-07-07 thomas return err;
1397 64486692 2022-07-07 thomas
1398 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1399 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1400 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1401 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1402 cf1fe301 2022-07-29 thomas if (err)
1403 cf1fe301 2022-07-29 thomas return err;
1404 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1405 cf1fe301 2022-07-29 thomas if (err)
1406 cf1fe301 2022-07-29 thomas return err;
1407 ddbc4d37 2022-07-12 thomas } else {
1408 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1409 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1410 64486692 2022-07-07 thomas }
1411 f4e6231a 2022-07-22 thomas if (v->resize)
1412 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1413 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1414 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1415 64486692 2022-07-07 thomas
1416 64486692 2022-07-07 thomas return err;
1417 0cf4efb1 2018-09-29 stsp }
1418 6d0fee91 2018-08-01 stsp
1419 07b0611c 2022-06-23 thomas /*
1420 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1421 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1422 07b0611c 2022-06-23 thomas */
1423 07b0611c 2022-06-23 thomas static int
1424 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1425 07b0611c 2022-06-23 thomas {
1426 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1427 a5d43cac 2022-07-01 thomas int x, n = 0;
1428 07b0611c 2022-06-23 thomas
1429 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1430 a5d43cac 2022-07-01 thomas v = view->child;
1431 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1432 a5d43cac 2022-07-01 thomas v = view->parent;
1433 a5d43cac 2022-07-01 thomas
1434 07b0611c 2022-06-23 thomas view->count = 0;
1435 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1436 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1437 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1438 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1439 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1440 07b0611c 2022-06-23 thomas
1441 07b0611c 2022-06-23 thomas do {
1442 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1443 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1444 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1445 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1446 a5d43cac 2022-07-01 thomas }
1447 a5d43cac 2022-07-01 thomas
1448 07b0611c 2022-06-23 thomas /*
1449 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1450 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1451 07b0611c 2022-06-23 thomas */
1452 07b0611c 2022-06-23 thomas if (n >= 9999999)
1453 07b0611c 2022-06-23 thomas n = 9999999;
1454 07b0611c 2022-06-23 thomas else
1455 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1456 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1457 07b0611c 2022-06-23 thomas
1458 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1459 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1460 07dd3ed3 2022-08-06 thomas n = 0;
1461 07dd3ed3 2022-08-06 thomas c = 0;
1462 07dd3ed3 2022-08-06 thomas }
1463 07dd3ed3 2022-08-06 thomas
1464 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1465 07b0611c 2022-06-23 thomas view->count = n;
1466 07b0611c 2022-06-23 thomas
1467 07b0611c 2022-06-23 thomas return c;
1468 f2d06bef 2023-01-23 thomas }
1469 f2d06bef 2023-01-23 thomas
1470 f2d06bef 2023-01-23 thomas static void
1471 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1472 f2d06bef 2023-01-23 thomas {
1473 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1474 f2d06bef 2023-01-23 thomas
1475 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1476 f2d06bef 2023-01-23 thomas v = view->child;
1477 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1478 f2d06bef 2023-01-23 thomas v = view->parent;
1479 f2d06bef 2023-01-23 thomas
1480 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1481 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1482 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1483 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1484 f2d06bef 2023-01-23 thomas
1485 f2d06bef 2023-01-23 thomas /*
1486 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1487 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1488 f2d06bef 2023-01-23 thomas */
1489 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1490 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1491 f2d06bef 2023-01-23 thomas view->action = NULL;
1492 f2d06bef 2023-01-23 thomas } else
1493 f2d06bef 2023-01-23 thomas view->action = NULL;
1494 07b0611c 2022-06-23 thomas }
1495 07b0611c 2022-06-23 thomas
1496 0cf4efb1 2018-09-29 stsp static const struct got_error *
1497 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1498 f54d892e 2023-02-17 thomas struct tog_view_list_head *views, int fast_refresh)
1499 e5a0f69f 2018-08-18 stsp {
1500 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1501 669b5ffa 2018-10-07 stsp struct tog_view *v;
1502 1a76625f 2018-10-22 stsp int ch, errcode;
1503 e5a0f69f 2018-08-18 stsp
1504 e5a0f69f 2018-08-18 stsp *new = NULL;
1505 f2d06bef 2023-01-23 thomas
1506 f2d06bef 2023-01-23 thomas if (view->action)
1507 f2d06bef 2023-01-23 thomas action_report(view);
1508 8f4ed634 2020-03-26 stsp
1509 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1510 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1511 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1512 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1513 07b0611c 2022-06-23 thomas view->count = 0;
1514 07b0611c 2022-06-23 thomas }
1515 e5a0f69f 2018-08-18 stsp
1516 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1517 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1518 82954512 2020-02-03 stsp if (errcode)
1519 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1520 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1521 3da8ef85 2021-09-21 stsp sched_yield();
1522 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1523 82954512 2020-02-03 stsp if (errcode)
1524 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1525 82954512 2020-02-03 stsp "pthread_mutex_lock");
1526 60493ae3 2019-06-20 stsp view->search_next(view);
1527 60493ae3 2019-06-20 stsp return NULL;
1528 60493ae3 2019-06-20 stsp }
1529 60493ae3 2019-06-20 stsp
1530 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1531 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1532 1a76625f 2018-10-22 stsp if (errcode)
1533 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1534 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1535 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1536 634cb454 2022-07-03 thomas cbreak();
1537 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1538 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1539 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1540 634cb454 2022-07-03 thomas view->count = 0;
1541 634cb454 2022-07-03 thomas else
1542 634cb454 2022-07-03 thomas ch = view->ch;
1543 634cb454 2022-07-03 thomas } else {
1544 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1545 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1546 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1547 07b0611c 2022-06-23 thomas }
1548 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1549 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1550 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1551 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1552 1a76625f 2018-10-22 stsp if (errcode)
1553 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1554 25791caa 2018-10-24 stsp
1555 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1556 25791caa 2018-10-24 stsp tog_resizeterm();
1557 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1558 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1559 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1560 25791caa 2018-10-24 stsp err = view_resize(v);
1561 25791caa 2018-10-24 stsp if (err)
1562 25791caa 2018-10-24 stsp return err;
1563 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1564 25791caa 2018-10-24 stsp if (err)
1565 25791caa 2018-10-24 stsp return err;
1566 cdfcfb03 2020-12-06 stsp if (v->child) {
1567 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1568 cdfcfb03 2020-12-06 stsp if (err)
1569 cdfcfb03 2020-12-06 stsp return err;
1570 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1571 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1572 cdfcfb03 2020-12-06 stsp if (err)
1573 cdfcfb03 2020-12-06 stsp return err;
1574 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1575 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1576 53d2bdd3 2022-07-10 thomas if (err)
1577 53d2bdd3 2022-07-10 thomas return err;
1578 53d2bdd3 2022-07-10 thomas }
1579 cdfcfb03 2020-12-06 stsp }
1580 25791caa 2018-10-24 stsp }
1581 25791caa 2018-10-24 stsp }
1582 25791caa 2018-10-24 stsp
1583 e5a0f69f 2018-08-18 stsp switch (ch) {
1584 fc2737d5 2022-09-15 thomas case '?':
1585 fc2737d5 2022-09-15 thomas case 'H':
1586 fc2737d5 2022-09-15 thomas case KEY_F(1):
1587 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1588 fc2737d5 2022-09-15 thomas err = view->reset(view);
1589 fc2737d5 2022-09-15 thomas else
1590 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1591 fc2737d5 2022-09-15 thomas break;
1592 1e37a5c2 2019-05-12 jcs case '\t':
1593 07b0611c 2022-06-23 thomas view->count = 0;
1594 1e37a5c2 2019-05-12 jcs if (view->child) {
1595 e78dc838 2020-12-04 stsp view->focussed = 0;
1596 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1597 e78dc838 2020-12-04 stsp view->focus_child = 1;
1598 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1599 e78dc838 2020-12-04 stsp view->focussed = 0;
1600 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1601 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1602 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1603 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1604 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1605 3a0139e8 2022-07-22 thomas 0);
1606 a5d43cac 2022-07-01 thomas if (err)
1607 a5d43cac 2022-07-01 thomas return err;
1608 a5d43cac 2022-07-01 thomas }
1609 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1610 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1611 a5d43cac 2022-07-01 thomas if (err)
1612 a5d43cac 2022-07-01 thomas return err;
1613 a5d43cac 2022-07-01 thomas }
1614 1e37a5c2 2019-05-12 jcs }
1615 1e37a5c2 2019-05-12 jcs break;
1616 1e37a5c2 2019-05-12 jcs case 'q':
1617 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1618 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1619 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1620 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1621 a5d43cac 2022-07-01 thomas if (err)
1622 a5d43cac 2022-07-01 thomas break;
1623 a5d43cac 2022-07-01 thomas }
1624 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1625 a5d43cac 2022-07-01 thomas }
1626 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1627 9970f7fc 2020-12-03 stsp view->dying = 1;
1628 1e37a5c2 2019-05-12 jcs break;
1629 1e37a5c2 2019-05-12 jcs case 'Q':
1630 1e37a5c2 2019-05-12 jcs *done = 1;
1631 1e37a5c2 2019-05-12 jcs break;
1632 1c5e5faa 2022-06-23 thomas case 'F':
1633 07b0611c 2022-06-23 thomas view->count = 0;
1634 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1635 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1636 1e37a5c2 2019-05-12 jcs break;
1637 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1638 e78dc838 2020-12-04 stsp view->focussed = 0;
1639 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1640 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1641 53d2bdd3 2022-07-10 thomas } else {
1642 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1643 53d2bdd3 2022-07-10 thomas if (!err)
1644 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1645 53d2bdd3 2022-07-10 thomas }
1646 1e37a5c2 2019-05-12 jcs if (err)
1647 1e37a5c2 2019-05-12 jcs break;
1648 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1649 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1650 1e37a5c2 2019-05-12 jcs } else {
1651 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1652 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1653 e78dc838 2020-12-04 stsp view->focussed = 1;
1654 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1655 1e37a5c2 2019-05-12 jcs } else {
1656 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1657 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1658 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1659 53d2bdd3 2022-07-10 thomas if (!err)
1660 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1661 669b5ffa 2018-10-07 stsp }
1662 1e37a5c2 2019-05-12 jcs if (err)
1663 1e37a5c2 2019-05-12 jcs break;
1664 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1665 a5d43cac 2022-07-01 thomas }
1666 a5d43cac 2022-07-01 thomas if (err)
1667 a5d43cac 2022-07-01 thomas break;
1668 3a0139e8 2022-07-22 thomas if (view->resize) {
1669 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1670 a5d43cac 2022-07-01 thomas if (err)
1671 a5d43cac 2022-07-01 thomas break;
1672 1e37a5c2 2019-05-12 jcs }
1673 a5d43cac 2022-07-01 thomas if (view->parent)
1674 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1675 a5d43cac 2022-07-01 thomas if (!err)
1676 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1677 1e37a5c2 2019-05-12 jcs break;
1678 64486692 2022-07-07 thomas case 'S':
1679 53d2bdd3 2022-07-10 thomas view->count = 0;
1680 64486692 2022-07-07 thomas err = switch_split(view);
1681 53d2bdd3 2022-07-10 thomas break;
1682 53d2bdd3 2022-07-10 thomas case '-':
1683 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1684 64486692 2022-07-07 thomas break;
1685 53d2bdd3 2022-07-10 thomas case '+':
1686 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1687 53d2bdd3 2022-07-10 thomas break;
1688 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1689 60493ae3 2019-06-20 stsp break;
1690 60493ae3 2019-06-20 stsp case '/':
1691 07b0611c 2022-06-23 thomas view->count = 0;
1692 60493ae3 2019-06-20 stsp if (view->search_start)
1693 f54d892e 2023-02-17 thomas view_search_start(view, fast_refresh);
1694 60493ae3 2019-06-20 stsp else
1695 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1696 1e37a5c2 2019-05-12 jcs break;
1697 b1bf1435 2019-06-21 stsp case 'N':
1698 60493ae3 2019-06-20 stsp case 'n':
1699 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1700 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1701 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1702 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1703 60493ae3 2019-06-20 stsp view->search_next(view);
1704 60493ae3 2019-06-20 stsp } else
1705 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1706 adf4c9e0 2022-07-03 thomas break;
1707 adf4c9e0 2022-07-03 thomas case 'A':
1708 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1709 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1710 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1711 f2d06bef 2023-01-23 thomas } else {
1712 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1713 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1714 f2d06bef 2023-01-23 thomas }
1715 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1716 adf4c9e0 2022-07-03 thomas if (v->reset) {
1717 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1718 adf4c9e0 2022-07-03 thomas if (err)
1719 adf4c9e0 2022-07-03 thomas return err;
1720 adf4c9e0 2022-07-03 thomas }
1721 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1722 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1723 adf4c9e0 2022-07-03 thomas if (err)
1724 adf4c9e0 2022-07-03 thomas return err;
1725 adf4c9e0 2022-07-03 thomas }
1726 adf4c9e0 2022-07-03 thomas }
1727 60493ae3 2019-06-20 stsp break;
1728 1e37a5c2 2019-05-12 jcs default:
1729 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1730 1e37a5c2 2019-05-12 jcs break;
1731 e5a0f69f 2018-08-18 stsp }
1732 e5a0f69f 2018-08-18 stsp
1733 e5a0f69f 2018-08-18 stsp return err;
1734 bcbd79e2 2018-08-19 stsp }
1735 bcbd79e2 2018-08-19 stsp
1736 ef20f542 2022-06-26 thomas static int
1737 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1738 a3404814 2018-09-02 stsp {
1739 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1740 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1741 669b5ffa 2018-10-07 stsp return 0;
1742 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1743 669b5ffa 2018-10-07 stsp return 0;
1744 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1745 a3404814 2018-09-02 stsp return 0;
1746 a3404814 2018-09-02 stsp
1747 669b5ffa 2018-10-07 stsp return view->focussed;
1748 a3404814 2018-09-02 stsp }
1749 a3404814 2018-09-02 stsp
1750 bcbd79e2 2018-08-19 stsp static const struct got_error *
1751 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1752 e5a0f69f 2018-08-18 stsp {
1753 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1754 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1755 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1756 64486692 2022-07-07 thomas char *mode;
1757 fd823528 2018-10-22 stsp int fast_refresh = 10;
1758 1a76625f 2018-10-22 stsp int done = 0, errcode;
1759 e5a0f69f 2018-08-18 stsp
1760 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1761 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1762 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1763 64486692 2022-07-07 thomas else
1764 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1765 64486692 2022-07-07 thomas
1766 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1767 1a76625f 2018-10-22 stsp if (errcode)
1768 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1769 1a76625f 2018-10-22 stsp
1770 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1771 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1772 e5a0f69f 2018-08-18 stsp
1773 1004088d 2018-09-29 stsp view->focussed = 1;
1774 878940b7 2018-09-29 stsp err = view->show(view);
1775 0cf4efb1 2018-09-29 stsp if (err)
1776 0cf4efb1 2018-09-29 stsp return err;
1777 0cf4efb1 2018-09-29 stsp update_panels();
1778 0cf4efb1 2018-09-29 stsp doupdate();
1779 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1780 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1781 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1782 b185e033 2023-01-23 thomas if (fast_refresh && --fast_refresh == 0)
1783 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1784 fd823528 2018-10-22 stsp
1785 f54d892e 2023-02-17 thomas err = view_input(&new_view, &done, view, &views, fast_refresh);
1786 e5a0f69f 2018-08-18 stsp if (err)
1787 e5a0f69f 2018-08-18 stsp break;
1788 6ec3d39c 2023-01-23 thomas
1789 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
1790 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
1791 6ec3d39c 2023-01-23 thomas done = 1;
1792 6ec3d39c 2023-01-23 thomas if (done) {
1793 6ec3d39c 2023-01-23 thomas struct tog_view *v;
1794 6ec3d39c 2023-01-23 thomas
1795 6ec3d39c 2023-01-23 thomas /*
1796 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
1797 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
1798 6ec3d39c 2023-01-23 thomas */
1799 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
1800 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
1801 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
1802 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
1803 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
1804 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
1805 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
1806 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
1807 6ec3d39c 2023-01-23 thomas }
1808 6ec3d39c 2023-01-23 thomas }
1809 6ec3d39c 2023-01-23 thomas doupdate();
1810 6ec3d39c 2023-01-23 thomas }
1811 6ec3d39c 2023-01-23 thomas
1812 9970f7fc 2020-12-03 stsp if (view->dying) {
1813 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1814 669b5ffa 2018-10-07 stsp
1815 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1816 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1817 9970f7fc 2020-12-03 stsp entry);
1818 e78dc838 2020-12-04 stsp else if (view->parent)
1819 669b5ffa 2018-10-07 stsp prev = view->parent;
1820 669b5ffa 2018-10-07 stsp
1821 e78dc838 2020-12-04 stsp if (view->parent) {
1822 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1823 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1824 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1825 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1826 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1827 40236d76 2022-06-23 thomas if (err)
1828 40236d76 2022-06-23 thomas break;
1829 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1830 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1831 e78dc838 2020-12-04 stsp } else
1832 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1833 669b5ffa 2018-10-07 stsp
1834 9970f7fc 2020-12-03 stsp err = view_close(view);
1835 fb59748f 2020-12-05 stsp if (err)
1836 e5a0f69f 2018-08-18 stsp goto done;
1837 669b5ffa 2018-10-07 stsp
1838 e78dc838 2020-12-04 stsp view = NULL;
1839 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1840 e78dc838 2020-12-04 stsp if (v->focussed)
1841 e78dc838 2020-12-04 stsp break;
1842 0cf4efb1 2018-09-29 stsp }
1843 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1844 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1845 e78dc838 2020-12-04 stsp if (prev)
1846 e78dc838 2020-12-04 stsp view = prev;
1847 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1848 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1849 e78dc838 2020-12-04 stsp tog_view_list_head);
1850 e78dc838 2020-12-04 stsp }
1851 e78dc838 2020-12-04 stsp if (view) {
1852 e78dc838 2020-12-04 stsp if (view->focus_child) {
1853 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1854 e78dc838 2020-12-04 stsp view = view->child;
1855 e78dc838 2020-12-04 stsp } else
1856 e78dc838 2020-12-04 stsp view->focussed = 1;
1857 e78dc838 2020-12-04 stsp }
1858 e78dc838 2020-12-04 stsp }
1859 e5a0f69f 2018-08-18 stsp }
1860 bcbd79e2 2018-08-19 stsp if (new_view) {
1861 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1862 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1863 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1864 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1865 86c66b02 2018-10-18 stsp continue;
1866 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1867 86c66b02 2018-10-18 stsp err = view_close(v);
1868 86c66b02 2018-10-18 stsp if (err)
1869 86c66b02 2018-10-18 stsp goto done;
1870 86c66b02 2018-10-18 stsp break;
1871 86c66b02 2018-10-18 stsp }
1872 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1873 fed7eaa8 2018-10-24 stsp view = new_view;
1874 7cd52833 2022-06-23 thomas }
1875 6ec3d39c 2023-01-23 thomas if (view && !done) {
1876 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1877 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1878 e78dc838 2020-12-04 stsp view = view->child;
1879 e78dc838 2020-12-04 stsp } else {
1880 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1881 e78dc838 2020-12-04 stsp view = view->parent;
1882 1a76625f 2018-10-22 stsp }
1883 e78dc838 2020-12-04 stsp show_panel(view->panel);
1884 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1885 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1886 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1887 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1888 669b5ffa 2018-10-07 stsp if (err)
1889 1a76625f 2018-10-22 stsp goto done;
1890 669b5ffa 2018-10-07 stsp }
1891 669b5ffa 2018-10-07 stsp err = view->show(view);
1892 0cf4efb1 2018-09-29 stsp if (err)
1893 1a76625f 2018-10-22 stsp goto done;
1894 669b5ffa 2018-10-07 stsp if (view->child) {
1895 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1896 669b5ffa 2018-10-07 stsp if (err)
1897 1a76625f 2018-10-22 stsp goto done;
1898 669b5ffa 2018-10-07 stsp }
1899 1a76625f 2018-10-22 stsp update_panels();
1900 1a76625f 2018-10-22 stsp doupdate();
1901 0cf4efb1 2018-09-29 stsp }
1902 e5a0f69f 2018-08-18 stsp }
1903 e5a0f69f 2018-08-18 stsp done:
1904 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1905 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1906 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1907 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1908 f2d749db 2022-07-12 thomas close_err = view_close(view);
1909 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1910 f2d749db 2022-07-12 thomas err = close_err;
1911 e5a0f69f 2018-08-18 stsp }
1912 1a76625f 2018-10-22 stsp
1913 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1914 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1915 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1916 1a76625f 2018-10-22 stsp
1917 e5a0f69f 2018-08-18 stsp return err;
1918 ea5e7bb5 2018-08-01 stsp }
1919 ea5e7bb5 2018-08-01 stsp
1920 4ed7e80c 2018-05-20 stsp __dead static void
1921 9f7d7167 2018-04-29 stsp usage_log(void)
1922 9f7d7167 2018-04-29 stsp {
1923 80ddbec8 2018-04-29 stsp endwin();
1924 c70c5802 2018-08-01 stsp fprintf(stderr,
1925 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1926 9f7d7167 2018-04-29 stsp getprogname());
1927 9f7d7167 2018-04-29 stsp exit(1);
1928 80ddbec8 2018-04-29 stsp }
1929 80ddbec8 2018-04-29 stsp
1930 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1931 80ddbec8 2018-04-29 stsp static const struct got_error *
1932 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1933 963b370f 2018-05-20 stsp {
1934 00dfcb92 2018-06-11 stsp char *vis = NULL;
1935 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1936 963b370f 2018-05-20 stsp
1937 963b370f 2018-05-20 stsp *ws = NULL;
1938 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1939 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1940 00dfcb92 2018-06-11 stsp int vislen;
1941 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1942 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1943 00dfcb92 2018-06-11 stsp
1944 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1945 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1946 00dfcb92 2018-06-11 stsp if (err)
1947 00dfcb92 2018-06-11 stsp return err;
1948 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1949 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1950 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1951 a7f50699 2018-06-11 stsp goto done;
1952 a7f50699 2018-06-11 stsp }
1953 00dfcb92 2018-06-11 stsp }
1954 963b370f 2018-05-20 stsp
1955 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1956 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1957 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1958 a7f50699 2018-06-11 stsp goto done;
1959 a7f50699 2018-06-11 stsp }
1960 963b370f 2018-05-20 stsp
1961 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1962 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1963 a7f50699 2018-06-11 stsp done:
1964 00dfcb92 2018-06-11 stsp free(vis);
1965 963b370f 2018-05-20 stsp if (err) {
1966 963b370f 2018-05-20 stsp free(*ws);
1967 963b370f 2018-05-20 stsp *ws = NULL;
1968 963b370f 2018-05-20 stsp *wlen = 0;
1969 963b370f 2018-05-20 stsp }
1970 963b370f 2018-05-20 stsp return err;
1971 05171be4 2022-06-23 thomas }
1972 05171be4 2022-06-23 thomas
1973 05171be4 2022-06-23 thomas static const struct got_error *
1974 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1975 05171be4 2022-06-23 thomas {
1976 05171be4 2022-06-23 thomas char *dst;
1977 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1978 05171be4 2022-06-23 thomas
1979 05171be4 2022-06-23 thomas *ptr = NULL;
1980 05171be4 2022-06-23 thomas n = len = strlen(src);
1981 83316834 2022-06-23 thomas dst = malloc(n + 1);
1982 05171be4 2022-06-23 thomas if (dst == NULL)
1983 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1984 05171be4 2022-06-23 thomas
1985 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1986 05171be4 2022-06-23 thomas const char c = src[idx];
1987 05171be4 2022-06-23 thomas
1988 05171be4 2022-06-23 thomas if (c == '\t') {
1989 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1990 b235ad5d 2022-06-23 thomas char *p;
1991 b235ad5d 2022-06-23 thomas
1992 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1993 83316834 2022-06-23 thomas if (p == NULL) {
1994 83316834 2022-06-23 thomas free(dst);
1995 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1996 83316834 2022-06-23 thomas
1997 83316834 2022-06-23 thomas }
1998 83316834 2022-06-23 thomas dst = p;
1999 05171be4 2022-06-23 thomas n += nb;
2000 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
2001 05171be4 2022-06-23 thomas sz += nb;
2002 05171be4 2022-06-23 thomas } else
2003 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2004 05171be4 2022-06-23 thomas ++idx;
2005 05171be4 2022-06-23 thomas }
2006 05171be4 2022-06-23 thomas
2007 05171be4 2022-06-23 thomas dst[sz] = '\0';
2008 05171be4 2022-06-23 thomas *ptr = dst;
2009 05171be4 2022-06-23 thomas return NULL;
2010 963b370f 2018-05-20 stsp }
2011 963b370f 2018-05-20 stsp
2012 8d208d34 2022-06-23 thomas /*
2013 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2014 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2015 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
2016 8d208d34 2022-06-23 thomas * *rcol.
2017 f91a2b48 2022-06-23 thomas */
2018 8d208d34 2022-06-23 thomas static int
2019 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2020 8d208d34 2022-06-23 thomas {
2021 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2022 f91a2b48 2022-06-23 thomas
2023 8d208d34 2022-06-23 thomas if (n == 0) {
2024 8d208d34 2022-06-23 thomas *rcol = cols;
2025 8d208d34 2022-06-23 thomas return off;
2026 8d208d34 2022-06-23 thomas }
2027 f91a2b48 2022-06-23 thomas
2028 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2029 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2030 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2031 8d208d34 2022-06-23 thomas else
2032 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2033 f91a2b48 2022-06-23 thomas
2034 8d208d34 2022-06-23 thomas if (width == -1) {
2035 8d208d34 2022-06-23 thomas width = 1;
2036 8d208d34 2022-06-23 thomas wline[i] = L'.';
2037 f91a2b48 2022-06-23 thomas }
2038 f91a2b48 2022-06-23 thomas
2039 8d208d34 2022-06-23 thomas if (cols + width > n)
2040 8d208d34 2022-06-23 thomas break;
2041 8d208d34 2022-06-23 thomas cols += width;
2042 f91a2b48 2022-06-23 thomas }
2043 f91a2b48 2022-06-23 thomas
2044 8d208d34 2022-06-23 thomas *rcol = cols;
2045 8d208d34 2022-06-23 thomas return i;
2046 f91a2b48 2022-06-23 thomas }
2047 f91a2b48 2022-06-23 thomas
2048 f91a2b48 2022-06-23 thomas /*
2049 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2050 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2051 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2052 f91a2b48 2022-06-23 thomas */
2053 f91a2b48 2022-06-23 thomas static const struct got_error *
2054 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2055 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2056 f91a2b48 2022-06-23 thomas {
2057 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2058 8d208d34 2022-06-23 thomas int cols;
2059 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2060 05171be4 2022-06-23 thomas char *exstr = NULL;
2061 963b370f 2018-05-20 stsp size_t wlen;
2062 8d208d34 2022-06-23 thomas int i, scrollx;
2063 963b370f 2018-05-20 stsp
2064 963b370f 2018-05-20 stsp *wlinep = NULL;
2065 b700b5d6 2018-07-10 stsp *widthp = 0;
2066 963b370f 2018-05-20 stsp
2067 05171be4 2022-06-23 thomas if (expand) {
2068 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2069 05171be4 2022-06-23 thomas if (err)
2070 05171be4 2022-06-23 thomas return err;
2071 05171be4 2022-06-23 thomas }
2072 05171be4 2022-06-23 thomas
2073 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2074 05171be4 2022-06-23 thomas free(exstr);
2075 963b370f 2018-05-20 stsp if (err)
2076 963b370f 2018-05-20 stsp return err;
2077 963b370f 2018-05-20 stsp
2078 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2079 f91a2b48 2022-06-23 thomas
2080 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2081 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2082 3f670bfb 2020-12-10 stsp wlen--;
2083 3f670bfb 2020-12-10 stsp }
2084 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2085 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2086 3f670bfb 2020-12-10 stsp wlen--;
2087 3f670bfb 2020-12-10 stsp }
2088 3f670bfb 2020-12-10 stsp
2089 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2090 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2091 27a741e5 2019-09-11 stsp
2092 b700b5d6 2018-07-10 stsp if (widthp)
2093 b700b5d6 2018-07-10 stsp *widthp = cols;
2094 f91a2b48 2022-06-23 thomas if (scrollxp)
2095 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2096 963b370f 2018-05-20 stsp if (err)
2097 963b370f 2018-05-20 stsp free(wline);
2098 963b370f 2018-05-20 stsp else
2099 963b370f 2018-05-20 stsp *wlinep = wline;
2100 963b370f 2018-05-20 stsp return err;
2101 963b370f 2018-05-20 stsp }
2102 963b370f 2018-05-20 stsp
2103 8b473291 2019-02-21 stsp static const struct got_error*
2104 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2105 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2106 8b473291 2019-02-21 stsp {
2107 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2108 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2109 8b473291 2019-02-21 stsp char *s;
2110 8b473291 2019-02-21 stsp const char *name;
2111 8b473291 2019-02-21 stsp
2112 8b473291 2019-02-21 stsp *refs_str = NULL;
2113 8b473291 2019-02-21 stsp
2114 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2115 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2116 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2117 52b5abe1 2019-08-13 stsp int cmp;
2118 52b5abe1 2019-08-13 stsp
2119 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2120 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2121 8b473291 2019-02-21 stsp continue;
2122 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2123 8b473291 2019-02-21 stsp name += 5;
2124 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2125 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2126 7143d404 2019-03-12 stsp continue;
2127 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2128 8b473291 2019-02-21 stsp name += 6;
2129 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2130 8b473291 2019-02-21 stsp name += 8;
2131 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2132 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2133 79cc719f 2020-04-24 stsp continue;
2134 79cc719f 2020-04-24 stsp }
2135 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2136 48cae60d 2020-09-22 stsp if (err)
2137 48cae60d 2020-09-22 stsp break;
2138 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2139 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2140 5d844a1e 2019-08-13 stsp if (err) {
2141 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2142 48cae60d 2020-09-22 stsp free(ref_id);
2143 5d844a1e 2019-08-13 stsp break;
2144 48cae60d 2020-09-22 stsp }
2145 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2146 5d844a1e 2019-08-13 stsp err = NULL;
2147 5d844a1e 2019-08-13 stsp tag = NULL;
2148 5d844a1e 2019-08-13 stsp }
2149 52b5abe1 2019-08-13 stsp }
2150 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2151 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2152 48cae60d 2020-09-22 stsp free(ref_id);
2153 52b5abe1 2019-08-13 stsp if (tag)
2154 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2155 52b5abe1 2019-08-13 stsp if (cmp != 0)
2156 52b5abe1 2019-08-13 stsp continue;
2157 8b473291 2019-02-21 stsp s = *refs_str;
2158 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2159 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2160 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2161 8b473291 2019-02-21 stsp free(s);
2162 8b473291 2019-02-21 stsp *refs_str = NULL;
2163 8b473291 2019-02-21 stsp break;
2164 8b473291 2019-02-21 stsp }
2165 8b473291 2019-02-21 stsp free(s);
2166 8b473291 2019-02-21 stsp }
2167 8b473291 2019-02-21 stsp
2168 8b473291 2019-02-21 stsp return err;
2169 8b473291 2019-02-21 stsp }
2170 8b473291 2019-02-21 stsp
2171 963b370f 2018-05-20 stsp static const struct got_error *
2172 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2173 27a741e5 2019-09-11 stsp int col_tab_align)
2174 5813d178 2019-03-09 stsp {
2175 e6b8b890 2020-12-29 naddy char *smallerthan;
2176 5813d178 2019-03-09 stsp
2177 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2178 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2179 5813d178 2019-03-09 stsp author = smallerthan + 1;
2180 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2181 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2182 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2183 5813d178 2019-03-09 stsp }
2184 5813d178 2019-03-09 stsp
2185 5813d178 2019-03-09 stsp static const struct got_error *
2186 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2187 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2188 8fdc79fe 2020-12-01 naddy int author_display_cols)
2189 80ddbec8 2018-04-29 stsp {
2190 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2191 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2192 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2193 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2194 5813d178 2019-03-09 stsp char *author = NULL;
2195 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2196 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2197 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2198 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2199 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2200 ccb26ccd 2018-11-05 stsp struct tm tm;
2201 45d799e2 2018-12-23 stsp time_t committer_time;
2202 11b20872 2019-11-08 stsp struct tog_color *tc;
2203 80ddbec8 2018-04-29 stsp
2204 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2205 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2206 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2207 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2208 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2209 b39d25c7 2018-07-10 stsp
2210 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2211 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2212 b39d25c7 2018-07-10 stsp else
2213 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2214 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2215 11b20872 2019-11-08 stsp if (tc)
2216 11b20872 2019-11-08 stsp wattr_on(view->window,
2217 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2218 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2219 11b20872 2019-11-08 stsp if (tc)
2220 11b20872 2019-11-08 stsp wattr_off(view->window,
2221 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2222 27a741e5 2019-09-11 stsp col = limit;
2223 b39d25c7 2018-07-10 stsp if (col > avail)
2224 b39d25c7 2018-07-10 stsp goto done;
2225 6570a66d 2019-11-08 stsp
2226 6570a66d 2019-11-08 stsp if (avail >= 120) {
2227 6570a66d 2019-11-08 stsp char *id_str;
2228 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2229 6570a66d 2019-11-08 stsp if (err)
2230 6570a66d 2019-11-08 stsp goto done;
2231 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2232 11b20872 2019-11-08 stsp if (tc)
2233 11b20872 2019-11-08 stsp wattr_on(view->window,
2234 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2235 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2236 11b20872 2019-11-08 stsp if (tc)
2237 11b20872 2019-11-08 stsp wattr_off(view->window,
2238 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2239 6570a66d 2019-11-08 stsp free(id_str);
2240 6570a66d 2019-11-08 stsp col += 9;
2241 6570a66d 2019-11-08 stsp if (col > avail)
2242 6570a66d 2019-11-08 stsp goto done;
2243 6570a66d 2019-11-08 stsp }
2244 b39d25c7 2018-07-10 stsp
2245 f69c5a46 2022-07-19 thomas if (s->use_committer)
2246 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2247 f69c5a46 2022-07-19 thomas else
2248 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2249 5813d178 2019-03-09 stsp if (author == NULL) {
2250 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2251 80ddbec8 2018-04-29 stsp goto done;
2252 80ddbec8 2018-04-29 stsp }
2253 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2254 bb737323 2018-05-20 stsp if (err)
2255 bb737323 2018-05-20 stsp goto done;
2256 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2257 11b20872 2019-11-08 stsp if (tc)
2258 11b20872 2019-11-08 stsp wattr_on(view->window,
2259 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2260 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2261 bb737323 2018-05-20 stsp col += author_width;
2262 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2263 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2264 bb737323 2018-05-20 stsp col++;
2265 bb737323 2018-05-20 stsp author_width++;
2266 bb737323 2018-05-20 stsp }
2267 f31d6c3b 2022-09-09 thomas if (tc)
2268 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2269 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2270 9c2eaf34 2018-05-20 stsp if (col > avail)
2271 9c2eaf34 2018-05-20 stsp goto done;
2272 80ddbec8 2018-04-29 stsp
2273 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2274 5943eee2 2019-08-13 stsp if (err)
2275 6d9fbc00 2018-04-29 stsp goto done;
2276 bb737323 2018-05-20 stsp logmsg = logmsg0;
2277 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2278 bb737323 2018-05-20 stsp logmsg++;
2279 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2280 bb737323 2018-05-20 stsp if (newline)
2281 bb737323 2018-05-20 stsp *newline = '\0';
2282 f91a2b48 2022-06-23 thomas limit = avail - col;
2283 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2284 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2285 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2286 f91a2b48 2022-06-23 thomas limit, col, 1);
2287 331b1a16 2022-06-23 thomas if (err)
2288 331b1a16 2022-06-23 thomas goto done;
2289 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2290 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2291 27a741e5 2019-09-11 stsp while (col < avail) {
2292 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2293 bb737323 2018-05-20 stsp col++;
2294 881b2d3e 2018-04-30 stsp }
2295 80ddbec8 2018-04-29 stsp done:
2296 80ddbec8 2018-04-29 stsp free(logmsg0);
2297 bb737323 2018-05-20 stsp free(wlogmsg);
2298 5813d178 2019-03-09 stsp free(author);
2299 bb737323 2018-05-20 stsp free(wauthor);
2300 80ddbec8 2018-04-29 stsp free(line);
2301 80ddbec8 2018-04-29 stsp return err;
2302 80ddbec8 2018-04-29 stsp }
2303 26ed57b2 2018-05-19 stsp
2304 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2305 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2306 899d86c2 2018-05-10 stsp struct got_object_id *id)
2307 80ddbec8 2018-04-29 stsp {
2308 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2309 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2310 80ddbec8 2018-04-29 stsp
2311 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2312 80ddbec8 2018-04-29 stsp if (entry == NULL)
2313 899d86c2 2018-05-10 stsp return NULL;
2314 99db9666 2018-05-07 stsp
2315 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2316 d68b9737 2022-09-05 thomas if (dup == NULL) {
2317 d68b9737 2022-09-05 thomas free(entry);
2318 d68b9737 2022-09-05 thomas return NULL;
2319 d68b9737 2022-09-05 thomas }
2320 d68b9737 2022-09-05 thomas
2321 d68b9737 2022-09-05 thomas entry->id = dup;
2322 99db9666 2018-05-07 stsp entry->commit = commit;
2323 899d86c2 2018-05-10 stsp return entry;
2324 99db9666 2018-05-07 stsp }
2325 80ddbec8 2018-04-29 stsp
2326 99db9666 2018-05-07 stsp static void
2327 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2328 99db9666 2018-05-07 stsp {
2329 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2330 99db9666 2018-05-07 stsp
2331 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2332 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2333 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2334 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2335 d68b9737 2022-09-05 thomas free(entry->id);
2336 99db9666 2018-05-07 stsp free(entry);
2337 99db9666 2018-05-07 stsp }
2338 99db9666 2018-05-07 stsp
2339 99db9666 2018-05-07 stsp static void
2340 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2341 99db9666 2018-05-07 stsp {
2342 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2343 99db9666 2018-05-07 stsp pop_commit(commits);
2344 c4972b91 2018-05-07 stsp }
2345 c4972b91 2018-05-07 stsp
2346 c4972b91 2018-05-07 stsp static const struct got_error *
2347 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2348 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2349 13add988 2019-10-15 stsp {
2350 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2351 13add988 2019-10-15 stsp regmatch_t regmatch;
2352 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2353 13add988 2019-10-15 stsp
2354 13add988 2019-10-15 stsp *have_match = 0;
2355 13add988 2019-10-15 stsp
2356 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2357 13add988 2019-10-15 stsp if (err)
2358 13add988 2019-10-15 stsp return err;
2359 13add988 2019-10-15 stsp
2360 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2361 13add988 2019-10-15 stsp if (err)
2362 13add988 2019-10-15 stsp goto done;
2363 13add988 2019-10-15 stsp
2364 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2365 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2366 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2367 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2368 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2369 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2370 13add988 2019-10-15 stsp *have_match = 1;
2371 13add988 2019-10-15 stsp done:
2372 13add988 2019-10-15 stsp free(id_str);
2373 13add988 2019-10-15 stsp free(logmsg);
2374 13add988 2019-10-15 stsp return err;
2375 13add988 2019-10-15 stsp }
2376 13add988 2019-10-15 stsp
2377 13add988 2019-10-15 stsp static const struct got_error *
2378 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2379 c4972b91 2018-05-07 stsp {
2380 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2381 9ba79e04 2018-06-11 stsp
2382 1a76625f 2018-10-22 stsp /*
2383 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2384 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2385 1a76625f 2018-10-22 stsp * while updating the display.
2386 1a76625f 2018-10-22 stsp */
2387 4e0d2870 2020-12-07 naddy do {
2388 7210b715 2022-09-11 thomas struct got_object_id id;
2389 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2390 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2391 7e8004ba 2022-09-11 thomas int limit_match = 0;
2392 1a76625f 2018-10-22 stsp int errcode;
2393 899d86c2 2018-05-10 stsp
2394 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2395 4e0d2870 2020-12-07 naddy NULL, NULL);
2396 7210b715 2022-09-11 thomas if (err)
2397 ecb28ae0 2018-07-16 stsp break;
2398 899d86c2 2018-05-10 stsp
2399 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2400 9ba79e04 2018-06-11 stsp if (err)
2401 9ba79e04 2018-06-11 stsp break;
2402 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2403 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2404 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2405 9ba79e04 2018-06-11 stsp break;
2406 9ba79e04 2018-06-11 stsp }
2407 93e45b7c 2018-09-24 stsp
2408 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2409 1a76625f 2018-10-22 stsp if (errcode) {
2410 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2411 13add988 2019-10-15 stsp "pthread_mutex_lock");
2412 1a76625f 2018-10-22 stsp break;
2413 1a76625f 2018-10-22 stsp }
2414 1a76625f 2018-10-22 stsp
2415 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2416 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2417 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2418 1a76625f 2018-10-22 stsp
2419 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2420 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2421 7e8004ba 2022-09-11 thomas a->limit_regex);
2422 7e8004ba 2022-09-11 thomas if (err)
2423 7e8004ba 2022-09-11 thomas break;
2424 7e8004ba 2022-09-11 thomas
2425 7e8004ba 2022-09-11 thomas if (limit_match) {
2426 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2427 7e8004ba 2022-09-11 thomas
2428 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2429 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2430 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2431 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2432 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2433 7e8004ba 2022-09-11 thomas break;
2434 7e8004ba 2022-09-11 thomas }
2435 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2436 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2437 7e8004ba 2022-09-11 thomas
2438 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2439 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2440 7e8004ba 2022-09-11 thomas matched, entry);
2441 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2442 7e8004ba 2022-09-11 thomas }
2443 7e8004ba 2022-09-11 thomas
2444 7e8004ba 2022-09-11 thomas /*
2445 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2446 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2447 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2448 7e8004ba 2022-09-11 thomas */
2449 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2450 7e8004ba 2022-09-11 thomas }
2451 7e8004ba 2022-09-11 thomas
2452 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2453 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2454 7c1452c1 2020-03-26 stsp int have_match;
2455 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2456 7c1452c1 2020-03-26 stsp if (err)
2457 7c1452c1 2020-03-26 stsp break;
2458 7e8004ba 2022-09-11 thomas
2459 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2460 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2461 7e8004ba 2022-09-11 thomas *a->search_next_done =
2462 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2463 7e8004ba 2022-09-11 thomas } else if (have_match)
2464 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2465 13add988 2019-10-15 stsp }
2466 13add988 2019-10-15 stsp
2467 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2468 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2469 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2470 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2471 7c1452c1 2020-03-26 stsp if (err)
2472 13add988 2019-10-15 stsp break;
2473 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2474 899d86c2 2018-05-10 stsp
2475 9ba79e04 2018-06-11 stsp return err;
2476 0553a4e3 2018-04-30 stsp }
2477 0553a4e3 2018-04-30 stsp
2478 2b779855 2020-12-05 naddy static void
2479 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2480 2b779855 2020-12-05 naddy {
2481 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2482 2b779855 2020-12-05 naddy int ncommits = 0;
2483 2b779855 2020-12-05 naddy
2484 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2485 2b779855 2020-12-05 naddy while (entry) {
2486 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2487 2b779855 2020-12-05 naddy s->selected_entry = entry;
2488 2b779855 2020-12-05 naddy break;
2489 2b779855 2020-12-05 naddy }
2490 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2491 2b779855 2020-12-05 naddy ncommits++;
2492 2b779855 2020-12-05 naddy }
2493 2b779855 2020-12-05 naddy }
2494 2b779855 2020-12-05 naddy
2495 0553a4e3 2018-04-30 stsp static const struct got_error *
2496 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2497 0553a4e3 2018-04-30 stsp {
2498 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2499 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2500 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2501 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2502 60493ae3 2019-06-20 stsp int width;
2503 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2504 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2505 8b473291 2019-02-21 stsp char *refs_str = NULL;
2506 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2507 11b20872 2019-11-08 stsp struct tog_color *tc;
2508 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2509 bd3f8225 2022-08-12 thomas
2510 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2511 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2512 0553a4e3 2018-04-30 stsp
2513 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2514 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2515 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2516 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2517 1a76625f 2018-10-22 stsp if (err)
2518 ecb28ae0 2018-07-16 stsp return err;
2519 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2520 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2521 d2075bf3 2020-12-25 stsp if (refs) {
2522 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2523 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2524 d2075bf3 2020-12-25 stsp if (err)
2525 d2075bf3 2020-12-25 stsp goto done;
2526 d2075bf3 2020-12-25 stsp }
2527 867c6645 2018-07-10 stsp }
2528 359bfafd 2019-02-22 stsp
2529 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2530 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2531 1a76625f 2018-10-22 stsp
2532 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2533 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2534 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2535 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2536 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2537 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2538 8f4ed634 2020-03-26 stsp goto done;
2539 8f4ed634 2020-03-26 stsp }
2540 8f4ed634 2020-03-26 stsp } else {
2541 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2542 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2543 f9686aa5 2020-03-27 stsp
2544 f9686aa5 2020-03-27 stsp if (view->searching) {
2545 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2546 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2547 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2548 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2549 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2550 f9686aa5 2020-03-27 stsp search_str = "searching...";
2551 f9686aa5 2020-03-27 stsp }
2552 f9686aa5 2020-03-27 stsp
2553 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2554 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2555 7e8004ba 2022-09-11 thomas
2556 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2557 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2558 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2559 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2560 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2561 8f4ed634 2020-03-26 stsp goto done;
2562 8f4ed634 2020-03-26 stsp }
2563 8b473291 2019-02-21 stsp }
2564 1a76625f 2018-10-22 stsp
2565 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2566 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2567 91198554 2022-06-23 thomas "........................................",
2568 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2569 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2570 1a76625f 2018-10-22 stsp header = NULL;
2571 1a76625f 2018-10-22 stsp goto done;
2572 1a76625f 2018-10-22 stsp }
2573 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2574 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2575 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2576 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2577 1a76625f 2018-10-22 stsp header = NULL;
2578 1a76625f 2018-10-22 stsp goto done;
2579 ecb28ae0 2018-07-16 stsp }
2580 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2581 1a76625f 2018-10-22 stsp if (err)
2582 1a76625f 2018-10-22 stsp goto done;
2583 867c6645 2018-07-10 stsp
2584 2814baeb 2018-08-01 stsp werase(view->window);
2585 867c6645 2018-07-10 stsp
2586 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2587 a3404814 2018-09-02 stsp wstandout(view->window);
2588 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2589 11b20872 2019-11-08 stsp if (tc)
2590 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2591 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2592 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2593 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2594 1a76625f 2018-10-22 stsp width++;
2595 1a76625f 2018-10-22 stsp }
2596 86f4aab9 2022-09-09 thomas if (tc)
2597 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2598 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2599 a3404814 2018-09-02 stsp wstandend(view->window);
2600 ecb28ae0 2018-07-16 stsp free(wline);
2601 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2602 1a76625f 2018-10-22 stsp goto done;
2603 0553a4e3 2018-04-30 stsp
2604 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2605 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2606 5813d178 2019-03-09 stsp ncommits = 0;
2607 05171be4 2022-06-23 thomas view->maxx = 0;
2608 5813d178 2019-03-09 stsp while (entry) {
2609 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2610 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2611 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2612 5813d178 2019-03-09 stsp int width;
2613 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2614 5813d178 2019-03-09 stsp break;
2615 f69c5a46 2022-07-19 thomas if (s->use_committer)
2616 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2617 f69c5a46 2022-07-19 thomas else
2618 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2619 5813d178 2019-03-09 stsp if (author == NULL) {
2620 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2621 5813d178 2019-03-09 stsp goto done;
2622 5813d178 2019-03-09 stsp }
2623 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2624 27a741e5 2019-09-11 stsp date_display_cols);
2625 5813d178 2019-03-09 stsp if (author_cols < width)
2626 5813d178 2019-03-09 stsp author_cols = width;
2627 5813d178 2019-03-09 stsp free(wauthor);
2628 5813d178 2019-03-09 stsp free(author);
2629 ef944b8b 2022-07-21 thomas if (err)
2630 ef944b8b 2022-07-21 thomas goto done;
2631 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2632 05171be4 2022-06-23 thomas if (err)
2633 05171be4 2022-06-23 thomas goto done;
2634 05171be4 2022-06-23 thomas msg = msg0;
2635 05171be4 2022-06-23 thomas while (*msg == '\n')
2636 05171be4 2022-06-23 thomas ++msg;
2637 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2638 331b1a16 2022-06-23 thomas *eol = '\0';
2639 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2640 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2641 331b1a16 2022-06-23 thomas if (err)
2642 331b1a16 2022-06-23 thomas goto done;
2643 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2644 05171be4 2022-06-23 thomas free(msg0);
2645 331b1a16 2022-06-23 thomas free(wmsg);
2646 7ca04879 2019-10-19 stsp ncommits++;
2647 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2648 5813d178 2019-03-09 stsp }
2649 5813d178 2019-03-09 stsp
2650 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2651 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2652 867c6645 2018-07-10 stsp ncommits = 0;
2653 899d86c2 2018-05-10 stsp while (entry) {
2654 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2655 899d86c2 2018-05-10 stsp break;
2656 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2657 2814baeb 2018-08-01 stsp wstandout(view->window);
2658 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2659 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2660 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2661 2814baeb 2018-08-01 stsp wstandend(view->window);
2662 0553a4e3 2018-04-30 stsp if (err)
2663 60493ae3 2019-06-20 stsp goto done;
2664 0553a4e3 2018-04-30 stsp ncommits++;
2665 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2666 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2667 80ddbec8 2018-04-29 stsp }
2668 80ddbec8 2018-04-29 stsp
2669 a5d43cac 2022-07-01 thomas view_border(view);
2670 1a76625f 2018-10-22 stsp done:
2671 1a76625f 2018-10-22 stsp free(id_str);
2672 8b473291 2019-02-21 stsp free(refs_str);
2673 1a76625f 2018-10-22 stsp free(ncommits_str);
2674 1a76625f 2018-10-22 stsp free(header);
2675 80ddbec8 2018-04-29 stsp return err;
2676 9f7d7167 2018-04-29 stsp }
2677 07b55e75 2018-05-10 stsp
2678 07b55e75 2018-05-10 stsp static void
2679 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2680 07b55e75 2018-05-10 stsp {
2681 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2682 07b55e75 2018-05-10 stsp int nscrolled = 0;
2683 07b55e75 2018-05-10 stsp
2684 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2685 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2686 07b55e75 2018-05-10 stsp return;
2687 9f7d7167 2018-04-29 stsp
2688 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2689 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2690 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2691 07b55e75 2018-05-10 stsp if (entry) {
2692 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2693 07b55e75 2018-05-10 stsp nscrolled++;
2694 07b55e75 2018-05-10 stsp }
2695 07b55e75 2018-05-10 stsp }
2696 aa075928 2018-05-10 stsp }
2697 aa075928 2018-05-10 stsp
2698 aa075928 2018-05-10 stsp static const struct got_error *
2699 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2700 aa075928 2018-05-10 stsp {
2701 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2702 5e224a3e 2019-02-22 stsp int errcode;
2703 8a42fca8 2019-02-22 stsp
2704 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2705 aa075928 2018-05-10 stsp
2706 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2707 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2708 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2709 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2710 7aafa0d1 2019-02-22 stsp if (errcode)
2711 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2712 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2713 7c1452c1 2020-03-26 stsp
2714 7c1452c1 2020-03-26 stsp /*
2715 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2716 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2717 7c1452c1 2020-03-26 stsp */
2718 7c1452c1 2020-03-26 stsp if (!wait)
2719 7c1452c1 2020-03-26 stsp break;
2720 7c1452c1 2020-03-26 stsp
2721 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2722 ffe38506 2020-12-01 naddy show_log_view(view);
2723 7c1452c1 2020-03-26 stsp update_panels();
2724 7c1452c1 2020-03-26 stsp doupdate();
2725 7c1452c1 2020-03-26 stsp
2726 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2727 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2728 82954512 2020-02-03 stsp if (errcode)
2729 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2730 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2731 82954512 2020-02-03 stsp
2732 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2733 ffe38506 2020-12-01 naddy show_log_view(view);
2734 7c1452c1 2020-03-26 stsp update_panels();
2735 7c1452c1 2020-03-26 stsp doupdate();
2736 5e224a3e 2019-02-22 stsp }
2737 5e224a3e 2019-02-22 stsp
2738 5e224a3e 2019-02-22 stsp return NULL;
2739 a5d43cac 2022-07-01 thomas }
2740 a5d43cac 2022-07-01 thomas
2741 a5d43cac 2022-07-01 thomas static const struct got_error *
2742 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2743 a5d43cac 2022-07-01 thomas {
2744 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2745 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2746 fe731b51 2022-07-14 thomas
2747 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2748 fe731b51 2022-07-14 thomas return NULL;
2749 a5d43cac 2022-07-01 thomas
2750 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2751 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2752 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2753 a5d43cac 2022-07-01 thomas
2754 a5d43cac 2022-07-01 thomas return err;
2755 5e224a3e 2019-02-22 stsp }
2756 5e224a3e 2019-02-22 stsp
2757 5e224a3e 2019-02-22 stsp static const struct got_error *
2758 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2759 5e224a3e 2019-02-22 stsp {
2760 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2761 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2762 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2763 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2764 5e224a3e 2019-02-22 stsp
2765 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2766 5e224a3e 2019-02-22 stsp return NULL;
2767 5e224a3e 2019-02-22 stsp
2768 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2769 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2770 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2771 08ebd0a9 2019-02-22 stsp /*
2772 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2773 08ebd0a9 2019-02-22 stsp */
2774 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2775 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2776 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2777 5e224a3e 2019-02-22 stsp if (err)
2778 5e224a3e 2019-02-22 stsp return err;
2779 7aafa0d1 2019-02-22 stsp }
2780 b295e71b 2019-02-22 stsp
2781 7aafa0d1 2019-02-22 stsp do {
2782 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2783 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2784 88048b54 2019-02-21 stsp break;
2785 88048b54 2019-02-21 stsp
2786 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2787 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
2788 aa075928 2018-05-10 stsp
2789 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2790 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2791 dd0a52c1 2018-05-20 stsp break;
2792 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2793 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2794 aa075928 2018-05-10 stsp
2795 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2796 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2797 a5d43cac 2022-07-01 thomas else
2798 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2799 a5d43cac 2022-07-01 thomas
2800 dd0a52c1 2018-05-20 stsp return err;
2801 07b55e75 2018-05-10 stsp }
2802 4a7f7875 2018-05-10 stsp
2803 cd0acaa7 2018-05-20 stsp static const struct got_error *
2804 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2805 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2806 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2807 cd0acaa7 2018-05-20 stsp {
2808 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2809 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2810 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2811 cd0acaa7 2018-05-20 stsp
2812 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2813 15a94983 2018-12-23 stsp if (diff_view == NULL)
2814 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2815 ea5e7bb5 2018-08-01 stsp
2816 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2817 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2818 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2819 e5a0f69f 2018-08-18 stsp if (err == NULL)
2820 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2821 cd0acaa7 2018-05-20 stsp return err;
2822 4a7f7875 2018-05-10 stsp }
2823 4a7f7875 2018-05-10 stsp
2824 80ddbec8 2018-04-29 stsp static const struct got_error *
2825 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2826 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2827 9343a5fb 2018-06-23 stsp {
2828 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2829 941e9f74 2019-05-21 stsp
2830 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2831 941e9f74 2019-05-21 stsp if (parent == NULL)
2832 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2833 941e9f74 2019-05-21 stsp
2834 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2835 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2836 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2837 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2838 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2839 941e9f74 2019-05-21 stsp s->tree = subtree;
2840 941e9f74 2019-05-21 stsp s->selected = 0;
2841 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2842 941e9f74 2019-05-21 stsp return NULL;
2843 941e9f74 2019-05-21 stsp }
2844 941e9f74 2019-05-21 stsp
2845 941e9f74 2019-05-21 stsp static const struct got_error *
2846 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2847 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2848 941e9f74 2019-05-21 stsp {
2849 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2850 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2851 941e9f74 2019-05-21 stsp const char *p;
2852 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2853 9343a5fb 2018-06-23 stsp
2854 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2855 941e9f74 2019-05-21 stsp p = path;
2856 941e9f74 2019-05-21 stsp while (*p) {
2857 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2858 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2859 56e0773d 2019-11-28 stsp char *te_name;
2860 33cbf02b 2020-01-12 stsp
2861 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2862 33cbf02b 2020-01-12 stsp p++;
2863 941e9f74 2019-05-21 stsp
2864 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2865 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2866 941e9f74 2019-05-21 stsp if (slash == NULL)
2867 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2868 33cbf02b 2020-01-12 stsp else
2869 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2870 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2871 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2872 56e0773d 2019-11-28 stsp break;
2873 941e9f74 2019-05-21 stsp }
2874 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2875 56e0773d 2019-11-28 stsp if (te == NULL) {
2876 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2877 56e0773d 2019-11-28 stsp free(te_name);
2878 941e9f74 2019-05-21 stsp break;
2879 941e9f74 2019-05-21 stsp }
2880 56e0773d 2019-11-28 stsp free(te_name);
2881 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2882 941e9f74 2019-05-21 stsp
2883 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2884 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2885 b03c880f 2019-05-21 stsp
2886 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2887 941e9f74 2019-05-21 stsp if (slash)
2888 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2889 941e9f74 2019-05-21 stsp else
2890 941e9f74 2019-05-21 stsp subpath = strdup(path);
2891 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2892 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2893 941e9f74 2019-05-21 stsp break;
2894 941e9f74 2019-05-21 stsp }
2895 941e9f74 2019-05-21 stsp
2896 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2897 941e9f74 2019-05-21 stsp subpath);
2898 941e9f74 2019-05-21 stsp if (err)
2899 941e9f74 2019-05-21 stsp break;
2900 941e9f74 2019-05-21 stsp
2901 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2902 941e9f74 2019-05-21 stsp free(tree_id);
2903 941e9f74 2019-05-21 stsp if (err)
2904 941e9f74 2019-05-21 stsp break;
2905 941e9f74 2019-05-21 stsp
2906 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2907 941e9f74 2019-05-21 stsp if (err) {
2908 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2909 941e9f74 2019-05-21 stsp break;
2910 941e9f74 2019-05-21 stsp }
2911 941e9f74 2019-05-21 stsp if (slash == NULL)
2912 941e9f74 2019-05-21 stsp break;
2913 941e9f74 2019-05-21 stsp free(subpath);
2914 941e9f74 2019-05-21 stsp subpath = NULL;
2915 941e9f74 2019-05-21 stsp p = slash;
2916 941e9f74 2019-05-21 stsp }
2917 941e9f74 2019-05-21 stsp
2918 941e9f74 2019-05-21 stsp free(subpath);
2919 1a76625f 2018-10-22 stsp return err;
2920 61266923 2020-01-14 stsp }
2921 61266923 2020-01-14 stsp
2922 61266923 2020-01-14 stsp static const struct got_error *
2923 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2924 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2925 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2926 55cccc34 2020-02-20 stsp {
2927 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2928 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2929 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2930 55cccc34 2020-02-20 stsp
2931 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2932 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2933 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2934 55cccc34 2020-02-20 stsp
2935 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2936 bc573f3b 2021-07-10 stsp if (err)
2937 55cccc34 2020-02-20 stsp return err;
2938 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2939 55cccc34 2020-02-20 stsp
2940 55cccc34 2020-02-20 stsp *new_view = tree_view;
2941 55cccc34 2020-02-20 stsp
2942 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2943 55cccc34 2020-02-20 stsp return NULL;
2944 55cccc34 2020-02-20 stsp
2945 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2946 55cccc34 2020-02-20 stsp }
2947 55cccc34 2020-02-20 stsp
2948 55cccc34 2020-02-20 stsp static const struct got_error *
2949 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2950 61266923 2020-01-14 stsp {
2951 61266923 2020-01-14 stsp sigset_t sigset;
2952 61266923 2020-01-14 stsp int errcode;
2953 61266923 2020-01-14 stsp
2954 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2955 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2956 61266923 2020-01-14 stsp
2957 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2958 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2959 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2960 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2961 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2962 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2963 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2964 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2965 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2966 61266923 2020-01-14 stsp
2967 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2968 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2969 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2970 61266923 2020-01-14 stsp
2971 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2972 61266923 2020-01-14 stsp if (errcode)
2973 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2974 61266923 2020-01-14 stsp
2975 61266923 2020-01-14 stsp return NULL;
2976 1a76625f 2018-10-22 stsp }
2977 1a76625f 2018-10-22 stsp
2978 1a76625f 2018-10-22 stsp static void *
2979 1a76625f 2018-10-22 stsp log_thread(void *arg)
2980 1a76625f 2018-10-22 stsp {
2981 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2982 1a76625f 2018-10-22 stsp int errcode = 0;
2983 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2984 1a76625f 2018-10-22 stsp int done = 0;
2985 61266923 2020-01-14 stsp
2986 f2d749db 2022-07-12 thomas /*
2987 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2988 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2989 f2d749db 2022-07-12 thomas */
2990 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2991 f2d749db 2022-07-12 thomas if (errcode) {
2992 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2993 61266923 2020-01-14 stsp return (void *)err;
2994 f2d749db 2022-07-12 thomas }
2995 1a76625f 2018-10-22 stsp
2996 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2997 f2d749db 2022-07-12 thomas if (err) {
2998 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2999 f2d749db 2022-07-12 thomas goto done;
3000 f2d749db 2022-07-12 thomas }
3001 f2d749db 2022-07-12 thomas
3002 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3003 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3004 f2d749db 2022-07-12 thomas if (errcode) {
3005 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3006 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3007 f2d749db 2022-07-12 thomas goto done;
3008 f2d749db 2022-07-12 thomas }
3009 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3010 1a76625f 2018-10-22 stsp if (err) {
3011 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3012 f2d749db 2022-07-12 thomas goto done;
3013 1a76625f 2018-10-22 stsp err = NULL;
3014 1a76625f 2018-10-22 stsp done = 1;
3015 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3016 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3017 7e8004ba 2022-09-11 thomas if (a->limit_match)
3018 7e8004ba 2022-09-11 thomas a->commits_needed--;
3019 7e8004ba 2022-09-11 thomas } else
3020 7e8004ba 2022-09-11 thomas a->commits_needed--;
3021 7e8004ba 2022-09-11 thomas }
3022 1a76625f 2018-10-22 stsp
3023 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3024 3abe8080 2019-04-10 stsp if (errcode) {
3025 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3026 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3027 f2d749db 2022-07-12 thomas goto done;
3028 3abe8080 2019-04-10 stsp } else if (*a->quit)
3029 1a76625f 2018-10-22 stsp done = 1;
3030 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3031 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3032 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3033 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3034 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3035 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3036 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3037 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3038 1a76625f 2018-10-22 stsp }
3039 1a76625f 2018-10-22 stsp
3040 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3041 7c1452c1 2020-03-26 stsp if (errcode) {
3042 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3043 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3044 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3045 f2d749db 2022-07-12 thomas goto done;
3046 7c1452c1 2020-03-26 stsp }
3047 7c1452c1 2020-03-26 stsp
3048 1a76625f 2018-10-22 stsp if (done)
3049 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3050 7c1452c1 2020-03-26 stsp else {
3051 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3052 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3053 7c1452c1 2020-03-26 stsp &tog_mutex);
3054 f2d749db 2022-07-12 thomas if (errcode) {
3055 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3056 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3057 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3058 f2d749db 2022-07-12 thomas goto done;
3059 f2d749db 2022-07-12 thomas }
3060 21355643 2020-12-06 stsp if (*a->quit)
3061 21355643 2020-12-06 stsp done = 1;
3062 7c1452c1 2020-03-26 stsp }
3063 1a76625f 2018-10-22 stsp }
3064 1a76625f 2018-10-22 stsp }
3065 3abe8080 2019-04-10 stsp a->log_complete = 1;
3066 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3067 f2d749db 2022-07-12 thomas if (errcode)
3068 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3069 f2d749db 2022-07-12 thomas done:
3070 f2d749db 2022-07-12 thomas if (err) {
3071 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3072 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3073 f2d749db 2022-07-12 thomas }
3074 1a76625f 2018-10-22 stsp return (void *)err;
3075 1a76625f 2018-10-22 stsp }
3076 1a76625f 2018-10-22 stsp
3077 1a76625f 2018-10-22 stsp static const struct got_error *
3078 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3079 1a76625f 2018-10-22 stsp {
3080 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3081 1a76625f 2018-10-22 stsp int errcode;
3082 1a76625f 2018-10-22 stsp
3083 1a76625f 2018-10-22 stsp if (s->thread) {
3084 1a76625f 2018-10-22 stsp s->quit = 1;
3085 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3086 1a76625f 2018-10-22 stsp if (errcode)
3087 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3088 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3090 1a76625f 2018-10-22 stsp if (errcode)
3091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3092 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3093 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3094 1a76625f 2018-10-22 stsp if (errcode)
3095 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3096 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3097 1a76625f 2018-10-22 stsp if (errcode)
3098 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3099 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3100 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3101 1a76625f 2018-10-22 stsp }
3102 1a76625f 2018-10-22 stsp
3103 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3104 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3105 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3106 1af5eddf 2022-06-23 thomas }
3107 1af5eddf 2022-06-23 thomas
3108 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3109 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3110 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3111 1af5eddf 2022-06-23 thomas if (err == NULL)
3112 1af5eddf 2022-06-23 thomas err = pack_err;
3113 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3114 1a76625f 2018-10-22 stsp }
3115 1a76625f 2018-10-22 stsp
3116 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3117 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3118 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3119 1a76625f 2018-10-22 stsp }
3120 1a76625f 2018-10-22 stsp
3121 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3122 9343a5fb 2018-06-23 stsp }
3123 9343a5fb 2018-06-23 stsp
3124 9343a5fb 2018-06-23 stsp static const struct got_error *
3125 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3126 1a76625f 2018-10-22 stsp {
3127 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3128 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3129 276b94a1 2020-11-13 naddy int errcode;
3130 1a76625f 2018-10-22 stsp
3131 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3132 276b94a1 2020-11-13 naddy
3133 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3134 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3135 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3136 276b94a1 2020-11-13 naddy
3137 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3138 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3139 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3140 276b94a1 2020-11-13 naddy
3141 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3142 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3143 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3144 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3145 1a76625f 2018-10-22 stsp free(s->start_id);
3146 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3147 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3148 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3149 1a76625f 2018-10-22 stsp return err;
3150 1a76625f 2018-10-22 stsp }
3151 1a76625f 2018-10-22 stsp
3152 7e8004ba 2022-09-11 thomas /*
3153 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3154 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3155 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3156 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3157 7e8004ba 2022-09-11 thomas * works with very slight change.
3158 7e8004ba 2022-09-11 thomas */
3159 1a76625f 2018-10-22 stsp static const struct got_error *
3160 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3161 7e8004ba 2022-09-11 thomas {
3162 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3163 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3164 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3165 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3166 7e8004ba 2022-09-11 thomas char pattern[1024];
3167 7e8004ba 2022-09-11 thomas int ret;
3168 7e8004ba 2022-09-11 thomas
3169 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3170 7e8004ba 2022-09-11 thomas v = view->child;
3171 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3172 7e8004ba 2022-09-11 thomas v = view->parent;
3173 7e8004ba 2022-09-11 thomas
3174 7e8004ba 2022-09-11 thomas /* Get the pattern */
3175 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3176 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3177 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3178 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3179 7e8004ba 2022-09-11 thomas nocbreak();
3180 7e8004ba 2022-09-11 thomas echo();
3181 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3182 7e8004ba 2022-09-11 thomas cbreak();
3183 7e8004ba 2022-09-11 thomas noecho();
3184 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3185 7e8004ba 2022-09-11 thomas if (ret == ERR)
3186 7e8004ba 2022-09-11 thomas return NULL;
3187 7e8004ba 2022-09-11 thomas
3188 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3189 7e8004ba 2022-09-11 thomas /*
3190 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3191 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3192 7e8004ba 2022-09-11 thomas */
3193 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3194 7e8004ba 2022-09-11 thomas return NULL;
3195 7e8004ba 2022-09-11 thomas
3196 7e8004ba 2022-09-11 thomas /*
3197 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3198 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3199 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3200 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3201 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3202 7e8004ba 2022-09-11 thomas * the queue.
3203 7e8004ba 2022-09-11 thomas */
3204 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3205 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3206 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3207 7e8004ba 2022-09-11 thomas s->selected = 0;
3208 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3209 7e8004ba 2022-09-11 thomas
3210 7e8004ba 2022-09-11 thomas return NULL;
3211 7e8004ba 2022-09-11 thomas }
3212 7e8004ba 2022-09-11 thomas
3213 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3214 7e8004ba 2022-09-11 thomas return NULL;
3215 7e8004ba 2022-09-11 thomas
3216 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3217 7e8004ba 2022-09-11 thomas
3218 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3219 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3220 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3221 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3222 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3223 7e8004ba 2022-09-11 thomas
3224 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3225 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3226 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3227 7e8004ba 2022-09-11 thomas
3228 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3229 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3230 7e8004ba 2022-09-11 thomas int have_match = 0;
3231 7e8004ba 2022-09-11 thomas
3232 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3233 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3234 7e8004ba 2022-09-11 thomas if (err)
3235 7e8004ba 2022-09-11 thomas return err;
3236 7e8004ba 2022-09-11 thomas
3237 7e8004ba 2022-09-11 thomas if (have_match) {
3238 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3239 7e8004ba 2022-09-11 thomas
3240 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3241 7e8004ba 2022-09-11 thomas entry->id);
3242 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3243 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3244 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3245 7e8004ba 2022-09-11 thomas break;
3246 7e8004ba 2022-09-11 thomas }
3247 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3248 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3249 7e8004ba 2022-09-11 thomas
3250 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3251 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3252 7e8004ba 2022-09-11 thomas matched, entry);
3253 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3254 7e8004ba 2022-09-11 thomas }
3255 7e8004ba 2022-09-11 thomas }
3256 7e8004ba 2022-09-11 thomas
3257 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3258 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3259 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3260 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3261 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3262 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3263 7e8004ba 2022-09-11 thomas if (err)
3264 7e8004ba 2022-09-11 thomas return err;
3265 7e8004ba 2022-09-11 thomas }
3266 7e8004ba 2022-09-11 thomas
3267 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3268 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3269 7e8004ba 2022-09-11 thomas s->selected = 0;
3270 7e8004ba 2022-09-11 thomas
3271 7e8004ba 2022-09-11 thomas return NULL;
3272 7e8004ba 2022-09-11 thomas }
3273 7e8004ba 2022-09-11 thomas
3274 7e8004ba 2022-09-11 thomas static const struct got_error *
3275 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3276 60493ae3 2019-06-20 stsp {
3277 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3278 60493ae3 2019-06-20 stsp
3279 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3280 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3281 60493ae3 2019-06-20 stsp return NULL;
3282 60493ae3 2019-06-20 stsp }
3283 60493ae3 2019-06-20 stsp
3284 60493ae3 2019-06-20 stsp static const struct got_error *
3285 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3286 60493ae3 2019-06-20 stsp {
3287 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3288 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3289 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3290 60493ae3 2019-06-20 stsp
3291 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3292 f9686aa5 2020-03-27 stsp show_log_view(view);
3293 f9686aa5 2020-03-27 stsp update_panels();
3294 f9686aa5 2020-03-27 stsp doupdate();
3295 f9686aa5 2020-03-27 stsp
3296 96e2b566 2019-07-08 stsp if (s->search_entry) {
3297 13add988 2019-10-15 stsp int errcode, ch;
3298 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3299 13add988 2019-10-15 stsp if (errcode)
3300 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3301 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3302 13add988 2019-10-15 stsp ch = wgetch(view->window);
3303 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3304 13add988 2019-10-15 stsp if (errcode)
3305 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3306 13add988 2019-10-15 stsp "pthread_mutex_lock");
3307 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3308 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3309 678cbce5 2019-07-28 stsp return NULL;
3310 678cbce5 2019-07-28 stsp }
3311 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3312 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3313 96e2b566 2019-07-08 stsp else
3314 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3315 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3316 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3317 df5e841d 2022-06-23 thomas /*
3318 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3319 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3320 1f4d5162 2022-06-23 thomas * might have changed.
3321 df5e841d 2022-06-23 thomas */
3322 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3323 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3324 e7baaec8 2022-09-13 thomas else
3325 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3326 e7baaec8 2022-09-13 thomas entry);
3327 20be8d96 2019-06-21 stsp } else {
3328 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3329 20be8d96 2019-06-21 stsp }
3330 60493ae3 2019-06-20 stsp
3331 60493ae3 2019-06-20 stsp while (1) {
3332 13add988 2019-10-15 stsp int have_match = 0;
3333 13add988 2019-10-15 stsp
3334 60493ae3 2019-06-20 stsp if (entry == NULL) {
3335 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3336 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3337 f9967bca 2020-03-27 stsp view->search_next_done =
3338 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3339 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3340 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3341 f801134a 2019-06-25 stsp return NULL;
3342 60493ae3 2019-06-20 stsp }
3343 96e2b566 2019-07-08 stsp /*
3344 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3345 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3346 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3347 96e2b566 2019-07-08 stsp */
3348 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3349 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3350 60493ae3 2019-06-20 stsp }
3351 60493ae3 2019-06-20 stsp
3352 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3353 13add988 2019-10-15 stsp &view->regex);
3354 5943eee2 2019-08-13 stsp if (err)
3355 13add988 2019-10-15 stsp break;
3356 13add988 2019-10-15 stsp if (have_match) {
3357 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3358 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3359 60493ae3 2019-06-20 stsp break;
3360 60493ae3 2019-06-20 stsp }
3361 13add988 2019-10-15 stsp
3362 96e2b566 2019-07-08 stsp s->search_entry = entry;
3363 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3364 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3365 b1bf1435 2019-06-21 stsp else
3366 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3367 60493ae3 2019-06-20 stsp }
3368 60493ae3 2019-06-20 stsp
3369 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3370 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3371 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3372 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3373 60493ae3 2019-06-20 stsp if (err)
3374 60493ae3 2019-06-20 stsp return err;
3375 ead14cbe 2019-06-21 stsp cur++;
3376 ead14cbe 2019-06-21 stsp }
3377 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3378 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3379 60493ae3 2019-06-20 stsp if (err)
3380 60493ae3 2019-06-20 stsp return err;
3381 ead14cbe 2019-06-21 stsp cur--;
3382 60493ae3 2019-06-20 stsp }
3383 60493ae3 2019-06-20 stsp }
3384 60493ae3 2019-06-20 stsp
3385 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3386 96e2b566 2019-07-08 stsp
3387 60493ae3 2019-06-20 stsp return NULL;
3388 60493ae3 2019-06-20 stsp }
3389 60493ae3 2019-06-20 stsp
3390 60493ae3 2019-06-20 stsp static const struct got_error *
3391 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3392 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3393 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3394 80ddbec8 2018-04-29 stsp {
3395 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3396 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3397 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3398 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3399 1a76625f 2018-10-22 stsp int errcode;
3400 80ddbec8 2018-04-29 stsp
3401 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3402 f135c941 2020-02-20 stsp free(s->in_repo_path);
3403 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3404 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3405 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3406 f135c941 2020-02-20 stsp }
3407 ecb28ae0 2018-07-16 stsp
3408 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3409 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3410 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3411 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3412 78756c87 2020-11-24 stsp
3413 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3414 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3415 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3416 7e8004ba 2022-09-11 thomas
3417 fb2756b9 2018-08-04 stsp s->repo = repo;
3418 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3419 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3420 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3421 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3422 9cd7cbd1 2020-12-07 stsp goto done;
3423 9cd7cbd1 2020-12-07 stsp }
3424 9cd7cbd1 2020-12-07 stsp }
3425 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3426 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3427 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3428 5036bf37 2018-09-24 stsp goto done;
3429 5036bf37 2018-09-24 stsp }
3430 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3431 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3432 e5a0f69f 2018-08-18 stsp
3433 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3434 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3435 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3436 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3437 11b20872 2019-11-08 stsp if (err)
3438 11b20872 2019-11-08 stsp goto done;
3439 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3440 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3441 11b20872 2019-11-08 stsp if (err) {
3442 11b20872 2019-11-08 stsp free_colors(&s->colors);
3443 11b20872 2019-11-08 stsp goto done;
3444 11b20872 2019-11-08 stsp }
3445 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3446 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3447 11b20872 2019-11-08 stsp if (err) {
3448 11b20872 2019-11-08 stsp free_colors(&s->colors);
3449 11b20872 2019-11-08 stsp goto done;
3450 11b20872 2019-11-08 stsp }
3451 11b20872 2019-11-08 stsp }
3452 11b20872 2019-11-08 stsp
3453 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3454 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3455 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3456 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3457 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3458 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3459 1a76625f 2018-10-22 stsp
3460 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3461 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3462 1af5eddf 2022-06-23 thomas if (err)
3463 1af5eddf 2022-06-23 thomas goto done;
3464 1af5eddf 2022-06-23 thomas }
3465 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3466 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3467 7cd52833 2022-06-23 thomas if (err)
3468 7cd52833 2022-06-23 thomas goto done;
3469 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3470 b672a97a 2020-01-27 stsp !s->log_branches);
3471 1a76625f 2018-10-22 stsp if (err)
3472 1a76625f 2018-10-22 stsp goto done;
3473 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3474 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3475 c5b78334 2020-01-12 stsp if (err)
3476 c5b78334 2020-01-12 stsp goto done;
3477 1a76625f 2018-10-22 stsp
3478 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3479 1a76625f 2018-10-22 stsp if (errcode) {
3480 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3481 1a76625f 2018-10-22 stsp goto done;
3482 1a76625f 2018-10-22 stsp }
3483 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3484 7c1452c1 2020-03-26 stsp if (errcode) {
3485 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3486 7c1452c1 2020-03-26 stsp goto done;
3487 7c1452c1 2020-03-26 stsp }
3488 1a76625f 2018-10-22 stsp
3489 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3490 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3491 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3492 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3493 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3494 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3495 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3496 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3497 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3498 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3499 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3500 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3501 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3502 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3503 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3504 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3505 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3506 ba4f502b 2018-08-04 stsp done:
3507 1a76625f 2018-10-22 stsp if (err)
3508 1a76625f 2018-10-22 stsp close_log_view(view);
3509 ba4f502b 2018-08-04 stsp return err;
3510 ba4f502b 2018-08-04 stsp }
3511 ba4f502b 2018-08-04 stsp
3512 e5a0f69f 2018-08-18 stsp static const struct got_error *
3513 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3514 ba4f502b 2018-08-04 stsp {
3515 f2f6d207 2020-11-24 stsp const struct got_error *err;
3516 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3517 ba4f502b 2018-08-04 stsp
3518 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3519 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3520 2b380cc8 2018-10-24 stsp &s->thread_args);
3521 2b380cc8 2018-10-24 stsp if (errcode)
3522 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3523 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3524 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3525 f2f6d207 2020-11-24 stsp if (err)
3526 f2f6d207 2020-11-24 stsp return err;
3527 f2f6d207 2020-11-24 stsp }
3528 2b380cc8 2018-10-24 stsp }
3529 2b380cc8 2018-10-24 stsp
3530 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3531 b31f89ff 2022-07-01 thomas }
3532 b31f89ff 2022-07-01 thomas
3533 b31f89ff 2022-07-01 thomas static void
3534 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3535 b31f89ff 2022-07-01 thomas {
3536 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3537 b31f89ff 2022-07-01 thomas
3538 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3539 b31f89ff 2022-07-01 thomas return;
3540 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3541 7e8004ba 2022-09-11 thomas view->count = 0;
3542 b31f89ff 2022-07-01 thomas
3543 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3544 b31f89ff 2022-07-01 thomas || home)
3545 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3546 b31f89ff 2022-07-01 thomas
3547 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3548 b31f89ff 2022-07-01 thomas --s->selected;
3549 b31f89ff 2022-07-01 thomas else
3550 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3551 b31f89ff 2022-07-01 thomas
3552 b31f89ff 2022-07-01 thomas select_commit(s);
3553 b31f89ff 2022-07-01 thomas return;
3554 b31f89ff 2022-07-01 thomas }
3555 b31f89ff 2022-07-01 thomas
3556 b31f89ff 2022-07-01 thomas static const struct got_error *
3557 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3558 b31f89ff 2022-07-01 thomas {
3559 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3560 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3561 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3562 b31f89ff 2022-07-01 thomas
3563 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3564 7e8004ba 2022-09-11 thomas return NULL;
3565 7e8004ba 2022-09-11 thomas
3566 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3567 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3568 b31f89ff 2022-07-01 thomas return NULL;
3569 b31f89ff 2022-07-01 thomas
3570 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3571 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3572 b31f89ff 2022-07-01 thomas
3573 7ed048bd 2022-08-12 thomas if (!page) {
3574 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3575 b31f89ff 2022-07-01 thomas ++s->selected;
3576 b31f89ff 2022-07-01 thomas else
3577 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3578 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3579 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3580 7ed048bd 2022-08-12 thomas int n;
3581 7ed048bd 2022-08-12 thomas
3582 7ed048bd 2022-08-12 thomas s->selected = 0;
3583 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3584 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3585 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3586 7ed048bd 2022-08-12 thomas if (entry == NULL)
3587 7ed048bd 2022-08-12 thomas break;
3588 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3589 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3590 7ed048bd 2022-08-12 thomas }
3591 7ed048bd 2022-08-12 thomas if (n > 0)
3592 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3593 b31f89ff 2022-07-01 thomas } else {
3594 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3595 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3596 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3597 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3598 bd3f8225 2022-08-12 thomas else
3599 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3600 b31f89ff 2022-07-01 thomas }
3601 b31f89ff 2022-07-01 thomas if (err)
3602 b31f89ff 2022-07-01 thomas return err;
3603 b31f89ff 2022-07-01 thomas
3604 a5d43cac 2022-07-01 thomas /*
3605 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3606 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3607 a5d43cac 2022-07-01 thomas */
3608 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3609 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3610 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3611 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3612 25100026 2022-08-13 thomas }
3613 a5d43cac 2022-07-01 thomas
3614 b31f89ff 2022-07-01 thomas select_commit(s);
3615 b31f89ff 2022-07-01 thomas
3616 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3617 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3618 b31f89ff 2022-07-01 thomas view->count = 0;
3619 b31f89ff 2022-07-01 thomas
3620 b31f89ff 2022-07-01 thomas return NULL;
3621 e5a0f69f 2018-08-18 stsp }
3622 04cc582a 2018-08-01 stsp
3623 a5d43cac 2022-07-01 thomas static void
3624 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3625 a5d43cac 2022-07-01 thomas {
3626 24415785 2022-07-03 thomas *x = 0;
3627 24415785 2022-07-03 thomas *y = 0;
3628 24415785 2022-07-03 thomas
3629 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3630 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3631 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3632 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3633 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3634 53d2bdd3 2022-07-10 thomas else
3635 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3636 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3637 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3638 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3639 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3640 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3641 53d2bdd3 2022-07-10 thomas else
3642 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3643 53d2bdd3 2022-07-10 thomas }
3644 a5d43cac 2022-07-01 thomas }
3645 a5d43cac 2022-07-01 thomas
3646 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3647 e5a0f69f 2018-08-18 stsp static const struct got_error *
3648 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3649 a5d43cac 2022-07-01 thomas {
3650 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3651 a5d43cac 2022-07-01 thomas
3652 a5d43cac 2022-07-01 thomas view->nlines = y;
3653 64486692 2022-07-07 thomas view->ncols = COLS;
3654 a5d43cac 2022-07-01 thomas err = view_resize(view);
3655 a5d43cac 2022-07-01 thomas if (err)
3656 a5d43cac 2022-07-01 thomas return err;
3657 a5d43cac 2022-07-01 thomas
3658 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3659 a5d43cac 2022-07-01 thomas
3660 a5d43cac 2022-07-01 thomas return err;
3661 07dd3ed3 2022-08-06 thomas }
3662 07dd3ed3 2022-08-06 thomas
3663 07dd3ed3 2022-08-06 thomas static const struct got_error *
3664 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3665 07dd3ed3 2022-08-06 thomas {
3666 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3667 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3668 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3669 25100026 2022-08-13 thomas
3670 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3671 25100026 2022-08-13 thomas return NULL;
3672 07dd3ed3 2022-08-06 thomas
3673 07dd3ed3 2022-08-06 thomas g = view->gline;
3674 07dd3ed3 2022-08-06 thomas view->gline = 0;
3675 07dd3ed3 2022-08-06 thomas
3676 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3677 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3678 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3679 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3680 07dd3ed3 2022-08-06 thomas select_commit(s);
3681 07dd3ed3 2022-08-06 thomas return NULL;
3682 07dd3ed3 2022-08-06 thomas }
3683 07dd3ed3 2022-08-06 thomas
3684 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3685 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3686 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3687 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3688 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3689 07dd3ed3 2022-08-06 thomas if (err)
3690 07dd3ed3 2022-08-06 thomas return err;
3691 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3692 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3693 07dd3ed3 2022-08-06 thomas
3694 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3695 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3696 07dd3ed3 2022-08-06 thomas
3697 07dd3ed3 2022-08-06 thomas select_commit(s);
3698 07dd3ed3 2022-08-06 thomas return NULL;
3699 c72de8ab 2023-02-03 thomas
3700 c72de8ab 2023-02-03 thomas }
3701 c72de8ab 2023-02-03 thomas
3702 c72de8ab 2023-02-03 thomas static void
3703 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
3704 c72de8ab 2023-02-03 thomas {
3705 07dd3ed3 2022-08-06 thomas
3706 c72de8ab 2023-02-03 thomas switch (ch) {
3707 c72de8ab 2023-02-03 thomas case KEY_LEFT:
3708 c72de8ab 2023-02-03 thomas case 'h':
3709 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
3710 c72de8ab 2023-02-03 thomas if (view->x <= 0)
3711 c72de8ab 2023-02-03 thomas view->count = 0;
3712 c72de8ab 2023-02-03 thomas break;
3713 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
3714 c72de8ab 2023-02-03 thomas case 'l':
3715 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
3716 c72de8ab 2023-02-03 thomas view->x += 2;
3717 c72de8ab 2023-02-03 thomas else
3718 c72de8ab 2023-02-03 thomas view->count = 0;
3719 c72de8ab 2023-02-03 thomas break;
3720 c72de8ab 2023-02-03 thomas case '0':
3721 c72de8ab 2023-02-03 thomas view->x = 0;
3722 c72de8ab 2023-02-03 thomas break;
3723 c72de8ab 2023-02-03 thomas case '$':
3724 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3725 c72de8ab 2023-02-03 thomas view->count = 0;
3726 c72de8ab 2023-02-03 thomas break;
3727 c72de8ab 2023-02-03 thomas default:
3728 c72de8ab 2023-02-03 thomas break;
3729 c72de8ab 2023-02-03 thomas }
3730 a5d43cac 2022-07-01 thomas }
3731 a5d43cac 2022-07-01 thomas
3732 a5d43cac 2022-07-01 thomas static const struct got_error *
3733 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3734 e5a0f69f 2018-08-18 stsp {
3735 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3736 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3737 7ed048bd 2022-08-12 thomas int eos, nscroll;
3738 80ddbec8 2018-04-29 stsp
3739 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3740 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3741 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3742 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3743 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3744 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3745 fb280deb 2021-08-30 stsp }
3746 c0be8933 2022-08-12 thomas if (err)
3747 c0be8933 2022-08-12 thomas return err;
3748 528dedf3 2021-08-30 stsp }
3749 068ab281 2022-07-01 thomas
3750 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3751 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3752 068ab281 2022-07-01 thomas --eos; /* border */
3753 07dd3ed3 2022-08-06 thomas
3754 07dd3ed3 2022-08-06 thomas if (view->gline)
3755 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3756 068ab281 2022-07-01 thomas
3757 528dedf3 2021-08-30 stsp switch (ch) {
3758 7e8004ba 2022-09-11 thomas case '&':
3759 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3760 7e8004ba 2022-09-11 thomas break;
3761 1e37a5c2 2019-05-12 jcs case 'q':
3762 1e37a5c2 2019-05-12 jcs s->quit = 1;
3763 05171be4 2022-06-23 thomas break;
3764 05171be4 2022-06-23 thomas case '0':
3765 05171be4 2022-06-23 thomas case '$':
3766 05171be4 2022-06-23 thomas case KEY_RIGHT:
3767 05171be4 2022-06-23 thomas case 'l':
3768 05171be4 2022-06-23 thomas case KEY_LEFT:
3769 05171be4 2022-06-23 thomas case 'h':
3770 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
3771 05171be4 2022-06-23 thomas break;
3772 1e37a5c2 2019-05-12 jcs case 'k':
3773 1e37a5c2 2019-05-12 jcs case KEY_UP:
3774 1e37a5c2 2019-05-12 jcs case '<':
3775 1e37a5c2 2019-05-12 jcs case ',':
3776 f7140bf5 2021-10-17 thomas case CTRL('p'):
3777 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3778 912a3f79 2021-08-30 j break;
3779 912a3f79 2021-08-30 j case 'g':
3780 aa7a1117 2023-01-09 thomas case '=':
3781 912a3f79 2021-08-30 j case KEY_HOME:
3782 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3783 07b0611c 2022-06-23 thomas view->count = 0;
3784 1e37a5c2 2019-05-12 jcs break;
3785 70f17a53 2022-06-13 thomas case CTRL('u'):
3786 23427b14 2022-06-23 thomas case 'u':
3787 70f17a53 2022-06-13 thomas nscroll /= 2;
3788 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3789 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3790 a4292ac5 2019-05-12 jcs case CTRL('b'):
3791 1c5e5faa 2022-06-23 thomas case 'b':
3792 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3793 1e37a5c2 2019-05-12 jcs break;
3794 1e37a5c2 2019-05-12 jcs case 'j':
3795 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3796 1e37a5c2 2019-05-12 jcs case '>':
3797 1e37a5c2 2019-05-12 jcs case '.':
3798 f7140bf5 2021-10-17 thomas case CTRL('n'):
3799 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3800 912a3f79 2021-08-30 j break;
3801 f69c5a46 2022-07-19 thomas case '@':
3802 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3803 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
3804 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
3805 f69c5a46 2022-07-19 thomas break;
3806 912a3f79 2021-08-30 j case 'G':
3807 aa7a1117 2023-01-09 thomas case '*':
3808 912a3f79 2021-08-30 j case KEY_END: {
3809 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3810 912a3f79 2021-08-30 j * traverse them all. */
3811 07b0611c 2022-06-23 thomas view->count = 0;
3812 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3813 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3814 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3815 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3816 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3817 1e37a5c2 2019-05-12 jcs break;
3818 912a3f79 2021-08-30 j }
3819 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3820 23427b14 2022-06-23 thomas case 'd':
3821 70f17a53 2022-06-13 thomas nscroll /= 2;
3822 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3823 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3824 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3825 4c2d69cb 2022-06-23 thomas case 'f':
3826 b31f89ff 2022-07-01 thomas case ' ':
3827 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3828 1e37a5c2 2019-05-12 jcs break;
3829 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3830 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3831 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3832 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3833 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3834 2b779855 2020-12-05 naddy select_commit(s);
3835 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3836 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3837 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3838 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3839 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3840 0bf7f153 2020-12-02 naddy }
3841 1e37a5c2 2019-05-12 jcs break;
3842 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3843 444d5325 2022-07-03 thomas case '\r':
3844 07b0611c 2022-06-23 thomas view->count = 0;
3845 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3846 e5a0f69f 2018-08-18 stsp break;
3847 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3848 1e37a5c2 2019-05-12 jcs break;
3849 1be4947a 2022-07-22 thomas case 'T':
3850 07b0611c 2022-06-23 thomas view->count = 0;
3851 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3852 5036bf37 2018-09-24 stsp break;
3853 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3854 1e37a5c2 2019-05-12 jcs break;
3855 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3856 21355643 2020-12-06 stsp case CTRL('l'):
3857 21355643 2020-12-06 stsp case 'B':
3858 07b0611c 2022-06-23 thomas view->count = 0;
3859 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3860 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3861 1e37a5c2 2019-05-12 jcs break;
3862 21355643 2020-12-06 stsp err = stop_log_thread(s);
3863 74cfe85e 2020-10-20 stsp if (err)
3864 74cfe85e 2020-10-20 stsp return err;
3865 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3866 21355643 2020-12-06 stsp char *parent_path;
3867 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3868 21355643 2020-12-06 stsp if (err)
3869 21355643 2020-12-06 stsp return err;
3870 21355643 2020-12-06 stsp free(s->in_repo_path);
3871 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3872 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3873 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3874 21355643 2020-12-06 stsp struct got_object_id *start_id;
3875 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3876 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3877 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3878 466429a1 2022-11-17 thomas if (err) {
3879 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
3880 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
3881 466429a1 2022-11-17 thomas return err;
3882 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
3883 466429a1 2022-11-17 thomas free(s->head_ref_name);
3884 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
3885 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
3886 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3887 466429a1 2022-11-17 thomas &tog_refs, s->repo);
3888 466429a1 2022-11-17 thomas if (err)
3889 466429a1 2022-11-17 thomas return err;
3890 466429a1 2022-11-17 thomas }
3891 21355643 2020-12-06 stsp free(s->start_id);
3892 21355643 2020-12-06 stsp s->start_id = start_id;
3893 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3894 21355643 2020-12-06 stsp } else /* 'B' */
3895 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3896 21355643 2020-12-06 stsp
3897 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3898 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3899 bf7e79b3 2022-06-23 thomas if (err)
3900 bf7e79b3 2022-06-23 thomas return err;
3901 bf7e79b3 2022-06-23 thomas }
3902 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3903 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3904 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3905 74cfe85e 2020-10-20 stsp if (err)
3906 21355643 2020-12-06 stsp return err;
3907 51a10b52 2020-12-26 stsp tog_free_refs();
3908 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3909 ca51c541 2020-12-07 stsp if (err)
3910 ca51c541 2020-12-07 stsp return err;
3911 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3912 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3913 d01904d4 2019-06-25 stsp if (err)
3914 d01904d4 2019-06-25 stsp return err;
3915 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3916 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3917 b672a97a 2020-01-27 stsp if (err)
3918 b672a97a 2020-01-27 stsp return err;
3919 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3920 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3921 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3922 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3923 21355643 2020-12-06 stsp s->selected_entry = NULL;
3924 21355643 2020-12-06 stsp s->selected = 0;
3925 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3926 21355643 2020-12-06 stsp s->quit = 0;
3927 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3928 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3929 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3930 94ecf40d 2022-07-22 thomas view->offset = 0;
3931 d01904d4 2019-06-25 stsp break;
3932 1be4947a 2022-07-22 thomas case 'R':
3933 07b0611c 2022-06-23 thomas view->count = 0;
3934 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3935 6458efa5 2020-11-24 stsp break;
3936 1e37a5c2 2019-05-12 jcs default:
3937 07b0611c 2022-06-23 thomas view->count = 0;
3938 1e37a5c2 2019-05-12 jcs break;
3939 899d86c2 2018-05-10 stsp }
3940 e5a0f69f 2018-08-18 stsp
3941 80ddbec8 2018-04-29 stsp return err;
3942 80ddbec8 2018-04-29 stsp }
3943 80ddbec8 2018-04-29 stsp
3944 4ed7e80c 2018-05-20 stsp static const struct got_error *
3945 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3946 c2db6724 2019-01-04 stsp {
3947 c2db6724 2019-01-04 stsp const struct got_error *error;
3948 c2db6724 2019-01-04 stsp
3949 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3950 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3951 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3952 37c06ea4 2019-07-15 stsp #endif
3953 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3954 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3955 c2db6724 2019-01-04 stsp
3956 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3957 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3958 c2db6724 2019-01-04 stsp
3959 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3960 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3961 c2db6724 2019-01-04 stsp
3962 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3963 c2db6724 2019-01-04 stsp if (error != NULL)
3964 c2db6724 2019-01-04 stsp return error;
3965 c2db6724 2019-01-04 stsp
3966 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3967 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3968 c2db6724 2019-01-04 stsp
3969 c2db6724 2019-01-04 stsp return NULL;
3970 c2db6724 2019-01-04 stsp }
3971 c2db6724 2019-01-04 stsp
3972 a915003a 2019-02-05 stsp static void
3973 a915003a 2019-02-05 stsp init_curses(void)
3974 a915003a 2019-02-05 stsp {
3975 296152d1 2022-05-31 thomas /*
3976 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3977 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3978 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3979 296152d1 2022-05-31 thomas */
3980 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3981 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3982 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3983 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3984 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3985 296152d1 2022-05-31 thomas
3986 a915003a 2019-02-05 stsp initscr();
3987 a915003a 2019-02-05 stsp cbreak();
3988 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3989 a915003a 2019-02-05 stsp noecho();
3990 a915003a 2019-02-05 stsp nonl();
3991 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3992 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3993 a915003a 2019-02-05 stsp curs_set(0);
3994 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3995 6d17833f 2019-11-08 stsp start_color();
3996 6d17833f 2019-11-08 stsp use_default_colors();
3997 6d17833f 2019-11-08 stsp }
3998 a915003a 2019-02-05 stsp }
3999 a915003a 2019-02-05 stsp
4000 c2db6724 2019-01-04 stsp static const struct got_error *
4001 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4002 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4003 f135c941 2020-02-20 stsp {
4004 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4005 f135c941 2020-02-20 stsp
4006 f135c941 2020-02-20 stsp if (argc == 0) {
4007 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4008 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4009 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4010 f135c941 2020-02-20 stsp return NULL;
4011 f135c941 2020-02-20 stsp }
4012 f135c941 2020-02-20 stsp
4013 f135c941 2020-02-20 stsp if (worktree) {
4014 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4015 bfd61697 2020-11-14 stsp char *p;
4016 f135c941 2020-02-20 stsp
4017 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4018 f135c941 2020-02-20 stsp if (err)
4019 f135c941 2020-02-20 stsp return err;
4020 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4021 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4022 bfd61697 2020-11-14 stsp p) == -1) {
4023 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4024 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4025 f135c941 2020-02-20 stsp }
4026 f135c941 2020-02-20 stsp free(p);
4027 f135c941 2020-02-20 stsp } else
4028 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4029 f135c941 2020-02-20 stsp
4030 f135c941 2020-02-20 stsp return err;
4031 f135c941 2020-02-20 stsp }
4032 f135c941 2020-02-20 stsp
4033 f135c941 2020-02-20 stsp static const struct got_error *
4034 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4035 9f7d7167 2018-04-29 stsp {
4036 80ddbec8 2018-04-29 stsp const struct got_error *error;
4037 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4038 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4039 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4040 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4041 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4042 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4043 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4044 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4045 04cc582a 2018-08-01 stsp struct tog_view *view;
4046 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4047 80ddbec8 2018-04-29 stsp
4048 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4049 80ddbec8 2018-04-29 stsp switch (ch) {
4050 b672a97a 2020-01-27 stsp case 'b':
4051 b672a97a 2020-01-27 stsp log_branches = 1;
4052 b672a97a 2020-01-27 stsp break;
4053 80ddbec8 2018-04-29 stsp case 'c':
4054 80ddbec8 2018-04-29 stsp start_commit = optarg;
4055 80ddbec8 2018-04-29 stsp break;
4056 ecb28ae0 2018-07-16 stsp case 'r':
4057 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4058 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4059 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4060 9ba1d308 2019-10-21 stsp optarg);
4061 ecb28ae0 2018-07-16 stsp break;
4062 80ddbec8 2018-04-29 stsp default:
4063 17020d27 2019-03-07 stsp usage_log();
4064 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4065 80ddbec8 2018-04-29 stsp }
4066 80ddbec8 2018-04-29 stsp }
4067 80ddbec8 2018-04-29 stsp
4068 80ddbec8 2018-04-29 stsp argc -= optind;
4069 80ddbec8 2018-04-29 stsp argv += optind;
4070 80ddbec8 2018-04-29 stsp
4071 f135c941 2020-02-20 stsp if (argc > 1)
4072 f135c941 2020-02-20 stsp usage_log();
4073 963f97a1 2019-03-18 stsp
4074 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4075 7cd52833 2022-06-23 thomas if (error != NULL)
4076 7cd52833 2022-06-23 thomas goto done;
4077 7cd52833 2022-06-23 thomas
4078 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4079 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4080 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4081 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4082 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4083 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4084 c156c7a4 2020-12-18 stsp goto done;
4085 a1fbf39a 2019-08-11 stsp if (worktree)
4086 6962eb72 2020-02-20 stsp repo_path =
4087 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4088 a1fbf39a 2019-08-11 stsp else
4089 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4090 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4091 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4092 c156c7a4 2020-12-18 stsp goto done;
4093 c156c7a4 2020-12-18 stsp }
4094 ecb28ae0 2018-07-16 stsp }
4095 ecb28ae0 2018-07-16 stsp
4096 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4097 80ddbec8 2018-04-29 stsp if (error != NULL)
4098 ecb28ae0 2018-07-16 stsp goto done;
4099 80ddbec8 2018-04-29 stsp
4100 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4101 f135c941 2020-02-20 stsp repo, worktree);
4102 f135c941 2020-02-20 stsp if (error)
4103 f135c941 2020-02-20 stsp goto done;
4104 f135c941 2020-02-20 stsp
4105 f135c941 2020-02-20 stsp init_curses();
4106 f135c941 2020-02-20 stsp
4107 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4108 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4109 c02c541e 2019-03-29 stsp if (error)
4110 c02c541e 2019-03-29 stsp goto done;
4111 c02c541e 2019-03-29 stsp
4112 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4113 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4114 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4115 87670572 2020-12-26 naddy if (error)
4116 87670572 2020-12-26 naddy goto done;
4117 87670572 2020-12-26 naddy }
4118 51a10b52 2020-12-26 stsp
4119 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4120 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4121 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4122 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4123 d8f38dc4 2020-12-05 stsp if (error)
4124 d8f38dc4 2020-12-05 stsp goto done;
4125 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4126 d8f38dc4 2020-12-05 stsp } else {
4127 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4128 d8f38dc4 2020-12-05 stsp if (error == NULL)
4129 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4130 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4131 d8f38dc4 2020-12-05 stsp goto done;
4132 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4133 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4134 d8f38dc4 2020-12-05 stsp if (error)
4135 d8f38dc4 2020-12-05 stsp goto done;
4136 d8f38dc4 2020-12-05 stsp }
4137 8b473291 2019-02-21 stsp
4138 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4139 04cc582a 2018-08-01 stsp if (view == NULL) {
4140 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4141 04cc582a 2018-08-01 stsp goto done;
4142 04cc582a 2018-08-01 stsp }
4143 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4144 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4145 ba4f502b 2018-08-04 stsp if (error)
4146 ba4f502b 2018-08-04 stsp goto done;
4147 2fc00ff4 2019-08-31 stsp if (worktree) {
4148 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4149 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4150 2fc00ff4 2019-08-31 stsp worktree = NULL;
4151 2fc00ff4 2019-08-31 stsp }
4152 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4153 ecb28ae0 2018-07-16 stsp done:
4154 f135c941 2020-02-20 stsp free(in_repo_path);
4155 ecb28ae0 2018-07-16 stsp free(repo_path);
4156 ecb28ae0 2018-07-16 stsp free(cwd);
4157 899d86c2 2018-05-10 stsp free(start_id);
4158 d8f38dc4 2020-12-05 stsp free(label);
4159 d8f38dc4 2020-12-05 stsp if (ref)
4160 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4161 1d0f4054 2021-06-17 stsp if (repo) {
4162 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4163 1d0f4054 2021-06-17 stsp if (error == NULL)
4164 1d0f4054 2021-06-17 stsp error = close_err;
4165 1d0f4054 2021-06-17 stsp }
4166 ec142235 2019-03-07 stsp if (worktree)
4167 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4168 7cd52833 2022-06-23 thomas if (pack_fds) {
4169 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4170 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4171 7cd52833 2022-06-23 thomas if (error == NULL)
4172 7cd52833 2022-06-23 thomas error = pack_err;
4173 7cd52833 2022-06-23 thomas }
4174 51a10b52 2020-12-26 stsp tog_free_refs();
4175 80ddbec8 2018-04-29 stsp return error;
4176 9f7d7167 2018-04-29 stsp }
4177 9f7d7167 2018-04-29 stsp
4178 4ed7e80c 2018-05-20 stsp __dead static void
4179 9f7d7167 2018-04-29 stsp usage_diff(void)
4180 9f7d7167 2018-04-29 stsp {
4181 80ddbec8 2018-04-29 stsp endwin();
4182 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4183 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4184 9f7d7167 2018-04-29 stsp exit(1);
4185 b304db33 2018-05-20 stsp }
4186 b304db33 2018-05-20 stsp
4187 6d17833f 2019-11-08 stsp static int
4188 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4189 41605754 2020-11-12 stsp regmatch_t *regmatch)
4190 6d17833f 2019-11-08 stsp {
4191 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4192 6d17833f 2019-11-08 stsp }
4193 6d17833f 2019-11-08 stsp
4194 ef20f542 2022-06-26 thomas static struct tog_color *
4195 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4196 6d17833f 2019-11-08 stsp {
4197 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4198 6d17833f 2019-11-08 stsp
4199 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4200 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4201 6d17833f 2019-11-08 stsp return tc;
4202 6d17833f 2019-11-08 stsp }
4203 6d17833f 2019-11-08 stsp
4204 6d17833f 2019-11-08 stsp return NULL;
4205 6d17833f 2019-11-08 stsp }
4206 6d17833f 2019-11-08 stsp
4207 4ed7e80c 2018-05-20 stsp static const struct got_error *
4208 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4209 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4210 41605754 2020-11-12 stsp {
4211 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4212 666f7b10 2022-06-23 thomas char *exstr = NULL;
4213 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4214 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4215 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4216 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4217 41605754 2020-11-12 stsp
4218 41605754 2020-11-12 stsp *wtotal = 0;
4219 cbea3800 2022-06-23 thomas
4220 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4221 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4222 41605754 2020-11-12 stsp
4223 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4224 666f7b10 2022-06-23 thomas if (err)
4225 666f7b10 2022-06-23 thomas return err;
4226 666f7b10 2022-06-23 thomas
4227 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4228 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4229 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4230 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4231 666f7b10 2022-06-23 thomas goto done;
4232 666f7b10 2022-06-23 thomas }
4233 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4234 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4235 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4236 cbea3800 2022-06-23 thomas goto done;
4237 cbea3800 2022-06-23 thomas }
4238 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4239 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4240 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4241 cbea3800 2022-06-23 thomas goto done;
4242 cbea3800 2022-06-23 thomas }
4243 05171be4 2022-06-23 thomas
4244 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4245 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4246 cbea3800 2022-06-23 thomas col_tab_align, 1);
4247 cbea3800 2022-06-23 thomas if (err)
4248 cbea3800 2022-06-23 thomas goto done;
4249 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4250 05171be4 2022-06-23 thomas if (n) {
4251 cbea3800 2022-06-23 thomas free(wline);
4252 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4253 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4254 cbea3800 2022-06-23 thomas if (err)
4255 cbea3800 2022-06-23 thomas goto done;
4256 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4257 cbea3800 2022-06-23 thomas wlimit -= width;
4258 cbea3800 2022-06-23 thomas *wtotal += width;
4259 41605754 2020-11-12 stsp }
4260 41605754 2020-11-12 stsp
4261 41605754 2020-11-12 stsp if (wlimit > 0) {
4262 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4263 cbea3800 2022-06-23 thomas size_t wlen;
4264 cbea3800 2022-06-23 thomas
4265 cbea3800 2022-06-23 thomas free(wline);
4266 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4267 cbea3800 2022-06-23 thomas col_tab_align, 1);
4268 cbea3800 2022-06-23 thomas if (err)
4269 cbea3800 2022-06-23 thomas goto done;
4270 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4271 cbea3800 2022-06-23 thomas while (i < wlen) {
4272 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4273 cbea3800 2022-06-23 thomas if (width == -1) {
4274 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4275 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4276 cbea3800 2022-06-23 thomas goto done;
4277 cbea3800 2022-06-23 thomas }
4278 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4279 cbea3800 2022-06-23 thomas break;
4280 1c5e5faa 2022-06-23 thomas w += width;
4281 cbea3800 2022-06-23 thomas i++;
4282 41605754 2020-11-12 stsp }
4283 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4284 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4285 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4286 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4287 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4288 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4289 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4290 41605754 2020-11-12 stsp }
4291 41605754 2020-11-12 stsp }
4292 41605754 2020-11-12 stsp
4293 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4294 cbea3800 2022-06-23 thomas free(wline);
4295 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4296 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4297 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4298 cbea3800 2022-06-23 thomas col_tab_align, 1);
4299 cbea3800 2022-06-23 thomas if (err)
4300 cbea3800 2022-06-23 thomas goto done;
4301 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4302 cbea3800 2022-06-23 thomas } else {
4303 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4304 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4305 cbea3800 2022-06-23 thomas if (err)
4306 cbea3800 2022-06-23 thomas goto done;
4307 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4308 cbea3800 2022-06-23 thomas }
4309 cbea3800 2022-06-23 thomas *wtotal += width2;
4310 41605754 2020-11-12 stsp }
4311 cbea3800 2022-06-23 thomas done:
4312 05171be4 2022-06-23 thomas free(wline);
4313 666f7b10 2022-06-23 thomas free(exstr);
4314 cbea3800 2022-06-23 thomas free(seg0);
4315 cbea3800 2022-06-23 thomas free(seg1);
4316 cbea3800 2022-06-23 thomas free(seg2);
4317 cbea3800 2022-06-23 thomas return err;
4318 41605754 2020-11-12 stsp }
4319 07dd3ed3 2022-08-06 thomas
4320 07dd3ed3 2022-08-06 thomas static int
4321 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4322 07dd3ed3 2022-08-06 thomas {
4323 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4324 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4325 07dd3ed3 2022-08-06 thomas
4326 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4327 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4328 fc2737d5 2022-09-15 thomas
4329 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4330 fc2737d5 2022-09-15 thomas selected = first;
4331 fc2737d5 2022-09-15 thomas eof = &s->eof;
4332 fc2737d5 2022-09-15 thomas f = s->f;
4333 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4334 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4335 41605754 2020-11-12 stsp
4336 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4337 07dd3ed3 2022-08-06 thomas selected = first;
4338 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4339 07dd3ed3 2022-08-06 thomas f = s->f;
4340 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4341 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4342 07dd3ed3 2022-08-06 thomas
4343 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4344 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4345 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4346 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4347 07dd3ed3 2022-08-06 thomas } else
4348 07dd3ed3 2022-08-06 thomas return 0;
4349 07dd3ed3 2022-08-06 thomas
4350 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4351 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4352 07dd3ed3 2022-08-06 thomas return 0;
4353 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4354 07dd3ed3 2022-08-06 thomas rewind(f);
4355 07dd3ed3 2022-08-06 thomas *eof = 0;
4356 07dd3ed3 2022-08-06 thomas *first = 1;
4357 07dd3ed3 2022-08-06 thomas *lineno = 0;
4358 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4359 07dd3ed3 2022-08-06 thomas return 0;
4360 07dd3ed3 2022-08-06 thomas }
4361 07dd3ed3 2022-08-06 thomas
4362 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4363 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4364 07dd3ed3 2022-08-06 thomas view->gline = 0;
4365 07dd3ed3 2022-08-06 thomas
4366 07dd3ed3 2022-08-06 thomas return 1;
4367 07dd3ed3 2022-08-06 thomas }
4368 07dd3ed3 2022-08-06 thomas
4369 41605754 2020-11-12 stsp static const struct got_error *
4370 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4371 26ed57b2 2018-05-19 stsp {
4372 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4373 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4374 61e69b96 2018-05-20 stsp const struct got_error *err;
4375 82c78e96 2022-08-06 thomas int nprinted = 0;
4376 b304db33 2018-05-20 stsp char *line;
4377 826082fe 2020-12-10 stsp size_t linesize = 0;
4378 826082fe 2020-12-10 stsp ssize_t linelen;
4379 61e69b96 2018-05-20 stsp wchar_t *wline;
4380 e0b650dd 2018-05-20 stsp int width;
4381 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4382 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4383 fe621944 2020-11-10 stsp off_t line_offset;
4384 26ed57b2 2018-05-19 stsp
4385 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4386 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4387 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4388 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4389 fe621944 2020-11-10 stsp
4390 f7d12f7e 2018-08-01 stsp werase(view->window);
4391 a3404814 2018-09-02 stsp
4392 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4393 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4394 07dd3ed3 2022-08-06 thomas
4395 a3404814 2018-09-02 stsp if (header) {
4396 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4397 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4398 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4399 07dd3ed3 2022-08-06 thomas
4400 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4401 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4402 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4403 f91a2b48 2022-06-23 thomas 0, 0);
4404 135a2da0 2020-11-11 stsp free(line);
4405 135a2da0 2020-11-11 stsp if (err)
4406 a3404814 2018-09-02 stsp return err;
4407 a3404814 2018-09-02 stsp
4408 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4409 a3404814 2018-09-02 stsp wstandout(view->window);
4410 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4411 e54cc94a 2020-11-11 stsp free(wline);
4412 e54cc94a 2020-11-11 stsp wline = NULL;
4413 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4414 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4415 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4416 a3404814 2018-09-02 stsp wstandend(view->window);
4417 26ed57b2 2018-05-19 stsp
4418 a3404814 2018-09-02 stsp if (max_lines <= 1)
4419 a3404814 2018-09-02 stsp return NULL;
4420 a3404814 2018-09-02 stsp max_lines--;
4421 a3404814 2018-09-02 stsp }
4422 a3404814 2018-09-02 stsp
4423 89f1a395 2020-12-01 naddy s->eof = 0;
4424 05171be4 2022-06-23 thomas view->maxx = 0;
4425 826082fe 2020-12-10 stsp line = NULL;
4426 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4427 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4428 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4429 6568f0aa 2022-08-06 thomas
4430 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4431 826082fe 2020-12-10 stsp if (linelen == -1) {
4432 826082fe 2020-12-10 stsp if (feof(s->f)) {
4433 826082fe 2020-12-10 stsp s->eof = 1;
4434 826082fe 2020-12-10 stsp break;
4435 826082fe 2020-12-10 stsp }
4436 826082fe 2020-12-10 stsp free(line);
4437 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4438 61e69b96 2018-05-20 stsp }
4439 05171be4 2022-06-23 thomas
4440 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4441 07dd3ed3 2022-08-06 thomas continue;
4442 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4443 07dd3ed3 2022-08-06 thomas continue;
4444 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4445 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4446 07dd3ed3 2022-08-06 thomas
4447 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4448 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4449 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4450 cbea3800 2022-06-23 thomas if (err) {
4451 cbea3800 2022-06-23 thomas free(line);
4452 cbea3800 2022-06-23 thomas return err;
4453 cbea3800 2022-06-23 thomas }
4454 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4455 cbea3800 2022-06-23 thomas free(wline);
4456 cbea3800 2022-06-23 thomas wline = NULL;
4457 cbea3800 2022-06-23 thomas
4458 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4459 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4460 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4461 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4462 07dd3ed3 2022-08-06 thomas if (attr)
4463 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4464 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4465 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4466 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4467 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4468 41605754 2020-11-12 stsp if (err) {
4469 41605754 2020-11-12 stsp free(line);
4470 41605754 2020-11-12 stsp return err;
4471 41605754 2020-11-12 stsp }
4472 41605754 2020-11-12 stsp } else {
4473 f1c0ec19 2022-06-23 thomas int skip;
4474 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4475 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4476 f1c0ec19 2022-06-23 thomas if (err) {
4477 f1c0ec19 2022-06-23 thomas free(line);
4478 f1c0ec19 2022-06-23 thomas return err;
4479 f1c0ec19 2022-06-23 thomas }
4480 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4481 f1c0ec19 2022-06-23 thomas free(wline);
4482 41605754 2020-11-12 stsp wline = NULL;
4483 41605754 2020-11-12 stsp }
4484 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4485 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4486 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4487 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4488 07dd3ed3 2022-08-06 thomas } else {
4489 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4490 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4491 07dd3ed3 2022-08-06 thomas }
4492 07dd3ed3 2022-08-06 thomas if (attr)
4493 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4494 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4495 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4496 826082fe 2020-12-10 stsp }
4497 826082fe 2020-12-10 stsp free(line);
4498 fe621944 2020-11-10 stsp if (nprinted >= 1)
4499 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4500 89f1a395 2020-12-01 naddy (nprinted - 1);
4501 fe621944 2020-11-10 stsp else
4502 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4503 26ed57b2 2018-05-19 stsp
4504 a5d43cac 2022-07-01 thomas view_border(view);
4505 c3e9aa98 2019-05-13 jcs
4506 89f1a395 2020-12-01 naddy if (s->eof) {
4507 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4508 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4509 c3e9aa98 2019-05-13 jcs nprinted++;
4510 c3e9aa98 2019-05-13 jcs }
4511 c3e9aa98 2019-05-13 jcs
4512 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4513 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4514 c3e9aa98 2019-05-13 jcs if (err) {
4515 c3e9aa98 2019-05-13 jcs return err;
4516 c3e9aa98 2019-05-13 jcs }
4517 26ed57b2 2018-05-19 stsp
4518 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4519 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4520 e54cc94a 2020-11-11 stsp free(wline);
4521 e54cc94a 2020-11-11 stsp wline = NULL;
4522 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4523 c3e9aa98 2019-05-13 jcs }
4524 c3e9aa98 2019-05-13 jcs
4525 26ed57b2 2018-05-19 stsp return NULL;
4526 abd2672a 2018-12-23 stsp }
4527 abd2672a 2018-12-23 stsp
4528 abd2672a 2018-12-23 stsp static char *
4529 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4530 abd2672a 2018-12-23 stsp {
4531 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4532 09867e48 2019-08-13 stsp char *p, *s;
4533 09867e48 2019-08-13 stsp
4534 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4535 09867e48 2019-08-13 stsp if (tm == NULL)
4536 09867e48 2019-08-13 stsp return NULL;
4537 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4538 09867e48 2019-08-13 stsp if (s == NULL)
4539 09867e48 2019-08-13 stsp return NULL;
4540 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4541 abd2672a 2018-12-23 stsp if (p)
4542 abd2672a 2018-12-23 stsp *p = '\0';
4543 abd2672a 2018-12-23 stsp return s;
4544 9f7d7167 2018-04-29 stsp }
4545 9f7d7167 2018-04-29 stsp
4546 4ed7e80c 2018-05-20 stsp static const struct got_error *
4547 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4548 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4549 abd2672a 2018-12-23 stsp {
4550 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4551 fe621944 2020-11-10 stsp
4552 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4553 fe621944 2020-11-10 stsp if (p == NULL)
4554 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4555 82c78e96 2022-08-06 thomas *lines = p;
4556 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4557 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4558 fe621944 2020-11-10 stsp (*nlines)++;
4559 82c78e96 2022-08-06 thomas
4560 fe621944 2020-11-10 stsp return NULL;
4561 fe621944 2020-11-10 stsp }
4562 fe621944 2020-11-10 stsp
4563 fe621944 2020-11-10 stsp static const struct got_error *
4564 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4565 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
4566 be97ab03 2023-01-19 thomas {
4567 be97ab03 2023-01-19 thomas struct got_diff_line *p;
4568 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
4569 be97ab03 2023-01-19 thomas size_t i, r;
4570 be97ab03 2023-01-19 thomas
4571 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
4572 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
4573 be97ab03 2023-01-19 thomas
4574 be97ab03 2023-01-19 thomas for (;;) {
4575 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
4576 be97ab03 2023-01-19 thomas if (r == 0) {
4577 be97ab03 2023-01-19 thomas if (ferror(src))
4578 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
4579 be97ab03 2023-01-19 thomas if (feof(src))
4580 be97ab03 2023-01-19 thomas break;
4581 be97ab03 2023-01-19 thomas }
4582 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
4583 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
4584 be97ab03 2023-01-19 thomas }
4585 be97ab03 2023-01-19 thomas
4586 9382c10a 2023-02-23 thomas if (s_nlines == 0 && *d_nlines == 0)
4587 9382c10a 2023-02-23 thomas return NULL;
4588 9382c10a 2023-02-23 thomas
4589 be97ab03 2023-01-19 thomas /*
4590 9382c10a 2023-02-23 thomas * If commit info was in dst, increment line offsets
4591 9382c10a 2023-02-23 thomas * of the appended diff content, but skip s_lines[0]
4592 9382c10a 2023-02-23 thomas * because offset zero is already in *d_lines.
4593 be97ab03 2023-01-19 thomas */
4594 9382c10a 2023-02-23 thomas if (*d_nlines > 0) {
4595 9382c10a 2023-02-23 thomas for (i = 1; i < s_nlines; ++i)
4596 9382c10a 2023-02-23 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4597 be97ab03 2023-01-19 thomas
4598 9382c10a 2023-02-23 thomas if (s_nlines > 0) {
4599 9382c10a 2023-02-23 thomas --s_nlines;
4600 9382c10a 2023-02-23 thomas ++s_lines;
4601 9382c10a 2023-02-23 thomas }
4602 9382c10a 2023-02-23 thomas }
4603 be97ab03 2023-01-19 thomas
4604 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4605 be97ab03 2023-01-19 thomas if (p == NULL) {
4606 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
4607 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
4608 be97ab03 2023-01-19 thomas }
4609 be97ab03 2023-01-19 thomas
4610 be97ab03 2023-01-19 thomas *d_lines = p;
4611 be97ab03 2023-01-19 thomas
4612 9382c10a 2023-02-23 thomas memcpy(*d_lines + *d_nlines, s_lines, s_nlines * sizeof(*s_lines));
4613 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
4614 be97ab03 2023-01-19 thomas
4615 be97ab03 2023-01-19 thomas return NULL;
4616 be97ab03 2023-01-19 thomas }
4617 be97ab03 2023-01-19 thomas
4618 be97ab03 2023-01-19 thomas static const struct got_error *
4619 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4620 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4621 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4622 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
4623 fe621944 2020-11-10 stsp {
4624 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4625 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4626 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4627 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4628 45d799e2 2018-12-23 stsp time_t committer_time;
4629 45d799e2 2018-12-23 stsp const char *author, *committer;
4630 8b473291 2019-02-21 stsp char *refs_str = NULL;
4631 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4632 fe621944 2020-11-10 stsp off_t outoff = 0;
4633 fe621944 2020-11-10 stsp int n;
4634 abd2672a 2018-12-23 stsp
4635 8b473291 2019-02-21 stsp if (refs) {
4636 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4637 8b473291 2019-02-21 stsp if (err)
4638 8b473291 2019-02-21 stsp return err;
4639 8b473291 2019-02-21 stsp }
4640 8b473291 2019-02-21 stsp
4641 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4642 abd2672a 2018-12-23 stsp if (err)
4643 abd2672a 2018-12-23 stsp return err;
4644 abd2672a 2018-12-23 stsp
4645 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4646 15a94983 2018-12-23 stsp if (err) {
4647 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4648 15a94983 2018-12-23 stsp goto done;
4649 15a94983 2018-12-23 stsp }
4650 abd2672a 2018-12-23 stsp
4651 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4652 fe621944 2020-11-10 stsp if (err)
4653 fe621944 2020-11-10 stsp goto done;
4654 fe621944 2020-11-10 stsp
4655 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4656 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4657 fe621944 2020-11-10 stsp if (n < 0) {
4658 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4659 abd2672a 2018-12-23 stsp goto done;
4660 abd2672a 2018-12-23 stsp }
4661 fe621944 2020-11-10 stsp outoff += n;
4662 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4663 fe621944 2020-11-10 stsp if (err)
4664 fe621944 2020-11-10 stsp goto done;
4665 fe621944 2020-11-10 stsp
4666 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4667 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4668 fe621944 2020-11-10 stsp if (n < 0) {
4669 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4670 abd2672a 2018-12-23 stsp goto done;
4671 abd2672a 2018-12-23 stsp }
4672 fe621944 2020-11-10 stsp outoff += n;
4673 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4674 fe621944 2020-11-10 stsp if (err)
4675 fe621944 2020-11-10 stsp goto done;
4676 fe621944 2020-11-10 stsp
4677 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
4678 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
4679 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
4680 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
4681 fe621944 2020-11-10 stsp if (n < 0) {
4682 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4683 fe621944 2020-11-10 stsp goto done;
4684 fe621944 2020-11-10 stsp }
4685 fe621944 2020-11-10 stsp outoff += n;
4686 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4687 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
4688 fe621944 2020-11-10 stsp if (err)
4689 fe621944 2020-11-10 stsp goto done;
4690 abd2672a 2018-12-23 stsp }
4691 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
4692 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
4693 48830929 2023-01-07 thomas if (datestr) {
4694 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
4695 fe621944 2020-11-10 stsp if (n < 0) {
4696 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4697 fe621944 2020-11-10 stsp goto done;
4698 fe621944 2020-11-10 stsp }
4699 fe621944 2020-11-10 stsp outoff += n;
4700 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4701 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
4702 fe621944 2020-11-10 stsp if (err)
4703 fe621944 2020-11-10 stsp goto done;
4704 abd2672a 2018-12-23 stsp }
4705 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4706 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4707 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4708 ac4dc263 2021-09-24 thomas int pn = 1;
4709 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4710 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4711 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4712 ac4dc263 2021-09-24 thomas if (err)
4713 ac4dc263 2021-09-24 thomas goto done;
4714 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4715 ac4dc263 2021-09-24 thomas if (n < 0) {
4716 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4717 ac4dc263 2021-09-24 thomas goto done;
4718 ac4dc263 2021-09-24 thomas }
4719 ac4dc263 2021-09-24 thomas outoff += n;
4720 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4721 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4722 ac4dc263 2021-09-24 thomas if (err)
4723 ac4dc263 2021-09-24 thomas goto done;
4724 ac4dc263 2021-09-24 thomas free(id_str);
4725 ac4dc263 2021-09-24 thomas id_str = NULL;
4726 ac4dc263 2021-09-24 thomas }
4727 ac4dc263 2021-09-24 thomas }
4728 ac4dc263 2021-09-24 thomas
4729 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4730 5943eee2 2019-08-13 stsp if (err)
4731 5943eee2 2019-08-13 stsp goto done;
4732 fe621944 2020-11-10 stsp s = logmsg;
4733 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4734 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4735 fe621944 2020-11-10 stsp if (n < 0) {
4736 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4737 fe621944 2020-11-10 stsp goto done;
4738 fe621944 2020-11-10 stsp }
4739 fe621944 2020-11-10 stsp outoff += n;
4740 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4741 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4742 fe621944 2020-11-10 stsp if (err)
4743 fe621944 2020-11-10 stsp goto done;
4744 abd2672a 2018-12-23 stsp }
4745 fe621944 2020-11-10 stsp
4746 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
4747 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4748 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
4749 772fcad5 2023-01-07 thomas
4750 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4751 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4752 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
4753 fe621944 2020-11-10 stsp if (n < 0) {
4754 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4755 fe621944 2020-11-10 stsp goto done;
4756 fe621944 2020-11-10 stsp }
4757 fe621944 2020-11-10 stsp outoff += n;
4758 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4759 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4760 fe621944 2020-11-10 stsp if (err)
4761 fe621944 2020-11-10 stsp goto done;
4762 0208f208 2020-05-05 stsp }
4763 fe621944 2020-11-10 stsp
4764 0208f208 2020-05-05 stsp fputc('\n', outfile);
4765 fe621944 2020-11-10 stsp outoff++;
4766 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4767 772fcad5 2023-01-07 thomas if (err)
4768 772fcad5 2023-01-07 thomas goto done;
4769 772fcad5 2023-01-07 thomas
4770 772fcad5 2023-01-07 thomas n = fprintf(outfile,
4771 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4772 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4773 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4774 772fcad5 2023-01-07 thomas if (n < 0) {
4775 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
4776 772fcad5 2023-01-07 thomas goto done;
4777 772fcad5 2023-01-07 thomas }
4778 772fcad5 2023-01-07 thomas outoff += n;
4779 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4780 772fcad5 2023-01-07 thomas if (err)
4781 772fcad5 2023-01-07 thomas goto done;
4782 772fcad5 2023-01-07 thomas
4783 772fcad5 2023-01-07 thomas fputc('\n', outfile);
4784 772fcad5 2023-01-07 thomas outoff++;
4785 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4786 abd2672a 2018-12-23 stsp done:
4787 abd2672a 2018-12-23 stsp free(id_str);
4788 5943eee2 2019-08-13 stsp free(logmsg);
4789 8b473291 2019-02-21 stsp free(refs_str);
4790 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4791 7510f233 2020-08-09 stsp if (err) {
4792 82c78e96 2022-08-06 thomas free(*lines);
4793 82c78e96 2022-08-06 thomas *lines = NULL;
4794 ae6a6978 2020-08-09 stsp *nlines = 0;
4795 7510f233 2020-08-09 stsp }
4796 fe621944 2020-11-10 stsp return err;
4797 abd2672a 2018-12-23 stsp }
4798 abd2672a 2018-12-23 stsp
4799 abd2672a 2018-12-23 stsp static const struct got_error *
4800 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4801 26ed57b2 2018-05-19 stsp {
4802 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4803 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
4804 15a94983 2018-12-23 stsp int obj_type;
4805 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
4806 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
4807 fe621944 2020-11-10 stsp
4808 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
4809 be97ab03 2023-01-19 thomas
4810 82c78e96 2022-08-06 thomas free(s->lines);
4811 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4812 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4813 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4814 fe621944 2020-11-10 stsp s->nlines = 0;
4815 26ed57b2 2018-05-19 stsp
4816 511a516b 2018-05-19 stsp f = got_opentemp();
4817 48ae06ee 2018-10-18 stsp if (f == NULL) {
4818 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4819 48ae06ee 2018-10-18 stsp goto done;
4820 48ae06ee 2018-10-18 stsp }
4821 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
4822 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
4823 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
4824 be97ab03 2023-01-19 thomas goto done;
4825 be97ab03 2023-01-19 thomas }
4826 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4827 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4828 fb43ecf1 2019-02-11 stsp goto done;
4829 fb43ecf1 2019-02-11 stsp }
4830 48ae06ee 2018-10-18 stsp s->f = f;
4831 26ed57b2 2018-05-19 stsp
4832 15a94983 2018-12-23 stsp if (s->id1)
4833 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4834 15a94983 2018-12-23 stsp else
4835 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4836 15a94983 2018-12-23 stsp if (err)
4837 15a94983 2018-12-23 stsp goto done;
4838 15a94983 2018-12-23 stsp
4839 15a94983 2018-12-23 stsp switch (obj_type) {
4840 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4841 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4842 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4843 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4844 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4845 53d03f97 2023-01-10 thomas s->f);
4846 26ed57b2 2018-05-19 stsp break;
4847 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4848 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4849 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4850 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4851 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
4852 26ed57b2 2018-05-19 stsp break;
4853 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4854 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4855 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4856 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4857 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4858 be97ab03 2023-01-19 thomas size_t nlines = 0;
4859 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
4860 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
4861 be97ab03 2023-01-19 thomas &changed_paths,
4862 be97ab03 2023-01-19 thomas s->ignore_whitespace,
4863 be97ab03 2023-01-19 thomas s->force_text_diff,
4864 be97ab03 2023-01-19 thomas tog_diff_algo
4865 be97ab03 2023-01-19 thomas };
4866 abd2672a 2018-12-23 stsp
4867 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
4868 be97ab03 2023-01-19 thomas if (lines == NULL) {
4869 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
4870 be97ab03 2023-01-19 thomas goto done;
4871 be97ab03 2023-01-19 thomas }
4872 be97ab03 2023-01-19 thomas
4873 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
4874 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
4875 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4876 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4877 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4878 be97ab03 2023-01-19 thomas if (err)
4879 be97ab03 2023-01-19 thomas break;
4880 be97ab03 2023-01-19 thomas
4881 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4882 abd2672a 2018-12-23 stsp if (err)
4883 3ffacbe1 2020-02-02 tracey goto done;
4884 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4885 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4886 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4887 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4888 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
4889 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
4890 f44b1f58 2020-02-02 tracey if (err)
4891 f44b1f58 2020-02-02 tracey goto done;
4892 f44b1f58 2020-02-02 tracey } else {
4893 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4894 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4895 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4896 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4897 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4898 772fcad5 2023-01-07 thomas s->ignore_whitespace,
4899 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
4900 f44b1f58 2020-02-02 tracey if (err)
4901 f44b1f58 2020-02-02 tracey goto done;
4902 f5404e4e 2020-02-02 tracey break;
4903 15a087fe 2019-02-21 stsp }
4904 abd2672a 2018-12-23 stsp }
4905 abd2672a 2018-12-23 stsp }
4906 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4907 abd2672a 2018-12-23 stsp
4908 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4909 be97ab03 2023-01-19 thomas lines, nlines);
4910 26ed57b2 2018-05-19 stsp break;
4911 abd2672a 2018-12-23 stsp }
4912 26ed57b2 2018-05-19 stsp default:
4913 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4914 48ae06ee 2018-10-18 stsp break;
4915 26ed57b2 2018-05-19 stsp }
4916 48ae06ee 2018-10-18 stsp done:
4917 be97ab03 2023-01-19 thomas free(lines);
4918 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4919 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4920 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4921 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4922 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
4923 48ae06ee 2018-10-18 stsp return err;
4924 48ae06ee 2018-10-18 stsp }
4925 26ed57b2 2018-05-19 stsp
4926 f5215bb9 2019-02-22 stsp static void
4927 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4928 f5215bb9 2019-02-22 stsp {
4929 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4930 f5215bb9 2019-02-22 stsp update_panels();
4931 f5215bb9 2019-02-22 stsp doupdate();
4932 f44b1f58 2020-02-02 tracey }
4933 f44b1f58 2020-02-02 tracey
4934 f44b1f58 2020-02-02 tracey static const struct got_error *
4935 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4936 f44b1f58 2020-02-02 tracey {
4937 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4938 f44b1f58 2020-02-02 tracey
4939 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4940 f44b1f58 2020-02-02 tracey return NULL;
4941 f44b1f58 2020-02-02 tracey }
4942 f44b1f58 2020-02-02 tracey
4943 2a7d3cd7 2022-09-17 thomas static void
4944 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4945 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
4946 f44b1f58 2020-02-02 tracey {
4947 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
4948 fc2737d5 2022-09-15 thomas
4949 2a7d3cd7 2022-09-17 thomas *f = s->f;
4950 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
4951 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
4952 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
4953 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
4954 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
4955 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
4956 fc2737d5 2022-09-15 thomas }
4957 fc2737d5 2022-09-15 thomas
4958 fc2737d5 2022-09-15 thomas static const struct got_error *
4959 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
4960 fc2737d5 2022-09-15 thomas {
4961 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4962 fc2737d5 2022-09-15 thomas FILE *f;
4963 f44b1f58 2020-02-02 tracey int lineno;
4964 78eecffc 2022-06-23 thomas char *line = NULL;
4965 826082fe 2020-12-10 stsp size_t linesize = 0;
4966 826082fe 2020-12-10 stsp ssize_t linelen;
4967 fc2737d5 2022-09-15 thomas off_t *line_offsets;
4968 fc2737d5 2022-09-15 thomas size_t nlines = 0;
4969 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
4970 f44b1f58 2020-02-02 tracey
4971 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
4972 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
4973 2a7d3cd7 2022-09-17 thomas "view search not supported");
4974 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4975 fc2737d5 2022-09-15 thomas &match, &selected);
4976 fc2737d5 2022-09-15 thomas
4977 f44b1f58 2020-02-02 tracey if (!view->searching) {
4978 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4979 f44b1f58 2020-02-02 tracey return NULL;
4980 f44b1f58 2020-02-02 tracey }
4981 f44b1f58 2020-02-02 tracey
4982 fc2737d5 2022-09-15 thomas if (*match) {
4983 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4984 fc2737d5 2022-09-15 thomas lineno = *match + 1;
4985 f44b1f58 2020-02-02 tracey else
4986 fc2737d5 2022-09-15 thomas lineno = *match - 1;
4987 4b3f9dac 2021-12-17 thomas } else
4988 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
4989 f44b1f58 2020-02-02 tracey
4990 f44b1f58 2020-02-02 tracey while (1) {
4991 f44b1f58 2020-02-02 tracey off_t offset;
4992 f44b1f58 2020-02-02 tracey
4993 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
4994 fc2737d5 2022-09-15 thomas if (*match == 0) {
4995 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4996 f44b1f58 2020-02-02 tracey break;
4997 f44b1f58 2020-02-02 tracey }
4998 f44b1f58 2020-02-02 tracey
4999 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5000 f44b1f58 2020-02-02 tracey lineno = 1;
5001 f44b1f58 2020-02-02 tracey else
5002 fc2737d5 2022-09-15 thomas lineno = nlines;
5003 f44b1f58 2020-02-02 tracey }
5004 f44b1f58 2020-02-02 tracey
5005 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
5006 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
5007 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
5008 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
5009 f44b1f58 2020-02-02 tracey free(line);
5010 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5011 f44b1f58 2020-02-02 tracey }
5012 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5013 78eecffc 2022-06-23 thomas if (linelen != -1) {
5014 78eecffc 2022-06-23 thomas char *exstr;
5015 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5016 78eecffc 2022-06-23 thomas if (err)
5017 78eecffc 2022-06-23 thomas break;
5018 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5019 78eecffc 2022-06-23 thomas &view->regmatch)) {
5020 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5021 fc2737d5 2022-09-15 thomas *match = lineno;
5022 78eecffc 2022-06-23 thomas free(exstr);
5023 78eecffc 2022-06-23 thomas break;
5024 78eecffc 2022-06-23 thomas }
5025 78eecffc 2022-06-23 thomas free(exstr);
5026 f44b1f58 2020-02-02 tracey }
5027 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5028 f44b1f58 2020-02-02 tracey lineno++;
5029 f44b1f58 2020-02-02 tracey else
5030 f44b1f58 2020-02-02 tracey lineno--;
5031 f44b1f58 2020-02-02 tracey }
5032 826082fe 2020-12-10 stsp free(line);
5033 f44b1f58 2020-02-02 tracey
5034 fc2737d5 2022-09-15 thomas if (*match) {
5035 fc2737d5 2022-09-15 thomas *first = *match;
5036 fc2737d5 2022-09-15 thomas *selected = 1;
5037 f44b1f58 2020-02-02 tracey }
5038 f44b1f58 2020-02-02 tracey
5039 05171be4 2022-06-23 thomas return err;
5040 f5215bb9 2019-02-22 stsp }
5041 f5215bb9 2019-02-22 stsp
5042 48ae06ee 2018-10-18 stsp static const struct got_error *
5043 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5044 a0f32f33 2022-06-13 thomas {
5045 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5046 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5047 a0f32f33 2022-06-13 thomas
5048 a0f32f33 2022-06-13 thomas free(s->id1);
5049 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5050 a0f32f33 2022-06-13 thomas free(s->id2);
5051 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5052 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5053 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5054 a0f32f33 2022-06-13 thomas s->f = NULL;
5055 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5056 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5057 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5058 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5059 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5060 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5061 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5062 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5063 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5064 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5065 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5066 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5067 82c78e96 2022-08-06 thomas free(s->lines);
5068 82c78e96 2022-08-06 thomas s->lines = NULL;
5069 a0f32f33 2022-06-13 thomas s->nlines = 0;
5070 a0f32f33 2022-06-13 thomas return err;
5071 a0f32f33 2022-06-13 thomas }
5072 a0f32f33 2022-06-13 thomas
5073 a0f32f33 2022-06-13 thomas static const struct got_error *
5074 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5075 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5076 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5077 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5078 48ae06ee 2018-10-18 stsp {
5079 48ae06ee 2018-10-18 stsp const struct got_error *err;
5080 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5081 5dc9f4bc 2018-08-04 stsp
5082 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5083 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5084 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5085 a0f32f33 2022-06-13 thomas
5086 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5087 15a94983 2018-12-23 stsp int type1, type2;
5088 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5089 15a94983 2018-12-23 stsp if (err)
5090 15a94983 2018-12-23 stsp return err;
5091 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5092 15a94983 2018-12-23 stsp if (err)
5093 15a94983 2018-12-23 stsp return err;
5094 15a94983 2018-12-23 stsp
5095 15a94983 2018-12-23 stsp if (type1 != type2)
5096 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5097 15a94983 2018-12-23 stsp }
5098 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5099 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5100 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5101 f44b1f58 2020-02-02 tracey s->repo = repo;
5102 f44b1f58 2020-02-02 tracey s->id1 = id1;
5103 f44b1f58 2020-02-02 tracey s->id2 = id2;
5104 3dbaef42 2020-11-24 stsp s->label1 = label1;
5105 3dbaef42 2020-11-24 stsp s->label2 = label2;
5106 48ae06ee 2018-10-18 stsp
5107 15a94983 2018-12-23 stsp if (id1) {
5108 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5109 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5110 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5111 48ae06ee 2018-10-18 stsp } else
5112 5465d566 2020-02-01 tracey s->id1 = NULL;
5113 48ae06ee 2018-10-18 stsp
5114 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5115 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5116 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5117 a0f32f33 2022-06-13 thomas goto done;
5118 48ae06ee 2018-10-18 stsp }
5119 a0f32f33 2022-06-13 thomas
5120 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5121 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5122 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5123 9a1d7435 2022-06-23 thomas goto done;
5124 9a1d7435 2022-06-23 thomas }
5125 9a1d7435 2022-06-23 thomas
5126 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5127 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5128 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5129 a0f32f33 2022-06-13 thomas goto done;
5130 a0f32f33 2022-06-13 thomas }
5131 a0f32f33 2022-06-13 thomas
5132 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5133 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5134 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5135 19a6a6b5 2022-07-01 thomas goto done;
5136 19a6a6b5 2022-07-01 thomas }
5137 19a6a6b5 2022-07-01 thomas
5138 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5139 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5140 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5141 19a6a6b5 2022-07-01 thomas goto done;
5142 19a6a6b5 2022-07-01 thomas }
5143 19a6a6b5 2022-07-01 thomas
5144 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5145 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5146 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5147 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5148 5465d566 2020-02-01 tracey s->repo = repo;
5149 6d17833f 2019-11-08 stsp
5150 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5151 6568f0aa 2022-08-06 thomas int rc;
5152 6d17833f 2019-11-08 stsp
5153 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5154 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5155 6568f0aa 2022-08-06 thomas if (rc != ERR)
5156 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5157 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5158 6568f0aa 2022-08-06 thomas if (rc != ERR)
5159 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5160 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5161 6568f0aa 2022-08-06 thomas if (rc != ERR)
5162 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5163 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5164 6568f0aa 2022-08-06 thomas if (rc != ERR)
5165 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5166 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5167 6568f0aa 2022-08-06 thomas if (rc != ERR)
5168 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5169 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5170 6568f0aa 2022-08-06 thomas if (rc != ERR)
5171 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5172 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5173 6568f0aa 2022-08-06 thomas if (rc != ERR)
5174 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5175 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5176 6568f0aa 2022-08-06 thomas if (rc != ERR)
5177 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5178 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5179 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5180 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5181 a0f32f33 2022-06-13 thomas goto done;
5182 6568f0aa 2022-08-06 thomas }
5183 6d17833f 2019-11-08 stsp }
5184 5dc9f4bc 2018-08-04 stsp
5185 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5186 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5187 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5188 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5189 f5215bb9 2019-02-22 stsp
5190 5465d566 2020-02-01 tracey err = create_diff(s);
5191 48ae06ee 2018-10-18 stsp
5192 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5193 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5194 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5195 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5196 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5197 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5198 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5199 a0f32f33 2022-06-13 thomas done:
5200 a0f32f33 2022-06-13 thomas if (err)
5201 a0f32f33 2022-06-13 thomas close_diff_view(view);
5202 e5a0f69f 2018-08-18 stsp return err;
5203 5dc9f4bc 2018-08-04 stsp }
5204 5dc9f4bc 2018-08-04 stsp
5205 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5206 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5207 5dc9f4bc 2018-08-04 stsp {
5208 a3404814 2018-09-02 stsp const struct got_error *err;
5209 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5210 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5211 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5212 a3404814 2018-09-02 stsp
5213 a3404814 2018-09-02 stsp if (s->id1) {
5214 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5215 a3404814 2018-09-02 stsp if (err)
5216 a3404814 2018-09-02 stsp return err;
5217 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5218 3dbaef42 2020-11-24 stsp } else
5219 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5220 3dbaef42 2020-11-24 stsp
5221 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5222 a3404814 2018-09-02 stsp if (err)
5223 a3404814 2018-09-02 stsp return err;
5224 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5225 26ed57b2 2018-05-19 stsp
5226 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5227 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5228 a3404814 2018-09-02 stsp free(id_str1);
5229 a3404814 2018-09-02 stsp free(id_str2);
5230 a3404814 2018-09-02 stsp return err;
5231 a3404814 2018-09-02 stsp }
5232 a3404814 2018-09-02 stsp free(id_str1);
5233 a3404814 2018-09-02 stsp free(id_str2);
5234 a3404814 2018-09-02 stsp
5235 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5236 267bb3b8 2021-08-01 stsp free(header);
5237 267bb3b8 2021-08-01 stsp return err;
5238 15a087fe 2019-02-21 stsp }
5239 15a087fe 2019-02-21 stsp
5240 15a087fe 2019-02-21 stsp static const struct got_error *
5241 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5242 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5243 15a087fe 2019-02-21 stsp {
5244 d7a04538 2019-02-21 stsp const struct got_error *err;
5245 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5246 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5247 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5248 15a087fe 2019-02-21 stsp
5249 15a087fe 2019-02-21 stsp free(s->id2);
5250 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5251 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5252 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5253 15a087fe 2019-02-21 stsp
5254 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5255 d7a04538 2019-02-21 stsp if (err)
5256 d7a04538 2019-02-21 stsp return err;
5257 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5258 15a087fe 2019-02-21 stsp free(s->id1);
5259 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5260 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5261 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5262 15a087fe 2019-02-21 stsp return NULL;
5263 0cf4efb1 2018-09-29 stsp }
5264 0cf4efb1 2018-09-29 stsp
5265 0cf4efb1 2018-09-29 stsp static const struct got_error *
5266 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5267 adf4c9e0 2022-07-03 thomas {
5268 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5269 adf4c9e0 2022-07-03 thomas
5270 adf4c9e0 2022-07-03 thomas view->count = 0;
5271 adf4c9e0 2022-07-03 thomas wclear(view->window);
5272 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5273 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5274 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5275 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5276 adf4c9e0 2022-07-03 thomas return create_diff(s);
5277 82c78e96 2022-08-06 thomas }
5278 82c78e96 2022-08-06 thomas
5279 82c78e96 2022-08-06 thomas static void
5280 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5281 82c78e96 2022-08-06 thomas {
5282 82c78e96 2022-08-06 thomas int start, i;
5283 82c78e96 2022-08-06 thomas
5284 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5285 82c78e96 2022-08-06 thomas
5286 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5287 82c78e96 2022-08-06 thomas if (i == 0)
5288 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5289 82c78e96 2022-08-06 thomas if (--i == start)
5290 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5291 82c78e96 2022-08-06 thomas }
5292 82c78e96 2022-08-06 thomas
5293 82c78e96 2022-08-06 thomas s->selected_line = 1;
5294 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5295 adf4c9e0 2022-07-03 thomas }
5296 adf4c9e0 2022-07-03 thomas
5297 82c78e96 2022-08-06 thomas static void
5298 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5299 82c78e96 2022-08-06 thomas {
5300 82c78e96 2022-08-06 thomas int start, i;
5301 82c78e96 2022-08-06 thomas
5302 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5303 82c78e96 2022-08-06 thomas
5304 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5305 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5306 82c78e96 2022-08-06 thomas i = 0;
5307 82c78e96 2022-08-06 thomas if (++i == start)
5308 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5309 82c78e96 2022-08-06 thomas }
5310 82c78e96 2022-08-06 thomas
5311 82c78e96 2022-08-06 thomas s->selected_line = 1;
5312 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5313 82c78e96 2022-08-06 thomas }
5314 82c78e96 2022-08-06 thomas
5315 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5316 4fc71f3b 2022-07-12 thomas int, int, int);
5317 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5318 4fc71f3b 2022-07-12 thomas int, int);
5319 4fc71f3b 2022-07-12 thomas
5320 adf4c9e0 2022-07-03 thomas static const struct got_error *
5321 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5322 e5a0f69f 2018-08-18 stsp {
5323 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5324 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5325 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5326 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5327 826082fe 2020-12-10 stsp char *line = NULL;
5328 826082fe 2020-12-10 stsp size_t linesize = 0;
5329 826082fe 2020-12-10 stsp ssize_t linelen;
5330 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5331 e5a0f69f 2018-08-18 stsp
5332 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5333 82c78e96 2022-08-06 thomas
5334 e5a0f69f 2018-08-18 stsp switch (ch) {
5335 05171be4 2022-06-23 thomas case '0':
5336 05171be4 2022-06-23 thomas case '$':
5337 05171be4 2022-06-23 thomas case KEY_RIGHT:
5338 05171be4 2022-06-23 thomas case 'l':
5339 05171be4 2022-06-23 thomas case KEY_LEFT:
5340 05171be4 2022-06-23 thomas case 'h':
5341 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5342 05171be4 2022-06-23 thomas break;
5343 64453f7e 2020-11-21 stsp case 'a':
5344 3dbaef42 2020-11-24 stsp case 'w':
5345 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5346 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5347 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5348 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5349 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5350 f2d06bef 2023-01-23 thomas }
5351 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5352 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5353 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5354 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5355 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5356 f2d06bef 2023-01-23 thomas }
5357 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5358 912a3f79 2021-08-30 j break;
5359 912a3f79 2021-08-30 j case 'g':
5360 912a3f79 2021-08-30 j case KEY_HOME:
5361 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5362 07b0611c 2022-06-23 thomas view->count = 0;
5363 64453f7e 2020-11-21 stsp break;
5364 912a3f79 2021-08-30 j case 'G':
5365 912a3f79 2021-08-30 j case KEY_END:
5366 07b0611c 2022-06-23 thomas view->count = 0;
5367 912a3f79 2021-08-30 j if (s->eof)
5368 912a3f79 2021-08-30 j break;
5369 912a3f79 2021-08-30 j
5370 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5371 912a3f79 2021-08-30 j s->eof = 1;
5372 912a3f79 2021-08-30 j break;
5373 1e37a5c2 2019-05-12 jcs case 'k':
5374 1e37a5c2 2019-05-12 jcs case KEY_UP:
5375 f7140bf5 2021-10-17 thomas case CTRL('p'):
5376 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5377 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5378 07b0611c 2022-06-23 thomas else
5379 07b0611c 2022-06-23 thomas view->count = 0;
5380 1e37a5c2 2019-05-12 jcs break;
5381 70f17a53 2022-06-13 thomas case CTRL('u'):
5382 23427b14 2022-06-23 thomas case 'u':
5383 70f17a53 2022-06-13 thomas nscroll /= 2;
5384 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5385 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5386 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5387 1c5e5faa 2022-06-23 thomas case 'b':
5388 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5389 07b0611c 2022-06-23 thomas view->count = 0;
5390 26ed57b2 2018-05-19 stsp break;
5391 07b0611c 2022-06-23 thomas }
5392 1e37a5c2 2019-05-12 jcs i = 0;
5393 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5394 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5395 1e37a5c2 2019-05-12 jcs break;
5396 1e37a5c2 2019-05-12 jcs case 'j':
5397 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5398 f7140bf5 2021-10-17 thomas case CTRL('n'):
5399 1e37a5c2 2019-05-12 jcs if (!s->eof)
5400 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5401 07b0611c 2022-06-23 thomas else
5402 07b0611c 2022-06-23 thomas view->count = 0;
5403 1e37a5c2 2019-05-12 jcs break;
5404 70f17a53 2022-06-13 thomas case CTRL('d'):
5405 23427b14 2022-06-23 thomas case 'd':
5406 70f17a53 2022-06-13 thomas nscroll /= 2;
5407 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5408 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5409 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5410 1c5e5faa 2022-06-23 thomas case 'f':
5411 1e37a5c2 2019-05-12 jcs case ' ':
5412 07b0611c 2022-06-23 thomas if (s->eof) {
5413 07b0611c 2022-06-23 thomas view->count = 0;
5414 1e37a5c2 2019-05-12 jcs break;
5415 07b0611c 2022-06-23 thomas }
5416 1e37a5c2 2019-05-12 jcs i = 0;
5417 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5418 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5419 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5420 826082fe 2020-12-10 stsp if (linelen == -1) {
5421 826082fe 2020-12-10 stsp if (feof(s->f)) {
5422 826082fe 2020-12-10 stsp s->eof = 1;
5423 826082fe 2020-12-10 stsp } else
5424 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5425 34bc9ec9 2019-02-22 stsp break;
5426 826082fe 2020-12-10 stsp }
5427 1e37a5c2 2019-05-12 jcs }
5428 826082fe 2020-12-10 stsp free(line);
5429 1e37a5c2 2019-05-12 jcs break;
5430 82c78e96 2022-08-06 thomas case '(':
5431 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5432 82c78e96 2022-08-06 thomas break;
5433 82c78e96 2022-08-06 thomas case ')':
5434 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5435 82c78e96 2022-08-06 thomas break;
5436 82c78e96 2022-08-06 thomas case '{':
5437 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5438 82c78e96 2022-08-06 thomas break;
5439 82c78e96 2022-08-06 thomas case '}':
5440 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5441 82c78e96 2022-08-06 thomas break;
5442 1e37a5c2 2019-05-12 jcs case '[':
5443 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5444 1e37a5c2 2019-05-12 jcs s->diff_context--;
5445 aa61903a 2021-12-31 thomas s->matched_line = 0;
5446 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5447 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5448 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5449 27829c9e 2020-11-21 stsp s->nlines) {
5450 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5451 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5452 27829c9e 2020-11-21 stsp }
5453 07b0611c 2022-06-23 thomas } else
5454 07b0611c 2022-06-23 thomas view->count = 0;
5455 1e37a5c2 2019-05-12 jcs break;
5456 1e37a5c2 2019-05-12 jcs case ']':
5457 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5458 1e37a5c2 2019-05-12 jcs s->diff_context++;
5459 aa61903a 2021-12-31 thomas s->matched_line = 0;
5460 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5461 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5462 07b0611c 2022-06-23 thomas } else
5463 07b0611c 2022-06-23 thomas view->count = 0;
5464 1e37a5c2 2019-05-12 jcs break;
5465 1e37a5c2 2019-05-12 jcs case '<':
5466 1e37a5c2 2019-05-12 jcs case ',':
5467 777aae21 2022-07-20 thomas case 'K':
5468 4fc71f3b 2022-07-12 thomas up = 1;
5469 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5470 4fc71f3b 2022-07-12 thomas case '>':
5471 4fc71f3b 2022-07-12 thomas case '.':
5472 777aae21 2022-07-20 thomas case 'J':
5473 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5474 07b0611c 2022-06-23 thomas view->count = 0;
5475 48ae06ee 2018-10-18 stsp break;
5476 07b0611c 2022-06-23 thomas }
5477 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5478 6524637e 2019-02-21 stsp
5479 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5480 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5481 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5482 15a087fe 2019-02-21 stsp
5483 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5484 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5485 4fc71f3b 2022-07-12 thomas if (err)
5486 4fc71f3b 2022-07-12 thomas break;
5487 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5488 15a087fe 2019-02-21 stsp
5489 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5490 4fc71f3b 2022-07-12 thomas break;
5491 15a087fe 2019-02-21 stsp
5492 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5493 4fc71f3b 2022-07-12 thomas if (err)
5494 4fc71f3b 2022-07-12 thomas break;
5495 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5496 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5497 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5498 5e224a3e 2019-02-22 stsp
5499 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5500 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5501 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5502 4fc71f3b 2022-07-12 thomas
5503 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5504 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5505 4fc71f3b 2022-07-12 thomas if (err)
5506 4fc71f3b 2022-07-12 thomas break;
5507 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5508 5a8b5076 2020-12-05 stsp
5509 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5510 4fc71f3b 2022-07-12 thomas break;
5511 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5512 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5513 4fc71f3b 2022-07-12 thomas bs->selected_line);
5514 4fc71f3b 2022-07-12 thomas if (id == NULL)
5515 4fc71f3b 2022-07-12 thomas break;
5516 15a087fe 2019-02-21 stsp
5517 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5518 4fc71f3b 2022-07-12 thomas break;
5519 15a087fe 2019-02-21 stsp
5520 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5521 4fc71f3b 2022-07-12 thomas if (err)
5522 4fc71f3b 2022-07-12 thomas break;
5523 4fc71f3b 2022-07-12 thomas }
5524 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5525 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5526 aa61903a 2021-12-31 thomas s->matched_line = 0;
5527 05171be4 2022-06-23 thomas view->x = 0;
5528 1e37a5c2 2019-05-12 jcs
5529 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5530 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5531 1e37a5c2 2019-05-12 jcs break;
5532 1e37a5c2 2019-05-12 jcs default:
5533 07b0611c 2022-06-23 thomas view->count = 0;
5534 1e37a5c2 2019-05-12 jcs break;
5535 26ed57b2 2018-05-19 stsp }
5536 e5a0f69f 2018-08-18 stsp
5537 bcbd79e2 2018-08-19 stsp return err;
5538 26ed57b2 2018-05-19 stsp }
5539 26ed57b2 2018-05-19 stsp
5540 4ed7e80c 2018-05-20 stsp static const struct got_error *
5541 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5542 9f7d7167 2018-04-29 stsp {
5543 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5544 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5545 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5546 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5547 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5548 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5549 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5550 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5551 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5552 3dbaef42 2020-11-24 stsp const char *errstr;
5553 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5554 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5555 70ac5f84 2019-03-28 stsp
5556 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5557 26ed57b2 2018-05-19 stsp switch (ch) {
5558 64453f7e 2020-11-21 stsp case 'a':
5559 64453f7e 2020-11-21 stsp force_text_diff = 1;
5560 3dbaef42 2020-11-24 stsp break;
5561 3dbaef42 2020-11-24 stsp case 'C':
5562 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5563 3dbaef42 2020-11-24 stsp &errstr);
5564 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5565 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5566 8f666e67 2022-02-12 thomas errstr, errstr);
5567 64453f7e 2020-11-21 stsp break;
5568 09b5bff8 2020-02-23 naddy case 'r':
5569 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5570 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5571 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5572 09b5bff8 2020-02-23 naddy optarg);
5573 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5574 09b5bff8 2020-02-23 naddy break;
5575 3dbaef42 2020-11-24 stsp case 'w':
5576 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5577 3dbaef42 2020-11-24 stsp break;
5578 26ed57b2 2018-05-19 stsp default:
5579 17020d27 2019-03-07 stsp usage_diff();
5580 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5581 26ed57b2 2018-05-19 stsp }
5582 26ed57b2 2018-05-19 stsp }
5583 26ed57b2 2018-05-19 stsp
5584 26ed57b2 2018-05-19 stsp argc -= optind;
5585 26ed57b2 2018-05-19 stsp argv += optind;
5586 26ed57b2 2018-05-19 stsp
5587 26ed57b2 2018-05-19 stsp if (argc == 0) {
5588 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5589 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5590 15a94983 2018-12-23 stsp id_str1 = argv[0];
5591 15a94983 2018-12-23 stsp id_str2 = argv[1];
5592 26ed57b2 2018-05-19 stsp } else
5593 26ed57b2 2018-05-19 stsp usage_diff();
5594 eb6600df 2019-01-04 stsp
5595 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5596 7cd52833 2022-06-23 thomas if (error)
5597 7cd52833 2022-06-23 thomas goto done;
5598 7cd52833 2022-06-23 thomas
5599 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5600 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5601 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5602 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5603 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5604 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5605 c156c7a4 2020-12-18 stsp goto done;
5606 a273ac94 2020-02-23 naddy if (worktree)
5607 a273ac94 2020-02-23 naddy repo_path =
5608 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5609 a273ac94 2020-02-23 naddy else
5610 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5611 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5612 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5613 c156c7a4 2020-12-18 stsp goto done;
5614 c156c7a4 2020-12-18 stsp }
5615 a273ac94 2020-02-23 naddy }
5616 a273ac94 2020-02-23 naddy
5617 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5618 eb6600df 2019-01-04 stsp if (error)
5619 eb6600df 2019-01-04 stsp goto done;
5620 26ed57b2 2018-05-19 stsp
5621 a273ac94 2020-02-23 naddy init_curses();
5622 a273ac94 2020-02-23 naddy
5623 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5624 51a10b52 2020-12-26 stsp if (error)
5625 51a10b52 2020-12-26 stsp goto done;
5626 51a10b52 2020-12-26 stsp
5627 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5628 26ed57b2 2018-05-19 stsp if (error)
5629 26ed57b2 2018-05-19 stsp goto done;
5630 26ed57b2 2018-05-19 stsp
5631 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5632 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5633 26ed57b2 2018-05-19 stsp if (error)
5634 26ed57b2 2018-05-19 stsp goto done;
5635 26ed57b2 2018-05-19 stsp
5636 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5637 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5638 26ed57b2 2018-05-19 stsp if (error)
5639 26ed57b2 2018-05-19 stsp goto done;
5640 26ed57b2 2018-05-19 stsp
5641 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5642 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5643 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5644 ea5e7bb5 2018-08-01 stsp goto done;
5645 ea5e7bb5 2018-08-01 stsp }
5646 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5647 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5648 5dc9f4bc 2018-08-04 stsp if (error)
5649 5dc9f4bc 2018-08-04 stsp goto done;
5650 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5651 26ed57b2 2018-05-19 stsp done:
5652 3dbaef42 2020-11-24 stsp free(label1);
5653 3dbaef42 2020-11-24 stsp free(label2);
5654 c02c541e 2019-03-29 stsp free(repo_path);
5655 a273ac94 2020-02-23 naddy free(cwd);
5656 1d0f4054 2021-06-17 stsp if (repo) {
5657 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5658 1d0f4054 2021-06-17 stsp if (error == NULL)
5659 1d0f4054 2021-06-17 stsp error = close_err;
5660 1d0f4054 2021-06-17 stsp }
5661 a273ac94 2020-02-23 naddy if (worktree)
5662 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5663 7cd52833 2022-06-23 thomas if (pack_fds) {
5664 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5665 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5666 7cd52833 2022-06-23 thomas if (error == NULL)
5667 7cd52833 2022-06-23 thomas error = pack_err;
5668 7cd52833 2022-06-23 thomas }
5669 51a10b52 2020-12-26 stsp tog_free_refs();
5670 26ed57b2 2018-05-19 stsp return error;
5671 9f7d7167 2018-04-29 stsp }
5672 9f7d7167 2018-04-29 stsp
5673 4ed7e80c 2018-05-20 stsp __dead static void
5674 9f7d7167 2018-04-29 stsp usage_blame(void)
5675 9f7d7167 2018-04-29 stsp {
5676 80ddbec8 2018-04-29 stsp endwin();
5677 91198554 2022-06-23 thomas fprintf(stderr,
5678 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5679 9f7d7167 2018-04-29 stsp getprogname());
5680 9f7d7167 2018-04-29 stsp exit(1);
5681 9f7d7167 2018-04-29 stsp }
5682 84451b3e 2018-07-10 stsp
5683 84451b3e 2018-07-10 stsp struct tog_blame_line {
5684 84451b3e 2018-07-10 stsp int annotated;
5685 84451b3e 2018-07-10 stsp struct got_object_id *id;
5686 84451b3e 2018-07-10 stsp };
5687 9f7d7167 2018-04-29 stsp
5688 4ed7e80c 2018-05-20 stsp static const struct got_error *
5689 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5690 84451b3e 2018-07-10 stsp {
5691 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5692 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5693 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5694 84451b3e 2018-07-10 stsp const struct got_error *err;
5695 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5696 826082fe 2020-12-10 stsp char *line = NULL;
5697 826082fe 2020-12-10 stsp size_t linesize = 0;
5698 826082fe 2020-12-10 stsp ssize_t linelen;
5699 84451b3e 2018-07-10 stsp wchar_t *wline;
5700 27a741e5 2019-09-11 stsp int width;
5701 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5702 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5703 ab089a2a 2018-07-12 stsp char *id_str;
5704 11b20872 2019-11-08 stsp struct tog_color *tc;
5705 ab089a2a 2018-07-12 stsp
5706 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5707 ab089a2a 2018-07-12 stsp if (err)
5708 ab089a2a 2018-07-12 stsp return err;
5709 84451b3e 2018-07-10 stsp
5710 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5711 f7d12f7e 2018-08-01 stsp werase(view->window);
5712 84451b3e 2018-07-10 stsp
5713 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5714 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5715 ab089a2a 2018-07-12 stsp free(id_str);
5716 ab089a2a 2018-07-12 stsp return err;
5717 ab089a2a 2018-07-12 stsp }
5718 ab089a2a 2018-07-12 stsp
5719 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5720 ab089a2a 2018-07-12 stsp free(line);
5721 2550e4c3 2018-07-13 stsp line = NULL;
5722 1cae65b4 2019-09-22 stsp if (err)
5723 1cae65b4 2019-09-22 stsp return err;
5724 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5725 a3404814 2018-09-02 stsp wstandout(view->window);
5726 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5727 11b20872 2019-11-08 stsp if (tc)
5728 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5729 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5730 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5731 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5732 11b20872 2019-11-08 stsp if (tc)
5733 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5734 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5735 a3404814 2018-09-02 stsp wstandend(view->window);
5736 2550e4c3 2018-07-13 stsp free(wline);
5737 2550e4c3 2018-07-13 stsp wline = NULL;
5738 ab089a2a 2018-07-12 stsp
5739 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5740 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5741 07dd3ed3 2022-08-06 thomas
5742 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5743 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5744 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5745 ab089a2a 2018-07-12 stsp free(id_str);
5746 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5747 ab089a2a 2018-07-12 stsp }
5748 ab089a2a 2018-07-12 stsp free(id_str);
5749 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5750 3f60a8ef 2018-07-10 stsp free(line);
5751 2550e4c3 2018-07-13 stsp line = NULL;
5752 3f60a8ef 2018-07-10 stsp if (err)
5753 3f60a8ef 2018-07-10 stsp return err;
5754 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5755 2550e4c3 2018-07-13 stsp free(wline);
5756 2550e4c3 2018-07-13 stsp wline = NULL;
5757 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5758 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5759 3f60a8ef 2018-07-10 stsp
5760 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5761 05171be4 2022-06-23 thomas view->maxx = 0;
5762 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5763 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5764 826082fe 2020-12-10 stsp if (linelen == -1) {
5765 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5766 826082fe 2020-12-10 stsp s->eof = 1;
5767 826082fe 2020-12-10 stsp break;
5768 826082fe 2020-12-10 stsp }
5769 84451b3e 2018-07-10 stsp free(line);
5770 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5771 84451b3e 2018-07-10 stsp }
5772 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5773 07dd3ed3 2022-08-06 thomas continue;
5774 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5775 826082fe 2020-12-10 stsp continue;
5776 cbea3800 2022-06-23 thomas
5777 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5778 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5779 cbea3800 2022-06-23 thomas if (err) {
5780 cbea3800 2022-06-23 thomas free(line);
5781 cbea3800 2022-06-23 thomas return err;
5782 cbea3800 2022-06-23 thomas }
5783 cbea3800 2022-06-23 thomas free(wline);
5784 cbea3800 2022-06-23 thomas wline = NULL;
5785 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5786 84451b3e 2018-07-10 stsp
5787 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5788 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5789 b700b5d6 2018-07-10 stsp
5790 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5791 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5792 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5793 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5794 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5795 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5796 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5797 8d0fe45a 2019-08-12 stsp char *id_str;
5798 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5799 91198554 2022-06-23 thomas blame_line->id);
5800 8d0fe45a 2019-08-12 stsp if (err) {
5801 8d0fe45a 2019-08-12 stsp free(line);
5802 8d0fe45a 2019-08-12 stsp return err;
5803 8d0fe45a 2019-08-12 stsp }
5804 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5805 11b20872 2019-11-08 stsp if (tc)
5806 11b20872 2019-11-08 stsp wattr_on(view->window,
5807 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5808 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5809 11b20872 2019-11-08 stsp if (tc)
5810 11b20872 2019-11-08 stsp wattr_off(view->window,
5811 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5812 8d0fe45a 2019-08-12 stsp free(id_str);
5813 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5814 8d0fe45a 2019-08-12 stsp } else {
5815 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5816 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5817 84451b3e 2018-07-10 stsp }
5818 ee41ec32 2018-07-10 stsp } else {
5819 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5820 ee41ec32 2018-07-10 stsp prev_id = NULL;
5821 ee41ec32 2018-07-10 stsp }
5822 84451b3e 2018-07-10 stsp
5823 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5824 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5825 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5826 27a741e5 2019-09-11 stsp
5827 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5828 41605754 2020-11-12 stsp width = 9;
5829 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5830 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5831 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5832 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5833 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5834 41605754 2020-11-12 stsp if (err) {
5835 41605754 2020-11-12 stsp free(line);
5836 41605754 2020-11-12 stsp return err;
5837 41605754 2020-11-12 stsp }
5838 41605754 2020-11-12 stsp width += 9;
5839 41605754 2020-11-12 stsp } else {
5840 923086fd 2022-06-23 thomas int skip;
5841 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5842 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5843 923086fd 2022-06-23 thomas if (err) {
5844 923086fd 2022-06-23 thomas free(line);
5845 923086fd 2022-06-23 thomas return err;
5846 05171be4 2022-06-23 thomas }
5847 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5848 02bce7e2 2022-06-23 thomas width += 9;
5849 41605754 2020-11-12 stsp free(wline);
5850 41605754 2020-11-12 stsp wline = NULL;
5851 41605754 2020-11-12 stsp }
5852 41605754 2020-11-12 stsp
5853 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5854 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5855 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5856 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5857 84451b3e 2018-07-10 stsp }
5858 826082fe 2020-12-10 stsp free(line);
5859 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5860 84451b3e 2018-07-10 stsp
5861 a5d43cac 2022-07-01 thomas view_border(view);
5862 84451b3e 2018-07-10 stsp
5863 84451b3e 2018-07-10 stsp return NULL;
5864 84451b3e 2018-07-10 stsp }
5865 84451b3e 2018-07-10 stsp
5866 84451b3e 2018-07-10 stsp static const struct got_error *
5867 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5868 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5869 84451b3e 2018-07-10 stsp {
5870 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5871 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5872 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5873 1a76625f 2018-10-22 stsp int errcode;
5874 84451b3e 2018-07-10 stsp
5875 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5876 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5877 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5878 84451b3e 2018-07-10 stsp
5879 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5880 1a76625f 2018-10-22 stsp if (errcode)
5881 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5882 84451b3e 2018-07-10 stsp
5883 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5884 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5885 d68a0a7d 2018-07-10 stsp goto done;
5886 d68a0a7d 2018-07-10 stsp }
5887 d68a0a7d 2018-07-10 stsp
5888 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5889 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5890 d68a0a7d 2018-07-10 stsp
5891 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5892 d68a0a7d 2018-07-10 stsp if (line->annotated)
5893 d68a0a7d 2018-07-10 stsp goto done;
5894 d68a0a7d 2018-07-10 stsp
5895 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5896 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5897 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5898 84451b3e 2018-07-10 stsp goto done;
5899 84451b3e 2018-07-10 stsp }
5900 84451b3e 2018-07-10 stsp line->annotated = 1;
5901 84451b3e 2018-07-10 stsp done:
5902 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5903 1a76625f 2018-10-22 stsp if (errcode)
5904 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5905 84451b3e 2018-07-10 stsp return err;
5906 84451b3e 2018-07-10 stsp }
5907 84451b3e 2018-07-10 stsp
5908 84451b3e 2018-07-10 stsp static void *
5909 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5910 84451b3e 2018-07-10 stsp {
5911 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5912 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5913 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5914 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5915 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5916 00a8878e 2022-07-01 thomas
5917 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5918 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5919 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5920 9117a7b7 2022-07-01 thomas
5921 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5922 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5923 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5924 9117a7b7 2022-07-01 thomas goto done;
5925 9117a7b7 2022-07-01 thomas }
5926 18430de3 2018-07-10 stsp
5927 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5928 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5929 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5930 9117a7b7 2022-07-01 thomas goto done;
5931 9117a7b7 2022-07-01 thomas }
5932 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5933 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5934 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5935 9117a7b7 2022-07-01 thomas goto done;
5936 9117a7b7 2022-07-01 thomas }
5937 9117a7b7 2022-07-01 thomas
5938 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5939 61266923 2020-01-14 stsp if (err)
5940 9117a7b7 2022-07-01 thomas goto done;
5941 61266923 2020-01-14 stsp
5942 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5943 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5944 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5945 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5946 fc06ba56 2019-08-22 stsp err = NULL;
5947 18430de3 2018-07-10 stsp
5948 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5949 9117a7b7 2022-07-01 thomas if (errcode) {
5950 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5951 9117a7b7 2022-07-01 thomas goto done;
5952 9117a7b7 2022-07-01 thomas }
5953 18430de3 2018-07-10 stsp
5954 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5955 1d0f4054 2021-06-17 stsp if (err == NULL)
5956 1d0f4054 2021-06-17 stsp err = close_err;
5957 c9beca56 2018-07-22 stsp ta->repo = NULL;
5958 c9beca56 2018-07-22 stsp *ta->complete = 1;
5959 18430de3 2018-07-10 stsp
5960 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5961 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5962 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5963 18430de3 2018-07-10 stsp
5964 9117a7b7 2022-07-01 thomas done:
5965 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5966 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5967 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5968 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5969 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5970 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5971 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5972 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5973 00a8878e 2022-07-01 thomas
5974 18430de3 2018-07-10 stsp return (void *)err;
5975 84451b3e 2018-07-10 stsp }
5976 84451b3e 2018-07-10 stsp
5977 245d91c1 2018-07-12 stsp static struct got_object_id *
5978 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5979 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5980 245d91c1 2018-07-12 stsp {
5981 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5982 8d0fe45a 2019-08-12 stsp
5983 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5984 8d0fe45a 2019-08-12 stsp return NULL;
5985 b880a918 2018-07-10 stsp
5986 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5987 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5988 4fc71f3b 2022-07-12 thomas return NULL;
5989 4fc71f3b 2022-07-12 thomas
5990 4fc71f3b 2022-07-12 thomas return line->id;
5991 4fc71f3b 2022-07-12 thomas }
5992 4fc71f3b 2022-07-12 thomas
5993 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5994 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5995 4fc71f3b 2022-07-12 thomas int lineno)
5996 4fc71f3b 2022-07-12 thomas {
5997 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5998 4fc71f3b 2022-07-12 thomas
5999 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
6000 4fc71f3b 2022-07-12 thomas return NULL;
6001 4fc71f3b 2022-07-12 thomas
6002 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
6003 245d91c1 2018-07-12 stsp if (!line->annotated)
6004 245d91c1 2018-07-12 stsp return NULL;
6005 245d91c1 2018-07-12 stsp
6006 245d91c1 2018-07-12 stsp return line->id;
6007 b880a918 2018-07-10 stsp }
6008 245d91c1 2018-07-12 stsp
6009 b880a918 2018-07-10 stsp static const struct got_error *
6010 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6011 a70480e0 2018-06-23 stsp {
6012 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6013 245d91c1 2018-07-12 stsp int i;
6014 245d91c1 2018-07-12 stsp
6015 245d91c1 2018-07-12 stsp if (blame->thread) {
6016 1a76625f 2018-10-22 stsp int errcode;
6017 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6018 1a76625f 2018-10-22 stsp if (errcode)
6019 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6020 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6021 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6022 1a76625f 2018-10-22 stsp if (errcode)
6023 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6024 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6025 1a76625f 2018-10-22 stsp if (errcode)
6026 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6027 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6028 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6029 245d91c1 2018-07-12 stsp err = NULL;
6030 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6031 245d91c1 2018-07-12 stsp }
6032 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6033 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6034 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6035 1d0f4054 2021-06-17 stsp if (err == NULL)
6036 1d0f4054 2021-06-17 stsp err = close_err;
6037 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6038 245d91c1 2018-07-12 stsp }
6039 245d91c1 2018-07-12 stsp if (blame->f) {
6040 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6041 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6042 245d91c1 2018-07-12 stsp blame->f = NULL;
6043 245d91c1 2018-07-12 stsp }
6044 57670559 2018-12-23 stsp if (blame->lines) {
6045 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6046 57670559 2018-12-23 stsp free(blame->lines[i].id);
6047 57670559 2018-12-23 stsp free(blame->lines);
6048 57670559 2018-12-23 stsp blame->lines = NULL;
6049 57670559 2018-12-23 stsp }
6050 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6051 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6052 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6053 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6054 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6055 7cd52833 2022-06-23 thomas if (err == NULL)
6056 7cd52833 2022-06-23 thomas err = pack_err;
6057 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6058 7cd52833 2022-06-23 thomas }
6059 245d91c1 2018-07-12 stsp return err;
6060 245d91c1 2018-07-12 stsp }
6061 245d91c1 2018-07-12 stsp
6062 245d91c1 2018-07-12 stsp static const struct got_error *
6063 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6064 fc06ba56 2019-08-22 stsp {
6065 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6066 fc06ba56 2019-08-22 stsp int *done = arg;
6067 fc06ba56 2019-08-22 stsp int errcode;
6068 fc06ba56 2019-08-22 stsp
6069 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6070 fc06ba56 2019-08-22 stsp if (errcode)
6071 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6072 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6073 fc06ba56 2019-08-22 stsp
6074 fc06ba56 2019-08-22 stsp if (*done)
6075 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6076 fc06ba56 2019-08-22 stsp
6077 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6078 fc06ba56 2019-08-22 stsp if (errcode)
6079 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6080 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6081 fc06ba56 2019-08-22 stsp
6082 fc06ba56 2019-08-22 stsp return err;
6083 fc06ba56 2019-08-22 stsp }
6084 fc06ba56 2019-08-22 stsp
6085 fc06ba56 2019-08-22 stsp static const struct got_error *
6086 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6087 245d91c1 2018-07-12 stsp {
6088 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6089 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6090 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6091 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6092 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6093 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6094 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6095 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6096 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6097 a70480e0 2018-06-23 stsp
6098 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6099 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6100 27d434c2 2018-09-15 stsp if (err)
6101 15a94983 2018-12-23 stsp return err;
6102 f4ae6ddb 2022-07-01 thomas
6103 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6104 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6105 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6106 f4ae6ddb 2022-07-01 thomas goto done;
6107 f4ae6ddb 2022-07-01 thomas }
6108 945f9229 2022-04-16 thomas
6109 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6110 945f9229 2022-04-16 thomas if (err)
6111 945f9229 2022-04-16 thomas goto done;
6112 27d434c2 2018-09-15 stsp
6113 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6114 84451b3e 2018-07-10 stsp if (err)
6115 84451b3e 2018-07-10 stsp goto done;
6116 27d434c2 2018-09-15 stsp
6117 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6118 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6119 84451b3e 2018-07-10 stsp goto done;
6120 84451b3e 2018-07-10 stsp }
6121 a70480e0 2018-06-23 stsp
6122 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6123 a70480e0 2018-06-23 stsp if (err)
6124 a70480e0 2018-06-23 stsp goto done;
6125 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6126 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6127 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6128 84451b3e 2018-07-10 stsp goto done;
6129 84451b3e 2018-07-10 stsp }
6130 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6131 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6132 1fddf795 2021-01-20 stsp if (err)
6133 1fddf795 2021-01-20 stsp goto done;
6134 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6135 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6136 84451b3e 2018-07-10 stsp goto done;
6137 1fddf795 2021-01-20 stsp }
6138 b02560ec 2019-08-19 stsp
6139 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6140 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6141 b02560ec 2019-08-19 stsp blame->nlines--;
6142 a70480e0 2018-06-23 stsp
6143 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6144 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6145 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6146 84451b3e 2018-07-10 stsp goto done;
6147 84451b3e 2018-07-10 stsp }
6148 a70480e0 2018-06-23 stsp
6149 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6150 bd24772e 2018-07-11 stsp if (err)
6151 bd24772e 2018-07-11 stsp goto done;
6152 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6153 7cd52833 2022-06-23 thomas pack_fds);
6154 7cd52833 2022-06-23 thomas if (err)
6155 7cd52833 2022-06-23 thomas goto done;
6156 bd24772e 2018-07-11 stsp
6157 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6158 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6159 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6160 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6161 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6162 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6163 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6164 245d91c1 2018-07-12 stsp goto done;
6165 245d91c1 2018-07-12 stsp }
6166 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6167 245d91c1 2018-07-12 stsp
6168 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6169 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6170 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6171 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6172 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6173 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6174 a5388363 2020-12-01 naddy s->blame_complete = 0;
6175 f5a09613 2020-12-13 naddy
6176 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6177 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6178 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6179 f5a09613 2020-12-13 naddy s->selected_line = 1;
6180 f5a09613 2020-12-13 naddy }
6181 aa61903a 2021-12-31 thomas s->matched_line = 0;
6182 245d91c1 2018-07-12 stsp
6183 245d91c1 2018-07-12 stsp done:
6184 945f9229 2022-04-16 thomas if (commit)
6185 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6186 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6187 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6188 245d91c1 2018-07-12 stsp if (blob)
6189 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6190 27d434c2 2018-09-15 stsp free(obj_id);
6191 245d91c1 2018-07-12 stsp if (err)
6192 245d91c1 2018-07-12 stsp stop_blame(blame);
6193 245d91c1 2018-07-12 stsp return err;
6194 245d91c1 2018-07-12 stsp }
6195 245d91c1 2018-07-12 stsp
6196 245d91c1 2018-07-12 stsp static const struct got_error *
6197 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6198 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6199 245d91c1 2018-07-12 stsp {
6200 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6201 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6202 dbc6a6b6 2018-07-12 stsp
6203 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6204 245d91c1 2018-07-12 stsp
6205 c4843652 2019-08-12 stsp s->path = strdup(path);
6206 c4843652 2019-08-12 stsp if (s->path == NULL)
6207 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6208 c4843652 2019-08-12 stsp
6209 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6210 c4843652 2019-08-12 stsp if (err) {
6211 c4843652 2019-08-12 stsp free(s->path);
6212 7cbe629d 2018-08-04 stsp return err;
6213 c4843652 2019-08-12 stsp }
6214 245d91c1 2018-07-12 stsp
6215 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6216 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6217 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6218 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6219 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6220 fb2756b9 2018-08-04 stsp s->repo = repo;
6221 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6222 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6223 7cbe629d 2018-08-04 stsp
6224 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6225 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6226 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6227 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6228 11b20872 2019-11-08 stsp if (err)
6229 11b20872 2019-11-08 stsp return err;
6230 11b20872 2019-11-08 stsp }
6231 11b20872 2019-11-08 stsp
6232 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6233 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6234 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6235 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6236 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6237 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6238 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6239 e5a0f69f 2018-08-18 stsp
6240 a5388363 2020-12-01 naddy return run_blame(view);
6241 7cbe629d 2018-08-04 stsp }
6242 7cbe629d 2018-08-04 stsp
6243 e5a0f69f 2018-08-18 stsp static const struct got_error *
6244 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6245 7cbe629d 2018-08-04 stsp {
6246 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6247 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6248 7cbe629d 2018-08-04 stsp
6249 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6250 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6251 e5a0f69f 2018-08-18 stsp
6252 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6253 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6254 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6255 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6256 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6257 7cbe629d 2018-08-04 stsp }
6258 e5a0f69f 2018-08-18 stsp
6259 e5a0f69f 2018-08-18 stsp free(s->path);
6260 11b20872 2019-11-08 stsp free_colors(&s->colors);
6261 e5a0f69f 2018-08-18 stsp return err;
6262 7cbe629d 2018-08-04 stsp }
6263 7cbe629d 2018-08-04 stsp
6264 7cbe629d 2018-08-04 stsp static const struct got_error *
6265 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6266 6c4c42e0 2019-06-24 stsp {
6267 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6268 6c4c42e0 2019-06-24 stsp
6269 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6270 6c4c42e0 2019-06-24 stsp return NULL;
6271 2a7d3cd7 2022-09-17 thomas }
6272 2a7d3cd7 2022-09-17 thomas
6273 2a7d3cd7 2022-09-17 thomas static void
6274 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6275 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6276 2a7d3cd7 2022-09-17 thomas {
6277 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6278 2a7d3cd7 2022-09-17 thomas
6279 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6280 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6281 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6282 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6283 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6284 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6285 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6286 6c4c42e0 2019-06-24 stsp }
6287 6c4c42e0 2019-06-24 stsp
6288 6c4c42e0 2019-06-24 stsp static const struct got_error *
6289 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6290 7cbe629d 2018-08-04 stsp {
6291 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6292 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6293 2b380cc8 2018-10-24 stsp int errcode;
6294 2b380cc8 2018-10-24 stsp
6295 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6296 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6297 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6298 2b380cc8 2018-10-24 stsp if (errcode)
6299 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6300 51fe7530 2019-08-19 stsp
6301 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6302 2b380cc8 2018-10-24 stsp }
6303 e5a0f69f 2018-08-18 stsp
6304 51fe7530 2019-08-19 stsp if (s->blame_complete)
6305 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6306 51fe7530 2019-08-19 stsp
6307 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6308 e5a0f69f 2018-08-18 stsp
6309 a5d43cac 2022-07-01 thomas view_border(view);
6310 e5a0f69f 2018-08-18 stsp return err;
6311 e5a0f69f 2018-08-18 stsp }
6312 e5a0f69f 2018-08-18 stsp
6313 e5a0f69f 2018-08-18 stsp static const struct got_error *
6314 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6315 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6316 eaeaa612 2022-07-20 thomas {
6317 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6318 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6319 eaeaa612 2022-07-20 thomas
6320 eaeaa612 2022-07-20 thomas *new_view = NULL;
6321 eaeaa612 2022-07-20 thomas
6322 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6323 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6324 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6325 eaeaa612 2022-07-20 thomas
6326 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6327 eaeaa612 2022-07-20 thomas if (err)
6328 eaeaa612 2022-07-20 thomas view_close(log_view);
6329 eaeaa612 2022-07-20 thomas else
6330 eaeaa612 2022-07-20 thomas *new_view = log_view;
6331 eaeaa612 2022-07-20 thomas
6332 eaeaa612 2022-07-20 thomas return err;
6333 eaeaa612 2022-07-20 thomas }
6334 eaeaa612 2022-07-20 thomas
6335 eaeaa612 2022-07-20 thomas static const struct got_error *
6336 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6337 e5a0f69f 2018-08-18 stsp {
6338 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6339 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6340 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6341 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6342 a5d43cac 2022-07-01 thomas
6343 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6344 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6345 a5d43cac 2022-07-01 thomas --eos; /* border */
6346 7cbe629d 2018-08-04 stsp
6347 e5a0f69f 2018-08-18 stsp switch (ch) {
6348 05171be4 2022-06-23 thomas case '0':
6349 05171be4 2022-06-23 thomas case '$':
6350 05171be4 2022-06-23 thomas case KEY_RIGHT:
6351 05171be4 2022-06-23 thomas case 'l':
6352 05171be4 2022-06-23 thomas case KEY_LEFT:
6353 05171be4 2022-06-23 thomas case 'h':
6354 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6355 05171be4 2022-06-23 thomas break;
6356 1e37a5c2 2019-05-12 jcs case 'q':
6357 1e37a5c2 2019-05-12 jcs s->done = 1;
6358 4deef56f 2021-09-02 naddy break;
6359 4deef56f 2021-09-02 naddy case 'g':
6360 4deef56f 2021-09-02 naddy case KEY_HOME:
6361 4deef56f 2021-09-02 naddy s->selected_line = 1;
6362 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6363 07b0611c 2022-06-23 thomas view->count = 0;
6364 4deef56f 2021-09-02 naddy break;
6365 4deef56f 2021-09-02 naddy case 'G':
6366 4deef56f 2021-09-02 naddy case KEY_END:
6367 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6368 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6369 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6370 4deef56f 2021-09-02 naddy } else {
6371 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6372 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6373 4deef56f 2021-09-02 naddy }
6374 07b0611c 2022-06-23 thomas view->count = 0;
6375 1e37a5c2 2019-05-12 jcs break;
6376 1e37a5c2 2019-05-12 jcs case 'k':
6377 1e37a5c2 2019-05-12 jcs case KEY_UP:
6378 f7140bf5 2021-10-17 thomas case CTRL('p'):
6379 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6380 1e37a5c2 2019-05-12 jcs s->selected_line--;
6381 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6382 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6383 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6384 07b0611c 2022-06-23 thomas else
6385 07b0611c 2022-06-23 thomas view->count = 0;
6386 1e37a5c2 2019-05-12 jcs break;
6387 70f17a53 2022-06-13 thomas case CTRL('u'):
6388 23427b14 2022-06-23 thomas case 'u':
6389 70f17a53 2022-06-13 thomas nscroll /= 2;
6390 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6391 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6392 ea025d1d 2020-02-22 naddy case CTRL('b'):
6393 1c5e5faa 2022-06-23 thomas case 'b':
6394 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6395 07b0611c 2022-06-23 thomas if (view->count > 1)
6396 07b0611c 2022-06-23 thomas nscroll += nscroll;
6397 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6398 07b0611c 2022-06-23 thomas view->count = 0;
6399 e5a0f69f 2018-08-18 stsp break;
6400 1e37a5c2 2019-05-12 jcs }
6401 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6402 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6403 1e37a5c2 2019-05-12 jcs else
6404 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6405 1e37a5c2 2019-05-12 jcs break;
6406 1e37a5c2 2019-05-12 jcs case 'j':
6407 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6408 f7140bf5 2021-10-17 thomas case CTRL('n'):
6409 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6410 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6411 1e37a5c2 2019-05-12 jcs s->selected_line++;
6412 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6413 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6414 07b0611c 2022-06-23 thomas else
6415 07b0611c 2022-06-23 thomas view->count = 0;
6416 1e37a5c2 2019-05-12 jcs break;
6417 1c5e5faa 2022-06-23 thomas case 'c':
6418 1e37a5c2 2019-05-12 jcs case 'p': {
6419 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6420 07b0611c 2022-06-23 thomas
6421 07b0611c 2022-06-23 thomas view->count = 0;
6422 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6423 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6424 1e37a5c2 2019-05-12 jcs if (id == NULL)
6425 e5a0f69f 2018-08-18 stsp break;
6426 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6427 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6428 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6429 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6430 1e37a5c2 2019-05-12 jcs int obj_type;
6431 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6432 1e37a5c2 2019-05-12 jcs s->repo, id);
6433 e5a0f69f 2018-08-18 stsp if (err)
6434 e5a0f69f 2018-08-18 stsp break;
6435 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6436 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6437 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6438 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6439 e5a0f69f 2018-08-18 stsp break;
6440 e5a0f69f 2018-08-18 stsp }
6441 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6442 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6443 ec242592 2022-04-22 thomas s->repo, &pid->id);
6444 945f9229 2022-04-16 thomas if (err)
6445 945f9229 2022-04-16 thomas break;
6446 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6447 945f9229 2022-04-16 thomas pcommit, s->path);
6448 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6449 e5a0f69f 2018-08-18 stsp if (err) {
6450 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6451 1e37a5c2 2019-05-12 jcs err = NULL;
6452 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6453 e5a0f69f 2018-08-18 stsp break;
6454 e5a0f69f 2018-08-18 stsp }
6455 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6456 1e37a5c2 2019-05-12 jcs blob_id);
6457 1e37a5c2 2019-05-12 jcs free(blob_id);
6458 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6459 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6460 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6461 e5a0f69f 2018-08-18 stsp break;
6462 1e37a5c2 2019-05-12 jcs }
6463 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6464 ec242592 2022-04-22 thomas &pid->id);
6465 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6466 1e37a5c2 2019-05-12 jcs } else {
6467 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6468 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6469 1e37a5c2 2019-05-12 jcs break;
6470 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6471 1e37a5c2 2019-05-12 jcs id);
6472 1e37a5c2 2019-05-12 jcs }
6473 1e37a5c2 2019-05-12 jcs if (err)
6474 e5a0f69f 2018-08-18 stsp break;
6475 1e37a5c2 2019-05-12 jcs s->done = 1;
6476 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6477 1e37a5c2 2019-05-12 jcs s->done = 0;
6478 1e37a5c2 2019-05-12 jcs if (thread_err)
6479 1e37a5c2 2019-05-12 jcs break;
6480 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6481 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6482 a5388363 2020-12-01 naddy err = run_blame(view);
6483 1e37a5c2 2019-05-12 jcs if (err)
6484 1e37a5c2 2019-05-12 jcs break;
6485 1e37a5c2 2019-05-12 jcs break;
6486 1e37a5c2 2019-05-12 jcs }
6487 1c5e5faa 2022-06-23 thomas case 'C': {
6488 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6489 07b0611c 2022-06-23 thomas
6490 07b0611c 2022-06-23 thomas view->count = 0;
6491 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6492 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6493 1e37a5c2 2019-05-12 jcs break;
6494 1e37a5c2 2019-05-12 jcs s->done = 1;
6495 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6496 1e37a5c2 2019-05-12 jcs s->done = 0;
6497 1e37a5c2 2019-05-12 jcs if (thread_err)
6498 1e37a5c2 2019-05-12 jcs break;
6499 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6500 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6501 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6502 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6503 a5388363 2020-12-01 naddy err = run_blame(view);
6504 1e37a5c2 2019-05-12 jcs if (err)
6505 1e37a5c2 2019-05-12 jcs break;
6506 1e37a5c2 2019-05-12 jcs break;
6507 1e37a5c2 2019-05-12 jcs }
6508 2a31b33b 2022-07-23 thomas case 'L':
6509 eaeaa612 2022-07-20 thomas view->count = 0;
6510 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6511 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6512 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6513 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6514 eaeaa612 2022-07-20 thomas break;
6515 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6516 1e37a5c2 2019-05-12 jcs case '\r': {
6517 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6518 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6519 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6520 07b0611c 2022-06-23 thomas
6521 07b0611c 2022-06-23 thomas view->count = 0;
6522 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6523 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6524 1e37a5c2 2019-05-12 jcs if (id == NULL)
6525 1e37a5c2 2019-05-12 jcs break;
6526 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6527 1e37a5c2 2019-05-12 jcs if (err)
6528 1e37a5c2 2019-05-12 jcs break;
6529 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6530 4fc71f3b 2022-07-12 thomas if (*new_view) {
6531 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6532 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6533 4fc71f3b 2022-07-12 thomas if (err)
6534 4fc71f3b 2022-07-12 thomas break;
6535 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6536 4fc71f3b 2022-07-12 thomas } else {
6537 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6538 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6539 a5d43cac 2022-07-01 thomas
6540 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6541 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6542 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6543 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6544 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6545 4fc71f3b 2022-07-12 thomas break;
6546 4fc71f3b 2022-07-12 thomas }
6547 15a94983 2018-12-23 stsp }
6548 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6549 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6550 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6551 1e37a5c2 2019-05-12 jcs if (err) {
6552 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6553 1e37a5c2 2019-05-12 jcs break;
6554 1e37a5c2 2019-05-12 jcs }
6555 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6556 4fc71f3b 2022-07-12 thomas s->selected_line;
6557 4fc71f3b 2022-07-12 thomas if (*new_view)
6558 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6559 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6560 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6561 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6562 a5d43cac 2022-07-01 thomas if (err)
6563 a5d43cac 2022-07-01 thomas break;
6564 a5d43cac 2022-07-01 thomas }
6565 a5d43cac 2022-07-01 thomas
6566 e78dc838 2020-12-04 stsp view->focussed = 0;
6567 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6568 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6569 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6570 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6571 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6572 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6573 1e37a5c2 2019-05-12 jcs if (err)
6574 34bc9ec9 2019-02-22 stsp break;
6575 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6576 40236d76 2022-06-23 thomas if (err)
6577 40236d76 2022-06-23 thomas break;
6578 e78dc838 2020-12-04 stsp view->focus_child = 1;
6579 1e37a5c2 2019-05-12 jcs } else
6580 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6581 1e37a5c2 2019-05-12 jcs if (err)
6582 e5a0f69f 2018-08-18 stsp break;
6583 1e37a5c2 2019-05-12 jcs break;
6584 1e37a5c2 2019-05-12 jcs }
6585 70f17a53 2022-06-13 thomas case CTRL('d'):
6586 23427b14 2022-06-23 thomas case 'd':
6587 70f17a53 2022-06-13 thomas nscroll /= 2;
6588 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6589 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6590 ea025d1d 2020-02-22 naddy case CTRL('f'):
6591 1c5e5faa 2022-06-23 thomas case 'f':
6592 1e37a5c2 2019-05-12 jcs case ' ':
6593 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6594 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6595 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6596 07b0611c 2022-06-23 thomas view->count = 0;
6597 e5a0f69f 2018-08-18 stsp break;
6598 1e37a5c2 2019-05-12 jcs }
6599 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6600 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6601 70f17a53 2022-06-13 thomas s->selected_line +=
6602 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6603 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6604 1e37a5c2 2019-05-12 jcs }
6605 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6606 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6607 1e37a5c2 2019-05-12 jcs else
6608 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6609 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6610 1e37a5c2 2019-05-12 jcs break;
6611 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6612 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6613 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6614 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6615 1e37a5c2 2019-05-12 jcs }
6616 1e37a5c2 2019-05-12 jcs break;
6617 1e37a5c2 2019-05-12 jcs default:
6618 07b0611c 2022-06-23 thomas view->count = 0;
6619 1e37a5c2 2019-05-12 jcs break;
6620 a70480e0 2018-06-23 stsp }
6621 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6622 adf4c9e0 2022-07-03 thomas }
6623 adf4c9e0 2022-07-03 thomas
6624 adf4c9e0 2022-07-03 thomas static const struct got_error *
6625 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6626 adf4c9e0 2022-07-03 thomas {
6627 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6628 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6629 adf4c9e0 2022-07-03 thomas
6630 adf4c9e0 2022-07-03 thomas view->count = 0;
6631 adf4c9e0 2022-07-03 thomas s->done = 1;
6632 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6633 adf4c9e0 2022-07-03 thomas s->done = 0;
6634 adf4c9e0 2022-07-03 thomas if (err)
6635 adf4c9e0 2022-07-03 thomas return err;
6636 adf4c9e0 2022-07-03 thomas return run_blame(view);
6637 a70480e0 2018-06-23 stsp }
6638 a70480e0 2018-06-23 stsp
6639 a70480e0 2018-06-23 stsp static const struct got_error *
6640 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6641 9f7d7167 2018-04-29 stsp {
6642 a70480e0 2018-06-23 stsp const struct got_error *error;
6643 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6644 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6645 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6646 0587e10c 2020-07-23 stsp char *link_target = NULL;
6647 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6648 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6649 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6650 a70480e0 2018-06-23 stsp int ch;
6651 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6652 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6653 a70480e0 2018-06-23 stsp
6654 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6655 a70480e0 2018-06-23 stsp switch (ch) {
6656 a70480e0 2018-06-23 stsp case 'c':
6657 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6658 a70480e0 2018-06-23 stsp break;
6659 69069811 2018-08-02 stsp case 'r':
6660 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6661 69069811 2018-08-02 stsp if (repo_path == NULL)
6662 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6663 9ba1d308 2019-10-21 stsp optarg);
6664 69069811 2018-08-02 stsp break;
6665 a70480e0 2018-06-23 stsp default:
6666 17020d27 2019-03-07 stsp usage_blame();
6667 a70480e0 2018-06-23 stsp /* NOTREACHED */
6668 a70480e0 2018-06-23 stsp }
6669 a70480e0 2018-06-23 stsp }
6670 a70480e0 2018-06-23 stsp
6671 a70480e0 2018-06-23 stsp argc -= optind;
6672 a70480e0 2018-06-23 stsp argv += optind;
6673 a70480e0 2018-06-23 stsp
6674 f135c941 2020-02-20 stsp if (argc != 1)
6675 a70480e0 2018-06-23 stsp usage_blame();
6676 6962eb72 2020-02-20 stsp
6677 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6678 7cd52833 2022-06-23 thomas if (error != NULL)
6679 7cd52833 2022-06-23 thomas goto done;
6680 7cd52833 2022-06-23 thomas
6681 69069811 2018-08-02 stsp if (repo_path == NULL) {
6682 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6683 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6684 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6685 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6686 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6687 c156c7a4 2020-12-18 stsp goto done;
6688 f135c941 2020-02-20 stsp if (worktree)
6689 eb41ed75 2019-02-05 stsp repo_path =
6690 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6691 f135c941 2020-02-20 stsp else
6692 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6693 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6694 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6695 c156c7a4 2020-12-18 stsp goto done;
6696 c156c7a4 2020-12-18 stsp }
6697 f135c941 2020-02-20 stsp }
6698 a915003a 2019-02-05 stsp
6699 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6700 c02c541e 2019-03-29 stsp if (error != NULL)
6701 8e94dd5b 2019-01-04 stsp goto done;
6702 69069811 2018-08-02 stsp
6703 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6704 f135c941 2020-02-20 stsp worktree);
6705 c02c541e 2019-03-29 stsp if (error)
6706 92205607 2019-01-04 stsp goto done;
6707 69069811 2018-08-02 stsp
6708 f135c941 2020-02-20 stsp init_curses();
6709 f135c941 2020-02-20 stsp
6710 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6711 51a10b52 2020-12-26 stsp if (error)
6712 51a10b52 2020-12-26 stsp goto done;
6713 51a10b52 2020-12-26 stsp
6714 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6715 eb41ed75 2019-02-05 stsp if (error)
6716 69069811 2018-08-02 stsp goto done;
6717 a70480e0 2018-06-23 stsp
6718 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6719 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6720 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6721 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6722 a70480e0 2018-06-23 stsp if (error != NULL)
6723 66b4983c 2018-06-23 stsp goto done;
6724 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6725 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6726 a70480e0 2018-06-23 stsp } else {
6727 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6728 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6729 a70480e0 2018-06-23 stsp }
6730 a19e88aa 2018-06-23 stsp if (error != NULL)
6731 8b473291 2019-02-21 stsp goto done;
6732 8b473291 2019-02-21 stsp
6733 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6734 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6735 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6736 e1cd8fed 2018-08-01 stsp goto done;
6737 e1cd8fed 2018-08-01 stsp }
6738 0587e10c 2020-07-23 stsp
6739 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6740 945f9229 2022-04-16 thomas if (error)
6741 945f9229 2022-04-16 thomas goto done;
6742 945f9229 2022-04-16 thomas
6743 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6744 945f9229 2022-04-16 thomas commit, repo);
6745 7cbe629d 2018-08-04 stsp if (error)
6746 7cbe629d 2018-08-04 stsp goto done;
6747 0587e10c 2020-07-23 stsp
6748 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6749 78756c87 2020-11-24 stsp commit_id, repo);
6750 0587e10c 2020-07-23 stsp if (error)
6751 0587e10c 2020-07-23 stsp goto done;
6752 12314ad4 2019-08-31 stsp if (worktree) {
6753 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6754 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6755 12314ad4 2019-08-31 stsp worktree = NULL;
6756 12314ad4 2019-08-31 stsp }
6757 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6758 a70480e0 2018-06-23 stsp done:
6759 69069811 2018-08-02 stsp free(repo_path);
6760 f135c941 2020-02-20 stsp free(in_repo_path);
6761 0587e10c 2020-07-23 stsp free(link_target);
6762 69069811 2018-08-02 stsp free(cwd);
6763 a70480e0 2018-06-23 stsp free(commit_id);
6764 945f9229 2022-04-16 thomas if (commit)
6765 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6766 eb41ed75 2019-02-05 stsp if (worktree)
6767 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6768 1d0f4054 2021-06-17 stsp if (repo) {
6769 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6770 1d0f4054 2021-06-17 stsp if (error == NULL)
6771 1d0f4054 2021-06-17 stsp error = close_err;
6772 1d0f4054 2021-06-17 stsp }
6773 7cd52833 2022-06-23 thomas if (pack_fds) {
6774 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6775 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6776 7cd52833 2022-06-23 thomas if (error == NULL)
6777 7cd52833 2022-06-23 thomas error = pack_err;
6778 7cd52833 2022-06-23 thomas }
6779 51a10b52 2020-12-26 stsp tog_free_refs();
6780 a70480e0 2018-06-23 stsp return error;
6781 ffd1d5e5 2018-06-23 stsp }
6782 ffd1d5e5 2018-06-23 stsp
6783 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6784 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6785 ffd1d5e5 2018-06-23 stsp {
6786 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6787 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6788 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6789 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6790 86f4aab9 2022-09-09 thomas char *index = NULL;
6791 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6792 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
6793 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6794 ffd1d5e5 2018-06-23 stsp
6795 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6796 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6797 a5d43cac 2022-07-01 thomas --limit; /* border */
6798 ffd1d5e5 2018-06-23 stsp
6799 f7d12f7e 2018-08-01 stsp werase(view->window);
6800 ffd1d5e5 2018-06-23 stsp
6801 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6802 ffd1d5e5 2018-06-23 stsp return NULL;
6803 ffd1d5e5 2018-06-23 stsp
6804 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6805 f91a2b48 2022-06-23 thomas 0, 0);
6806 ffd1d5e5 2018-06-23 stsp if (err)
6807 ffd1d5e5 2018-06-23 stsp return err;
6808 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6809 a3404814 2018-09-02 stsp wstandout(view->window);
6810 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6811 11b20872 2019-11-08 stsp if (tc)
6812 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6813 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6814 86f4aab9 2022-09-09 thomas free(wline);
6815 86f4aab9 2022-09-09 thomas wline = NULL;
6816 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6817 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6818 11b20872 2019-11-08 stsp if (tc)
6819 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6820 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6821 a3404814 2018-09-02 stsp wstandend(view->window);
6822 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6823 86f4aab9 2022-09-09 thomas return NULL;
6824 07dd3ed3 2022-08-06 thomas
6825 6f5f393a 2022-08-12 thomas i += s->selected;
6826 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6827 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6828 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6829 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6830 07dd3ed3 2022-08-06 thomas }
6831 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6832 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6833 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6834 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6835 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6836 86f4aab9 2022-09-09 thomas free(index);
6837 ce52c690 2018-06-23 stsp if (err)
6838 ce52c690 2018-06-23 stsp return err;
6839 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6840 2550e4c3 2018-07-13 stsp free(wline);
6841 2550e4c3 2018-07-13 stsp wline = NULL;
6842 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6843 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6844 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6845 ffd1d5e5 2018-06-23 stsp return NULL;
6846 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6847 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6848 a1eca9bb 2018-06-23 stsp return NULL;
6849 ffd1d5e5 2018-06-23 stsp
6850 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6851 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6852 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6853 0cf4efb1 2018-09-29 stsp if (view->focussed)
6854 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6855 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6856 ffd1d5e5 2018-06-23 stsp }
6857 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6858 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6859 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6860 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6861 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6862 ffd1d5e5 2018-06-23 stsp return NULL;
6863 ffd1d5e5 2018-06-23 stsp n = 1;
6864 ffd1d5e5 2018-06-23 stsp } else {
6865 ffd1d5e5 2018-06-23 stsp n = 0;
6866 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6867 ffd1d5e5 2018-06-23 stsp }
6868 ffd1d5e5 2018-06-23 stsp
6869 c72de8ab 2023-02-03 thomas view->maxx = 0;
6870 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6871 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6872 848d6979 2019-08-12 stsp const char *modestr = "";
6873 56e0773d 2019-11-28 stsp mode_t mode;
6874 1d13200f 2018-07-12 stsp
6875 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6876 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6877 56e0773d 2019-11-28 stsp
6878 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6879 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6880 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6881 1d13200f 2018-07-12 stsp if (err)
6882 638f9024 2019-05-13 stsp return got_error_from_errno(
6883 230a42bd 2019-05-11 jcs "got_object_id_str");
6884 1d13200f 2018-07-12 stsp }
6885 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6886 63c5ca5d 2019-08-24 stsp modestr = "$";
6887 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6888 0d6c6ee3 2020-05-20 stsp int i;
6889 0d6c6ee3 2020-05-20 stsp
6890 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6891 d86d3b18 2020-12-01 naddy te, s->repo);
6892 0d6c6ee3 2020-05-20 stsp if (err) {
6893 0d6c6ee3 2020-05-20 stsp free(id_str);
6894 0d6c6ee3 2020-05-20 stsp return err;
6895 0d6c6ee3 2020-05-20 stsp }
6896 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6897 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6898 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6899 0d6c6ee3 2020-05-20 stsp }
6900 848d6979 2019-08-12 stsp modestr = "@";
6901 0d6c6ee3 2020-05-20 stsp }
6902 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6903 848d6979 2019-08-12 stsp modestr = "/";
6904 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6905 848d6979 2019-08-12 stsp modestr = "*";
6906 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6907 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6908 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6909 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6910 1d13200f 2018-07-12 stsp free(id_str);
6911 0d6c6ee3 2020-05-20 stsp free(link_target);
6912 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6913 1d13200f 2018-07-12 stsp }
6914 1d13200f 2018-07-12 stsp free(id_str);
6915 0d6c6ee3 2020-05-20 stsp free(link_target);
6916 c72de8ab 2023-02-03 thomas
6917 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
6918 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6919 ffd1d5e5 2018-06-23 stsp if (err) {
6920 ffd1d5e5 2018-06-23 stsp free(line);
6921 ffd1d5e5 2018-06-23 stsp break;
6922 ffd1d5e5 2018-06-23 stsp }
6923 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
6924 c72de8ab 2023-02-03 thomas free(wline);
6925 c72de8ab 2023-02-03 thomas wline = NULL;
6926 c72de8ab 2023-02-03 thomas
6927 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
6928 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
6929 c72de8ab 2023-02-03 thomas if (err) {
6930 c72de8ab 2023-02-03 thomas free(line);
6931 c72de8ab 2023-02-03 thomas break;
6932 c72de8ab 2023-02-03 thomas }
6933 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6934 0cf4efb1 2018-09-29 stsp if (view->focussed)
6935 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6936 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6937 ffd1d5e5 2018-06-23 stsp }
6938 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6939 f26dddb7 2019-11-08 stsp if (tc)
6940 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6941 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6942 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
6943 f26dddb7 2019-11-08 stsp if (tc)
6944 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6945 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6946 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
6947 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6948 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6949 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6950 ffd1d5e5 2018-06-23 stsp free(line);
6951 2550e4c3 2018-07-13 stsp free(wline);
6952 2550e4c3 2018-07-13 stsp wline = NULL;
6953 ffd1d5e5 2018-06-23 stsp n++;
6954 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6955 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6956 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6957 ffd1d5e5 2018-06-23 stsp break;
6958 ffd1d5e5 2018-06-23 stsp }
6959 ffd1d5e5 2018-06-23 stsp
6960 ffd1d5e5 2018-06-23 stsp return err;
6961 ffd1d5e5 2018-06-23 stsp }
6962 ffd1d5e5 2018-06-23 stsp
6963 ffd1d5e5 2018-06-23 stsp static void
6964 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6965 ffd1d5e5 2018-06-23 stsp {
6966 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6967 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6968 fa86c4bf 2020-11-29 stsp int i = 0;
6969 ffd1d5e5 2018-06-23 stsp
6970 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6971 ffd1d5e5 2018-06-23 stsp return;
6972 ffd1d5e5 2018-06-23 stsp
6973 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6974 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6975 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6976 fa86c4bf 2020-11-29 stsp if (!isroot)
6977 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6978 fa86c4bf 2020-11-29 stsp break;
6979 fa86c4bf 2020-11-29 stsp }
6980 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6981 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6982 ffd1d5e5 2018-06-23 stsp }
6983 ffd1d5e5 2018-06-23 stsp }
6984 ffd1d5e5 2018-06-23 stsp
6985 a5d43cac 2022-07-01 thomas static const struct got_error *
6986 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6987 ffd1d5e5 2018-06-23 stsp {
6988 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6989 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6990 ffd1d5e5 2018-06-23 stsp int n = 0;
6991 ffd1d5e5 2018-06-23 stsp
6992 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6993 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6994 694d3271 2020-12-01 naddy s->first_displayed_entry);
6995 694d3271 2020-12-01 naddy else
6996 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6997 56e0773d 2019-11-28 stsp
6998 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6999 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7000 07dd3ed3 2022-08-06 thomas if (last) {
7001 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7002 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
7003 07dd3ed3 2022-08-06 thomas }
7004 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
7005 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7006 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
7007 768394f3 2019-01-24 stsp }
7008 ffd1d5e5 2018-06-23 stsp }
7009 a5d43cac 2022-07-01 thomas
7010 a5d43cac 2022-07-01 thomas return NULL;
7011 ffd1d5e5 2018-06-23 stsp }
7012 ffd1d5e5 2018-06-23 stsp
7013 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7014 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7015 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7016 ffd1d5e5 2018-06-23 stsp {
7017 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7018 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7019 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7020 ffd1d5e5 2018-06-23 stsp
7021 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7022 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7023 56e0773d 2019-11-28 stsp + 1 /* slash */;
7024 ce52c690 2018-06-23 stsp if (te)
7025 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7026 ce52c690 2018-06-23 stsp
7027 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7028 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7029 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7030 ffd1d5e5 2018-06-23 stsp
7031 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7032 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7033 d9765a41 2018-06-23 stsp while (pt) {
7034 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7035 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7036 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7037 cb2ebc8a 2018-06-23 stsp goto done;
7038 cb2ebc8a 2018-06-23 stsp }
7039 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7040 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7041 cb2ebc8a 2018-06-23 stsp goto done;
7042 cb2ebc8a 2018-06-23 stsp }
7043 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7044 ffd1d5e5 2018-06-23 stsp }
7045 ce52c690 2018-06-23 stsp if (te) {
7046 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7047 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7048 ce52c690 2018-06-23 stsp goto done;
7049 ce52c690 2018-06-23 stsp }
7050 cb2ebc8a 2018-06-23 stsp }
7051 ce52c690 2018-06-23 stsp done:
7052 ce52c690 2018-06-23 stsp if (err) {
7053 ce52c690 2018-06-23 stsp free(*path);
7054 ce52c690 2018-06-23 stsp *path = NULL;
7055 ce52c690 2018-06-23 stsp }
7056 ce52c690 2018-06-23 stsp return err;
7057 ce52c690 2018-06-23 stsp }
7058 ce52c690 2018-06-23 stsp
7059 ce52c690 2018-06-23 stsp static const struct got_error *
7060 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7061 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7062 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7063 ce52c690 2018-06-23 stsp {
7064 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7065 ce52c690 2018-06-23 stsp char *path;
7066 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7067 a0de39f3 2019-08-09 stsp
7068 a0de39f3 2019-08-09 stsp *new_view = NULL;
7069 69efd4c4 2018-07-18 stsp
7070 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7071 ce52c690 2018-06-23 stsp if (err)
7072 ce52c690 2018-06-23 stsp return err;
7073 ffd1d5e5 2018-06-23 stsp
7074 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7075 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7076 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7077 83ce39e3 2019-08-12 stsp goto done;
7078 83ce39e3 2019-08-12 stsp }
7079 cdf1ee82 2018-08-01 stsp
7080 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7081 e5a0f69f 2018-08-18 stsp if (err) {
7082 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7083 fc06ba56 2019-08-22 stsp err = NULL;
7084 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7085 e5a0f69f 2018-08-18 stsp } else
7086 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7087 83ce39e3 2019-08-12 stsp done:
7088 83ce39e3 2019-08-12 stsp free(path);
7089 69efd4c4 2018-07-18 stsp return err;
7090 69efd4c4 2018-07-18 stsp }
7091 69efd4c4 2018-07-18 stsp
7092 69efd4c4 2018-07-18 stsp static const struct got_error *
7093 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7094 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7095 69efd4c4 2018-07-18 stsp {
7096 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7097 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7098 69efd4c4 2018-07-18 stsp char *path;
7099 a0de39f3 2019-08-09 stsp
7100 a0de39f3 2019-08-09 stsp *new_view = NULL;
7101 69efd4c4 2018-07-18 stsp
7102 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7103 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7104 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7105 e5a0f69f 2018-08-18 stsp
7106 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7107 69efd4c4 2018-07-18 stsp if (err)
7108 69efd4c4 2018-07-18 stsp return err;
7109 69efd4c4 2018-07-18 stsp
7110 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7111 4e97c21c 2020-12-06 stsp path, 0);
7112 ba4f502b 2018-08-04 stsp if (err)
7113 e5a0f69f 2018-08-18 stsp view_close(log_view);
7114 e5a0f69f 2018-08-18 stsp else
7115 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7116 cb2ebc8a 2018-06-23 stsp free(path);
7117 cb2ebc8a 2018-06-23 stsp return err;
7118 ffd1d5e5 2018-06-23 stsp }
7119 ffd1d5e5 2018-06-23 stsp
7120 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7121 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7122 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7123 ffd1d5e5 2018-06-23 stsp {
7124 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7125 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7126 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7127 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7128 ffd1d5e5 2018-06-23 stsp
7129 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7130 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7131 bc573f3b 2021-07-10 stsp
7132 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7133 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7134 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7135 ffd1d5e5 2018-06-23 stsp
7136 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7137 bc573f3b 2021-07-10 stsp if (err)
7138 bc573f3b 2021-07-10 stsp goto done;
7139 bc573f3b 2021-07-10 stsp
7140 bc573f3b 2021-07-10 stsp /*
7141 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7142 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7143 bc573f3b 2021-07-10 stsp * closed on demand.
7144 bc573f3b 2021-07-10 stsp */
7145 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7146 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7147 bc573f3b 2021-07-10 stsp if (err)
7148 bc573f3b 2021-07-10 stsp goto done;
7149 bc573f3b 2021-07-10 stsp s->tree = s->root;
7150 bc573f3b 2021-07-10 stsp
7151 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7152 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7153 ffd1d5e5 2018-06-23 stsp goto done;
7154 ffd1d5e5 2018-06-23 stsp
7155 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7156 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7157 ffd1d5e5 2018-06-23 stsp goto done;
7158 ffd1d5e5 2018-06-23 stsp }
7159 ffd1d5e5 2018-06-23 stsp
7160 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7161 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7162 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7163 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7164 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7165 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7166 9cd7cbd1 2020-12-07 stsp goto done;
7167 9cd7cbd1 2020-12-07 stsp }
7168 9cd7cbd1 2020-12-07 stsp }
7169 fb2756b9 2018-08-04 stsp s->repo = repo;
7170 c0b01bdb 2019-11-08 stsp
7171 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7172 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7173 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7174 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7175 c0b01bdb 2019-11-08 stsp if (err)
7176 c0b01bdb 2019-11-08 stsp goto done;
7177 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7178 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7179 bc573f3b 2021-07-10 stsp if (err)
7180 c0b01bdb 2019-11-08 stsp goto done;
7181 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7182 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7183 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7184 bc573f3b 2021-07-10 stsp if (err)
7185 c0b01bdb 2019-11-08 stsp goto done;
7186 e5a0f69f 2018-08-18 stsp
7187 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7188 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7189 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7190 bc573f3b 2021-07-10 stsp if (err)
7191 11b20872 2019-11-08 stsp goto done;
7192 11b20872 2019-11-08 stsp
7193 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7194 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7195 bc573f3b 2021-07-10 stsp if (err)
7196 c0b01bdb 2019-11-08 stsp goto done;
7197 c0b01bdb 2019-11-08 stsp }
7198 c0b01bdb 2019-11-08 stsp
7199 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7200 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7201 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7202 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7203 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7204 ad80ab7b 2018-08-04 stsp done:
7205 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7206 bc573f3b 2021-07-10 stsp if (commit)
7207 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7208 bc573f3b 2021-07-10 stsp if (err)
7209 bc573f3b 2021-07-10 stsp close_tree_view(view);
7210 ad80ab7b 2018-08-04 stsp return err;
7211 ad80ab7b 2018-08-04 stsp }
7212 ad80ab7b 2018-08-04 stsp
7213 e5a0f69f 2018-08-18 stsp static const struct got_error *
7214 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7215 ad80ab7b 2018-08-04 stsp {
7216 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7217 ad80ab7b 2018-08-04 stsp
7218 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7219 fb2756b9 2018-08-04 stsp free(s->tree_label);
7220 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7221 6484ec90 2018-09-29 stsp free(s->commit_id);
7222 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7223 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7224 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7225 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7226 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7227 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7228 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7229 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7230 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7231 ad80ab7b 2018-08-04 stsp free(parent);
7232 ad80ab7b 2018-08-04 stsp
7233 ad80ab7b 2018-08-04 stsp }
7234 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7235 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7236 bc573f3b 2021-07-10 stsp if (s->root)
7237 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7238 7c32bd05 2019-06-22 stsp return NULL;
7239 7c32bd05 2019-06-22 stsp }
7240 7c32bd05 2019-06-22 stsp
7241 7c32bd05 2019-06-22 stsp static const struct got_error *
7242 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7243 7c32bd05 2019-06-22 stsp {
7244 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7245 7c32bd05 2019-06-22 stsp
7246 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7247 7c32bd05 2019-06-22 stsp return NULL;
7248 7c32bd05 2019-06-22 stsp }
7249 7c32bd05 2019-06-22 stsp
7250 7c32bd05 2019-06-22 stsp static int
7251 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7252 7c32bd05 2019-06-22 stsp {
7253 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7254 7c32bd05 2019-06-22 stsp
7255 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7256 56e0773d 2019-11-28 stsp 0) == 0;
7257 7c32bd05 2019-06-22 stsp }
7258 7c32bd05 2019-06-22 stsp
7259 7c32bd05 2019-06-22 stsp static const struct got_error *
7260 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7261 7c32bd05 2019-06-22 stsp {
7262 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7263 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7264 7c32bd05 2019-06-22 stsp
7265 7c32bd05 2019-06-22 stsp if (!view->searching) {
7266 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7267 7c32bd05 2019-06-22 stsp return NULL;
7268 7c32bd05 2019-06-22 stsp }
7269 7c32bd05 2019-06-22 stsp
7270 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7271 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7272 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7273 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7274 56e0773d 2019-11-28 stsp s->selected_entry);
7275 7c32bd05 2019-06-22 stsp else
7276 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7277 56e0773d 2019-11-28 stsp } else {
7278 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7279 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7280 56e0773d 2019-11-28 stsp else
7281 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7282 56e0773d 2019-11-28 stsp s->selected_entry);
7283 7c32bd05 2019-06-22 stsp }
7284 7c32bd05 2019-06-22 stsp } else {
7285 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7286 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7287 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7288 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7289 56e0773d 2019-11-28 stsp else
7290 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7291 7c32bd05 2019-06-22 stsp }
7292 7c32bd05 2019-06-22 stsp
7293 7c32bd05 2019-06-22 stsp while (1) {
7294 56e0773d 2019-11-28 stsp if (te == NULL) {
7295 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7296 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7297 ac66afb8 2019-06-24 stsp return NULL;
7298 ac66afb8 2019-06-24 stsp }
7299 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7300 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7301 56e0773d 2019-11-28 stsp else
7302 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7303 7c32bd05 2019-06-22 stsp }
7304 7c32bd05 2019-06-22 stsp
7305 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7306 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7307 56e0773d 2019-11-28 stsp s->matched_entry = te;
7308 7c32bd05 2019-06-22 stsp break;
7309 7c32bd05 2019-06-22 stsp }
7310 7c32bd05 2019-06-22 stsp
7311 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7312 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7313 56e0773d 2019-11-28 stsp else
7314 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7315 7c32bd05 2019-06-22 stsp }
7316 e5a0f69f 2018-08-18 stsp
7317 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7318 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7319 7c32bd05 2019-06-22 stsp s->selected = 0;
7320 7c32bd05 2019-06-22 stsp }
7321 7c32bd05 2019-06-22 stsp
7322 e5a0f69f 2018-08-18 stsp return NULL;
7323 ad80ab7b 2018-08-04 stsp }
7324 ad80ab7b 2018-08-04 stsp
7325 ad80ab7b 2018-08-04 stsp static const struct got_error *
7326 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7327 ad80ab7b 2018-08-04 stsp {
7328 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7329 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7330 e5a0f69f 2018-08-18 stsp char *parent_path;
7331 ad80ab7b 2018-08-04 stsp
7332 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7333 e5a0f69f 2018-08-18 stsp if (err)
7334 e5a0f69f 2018-08-18 stsp return err;
7335 ffd1d5e5 2018-06-23 stsp
7336 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7337 e5a0f69f 2018-08-18 stsp free(parent_path);
7338 669b5ffa 2018-10-07 stsp
7339 a5d43cac 2022-07-01 thomas view_border(view);
7340 e5a0f69f 2018-08-18 stsp return err;
7341 07dd3ed3 2022-08-06 thomas }
7342 07dd3ed3 2022-08-06 thomas
7343 07dd3ed3 2022-08-06 thomas static const struct got_error *
7344 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7345 07dd3ed3 2022-08-06 thomas {
7346 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7347 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7348 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7349 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7350 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7351 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7352 07dd3ed3 2022-08-06 thomas
7353 07dd3ed3 2022-08-06 thomas g = view->gline;
7354 07dd3ed3 2022-08-06 thomas view->gline = 0;
7355 07dd3ed3 2022-08-06 thomas
7356 07dd3ed3 2022-08-06 thomas if (g == 0)
7357 07dd3ed3 2022-08-06 thomas g = 1;
7358 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7359 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7360 07dd3ed3 2022-08-06 thomas
7361 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7362 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7363 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7364 07dd3ed3 2022-08-06 thomas
7365 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7366 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7367 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7368 07dd3ed3 2022-08-06 thomas }
7369 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7370 07dd3ed3 2022-08-06 thomas last += off;
7371 07dd3ed3 2022-08-06 thomas
7372 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7373 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7374 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7375 07dd3ed3 2022-08-06 thomas }
7376 07dd3ed3 2022-08-06 thomas
7377 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7378 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7379 07dd3ed3 2022-08-06 thomas i += off;
7380 07dd3ed3 2022-08-06 thomas }
7381 07dd3ed3 2022-08-06 thomas
7382 07dd3ed3 2022-08-06 thomas if (i < g) {
7383 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7384 07dd3ed3 2022-08-06 thomas if (err)
7385 07dd3ed3 2022-08-06 thomas return err;
7386 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7387 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7388 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7389 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7390 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7391 07dd3ed3 2022-08-06 thomas first += off;
7392 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7393 07dd3ed3 2022-08-06 thomas }
7394 07dd3ed3 2022-08-06 thomas } else if (i > g)
7395 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7396 07dd3ed3 2022-08-06 thomas
7397 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7398 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7399 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7400 07dd3ed3 2022-08-06 thomas
7401 07dd3ed3 2022-08-06 thomas return NULL;
7402 e5a0f69f 2018-08-18 stsp }
7403 ce52c690 2018-06-23 stsp
7404 e5a0f69f 2018-08-18 stsp static const struct got_error *
7405 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7406 e5a0f69f 2018-08-18 stsp {
7407 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7408 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7409 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7410 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7411 ffd1d5e5 2018-06-23 stsp
7412 07dd3ed3 2022-08-06 thomas if (view->gline)
7413 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7414 07dd3ed3 2022-08-06 thomas
7415 e5a0f69f 2018-08-18 stsp switch (ch) {
7416 c72de8ab 2023-02-03 thomas case '0':
7417 c72de8ab 2023-02-03 thomas case '$':
7418 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
7419 c72de8ab 2023-02-03 thomas case 'l':
7420 c72de8ab 2023-02-03 thomas case KEY_LEFT:
7421 c72de8ab 2023-02-03 thomas case 'h':
7422 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
7423 c72de8ab 2023-02-03 thomas break;
7424 1e37a5c2 2019-05-12 jcs case 'i':
7425 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7426 07b0611c 2022-06-23 thomas view->count = 0;
7427 1e37a5c2 2019-05-12 jcs break;
7428 1be4947a 2022-07-22 thomas case 'L':
7429 07b0611c 2022-06-23 thomas view->count = 0;
7430 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7431 ffd1d5e5 2018-06-23 stsp break;
7432 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7433 152c1c93 2020-11-29 stsp break;
7434 1be4947a 2022-07-22 thomas case 'R':
7435 07b0611c 2022-06-23 thomas view->count = 0;
7436 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7437 e4526bf5 2021-09-03 naddy break;
7438 e4526bf5 2021-09-03 naddy case 'g':
7439 aa7a1117 2023-01-09 thomas case '=':
7440 e4526bf5 2021-09-03 naddy case KEY_HOME:
7441 e4526bf5 2021-09-03 naddy s->selected = 0;
7442 07b0611c 2022-06-23 thomas view->count = 0;
7443 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7444 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7445 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7446 e4526bf5 2021-09-03 naddy else
7447 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7448 1e37a5c2 2019-05-12 jcs break;
7449 e4526bf5 2021-09-03 naddy case 'G':
7450 aa7a1117 2023-01-09 thomas case '*':
7451 a5d43cac 2022-07-01 thomas case KEY_END: {
7452 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7453 a5d43cac 2022-07-01 thomas
7454 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7455 a5d43cac 2022-07-01 thomas --eos; /* border */
7456 e4526bf5 2021-09-03 naddy s->selected = 0;
7457 07b0611c 2022-06-23 thomas view->count = 0;
7458 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7459 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7460 e4526bf5 2021-09-03 naddy if (te == NULL) {
7461 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7462 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7463 e4526bf5 2021-09-03 naddy n++;
7464 e4526bf5 2021-09-03 naddy }
7465 e4526bf5 2021-09-03 naddy break;
7466 e4526bf5 2021-09-03 naddy }
7467 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7468 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7469 e4526bf5 2021-09-03 naddy }
7470 e4526bf5 2021-09-03 naddy if (n > 0)
7471 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7472 e4526bf5 2021-09-03 naddy break;
7473 a5d43cac 2022-07-01 thomas }
7474 1e37a5c2 2019-05-12 jcs case 'k':
7475 1e37a5c2 2019-05-12 jcs case KEY_UP:
7476 f7140bf5 2021-10-17 thomas case CTRL('p'):
7477 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7478 1e37a5c2 2019-05-12 jcs s->selected--;
7479 fa86c4bf 2020-11-29 stsp break;
7480 1e37a5c2 2019-05-12 jcs }
7481 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7482 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7483 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7484 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7485 07b0611c 2022-06-23 thomas view->count = 0;
7486 1e37a5c2 2019-05-12 jcs break;
7487 70f17a53 2022-06-13 thomas case CTRL('u'):
7488 23427b14 2022-06-23 thomas case 'u':
7489 70f17a53 2022-06-13 thomas nscroll /= 2;
7490 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7491 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7492 ea025d1d 2020-02-22 naddy case CTRL('b'):
7493 1c5e5faa 2022-06-23 thomas case 'b':
7494 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7495 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7496 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7497 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7498 fa86c4bf 2020-11-29 stsp } else {
7499 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7500 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7501 fa86c4bf 2020-11-29 stsp }
7502 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7503 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7504 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7505 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7506 07b0611c 2022-06-23 thomas view->count = 0;
7507 1e37a5c2 2019-05-12 jcs break;
7508 1e37a5c2 2019-05-12 jcs case 'j':
7509 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7510 f7140bf5 2021-10-17 thomas case CTRL('n'):
7511 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7512 1e37a5c2 2019-05-12 jcs s->selected++;
7513 1e37a5c2 2019-05-12 jcs break;
7514 1e37a5c2 2019-05-12 jcs }
7515 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7516 07b0611c 2022-06-23 thomas == NULL) {
7517 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7518 07b0611c 2022-06-23 thomas view->count = 0;
7519 1e37a5c2 2019-05-12 jcs break;
7520 07b0611c 2022-06-23 thomas }
7521 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7522 1e37a5c2 2019-05-12 jcs break;
7523 70f17a53 2022-06-13 thomas case CTRL('d'):
7524 23427b14 2022-06-23 thomas case 'd':
7525 70f17a53 2022-06-13 thomas nscroll /= 2;
7526 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7527 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7528 ea025d1d 2020-02-22 naddy case CTRL('f'):
7529 1c5e5faa 2022-06-23 thomas case 'f':
7530 4c2d69cb 2022-06-23 thomas case ' ':
7531 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7532 1e37a5c2 2019-05-12 jcs == NULL) {
7533 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7534 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7535 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7536 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7537 07b0611c 2022-06-23 thomas else
7538 07b0611c 2022-06-23 thomas view->count = 0;
7539 1e37a5c2 2019-05-12 jcs break;
7540 1e37a5c2 2019-05-12 jcs }
7541 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7542 1e37a5c2 2019-05-12 jcs break;
7543 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7544 1e37a5c2 2019-05-12 jcs case '\r':
7545 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7546 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7547 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7548 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7549 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7550 07b0611c 2022-06-23 thomas view->count = 0;
7551 1e37a5c2 2019-05-12 jcs break;
7552 07b0611c 2022-06-23 thomas }
7553 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7554 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7555 1e37a5c2 2019-05-12 jcs entry);
7556 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7557 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7558 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7559 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7560 1e37a5c2 2019-05-12 jcs s->selected_entry =
7561 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7562 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7563 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7564 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7565 a5d43cac 2022-07-01 thomas if (err)
7566 a5d43cac 2022-07-01 thomas break;
7567 a5d43cac 2022-07-01 thomas }
7568 1e37a5c2 2019-05-12 jcs free(parent);
7569 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7570 56e0773d 2019-11-28 stsp s->selected_entry))) {
7571 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7572 07b0611c 2022-06-23 thomas view->count = 0;
7573 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7574 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7575 1e37a5c2 2019-05-12 jcs if (err)
7576 1e37a5c2 2019-05-12 jcs break;
7577 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7578 941e9f74 2019-05-21 stsp if (err) {
7579 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7580 1e37a5c2 2019-05-12 jcs break;
7581 1e37a5c2 2019-05-12 jcs }
7582 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7583 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7584 1e37a5c2 2019-05-12 jcs break;
7585 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7586 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7587 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7588 07b0611c 2022-06-23 thomas view->count = 0;
7589 1e37a5c2 2019-05-12 jcs break;
7590 1e37a5c2 2019-05-12 jcs default:
7591 07b0611c 2022-06-23 thomas view->count = 0;
7592 1e37a5c2 2019-05-12 jcs break;
7593 ffd1d5e5 2018-06-23 stsp }
7594 e5a0f69f 2018-08-18 stsp
7595 ffd1d5e5 2018-06-23 stsp return err;
7596 9f7d7167 2018-04-29 stsp }
7597 9f7d7167 2018-04-29 stsp
7598 ffd1d5e5 2018-06-23 stsp __dead static void
7599 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7600 ffd1d5e5 2018-06-23 stsp {
7601 ffd1d5e5 2018-06-23 stsp endwin();
7602 91198554 2022-06-23 thomas fprintf(stderr,
7603 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7604 ffd1d5e5 2018-06-23 stsp getprogname());
7605 ffd1d5e5 2018-06-23 stsp exit(1);
7606 ffd1d5e5 2018-06-23 stsp }
7607 ffd1d5e5 2018-06-23 stsp
7608 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7609 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7610 ffd1d5e5 2018-06-23 stsp {
7611 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7612 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7613 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7614 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7615 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7616 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7617 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7618 4e97c21c 2020-12-06 stsp char *label = NULL;
7619 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7620 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7621 ffd1d5e5 2018-06-23 stsp int ch;
7622 5221c383 2018-08-01 stsp struct tog_view *view;
7623 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7624 70ac5f84 2019-03-28 stsp
7625 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7626 ffd1d5e5 2018-06-23 stsp switch (ch) {
7627 ffd1d5e5 2018-06-23 stsp case 'c':
7628 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7629 74283ab8 2020-02-07 stsp break;
7630 74283ab8 2020-02-07 stsp case 'r':
7631 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7632 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7633 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7634 74283ab8 2020-02-07 stsp optarg);
7635 ffd1d5e5 2018-06-23 stsp break;
7636 ffd1d5e5 2018-06-23 stsp default:
7637 e99e2d15 2020-11-24 naddy usage_tree();
7638 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7639 ffd1d5e5 2018-06-23 stsp }
7640 ffd1d5e5 2018-06-23 stsp }
7641 ffd1d5e5 2018-06-23 stsp
7642 ffd1d5e5 2018-06-23 stsp argc -= optind;
7643 ffd1d5e5 2018-06-23 stsp argv += optind;
7644 ffd1d5e5 2018-06-23 stsp
7645 55cccc34 2020-02-20 stsp if (argc > 1)
7646 e99e2d15 2020-11-24 naddy usage_tree();
7647 7cd52833 2022-06-23 thomas
7648 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7649 7cd52833 2022-06-23 thomas if (error != NULL)
7650 7cd52833 2022-06-23 thomas goto done;
7651 74283ab8 2020-02-07 stsp
7652 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7653 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7654 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7655 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7656 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7657 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7658 c156c7a4 2020-12-18 stsp goto done;
7659 55cccc34 2020-02-20 stsp if (worktree)
7660 52185f70 2019-02-05 stsp repo_path =
7661 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7662 55cccc34 2020-02-20 stsp else
7663 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7664 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7665 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7666 c156c7a4 2020-12-18 stsp goto done;
7667 c156c7a4 2020-12-18 stsp }
7668 55cccc34 2020-02-20 stsp }
7669 a915003a 2019-02-05 stsp
7670 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7671 c02c541e 2019-03-29 stsp if (error != NULL)
7672 52185f70 2019-02-05 stsp goto done;
7673 d188b9a6 2019-01-04 stsp
7674 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7675 55cccc34 2020-02-20 stsp repo, worktree);
7676 55cccc34 2020-02-20 stsp if (error)
7677 55cccc34 2020-02-20 stsp goto done;
7678 55cccc34 2020-02-20 stsp
7679 55cccc34 2020-02-20 stsp init_curses();
7680 55cccc34 2020-02-20 stsp
7681 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7682 c02c541e 2019-03-29 stsp if (error)
7683 52185f70 2019-02-05 stsp goto done;
7684 ffd1d5e5 2018-06-23 stsp
7685 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7686 51a10b52 2020-12-26 stsp if (error)
7687 51a10b52 2020-12-26 stsp goto done;
7688 51a10b52 2020-12-26 stsp
7689 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7690 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7691 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7692 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7693 4e97c21c 2020-12-06 stsp if (error)
7694 4e97c21c 2020-12-06 stsp goto done;
7695 4e97c21c 2020-12-06 stsp head_ref_name = label;
7696 4e97c21c 2020-12-06 stsp } else {
7697 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7698 4e97c21c 2020-12-06 stsp if (error == NULL)
7699 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7700 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7701 4e97c21c 2020-12-06 stsp goto done;
7702 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7703 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7704 4e97c21c 2020-12-06 stsp if (error)
7705 4e97c21c 2020-12-06 stsp goto done;
7706 4e97c21c 2020-12-06 stsp }
7707 ffd1d5e5 2018-06-23 stsp
7708 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7709 945f9229 2022-04-16 thomas if (error)
7710 945f9229 2022-04-16 thomas goto done;
7711 945f9229 2022-04-16 thomas
7712 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7713 5221c383 2018-08-01 stsp if (view == NULL) {
7714 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7715 5221c383 2018-08-01 stsp goto done;
7716 5221c383 2018-08-01 stsp }
7717 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7718 ad80ab7b 2018-08-04 stsp if (error)
7719 ad80ab7b 2018-08-04 stsp goto done;
7720 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7721 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7722 d91faf3b 2020-12-01 naddy in_repo_path);
7723 55cccc34 2020-02-20 stsp if (error)
7724 55cccc34 2020-02-20 stsp goto done;
7725 55cccc34 2020-02-20 stsp }
7726 55cccc34 2020-02-20 stsp
7727 55cccc34 2020-02-20 stsp if (worktree) {
7728 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7729 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7730 55cccc34 2020-02-20 stsp worktree = NULL;
7731 55cccc34 2020-02-20 stsp }
7732 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7733 ffd1d5e5 2018-06-23 stsp done:
7734 52185f70 2019-02-05 stsp free(repo_path);
7735 e4a0e26d 2020-02-20 stsp free(cwd);
7736 ffd1d5e5 2018-06-23 stsp free(commit_id);
7737 4e97c21c 2020-12-06 stsp free(label);
7738 486cd271 2020-12-06 stsp if (ref)
7739 486cd271 2020-12-06 stsp got_ref_close(ref);
7740 1d0f4054 2021-06-17 stsp if (repo) {
7741 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7742 1d0f4054 2021-06-17 stsp if (error == NULL)
7743 1d0f4054 2021-06-17 stsp error = close_err;
7744 1d0f4054 2021-06-17 stsp }
7745 7cd52833 2022-06-23 thomas if (pack_fds) {
7746 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7747 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7748 7cd52833 2022-06-23 thomas if (error == NULL)
7749 7cd52833 2022-06-23 thomas error = pack_err;
7750 7cd52833 2022-06-23 thomas }
7751 51a10b52 2020-12-26 stsp tog_free_refs();
7752 ffd1d5e5 2018-06-23 stsp return error;
7753 6458efa5 2020-11-24 stsp }
7754 6458efa5 2020-11-24 stsp
7755 6458efa5 2020-11-24 stsp static const struct got_error *
7756 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7757 6458efa5 2020-11-24 stsp {
7758 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7759 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7760 6458efa5 2020-11-24 stsp
7761 6458efa5 2020-11-24 stsp s->nrefs = 0;
7762 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7763 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7764 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7765 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7766 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7767 6458efa5 2020-11-24 stsp continue;
7768 6458efa5 2020-11-24 stsp
7769 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7770 6458efa5 2020-11-24 stsp if (re == NULL)
7771 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7772 6458efa5 2020-11-24 stsp
7773 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7774 8924d611 2020-12-26 stsp if (re->ref == NULL)
7775 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7776 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7777 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7778 6458efa5 2020-11-24 stsp }
7779 6458efa5 2020-11-24 stsp
7780 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7781 6458efa5 2020-11-24 stsp return NULL;
7782 6458efa5 2020-11-24 stsp }
7783 6458efa5 2020-11-24 stsp
7784 ef20f542 2022-06-26 thomas static void
7785 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7786 6458efa5 2020-11-24 stsp {
7787 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7788 6458efa5 2020-11-24 stsp
7789 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7790 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7791 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7792 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7793 6458efa5 2020-11-24 stsp free(re);
7794 6458efa5 2020-11-24 stsp }
7795 6458efa5 2020-11-24 stsp }
7796 6458efa5 2020-11-24 stsp
7797 6458efa5 2020-11-24 stsp static const struct got_error *
7798 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7799 6458efa5 2020-11-24 stsp {
7800 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7801 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7802 6458efa5 2020-11-24 stsp
7803 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7804 6458efa5 2020-11-24 stsp s->repo = repo;
7805 6458efa5 2020-11-24 stsp
7806 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7807 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7808 6458efa5 2020-11-24 stsp
7809 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7810 6458efa5 2020-11-24 stsp if (err)
7811 6458efa5 2020-11-24 stsp return err;
7812 34ba6917 2020-11-30 stsp
7813 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7814 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7815 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7816 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7817 6458efa5 2020-11-24 stsp if (err)
7818 6458efa5 2020-11-24 stsp goto done;
7819 6458efa5 2020-11-24 stsp
7820 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7821 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7822 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7823 6458efa5 2020-11-24 stsp if (err)
7824 6458efa5 2020-11-24 stsp goto done;
7825 6458efa5 2020-11-24 stsp
7826 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7827 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7828 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7829 2183bbf6 2022-01-23 thomas if (err)
7830 2183bbf6 2022-01-23 thomas goto done;
7831 2183bbf6 2022-01-23 thomas
7832 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7833 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7834 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7835 6458efa5 2020-11-24 stsp if (err)
7836 6458efa5 2020-11-24 stsp goto done;
7837 6458efa5 2020-11-24 stsp }
7838 6458efa5 2020-11-24 stsp
7839 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7840 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7841 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7842 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7843 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7844 6458efa5 2020-11-24 stsp done:
7845 6458efa5 2020-11-24 stsp if (err)
7846 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7847 6458efa5 2020-11-24 stsp return err;
7848 6458efa5 2020-11-24 stsp }
7849 6458efa5 2020-11-24 stsp
7850 6458efa5 2020-11-24 stsp static const struct got_error *
7851 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7852 6458efa5 2020-11-24 stsp {
7853 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7854 6458efa5 2020-11-24 stsp
7855 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7856 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7857 6458efa5 2020-11-24 stsp
7858 6458efa5 2020-11-24 stsp return NULL;
7859 9f7d7167 2018-04-29 stsp }
7860 ce5b7c56 2019-07-09 stsp
7861 6458efa5 2020-11-24 stsp static const struct got_error *
7862 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7863 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7864 6458efa5 2020-11-24 stsp {
7865 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7866 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7867 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7868 6458efa5 2020-11-24 stsp int obj_type;
7869 6458efa5 2020-11-24 stsp
7870 c42c9805 2020-11-24 stsp *commit_id = NULL;
7871 6458efa5 2020-11-24 stsp
7872 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7873 6458efa5 2020-11-24 stsp if (err)
7874 6458efa5 2020-11-24 stsp return err;
7875 6458efa5 2020-11-24 stsp
7876 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7877 6458efa5 2020-11-24 stsp if (err)
7878 6458efa5 2020-11-24 stsp goto done;
7879 6458efa5 2020-11-24 stsp
7880 6458efa5 2020-11-24 stsp switch (obj_type) {
7881 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7882 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7883 6458efa5 2020-11-24 stsp break;
7884 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7885 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7886 6458efa5 2020-11-24 stsp if (err)
7887 6458efa5 2020-11-24 stsp goto done;
7888 c42c9805 2020-11-24 stsp free(obj_id);
7889 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7890 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7891 c42c9805 2020-11-24 stsp if (err)
7892 6458efa5 2020-11-24 stsp goto done;
7893 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7894 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7895 c42c9805 2020-11-24 stsp goto done;
7896 c42c9805 2020-11-24 stsp }
7897 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7898 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7899 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7900 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7901 c42c9805 2020-11-24 stsp goto done;
7902 c42c9805 2020-11-24 stsp }
7903 6458efa5 2020-11-24 stsp break;
7904 6458efa5 2020-11-24 stsp default:
7905 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7906 c42c9805 2020-11-24 stsp break;
7907 6458efa5 2020-11-24 stsp }
7908 6458efa5 2020-11-24 stsp
7909 c42c9805 2020-11-24 stsp done:
7910 c42c9805 2020-11-24 stsp if (tag)
7911 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7912 c42c9805 2020-11-24 stsp if (err) {
7913 c42c9805 2020-11-24 stsp free(*commit_id);
7914 c42c9805 2020-11-24 stsp *commit_id = NULL;
7915 c42c9805 2020-11-24 stsp }
7916 c42c9805 2020-11-24 stsp return err;
7917 c42c9805 2020-11-24 stsp }
7918 c42c9805 2020-11-24 stsp
7919 c42c9805 2020-11-24 stsp static const struct got_error *
7920 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7921 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7922 c42c9805 2020-11-24 stsp {
7923 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7924 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7925 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7926 c42c9805 2020-11-24 stsp
7927 c42c9805 2020-11-24 stsp *new_view = NULL;
7928 c42c9805 2020-11-24 stsp
7929 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7930 c42c9805 2020-11-24 stsp if (err) {
7931 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7932 c42c9805 2020-11-24 stsp return err;
7933 c42c9805 2020-11-24 stsp else
7934 c42c9805 2020-11-24 stsp return NULL;
7935 c42c9805 2020-11-24 stsp }
7936 c42c9805 2020-11-24 stsp
7937 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7938 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7939 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7940 6458efa5 2020-11-24 stsp goto done;
7941 6458efa5 2020-11-24 stsp }
7942 6458efa5 2020-11-24 stsp
7943 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7944 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7945 6458efa5 2020-11-24 stsp done:
7946 6458efa5 2020-11-24 stsp if (err)
7947 6458efa5 2020-11-24 stsp view_close(log_view);
7948 6458efa5 2020-11-24 stsp else
7949 6458efa5 2020-11-24 stsp *new_view = log_view;
7950 c42c9805 2020-11-24 stsp free(commit_id);
7951 6458efa5 2020-11-24 stsp return err;
7952 6458efa5 2020-11-24 stsp }
7953 6458efa5 2020-11-24 stsp
7954 ce5b7c56 2019-07-09 stsp static void
7955 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7956 6458efa5 2020-11-24 stsp {
7957 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7958 34ba6917 2020-11-30 stsp int i = 0;
7959 6458efa5 2020-11-24 stsp
7960 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7961 6458efa5 2020-11-24 stsp return;
7962 6458efa5 2020-11-24 stsp
7963 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7964 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7965 34ba6917 2020-11-30 stsp if (re == NULL)
7966 34ba6917 2020-11-30 stsp break;
7967 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7968 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7969 6458efa5 2020-11-24 stsp }
7970 6458efa5 2020-11-24 stsp }
7971 6458efa5 2020-11-24 stsp
7972 a5d43cac 2022-07-01 thomas static const struct got_error *
7973 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7974 6458efa5 2020-11-24 stsp {
7975 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7976 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7977 6458efa5 2020-11-24 stsp int n = 0;
7978 6458efa5 2020-11-24 stsp
7979 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7980 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7981 6458efa5 2020-11-24 stsp else
7982 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7983 6458efa5 2020-11-24 stsp
7984 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7985 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7986 07dd3ed3 2022-08-06 thomas if (last) {
7987 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7988 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7989 07dd3ed3 2022-08-06 thomas }
7990 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7991 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7992 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7993 6458efa5 2020-11-24 stsp }
7994 6458efa5 2020-11-24 stsp }
7995 a5d43cac 2022-07-01 thomas
7996 a5d43cac 2022-07-01 thomas return NULL;
7997 6458efa5 2020-11-24 stsp }
7998 6458efa5 2020-11-24 stsp
7999 6458efa5 2020-11-24 stsp static const struct got_error *
8000 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
8001 6458efa5 2020-11-24 stsp {
8002 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8003 6458efa5 2020-11-24 stsp
8004 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
8005 6458efa5 2020-11-24 stsp return NULL;
8006 6458efa5 2020-11-24 stsp }
8007 6458efa5 2020-11-24 stsp
8008 6458efa5 2020-11-24 stsp static int
8009 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
8010 6458efa5 2020-11-24 stsp {
8011 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8012 6458efa5 2020-11-24 stsp
8013 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
8014 6458efa5 2020-11-24 stsp 0) == 0;
8015 6458efa5 2020-11-24 stsp }
8016 6458efa5 2020-11-24 stsp
8017 6458efa5 2020-11-24 stsp static const struct got_error *
8018 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8019 6458efa5 2020-11-24 stsp {
8020 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8021 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8022 6458efa5 2020-11-24 stsp
8023 6458efa5 2020-11-24 stsp if (!view->searching) {
8024 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8025 6458efa5 2020-11-24 stsp return NULL;
8026 6458efa5 2020-11-24 stsp }
8027 6458efa5 2020-11-24 stsp
8028 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8029 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8030 6458efa5 2020-11-24 stsp if (s->selected_entry)
8031 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8032 6458efa5 2020-11-24 stsp else
8033 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8034 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8035 6458efa5 2020-11-24 stsp } else {
8036 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8037 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8038 6458efa5 2020-11-24 stsp else
8039 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8040 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8041 6458efa5 2020-11-24 stsp }
8042 6458efa5 2020-11-24 stsp } else {
8043 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8044 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8045 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8046 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8047 6458efa5 2020-11-24 stsp else
8048 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8049 6458efa5 2020-11-24 stsp }
8050 6458efa5 2020-11-24 stsp
8051 6458efa5 2020-11-24 stsp while (1) {
8052 6458efa5 2020-11-24 stsp if (re == NULL) {
8053 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8054 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8055 6458efa5 2020-11-24 stsp return NULL;
8056 6458efa5 2020-11-24 stsp }
8057 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8058 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8059 6458efa5 2020-11-24 stsp else
8060 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8061 6458efa5 2020-11-24 stsp }
8062 6458efa5 2020-11-24 stsp
8063 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8064 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8065 6458efa5 2020-11-24 stsp s->matched_entry = re;
8066 6458efa5 2020-11-24 stsp break;
8067 6458efa5 2020-11-24 stsp }
8068 6458efa5 2020-11-24 stsp
8069 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8070 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8071 6458efa5 2020-11-24 stsp else
8072 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8073 6458efa5 2020-11-24 stsp }
8074 6458efa5 2020-11-24 stsp
8075 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8076 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8077 6458efa5 2020-11-24 stsp s->selected = 0;
8078 6458efa5 2020-11-24 stsp }
8079 6458efa5 2020-11-24 stsp
8080 6458efa5 2020-11-24 stsp return NULL;
8081 6458efa5 2020-11-24 stsp }
8082 6458efa5 2020-11-24 stsp
8083 6458efa5 2020-11-24 stsp static const struct got_error *
8084 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8085 6458efa5 2020-11-24 stsp {
8086 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8087 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8088 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8089 6458efa5 2020-11-24 stsp char *line = NULL;
8090 6458efa5 2020-11-24 stsp wchar_t *wline;
8091 6458efa5 2020-11-24 stsp struct tog_color *tc;
8092 66a70b97 2023-02-03 thomas int width, n, scrollx;
8093 6458efa5 2020-11-24 stsp int limit = view->nlines;
8094 6458efa5 2020-11-24 stsp
8095 6458efa5 2020-11-24 stsp werase(view->window);
8096 6458efa5 2020-11-24 stsp
8097 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8098 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8099 a5d43cac 2022-07-01 thomas --limit; /* border */
8100 6458efa5 2020-11-24 stsp
8101 6458efa5 2020-11-24 stsp if (limit == 0)
8102 6458efa5 2020-11-24 stsp return NULL;
8103 6458efa5 2020-11-24 stsp
8104 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8105 6458efa5 2020-11-24 stsp
8106 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8107 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8108 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8109 6458efa5 2020-11-24 stsp
8110 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8111 6458efa5 2020-11-24 stsp if (err) {
8112 6458efa5 2020-11-24 stsp free(line);
8113 6458efa5 2020-11-24 stsp return err;
8114 6458efa5 2020-11-24 stsp }
8115 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8116 6458efa5 2020-11-24 stsp wstandout(view->window);
8117 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8118 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8119 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8120 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8121 6458efa5 2020-11-24 stsp wstandend(view->window);
8122 6458efa5 2020-11-24 stsp free(wline);
8123 6458efa5 2020-11-24 stsp wline = NULL;
8124 6458efa5 2020-11-24 stsp free(line);
8125 6458efa5 2020-11-24 stsp line = NULL;
8126 6458efa5 2020-11-24 stsp if (--limit <= 0)
8127 6458efa5 2020-11-24 stsp return NULL;
8128 6458efa5 2020-11-24 stsp
8129 6458efa5 2020-11-24 stsp n = 0;
8130 66a70b97 2023-02-03 thomas view->maxx = 0;
8131 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8132 6458efa5 2020-11-24 stsp char *line = NULL;
8133 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8134 6458efa5 2020-11-24 stsp
8135 84227eb1 2022-06-23 thomas if (s->show_date) {
8136 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8137 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8138 84227eb1 2022-06-23 thomas struct got_object_id *id;
8139 84227eb1 2022-06-23 thomas struct tm tm;
8140 84227eb1 2022-06-23 thomas time_t t;
8141 84227eb1 2022-06-23 thomas
8142 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8143 84227eb1 2022-06-23 thomas if (err)
8144 84227eb1 2022-06-23 thomas return err;
8145 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8146 84227eb1 2022-06-23 thomas if (err) {
8147 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8148 84227eb1 2022-06-23 thomas free(id);
8149 84227eb1 2022-06-23 thomas return err;
8150 84227eb1 2022-06-23 thomas }
8151 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8152 84227eb1 2022-06-23 thomas id);
8153 84227eb1 2022-06-23 thomas if (err) {
8154 84227eb1 2022-06-23 thomas free(id);
8155 84227eb1 2022-06-23 thomas return err;
8156 84227eb1 2022-06-23 thomas }
8157 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8158 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8159 84227eb1 2022-06-23 thomas } else {
8160 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8161 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8162 84227eb1 2022-06-23 thomas }
8163 84227eb1 2022-06-23 thomas free(id);
8164 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8165 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8166 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8167 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8168 84227eb1 2022-06-23 thomas }
8169 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8170 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8171 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8172 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8173 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8174 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8175 6458efa5 2020-11-24 stsp struct got_object_id *id;
8176 6458efa5 2020-11-24 stsp char *id_str;
8177 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8178 6458efa5 2020-11-24 stsp if (err)
8179 6458efa5 2020-11-24 stsp return err;
8180 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8181 6458efa5 2020-11-24 stsp if (err) {
8182 6458efa5 2020-11-24 stsp free(id);
8183 6458efa5 2020-11-24 stsp return err;
8184 6458efa5 2020-11-24 stsp }
8185 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8186 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8187 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8188 6458efa5 2020-11-24 stsp free(id);
8189 6458efa5 2020-11-24 stsp free(id_str);
8190 6458efa5 2020-11-24 stsp return err;
8191 6458efa5 2020-11-24 stsp }
8192 6458efa5 2020-11-24 stsp free(id);
8193 6458efa5 2020-11-24 stsp free(id_str);
8194 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8195 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8196 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8197 6458efa5 2020-11-24 stsp
8198 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8199 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8200 6458efa5 2020-11-24 stsp if (err) {
8201 6458efa5 2020-11-24 stsp free(line);
8202 6458efa5 2020-11-24 stsp return err;
8203 6458efa5 2020-11-24 stsp }
8204 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8205 66a70b97 2023-02-03 thomas free(wline);
8206 66a70b97 2023-02-03 thomas wline = NULL;
8207 66a70b97 2023-02-03 thomas
8208 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8209 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8210 66a70b97 2023-02-03 thomas if (err) {
8211 66a70b97 2023-02-03 thomas free(line);
8212 66a70b97 2023-02-03 thomas return err;
8213 66a70b97 2023-02-03 thomas }
8214 6458efa5 2020-11-24 stsp if (n == s->selected) {
8215 6458efa5 2020-11-24 stsp if (view->focussed)
8216 6458efa5 2020-11-24 stsp wstandout(view->window);
8217 6458efa5 2020-11-24 stsp s->selected_entry = re;
8218 6458efa5 2020-11-24 stsp }
8219 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8220 6458efa5 2020-11-24 stsp if (tc)
8221 6458efa5 2020-11-24 stsp wattr_on(view->window,
8222 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8223 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8224 6458efa5 2020-11-24 stsp if (tc)
8225 6458efa5 2020-11-24 stsp wattr_off(view->window,
8226 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8227 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8228 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8229 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8230 6458efa5 2020-11-24 stsp wstandend(view->window);
8231 6458efa5 2020-11-24 stsp free(line);
8232 6458efa5 2020-11-24 stsp free(wline);
8233 6458efa5 2020-11-24 stsp wline = NULL;
8234 6458efa5 2020-11-24 stsp n++;
8235 6458efa5 2020-11-24 stsp s->ndisplayed++;
8236 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8237 6458efa5 2020-11-24 stsp
8238 6458efa5 2020-11-24 stsp limit--;
8239 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8240 6458efa5 2020-11-24 stsp }
8241 6458efa5 2020-11-24 stsp
8242 a5d43cac 2022-07-01 thomas view_border(view);
8243 6458efa5 2020-11-24 stsp return err;
8244 6458efa5 2020-11-24 stsp }
8245 6458efa5 2020-11-24 stsp
8246 6458efa5 2020-11-24 stsp static const struct got_error *
8247 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8248 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8249 c42c9805 2020-11-24 stsp {
8250 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8251 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8252 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8253 c42c9805 2020-11-24 stsp
8254 c42c9805 2020-11-24 stsp *new_view = NULL;
8255 c42c9805 2020-11-24 stsp
8256 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8257 c42c9805 2020-11-24 stsp if (err) {
8258 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8259 c42c9805 2020-11-24 stsp return err;
8260 c42c9805 2020-11-24 stsp else
8261 c42c9805 2020-11-24 stsp return NULL;
8262 c42c9805 2020-11-24 stsp }
8263 c42c9805 2020-11-24 stsp
8264 c42c9805 2020-11-24 stsp
8265 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8266 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8267 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8268 c42c9805 2020-11-24 stsp goto done;
8269 c42c9805 2020-11-24 stsp }
8270 c42c9805 2020-11-24 stsp
8271 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8272 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8273 c42c9805 2020-11-24 stsp if (err)
8274 c42c9805 2020-11-24 stsp goto done;
8275 c42c9805 2020-11-24 stsp
8276 c42c9805 2020-11-24 stsp *new_view = tree_view;
8277 c42c9805 2020-11-24 stsp done:
8278 c42c9805 2020-11-24 stsp free(commit_id);
8279 c42c9805 2020-11-24 stsp return err;
8280 07dd3ed3 2022-08-06 thomas }
8281 07dd3ed3 2022-08-06 thomas
8282 07dd3ed3 2022-08-06 thomas static const struct got_error *
8283 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8284 07dd3ed3 2022-08-06 thomas {
8285 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8286 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8287 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8288 07dd3ed3 2022-08-06 thomas
8289 07dd3ed3 2022-08-06 thomas g = view->gline;
8290 07dd3ed3 2022-08-06 thomas view->gline = 0;
8291 07dd3ed3 2022-08-06 thomas
8292 07dd3ed3 2022-08-06 thomas if (g == 0)
8293 07dd3ed3 2022-08-06 thomas g = 1;
8294 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8295 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8296 07dd3ed3 2022-08-06 thomas
8297 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8298 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8299 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8300 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8301 07dd3ed3 2022-08-06 thomas return NULL;
8302 07dd3ed3 2022-08-06 thomas }
8303 07dd3ed3 2022-08-06 thomas
8304 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8305 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8306 07dd3ed3 2022-08-06 thomas if (err)
8307 07dd3ed3 2022-08-06 thomas return err;
8308 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8309 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8310 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8311 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8312 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8313 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8314 07dd3ed3 2022-08-06 thomas
8315 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8316 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8317 07dd3ed3 2022-08-06 thomas
8318 07dd3ed3 2022-08-06 thomas return NULL;
8319 07dd3ed3 2022-08-06 thomas
8320 c42c9805 2020-11-24 stsp }
8321 07dd3ed3 2022-08-06 thomas
8322 c42c9805 2020-11-24 stsp static const struct got_error *
8323 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8324 6458efa5 2020-11-24 stsp {
8325 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8326 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8327 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8328 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8329 6458efa5 2020-11-24 stsp
8330 07dd3ed3 2022-08-06 thomas if (view->gline)
8331 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8332 07dd3ed3 2022-08-06 thomas
8333 6458efa5 2020-11-24 stsp switch (ch) {
8334 66a70b97 2023-02-03 thomas case '0':
8335 66a70b97 2023-02-03 thomas case '$':
8336 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8337 66a70b97 2023-02-03 thomas case 'l':
8338 66a70b97 2023-02-03 thomas case KEY_LEFT:
8339 66a70b97 2023-02-03 thomas case 'h':
8340 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8341 66a70b97 2023-02-03 thomas break;
8342 6458efa5 2020-11-24 stsp case 'i':
8343 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8344 07b0611c 2022-06-23 thomas view->count = 0;
8345 3bfadbd4 2021-11-20 thomas break;
8346 84227eb1 2022-06-23 thomas case 'm':
8347 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8348 07b0611c 2022-06-23 thomas view->count = 0;
8349 84227eb1 2022-06-23 thomas break;
8350 98182bd0 2021-11-20 thomas case 'o':
8351 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8352 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8353 07b0611c 2022-06-23 thomas view->count = 0;
8354 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8355 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8356 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8357 c591440f 2021-11-20 thomas if (err)
8358 c591440f 2021-11-20 thomas break;
8359 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8360 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8361 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8362 3bfadbd4 2021-11-20 thomas if (err)
8363 3bfadbd4 2021-11-20 thomas break;
8364 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8365 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8366 6458efa5 2020-11-24 stsp break;
8367 6458efa5 2020-11-24 stsp case KEY_ENTER:
8368 6458efa5 2020-11-24 stsp case '\r':
8369 07b0611c 2022-06-23 thomas view->count = 0;
8370 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8371 a5d43cac 2022-07-01 thomas break;
8372 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8373 6458efa5 2020-11-24 stsp break;
8374 1be4947a 2022-07-22 thomas case 'T':
8375 07b0611c 2022-06-23 thomas view->count = 0;
8376 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8377 c42c9805 2020-11-24 stsp break;
8378 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8379 c42c9805 2020-11-24 stsp break;
8380 e4526bf5 2021-09-03 naddy case 'g':
8381 aa7a1117 2023-01-09 thomas case '=':
8382 e4526bf5 2021-09-03 naddy case KEY_HOME:
8383 e4526bf5 2021-09-03 naddy s->selected = 0;
8384 07b0611c 2022-06-23 thomas view->count = 0;
8385 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8386 e4526bf5 2021-09-03 naddy break;
8387 e4526bf5 2021-09-03 naddy case 'G':
8388 aa7a1117 2023-01-09 thomas case '*':
8389 a5d43cac 2022-07-01 thomas case KEY_END: {
8390 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8391 a5d43cac 2022-07-01 thomas
8392 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8393 a5d43cac 2022-07-01 thomas --eos; /* border */
8394 e4526bf5 2021-09-03 naddy s->selected = 0;
8395 07b0611c 2022-06-23 thomas view->count = 0;
8396 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8397 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8398 e4526bf5 2021-09-03 naddy if (re == NULL)
8399 e4526bf5 2021-09-03 naddy break;
8400 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8401 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8402 e4526bf5 2021-09-03 naddy }
8403 e4526bf5 2021-09-03 naddy if (n > 0)
8404 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8405 e4526bf5 2021-09-03 naddy break;
8406 a5d43cac 2022-07-01 thomas }
8407 6458efa5 2020-11-24 stsp case 'k':
8408 6458efa5 2020-11-24 stsp case KEY_UP:
8409 f7140bf5 2021-10-17 thomas case CTRL('p'):
8410 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8411 6458efa5 2020-11-24 stsp s->selected--;
8412 6458efa5 2020-11-24 stsp break;
8413 34ba6917 2020-11-30 stsp }
8414 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8415 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8416 07b0611c 2022-06-23 thomas view->count = 0;
8417 6458efa5 2020-11-24 stsp break;
8418 70f17a53 2022-06-13 thomas case CTRL('u'):
8419 23427b14 2022-06-23 thomas case 'u':
8420 70f17a53 2022-06-13 thomas nscroll /= 2;
8421 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8422 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8423 6458efa5 2020-11-24 stsp case CTRL('b'):
8424 1c5e5faa 2022-06-23 thomas case 'b':
8425 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8426 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8427 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8428 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8429 07b0611c 2022-06-23 thomas view->count = 0;
8430 6458efa5 2020-11-24 stsp break;
8431 6458efa5 2020-11-24 stsp case 'j':
8432 6458efa5 2020-11-24 stsp case KEY_DOWN:
8433 f7140bf5 2021-10-17 thomas case CTRL('n'):
8434 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8435 6458efa5 2020-11-24 stsp s->selected++;
8436 6458efa5 2020-11-24 stsp break;
8437 6458efa5 2020-11-24 stsp }
8438 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8439 6458efa5 2020-11-24 stsp /* can't scroll any further */
8440 07b0611c 2022-06-23 thomas view->count = 0;
8441 6458efa5 2020-11-24 stsp break;
8442 07b0611c 2022-06-23 thomas }
8443 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8444 6458efa5 2020-11-24 stsp break;
8445 70f17a53 2022-06-13 thomas case CTRL('d'):
8446 23427b14 2022-06-23 thomas case 'd':
8447 70f17a53 2022-06-13 thomas nscroll /= 2;
8448 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8449 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8450 6458efa5 2020-11-24 stsp case CTRL('f'):
8451 1c5e5faa 2022-06-23 thomas case 'f':
8452 4c2d69cb 2022-06-23 thomas case ' ':
8453 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8454 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8455 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8456 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8457 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8458 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8459 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8460 07b0611c 2022-06-23 thomas view->count = 0;
8461 6458efa5 2020-11-24 stsp break;
8462 6458efa5 2020-11-24 stsp }
8463 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8464 6458efa5 2020-11-24 stsp break;
8465 6458efa5 2020-11-24 stsp case CTRL('l'):
8466 07b0611c 2022-06-23 thomas view->count = 0;
8467 8924d611 2020-12-26 stsp tog_free_refs();
8468 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8469 8924d611 2020-12-26 stsp if (err)
8470 8924d611 2020-12-26 stsp break;
8471 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8472 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8473 6458efa5 2020-11-24 stsp break;
8474 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8475 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8476 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8477 6458efa5 2020-11-24 stsp break;
8478 6458efa5 2020-11-24 stsp default:
8479 07b0611c 2022-06-23 thomas view->count = 0;
8480 6458efa5 2020-11-24 stsp break;
8481 6458efa5 2020-11-24 stsp }
8482 6458efa5 2020-11-24 stsp
8483 6458efa5 2020-11-24 stsp return err;
8484 6458efa5 2020-11-24 stsp }
8485 6458efa5 2020-11-24 stsp
8486 6458efa5 2020-11-24 stsp __dead static void
8487 6458efa5 2020-11-24 stsp usage_ref(void)
8488 6458efa5 2020-11-24 stsp {
8489 6458efa5 2020-11-24 stsp endwin();
8490 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8491 6458efa5 2020-11-24 stsp getprogname());
8492 6458efa5 2020-11-24 stsp exit(1);
8493 6458efa5 2020-11-24 stsp }
8494 6458efa5 2020-11-24 stsp
8495 6458efa5 2020-11-24 stsp static const struct got_error *
8496 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8497 6458efa5 2020-11-24 stsp {
8498 6458efa5 2020-11-24 stsp const struct got_error *error;
8499 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8500 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8501 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8502 6458efa5 2020-11-24 stsp int ch;
8503 6458efa5 2020-11-24 stsp struct tog_view *view;
8504 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8505 6458efa5 2020-11-24 stsp
8506 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8507 6458efa5 2020-11-24 stsp switch (ch) {
8508 6458efa5 2020-11-24 stsp case 'r':
8509 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8510 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8511 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8512 6458efa5 2020-11-24 stsp optarg);
8513 6458efa5 2020-11-24 stsp break;
8514 6458efa5 2020-11-24 stsp default:
8515 e99e2d15 2020-11-24 naddy usage_ref();
8516 6458efa5 2020-11-24 stsp /* NOTREACHED */
8517 6458efa5 2020-11-24 stsp }
8518 6458efa5 2020-11-24 stsp }
8519 6458efa5 2020-11-24 stsp
8520 6458efa5 2020-11-24 stsp argc -= optind;
8521 6458efa5 2020-11-24 stsp argv += optind;
8522 6458efa5 2020-11-24 stsp
8523 6458efa5 2020-11-24 stsp if (argc > 1)
8524 e99e2d15 2020-11-24 naddy usage_ref();
8525 7cd52833 2022-06-23 thomas
8526 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8527 7cd52833 2022-06-23 thomas if (error != NULL)
8528 7cd52833 2022-06-23 thomas goto done;
8529 6458efa5 2020-11-24 stsp
8530 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8531 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8532 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8533 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8534 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8535 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8536 c156c7a4 2020-12-18 stsp goto done;
8537 6458efa5 2020-11-24 stsp if (worktree)
8538 6458efa5 2020-11-24 stsp repo_path =
8539 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8540 6458efa5 2020-11-24 stsp else
8541 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8542 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8543 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8544 c156c7a4 2020-12-18 stsp goto done;
8545 c156c7a4 2020-12-18 stsp }
8546 6458efa5 2020-11-24 stsp }
8547 6458efa5 2020-11-24 stsp
8548 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8549 6458efa5 2020-11-24 stsp if (error != NULL)
8550 6458efa5 2020-11-24 stsp goto done;
8551 6458efa5 2020-11-24 stsp
8552 6458efa5 2020-11-24 stsp init_curses();
8553 6458efa5 2020-11-24 stsp
8554 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8555 51a10b52 2020-12-26 stsp if (error)
8556 51a10b52 2020-12-26 stsp goto done;
8557 51a10b52 2020-12-26 stsp
8558 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8559 6458efa5 2020-11-24 stsp if (error)
8560 6458efa5 2020-11-24 stsp goto done;
8561 6458efa5 2020-11-24 stsp
8562 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8563 6458efa5 2020-11-24 stsp if (view == NULL) {
8564 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8565 6458efa5 2020-11-24 stsp goto done;
8566 6458efa5 2020-11-24 stsp }
8567 6458efa5 2020-11-24 stsp
8568 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8569 6458efa5 2020-11-24 stsp if (error)
8570 6458efa5 2020-11-24 stsp goto done;
8571 6458efa5 2020-11-24 stsp
8572 6458efa5 2020-11-24 stsp if (worktree) {
8573 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8574 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8575 6458efa5 2020-11-24 stsp worktree = NULL;
8576 6458efa5 2020-11-24 stsp }
8577 6458efa5 2020-11-24 stsp error = view_loop(view);
8578 6458efa5 2020-11-24 stsp done:
8579 6458efa5 2020-11-24 stsp free(repo_path);
8580 6458efa5 2020-11-24 stsp free(cwd);
8581 1d0f4054 2021-06-17 stsp if (repo) {
8582 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8583 1d0f4054 2021-06-17 stsp if (close_err)
8584 1d0f4054 2021-06-17 stsp error = close_err;
8585 1d0f4054 2021-06-17 stsp }
8586 7cd52833 2022-06-23 thomas if (pack_fds) {
8587 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8588 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8589 7cd52833 2022-06-23 thomas if (error == NULL)
8590 7cd52833 2022-06-23 thomas error = pack_err;
8591 7cd52833 2022-06-23 thomas }
8592 51a10b52 2020-12-26 stsp tog_free_refs();
8593 6458efa5 2020-11-24 stsp return error;
8594 6458efa5 2020-11-24 stsp }
8595 6458efa5 2020-11-24 stsp
8596 fc2737d5 2022-09-15 thomas static const struct got_error*
8597 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8598 fc2737d5 2022-09-15 thomas const char *str)
8599 fc2737d5 2022-09-15 thomas {
8600 fc2737d5 2022-09-15 thomas size_t len;
8601 fc2737d5 2022-09-15 thomas
8602 fc2737d5 2022-09-15 thomas if (win == NULL)
8603 fc2737d5 2022-09-15 thomas win = stdscr;
8604 fc2737d5 2022-09-15 thomas
8605 fc2737d5 2022-09-15 thomas len = strlen(str);
8606 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8607 fc2737d5 2022-09-15 thomas
8608 fc2737d5 2022-09-15 thomas if (focus)
8609 fc2737d5 2022-09-15 thomas wstandout(win);
8610 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8611 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8612 fc2737d5 2022-09-15 thomas if (focus)
8613 fc2737d5 2022-09-15 thomas wstandend(win);
8614 fc2737d5 2022-09-15 thomas
8615 fc2737d5 2022-09-15 thomas return NULL;
8616 fc2737d5 2022-09-15 thomas }
8617 fc2737d5 2022-09-15 thomas
8618 2a31b33b 2022-07-23 thomas static const struct got_error *
8619 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8620 fc2737d5 2022-09-15 thomas {
8621 fc2737d5 2022-09-15 thomas off_t *p;
8622 fc2737d5 2022-09-15 thomas
8623 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8624 fc2737d5 2022-09-15 thomas if (p == NULL) {
8625 fc2737d5 2022-09-15 thomas free(*line_offsets);
8626 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8627 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8628 fc2737d5 2022-09-15 thomas }
8629 fc2737d5 2022-09-15 thomas
8630 fc2737d5 2022-09-15 thomas *line_offsets = p;
8631 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8632 fc2737d5 2022-09-15 thomas ++(*nlines);
8633 fc2737d5 2022-09-15 thomas return NULL;
8634 fc2737d5 2022-09-15 thomas }
8635 fc2737d5 2022-09-15 thomas
8636 fc2737d5 2022-09-15 thomas static const struct got_error *
8637 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8638 fc2737d5 2022-09-15 thomas {
8639 fc2737d5 2022-09-15 thomas *ret = 0;
8640 fc2737d5 2022-09-15 thomas
8641 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8642 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8643 fc2737d5 2022-09-15 thomas size_t len = 1;
8644 fc2737d5 2022-09-15 thomas
8645 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8646 fc2737d5 2022-09-15 thomas continue;
8647 fc2737d5 2022-09-15 thomas
8648 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8649 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8650 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8651 fc2737d5 2022-09-15 thomas
8652 fc2737d5 2022-09-15 thomas len += strlen(t);
8653 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8654 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8655 fc2737d5 2022-09-15 thomas free(t0);
8656 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8657 fc2737d5 2022-09-15 thomas }
8658 fc2737d5 2022-09-15 thomas
8659 fc2737d5 2022-09-15 thomas return NULL;
8660 fc2737d5 2022-09-15 thomas }
8661 fc2737d5 2022-09-15 thomas
8662 fc2737d5 2022-09-15 thomas /*
8663 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8664 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8665 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8666 fc2737d5 2022-09-15 thomas */
8667 fc2737d5 2022-09-15 thomas static const struct got_error *
8668 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8669 fc2737d5 2022-09-15 thomas {
8670 fc2737d5 2022-09-15 thomas int n, len = width;
8671 fc2737d5 2022-09-15 thomas
8672 fc2737d5 2022-09-15 thomas if (km->keys) {
8673 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8674 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8675 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8676 30e77f6a 2022-09-18 thomas };
8677 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8678 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8679 fc2737d5 2022-09-15 thomas
8680 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8681 fc2737d5 2022-09-15 thomas
8682 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8683 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8684 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8685 fc2737d5 2022-09-15 thomas
8686 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8687 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8688 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8689 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8690 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8691 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8692 fc2737d5 2022-09-15 thomas if (n < 0) {
8693 fc2737d5 2022-09-15 thomas free(t0);
8694 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8695 fc2737d5 2022-09-15 thomas }
8696 fc2737d5 2022-09-15 thomas first = 0;
8697 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8698 fc2737d5 2022-09-15 thomas *off += n;
8699 fc2737d5 2022-09-15 thomas }
8700 fc2737d5 2022-09-15 thomas free(t0);
8701 fc2737d5 2022-09-15 thomas }
8702 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8703 fc2737d5 2022-09-15 thomas if (n < 0)
8704 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8705 fc2737d5 2022-09-15 thomas *off += n;
8706 fc2737d5 2022-09-15 thomas
8707 fc2737d5 2022-09-15 thomas return NULL;
8708 fc2737d5 2022-09-15 thomas }
8709 fc2737d5 2022-09-15 thomas
8710 fc2737d5 2022-09-15 thomas static const struct got_error *
8711 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
8712 fc2737d5 2022-09-15 thomas {
8713 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8714 fc2737d5 2022-09-15 thomas off_t off = 0;
8715 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
8716 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
8717 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
8718 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8719 fc2737d5 2022-09-15 thomas GENERATE_HELP
8720 fc2737d5 2022-09-15 thomas #undef KEYMAP_
8721 fc2737d5 2022-09-15 thomas #undef KEY_
8722 fc2737d5 2022-09-15 thomas };
8723 fc2737d5 2022-09-15 thomas
8724 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8725 fc2737d5 2022-09-15 thomas if (err)
8726 fc2737d5 2022-09-15 thomas return err;
8727 fc2737d5 2022-09-15 thomas
8728 fc2737d5 2022-09-15 thomas n = nitems(km);
8729 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
8730 fc2737d5 2022-09-15 thomas if (err)
8731 fc2737d5 2022-09-15 thomas return err;
8732 fc2737d5 2022-09-15 thomas
8733 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
8734 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
8735 fc2737d5 2022-09-15 thomas show = s->all;
8736 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
8737 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
8738 fc2737d5 2022-09-15 thomas show = 1;
8739 fc2737d5 2022-09-15 thomas }
8740 fc2737d5 2022-09-15 thomas if (show) {
8741 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
8742 fc2737d5 2022-09-15 thomas if (err)
8743 fc2737d5 2022-09-15 thomas return err;
8744 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8745 fc2737d5 2022-09-15 thomas if (err)
8746 fc2737d5 2022-09-15 thomas return err;
8747 fc2737d5 2022-09-15 thomas }
8748 fc2737d5 2022-09-15 thomas }
8749 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
8750 fc2737d5 2022-09-15 thomas ++off;
8751 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8752 fc2737d5 2022-09-15 thomas return err;
8753 fc2737d5 2022-09-15 thomas }
8754 fc2737d5 2022-09-15 thomas
8755 fc2737d5 2022-09-15 thomas static const struct got_error *
8756 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
8757 fc2737d5 2022-09-15 thomas {
8758 fc2737d5 2022-09-15 thomas FILE *f;
8759 fc2737d5 2022-09-15 thomas const struct got_error *err;
8760 fc2737d5 2022-09-15 thomas
8761 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8762 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8763 fc2737d5 2022-09-15 thomas s->nlines = 0;
8764 fc2737d5 2022-09-15 thomas
8765 fc2737d5 2022-09-15 thomas f = got_opentemp();
8766 fc2737d5 2022-09-15 thomas if (f == NULL)
8767 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
8768 fc2737d5 2022-09-15 thomas s->f = f;
8769 fc2737d5 2022-09-15 thomas
8770 fc2737d5 2022-09-15 thomas err = format_help(s);
8771 fc2737d5 2022-09-15 thomas if (err)
8772 fc2737d5 2022-09-15 thomas return err;
8773 fc2737d5 2022-09-15 thomas
8774 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
8775 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
8776 fc2737d5 2022-09-15 thomas
8777 fc2737d5 2022-09-15 thomas return NULL;
8778 fc2737d5 2022-09-15 thomas }
8779 fc2737d5 2022-09-15 thomas
8780 fc2737d5 2022-09-15 thomas static const struct got_error *
8781 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
8782 fc2737d5 2022-09-15 thomas {
8783 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
8784 fc2737d5 2022-09-15 thomas return NULL;
8785 fc2737d5 2022-09-15 thomas }
8786 fc2737d5 2022-09-15 thomas
8787 2a7d3cd7 2022-09-17 thomas static void
8788 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8789 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
8790 2a7d3cd7 2022-09-17 thomas {
8791 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
8792 2a7d3cd7 2022-09-17 thomas
8793 2a7d3cd7 2022-09-17 thomas *f = s->f;
8794 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
8795 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
8796 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
8797 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
8798 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
8799 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
8800 2a7d3cd7 2022-09-17 thomas }
8801 2a7d3cd7 2022-09-17 thomas
8802 fc2737d5 2022-09-15 thomas static const struct got_error *
8803 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
8804 fc2737d5 2022-09-15 thomas {
8805 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8806 fc2737d5 2022-09-15 thomas const struct got_error *err;
8807 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
8808 fc2737d5 2022-09-15 thomas wchar_t *wline;
8809 fc2737d5 2022-09-15 thomas char *line;
8810 fc2737d5 2022-09-15 thomas ssize_t linelen;
8811 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8812 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
8813 fc2737d5 2022-09-15 thomas int eos = view->nlines;
8814 fc2737d5 2022-09-15 thomas
8815 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8816 fc2737d5 2022-09-15 thomas --eos; /* account for border */
8817 fc2737d5 2022-09-15 thomas
8818 fc2737d5 2022-09-15 thomas s->lineno = 0;
8819 fc2737d5 2022-09-15 thomas rewind(s->f);
8820 fc2737d5 2022-09-15 thomas werase(view->window);
8821 fc2737d5 2022-09-15 thomas
8822 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
8823 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
8824 fc2737d5 2022-09-15 thomas
8825 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
8826 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
8827 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
8828 fc2737d5 2022-09-15 thomas if (err)
8829 fc2737d5 2022-09-15 thomas return err;
8830 fc2737d5 2022-09-15 thomas if (eos <= 1)
8831 fc2737d5 2022-09-15 thomas return NULL;
8832 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
8833 fc2737d5 2022-09-15 thomas eos -= 2;
8834 fc2737d5 2022-09-15 thomas
8835 fc2737d5 2022-09-15 thomas s->eof = 0;
8836 fc2737d5 2022-09-15 thomas view->maxx = 0;
8837 fc2737d5 2022-09-15 thomas line = NULL;
8838 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
8839 fc2737d5 2022-09-15 thomas attr_t attr = 0;
8840 fc2737d5 2022-09-15 thomas
8841 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8842 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8843 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
8844 fc2737d5 2022-09-15 thomas free(line);
8845 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
8846 fc2737d5 2022-09-15 thomas }
8847 fc2737d5 2022-09-15 thomas s->eof = 1;
8848 fc2737d5 2022-09-15 thomas break;
8849 fc2737d5 2022-09-15 thomas }
8850 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
8851 fc2737d5 2022-09-15 thomas continue;
8852 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8853 fc2737d5 2022-09-15 thomas continue;
8854 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
8855 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
8856 fc2737d5 2022-09-15 thomas
8857 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8858 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
8859 fc2737d5 2022-09-15 thomas if (err) {
8860 fc2737d5 2022-09-15 thomas free(line);
8861 fc2737d5 2022-09-15 thomas return err;
8862 fc2737d5 2022-09-15 thomas }
8863 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
8864 fc2737d5 2022-09-15 thomas free(wline);
8865 fc2737d5 2022-09-15 thomas wline = NULL;
8866 fc2737d5 2022-09-15 thomas
8867 fc2737d5 2022-09-15 thomas if (attr)
8868 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
8869 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
8870 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8871 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
8872 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
8873 fc2737d5 2022-09-15 thomas if (err) {
8874 fc2737d5 2022-09-15 thomas free(line);
8875 fc2737d5 2022-09-15 thomas return err;
8876 fc2737d5 2022-09-15 thomas }
8877 fc2737d5 2022-09-15 thomas } else {
8878 fc2737d5 2022-09-15 thomas int skip;
8879 fc2737d5 2022-09-15 thomas
8880 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
8881 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
8882 fc2737d5 2022-09-15 thomas if (err) {
8883 fc2737d5 2022-09-15 thomas free(line);
8884 fc2737d5 2022-09-15 thomas return err;
8885 fc2737d5 2022-09-15 thomas }
8886 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
8887 fc2737d5 2022-09-15 thomas free(wline);
8888 fc2737d5 2022-09-15 thomas wline = NULL;
8889 fc2737d5 2022-09-15 thomas }
8890 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
8891 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
8892 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
8893 fc2737d5 2022-09-15 thomas } else {
8894 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8895 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
8896 fc2737d5 2022-09-15 thomas }
8897 fc2737d5 2022-09-15 thomas if (attr)
8898 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
8899 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
8900 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
8901 fc2737d5 2022-09-15 thomas }
8902 fc2737d5 2022-09-15 thomas free(line);
8903 fc2737d5 2022-09-15 thomas if (nprinted > 0)
8904 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8905 fc2737d5 2022-09-15 thomas else
8906 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
8907 fc2737d5 2022-09-15 thomas
8908 fc2737d5 2022-09-15 thomas view_border(view);
8909 fc2737d5 2022-09-15 thomas
8910 fc2737d5 2022-09-15 thomas if (s->eof) {
8911 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
8912 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
8913 fc2737d5 2022-09-15 thomas view->ncols - 1);
8914 fc2737d5 2022-09-15 thomas if (rc == ERR)
8915 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8916 fc2737d5 2022-09-15 thomas } else {
8917 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
8918 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
8919 fc2737d5 2022-09-15 thomas wstandout(view->window);
8920 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
8921 fc2737d5 2022-09-15 thomas view->ncols - 1);
8922 fc2737d5 2022-09-15 thomas if (rc == ERR)
8923 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8924 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
8925 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
8926 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
8927 fc2737d5 2022-09-15 thomas if (rc == ERR)
8928 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
8929 fc2737d5 2022-09-15 thomas }
8930 fc2737d5 2022-09-15 thomas wstandend(view->window);
8931 fc2737d5 2022-09-15 thomas }
8932 fc2737d5 2022-09-15 thomas
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 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8938 fc2737d5 2022-09-15 thomas {
8939 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8940 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8941 fc2737d5 2022-09-15 thomas char *line = NULL;
8942 fc2737d5 2022-09-15 thomas ssize_t linelen;
8943 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8944 fc2737d5 2022-09-15 thomas int eos, nscroll;
8945 fc2737d5 2022-09-15 thomas
8946 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
8947 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8948 fc2737d5 2022-09-15 thomas --eos; /* border */
8949 fc2737d5 2022-09-15 thomas
8950 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
8951 fc2737d5 2022-09-15 thomas
8952 fc2737d5 2022-09-15 thomas switch (ch) {
8953 fc2737d5 2022-09-15 thomas case '0':
8954 fc2737d5 2022-09-15 thomas case '$':
8955 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
8956 fc2737d5 2022-09-15 thomas case 'l':
8957 fc2737d5 2022-09-15 thomas case KEY_LEFT:
8958 fc2737d5 2022-09-15 thomas case 'h':
8959 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8960 fc2737d5 2022-09-15 thomas break;
8961 fc2737d5 2022-09-15 thomas case 'g':
8962 fc2737d5 2022-09-15 thomas case KEY_HOME:
8963 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8964 fc2737d5 2022-09-15 thomas view->count = 0;
8965 fc2737d5 2022-09-15 thomas break;
8966 fc2737d5 2022-09-15 thomas case 'G':
8967 fc2737d5 2022-09-15 thomas case KEY_END:
8968 fc2737d5 2022-09-15 thomas view->count = 0;
8969 fc2737d5 2022-09-15 thomas if (s->eof)
8970 fc2737d5 2022-09-15 thomas break;
8971 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
8972 fc2737d5 2022-09-15 thomas s->eof = 1;
8973 fc2737d5 2022-09-15 thomas break;
8974 fc2737d5 2022-09-15 thomas case 'k':
8975 fc2737d5 2022-09-15 thomas case KEY_UP:
8976 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
8977 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
8978 fc2737d5 2022-09-15 thomas else
8979 fc2737d5 2022-09-15 thomas view->count = 0;
8980 fc2737d5 2022-09-15 thomas break;
8981 fc2737d5 2022-09-15 thomas case CTRL('u'):
8982 fc2737d5 2022-09-15 thomas case 'u':
8983 fc2737d5 2022-09-15 thomas nscroll /= 2;
8984 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8985 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
8986 fc2737d5 2022-09-15 thomas case CTRL('b'):
8987 fc2737d5 2022-09-15 thomas case 'b':
8988 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
8989 fc2737d5 2022-09-15 thomas view->count = 0;
8990 fc2737d5 2022-09-15 thomas break;
8991 fc2737d5 2022-09-15 thomas }
8992 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
8993 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
8994 fc2737d5 2022-09-15 thomas break;
8995 fc2737d5 2022-09-15 thomas case 'j':
8996 fc2737d5 2022-09-15 thomas case KEY_DOWN:
8997 fc2737d5 2022-09-15 thomas case CTRL('n'):
8998 fc2737d5 2022-09-15 thomas if (!s->eof)
8999 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
9000 fc2737d5 2022-09-15 thomas else
9001 fc2737d5 2022-09-15 thomas view->count = 0;
9002 fc2737d5 2022-09-15 thomas break;
9003 fc2737d5 2022-09-15 thomas case CTRL('d'):
9004 fc2737d5 2022-09-15 thomas case 'd':
9005 fc2737d5 2022-09-15 thomas nscroll /= 2;
9006 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
9007 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
9008 fc2737d5 2022-09-15 thomas case CTRL('f'):
9009 fc2737d5 2022-09-15 thomas case 'f':
9010 fc2737d5 2022-09-15 thomas case ' ':
9011 fc2737d5 2022-09-15 thomas if (s->eof) {
9012 fc2737d5 2022-09-15 thomas view->count = 0;
9013 fc2737d5 2022-09-15 thomas break;
9014 fc2737d5 2022-09-15 thomas }
9015 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9016 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9017 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9018 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9019 fc2737d5 2022-09-15 thomas if (feof(s->f))
9020 fc2737d5 2022-09-15 thomas s->eof = 1;
9021 fc2737d5 2022-09-15 thomas else
9022 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9023 fc2737d5 2022-09-15 thomas break;
9024 fc2737d5 2022-09-15 thomas }
9025 fc2737d5 2022-09-15 thomas }
9026 fc2737d5 2022-09-15 thomas free(line);
9027 fc2737d5 2022-09-15 thomas break;
9028 fc2737d5 2022-09-15 thomas default:
9029 fc2737d5 2022-09-15 thomas view->count = 0;
9030 fc2737d5 2022-09-15 thomas break;
9031 fc2737d5 2022-09-15 thomas }
9032 fc2737d5 2022-09-15 thomas
9033 fc2737d5 2022-09-15 thomas return err;
9034 fc2737d5 2022-09-15 thomas }
9035 fc2737d5 2022-09-15 thomas
9036 fc2737d5 2022-09-15 thomas static const struct got_error *
9037 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9038 fc2737d5 2022-09-15 thomas {
9039 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9040 fc2737d5 2022-09-15 thomas
9041 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9042 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9043 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9044 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9045 fc2737d5 2022-09-15 thomas
9046 fc2737d5 2022-09-15 thomas return NULL;
9047 fc2737d5 2022-09-15 thomas }
9048 fc2737d5 2022-09-15 thomas
9049 fc2737d5 2022-09-15 thomas static const struct got_error *
9050 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9051 fc2737d5 2022-09-15 thomas {
9052 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9053 fc2737d5 2022-09-15 thomas
9054 fc2737d5 2022-09-15 thomas
9055 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9056 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9057 fc2737d5 2022-09-15 thomas
9058 fc2737d5 2022-09-15 thomas wclear(view->window);
9059 fc2737d5 2022-09-15 thomas view->count = 0;
9060 fc2737d5 2022-09-15 thomas view->x = 0;
9061 fc2737d5 2022-09-15 thomas s->all = !s->all;
9062 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9063 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9064 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9065 fc2737d5 2022-09-15 thomas
9066 fc2737d5 2022-09-15 thomas return create_help(s);
9067 fc2737d5 2022-09-15 thomas }
9068 fc2737d5 2022-09-15 thomas
9069 fc2737d5 2022-09-15 thomas static const struct got_error *
9070 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9071 fc2737d5 2022-09-15 thomas {
9072 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9073 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9074 fc2737d5 2022-09-15 thomas
9075 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9076 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9077 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9078 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9079 fc2737d5 2022-09-15 thomas
9080 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9081 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9082 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9083 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9084 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9085 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9086 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9087 fc2737d5 2022-09-15 thomas
9088 fc2737d5 2022-09-15 thomas err = create_help(s);
9089 fc2737d5 2022-09-15 thomas return err;
9090 fc2737d5 2022-09-15 thomas }
9091 fc2737d5 2022-09-15 thomas
9092 fc2737d5 2022-09-15 thomas static const struct got_error *
9093 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9094 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9095 2a31b33b 2022-07-23 thomas {
9096 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9097 2a31b33b 2022-07-23 thomas
9098 2a31b33b 2022-07-23 thomas *new_view = NULL;
9099 2a31b33b 2022-07-23 thomas
9100 2a31b33b 2022-07-23 thomas switch (request) {
9101 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9102 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9103 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9104 2a31b33b 2022-07-23 thomas
9105 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9106 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9107 2a31b33b 2022-07-23 thomas view, s->repo);
9108 2a31b33b 2022-07-23 thomas } else
9109 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9110 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9111 2a31b33b 2022-07-23 thomas break;
9112 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9113 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9114 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9115 2a31b33b 2022-07-23 thomas
9116 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9117 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9118 2a31b33b 2022-07-23 thomas s->repo);
9119 2a31b33b 2022-07-23 thomas } else
9120 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9121 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9122 2a31b33b 2022-07-23 thomas break;
9123 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9124 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9125 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9126 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9127 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9128 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9129 2a31b33b 2022-07-23 thomas &view->state.tree);
9130 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9131 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9132 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9133 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9134 2a31b33b 2022-07-23 thomas else
9135 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9136 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9137 2a31b33b 2022-07-23 thomas break;
9138 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9139 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9140 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9141 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9142 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9143 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9144 2a31b33b 2022-07-23 thomas view->state.log.repo);
9145 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9146 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9147 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9148 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9149 2a31b33b 2022-07-23 thomas else
9150 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9151 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9152 2a31b33b 2022-07-23 thomas break;
9153 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9154 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9155 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9156 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9157 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9158 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9159 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9160 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9161 2a31b33b 2022-07-23 thomas else
9162 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9163 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9164 2a31b33b 2022-07-23 thomas if (err)
9165 2a31b33b 2022-07-23 thomas view_close(*new_view);
9166 2a31b33b 2022-07-23 thomas break;
9167 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9168 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9169 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9170 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9171 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9172 fc2737d5 2022-09-15 thomas if (err)
9173 fc2737d5 2022-09-15 thomas view_close(*new_view);
9174 fc2737d5 2022-09-15 thomas break;
9175 2a31b33b 2022-07-23 thomas default:
9176 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9177 2a31b33b 2022-07-23 thomas }
9178 2a31b33b 2022-07-23 thomas
9179 2a31b33b 2022-07-23 thomas return err;
9180 2a31b33b 2022-07-23 thomas }
9181 2a31b33b 2022-07-23 thomas
9182 a5d43cac 2022-07-01 thomas /*
9183 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9184 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9185 a5d43cac 2022-07-01 thomas */
9186 6458efa5 2020-11-24 stsp static void
9187 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9188 a5d43cac 2022-07-01 thomas {
9189 a5d43cac 2022-07-01 thomas switch (view->type) {
9190 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9191 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9192 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9193 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9194 a5d43cac 2022-07-01 thomas 1);
9195 a5d43cac 2022-07-01 thomas break;
9196 a5d43cac 2022-07-01 thomas }
9197 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9198 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9199 a5d43cac 2022-07-01 thomas else
9200 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9201 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9202 a5d43cac 2022-07-01 thomas break;
9203 a5d43cac 2022-07-01 thomas }
9204 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9205 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9206 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9207 a5d43cac 2022-07-01 thomas break;
9208 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9209 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9210 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9211 a5d43cac 2022-07-01 thomas break;
9212 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9213 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9214 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9215 a5d43cac 2022-07-01 thomas break;
9216 a5d43cac 2022-07-01 thomas default:
9217 a5d43cac 2022-07-01 thomas break;
9218 a5d43cac 2022-07-01 thomas }
9219 a5d43cac 2022-07-01 thomas
9220 a5d43cac 2022-07-01 thomas view->offset = 0;
9221 a5d43cac 2022-07-01 thomas }
9222 a5d43cac 2022-07-01 thomas
9223 a5d43cac 2022-07-01 thomas /*
9224 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9225 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9226 a5d43cac 2022-07-01 thomas */
9227 a5d43cac 2022-07-01 thomas static const struct got_error *
9228 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9229 a5d43cac 2022-07-01 thomas {
9230 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9231 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9232 a5d43cac 2022-07-01 thomas int *selected = NULL;
9233 a5d43cac 2022-07-01 thomas int header, offset;
9234 a5d43cac 2022-07-01 thomas
9235 a5d43cac 2022-07-01 thomas switch (view->type) {
9236 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9237 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9238 a5d43cac 2022-07-01 thomas header = 3;
9239 a5d43cac 2022-07-01 thomas scrolld = NULL;
9240 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9241 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9242 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9243 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9244 a5d43cac 2022-07-01 thomas view->offset = offset;
9245 a5d43cac 2022-07-01 thomas }
9246 a5d43cac 2022-07-01 thomas break;
9247 a5d43cac 2022-07-01 thomas }
9248 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9249 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9250 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9251 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9252 a5d43cac 2022-07-01 thomas selected = &s->selected;
9253 a5d43cac 2022-07-01 thomas break;
9254 a5d43cac 2022-07-01 thomas }
9255 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9256 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9257 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9258 a5d43cac 2022-07-01 thomas header = 3;
9259 a5d43cac 2022-07-01 thomas selected = &s->selected;
9260 a5d43cac 2022-07-01 thomas break;
9261 a5d43cac 2022-07-01 thomas }
9262 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9263 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9264 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9265 a5d43cac 2022-07-01 thomas header = 5;
9266 a5d43cac 2022-07-01 thomas selected = &s->selected;
9267 a5d43cac 2022-07-01 thomas break;
9268 a5d43cac 2022-07-01 thomas }
9269 a5d43cac 2022-07-01 thomas default:
9270 a5d43cac 2022-07-01 thomas selected = NULL;
9271 a5d43cac 2022-07-01 thomas scrolld = NULL;
9272 a5d43cac 2022-07-01 thomas header = 0;
9273 a5d43cac 2022-07-01 thomas break;
9274 a5d43cac 2022-07-01 thomas }
9275 a5d43cac 2022-07-01 thomas
9276 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9277 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9278 a5d43cac 2022-07-01 thomas view->offset = offset;
9279 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9280 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9281 a5d43cac 2022-07-01 thomas *selected -= offset;
9282 a5d43cac 2022-07-01 thomas }
9283 a5d43cac 2022-07-01 thomas }
9284 a5d43cac 2022-07-01 thomas
9285 a5d43cac 2022-07-01 thomas return err;
9286 a5d43cac 2022-07-01 thomas }
9287 a5d43cac 2022-07-01 thomas
9288 a5d43cac 2022-07-01 thomas static void
9289 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9290 ce5b7c56 2019-07-09 stsp {
9291 6059809a 2020-12-17 stsp size_t i;
9292 9f7d7167 2018-04-29 stsp
9293 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9294 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9295 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9296 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9297 ce5b7c56 2019-07-09 stsp }
9298 6879ba42 2020-10-01 naddy fputc('\n', fp);
9299 ce5b7c56 2019-07-09 stsp }
9300 ce5b7c56 2019-07-09 stsp
9301 4ed7e80c 2018-05-20 stsp __dead static void
9302 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9303 9f7d7167 2018-04-29 stsp {
9304 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9305 6879ba42 2020-10-01 naddy
9306 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9307 6879ba42 2020-10-01 naddy getprogname());
9308 6879ba42 2020-10-01 naddy if (hflag) {
9309 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9310 6879ba42 2020-10-01 naddy list_commands(fp);
9311 ee85c5e8 2020-02-29 stsp }
9312 6879ba42 2020-10-01 naddy exit(status);
9313 9f7d7167 2018-04-29 stsp }
9314 9f7d7167 2018-04-29 stsp
9315 c2301be8 2018-04-30 stsp static char **
9316 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9317 c2301be8 2018-04-30 stsp {
9318 ee85c5e8 2020-02-29 stsp va_list ap;
9319 c2301be8 2018-04-30 stsp char **argv;
9320 ee85c5e8 2020-02-29 stsp int i;
9321 c2301be8 2018-04-30 stsp
9322 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9323 ee85c5e8 2020-02-29 stsp
9324 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9325 c2301be8 2018-04-30 stsp if (argv == NULL)
9326 c2301be8 2018-04-30 stsp err(1, "calloc");
9327 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9328 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9329 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9330 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9331 c2301be8 2018-04-30 stsp }
9332 c2301be8 2018-04-30 stsp
9333 ee85c5e8 2020-02-29 stsp va_end(ap);
9334 c2301be8 2018-04-30 stsp return argv;
9335 ee85c5e8 2020-02-29 stsp }
9336 ee85c5e8 2020-02-29 stsp
9337 ee85c5e8 2020-02-29 stsp /*
9338 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9339 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9340 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9341 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9342 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9343 ee85c5e8 2020-02-29 stsp */
9344 ee85c5e8 2020-02-29 stsp static const struct got_error *
9345 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9346 ee85c5e8 2020-02-29 stsp {
9347 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9348 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9349 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9350 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9351 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9352 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9353 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9354 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9355 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9356 84de9106 2020-12-26 stsp
9357 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9358 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9359 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9360 ee85c5e8 2020-02-29 stsp
9361 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9362 7cd52833 2022-06-23 thomas if (error != NULL)
9363 7cd52833 2022-06-23 thomas goto done;
9364 7cd52833 2022-06-23 thomas
9365 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9366 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9367 ee85c5e8 2020-02-29 stsp goto done;
9368 ee85c5e8 2020-02-29 stsp
9369 ee85c5e8 2020-02-29 stsp if (worktree)
9370 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9371 ee85c5e8 2020-02-29 stsp else
9372 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9373 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9374 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9375 ee85c5e8 2020-02-29 stsp goto done;
9376 ee85c5e8 2020-02-29 stsp }
9377 ee85c5e8 2020-02-29 stsp
9378 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9379 ee85c5e8 2020-02-29 stsp if (error != NULL)
9380 ee85c5e8 2020-02-29 stsp goto done;
9381 ee85c5e8 2020-02-29 stsp
9382 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9383 ee85c5e8 2020-02-29 stsp repo, worktree);
9384 ee85c5e8 2020-02-29 stsp if (error)
9385 ee85c5e8 2020-02-29 stsp goto done;
9386 ee85c5e8 2020-02-29 stsp
9387 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9388 84de9106 2020-12-26 stsp if (error)
9389 84de9106 2020-12-26 stsp goto done;
9390 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9391 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9392 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9393 ee85c5e8 2020-02-29 stsp if (error)
9394 ee85c5e8 2020-02-29 stsp goto done;
9395 ee85c5e8 2020-02-29 stsp
9396 ee85c5e8 2020-02-29 stsp if (worktree) {
9397 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9398 ee85c5e8 2020-02-29 stsp worktree = NULL;
9399 ee85c5e8 2020-02-29 stsp }
9400 ee85c5e8 2020-02-29 stsp
9401 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9402 945f9229 2022-04-16 thomas if (error)
9403 945f9229 2022-04-16 thomas goto done;
9404 945f9229 2022-04-16 thomas
9405 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9406 ee85c5e8 2020-02-29 stsp if (error) {
9407 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9408 ee85c5e8 2020-02-29 stsp goto done;
9409 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9410 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9411 6879ba42 2020-10-01 naddy usage(1, 1);
9412 ee85c5e8 2020-02-29 stsp /* not reached */
9413 ee85c5e8 2020-02-29 stsp }
9414 ee85c5e8 2020-02-29 stsp
9415 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9416 ee85c5e8 2020-02-29 stsp if (error)
9417 ee85c5e8 2020-02-29 stsp goto done;
9418 ee85c5e8 2020-02-29 stsp
9419 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9420 ee85c5e8 2020-02-29 stsp argc = 4;
9421 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9422 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9423 ee85c5e8 2020-02-29 stsp done:
9424 1d0f4054 2021-06-17 stsp if (repo) {
9425 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9426 1d0f4054 2021-06-17 stsp if (error == NULL)
9427 1d0f4054 2021-06-17 stsp error = close_err;
9428 1d0f4054 2021-06-17 stsp }
9429 945f9229 2022-04-16 thomas if (commit)
9430 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9431 ee85c5e8 2020-02-29 stsp if (worktree)
9432 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9433 7cd52833 2022-06-23 thomas if (pack_fds) {
9434 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9435 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9436 7cd52833 2022-06-23 thomas if (error == NULL)
9437 7cd52833 2022-06-23 thomas error = pack_err;
9438 7cd52833 2022-06-23 thomas }
9439 ee85c5e8 2020-02-29 stsp free(id);
9440 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9441 ee85c5e8 2020-02-29 stsp free(commit_id);
9442 ee85c5e8 2020-02-29 stsp free(cwd);
9443 ee85c5e8 2020-02-29 stsp free(repo_path);
9444 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9445 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9446 ee85c5e8 2020-02-29 stsp int i;
9447 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9448 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9449 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9450 ee85c5e8 2020-02-29 stsp }
9451 87670572 2020-12-26 naddy tog_free_refs();
9452 ee85c5e8 2020-02-29 stsp return error;
9453 c2301be8 2018-04-30 stsp }
9454 c2301be8 2018-04-30 stsp
9455 9f7d7167 2018-04-29 stsp int
9456 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9457 9f7d7167 2018-04-29 stsp {
9458 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9459 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9460 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9461 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9462 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9463 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9464 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9465 83cd27f8 2020-01-13 stsp };
9466 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9467 a0abeae5 2023-02-17 thomas
9468 a0abeae5 2023-02-17 thomas setlocale(LC_CTYPE, "");
9469 a0abeae5 2023-02-17 thomas
9470 a0abeae5 2023-02-17 thomas #ifndef PROFILE
9471 a0abeae5 2023-02-17 thomas if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9472 a0abeae5 2023-02-17 thomas NULL) == -1)
9473 a0abeae5 2023-02-17 thomas err(1, "pledge");
9474 a0abeae5 2023-02-17 thomas #endif
9475 9f7d7167 2018-04-29 stsp
9476 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9477 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9478 cacf1690 2022-09-06 thomas
9479 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9480 9f7d7167 2018-04-29 stsp switch (ch) {
9481 9f7d7167 2018-04-29 stsp case 'h':
9482 9f7d7167 2018-04-29 stsp hflag = 1;
9483 9f7d7167 2018-04-29 stsp break;
9484 53ccebc2 2019-07-30 stsp case 'V':
9485 53ccebc2 2019-07-30 stsp Vflag = 1;
9486 53ccebc2 2019-07-30 stsp break;
9487 9f7d7167 2018-04-29 stsp default:
9488 6879ba42 2020-10-01 naddy usage(hflag, 1);
9489 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9490 9f7d7167 2018-04-29 stsp }
9491 9f7d7167 2018-04-29 stsp }
9492 9f7d7167 2018-04-29 stsp
9493 9f7d7167 2018-04-29 stsp argc -= optind;
9494 9f7d7167 2018-04-29 stsp argv += optind;
9495 9814e6a3 2020-09-27 naddy optind = 1;
9496 c2301be8 2018-04-30 stsp optreset = 1;
9497 9f7d7167 2018-04-29 stsp
9498 53ccebc2 2019-07-30 stsp if (Vflag) {
9499 53ccebc2 2019-07-30 stsp got_version_print_str();
9500 6879ba42 2020-10-01 naddy return 0;
9501 53ccebc2 2019-07-30 stsp }
9502 4010e238 2020-12-04 stsp
9503 c2301be8 2018-04-30 stsp if (argc == 0) {
9504 f29d3e89 2018-06-23 stsp if (hflag)
9505 6879ba42 2020-10-01 naddy usage(hflag, 0);
9506 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9507 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9508 c2301be8 2018-04-30 stsp argc = 1;
9509 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9510 c2301be8 2018-04-30 stsp } else {
9511 6059809a 2020-12-17 stsp size_t i;
9512 9f7d7167 2018-04-29 stsp
9513 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9514 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9515 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9516 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9517 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9518 9f7d7167 2018-04-29 stsp break;
9519 9f7d7167 2018-04-29 stsp }
9520 9f7d7167 2018-04-29 stsp }
9521 ee85c5e8 2020-02-29 stsp }
9522 3642c4c6 2019-07-09 stsp
9523 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9524 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9525 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9526 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9527 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9528 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9529 adf4c9e0 2022-07-03 thomas }
9530 adf4c9e0 2022-07-03 thomas
9531 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9532 ee85c5e8 2020-02-29 stsp if (argc != 1)
9533 6879ba42 2020-10-01 naddy usage(0, 1);
9534 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9535 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9536 ee85c5e8 2020-02-29 stsp } else {
9537 ee85c5e8 2020-02-29 stsp if (hflag)
9538 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9539 ee85c5e8 2020-02-29 stsp else
9540 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9541 9f7d7167 2018-04-29 stsp }
9542 9f7d7167 2018-04-29 stsp
9543 9f7d7167 2018-04-29 stsp endwin();
9544 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9545 a2f4a359 2020-02-28 stsp int i;
9546 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9547 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9548 a2f4a359 2020-02-28 stsp free(cmd_argv);
9549 a2f4a359 2020-02-28 stsp }
9550 a2f4a359 2020-02-28 stsp
9551 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9552 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9553 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9554 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9555 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9556 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9557 9f7d7167 2018-04-29 stsp return 0;
9558 9f7d7167 2018-04-29 stsp }