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 9f7d7167 2018-04-29 stsp
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 def2f970 2021-09-28 thomas #endif
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
44 9f7d7167 2018-04-29 stsp
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.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 fc2737d5 2022-09-15 thomas KEY_("g Home", "Go to line N (default: first line)"), \
542 fc2737d5 2022-09-15 thomas KEY_("G End", "Go to line N (default: last line)"), \
543 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
544 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
545 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
546 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
547 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
548 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
549 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
550 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
551 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
552 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
553 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
554 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
555 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
556 fc2737d5 2022-09-15 thomas \
557 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
558 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
559 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
560 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
561 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
562 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
563 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
564 fc2737d5 2022-09-15 thomas " commit"), \
565 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
566 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
567 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
568 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
569 fc2737d5 2022-09-15 thomas \
570 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
571 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
572 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
573 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
574 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
575 fc2737d5 2022-09-15 thomas " data"), \
576 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
577 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
578 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
579 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
580 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
581 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
582 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
583 fc2737d5 2022-09-15 thomas \
584 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
585 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
586 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
587 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
588 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
589 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
590 fc2737d5 2022-09-15 thomas " selected line's commit"), \
591 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
592 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
593 fc2737d5 2022-09-15 thomas \
594 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
595 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
596 fc2737d5 2022-09-15 thomas " selected file"), \
597 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
598 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
599 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
600 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
601 fc2737d5 2022-09-15 thomas \
602 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
603 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
604 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
605 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
606 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
607 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
608 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
609 fc2737d5 2022-09-15 thomas
610 fc2737d5 2022-09-15 thomas struct tog_key_map {
611 fc2737d5 2022-09-15 thomas const char *keys;
612 fc2737d5 2022-09-15 thomas const char *info;
613 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
614 7cbe629d 2018-08-04 stsp };
615 7cbe629d 2018-08-04 stsp
616 669b5ffa 2018-10-07 stsp /*
617 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
618 669b5ffa 2018-10-07 stsp *
619 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
620 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
621 669b5ffa 2018-10-07 stsp * there is enough screen estate.
622 669b5ffa 2018-10-07 stsp *
623 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
624 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
625 669b5ffa 2018-10-07 stsp *
626 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
627 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
628 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
629 669b5ffa 2018-10-07 stsp *
630 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
631 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
632 669b5ffa 2018-10-07 stsp */
633 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
634 669b5ffa 2018-10-07 stsp
635 cc3c9aac 2018-08-01 stsp struct tog_view {
636 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
637 26ed57b2 2018-05-19 stsp WINDOW *window;
638 26ed57b2 2018-05-19 stsp PANEL *panel;
639 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
640 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
641 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
642 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
643 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
644 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
645 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
646 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
647 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
648 9970f7fc 2020-12-03 stsp int dying;
649 669b5ffa 2018-10-07 stsp struct tog_view *parent;
650 669b5ffa 2018-10-07 stsp struct tog_view *child;
651 5dc9f4bc 2018-08-04 stsp
652 e78dc838 2020-12-04 stsp /*
653 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
654 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
655 e78dc838 2020-12-04 stsp * between parent and child.
656 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
657 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
658 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
659 e78dc838 2020-12-04 stsp * situations.
660 e78dc838 2020-12-04 stsp */
661 e78dc838 2020-12-04 stsp int focus_child;
662 e78dc838 2020-12-04 stsp
663 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
664 5dc9f4bc 2018-08-04 stsp /* type-specific state */
665 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
666 5dc9f4bc 2018-08-04 stsp union {
667 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
668 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
669 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
670 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
671 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
672 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
673 5dc9f4bc 2018-08-04 stsp } state;
674 e5a0f69f 2018-08-18 stsp
675 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
676 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
677 e78dc838 2020-12-04 stsp struct tog_view *, int);
678 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
679 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
680 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
681 60493ae3 2019-06-20 stsp
682 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
683 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
684 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
685 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
686 c0c4acc8 2021-01-24 stsp int search_started;
687 60493ae3 2019-06-20 stsp int searching;
688 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
689 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
690 60493ae3 2019-06-20 stsp int search_next_done;
691 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
692 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
693 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
694 1803e47f 2019-06-22 stsp regex_t regex;
695 41605754 2020-11-12 stsp regmatch_t regmatch;
696 cc3c9aac 2018-08-01 stsp };
697 cd0acaa7 2018-05-20 stsp
698 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
699 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
700 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
701 78756c87 2020-11-24 stsp struct got_repository *);
702 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
703 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
704 e78dc838 2020-12-04 stsp struct tog_view *, int);
705 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
706 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
707 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
708 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
709 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
710 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
711 e5a0f69f 2018-08-18 stsp
712 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
713 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
714 78756c87 2020-11-24 stsp const char *, const char *, int);
715 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
716 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
717 e78dc838 2020-12-04 stsp struct tog_view *, int);
718 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
719 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
720 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
721 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
722 e5a0f69f 2018-08-18 stsp
723 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
724 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
725 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
726 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
727 e78dc838 2020-12-04 stsp struct tog_view *, int);
728 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
729 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
730 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
731 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
732 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
733 e5a0f69f 2018-08-18 stsp
734 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
735 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
736 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
737 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
738 e78dc838 2020-12-04 stsp struct tog_view *, int);
739 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
740 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
741 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
742 6458efa5 2020-11-24 stsp
743 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
744 6458efa5 2020-11-24 stsp struct got_repository *);
745 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
746 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
747 e78dc838 2020-12-04 stsp struct tog_view *, int);
748 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
749 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
750 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
751 2a7d3cd7 2022-09-17 thomas
752 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
753 2a7d3cd7 2022-09-17 thomas struct tog_view *);
754 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
755 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
756 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
757 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
758 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
759 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
760 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
761 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
762 25791caa 2018-10-24 stsp
763 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
764 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
765 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
766 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
767 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
768 25791caa 2018-10-24 stsp
769 25791caa 2018-10-24 stsp static void
770 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
771 25791caa 2018-10-24 stsp {
772 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
773 83baff54 2019-08-12 stsp }
774 83baff54 2019-08-12 stsp
775 83baff54 2019-08-12 stsp static void
776 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
777 83baff54 2019-08-12 stsp {
778 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
779 25791caa 2018-10-24 stsp }
780 26ed57b2 2018-05-19 stsp
781 61266923 2020-01-14 stsp static void
782 61266923 2020-01-14 stsp tog_sigcont(int signo)
783 61266923 2020-01-14 stsp {
784 61266923 2020-01-14 stsp tog_sigcont_received = 1;
785 61266923 2020-01-14 stsp }
786 61266923 2020-01-14 stsp
787 296152d1 2022-05-31 thomas static void
788 296152d1 2022-05-31 thomas tog_sigint(int signo)
789 296152d1 2022-05-31 thomas {
790 296152d1 2022-05-31 thomas tog_sigint_received = 1;
791 296152d1 2022-05-31 thomas }
792 296152d1 2022-05-31 thomas
793 296152d1 2022-05-31 thomas static void
794 296152d1 2022-05-31 thomas tog_sigterm(int signo)
795 296152d1 2022-05-31 thomas {
796 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
797 296152d1 2022-05-31 thomas }
798 296152d1 2022-05-31 thomas
799 296152d1 2022-05-31 thomas static int
800 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
801 296152d1 2022-05-31 thomas {
802 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
803 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
804 296152d1 2022-05-31 thomas }
805 296152d1 2022-05-31 thomas
806 e5a0f69f 2018-08-18 stsp static const struct got_error *
807 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
808 ea5e7bb5 2018-08-01 stsp {
809 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
810 e5a0f69f 2018-08-18 stsp
811 669b5ffa 2018-10-07 stsp if (view->child) {
812 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
813 669b5ffa 2018-10-07 stsp view->child = NULL;
814 669b5ffa 2018-10-07 stsp }
815 e5a0f69f 2018-08-18 stsp if (view->close)
816 e5a0f69f 2018-08-18 stsp err = view->close(view);
817 ea5e7bb5 2018-08-01 stsp if (view->panel)
818 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
819 ea5e7bb5 2018-08-01 stsp if (view->window)
820 ea5e7bb5 2018-08-01 stsp delwin(view->window);
821 ea5e7bb5 2018-08-01 stsp free(view);
822 f2d749db 2022-07-12 thomas return err ? err : child_err;
823 ea5e7bb5 2018-08-01 stsp }
824 ea5e7bb5 2018-08-01 stsp
825 ea5e7bb5 2018-08-01 stsp static struct tog_view *
826 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
827 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
828 ea5e7bb5 2018-08-01 stsp {
829 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
830 ea5e7bb5 2018-08-01 stsp
831 ea5e7bb5 2018-08-01 stsp if (view == NULL)
832 ea5e7bb5 2018-08-01 stsp return NULL;
833 ea5e7bb5 2018-08-01 stsp
834 d6b05b5b 2018-08-04 stsp view->type = type;
835 f7d12f7e 2018-08-01 stsp view->lines = LINES;
836 f7d12f7e 2018-08-01 stsp view->cols = COLS;
837 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
838 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
839 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
840 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
841 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
842 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
843 96a765a8 2018-08-04 stsp view_close(view);
844 ea5e7bb5 2018-08-01 stsp return NULL;
845 ea5e7bb5 2018-08-01 stsp }
846 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
847 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
848 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
849 96a765a8 2018-08-04 stsp view_close(view);
850 ea5e7bb5 2018-08-01 stsp return NULL;
851 ea5e7bb5 2018-08-01 stsp }
852 ea5e7bb5 2018-08-01 stsp
853 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
854 ea5e7bb5 2018-08-01 stsp return view;
855 cdf1ee82 2018-08-01 stsp }
856 cdf1ee82 2018-08-01 stsp
857 0cf4efb1 2018-09-29 stsp static int
858 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
859 0cf4efb1 2018-09-29 stsp {
860 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
861 0cf4efb1 2018-09-29 stsp return 0;
862 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
863 5c60c32a 2018-10-18 stsp }
864 5c60c32a 2018-10-18 stsp
865 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
866 a5d43cac 2022-07-01 thomas static int
867 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
868 a5d43cac 2022-07-01 thomas {
869 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
870 a5d43cac 2022-07-01 thomas }
871 a5d43cac 2022-07-01 thomas
872 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
873 5c60c32a 2018-10-18 stsp
874 5c60c32a 2018-10-18 stsp static const struct got_error *
875 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
876 5c60c32a 2018-10-18 stsp {
877 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
878 5c60c32a 2018-10-18 stsp
879 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
880 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
881 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
882 53d2bdd3 2022-07-10 thomas else
883 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
884 a5d43cac 2022-07-01 thomas view->begin_x = 0;
885 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
886 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
887 53d2bdd3 2022-07-10 thomas view->cols > 119)
888 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
889 53d2bdd3 2022-07-10 thomas else
890 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
891 a5d43cac 2022-07-01 thomas view->begin_y = 0;
892 a5d43cac 2022-07-01 thomas }
893 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
894 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
895 5c60c32a 2018-10-18 stsp view->lines = LINES;
896 5c60c32a 2018-10-18 stsp view->cols = COLS;
897 5c60c32a 2018-10-18 stsp err = view_resize(view);
898 5c60c32a 2018-10-18 stsp if (err)
899 5c60c32a 2018-10-18 stsp return err;
900 5c60c32a 2018-10-18 stsp
901 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
902 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
903 a5d43cac 2022-07-01 thomas
904 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
905 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
906 5c60c32a 2018-10-18 stsp
907 5c60c32a 2018-10-18 stsp return NULL;
908 5c60c32a 2018-10-18 stsp }
909 5c60c32a 2018-10-18 stsp
910 5c60c32a 2018-10-18 stsp static const struct got_error *
911 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
912 5c60c32a 2018-10-18 stsp {
913 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
914 5c60c32a 2018-10-18 stsp
915 5c60c32a 2018-10-18 stsp view->begin_x = 0;
916 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
917 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
918 5c60c32a 2018-10-18 stsp view->ncols = COLS;
919 5c60c32a 2018-10-18 stsp view->lines = LINES;
920 5c60c32a 2018-10-18 stsp view->cols = COLS;
921 5c60c32a 2018-10-18 stsp err = view_resize(view);
922 5c60c32a 2018-10-18 stsp if (err)
923 5c60c32a 2018-10-18 stsp return err;
924 5c60c32a 2018-10-18 stsp
925 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
926 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
927 5c60c32a 2018-10-18 stsp
928 5c60c32a 2018-10-18 stsp return NULL;
929 0cf4efb1 2018-09-29 stsp }
930 0cf4efb1 2018-09-29 stsp
931 5c60c32a 2018-10-18 stsp static int
932 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
933 5c60c32a 2018-10-18 stsp {
934 5c60c32a 2018-10-18 stsp return view->parent == NULL;
935 5c60c32a 2018-10-18 stsp }
936 5c60c32a 2018-10-18 stsp
937 b65b3ea0 2022-06-23 thomas static int
938 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
939 b65b3ea0 2022-06-23 thomas {
940 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
941 e44940c3 2022-07-01 thomas }
942 e44940c3 2022-07-01 thomas
943 e44940c3 2022-07-01 thomas static int
944 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
945 e44940c3 2022-07-01 thomas {
946 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
947 b65b3ea0 2022-06-23 thomas }
948 b65b3ea0 2022-06-23 thomas
949 444d5325 2022-07-03 thomas static int
950 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
951 444d5325 2022-07-03 thomas {
952 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
953 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
954 444d5325 2022-07-03 thomas }
955 444d5325 2022-07-03 thomas
956 a5d43cac 2022-07-01 thomas static void
957 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
958 a5d43cac 2022-07-01 thomas {
959 a5d43cac 2022-07-01 thomas PANEL *panel;
960 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
961 b65b3ea0 2022-06-23 thomas
962 a5d43cac 2022-07-01 thomas if (view->parent)
963 a5d43cac 2022-07-01 thomas return view_border(view->parent);
964 a5d43cac 2022-07-01 thomas
965 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
966 a5d43cac 2022-07-01 thomas if (panel == NULL)
967 a5d43cac 2022-07-01 thomas return;
968 a5d43cac 2022-07-01 thomas
969 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
970 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
971 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
972 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
973 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
974 a5d43cac 2022-07-01 thomas else
975 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
976 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
977 a5d43cac 2022-07-01 thomas }
978 a5d43cac 2022-07-01 thomas
979 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
980 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
981 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
982 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
983 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
984 a5d43cac 2022-07-01 thomas
985 4d8c2215 2018-08-19 stsp static const struct got_error *
986 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
987 f7d12f7e 2018-08-01 stsp {
988 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
989 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
990 f7d12f7e 2018-08-01 stsp
991 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
992 a5d43cac 2022-07-01 thomas
993 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
994 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
995 0cf4efb1 2018-09-29 stsp else
996 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
997 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
998 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
999 0cf4efb1 2018-09-29 stsp else
1000 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1001 6d0fee91 2018-08-01 stsp
1002 4918811f 2022-07-01 thomas if (view->child) {
1003 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1004 a5d43cac 2022-07-01 thomas
1005 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1006 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1007 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1008 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1009 40236d76 2022-06-23 thomas ncols = COLS;
1010 40236d76 2022-06-23 thomas
1011 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1012 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1013 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1014 5c60c32a 2018-10-18 stsp else
1015 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1016 5c60c32a 2018-10-18 stsp } else {
1017 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1018 40236d76 2022-06-23 thomas
1019 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1020 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1021 a5d43cac 2022-07-01 thomas }
1022 a5d43cac 2022-07-01 thomas /*
1023 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1024 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1025 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1026 a5d43cac 2022-07-01 thomas */
1027 a5d43cac 2022-07-01 thomas if (hs) {
1028 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1029 a5d43cac 2022-07-01 thomas if (err)
1030 a5d43cac 2022-07-01 thomas return err;
1031 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1032 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1033 a5d43cac 2022-07-01 thomas if (err)
1034 a5d43cac 2022-07-01 thomas return err;
1035 a5d43cac 2022-07-01 thomas }
1036 a5d43cac 2022-07-01 thomas view_border(view);
1037 a5d43cac 2022-07-01 thomas update_panels();
1038 a5d43cac 2022-07-01 thomas doupdate();
1039 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1040 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1041 a5d43cac 2022-07-01 thomas }
1042 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1043 40236d76 2022-06-23 thomas ncols = COLS;
1044 669b5ffa 2018-10-07 stsp
1045 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1046 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1047 ea0bff04 2022-07-19 thomas if (err)
1048 ea0bff04 2022-07-19 thomas return err;
1049 ea0bff04 2022-07-19 thomas }
1050 ea0bff04 2022-07-19 thomas
1051 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1052 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1053 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1054 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1055 40236d76 2022-06-23 thomas wclear(view->window);
1056 40236d76 2022-06-23 thomas
1057 40236d76 2022-06-23 thomas view->nlines = nlines;
1058 40236d76 2022-06-23 thomas view->ncols = ncols;
1059 40236d76 2022-06-23 thomas view->lines = LINES;
1060 40236d76 2022-06-23 thomas view->cols = COLS;
1061 40236d76 2022-06-23 thomas
1062 5c60c32a 2018-10-18 stsp return NULL;
1063 ea0bff04 2022-07-19 thomas }
1064 ea0bff04 2022-07-19 thomas
1065 ea0bff04 2022-07-19 thomas static const struct got_error *
1066 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1067 ea0bff04 2022-07-19 thomas {
1068 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1069 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1070 3a0139e8 2022-07-22 thomas int n = 0;
1071 ea0bff04 2022-07-19 thomas
1072 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1073 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1074 3a0139e8 2022-07-22 thomas
1075 ea0bff04 2022-07-19 thomas /*
1076 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1077 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1078 ea0bff04 2022-07-19 thomas */
1079 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1080 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1081 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1082 ea0bff04 2022-07-19 thomas }
1083 ea0bff04 2022-07-19 thomas
1084 ea0bff04 2022-07-19 thomas return err;
1085 97cb21cd 2022-07-12 thomas }
1086 97cb21cd 2022-07-12 thomas
1087 97cb21cd 2022-07-12 thomas static void
1088 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1089 97cb21cd 2022-07-12 thomas {
1090 97cb21cd 2022-07-12 thomas if (n == 0)
1091 97cb21cd 2022-07-12 thomas return;
1092 97cb21cd 2022-07-12 thomas
1093 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1094 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1095 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1096 97cb21cd 2022-07-12 thomas else
1097 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1098 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1099 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1100 97cb21cd 2022-07-12 thomas view->offset -= n;
1101 97cb21cd 2022-07-12 thomas else
1102 97cb21cd 2022-07-12 thomas view->offset = 0;
1103 97cb21cd 2022-07-12 thomas }
1104 53d2bdd3 2022-07-10 thomas }
1105 53d2bdd3 2022-07-10 thomas
1106 53d2bdd3 2022-07-10 thomas static const struct got_error *
1107 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1108 53d2bdd3 2022-07-10 thomas {
1109 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1110 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1111 53d2bdd3 2022-07-10 thomas
1112 53d2bdd3 2022-07-10 thomas if (view->parent)
1113 53d2bdd3 2022-07-10 thomas v = view->parent;
1114 53d2bdd3 2022-07-10 thomas else
1115 53d2bdd3 2022-07-10 thomas v = view;
1116 53d2bdd3 2022-07-10 thomas
1117 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1118 53d2bdd3 2022-07-10 thomas return NULL;
1119 53d2bdd3 2022-07-10 thomas
1120 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1121 53d2bdd3 2022-07-10 thomas
1122 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1123 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1124 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1125 53d2bdd3 2022-07-10 thomas if (view->parent)
1126 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1127 53d2bdd3 2022-07-10 thomas else
1128 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1129 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1130 53d2bdd3 2022-07-10 thomas view->count = 0;
1131 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1132 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1133 53d2bdd3 2022-07-10 thomas view->count = 0;
1134 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1135 53d2bdd3 2022-07-10 thomas }
1136 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1137 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1138 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1139 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1140 53d2bdd3 2022-07-10 thomas if (err)
1141 53d2bdd3 2022-07-10 thomas return err;
1142 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1143 53d2bdd3 2022-07-10 thomas } else {
1144 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1145 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1146 53d2bdd3 2022-07-10 thomas if (view->parent)
1147 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1148 53d2bdd3 2022-07-10 thomas else
1149 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1150 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1151 53d2bdd3 2022-07-10 thomas view->count = 0;
1152 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1153 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1154 53d2bdd3 2022-07-10 thomas view->count = 0;
1155 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1156 53d2bdd3 2022-07-10 thomas }
1157 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1158 53d2bdd3 2022-07-10 thomas }
1159 53d2bdd3 2022-07-10 thomas
1160 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1161 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1162 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1163 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1164 53d2bdd3 2022-07-10 thomas
1165 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1166 53d2bdd3 2022-07-10 thomas if (err)
1167 53d2bdd3 2022-07-10 thomas return err;
1168 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1169 53d2bdd3 2022-07-10 thomas if (err)
1170 53d2bdd3 2022-07-10 thomas return err;
1171 53d2bdd3 2022-07-10 thomas
1172 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1173 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1174 53d2bdd3 2022-07-10 thomas if (err)
1175 53d2bdd3 2022-07-10 thomas return err;
1176 53d2bdd3 2022-07-10 thomas }
1177 53d2bdd3 2022-07-10 thomas
1178 3a0139e8 2022-07-22 thomas if (v->resize)
1179 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1180 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1181 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1182 53d2bdd3 2022-07-10 thomas
1183 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1184 53d2bdd3 2022-07-10 thomas
1185 53d2bdd3 2022-07-10 thomas return err;
1186 53d2bdd3 2022-07-10 thomas }
1187 53d2bdd3 2022-07-10 thomas
1188 53d2bdd3 2022-07-10 thomas static void
1189 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1190 53d2bdd3 2022-07-10 thomas {
1191 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1192 53d2bdd3 2022-07-10 thomas
1193 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1194 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1195 669b5ffa 2018-10-07 stsp }
1196 669b5ffa 2018-10-07 stsp
1197 669b5ffa 2018-10-07 stsp static const struct got_error *
1198 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1199 669b5ffa 2018-10-07 stsp {
1200 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1201 669b5ffa 2018-10-07 stsp
1202 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1203 669b5ffa 2018-10-07 stsp return NULL;
1204 669b5ffa 2018-10-07 stsp
1205 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1206 669b5ffa 2018-10-07 stsp view->child = NULL;
1207 669b5ffa 2018-10-07 stsp return err;
1208 669b5ffa 2018-10-07 stsp }
1209 669b5ffa 2018-10-07 stsp
1210 40236d76 2022-06-23 thomas static const struct got_error *
1211 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1212 669b5ffa 2018-10-07 stsp {
1213 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1214 53d2bdd3 2022-07-10 thomas
1215 669b5ffa 2018-10-07 stsp view->child = child;
1216 669b5ffa 2018-10-07 stsp child->parent = view;
1217 40236d76 2022-06-23 thomas
1218 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1219 53d2bdd3 2022-07-10 thomas if (err)
1220 53d2bdd3 2022-07-10 thomas return err;
1221 53d2bdd3 2022-07-10 thomas
1222 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1223 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1224 53d2bdd3 2022-07-10 thomas
1225 53d2bdd3 2022-07-10 thomas return err;
1226 bfddd0d9 2018-09-29 stsp }
1227 2a31b33b 2022-07-23 thomas
1228 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1229 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1230 2a31b33b 2022-07-23 thomas
1231 2a31b33b 2022-07-23 thomas static const struct got_error *
1232 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1233 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1234 2a31b33b 2022-07-23 thomas {
1235 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1236 2a31b33b 2022-07-23 thomas const struct got_error *err;
1237 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1238 2a31b33b 2022-07-23 thomas
1239 2a31b33b 2022-07-23 thomas *requested = NULL;
1240 2a31b33b 2022-07-23 thomas
1241 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1242 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1243 bfddd0d9 2018-09-29 stsp
1244 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1245 2a31b33b 2022-07-23 thomas if (err)
1246 2a31b33b 2022-07-23 thomas return err;
1247 2a31b33b 2022-07-23 thomas
1248 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1249 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1250 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1251 2a31b33b 2022-07-23 thomas if (err)
1252 2a31b33b 2022-07-23 thomas return err;
1253 2a31b33b 2022-07-23 thomas }
1254 2a31b33b 2022-07-23 thomas
1255 2a31b33b 2022-07-23 thomas view->focussed = 0;
1256 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1257 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1258 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1259 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1260 2a31b33b 2022-07-23 thomas
1261 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1262 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1263 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1264 2a31b33b 2022-07-23 thomas if (err)
1265 2a31b33b 2022-07-23 thomas return err;
1266 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1267 2a31b33b 2022-07-23 thomas if (err)
1268 2a31b33b 2022-07-23 thomas return err;
1269 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1270 2a31b33b 2022-07-23 thomas } else
1271 2a31b33b 2022-07-23 thomas *requested = new_view;
1272 2a31b33b 2022-07-23 thomas
1273 2a31b33b 2022-07-23 thomas return NULL;
1274 2a31b33b 2022-07-23 thomas }
1275 2a31b33b 2022-07-23 thomas
1276 34bc9ec9 2019-02-22 stsp static void
1277 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1278 25791caa 2018-10-24 stsp {
1279 25791caa 2018-10-24 stsp int cols, lines;
1280 25791caa 2018-10-24 stsp struct winsize size;
1281 25791caa 2018-10-24 stsp
1282 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1283 25791caa 2018-10-24 stsp cols = 80; /* Default */
1284 25791caa 2018-10-24 stsp lines = 24;
1285 25791caa 2018-10-24 stsp } else {
1286 25791caa 2018-10-24 stsp cols = size.ws_col;
1287 25791caa 2018-10-24 stsp lines = size.ws_row;
1288 25791caa 2018-10-24 stsp }
1289 25791caa 2018-10-24 stsp resize_term(lines, cols);
1290 2b49a8ae 2019-06-22 stsp }
1291 2b49a8ae 2019-06-22 stsp
1292 2b49a8ae 2019-06-22 stsp static const struct got_error *
1293 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1294 2b49a8ae 2019-06-22 stsp {
1295 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1296 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1297 2b49a8ae 2019-06-22 stsp char pattern[1024];
1298 2b49a8ae 2019-06-22 stsp int ret;
1299 c0c4acc8 2021-01-24 stsp
1300 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1301 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1302 c0c4acc8 2021-01-24 stsp view->searching = 0;
1303 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1304 c0c4acc8 2021-01-24 stsp }
1305 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1306 2b49a8ae 2019-06-22 stsp
1307 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1308 2b49a8ae 2019-06-22 stsp return NULL;
1309 2b49a8ae 2019-06-22 stsp
1310 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1311 a5d43cac 2022-07-01 thomas v = view->child;
1312 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1313 d07291c6 2022-12-30 thomas v = view->parent;
1314 2b49a8ae 2019-06-22 stsp
1315 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1316 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1317 a5d43cac 2022-07-01 thomas
1318 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1319 2b49a8ae 2019-06-22 stsp nocbreak();
1320 2b49a8ae 2019-06-22 stsp echo();
1321 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1322 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1323 2b49a8ae 2019-06-22 stsp cbreak();
1324 2b49a8ae 2019-06-22 stsp noecho();
1325 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1326 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1327 2b49a8ae 2019-06-22 stsp return NULL;
1328 2b49a8ae 2019-06-22 stsp
1329 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1330 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1331 7c32bd05 2019-06-22 stsp if (err) {
1332 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1333 7c32bd05 2019-06-22 stsp return err;
1334 7c32bd05 2019-06-22 stsp }
1335 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1336 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1337 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1338 2b49a8ae 2019-06-22 stsp view->search_next(view);
1339 2b49a8ae 2019-06-22 stsp }
1340 2b49a8ae 2019-06-22 stsp
1341 2b49a8ae 2019-06-22 stsp return NULL;
1342 64486692 2022-07-07 thomas }
1343 64486692 2022-07-07 thomas
1344 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1345 64486692 2022-07-07 thomas static const struct got_error *
1346 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1347 64486692 2022-07-07 thomas {
1348 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1349 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1350 64486692 2022-07-07 thomas
1351 64486692 2022-07-07 thomas if (view->parent)
1352 64486692 2022-07-07 thomas v = view->parent;
1353 64486692 2022-07-07 thomas else
1354 64486692 2022-07-07 thomas v = view;
1355 64486692 2022-07-07 thomas
1356 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1357 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1358 ddbc4d37 2022-07-12 thomas else
1359 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1360 64486692 2022-07-07 thomas
1361 ddbc4d37 2022-07-12 thomas if (!v->child)
1362 ddbc4d37 2022-07-12 thomas return NULL;
1363 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1364 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1365 ddbc4d37 2022-07-12 thomas
1366 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1367 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1368 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1369 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1370 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1371 64486692 2022-07-07 thomas
1372 ddbc4d37 2022-07-12 thomas
1373 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1374 64486692 2022-07-07 thomas v->ncols = COLS;
1375 64486692 2022-07-07 thomas v->child->ncols = COLS;
1376 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1377 64486692 2022-07-07 thomas
1378 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1379 64486692 2022-07-07 thomas if (err)
1380 64486692 2022-07-07 thomas return err;
1381 64486692 2022-07-07 thomas }
1382 64486692 2022-07-07 thomas v->child->mode = v->mode;
1383 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1384 64486692 2022-07-07 thomas v->focus_child = 1;
1385 64486692 2022-07-07 thomas
1386 64486692 2022-07-07 thomas err = view_fullscreen(v);
1387 64486692 2022-07-07 thomas if (err)
1388 64486692 2022-07-07 thomas return err;
1389 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1390 64486692 2022-07-07 thomas if (err)
1391 64486692 2022-07-07 thomas return err;
1392 64486692 2022-07-07 thomas
1393 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1394 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1395 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1396 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1397 cf1fe301 2022-07-29 thomas if (err)
1398 cf1fe301 2022-07-29 thomas return err;
1399 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1400 cf1fe301 2022-07-29 thomas if (err)
1401 cf1fe301 2022-07-29 thomas return err;
1402 ddbc4d37 2022-07-12 thomas } else {
1403 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1404 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1405 64486692 2022-07-07 thomas }
1406 f4e6231a 2022-07-22 thomas if (v->resize)
1407 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1408 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1409 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1410 64486692 2022-07-07 thomas
1411 64486692 2022-07-07 thomas return err;
1412 0cf4efb1 2018-09-29 stsp }
1413 6d0fee91 2018-08-01 stsp
1414 07b0611c 2022-06-23 thomas /*
1415 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1416 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1417 07b0611c 2022-06-23 thomas */
1418 07b0611c 2022-06-23 thomas static int
1419 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1420 07b0611c 2022-06-23 thomas {
1421 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1422 a5d43cac 2022-07-01 thomas int x, n = 0;
1423 07b0611c 2022-06-23 thomas
1424 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1425 a5d43cac 2022-07-01 thomas v = view->child;
1426 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1427 a5d43cac 2022-07-01 thomas v = view->parent;
1428 a5d43cac 2022-07-01 thomas
1429 07b0611c 2022-06-23 thomas view->count = 0;
1430 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1431 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1432 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1433 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1434 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1435 07b0611c 2022-06-23 thomas
1436 07b0611c 2022-06-23 thomas do {
1437 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1438 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1439 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1440 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1441 a5d43cac 2022-07-01 thomas }
1442 a5d43cac 2022-07-01 thomas
1443 07b0611c 2022-06-23 thomas /*
1444 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1445 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1446 07b0611c 2022-06-23 thomas */
1447 07b0611c 2022-06-23 thomas if (n >= 9999999)
1448 07b0611c 2022-06-23 thomas n = 9999999;
1449 07b0611c 2022-06-23 thomas else
1450 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1451 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1452 07b0611c 2022-06-23 thomas
1453 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1454 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1455 07dd3ed3 2022-08-06 thomas n = 0;
1456 07dd3ed3 2022-08-06 thomas c = 0;
1457 07dd3ed3 2022-08-06 thomas }
1458 07dd3ed3 2022-08-06 thomas
1459 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1460 07b0611c 2022-06-23 thomas view->count = n;
1461 07b0611c 2022-06-23 thomas
1462 07b0611c 2022-06-23 thomas return c;
1463 07b0611c 2022-06-23 thomas }
1464 07b0611c 2022-06-23 thomas
1465 0cf4efb1 2018-09-29 stsp static const struct got_error *
1466 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1467 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1468 e5a0f69f 2018-08-18 stsp {
1469 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1470 669b5ffa 2018-10-07 stsp struct tog_view *v;
1471 1a76625f 2018-10-22 stsp int ch, errcode;
1472 e5a0f69f 2018-08-18 stsp
1473 e5a0f69f 2018-08-18 stsp *new = NULL;
1474 8f4ed634 2020-03-26 stsp
1475 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1476 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1477 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1478 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1479 07b0611c 2022-06-23 thomas view->count = 0;
1480 07b0611c 2022-06-23 thomas }
1481 e5a0f69f 2018-08-18 stsp
1482 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1483 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1484 82954512 2020-02-03 stsp if (errcode)
1485 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1486 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1487 3da8ef85 2021-09-21 stsp sched_yield();
1488 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1489 82954512 2020-02-03 stsp if (errcode)
1490 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1491 82954512 2020-02-03 stsp "pthread_mutex_lock");
1492 60493ae3 2019-06-20 stsp view->search_next(view);
1493 60493ae3 2019-06-20 stsp return NULL;
1494 60493ae3 2019-06-20 stsp }
1495 60493ae3 2019-06-20 stsp
1496 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1497 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1498 1a76625f 2018-10-22 stsp if (errcode)
1499 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1500 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1501 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1502 634cb454 2022-07-03 thomas cbreak();
1503 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1504 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1505 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1506 634cb454 2022-07-03 thomas view->count = 0;
1507 634cb454 2022-07-03 thomas else
1508 634cb454 2022-07-03 thomas ch = view->ch;
1509 634cb454 2022-07-03 thomas } else {
1510 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1511 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1512 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1513 07b0611c 2022-06-23 thomas }
1514 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1515 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1516 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1517 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1518 1a76625f 2018-10-22 stsp if (errcode)
1519 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1520 25791caa 2018-10-24 stsp
1521 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1522 25791caa 2018-10-24 stsp tog_resizeterm();
1523 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1524 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1525 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1526 25791caa 2018-10-24 stsp err = view_resize(v);
1527 25791caa 2018-10-24 stsp if (err)
1528 25791caa 2018-10-24 stsp return err;
1529 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1530 25791caa 2018-10-24 stsp if (err)
1531 25791caa 2018-10-24 stsp return err;
1532 cdfcfb03 2020-12-06 stsp if (v->child) {
1533 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1534 cdfcfb03 2020-12-06 stsp if (err)
1535 cdfcfb03 2020-12-06 stsp return err;
1536 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1537 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1538 cdfcfb03 2020-12-06 stsp if (err)
1539 cdfcfb03 2020-12-06 stsp return err;
1540 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1541 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1542 53d2bdd3 2022-07-10 thomas if (err)
1543 53d2bdd3 2022-07-10 thomas return err;
1544 53d2bdd3 2022-07-10 thomas }
1545 cdfcfb03 2020-12-06 stsp }
1546 25791caa 2018-10-24 stsp }
1547 25791caa 2018-10-24 stsp }
1548 25791caa 2018-10-24 stsp
1549 e5a0f69f 2018-08-18 stsp switch (ch) {
1550 fc2737d5 2022-09-15 thomas case '?':
1551 fc2737d5 2022-09-15 thomas case 'H':
1552 fc2737d5 2022-09-15 thomas case KEY_F(1):
1553 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1554 fc2737d5 2022-09-15 thomas err = view->reset(view);
1555 fc2737d5 2022-09-15 thomas else
1556 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1557 fc2737d5 2022-09-15 thomas break;
1558 1e37a5c2 2019-05-12 jcs case '\t':
1559 07b0611c 2022-06-23 thomas view->count = 0;
1560 1e37a5c2 2019-05-12 jcs if (view->child) {
1561 e78dc838 2020-12-04 stsp view->focussed = 0;
1562 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1563 e78dc838 2020-12-04 stsp view->focus_child = 1;
1564 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1565 e78dc838 2020-12-04 stsp view->focussed = 0;
1566 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1567 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1568 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1569 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1570 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1571 3a0139e8 2022-07-22 thomas 0);
1572 a5d43cac 2022-07-01 thomas if (err)
1573 a5d43cac 2022-07-01 thomas return err;
1574 a5d43cac 2022-07-01 thomas }
1575 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1576 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1577 a5d43cac 2022-07-01 thomas if (err)
1578 a5d43cac 2022-07-01 thomas return err;
1579 a5d43cac 2022-07-01 thomas }
1580 1e37a5c2 2019-05-12 jcs }
1581 1e37a5c2 2019-05-12 jcs break;
1582 1e37a5c2 2019-05-12 jcs case 'q':
1583 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1584 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1585 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1586 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1587 a5d43cac 2022-07-01 thomas if (err)
1588 a5d43cac 2022-07-01 thomas break;
1589 a5d43cac 2022-07-01 thomas }
1590 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1591 a5d43cac 2022-07-01 thomas }
1592 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1593 9970f7fc 2020-12-03 stsp view->dying = 1;
1594 1e37a5c2 2019-05-12 jcs break;
1595 1e37a5c2 2019-05-12 jcs case 'Q':
1596 1e37a5c2 2019-05-12 jcs *done = 1;
1597 1e37a5c2 2019-05-12 jcs break;
1598 1c5e5faa 2022-06-23 thomas case 'F':
1599 07b0611c 2022-06-23 thomas view->count = 0;
1600 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1601 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1602 1e37a5c2 2019-05-12 jcs break;
1603 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1604 e78dc838 2020-12-04 stsp view->focussed = 0;
1605 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1606 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1607 53d2bdd3 2022-07-10 thomas } else {
1608 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1609 53d2bdd3 2022-07-10 thomas if (!err)
1610 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1611 53d2bdd3 2022-07-10 thomas }
1612 1e37a5c2 2019-05-12 jcs if (err)
1613 1e37a5c2 2019-05-12 jcs break;
1614 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1615 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1616 1e37a5c2 2019-05-12 jcs } else {
1617 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1618 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1619 e78dc838 2020-12-04 stsp view->focussed = 1;
1620 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1621 1e37a5c2 2019-05-12 jcs } else {
1622 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1623 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1624 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1625 53d2bdd3 2022-07-10 thomas if (!err)
1626 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1627 669b5ffa 2018-10-07 stsp }
1628 1e37a5c2 2019-05-12 jcs if (err)
1629 1e37a5c2 2019-05-12 jcs break;
1630 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1631 a5d43cac 2022-07-01 thomas }
1632 a5d43cac 2022-07-01 thomas if (err)
1633 a5d43cac 2022-07-01 thomas break;
1634 3a0139e8 2022-07-22 thomas if (view->resize) {
1635 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1636 a5d43cac 2022-07-01 thomas if (err)
1637 a5d43cac 2022-07-01 thomas break;
1638 1e37a5c2 2019-05-12 jcs }
1639 a5d43cac 2022-07-01 thomas if (view->parent)
1640 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1641 a5d43cac 2022-07-01 thomas if (!err)
1642 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1643 1e37a5c2 2019-05-12 jcs break;
1644 64486692 2022-07-07 thomas case 'S':
1645 53d2bdd3 2022-07-10 thomas view->count = 0;
1646 64486692 2022-07-07 thomas err = switch_split(view);
1647 53d2bdd3 2022-07-10 thomas break;
1648 53d2bdd3 2022-07-10 thomas case '-':
1649 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1650 64486692 2022-07-07 thomas break;
1651 53d2bdd3 2022-07-10 thomas case '+':
1652 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1653 53d2bdd3 2022-07-10 thomas break;
1654 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1655 60493ae3 2019-06-20 stsp break;
1656 60493ae3 2019-06-20 stsp case '/':
1657 07b0611c 2022-06-23 thomas view->count = 0;
1658 60493ae3 2019-06-20 stsp if (view->search_start)
1659 2b49a8ae 2019-06-22 stsp view_search_start(view);
1660 60493ae3 2019-06-20 stsp else
1661 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1662 1e37a5c2 2019-05-12 jcs break;
1663 b1bf1435 2019-06-21 stsp case 'N':
1664 60493ae3 2019-06-20 stsp case 'n':
1665 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1666 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1667 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1668 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1669 60493ae3 2019-06-20 stsp view->search_next(view);
1670 60493ae3 2019-06-20 stsp } else
1671 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1672 adf4c9e0 2022-07-03 thomas break;
1673 adf4c9e0 2022-07-03 thomas case 'A':
1674 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1675 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1676 adf4c9e0 2022-07-03 thomas else
1677 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1678 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1679 adf4c9e0 2022-07-03 thomas if (v->reset) {
1680 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1681 adf4c9e0 2022-07-03 thomas if (err)
1682 adf4c9e0 2022-07-03 thomas return err;
1683 adf4c9e0 2022-07-03 thomas }
1684 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1685 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1686 adf4c9e0 2022-07-03 thomas if (err)
1687 adf4c9e0 2022-07-03 thomas return err;
1688 adf4c9e0 2022-07-03 thomas }
1689 adf4c9e0 2022-07-03 thomas }
1690 60493ae3 2019-06-20 stsp break;
1691 1e37a5c2 2019-05-12 jcs default:
1692 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1693 1e37a5c2 2019-05-12 jcs break;
1694 e5a0f69f 2018-08-18 stsp }
1695 e5a0f69f 2018-08-18 stsp
1696 e5a0f69f 2018-08-18 stsp return err;
1697 bcbd79e2 2018-08-19 stsp }
1698 bcbd79e2 2018-08-19 stsp
1699 ef20f542 2022-06-26 thomas static int
1700 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1701 a3404814 2018-09-02 stsp {
1702 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1703 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1704 669b5ffa 2018-10-07 stsp return 0;
1705 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1706 669b5ffa 2018-10-07 stsp return 0;
1707 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1708 a3404814 2018-09-02 stsp return 0;
1709 a3404814 2018-09-02 stsp
1710 669b5ffa 2018-10-07 stsp return view->focussed;
1711 a3404814 2018-09-02 stsp }
1712 a3404814 2018-09-02 stsp
1713 bcbd79e2 2018-08-19 stsp static const struct got_error *
1714 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1715 e5a0f69f 2018-08-18 stsp {
1716 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1717 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1718 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1719 64486692 2022-07-07 thomas char *mode;
1720 fd823528 2018-10-22 stsp int fast_refresh = 10;
1721 1a76625f 2018-10-22 stsp int done = 0, errcode;
1722 e5a0f69f 2018-08-18 stsp
1723 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1724 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1725 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1726 64486692 2022-07-07 thomas else
1727 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1728 64486692 2022-07-07 thomas
1729 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1730 1a76625f 2018-10-22 stsp if (errcode)
1731 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1732 1a76625f 2018-10-22 stsp
1733 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1734 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1735 e5a0f69f 2018-08-18 stsp
1736 1004088d 2018-09-29 stsp view->focussed = 1;
1737 878940b7 2018-09-29 stsp err = view->show(view);
1738 0cf4efb1 2018-09-29 stsp if (err)
1739 0cf4efb1 2018-09-29 stsp return err;
1740 0cf4efb1 2018-09-29 stsp update_panels();
1741 0cf4efb1 2018-09-29 stsp doupdate();
1742 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1743 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1744 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1745 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1746 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1747 fd823528 2018-10-22 stsp
1748 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1749 e5a0f69f 2018-08-18 stsp if (err)
1750 e5a0f69f 2018-08-18 stsp break;
1751 9970f7fc 2020-12-03 stsp if (view->dying) {
1752 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1753 669b5ffa 2018-10-07 stsp
1754 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1755 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1756 9970f7fc 2020-12-03 stsp entry);
1757 e78dc838 2020-12-04 stsp else if (view->parent)
1758 669b5ffa 2018-10-07 stsp prev = view->parent;
1759 669b5ffa 2018-10-07 stsp
1760 e78dc838 2020-12-04 stsp if (view->parent) {
1761 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1762 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1763 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1764 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1765 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1766 40236d76 2022-06-23 thomas if (err)
1767 40236d76 2022-06-23 thomas break;
1768 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1769 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1770 e78dc838 2020-12-04 stsp } else
1771 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1772 669b5ffa 2018-10-07 stsp
1773 9970f7fc 2020-12-03 stsp err = view_close(view);
1774 fb59748f 2020-12-05 stsp if (err)
1775 e5a0f69f 2018-08-18 stsp goto done;
1776 669b5ffa 2018-10-07 stsp
1777 e78dc838 2020-12-04 stsp view = NULL;
1778 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1779 e78dc838 2020-12-04 stsp if (v->focussed)
1780 e78dc838 2020-12-04 stsp break;
1781 0cf4efb1 2018-09-29 stsp }
1782 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1783 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1784 e78dc838 2020-12-04 stsp if (prev)
1785 e78dc838 2020-12-04 stsp view = prev;
1786 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1787 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1788 e78dc838 2020-12-04 stsp tog_view_list_head);
1789 e78dc838 2020-12-04 stsp }
1790 e78dc838 2020-12-04 stsp if (view) {
1791 e78dc838 2020-12-04 stsp if (view->focus_child) {
1792 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1793 e78dc838 2020-12-04 stsp view = view->child;
1794 e78dc838 2020-12-04 stsp } else
1795 e78dc838 2020-12-04 stsp view->focussed = 1;
1796 e78dc838 2020-12-04 stsp }
1797 e78dc838 2020-12-04 stsp }
1798 e5a0f69f 2018-08-18 stsp }
1799 bcbd79e2 2018-08-19 stsp if (new_view) {
1800 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1801 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1802 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1803 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1804 86c66b02 2018-10-18 stsp continue;
1805 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1806 86c66b02 2018-10-18 stsp err = view_close(v);
1807 86c66b02 2018-10-18 stsp if (err)
1808 86c66b02 2018-10-18 stsp goto done;
1809 86c66b02 2018-10-18 stsp break;
1810 86c66b02 2018-10-18 stsp }
1811 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1812 fed7eaa8 2018-10-24 stsp view = new_view;
1813 7cd52833 2022-06-23 thomas }
1814 669b5ffa 2018-10-07 stsp if (view) {
1815 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1816 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1817 e78dc838 2020-12-04 stsp view = view->child;
1818 e78dc838 2020-12-04 stsp } else {
1819 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1820 e78dc838 2020-12-04 stsp view = view->parent;
1821 1a76625f 2018-10-22 stsp }
1822 e78dc838 2020-12-04 stsp show_panel(view->panel);
1823 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1824 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1825 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1826 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1827 669b5ffa 2018-10-07 stsp if (err)
1828 1a76625f 2018-10-22 stsp goto done;
1829 669b5ffa 2018-10-07 stsp }
1830 669b5ffa 2018-10-07 stsp err = view->show(view);
1831 0cf4efb1 2018-09-29 stsp if (err)
1832 1a76625f 2018-10-22 stsp goto done;
1833 669b5ffa 2018-10-07 stsp if (view->child) {
1834 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1835 669b5ffa 2018-10-07 stsp if (err)
1836 1a76625f 2018-10-22 stsp goto done;
1837 669b5ffa 2018-10-07 stsp }
1838 1a76625f 2018-10-22 stsp update_panels();
1839 1a76625f 2018-10-22 stsp doupdate();
1840 0cf4efb1 2018-09-29 stsp }
1841 e5a0f69f 2018-08-18 stsp }
1842 e5a0f69f 2018-08-18 stsp done:
1843 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1844 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1845 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1846 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1847 f2d749db 2022-07-12 thomas close_err = view_close(view);
1848 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1849 f2d749db 2022-07-12 thomas err = close_err;
1850 e5a0f69f 2018-08-18 stsp }
1851 1a76625f 2018-10-22 stsp
1852 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1853 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1854 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1855 1a76625f 2018-10-22 stsp
1856 e5a0f69f 2018-08-18 stsp return err;
1857 ea5e7bb5 2018-08-01 stsp }
1858 ea5e7bb5 2018-08-01 stsp
1859 4ed7e80c 2018-05-20 stsp __dead static void
1860 9f7d7167 2018-04-29 stsp usage_log(void)
1861 9f7d7167 2018-04-29 stsp {
1862 80ddbec8 2018-04-29 stsp endwin();
1863 c70c5802 2018-08-01 stsp fprintf(stderr,
1864 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1865 9f7d7167 2018-04-29 stsp getprogname());
1866 9f7d7167 2018-04-29 stsp exit(1);
1867 80ddbec8 2018-04-29 stsp }
1868 80ddbec8 2018-04-29 stsp
1869 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1870 80ddbec8 2018-04-29 stsp static const struct got_error *
1871 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1872 963b370f 2018-05-20 stsp {
1873 00dfcb92 2018-06-11 stsp char *vis = NULL;
1874 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1875 963b370f 2018-05-20 stsp
1876 963b370f 2018-05-20 stsp *ws = NULL;
1877 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1878 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1879 00dfcb92 2018-06-11 stsp int vislen;
1880 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1881 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1882 00dfcb92 2018-06-11 stsp
1883 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1884 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1885 00dfcb92 2018-06-11 stsp if (err)
1886 00dfcb92 2018-06-11 stsp return err;
1887 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1888 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1889 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1890 a7f50699 2018-06-11 stsp goto done;
1891 a7f50699 2018-06-11 stsp }
1892 00dfcb92 2018-06-11 stsp }
1893 963b370f 2018-05-20 stsp
1894 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1895 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1896 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1897 a7f50699 2018-06-11 stsp goto done;
1898 a7f50699 2018-06-11 stsp }
1899 963b370f 2018-05-20 stsp
1900 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1901 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1902 a7f50699 2018-06-11 stsp done:
1903 00dfcb92 2018-06-11 stsp free(vis);
1904 963b370f 2018-05-20 stsp if (err) {
1905 963b370f 2018-05-20 stsp free(*ws);
1906 963b370f 2018-05-20 stsp *ws = NULL;
1907 963b370f 2018-05-20 stsp *wlen = 0;
1908 963b370f 2018-05-20 stsp }
1909 963b370f 2018-05-20 stsp return err;
1910 05171be4 2022-06-23 thomas }
1911 05171be4 2022-06-23 thomas
1912 05171be4 2022-06-23 thomas static const struct got_error *
1913 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1914 05171be4 2022-06-23 thomas {
1915 05171be4 2022-06-23 thomas char *dst;
1916 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1917 05171be4 2022-06-23 thomas
1918 05171be4 2022-06-23 thomas *ptr = NULL;
1919 05171be4 2022-06-23 thomas n = len = strlen(src);
1920 83316834 2022-06-23 thomas dst = malloc(n + 1);
1921 05171be4 2022-06-23 thomas if (dst == NULL)
1922 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1923 05171be4 2022-06-23 thomas
1924 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1925 05171be4 2022-06-23 thomas const char c = src[idx];
1926 05171be4 2022-06-23 thomas
1927 05171be4 2022-06-23 thomas if (c == '\t') {
1928 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1929 b235ad5d 2022-06-23 thomas char *p;
1930 b235ad5d 2022-06-23 thomas
1931 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1932 83316834 2022-06-23 thomas if (p == NULL) {
1933 83316834 2022-06-23 thomas free(dst);
1934 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1935 83316834 2022-06-23 thomas
1936 83316834 2022-06-23 thomas }
1937 83316834 2022-06-23 thomas dst = p;
1938 05171be4 2022-06-23 thomas n += nb;
1939 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1940 05171be4 2022-06-23 thomas sz += nb;
1941 05171be4 2022-06-23 thomas } else
1942 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1943 05171be4 2022-06-23 thomas ++idx;
1944 05171be4 2022-06-23 thomas }
1945 05171be4 2022-06-23 thomas
1946 05171be4 2022-06-23 thomas dst[sz] = '\0';
1947 05171be4 2022-06-23 thomas *ptr = dst;
1948 05171be4 2022-06-23 thomas return NULL;
1949 963b370f 2018-05-20 stsp }
1950 963b370f 2018-05-20 stsp
1951 8d208d34 2022-06-23 thomas /*
1952 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1953 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1954 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1955 8d208d34 2022-06-23 thomas * *rcol.
1956 f91a2b48 2022-06-23 thomas */
1957 8d208d34 2022-06-23 thomas static int
1958 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1959 8d208d34 2022-06-23 thomas {
1960 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1961 f91a2b48 2022-06-23 thomas
1962 8d208d34 2022-06-23 thomas if (n == 0) {
1963 8d208d34 2022-06-23 thomas *rcol = cols;
1964 8d208d34 2022-06-23 thomas return off;
1965 8d208d34 2022-06-23 thomas }
1966 f91a2b48 2022-06-23 thomas
1967 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1968 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1969 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1970 8d208d34 2022-06-23 thomas else
1971 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1972 f91a2b48 2022-06-23 thomas
1973 8d208d34 2022-06-23 thomas if (width == -1) {
1974 8d208d34 2022-06-23 thomas width = 1;
1975 8d208d34 2022-06-23 thomas wline[i] = L'.';
1976 f91a2b48 2022-06-23 thomas }
1977 f91a2b48 2022-06-23 thomas
1978 8d208d34 2022-06-23 thomas if (cols + width > n)
1979 8d208d34 2022-06-23 thomas break;
1980 8d208d34 2022-06-23 thomas cols += width;
1981 f91a2b48 2022-06-23 thomas }
1982 f91a2b48 2022-06-23 thomas
1983 8d208d34 2022-06-23 thomas *rcol = cols;
1984 8d208d34 2022-06-23 thomas return i;
1985 f91a2b48 2022-06-23 thomas }
1986 f91a2b48 2022-06-23 thomas
1987 f91a2b48 2022-06-23 thomas /*
1988 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1989 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1990 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1991 f91a2b48 2022-06-23 thomas */
1992 f91a2b48 2022-06-23 thomas static const struct got_error *
1993 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1994 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1995 f91a2b48 2022-06-23 thomas {
1996 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1997 8d208d34 2022-06-23 thomas int cols;
1998 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1999 05171be4 2022-06-23 thomas char *exstr = NULL;
2000 963b370f 2018-05-20 stsp size_t wlen;
2001 8d208d34 2022-06-23 thomas int i, scrollx;
2002 963b370f 2018-05-20 stsp
2003 963b370f 2018-05-20 stsp *wlinep = NULL;
2004 b700b5d6 2018-07-10 stsp *widthp = 0;
2005 963b370f 2018-05-20 stsp
2006 05171be4 2022-06-23 thomas if (expand) {
2007 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2008 05171be4 2022-06-23 thomas if (err)
2009 05171be4 2022-06-23 thomas return err;
2010 05171be4 2022-06-23 thomas }
2011 05171be4 2022-06-23 thomas
2012 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2013 05171be4 2022-06-23 thomas free(exstr);
2014 963b370f 2018-05-20 stsp if (err)
2015 963b370f 2018-05-20 stsp return err;
2016 963b370f 2018-05-20 stsp
2017 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2018 f91a2b48 2022-06-23 thomas
2019 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2020 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2021 3f670bfb 2020-12-10 stsp wlen--;
2022 3f670bfb 2020-12-10 stsp }
2023 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2024 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2025 3f670bfb 2020-12-10 stsp wlen--;
2026 3f670bfb 2020-12-10 stsp }
2027 3f670bfb 2020-12-10 stsp
2028 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2029 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2030 27a741e5 2019-09-11 stsp
2031 b700b5d6 2018-07-10 stsp if (widthp)
2032 b700b5d6 2018-07-10 stsp *widthp = cols;
2033 f91a2b48 2022-06-23 thomas if (scrollxp)
2034 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2035 963b370f 2018-05-20 stsp if (err)
2036 963b370f 2018-05-20 stsp free(wline);
2037 963b370f 2018-05-20 stsp else
2038 963b370f 2018-05-20 stsp *wlinep = wline;
2039 963b370f 2018-05-20 stsp return err;
2040 963b370f 2018-05-20 stsp }
2041 963b370f 2018-05-20 stsp
2042 8b473291 2019-02-21 stsp static const struct got_error*
2043 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2044 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2045 8b473291 2019-02-21 stsp {
2046 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2047 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2048 8b473291 2019-02-21 stsp char *s;
2049 8b473291 2019-02-21 stsp const char *name;
2050 8b473291 2019-02-21 stsp
2051 8b473291 2019-02-21 stsp *refs_str = NULL;
2052 8b473291 2019-02-21 stsp
2053 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2054 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2055 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2056 52b5abe1 2019-08-13 stsp int cmp;
2057 52b5abe1 2019-08-13 stsp
2058 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2059 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2060 8b473291 2019-02-21 stsp continue;
2061 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2062 8b473291 2019-02-21 stsp name += 5;
2063 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2064 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2065 7143d404 2019-03-12 stsp continue;
2066 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2067 8b473291 2019-02-21 stsp name += 6;
2068 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2069 8b473291 2019-02-21 stsp name += 8;
2070 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2071 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2072 79cc719f 2020-04-24 stsp continue;
2073 79cc719f 2020-04-24 stsp }
2074 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2075 48cae60d 2020-09-22 stsp if (err)
2076 48cae60d 2020-09-22 stsp break;
2077 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2078 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2079 5d844a1e 2019-08-13 stsp if (err) {
2080 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2081 48cae60d 2020-09-22 stsp free(ref_id);
2082 5d844a1e 2019-08-13 stsp break;
2083 48cae60d 2020-09-22 stsp }
2084 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2085 5d844a1e 2019-08-13 stsp err = NULL;
2086 5d844a1e 2019-08-13 stsp tag = NULL;
2087 5d844a1e 2019-08-13 stsp }
2088 52b5abe1 2019-08-13 stsp }
2089 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2090 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2091 48cae60d 2020-09-22 stsp free(ref_id);
2092 52b5abe1 2019-08-13 stsp if (tag)
2093 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2094 52b5abe1 2019-08-13 stsp if (cmp != 0)
2095 52b5abe1 2019-08-13 stsp continue;
2096 8b473291 2019-02-21 stsp s = *refs_str;
2097 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2098 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2099 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2100 8b473291 2019-02-21 stsp free(s);
2101 8b473291 2019-02-21 stsp *refs_str = NULL;
2102 8b473291 2019-02-21 stsp break;
2103 8b473291 2019-02-21 stsp }
2104 8b473291 2019-02-21 stsp free(s);
2105 8b473291 2019-02-21 stsp }
2106 8b473291 2019-02-21 stsp
2107 8b473291 2019-02-21 stsp return err;
2108 8b473291 2019-02-21 stsp }
2109 8b473291 2019-02-21 stsp
2110 963b370f 2018-05-20 stsp static const struct got_error *
2111 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2112 27a741e5 2019-09-11 stsp int col_tab_align)
2113 5813d178 2019-03-09 stsp {
2114 e6b8b890 2020-12-29 naddy char *smallerthan;
2115 5813d178 2019-03-09 stsp
2116 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2117 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2118 5813d178 2019-03-09 stsp author = smallerthan + 1;
2119 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2120 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2121 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2122 5813d178 2019-03-09 stsp }
2123 5813d178 2019-03-09 stsp
2124 5813d178 2019-03-09 stsp static const struct got_error *
2125 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2126 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2127 8fdc79fe 2020-12-01 naddy int author_display_cols)
2128 80ddbec8 2018-04-29 stsp {
2129 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2130 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2131 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2132 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2133 5813d178 2019-03-09 stsp char *author = NULL;
2134 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2135 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2136 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2137 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2138 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2139 ccb26ccd 2018-11-05 stsp struct tm tm;
2140 45d799e2 2018-12-23 stsp time_t committer_time;
2141 11b20872 2019-11-08 stsp struct tog_color *tc;
2142 80ddbec8 2018-04-29 stsp
2143 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2144 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2145 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2146 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2147 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2148 b39d25c7 2018-07-10 stsp
2149 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2150 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2151 b39d25c7 2018-07-10 stsp else
2152 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2153 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2154 11b20872 2019-11-08 stsp if (tc)
2155 11b20872 2019-11-08 stsp wattr_on(view->window,
2156 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2157 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2158 11b20872 2019-11-08 stsp if (tc)
2159 11b20872 2019-11-08 stsp wattr_off(view->window,
2160 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2161 27a741e5 2019-09-11 stsp col = limit;
2162 b39d25c7 2018-07-10 stsp if (col > avail)
2163 b39d25c7 2018-07-10 stsp goto done;
2164 6570a66d 2019-11-08 stsp
2165 6570a66d 2019-11-08 stsp if (avail >= 120) {
2166 6570a66d 2019-11-08 stsp char *id_str;
2167 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2168 6570a66d 2019-11-08 stsp if (err)
2169 6570a66d 2019-11-08 stsp goto done;
2170 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2171 11b20872 2019-11-08 stsp if (tc)
2172 11b20872 2019-11-08 stsp wattr_on(view->window,
2173 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2174 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2175 11b20872 2019-11-08 stsp if (tc)
2176 11b20872 2019-11-08 stsp wattr_off(view->window,
2177 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2178 6570a66d 2019-11-08 stsp free(id_str);
2179 6570a66d 2019-11-08 stsp col += 9;
2180 6570a66d 2019-11-08 stsp if (col > avail)
2181 6570a66d 2019-11-08 stsp goto done;
2182 6570a66d 2019-11-08 stsp }
2183 b39d25c7 2018-07-10 stsp
2184 f69c5a46 2022-07-19 thomas if (s->use_committer)
2185 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2186 f69c5a46 2022-07-19 thomas else
2187 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2188 5813d178 2019-03-09 stsp if (author == NULL) {
2189 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2190 80ddbec8 2018-04-29 stsp goto done;
2191 80ddbec8 2018-04-29 stsp }
2192 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2193 bb737323 2018-05-20 stsp if (err)
2194 bb737323 2018-05-20 stsp goto done;
2195 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2196 11b20872 2019-11-08 stsp if (tc)
2197 11b20872 2019-11-08 stsp wattr_on(view->window,
2198 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2199 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2200 bb737323 2018-05-20 stsp col += author_width;
2201 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2202 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2203 bb737323 2018-05-20 stsp col++;
2204 bb737323 2018-05-20 stsp author_width++;
2205 bb737323 2018-05-20 stsp }
2206 f31d6c3b 2022-09-09 thomas if (tc)
2207 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2208 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2209 9c2eaf34 2018-05-20 stsp if (col > avail)
2210 9c2eaf34 2018-05-20 stsp goto done;
2211 80ddbec8 2018-04-29 stsp
2212 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2213 5943eee2 2019-08-13 stsp if (err)
2214 6d9fbc00 2018-04-29 stsp goto done;
2215 bb737323 2018-05-20 stsp logmsg = logmsg0;
2216 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2217 bb737323 2018-05-20 stsp logmsg++;
2218 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2219 bb737323 2018-05-20 stsp if (newline)
2220 bb737323 2018-05-20 stsp *newline = '\0';
2221 f91a2b48 2022-06-23 thomas limit = avail - col;
2222 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2223 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2224 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2225 f91a2b48 2022-06-23 thomas limit, col, 1);
2226 331b1a16 2022-06-23 thomas if (err)
2227 331b1a16 2022-06-23 thomas goto done;
2228 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2229 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2230 27a741e5 2019-09-11 stsp while (col < avail) {
2231 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2232 bb737323 2018-05-20 stsp col++;
2233 881b2d3e 2018-04-30 stsp }
2234 80ddbec8 2018-04-29 stsp done:
2235 80ddbec8 2018-04-29 stsp free(logmsg0);
2236 bb737323 2018-05-20 stsp free(wlogmsg);
2237 5813d178 2019-03-09 stsp free(author);
2238 bb737323 2018-05-20 stsp free(wauthor);
2239 80ddbec8 2018-04-29 stsp free(line);
2240 80ddbec8 2018-04-29 stsp return err;
2241 80ddbec8 2018-04-29 stsp }
2242 26ed57b2 2018-05-19 stsp
2243 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2244 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2245 899d86c2 2018-05-10 stsp struct got_object_id *id)
2246 80ddbec8 2018-04-29 stsp {
2247 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2248 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2249 80ddbec8 2018-04-29 stsp
2250 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2251 80ddbec8 2018-04-29 stsp if (entry == NULL)
2252 899d86c2 2018-05-10 stsp return NULL;
2253 99db9666 2018-05-07 stsp
2254 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2255 d68b9737 2022-09-05 thomas if (dup == NULL) {
2256 d68b9737 2022-09-05 thomas free(entry);
2257 d68b9737 2022-09-05 thomas return NULL;
2258 d68b9737 2022-09-05 thomas }
2259 d68b9737 2022-09-05 thomas
2260 d68b9737 2022-09-05 thomas entry->id = dup;
2261 99db9666 2018-05-07 stsp entry->commit = commit;
2262 899d86c2 2018-05-10 stsp return entry;
2263 99db9666 2018-05-07 stsp }
2264 80ddbec8 2018-04-29 stsp
2265 99db9666 2018-05-07 stsp static void
2266 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2267 99db9666 2018-05-07 stsp {
2268 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2269 99db9666 2018-05-07 stsp
2270 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2271 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2272 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2273 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2274 d68b9737 2022-09-05 thomas free(entry->id);
2275 99db9666 2018-05-07 stsp free(entry);
2276 99db9666 2018-05-07 stsp }
2277 99db9666 2018-05-07 stsp
2278 99db9666 2018-05-07 stsp static void
2279 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2280 99db9666 2018-05-07 stsp {
2281 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2282 99db9666 2018-05-07 stsp pop_commit(commits);
2283 c4972b91 2018-05-07 stsp }
2284 c4972b91 2018-05-07 stsp
2285 c4972b91 2018-05-07 stsp static const struct got_error *
2286 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2287 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2288 13add988 2019-10-15 stsp {
2289 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2290 13add988 2019-10-15 stsp regmatch_t regmatch;
2291 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2292 13add988 2019-10-15 stsp
2293 13add988 2019-10-15 stsp *have_match = 0;
2294 13add988 2019-10-15 stsp
2295 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2296 13add988 2019-10-15 stsp if (err)
2297 13add988 2019-10-15 stsp return err;
2298 13add988 2019-10-15 stsp
2299 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2300 13add988 2019-10-15 stsp if (err)
2301 13add988 2019-10-15 stsp goto done;
2302 13add988 2019-10-15 stsp
2303 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2304 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2305 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2306 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2307 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2308 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2309 13add988 2019-10-15 stsp *have_match = 1;
2310 13add988 2019-10-15 stsp done:
2311 13add988 2019-10-15 stsp free(id_str);
2312 13add988 2019-10-15 stsp free(logmsg);
2313 13add988 2019-10-15 stsp return err;
2314 13add988 2019-10-15 stsp }
2315 13add988 2019-10-15 stsp
2316 13add988 2019-10-15 stsp static const struct got_error *
2317 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2318 c4972b91 2018-05-07 stsp {
2319 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2320 9ba79e04 2018-06-11 stsp
2321 1a76625f 2018-10-22 stsp /*
2322 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2323 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2324 1a76625f 2018-10-22 stsp * while updating the display.
2325 1a76625f 2018-10-22 stsp */
2326 4e0d2870 2020-12-07 naddy do {
2327 7210b715 2022-09-11 thomas struct got_object_id id;
2328 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2329 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2330 7e8004ba 2022-09-11 thomas int limit_match = 0;
2331 1a76625f 2018-10-22 stsp int errcode;
2332 899d86c2 2018-05-10 stsp
2333 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2334 4e0d2870 2020-12-07 naddy NULL, NULL);
2335 7210b715 2022-09-11 thomas if (err)
2336 ecb28ae0 2018-07-16 stsp break;
2337 899d86c2 2018-05-10 stsp
2338 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2339 9ba79e04 2018-06-11 stsp if (err)
2340 9ba79e04 2018-06-11 stsp break;
2341 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2342 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2343 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2344 9ba79e04 2018-06-11 stsp break;
2345 9ba79e04 2018-06-11 stsp }
2346 93e45b7c 2018-09-24 stsp
2347 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2348 1a76625f 2018-10-22 stsp if (errcode) {
2349 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2350 13add988 2019-10-15 stsp "pthread_mutex_lock");
2351 1a76625f 2018-10-22 stsp break;
2352 1a76625f 2018-10-22 stsp }
2353 1a76625f 2018-10-22 stsp
2354 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2355 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2356 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2357 1a76625f 2018-10-22 stsp
2358 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2359 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2360 7e8004ba 2022-09-11 thomas a->limit_regex);
2361 7e8004ba 2022-09-11 thomas if (err)
2362 7e8004ba 2022-09-11 thomas break;
2363 7e8004ba 2022-09-11 thomas
2364 7e8004ba 2022-09-11 thomas if (limit_match) {
2365 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2366 7e8004ba 2022-09-11 thomas
2367 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2368 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2369 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2370 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2371 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2372 7e8004ba 2022-09-11 thomas break;
2373 7e8004ba 2022-09-11 thomas }
2374 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2375 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2376 7e8004ba 2022-09-11 thomas
2377 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2378 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2379 7e8004ba 2022-09-11 thomas matched, entry);
2380 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2381 7e8004ba 2022-09-11 thomas }
2382 7e8004ba 2022-09-11 thomas
2383 7e8004ba 2022-09-11 thomas /*
2384 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2385 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2386 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2387 7e8004ba 2022-09-11 thomas */
2388 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2389 7e8004ba 2022-09-11 thomas }
2390 7e8004ba 2022-09-11 thomas
2391 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2392 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2393 7c1452c1 2020-03-26 stsp int have_match;
2394 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2395 7c1452c1 2020-03-26 stsp if (err)
2396 7c1452c1 2020-03-26 stsp break;
2397 7e8004ba 2022-09-11 thomas
2398 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2399 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2400 7e8004ba 2022-09-11 thomas *a->search_next_done =
2401 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2402 7e8004ba 2022-09-11 thomas } else if (have_match)
2403 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2404 13add988 2019-10-15 stsp }
2405 13add988 2019-10-15 stsp
2406 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2407 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2408 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2409 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2410 7c1452c1 2020-03-26 stsp if (err)
2411 13add988 2019-10-15 stsp break;
2412 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2413 899d86c2 2018-05-10 stsp
2414 9ba79e04 2018-06-11 stsp return err;
2415 0553a4e3 2018-04-30 stsp }
2416 0553a4e3 2018-04-30 stsp
2417 2b779855 2020-12-05 naddy static void
2418 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2419 2b779855 2020-12-05 naddy {
2420 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2421 2b779855 2020-12-05 naddy int ncommits = 0;
2422 2b779855 2020-12-05 naddy
2423 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2424 2b779855 2020-12-05 naddy while (entry) {
2425 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2426 2b779855 2020-12-05 naddy s->selected_entry = entry;
2427 2b779855 2020-12-05 naddy break;
2428 2b779855 2020-12-05 naddy }
2429 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2430 2b779855 2020-12-05 naddy ncommits++;
2431 2b779855 2020-12-05 naddy }
2432 2b779855 2020-12-05 naddy }
2433 2b779855 2020-12-05 naddy
2434 0553a4e3 2018-04-30 stsp static const struct got_error *
2435 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2436 0553a4e3 2018-04-30 stsp {
2437 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2438 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2439 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2440 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2441 60493ae3 2019-06-20 stsp int width;
2442 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2443 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2444 8b473291 2019-02-21 stsp char *refs_str = NULL;
2445 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2446 11b20872 2019-11-08 stsp struct tog_color *tc;
2447 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2448 bd3f8225 2022-08-12 thomas
2449 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2450 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2451 0553a4e3 2018-04-30 stsp
2452 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2453 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2454 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2455 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2456 1a76625f 2018-10-22 stsp if (err)
2457 ecb28ae0 2018-07-16 stsp return err;
2458 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2459 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2460 d2075bf3 2020-12-25 stsp if (refs) {
2461 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2462 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2463 d2075bf3 2020-12-25 stsp if (err)
2464 d2075bf3 2020-12-25 stsp goto done;
2465 d2075bf3 2020-12-25 stsp }
2466 867c6645 2018-07-10 stsp }
2467 359bfafd 2019-02-22 stsp
2468 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2469 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2470 1a76625f 2018-10-22 stsp
2471 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2472 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2473 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2474 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2475 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2476 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2477 8f4ed634 2020-03-26 stsp goto done;
2478 8f4ed634 2020-03-26 stsp }
2479 8f4ed634 2020-03-26 stsp } else {
2480 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2481 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2482 f9686aa5 2020-03-27 stsp
2483 f9686aa5 2020-03-27 stsp if (view->searching) {
2484 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2485 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2486 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2487 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2488 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2489 f9686aa5 2020-03-27 stsp search_str = "searching...";
2490 f9686aa5 2020-03-27 stsp }
2491 f9686aa5 2020-03-27 stsp
2492 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2493 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2494 7e8004ba 2022-09-11 thomas
2495 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2496 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2497 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2498 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2499 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2500 8f4ed634 2020-03-26 stsp goto done;
2501 8f4ed634 2020-03-26 stsp }
2502 8b473291 2019-02-21 stsp }
2503 1a76625f 2018-10-22 stsp
2504 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2505 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2506 91198554 2022-06-23 thomas "........................................",
2507 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2508 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2509 1a76625f 2018-10-22 stsp header = NULL;
2510 1a76625f 2018-10-22 stsp goto done;
2511 1a76625f 2018-10-22 stsp }
2512 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2513 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2514 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2515 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2516 1a76625f 2018-10-22 stsp header = NULL;
2517 1a76625f 2018-10-22 stsp goto done;
2518 ecb28ae0 2018-07-16 stsp }
2519 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2520 1a76625f 2018-10-22 stsp if (err)
2521 1a76625f 2018-10-22 stsp goto done;
2522 867c6645 2018-07-10 stsp
2523 2814baeb 2018-08-01 stsp werase(view->window);
2524 867c6645 2018-07-10 stsp
2525 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2526 a3404814 2018-09-02 stsp wstandout(view->window);
2527 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2528 11b20872 2019-11-08 stsp if (tc)
2529 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2530 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2531 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2532 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2533 1a76625f 2018-10-22 stsp width++;
2534 1a76625f 2018-10-22 stsp }
2535 86f4aab9 2022-09-09 thomas if (tc)
2536 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2537 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2538 a3404814 2018-09-02 stsp wstandend(view->window);
2539 ecb28ae0 2018-07-16 stsp free(wline);
2540 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2541 1a76625f 2018-10-22 stsp goto done;
2542 0553a4e3 2018-04-30 stsp
2543 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2544 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2545 5813d178 2019-03-09 stsp ncommits = 0;
2546 05171be4 2022-06-23 thomas view->maxx = 0;
2547 5813d178 2019-03-09 stsp while (entry) {
2548 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2549 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2550 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2551 5813d178 2019-03-09 stsp int width;
2552 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2553 5813d178 2019-03-09 stsp break;
2554 f69c5a46 2022-07-19 thomas if (s->use_committer)
2555 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2556 f69c5a46 2022-07-19 thomas else
2557 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2558 5813d178 2019-03-09 stsp if (author == NULL) {
2559 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2560 5813d178 2019-03-09 stsp goto done;
2561 5813d178 2019-03-09 stsp }
2562 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2563 27a741e5 2019-09-11 stsp date_display_cols);
2564 5813d178 2019-03-09 stsp if (author_cols < width)
2565 5813d178 2019-03-09 stsp author_cols = width;
2566 5813d178 2019-03-09 stsp free(wauthor);
2567 5813d178 2019-03-09 stsp free(author);
2568 ef944b8b 2022-07-21 thomas if (err)
2569 ef944b8b 2022-07-21 thomas goto done;
2570 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2571 05171be4 2022-06-23 thomas if (err)
2572 05171be4 2022-06-23 thomas goto done;
2573 05171be4 2022-06-23 thomas msg = msg0;
2574 05171be4 2022-06-23 thomas while (*msg == '\n')
2575 05171be4 2022-06-23 thomas ++msg;
2576 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2577 331b1a16 2022-06-23 thomas *eol = '\0';
2578 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2579 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2580 331b1a16 2022-06-23 thomas if (err)
2581 331b1a16 2022-06-23 thomas goto done;
2582 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2583 05171be4 2022-06-23 thomas free(msg0);
2584 331b1a16 2022-06-23 thomas free(wmsg);
2585 7ca04879 2019-10-19 stsp ncommits++;
2586 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2587 5813d178 2019-03-09 stsp }
2588 5813d178 2019-03-09 stsp
2589 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2590 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2591 867c6645 2018-07-10 stsp ncommits = 0;
2592 899d86c2 2018-05-10 stsp while (entry) {
2593 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2594 899d86c2 2018-05-10 stsp break;
2595 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2596 2814baeb 2018-08-01 stsp wstandout(view->window);
2597 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2598 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2599 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2600 2814baeb 2018-08-01 stsp wstandend(view->window);
2601 0553a4e3 2018-04-30 stsp if (err)
2602 60493ae3 2019-06-20 stsp goto done;
2603 0553a4e3 2018-04-30 stsp ncommits++;
2604 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2605 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2606 80ddbec8 2018-04-29 stsp }
2607 80ddbec8 2018-04-29 stsp
2608 a5d43cac 2022-07-01 thomas view_border(view);
2609 1a76625f 2018-10-22 stsp done:
2610 1a76625f 2018-10-22 stsp free(id_str);
2611 8b473291 2019-02-21 stsp free(refs_str);
2612 1a76625f 2018-10-22 stsp free(ncommits_str);
2613 1a76625f 2018-10-22 stsp free(header);
2614 80ddbec8 2018-04-29 stsp return err;
2615 9f7d7167 2018-04-29 stsp }
2616 07b55e75 2018-05-10 stsp
2617 07b55e75 2018-05-10 stsp static void
2618 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2619 07b55e75 2018-05-10 stsp {
2620 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2621 07b55e75 2018-05-10 stsp int nscrolled = 0;
2622 07b55e75 2018-05-10 stsp
2623 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2624 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2625 07b55e75 2018-05-10 stsp return;
2626 9f7d7167 2018-04-29 stsp
2627 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2628 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2629 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2630 07b55e75 2018-05-10 stsp if (entry) {
2631 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2632 07b55e75 2018-05-10 stsp nscrolled++;
2633 07b55e75 2018-05-10 stsp }
2634 07b55e75 2018-05-10 stsp }
2635 aa075928 2018-05-10 stsp }
2636 aa075928 2018-05-10 stsp
2637 aa075928 2018-05-10 stsp static const struct got_error *
2638 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2639 aa075928 2018-05-10 stsp {
2640 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2641 5e224a3e 2019-02-22 stsp int errcode;
2642 8a42fca8 2019-02-22 stsp
2643 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2644 aa075928 2018-05-10 stsp
2645 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2646 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2647 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2648 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2649 7aafa0d1 2019-02-22 stsp if (errcode)
2650 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2651 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2652 7c1452c1 2020-03-26 stsp
2653 7c1452c1 2020-03-26 stsp /*
2654 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2655 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2656 7c1452c1 2020-03-26 stsp */
2657 7c1452c1 2020-03-26 stsp if (!wait)
2658 7c1452c1 2020-03-26 stsp break;
2659 7c1452c1 2020-03-26 stsp
2660 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2661 ffe38506 2020-12-01 naddy show_log_view(view);
2662 7c1452c1 2020-03-26 stsp update_panels();
2663 7c1452c1 2020-03-26 stsp doupdate();
2664 7c1452c1 2020-03-26 stsp
2665 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2666 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2667 82954512 2020-02-03 stsp if (errcode)
2668 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2669 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2670 82954512 2020-02-03 stsp
2671 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2672 ffe38506 2020-12-01 naddy show_log_view(view);
2673 7c1452c1 2020-03-26 stsp update_panels();
2674 7c1452c1 2020-03-26 stsp doupdate();
2675 5e224a3e 2019-02-22 stsp }
2676 5e224a3e 2019-02-22 stsp
2677 5e224a3e 2019-02-22 stsp return NULL;
2678 a5d43cac 2022-07-01 thomas }
2679 a5d43cac 2022-07-01 thomas
2680 a5d43cac 2022-07-01 thomas static const struct got_error *
2681 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2682 a5d43cac 2022-07-01 thomas {
2683 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2684 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2685 fe731b51 2022-07-14 thomas
2686 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2687 fe731b51 2022-07-14 thomas return NULL;
2688 a5d43cac 2022-07-01 thomas
2689 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2690 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2691 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2692 a5d43cac 2022-07-01 thomas
2693 a5d43cac 2022-07-01 thomas return err;
2694 5e224a3e 2019-02-22 stsp }
2695 5e224a3e 2019-02-22 stsp
2696 5e224a3e 2019-02-22 stsp static const struct got_error *
2697 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2698 5e224a3e 2019-02-22 stsp {
2699 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2700 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2701 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2702 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2703 5e224a3e 2019-02-22 stsp
2704 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2705 5e224a3e 2019-02-22 stsp return NULL;
2706 5e224a3e 2019-02-22 stsp
2707 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2708 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2709 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2710 08ebd0a9 2019-02-22 stsp /*
2711 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2712 08ebd0a9 2019-02-22 stsp */
2713 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2714 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2715 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2716 5e224a3e 2019-02-22 stsp if (err)
2717 5e224a3e 2019-02-22 stsp return err;
2718 7aafa0d1 2019-02-22 stsp }
2719 b295e71b 2019-02-22 stsp
2720 7aafa0d1 2019-02-22 stsp do {
2721 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2722 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2723 88048b54 2019-02-21 stsp break;
2724 88048b54 2019-02-21 stsp
2725 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2726 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2727 aa075928 2018-05-10 stsp
2728 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2729 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2730 dd0a52c1 2018-05-20 stsp break;
2731 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2732 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2733 aa075928 2018-05-10 stsp
2734 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2735 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2736 a5d43cac 2022-07-01 thomas else
2737 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2738 a5d43cac 2022-07-01 thomas
2739 dd0a52c1 2018-05-20 stsp return err;
2740 07b55e75 2018-05-10 stsp }
2741 4a7f7875 2018-05-10 stsp
2742 cd0acaa7 2018-05-20 stsp static const struct got_error *
2743 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2744 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2745 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2746 cd0acaa7 2018-05-20 stsp {
2747 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2748 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2749 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2750 cd0acaa7 2018-05-20 stsp
2751 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2752 15a94983 2018-12-23 stsp if (diff_view == NULL)
2753 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2754 ea5e7bb5 2018-08-01 stsp
2755 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2756 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2757 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2758 e5a0f69f 2018-08-18 stsp if (err == NULL)
2759 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2760 cd0acaa7 2018-05-20 stsp return err;
2761 4a7f7875 2018-05-10 stsp }
2762 4a7f7875 2018-05-10 stsp
2763 80ddbec8 2018-04-29 stsp static const struct got_error *
2764 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2765 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2766 9343a5fb 2018-06-23 stsp {
2767 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2768 941e9f74 2019-05-21 stsp
2769 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2770 941e9f74 2019-05-21 stsp if (parent == NULL)
2771 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2772 941e9f74 2019-05-21 stsp
2773 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2774 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2775 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2776 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2777 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2778 941e9f74 2019-05-21 stsp s->tree = subtree;
2779 941e9f74 2019-05-21 stsp s->selected = 0;
2780 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2781 941e9f74 2019-05-21 stsp return NULL;
2782 941e9f74 2019-05-21 stsp }
2783 941e9f74 2019-05-21 stsp
2784 941e9f74 2019-05-21 stsp static const struct got_error *
2785 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2786 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2787 941e9f74 2019-05-21 stsp {
2788 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2789 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2790 941e9f74 2019-05-21 stsp const char *p;
2791 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2792 9343a5fb 2018-06-23 stsp
2793 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2794 941e9f74 2019-05-21 stsp p = path;
2795 941e9f74 2019-05-21 stsp while (*p) {
2796 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2797 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2798 56e0773d 2019-11-28 stsp char *te_name;
2799 33cbf02b 2020-01-12 stsp
2800 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2801 33cbf02b 2020-01-12 stsp p++;
2802 941e9f74 2019-05-21 stsp
2803 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2804 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2805 941e9f74 2019-05-21 stsp if (slash == NULL)
2806 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2807 33cbf02b 2020-01-12 stsp else
2808 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2809 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2810 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2811 56e0773d 2019-11-28 stsp break;
2812 941e9f74 2019-05-21 stsp }
2813 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2814 56e0773d 2019-11-28 stsp if (te == NULL) {
2815 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2816 56e0773d 2019-11-28 stsp free(te_name);
2817 941e9f74 2019-05-21 stsp break;
2818 941e9f74 2019-05-21 stsp }
2819 56e0773d 2019-11-28 stsp free(te_name);
2820 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2821 941e9f74 2019-05-21 stsp
2822 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2823 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2824 b03c880f 2019-05-21 stsp
2825 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2826 941e9f74 2019-05-21 stsp if (slash)
2827 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2828 941e9f74 2019-05-21 stsp else
2829 941e9f74 2019-05-21 stsp subpath = strdup(path);
2830 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2831 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2832 941e9f74 2019-05-21 stsp break;
2833 941e9f74 2019-05-21 stsp }
2834 941e9f74 2019-05-21 stsp
2835 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2836 941e9f74 2019-05-21 stsp subpath);
2837 941e9f74 2019-05-21 stsp if (err)
2838 941e9f74 2019-05-21 stsp break;
2839 941e9f74 2019-05-21 stsp
2840 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2841 941e9f74 2019-05-21 stsp free(tree_id);
2842 941e9f74 2019-05-21 stsp if (err)
2843 941e9f74 2019-05-21 stsp break;
2844 941e9f74 2019-05-21 stsp
2845 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2846 941e9f74 2019-05-21 stsp if (err) {
2847 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2848 941e9f74 2019-05-21 stsp break;
2849 941e9f74 2019-05-21 stsp }
2850 941e9f74 2019-05-21 stsp if (slash == NULL)
2851 941e9f74 2019-05-21 stsp break;
2852 941e9f74 2019-05-21 stsp free(subpath);
2853 941e9f74 2019-05-21 stsp subpath = NULL;
2854 941e9f74 2019-05-21 stsp p = slash;
2855 941e9f74 2019-05-21 stsp }
2856 941e9f74 2019-05-21 stsp
2857 941e9f74 2019-05-21 stsp free(subpath);
2858 1a76625f 2018-10-22 stsp return err;
2859 61266923 2020-01-14 stsp }
2860 61266923 2020-01-14 stsp
2861 61266923 2020-01-14 stsp static const struct got_error *
2862 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2863 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2864 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2865 55cccc34 2020-02-20 stsp {
2866 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2867 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2868 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2869 55cccc34 2020-02-20 stsp
2870 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2871 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2872 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2873 55cccc34 2020-02-20 stsp
2874 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2875 bc573f3b 2021-07-10 stsp if (err)
2876 55cccc34 2020-02-20 stsp return err;
2877 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2878 55cccc34 2020-02-20 stsp
2879 55cccc34 2020-02-20 stsp *new_view = tree_view;
2880 55cccc34 2020-02-20 stsp
2881 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2882 55cccc34 2020-02-20 stsp return NULL;
2883 55cccc34 2020-02-20 stsp
2884 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2885 55cccc34 2020-02-20 stsp }
2886 55cccc34 2020-02-20 stsp
2887 55cccc34 2020-02-20 stsp static const struct got_error *
2888 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2889 61266923 2020-01-14 stsp {
2890 61266923 2020-01-14 stsp sigset_t sigset;
2891 61266923 2020-01-14 stsp int errcode;
2892 61266923 2020-01-14 stsp
2893 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2894 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2895 61266923 2020-01-14 stsp
2896 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2897 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2898 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2899 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2900 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2901 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2902 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2903 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2904 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2905 61266923 2020-01-14 stsp
2906 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2907 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2908 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2909 61266923 2020-01-14 stsp
2910 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2911 61266923 2020-01-14 stsp if (errcode)
2912 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2913 61266923 2020-01-14 stsp
2914 61266923 2020-01-14 stsp return NULL;
2915 1a76625f 2018-10-22 stsp }
2916 1a76625f 2018-10-22 stsp
2917 1a76625f 2018-10-22 stsp static void *
2918 1a76625f 2018-10-22 stsp log_thread(void *arg)
2919 1a76625f 2018-10-22 stsp {
2920 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2921 1a76625f 2018-10-22 stsp int errcode = 0;
2922 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2923 1a76625f 2018-10-22 stsp int done = 0;
2924 61266923 2020-01-14 stsp
2925 f2d749db 2022-07-12 thomas /*
2926 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2927 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2928 f2d749db 2022-07-12 thomas */
2929 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2930 f2d749db 2022-07-12 thomas if (errcode) {
2931 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2932 61266923 2020-01-14 stsp return (void *)err;
2933 f2d749db 2022-07-12 thomas }
2934 1a76625f 2018-10-22 stsp
2935 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2936 f2d749db 2022-07-12 thomas if (err) {
2937 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2938 f2d749db 2022-07-12 thomas goto done;
2939 f2d749db 2022-07-12 thomas }
2940 f2d749db 2022-07-12 thomas
2941 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2942 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2943 f2d749db 2022-07-12 thomas if (errcode) {
2944 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2945 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2946 f2d749db 2022-07-12 thomas goto done;
2947 f2d749db 2022-07-12 thomas }
2948 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2949 1a76625f 2018-10-22 stsp if (err) {
2950 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2951 f2d749db 2022-07-12 thomas goto done;
2952 1a76625f 2018-10-22 stsp err = NULL;
2953 1a76625f 2018-10-22 stsp done = 1;
2954 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
2955 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2956 7e8004ba 2022-09-11 thomas if (a->limit_match)
2957 7e8004ba 2022-09-11 thomas a->commits_needed--;
2958 7e8004ba 2022-09-11 thomas } else
2959 7e8004ba 2022-09-11 thomas a->commits_needed--;
2960 7e8004ba 2022-09-11 thomas }
2961 1a76625f 2018-10-22 stsp
2962 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2963 3abe8080 2019-04-10 stsp if (errcode) {
2964 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2965 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2966 f2d749db 2022-07-12 thomas goto done;
2967 3abe8080 2019-04-10 stsp } else if (*a->quit)
2968 1a76625f 2018-10-22 stsp done = 1;
2969 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
2970 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2971 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
2972 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2973 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
2974 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
2975 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
2976 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
2977 1a76625f 2018-10-22 stsp }
2978 1a76625f 2018-10-22 stsp
2979 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2980 7c1452c1 2020-03-26 stsp if (errcode) {
2981 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2982 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2983 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2984 f2d749db 2022-07-12 thomas goto done;
2985 7c1452c1 2020-03-26 stsp }
2986 7c1452c1 2020-03-26 stsp
2987 1a76625f 2018-10-22 stsp if (done)
2988 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2989 7c1452c1 2020-03-26 stsp else {
2990 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2991 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2992 7c1452c1 2020-03-26 stsp &tog_mutex);
2993 f2d749db 2022-07-12 thomas if (errcode) {
2994 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2995 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2996 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2997 f2d749db 2022-07-12 thomas goto done;
2998 f2d749db 2022-07-12 thomas }
2999 21355643 2020-12-06 stsp if (*a->quit)
3000 21355643 2020-12-06 stsp done = 1;
3001 7c1452c1 2020-03-26 stsp }
3002 1a76625f 2018-10-22 stsp }
3003 1a76625f 2018-10-22 stsp }
3004 3abe8080 2019-04-10 stsp a->log_complete = 1;
3005 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3006 f2d749db 2022-07-12 thomas if (errcode)
3007 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3008 f2d749db 2022-07-12 thomas done:
3009 f2d749db 2022-07-12 thomas if (err) {
3010 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3011 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3012 f2d749db 2022-07-12 thomas }
3013 1a76625f 2018-10-22 stsp return (void *)err;
3014 1a76625f 2018-10-22 stsp }
3015 1a76625f 2018-10-22 stsp
3016 1a76625f 2018-10-22 stsp static const struct got_error *
3017 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3018 1a76625f 2018-10-22 stsp {
3019 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3020 1a76625f 2018-10-22 stsp int errcode;
3021 1a76625f 2018-10-22 stsp
3022 1a76625f 2018-10-22 stsp if (s->thread) {
3023 1a76625f 2018-10-22 stsp s->quit = 1;
3024 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3025 1a76625f 2018-10-22 stsp if (errcode)
3026 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3027 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3028 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3029 1a76625f 2018-10-22 stsp if (errcode)
3030 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3031 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3032 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3033 1a76625f 2018-10-22 stsp if (errcode)
3034 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3035 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3036 1a76625f 2018-10-22 stsp if (errcode)
3037 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3038 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3039 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3040 1a76625f 2018-10-22 stsp }
3041 1a76625f 2018-10-22 stsp
3042 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3043 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3044 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3045 1af5eddf 2022-06-23 thomas }
3046 1af5eddf 2022-06-23 thomas
3047 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3048 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3049 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3050 1af5eddf 2022-06-23 thomas if (err == NULL)
3051 1af5eddf 2022-06-23 thomas err = pack_err;
3052 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3053 1a76625f 2018-10-22 stsp }
3054 1a76625f 2018-10-22 stsp
3055 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3056 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3057 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3058 1a76625f 2018-10-22 stsp }
3059 1a76625f 2018-10-22 stsp
3060 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3061 9343a5fb 2018-06-23 stsp }
3062 9343a5fb 2018-06-23 stsp
3063 9343a5fb 2018-06-23 stsp static const struct got_error *
3064 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3065 1a76625f 2018-10-22 stsp {
3066 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3067 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3068 276b94a1 2020-11-13 naddy int errcode;
3069 1a76625f 2018-10-22 stsp
3070 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3071 276b94a1 2020-11-13 naddy
3072 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3073 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3074 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3075 276b94a1 2020-11-13 naddy
3076 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3077 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3078 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3079 276b94a1 2020-11-13 naddy
3080 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3081 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3082 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3083 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3084 1a76625f 2018-10-22 stsp free(s->start_id);
3085 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3086 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3087 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3088 1a76625f 2018-10-22 stsp return err;
3089 1a76625f 2018-10-22 stsp }
3090 1a76625f 2018-10-22 stsp
3091 7e8004ba 2022-09-11 thomas /*
3092 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3093 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3094 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3095 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3096 7e8004ba 2022-09-11 thomas * works with very slight change.
3097 7e8004ba 2022-09-11 thomas */
3098 1a76625f 2018-10-22 stsp static const struct got_error *
3099 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3100 7e8004ba 2022-09-11 thomas {
3101 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3102 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3103 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3104 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3105 7e8004ba 2022-09-11 thomas char pattern[1024];
3106 7e8004ba 2022-09-11 thomas int ret;
3107 7e8004ba 2022-09-11 thomas
3108 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3109 7e8004ba 2022-09-11 thomas v = view->child;
3110 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3111 7e8004ba 2022-09-11 thomas v = view->parent;
3112 7e8004ba 2022-09-11 thomas
3113 7e8004ba 2022-09-11 thomas /* Get the pattern */
3114 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3115 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3116 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3117 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3118 7e8004ba 2022-09-11 thomas nocbreak();
3119 7e8004ba 2022-09-11 thomas echo();
3120 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3121 7e8004ba 2022-09-11 thomas cbreak();
3122 7e8004ba 2022-09-11 thomas noecho();
3123 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3124 7e8004ba 2022-09-11 thomas if (ret == ERR)
3125 7e8004ba 2022-09-11 thomas return NULL;
3126 7e8004ba 2022-09-11 thomas
3127 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3128 7e8004ba 2022-09-11 thomas /*
3129 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3130 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3131 7e8004ba 2022-09-11 thomas */
3132 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3133 7e8004ba 2022-09-11 thomas return NULL;
3134 7e8004ba 2022-09-11 thomas
3135 7e8004ba 2022-09-11 thomas /*
3136 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3137 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3138 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3139 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3140 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3141 7e8004ba 2022-09-11 thomas * the queue.
3142 7e8004ba 2022-09-11 thomas */
3143 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3144 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3145 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3146 7e8004ba 2022-09-11 thomas s->selected = 0;
3147 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3148 7e8004ba 2022-09-11 thomas
3149 7e8004ba 2022-09-11 thomas return NULL;
3150 7e8004ba 2022-09-11 thomas }
3151 7e8004ba 2022-09-11 thomas
3152 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3153 7e8004ba 2022-09-11 thomas return NULL;
3154 7e8004ba 2022-09-11 thomas
3155 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3156 7e8004ba 2022-09-11 thomas
3157 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3158 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3159 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3160 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3161 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3162 7e8004ba 2022-09-11 thomas
3163 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3164 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3165 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3166 7e8004ba 2022-09-11 thomas
3167 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3168 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3169 7e8004ba 2022-09-11 thomas int have_match = 0;
3170 7e8004ba 2022-09-11 thomas
3171 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3172 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3173 7e8004ba 2022-09-11 thomas if (err)
3174 7e8004ba 2022-09-11 thomas return err;
3175 7e8004ba 2022-09-11 thomas
3176 7e8004ba 2022-09-11 thomas if (have_match) {
3177 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3178 7e8004ba 2022-09-11 thomas
3179 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3180 7e8004ba 2022-09-11 thomas entry->id);
3181 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3182 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3183 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3184 7e8004ba 2022-09-11 thomas break;
3185 7e8004ba 2022-09-11 thomas }
3186 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3187 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3188 7e8004ba 2022-09-11 thomas
3189 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3190 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3191 7e8004ba 2022-09-11 thomas matched, entry);
3192 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3193 7e8004ba 2022-09-11 thomas }
3194 7e8004ba 2022-09-11 thomas }
3195 7e8004ba 2022-09-11 thomas
3196 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3197 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3198 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3199 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3200 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3201 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3202 7e8004ba 2022-09-11 thomas if (err)
3203 7e8004ba 2022-09-11 thomas return err;
3204 7e8004ba 2022-09-11 thomas }
3205 7e8004ba 2022-09-11 thomas
3206 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3207 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3208 7e8004ba 2022-09-11 thomas s->selected = 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 static const struct got_error *
3214 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3215 60493ae3 2019-06-20 stsp {
3216 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3217 60493ae3 2019-06-20 stsp
3218 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3219 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3220 60493ae3 2019-06-20 stsp return NULL;
3221 60493ae3 2019-06-20 stsp }
3222 60493ae3 2019-06-20 stsp
3223 60493ae3 2019-06-20 stsp static const struct got_error *
3224 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3225 60493ae3 2019-06-20 stsp {
3226 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3227 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3228 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3229 60493ae3 2019-06-20 stsp
3230 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3231 f9686aa5 2020-03-27 stsp show_log_view(view);
3232 f9686aa5 2020-03-27 stsp update_panels();
3233 f9686aa5 2020-03-27 stsp doupdate();
3234 f9686aa5 2020-03-27 stsp
3235 96e2b566 2019-07-08 stsp if (s->search_entry) {
3236 13add988 2019-10-15 stsp int errcode, ch;
3237 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3238 13add988 2019-10-15 stsp if (errcode)
3239 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3240 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3241 13add988 2019-10-15 stsp ch = wgetch(view->window);
3242 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3243 13add988 2019-10-15 stsp if (errcode)
3244 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3245 13add988 2019-10-15 stsp "pthread_mutex_lock");
3246 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3247 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3248 678cbce5 2019-07-28 stsp return NULL;
3249 678cbce5 2019-07-28 stsp }
3250 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3251 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3252 96e2b566 2019-07-08 stsp else
3253 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3254 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3255 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3256 df5e841d 2022-06-23 thomas /*
3257 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3258 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3259 1f4d5162 2022-06-23 thomas * might have changed.
3260 df5e841d 2022-06-23 thomas */
3261 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3262 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3263 e7baaec8 2022-09-13 thomas else
3264 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3265 e7baaec8 2022-09-13 thomas entry);
3266 20be8d96 2019-06-21 stsp } else {
3267 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3268 20be8d96 2019-06-21 stsp }
3269 60493ae3 2019-06-20 stsp
3270 60493ae3 2019-06-20 stsp while (1) {
3271 13add988 2019-10-15 stsp int have_match = 0;
3272 13add988 2019-10-15 stsp
3273 60493ae3 2019-06-20 stsp if (entry == NULL) {
3274 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3275 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3276 f9967bca 2020-03-27 stsp view->search_next_done =
3277 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3278 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3279 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3280 f801134a 2019-06-25 stsp return NULL;
3281 60493ae3 2019-06-20 stsp }
3282 96e2b566 2019-07-08 stsp /*
3283 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3284 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3285 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3286 96e2b566 2019-07-08 stsp */
3287 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3288 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3289 60493ae3 2019-06-20 stsp }
3290 60493ae3 2019-06-20 stsp
3291 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3292 13add988 2019-10-15 stsp &view->regex);
3293 5943eee2 2019-08-13 stsp if (err)
3294 13add988 2019-10-15 stsp break;
3295 13add988 2019-10-15 stsp if (have_match) {
3296 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3297 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3298 60493ae3 2019-06-20 stsp break;
3299 60493ae3 2019-06-20 stsp }
3300 13add988 2019-10-15 stsp
3301 96e2b566 2019-07-08 stsp s->search_entry = entry;
3302 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3303 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3304 b1bf1435 2019-06-21 stsp else
3305 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3306 60493ae3 2019-06-20 stsp }
3307 60493ae3 2019-06-20 stsp
3308 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3309 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3310 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3311 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3312 60493ae3 2019-06-20 stsp if (err)
3313 60493ae3 2019-06-20 stsp return err;
3314 ead14cbe 2019-06-21 stsp cur++;
3315 ead14cbe 2019-06-21 stsp }
3316 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3317 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3318 60493ae3 2019-06-20 stsp if (err)
3319 60493ae3 2019-06-20 stsp return err;
3320 ead14cbe 2019-06-21 stsp cur--;
3321 60493ae3 2019-06-20 stsp }
3322 60493ae3 2019-06-20 stsp }
3323 60493ae3 2019-06-20 stsp
3324 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3325 96e2b566 2019-07-08 stsp
3326 60493ae3 2019-06-20 stsp return NULL;
3327 60493ae3 2019-06-20 stsp }
3328 60493ae3 2019-06-20 stsp
3329 60493ae3 2019-06-20 stsp static const struct got_error *
3330 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3331 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3332 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3333 80ddbec8 2018-04-29 stsp {
3334 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3335 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3336 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3337 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3338 1a76625f 2018-10-22 stsp int errcode;
3339 80ddbec8 2018-04-29 stsp
3340 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3341 f135c941 2020-02-20 stsp free(s->in_repo_path);
3342 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3343 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3344 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3345 f135c941 2020-02-20 stsp }
3346 ecb28ae0 2018-07-16 stsp
3347 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3348 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3349 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3350 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3351 78756c87 2020-11-24 stsp
3352 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3353 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3354 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3355 7e8004ba 2022-09-11 thomas
3356 fb2756b9 2018-08-04 stsp s->repo = repo;
3357 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3358 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3359 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3360 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3361 9cd7cbd1 2020-12-07 stsp goto done;
3362 9cd7cbd1 2020-12-07 stsp }
3363 9cd7cbd1 2020-12-07 stsp }
3364 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3365 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3366 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3367 5036bf37 2018-09-24 stsp goto done;
3368 5036bf37 2018-09-24 stsp }
3369 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3370 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3371 e5a0f69f 2018-08-18 stsp
3372 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3373 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3374 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3375 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3376 11b20872 2019-11-08 stsp if (err)
3377 11b20872 2019-11-08 stsp goto done;
3378 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3379 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3380 11b20872 2019-11-08 stsp if (err) {
3381 11b20872 2019-11-08 stsp free_colors(&s->colors);
3382 11b20872 2019-11-08 stsp goto done;
3383 11b20872 2019-11-08 stsp }
3384 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3385 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3386 11b20872 2019-11-08 stsp if (err) {
3387 11b20872 2019-11-08 stsp free_colors(&s->colors);
3388 11b20872 2019-11-08 stsp goto done;
3389 11b20872 2019-11-08 stsp }
3390 11b20872 2019-11-08 stsp }
3391 11b20872 2019-11-08 stsp
3392 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3393 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3394 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3395 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3396 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3397 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3398 1a76625f 2018-10-22 stsp
3399 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3400 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3401 1af5eddf 2022-06-23 thomas if (err)
3402 1af5eddf 2022-06-23 thomas goto done;
3403 1af5eddf 2022-06-23 thomas }
3404 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3405 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3406 7cd52833 2022-06-23 thomas if (err)
3407 7cd52833 2022-06-23 thomas goto done;
3408 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3409 b672a97a 2020-01-27 stsp !s->log_branches);
3410 1a76625f 2018-10-22 stsp if (err)
3411 1a76625f 2018-10-22 stsp goto done;
3412 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3413 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3414 c5b78334 2020-01-12 stsp if (err)
3415 c5b78334 2020-01-12 stsp goto done;
3416 1a76625f 2018-10-22 stsp
3417 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3418 1a76625f 2018-10-22 stsp if (errcode) {
3419 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3420 1a76625f 2018-10-22 stsp goto done;
3421 1a76625f 2018-10-22 stsp }
3422 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3423 7c1452c1 2020-03-26 stsp if (errcode) {
3424 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3425 7c1452c1 2020-03-26 stsp goto done;
3426 7c1452c1 2020-03-26 stsp }
3427 1a76625f 2018-10-22 stsp
3428 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3429 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3430 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3431 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3432 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3433 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3434 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3435 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3436 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3437 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3438 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3439 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3440 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3441 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3442 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3443 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3444 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3445 ba4f502b 2018-08-04 stsp done:
3446 1a76625f 2018-10-22 stsp if (err)
3447 1a76625f 2018-10-22 stsp close_log_view(view);
3448 ba4f502b 2018-08-04 stsp return err;
3449 ba4f502b 2018-08-04 stsp }
3450 ba4f502b 2018-08-04 stsp
3451 e5a0f69f 2018-08-18 stsp static const struct got_error *
3452 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3453 ba4f502b 2018-08-04 stsp {
3454 f2f6d207 2020-11-24 stsp const struct got_error *err;
3455 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3456 ba4f502b 2018-08-04 stsp
3457 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3458 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3459 2b380cc8 2018-10-24 stsp &s->thread_args);
3460 2b380cc8 2018-10-24 stsp if (errcode)
3461 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3462 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3463 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3464 f2f6d207 2020-11-24 stsp if (err)
3465 f2f6d207 2020-11-24 stsp return err;
3466 f2f6d207 2020-11-24 stsp }
3467 2b380cc8 2018-10-24 stsp }
3468 2b380cc8 2018-10-24 stsp
3469 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3470 b31f89ff 2022-07-01 thomas }
3471 b31f89ff 2022-07-01 thomas
3472 b31f89ff 2022-07-01 thomas static void
3473 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3474 b31f89ff 2022-07-01 thomas {
3475 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3476 b31f89ff 2022-07-01 thomas
3477 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3478 b31f89ff 2022-07-01 thomas return;
3479 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3480 7e8004ba 2022-09-11 thomas view->count = 0;
3481 b31f89ff 2022-07-01 thomas
3482 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3483 b31f89ff 2022-07-01 thomas || home)
3484 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3485 b31f89ff 2022-07-01 thomas
3486 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3487 b31f89ff 2022-07-01 thomas --s->selected;
3488 b31f89ff 2022-07-01 thomas else
3489 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3490 b31f89ff 2022-07-01 thomas
3491 b31f89ff 2022-07-01 thomas select_commit(s);
3492 b31f89ff 2022-07-01 thomas return;
3493 b31f89ff 2022-07-01 thomas }
3494 b31f89ff 2022-07-01 thomas
3495 b31f89ff 2022-07-01 thomas static const struct got_error *
3496 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3497 b31f89ff 2022-07-01 thomas {
3498 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3499 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3500 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3501 b31f89ff 2022-07-01 thomas
3502 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3503 7e8004ba 2022-09-11 thomas return NULL;
3504 7e8004ba 2022-09-11 thomas
3505 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3506 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3507 b31f89ff 2022-07-01 thomas return NULL;
3508 b31f89ff 2022-07-01 thomas
3509 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3510 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3511 b31f89ff 2022-07-01 thomas
3512 7ed048bd 2022-08-12 thomas if (!page) {
3513 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3514 b31f89ff 2022-07-01 thomas ++s->selected;
3515 b31f89ff 2022-07-01 thomas else
3516 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3517 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3518 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3519 7ed048bd 2022-08-12 thomas int n;
3520 7ed048bd 2022-08-12 thomas
3521 7ed048bd 2022-08-12 thomas s->selected = 0;
3522 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3523 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3524 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3525 7ed048bd 2022-08-12 thomas if (entry == NULL)
3526 7ed048bd 2022-08-12 thomas break;
3527 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3528 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3529 7ed048bd 2022-08-12 thomas }
3530 7ed048bd 2022-08-12 thomas if (n > 0)
3531 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3532 b31f89ff 2022-07-01 thomas } else {
3533 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3534 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3535 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3536 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3537 bd3f8225 2022-08-12 thomas else
3538 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3539 b31f89ff 2022-07-01 thomas }
3540 b31f89ff 2022-07-01 thomas if (err)
3541 b31f89ff 2022-07-01 thomas return err;
3542 b31f89ff 2022-07-01 thomas
3543 a5d43cac 2022-07-01 thomas /*
3544 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3545 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3546 a5d43cac 2022-07-01 thomas */
3547 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3548 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3549 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3550 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3551 25100026 2022-08-13 thomas }
3552 a5d43cac 2022-07-01 thomas
3553 b31f89ff 2022-07-01 thomas select_commit(s);
3554 b31f89ff 2022-07-01 thomas
3555 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3556 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3557 b31f89ff 2022-07-01 thomas view->count = 0;
3558 b31f89ff 2022-07-01 thomas
3559 b31f89ff 2022-07-01 thomas return NULL;
3560 e5a0f69f 2018-08-18 stsp }
3561 04cc582a 2018-08-01 stsp
3562 a5d43cac 2022-07-01 thomas static void
3563 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3564 a5d43cac 2022-07-01 thomas {
3565 24415785 2022-07-03 thomas *x = 0;
3566 24415785 2022-07-03 thomas *y = 0;
3567 24415785 2022-07-03 thomas
3568 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3569 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3570 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3571 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3572 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3573 53d2bdd3 2022-07-10 thomas else
3574 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3575 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3576 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3577 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3578 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3579 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3580 53d2bdd3 2022-07-10 thomas else
3581 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3582 53d2bdd3 2022-07-10 thomas }
3583 a5d43cac 2022-07-01 thomas }
3584 a5d43cac 2022-07-01 thomas
3585 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3586 e5a0f69f 2018-08-18 stsp static const struct got_error *
3587 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3588 a5d43cac 2022-07-01 thomas {
3589 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3590 a5d43cac 2022-07-01 thomas
3591 a5d43cac 2022-07-01 thomas view->nlines = y;
3592 64486692 2022-07-07 thomas view->ncols = COLS;
3593 a5d43cac 2022-07-01 thomas err = view_resize(view);
3594 a5d43cac 2022-07-01 thomas if (err)
3595 a5d43cac 2022-07-01 thomas return err;
3596 a5d43cac 2022-07-01 thomas
3597 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3598 a5d43cac 2022-07-01 thomas
3599 a5d43cac 2022-07-01 thomas return err;
3600 07dd3ed3 2022-08-06 thomas }
3601 07dd3ed3 2022-08-06 thomas
3602 07dd3ed3 2022-08-06 thomas static const struct got_error *
3603 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3604 07dd3ed3 2022-08-06 thomas {
3605 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3606 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3607 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3608 25100026 2022-08-13 thomas
3609 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3610 25100026 2022-08-13 thomas return NULL;
3611 07dd3ed3 2022-08-06 thomas
3612 07dd3ed3 2022-08-06 thomas g = view->gline;
3613 07dd3ed3 2022-08-06 thomas view->gline = 0;
3614 07dd3ed3 2022-08-06 thomas
3615 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3616 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3617 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3618 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3619 07dd3ed3 2022-08-06 thomas select_commit(s);
3620 07dd3ed3 2022-08-06 thomas return NULL;
3621 07dd3ed3 2022-08-06 thomas }
3622 07dd3ed3 2022-08-06 thomas
3623 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3624 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3625 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3626 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3627 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3628 07dd3ed3 2022-08-06 thomas if (err)
3629 07dd3ed3 2022-08-06 thomas return err;
3630 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3631 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3632 07dd3ed3 2022-08-06 thomas
3633 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3634 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3635 07dd3ed3 2022-08-06 thomas
3636 07dd3ed3 2022-08-06 thomas select_commit(s);
3637 07dd3ed3 2022-08-06 thomas return NULL;
3638 07dd3ed3 2022-08-06 thomas
3639 a5d43cac 2022-07-01 thomas }
3640 a5d43cac 2022-07-01 thomas
3641 a5d43cac 2022-07-01 thomas static const struct got_error *
3642 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3643 e5a0f69f 2018-08-18 stsp {
3644 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3645 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3646 7ed048bd 2022-08-12 thomas int eos, nscroll;
3647 80ddbec8 2018-04-29 stsp
3648 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3649 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3650 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3651 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3652 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3653 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3654 fb280deb 2021-08-30 stsp }
3655 c0be8933 2022-08-12 thomas if (err)
3656 c0be8933 2022-08-12 thomas return err;
3657 528dedf3 2021-08-30 stsp }
3658 068ab281 2022-07-01 thomas
3659 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3660 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3661 068ab281 2022-07-01 thomas --eos; /* border */
3662 07dd3ed3 2022-08-06 thomas
3663 07dd3ed3 2022-08-06 thomas if (view->gline)
3664 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3665 068ab281 2022-07-01 thomas
3666 528dedf3 2021-08-30 stsp switch (ch) {
3667 7e8004ba 2022-09-11 thomas case '&':
3668 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3669 7e8004ba 2022-09-11 thomas break;
3670 1e37a5c2 2019-05-12 jcs case 'q':
3671 1e37a5c2 2019-05-12 jcs s->quit = 1;
3672 05171be4 2022-06-23 thomas break;
3673 05171be4 2022-06-23 thomas case '0':
3674 05171be4 2022-06-23 thomas view->x = 0;
3675 05171be4 2022-06-23 thomas break;
3676 05171be4 2022-06-23 thomas case '$':
3677 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3678 07b0611c 2022-06-23 thomas view->count = 0;
3679 05171be4 2022-06-23 thomas break;
3680 05171be4 2022-06-23 thomas case KEY_RIGHT:
3681 05171be4 2022-06-23 thomas case 'l':
3682 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3683 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3684 07b0611c 2022-06-23 thomas else
3685 07b0611c 2022-06-23 thomas view->count = 0;
3686 1e37a5c2 2019-05-12 jcs break;
3687 05171be4 2022-06-23 thomas case KEY_LEFT:
3688 05171be4 2022-06-23 thomas case 'h':
3689 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3690 07b0611c 2022-06-23 thomas if (view->x <= 0)
3691 07b0611c 2022-06-23 thomas view->count = 0;
3692 05171be4 2022-06-23 thomas break;
3693 1e37a5c2 2019-05-12 jcs case 'k':
3694 1e37a5c2 2019-05-12 jcs case KEY_UP:
3695 1e37a5c2 2019-05-12 jcs case '<':
3696 1e37a5c2 2019-05-12 jcs case ',':
3697 f7140bf5 2021-10-17 thomas case CTRL('p'):
3698 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3699 912a3f79 2021-08-30 j break;
3700 912a3f79 2021-08-30 j case 'g':
3701 912a3f79 2021-08-30 j case KEY_HOME:
3702 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3703 07b0611c 2022-06-23 thomas view->count = 0;
3704 1e37a5c2 2019-05-12 jcs break;
3705 70f17a53 2022-06-13 thomas case CTRL('u'):
3706 23427b14 2022-06-23 thomas case 'u':
3707 70f17a53 2022-06-13 thomas nscroll /= 2;
3708 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3709 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3710 a4292ac5 2019-05-12 jcs case CTRL('b'):
3711 1c5e5faa 2022-06-23 thomas case 'b':
3712 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3713 1e37a5c2 2019-05-12 jcs break;
3714 1e37a5c2 2019-05-12 jcs case 'j':
3715 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3716 1e37a5c2 2019-05-12 jcs case '>':
3717 1e37a5c2 2019-05-12 jcs case '.':
3718 f7140bf5 2021-10-17 thomas case CTRL('n'):
3719 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3720 912a3f79 2021-08-30 j break;
3721 f69c5a46 2022-07-19 thomas case '@':
3722 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3723 f69c5a46 2022-07-19 thomas break;
3724 912a3f79 2021-08-30 j case 'G':
3725 912a3f79 2021-08-30 j case KEY_END: {
3726 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3727 912a3f79 2021-08-30 j * traverse them all. */
3728 07b0611c 2022-06-23 thomas view->count = 0;
3729 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3730 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3731 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3732 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3733 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3734 1e37a5c2 2019-05-12 jcs break;
3735 912a3f79 2021-08-30 j }
3736 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3737 23427b14 2022-06-23 thomas case 'd':
3738 70f17a53 2022-06-13 thomas nscroll /= 2;
3739 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3740 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3741 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3742 4c2d69cb 2022-06-23 thomas case 'f':
3743 b31f89ff 2022-07-01 thomas case ' ':
3744 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3745 1e37a5c2 2019-05-12 jcs break;
3746 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3747 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3748 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3749 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3750 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3751 2b779855 2020-12-05 naddy select_commit(s);
3752 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3753 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3754 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3755 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3756 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3757 0bf7f153 2020-12-02 naddy }
3758 1e37a5c2 2019-05-12 jcs break;
3759 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3760 444d5325 2022-07-03 thomas case '\r':
3761 07b0611c 2022-06-23 thomas view->count = 0;
3762 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3763 e5a0f69f 2018-08-18 stsp break;
3764 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3765 1e37a5c2 2019-05-12 jcs break;
3766 1be4947a 2022-07-22 thomas case 'T':
3767 07b0611c 2022-06-23 thomas view->count = 0;
3768 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3769 5036bf37 2018-09-24 stsp break;
3770 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3771 1e37a5c2 2019-05-12 jcs break;
3772 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3773 21355643 2020-12-06 stsp case CTRL('l'):
3774 21355643 2020-12-06 stsp case 'B':
3775 07b0611c 2022-06-23 thomas view->count = 0;
3776 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3777 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3778 1e37a5c2 2019-05-12 jcs break;
3779 21355643 2020-12-06 stsp err = stop_log_thread(s);
3780 74cfe85e 2020-10-20 stsp if (err)
3781 74cfe85e 2020-10-20 stsp return err;
3782 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3783 21355643 2020-12-06 stsp char *parent_path;
3784 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3785 21355643 2020-12-06 stsp if (err)
3786 21355643 2020-12-06 stsp return err;
3787 21355643 2020-12-06 stsp free(s->in_repo_path);
3788 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3789 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3790 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3791 21355643 2020-12-06 stsp struct got_object_id *start_id;
3792 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3793 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3794 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3795 466429a1 2022-11-17 thomas if (err) {
3796 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
3797 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
3798 466429a1 2022-11-17 thomas return err;
3799 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
3800 466429a1 2022-11-17 thomas free(s->head_ref_name);
3801 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
3802 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
3803 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3804 466429a1 2022-11-17 thomas &tog_refs, s->repo);
3805 466429a1 2022-11-17 thomas if (err)
3806 466429a1 2022-11-17 thomas return err;
3807 466429a1 2022-11-17 thomas }
3808 21355643 2020-12-06 stsp free(s->start_id);
3809 21355643 2020-12-06 stsp s->start_id = start_id;
3810 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3811 21355643 2020-12-06 stsp } else /* 'B' */
3812 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3813 21355643 2020-12-06 stsp
3814 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3815 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3816 bf7e79b3 2022-06-23 thomas if (err)
3817 bf7e79b3 2022-06-23 thomas return err;
3818 bf7e79b3 2022-06-23 thomas }
3819 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3820 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3821 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3822 74cfe85e 2020-10-20 stsp if (err)
3823 21355643 2020-12-06 stsp return err;
3824 51a10b52 2020-12-26 stsp tog_free_refs();
3825 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3826 ca51c541 2020-12-07 stsp if (err)
3827 ca51c541 2020-12-07 stsp return err;
3828 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3829 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3830 d01904d4 2019-06-25 stsp if (err)
3831 d01904d4 2019-06-25 stsp return err;
3832 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3833 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3834 b672a97a 2020-01-27 stsp if (err)
3835 b672a97a 2020-01-27 stsp return err;
3836 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3837 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3838 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3839 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3840 21355643 2020-12-06 stsp s->selected_entry = NULL;
3841 21355643 2020-12-06 stsp s->selected = 0;
3842 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3843 21355643 2020-12-06 stsp s->quit = 0;
3844 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3845 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3846 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3847 94ecf40d 2022-07-22 thomas view->offset = 0;
3848 d01904d4 2019-06-25 stsp break;
3849 1be4947a 2022-07-22 thomas case 'R':
3850 07b0611c 2022-06-23 thomas view->count = 0;
3851 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3852 6458efa5 2020-11-24 stsp break;
3853 1e37a5c2 2019-05-12 jcs default:
3854 07b0611c 2022-06-23 thomas view->count = 0;
3855 1e37a5c2 2019-05-12 jcs break;
3856 899d86c2 2018-05-10 stsp }
3857 e5a0f69f 2018-08-18 stsp
3858 80ddbec8 2018-04-29 stsp return err;
3859 80ddbec8 2018-04-29 stsp }
3860 80ddbec8 2018-04-29 stsp
3861 4ed7e80c 2018-05-20 stsp static const struct got_error *
3862 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3863 c2db6724 2019-01-04 stsp {
3864 c2db6724 2019-01-04 stsp const struct got_error *error;
3865 c2db6724 2019-01-04 stsp
3866 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3867 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3868 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3869 37c06ea4 2019-07-15 stsp #endif
3870 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3871 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3872 c2db6724 2019-01-04 stsp
3873 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3874 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3875 c2db6724 2019-01-04 stsp
3876 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3877 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3878 c2db6724 2019-01-04 stsp
3879 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3880 c2db6724 2019-01-04 stsp if (error != NULL)
3881 c2db6724 2019-01-04 stsp return error;
3882 c2db6724 2019-01-04 stsp
3883 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3884 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3885 c2db6724 2019-01-04 stsp
3886 c2db6724 2019-01-04 stsp return NULL;
3887 c2db6724 2019-01-04 stsp }
3888 c2db6724 2019-01-04 stsp
3889 a915003a 2019-02-05 stsp static void
3890 a915003a 2019-02-05 stsp init_curses(void)
3891 a915003a 2019-02-05 stsp {
3892 296152d1 2022-05-31 thomas /*
3893 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3894 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3895 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3896 296152d1 2022-05-31 thomas */
3897 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3898 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3899 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3900 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3901 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3902 296152d1 2022-05-31 thomas
3903 a915003a 2019-02-05 stsp initscr();
3904 a915003a 2019-02-05 stsp cbreak();
3905 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3906 a915003a 2019-02-05 stsp noecho();
3907 a915003a 2019-02-05 stsp nonl();
3908 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3909 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3910 a915003a 2019-02-05 stsp curs_set(0);
3911 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3912 6d17833f 2019-11-08 stsp start_color();
3913 6d17833f 2019-11-08 stsp use_default_colors();
3914 6d17833f 2019-11-08 stsp }
3915 a915003a 2019-02-05 stsp }
3916 a915003a 2019-02-05 stsp
3917 c2db6724 2019-01-04 stsp static const struct got_error *
3918 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3919 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3920 f135c941 2020-02-20 stsp {
3921 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3922 f135c941 2020-02-20 stsp
3923 f135c941 2020-02-20 stsp if (argc == 0) {
3924 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3925 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3926 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3927 f135c941 2020-02-20 stsp return NULL;
3928 f135c941 2020-02-20 stsp }
3929 f135c941 2020-02-20 stsp
3930 f135c941 2020-02-20 stsp if (worktree) {
3931 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3932 bfd61697 2020-11-14 stsp char *p;
3933 f135c941 2020-02-20 stsp
3934 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3935 f135c941 2020-02-20 stsp if (err)
3936 f135c941 2020-02-20 stsp return err;
3937 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3938 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3939 bfd61697 2020-11-14 stsp p) == -1) {
3940 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3941 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3942 f135c941 2020-02-20 stsp }
3943 f135c941 2020-02-20 stsp free(p);
3944 f135c941 2020-02-20 stsp } else
3945 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3946 f135c941 2020-02-20 stsp
3947 f135c941 2020-02-20 stsp return err;
3948 f135c941 2020-02-20 stsp }
3949 f135c941 2020-02-20 stsp
3950 f135c941 2020-02-20 stsp static const struct got_error *
3951 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3952 9f7d7167 2018-04-29 stsp {
3953 80ddbec8 2018-04-29 stsp const struct got_error *error;
3954 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3955 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3956 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3957 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3958 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3959 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3960 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3961 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3962 04cc582a 2018-08-01 stsp struct tog_view *view;
3963 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3964 80ddbec8 2018-04-29 stsp
3965 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3966 80ddbec8 2018-04-29 stsp switch (ch) {
3967 b672a97a 2020-01-27 stsp case 'b':
3968 b672a97a 2020-01-27 stsp log_branches = 1;
3969 b672a97a 2020-01-27 stsp break;
3970 80ddbec8 2018-04-29 stsp case 'c':
3971 80ddbec8 2018-04-29 stsp start_commit = optarg;
3972 80ddbec8 2018-04-29 stsp break;
3973 ecb28ae0 2018-07-16 stsp case 'r':
3974 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3975 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3976 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3977 9ba1d308 2019-10-21 stsp optarg);
3978 ecb28ae0 2018-07-16 stsp break;
3979 80ddbec8 2018-04-29 stsp default:
3980 17020d27 2019-03-07 stsp usage_log();
3981 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3982 80ddbec8 2018-04-29 stsp }
3983 80ddbec8 2018-04-29 stsp }
3984 80ddbec8 2018-04-29 stsp
3985 80ddbec8 2018-04-29 stsp argc -= optind;
3986 80ddbec8 2018-04-29 stsp argv += optind;
3987 80ddbec8 2018-04-29 stsp
3988 f135c941 2020-02-20 stsp if (argc > 1)
3989 f135c941 2020-02-20 stsp usage_log();
3990 963f97a1 2019-03-18 stsp
3991 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3992 7cd52833 2022-06-23 thomas if (error != NULL)
3993 7cd52833 2022-06-23 thomas goto done;
3994 7cd52833 2022-06-23 thomas
3995 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3996 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3997 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3998 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3999 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4000 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4001 c156c7a4 2020-12-18 stsp goto done;
4002 a1fbf39a 2019-08-11 stsp if (worktree)
4003 6962eb72 2020-02-20 stsp repo_path =
4004 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4005 a1fbf39a 2019-08-11 stsp else
4006 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4007 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4008 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4009 c156c7a4 2020-12-18 stsp goto done;
4010 c156c7a4 2020-12-18 stsp }
4011 ecb28ae0 2018-07-16 stsp }
4012 ecb28ae0 2018-07-16 stsp
4013 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4014 80ddbec8 2018-04-29 stsp if (error != NULL)
4015 ecb28ae0 2018-07-16 stsp goto done;
4016 80ddbec8 2018-04-29 stsp
4017 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4018 f135c941 2020-02-20 stsp repo, worktree);
4019 f135c941 2020-02-20 stsp if (error)
4020 f135c941 2020-02-20 stsp goto done;
4021 f135c941 2020-02-20 stsp
4022 f135c941 2020-02-20 stsp init_curses();
4023 f135c941 2020-02-20 stsp
4024 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4025 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4026 c02c541e 2019-03-29 stsp if (error)
4027 c02c541e 2019-03-29 stsp goto done;
4028 c02c541e 2019-03-29 stsp
4029 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4030 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4031 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4032 87670572 2020-12-26 naddy if (error)
4033 87670572 2020-12-26 naddy goto done;
4034 87670572 2020-12-26 naddy }
4035 51a10b52 2020-12-26 stsp
4036 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4037 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4038 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4039 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4040 d8f38dc4 2020-12-05 stsp if (error)
4041 d8f38dc4 2020-12-05 stsp goto done;
4042 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4043 d8f38dc4 2020-12-05 stsp } else {
4044 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4045 d8f38dc4 2020-12-05 stsp if (error == NULL)
4046 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4047 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4048 d8f38dc4 2020-12-05 stsp goto done;
4049 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4050 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4051 d8f38dc4 2020-12-05 stsp if (error)
4052 d8f38dc4 2020-12-05 stsp goto done;
4053 d8f38dc4 2020-12-05 stsp }
4054 8b473291 2019-02-21 stsp
4055 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4056 04cc582a 2018-08-01 stsp if (view == NULL) {
4057 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4058 04cc582a 2018-08-01 stsp goto done;
4059 04cc582a 2018-08-01 stsp }
4060 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4061 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4062 ba4f502b 2018-08-04 stsp if (error)
4063 ba4f502b 2018-08-04 stsp goto done;
4064 2fc00ff4 2019-08-31 stsp if (worktree) {
4065 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4066 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4067 2fc00ff4 2019-08-31 stsp worktree = NULL;
4068 2fc00ff4 2019-08-31 stsp }
4069 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4070 ecb28ae0 2018-07-16 stsp done:
4071 f135c941 2020-02-20 stsp free(in_repo_path);
4072 ecb28ae0 2018-07-16 stsp free(repo_path);
4073 ecb28ae0 2018-07-16 stsp free(cwd);
4074 899d86c2 2018-05-10 stsp free(start_id);
4075 d8f38dc4 2020-12-05 stsp free(label);
4076 d8f38dc4 2020-12-05 stsp if (ref)
4077 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4078 1d0f4054 2021-06-17 stsp if (repo) {
4079 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4080 1d0f4054 2021-06-17 stsp if (error == NULL)
4081 1d0f4054 2021-06-17 stsp error = close_err;
4082 1d0f4054 2021-06-17 stsp }
4083 ec142235 2019-03-07 stsp if (worktree)
4084 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4085 7cd52833 2022-06-23 thomas if (pack_fds) {
4086 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4087 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4088 7cd52833 2022-06-23 thomas if (error == NULL)
4089 7cd52833 2022-06-23 thomas error = pack_err;
4090 7cd52833 2022-06-23 thomas }
4091 51a10b52 2020-12-26 stsp tog_free_refs();
4092 80ddbec8 2018-04-29 stsp return error;
4093 9f7d7167 2018-04-29 stsp }
4094 9f7d7167 2018-04-29 stsp
4095 4ed7e80c 2018-05-20 stsp __dead static void
4096 9f7d7167 2018-04-29 stsp usage_diff(void)
4097 9f7d7167 2018-04-29 stsp {
4098 80ddbec8 2018-04-29 stsp endwin();
4099 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4100 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4101 9f7d7167 2018-04-29 stsp exit(1);
4102 b304db33 2018-05-20 stsp }
4103 b304db33 2018-05-20 stsp
4104 6d17833f 2019-11-08 stsp static int
4105 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4106 41605754 2020-11-12 stsp regmatch_t *regmatch)
4107 6d17833f 2019-11-08 stsp {
4108 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4109 6d17833f 2019-11-08 stsp }
4110 6d17833f 2019-11-08 stsp
4111 ef20f542 2022-06-26 thomas static struct tog_color *
4112 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4113 6d17833f 2019-11-08 stsp {
4114 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4115 6d17833f 2019-11-08 stsp
4116 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4117 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4118 6d17833f 2019-11-08 stsp return tc;
4119 6d17833f 2019-11-08 stsp }
4120 6d17833f 2019-11-08 stsp
4121 6d17833f 2019-11-08 stsp return NULL;
4122 6d17833f 2019-11-08 stsp }
4123 6d17833f 2019-11-08 stsp
4124 4ed7e80c 2018-05-20 stsp static const struct got_error *
4125 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4126 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4127 41605754 2020-11-12 stsp {
4128 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4129 666f7b10 2022-06-23 thomas char *exstr = NULL;
4130 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4131 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4132 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4133 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4134 41605754 2020-11-12 stsp
4135 41605754 2020-11-12 stsp *wtotal = 0;
4136 cbea3800 2022-06-23 thomas
4137 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4138 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4139 41605754 2020-11-12 stsp
4140 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4141 666f7b10 2022-06-23 thomas if (err)
4142 666f7b10 2022-06-23 thomas return err;
4143 666f7b10 2022-06-23 thomas
4144 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4145 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4146 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4147 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4148 666f7b10 2022-06-23 thomas goto done;
4149 666f7b10 2022-06-23 thomas }
4150 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4151 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4152 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4153 cbea3800 2022-06-23 thomas goto done;
4154 cbea3800 2022-06-23 thomas }
4155 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4156 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4157 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4158 cbea3800 2022-06-23 thomas goto done;
4159 cbea3800 2022-06-23 thomas }
4160 05171be4 2022-06-23 thomas
4161 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4162 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4163 cbea3800 2022-06-23 thomas col_tab_align, 1);
4164 cbea3800 2022-06-23 thomas if (err)
4165 cbea3800 2022-06-23 thomas goto done;
4166 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4167 05171be4 2022-06-23 thomas if (n) {
4168 cbea3800 2022-06-23 thomas free(wline);
4169 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4170 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4171 cbea3800 2022-06-23 thomas if (err)
4172 cbea3800 2022-06-23 thomas goto done;
4173 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4174 cbea3800 2022-06-23 thomas wlimit -= width;
4175 cbea3800 2022-06-23 thomas *wtotal += width;
4176 41605754 2020-11-12 stsp }
4177 41605754 2020-11-12 stsp
4178 41605754 2020-11-12 stsp if (wlimit > 0) {
4179 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4180 cbea3800 2022-06-23 thomas size_t wlen;
4181 cbea3800 2022-06-23 thomas
4182 cbea3800 2022-06-23 thomas free(wline);
4183 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4184 cbea3800 2022-06-23 thomas col_tab_align, 1);
4185 cbea3800 2022-06-23 thomas if (err)
4186 cbea3800 2022-06-23 thomas goto done;
4187 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4188 cbea3800 2022-06-23 thomas while (i < wlen) {
4189 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4190 cbea3800 2022-06-23 thomas if (width == -1) {
4191 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4192 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4193 cbea3800 2022-06-23 thomas goto done;
4194 cbea3800 2022-06-23 thomas }
4195 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4196 cbea3800 2022-06-23 thomas break;
4197 1c5e5faa 2022-06-23 thomas w += width;
4198 cbea3800 2022-06-23 thomas i++;
4199 41605754 2020-11-12 stsp }
4200 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4201 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4202 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4203 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4204 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4205 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4206 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4207 41605754 2020-11-12 stsp }
4208 41605754 2020-11-12 stsp }
4209 41605754 2020-11-12 stsp
4210 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4211 cbea3800 2022-06-23 thomas free(wline);
4212 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4213 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4214 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4215 cbea3800 2022-06-23 thomas col_tab_align, 1);
4216 cbea3800 2022-06-23 thomas if (err)
4217 cbea3800 2022-06-23 thomas goto done;
4218 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4219 cbea3800 2022-06-23 thomas } else {
4220 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4221 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4222 cbea3800 2022-06-23 thomas if (err)
4223 cbea3800 2022-06-23 thomas goto done;
4224 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4225 cbea3800 2022-06-23 thomas }
4226 cbea3800 2022-06-23 thomas *wtotal += width2;
4227 41605754 2020-11-12 stsp }
4228 cbea3800 2022-06-23 thomas done:
4229 05171be4 2022-06-23 thomas free(wline);
4230 666f7b10 2022-06-23 thomas free(exstr);
4231 cbea3800 2022-06-23 thomas free(seg0);
4232 cbea3800 2022-06-23 thomas free(seg1);
4233 cbea3800 2022-06-23 thomas free(seg2);
4234 cbea3800 2022-06-23 thomas return err;
4235 41605754 2020-11-12 stsp }
4236 07dd3ed3 2022-08-06 thomas
4237 07dd3ed3 2022-08-06 thomas static int
4238 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4239 07dd3ed3 2022-08-06 thomas {
4240 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4241 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4242 07dd3ed3 2022-08-06 thomas
4243 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4244 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4245 fc2737d5 2022-09-15 thomas
4246 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4247 fc2737d5 2022-09-15 thomas selected = first;
4248 fc2737d5 2022-09-15 thomas eof = &s->eof;
4249 fc2737d5 2022-09-15 thomas f = s->f;
4250 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4251 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4252 41605754 2020-11-12 stsp
4253 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4254 07dd3ed3 2022-08-06 thomas selected = first;
4255 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4256 07dd3ed3 2022-08-06 thomas f = s->f;
4257 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4258 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4259 07dd3ed3 2022-08-06 thomas
4260 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4261 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4262 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4263 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4264 07dd3ed3 2022-08-06 thomas } else
4265 07dd3ed3 2022-08-06 thomas return 0;
4266 07dd3ed3 2022-08-06 thomas
4267 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4268 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4269 07dd3ed3 2022-08-06 thomas return 0;
4270 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4271 07dd3ed3 2022-08-06 thomas rewind(f);
4272 07dd3ed3 2022-08-06 thomas *eof = 0;
4273 07dd3ed3 2022-08-06 thomas *first = 1;
4274 07dd3ed3 2022-08-06 thomas *lineno = 0;
4275 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4276 07dd3ed3 2022-08-06 thomas return 0;
4277 07dd3ed3 2022-08-06 thomas }
4278 07dd3ed3 2022-08-06 thomas
4279 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4280 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4281 07dd3ed3 2022-08-06 thomas view->gline = 0;
4282 07dd3ed3 2022-08-06 thomas
4283 07dd3ed3 2022-08-06 thomas return 1;
4284 07dd3ed3 2022-08-06 thomas }
4285 07dd3ed3 2022-08-06 thomas
4286 41605754 2020-11-12 stsp static const struct got_error *
4287 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4288 26ed57b2 2018-05-19 stsp {
4289 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4290 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4291 61e69b96 2018-05-20 stsp const struct got_error *err;
4292 82c78e96 2022-08-06 thomas int nprinted = 0;
4293 b304db33 2018-05-20 stsp char *line;
4294 826082fe 2020-12-10 stsp size_t linesize = 0;
4295 826082fe 2020-12-10 stsp ssize_t linelen;
4296 61e69b96 2018-05-20 stsp wchar_t *wline;
4297 e0b650dd 2018-05-20 stsp int width;
4298 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4299 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4300 fe621944 2020-11-10 stsp off_t line_offset;
4301 26ed57b2 2018-05-19 stsp
4302 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4303 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4304 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4305 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4306 fe621944 2020-11-10 stsp
4307 f7d12f7e 2018-08-01 stsp werase(view->window);
4308 a3404814 2018-09-02 stsp
4309 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4310 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4311 07dd3ed3 2022-08-06 thomas
4312 a3404814 2018-09-02 stsp if (header) {
4313 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4314 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4315 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4316 07dd3ed3 2022-08-06 thomas
4317 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4318 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4319 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4320 f91a2b48 2022-06-23 thomas 0, 0);
4321 135a2da0 2020-11-11 stsp free(line);
4322 135a2da0 2020-11-11 stsp if (err)
4323 a3404814 2018-09-02 stsp return err;
4324 a3404814 2018-09-02 stsp
4325 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4326 a3404814 2018-09-02 stsp wstandout(view->window);
4327 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4328 e54cc94a 2020-11-11 stsp free(wline);
4329 e54cc94a 2020-11-11 stsp wline = NULL;
4330 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4331 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4332 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4333 a3404814 2018-09-02 stsp wstandend(view->window);
4334 26ed57b2 2018-05-19 stsp
4335 a3404814 2018-09-02 stsp if (max_lines <= 1)
4336 a3404814 2018-09-02 stsp return NULL;
4337 a3404814 2018-09-02 stsp max_lines--;
4338 a3404814 2018-09-02 stsp }
4339 a3404814 2018-09-02 stsp
4340 89f1a395 2020-12-01 naddy s->eof = 0;
4341 05171be4 2022-06-23 thomas view->maxx = 0;
4342 826082fe 2020-12-10 stsp line = NULL;
4343 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4344 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4345 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4346 6568f0aa 2022-08-06 thomas
4347 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4348 826082fe 2020-12-10 stsp if (linelen == -1) {
4349 826082fe 2020-12-10 stsp if (feof(s->f)) {
4350 826082fe 2020-12-10 stsp s->eof = 1;
4351 826082fe 2020-12-10 stsp break;
4352 826082fe 2020-12-10 stsp }
4353 826082fe 2020-12-10 stsp free(line);
4354 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4355 61e69b96 2018-05-20 stsp }
4356 05171be4 2022-06-23 thomas
4357 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4358 07dd3ed3 2022-08-06 thomas continue;
4359 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4360 07dd3ed3 2022-08-06 thomas continue;
4361 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4362 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4363 07dd3ed3 2022-08-06 thomas
4364 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4365 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4366 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4367 cbea3800 2022-06-23 thomas if (err) {
4368 cbea3800 2022-06-23 thomas free(line);
4369 cbea3800 2022-06-23 thomas return err;
4370 cbea3800 2022-06-23 thomas }
4371 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4372 cbea3800 2022-06-23 thomas free(wline);
4373 cbea3800 2022-06-23 thomas wline = NULL;
4374 cbea3800 2022-06-23 thomas
4375 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4376 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4377 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4378 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4379 07dd3ed3 2022-08-06 thomas if (attr)
4380 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4381 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4382 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4383 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4384 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4385 41605754 2020-11-12 stsp if (err) {
4386 41605754 2020-11-12 stsp free(line);
4387 41605754 2020-11-12 stsp return err;
4388 41605754 2020-11-12 stsp }
4389 41605754 2020-11-12 stsp } else {
4390 f1c0ec19 2022-06-23 thomas int skip;
4391 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4392 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4393 f1c0ec19 2022-06-23 thomas if (err) {
4394 f1c0ec19 2022-06-23 thomas free(line);
4395 f1c0ec19 2022-06-23 thomas return err;
4396 f1c0ec19 2022-06-23 thomas }
4397 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4398 f1c0ec19 2022-06-23 thomas free(wline);
4399 41605754 2020-11-12 stsp wline = NULL;
4400 41605754 2020-11-12 stsp }
4401 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4402 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4403 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4404 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4405 07dd3ed3 2022-08-06 thomas } else {
4406 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4407 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4408 07dd3ed3 2022-08-06 thomas }
4409 07dd3ed3 2022-08-06 thomas if (attr)
4410 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4411 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4412 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4413 826082fe 2020-12-10 stsp }
4414 826082fe 2020-12-10 stsp free(line);
4415 fe621944 2020-11-10 stsp if (nprinted >= 1)
4416 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4417 89f1a395 2020-12-01 naddy (nprinted - 1);
4418 fe621944 2020-11-10 stsp else
4419 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4420 26ed57b2 2018-05-19 stsp
4421 a5d43cac 2022-07-01 thomas view_border(view);
4422 c3e9aa98 2019-05-13 jcs
4423 89f1a395 2020-12-01 naddy if (s->eof) {
4424 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4425 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4426 c3e9aa98 2019-05-13 jcs nprinted++;
4427 c3e9aa98 2019-05-13 jcs }
4428 c3e9aa98 2019-05-13 jcs
4429 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4430 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4431 c3e9aa98 2019-05-13 jcs if (err) {
4432 c3e9aa98 2019-05-13 jcs return err;
4433 c3e9aa98 2019-05-13 jcs }
4434 26ed57b2 2018-05-19 stsp
4435 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4436 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4437 e54cc94a 2020-11-11 stsp free(wline);
4438 e54cc94a 2020-11-11 stsp wline = NULL;
4439 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4440 c3e9aa98 2019-05-13 jcs }
4441 c3e9aa98 2019-05-13 jcs
4442 26ed57b2 2018-05-19 stsp return NULL;
4443 abd2672a 2018-12-23 stsp }
4444 abd2672a 2018-12-23 stsp
4445 abd2672a 2018-12-23 stsp static char *
4446 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4447 abd2672a 2018-12-23 stsp {
4448 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4449 09867e48 2019-08-13 stsp char *p, *s;
4450 09867e48 2019-08-13 stsp
4451 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4452 09867e48 2019-08-13 stsp if (tm == NULL)
4453 09867e48 2019-08-13 stsp return NULL;
4454 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4455 09867e48 2019-08-13 stsp if (s == NULL)
4456 09867e48 2019-08-13 stsp return NULL;
4457 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4458 abd2672a 2018-12-23 stsp if (p)
4459 abd2672a 2018-12-23 stsp *p = '\0';
4460 abd2672a 2018-12-23 stsp return s;
4461 9f7d7167 2018-04-29 stsp }
4462 9f7d7167 2018-04-29 stsp
4463 4ed7e80c 2018-05-20 stsp static const struct got_error *
4464 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4465 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4466 0208f208 2020-05-05 stsp {
4467 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4468 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4469 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4470 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4471 0208f208 2020-05-05 stsp
4472 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4473 0208f208 2020-05-05 stsp if (qid != NULL) {
4474 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4475 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4476 ec242592 2022-04-22 thomas &qid->id);
4477 0208f208 2020-05-05 stsp if (err)
4478 0208f208 2020-05-05 stsp return err;
4479 0208f208 2020-05-05 stsp
4480 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4481 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4482 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4483 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4484 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4485 aa8b5dd0 2021-08-01 stsp }
4486 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4487 0208f208 2020-05-05 stsp
4488 0208f208 2020-05-05 stsp }
4489 0208f208 2020-05-05 stsp
4490 0208f208 2020-05-05 stsp if (tree_id1) {
4491 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4492 0208f208 2020-05-05 stsp if (err)
4493 0208f208 2020-05-05 stsp goto done;
4494 0208f208 2020-05-05 stsp }
4495 0208f208 2020-05-05 stsp
4496 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4497 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4498 0208f208 2020-05-05 stsp if (err)
4499 0208f208 2020-05-05 stsp goto done;
4500 0208f208 2020-05-05 stsp
4501 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4502 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4503 0208f208 2020-05-05 stsp done:
4504 0208f208 2020-05-05 stsp if (tree1)
4505 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4506 0208f208 2020-05-05 stsp if (tree2)
4507 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4508 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4509 0208f208 2020-05-05 stsp return err;
4510 0208f208 2020-05-05 stsp }
4511 0208f208 2020-05-05 stsp
4512 0208f208 2020-05-05 stsp static const struct got_error *
4513 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4514 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4515 abd2672a 2018-12-23 stsp {
4516 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4517 fe621944 2020-11-10 stsp
4518 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4519 fe621944 2020-11-10 stsp if (p == NULL)
4520 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4521 82c78e96 2022-08-06 thomas *lines = p;
4522 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4523 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4524 fe621944 2020-11-10 stsp (*nlines)++;
4525 82c78e96 2022-08-06 thomas
4526 fe621944 2020-11-10 stsp return NULL;
4527 fe621944 2020-11-10 stsp }
4528 fe621944 2020-11-10 stsp
4529 fe621944 2020-11-10 stsp static const struct got_error *
4530 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4531 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4532 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4533 fe621944 2020-11-10 stsp {
4534 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4535 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4536 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4537 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4538 45d799e2 2018-12-23 stsp time_t committer_time;
4539 45d799e2 2018-12-23 stsp const char *author, *committer;
4540 8b473291 2019-02-21 stsp char *refs_str = NULL;
4541 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4542 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4543 fe621944 2020-11-10 stsp off_t outoff = 0;
4544 fe621944 2020-11-10 stsp int n;
4545 abd2672a 2018-12-23 stsp
4546 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4547 0208f208 2020-05-05 stsp
4548 8b473291 2019-02-21 stsp if (refs) {
4549 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4550 8b473291 2019-02-21 stsp if (err)
4551 8b473291 2019-02-21 stsp return err;
4552 8b473291 2019-02-21 stsp }
4553 8b473291 2019-02-21 stsp
4554 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4555 abd2672a 2018-12-23 stsp if (err)
4556 abd2672a 2018-12-23 stsp return err;
4557 abd2672a 2018-12-23 stsp
4558 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4559 15a94983 2018-12-23 stsp if (err) {
4560 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4561 15a94983 2018-12-23 stsp goto done;
4562 15a94983 2018-12-23 stsp }
4563 abd2672a 2018-12-23 stsp
4564 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4565 fe621944 2020-11-10 stsp if (err)
4566 fe621944 2020-11-10 stsp goto done;
4567 fe621944 2020-11-10 stsp
4568 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4569 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4570 fe621944 2020-11-10 stsp if (n < 0) {
4571 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4572 abd2672a 2018-12-23 stsp goto done;
4573 abd2672a 2018-12-23 stsp }
4574 fe621944 2020-11-10 stsp outoff += n;
4575 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4576 fe621944 2020-11-10 stsp if (err)
4577 fe621944 2020-11-10 stsp goto done;
4578 fe621944 2020-11-10 stsp
4579 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4580 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4581 fe621944 2020-11-10 stsp if (n < 0) {
4582 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4583 abd2672a 2018-12-23 stsp goto done;
4584 abd2672a 2018-12-23 stsp }
4585 fe621944 2020-11-10 stsp outoff += n;
4586 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4587 fe621944 2020-11-10 stsp if (err)
4588 fe621944 2020-11-10 stsp goto done;
4589 fe621944 2020-11-10 stsp
4590 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4591 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4592 fe621944 2020-11-10 stsp if (datestr) {
4593 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4594 fe621944 2020-11-10 stsp if (n < 0) {
4595 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4596 fe621944 2020-11-10 stsp goto done;
4597 fe621944 2020-11-10 stsp }
4598 fe621944 2020-11-10 stsp outoff += n;
4599 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4600 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4601 fe621944 2020-11-10 stsp if (err)
4602 fe621944 2020-11-10 stsp goto done;
4603 abd2672a 2018-12-23 stsp }
4604 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4605 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4606 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4607 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4608 fe621944 2020-11-10 stsp if (n < 0) {
4609 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4610 fe621944 2020-11-10 stsp goto done;
4611 fe621944 2020-11-10 stsp }
4612 fe621944 2020-11-10 stsp outoff += n;
4613 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4614 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_AUTHOR);
4615 fe621944 2020-11-10 stsp if (err)
4616 fe621944 2020-11-10 stsp goto done;
4617 abd2672a 2018-12-23 stsp }
4618 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4619 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4620 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4621 ac4dc263 2021-09-24 thomas int pn = 1;
4622 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4623 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4624 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4625 ac4dc263 2021-09-24 thomas if (err)
4626 ac4dc263 2021-09-24 thomas goto done;
4627 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4628 ac4dc263 2021-09-24 thomas if (n < 0) {
4629 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4630 ac4dc263 2021-09-24 thomas goto done;
4631 ac4dc263 2021-09-24 thomas }
4632 ac4dc263 2021-09-24 thomas outoff += n;
4633 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4634 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4635 ac4dc263 2021-09-24 thomas if (err)
4636 ac4dc263 2021-09-24 thomas goto done;
4637 ac4dc263 2021-09-24 thomas free(id_str);
4638 ac4dc263 2021-09-24 thomas id_str = NULL;
4639 ac4dc263 2021-09-24 thomas }
4640 ac4dc263 2021-09-24 thomas }
4641 ac4dc263 2021-09-24 thomas
4642 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4643 5943eee2 2019-08-13 stsp if (err)
4644 5943eee2 2019-08-13 stsp goto done;
4645 fe621944 2020-11-10 stsp s = logmsg;
4646 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4647 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4648 fe621944 2020-11-10 stsp if (n < 0) {
4649 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4650 fe621944 2020-11-10 stsp goto done;
4651 fe621944 2020-11-10 stsp }
4652 fe621944 2020-11-10 stsp outoff += n;
4653 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4654 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4655 fe621944 2020-11-10 stsp if (err)
4656 fe621944 2020-11-10 stsp goto done;
4657 abd2672a 2018-12-23 stsp }
4658 fe621944 2020-11-10 stsp
4659 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4660 0208f208 2020-05-05 stsp if (err)
4661 0208f208 2020-05-05 stsp goto done;
4662 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4663 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4664 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4665 fe621944 2020-11-10 stsp if (n < 0) {
4666 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4667 fe621944 2020-11-10 stsp goto done;
4668 fe621944 2020-11-10 stsp }
4669 fe621944 2020-11-10 stsp outoff += n;
4670 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4671 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4672 fe621944 2020-11-10 stsp if (err)
4673 fe621944 2020-11-10 stsp goto done;
4674 0208f208 2020-05-05 stsp free((char *)pe->path);
4675 0208f208 2020-05-05 stsp free(pe->data);
4676 0208f208 2020-05-05 stsp }
4677 fe621944 2020-11-10 stsp
4678 0208f208 2020-05-05 stsp fputc('\n', outfile);
4679 fe621944 2020-11-10 stsp outoff++;
4680 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4681 abd2672a 2018-12-23 stsp done:
4682 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4683 abd2672a 2018-12-23 stsp free(id_str);
4684 5943eee2 2019-08-13 stsp free(logmsg);
4685 8b473291 2019-02-21 stsp free(refs_str);
4686 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4687 7510f233 2020-08-09 stsp if (err) {
4688 82c78e96 2022-08-06 thomas free(*lines);
4689 82c78e96 2022-08-06 thomas *lines = NULL;
4690 ae6a6978 2020-08-09 stsp *nlines = 0;
4691 7510f233 2020-08-09 stsp }
4692 fe621944 2020-11-10 stsp return err;
4693 abd2672a 2018-12-23 stsp }
4694 abd2672a 2018-12-23 stsp
4695 abd2672a 2018-12-23 stsp static const struct got_error *
4696 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4697 26ed57b2 2018-05-19 stsp {
4698 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4699 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4700 15a94983 2018-12-23 stsp int obj_type;
4701 fe621944 2020-11-10 stsp
4702 82c78e96 2022-08-06 thomas free(s->lines);
4703 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4704 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4705 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4706 fe621944 2020-11-10 stsp s->nlines = 0;
4707 26ed57b2 2018-05-19 stsp
4708 511a516b 2018-05-19 stsp f = got_opentemp();
4709 48ae06ee 2018-10-18 stsp if (f == NULL) {
4710 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4711 48ae06ee 2018-10-18 stsp goto done;
4712 48ae06ee 2018-10-18 stsp }
4713 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4714 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4715 fb43ecf1 2019-02-11 stsp goto done;
4716 fb43ecf1 2019-02-11 stsp }
4717 48ae06ee 2018-10-18 stsp s->f = f;
4718 26ed57b2 2018-05-19 stsp
4719 15a94983 2018-12-23 stsp if (s->id1)
4720 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4721 15a94983 2018-12-23 stsp else
4722 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4723 15a94983 2018-12-23 stsp if (err)
4724 15a94983 2018-12-23 stsp goto done;
4725 15a94983 2018-12-23 stsp
4726 15a94983 2018-12-23 stsp switch (obj_type) {
4727 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4728 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4729 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4730 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4731 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4732 26ed57b2 2018-05-19 stsp break;
4733 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4734 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4735 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4736 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4737 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4738 26ed57b2 2018-05-19 stsp break;
4739 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4740 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4741 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4742 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4743 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4744 abd2672a 2018-12-23 stsp
4745 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4746 abd2672a 2018-12-23 stsp if (err)
4747 3ffacbe1 2020-02-02 tracey goto done;
4748 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4749 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4750 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4751 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4752 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4753 f44b1f58 2020-02-02 tracey if (err)
4754 f44b1f58 2020-02-02 tracey goto done;
4755 f44b1f58 2020-02-02 tracey } else {
4756 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4757 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4758 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4759 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4760 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4761 82c78e96 2022-08-06 thomas s->f);
4762 f44b1f58 2020-02-02 tracey if (err)
4763 f44b1f58 2020-02-02 tracey goto done;
4764 f5404e4e 2020-02-02 tracey break;
4765 15a087fe 2019-02-21 stsp }
4766 abd2672a 2018-12-23 stsp }
4767 abd2672a 2018-12-23 stsp }
4768 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4769 abd2672a 2018-12-23 stsp
4770 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4771 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4772 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4773 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4774 26ed57b2 2018-05-19 stsp break;
4775 abd2672a 2018-12-23 stsp }
4776 26ed57b2 2018-05-19 stsp default:
4777 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4778 48ae06ee 2018-10-18 stsp break;
4779 26ed57b2 2018-05-19 stsp }
4780 48ae06ee 2018-10-18 stsp done:
4781 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4782 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4783 48ae06ee 2018-10-18 stsp return err;
4784 48ae06ee 2018-10-18 stsp }
4785 26ed57b2 2018-05-19 stsp
4786 f5215bb9 2019-02-22 stsp static void
4787 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4788 f5215bb9 2019-02-22 stsp {
4789 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4790 f5215bb9 2019-02-22 stsp update_panels();
4791 f5215bb9 2019-02-22 stsp doupdate();
4792 f44b1f58 2020-02-02 tracey }
4793 f44b1f58 2020-02-02 tracey
4794 f44b1f58 2020-02-02 tracey static const struct got_error *
4795 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4796 f44b1f58 2020-02-02 tracey {
4797 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4798 f44b1f58 2020-02-02 tracey
4799 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4800 f44b1f58 2020-02-02 tracey return NULL;
4801 f44b1f58 2020-02-02 tracey }
4802 f44b1f58 2020-02-02 tracey
4803 2a7d3cd7 2022-09-17 thomas static void
4804 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4805 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
4806 f44b1f58 2020-02-02 tracey {
4807 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
4808 fc2737d5 2022-09-15 thomas
4809 2a7d3cd7 2022-09-17 thomas *f = s->f;
4810 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
4811 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
4812 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
4813 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
4814 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
4815 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
4816 fc2737d5 2022-09-15 thomas }
4817 fc2737d5 2022-09-15 thomas
4818 fc2737d5 2022-09-15 thomas static const struct got_error *
4819 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
4820 fc2737d5 2022-09-15 thomas {
4821 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4822 fc2737d5 2022-09-15 thomas FILE *f;
4823 f44b1f58 2020-02-02 tracey int lineno;
4824 78eecffc 2022-06-23 thomas char *line = NULL;
4825 826082fe 2020-12-10 stsp size_t linesize = 0;
4826 826082fe 2020-12-10 stsp ssize_t linelen;
4827 fc2737d5 2022-09-15 thomas off_t *line_offsets;
4828 fc2737d5 2022-09-15 thomas size_t nlines = 0;
4829 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
4830 f44b1f58 2020-02-02 tracey
4831 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
4832 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
4833 2a7d3cd7 2022-09-17 thomas "view search not supported");
4834 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4835 fc2737d5 2022-09-15 thomas &match, &selected);
4836 fc2737d5 2022-09-15 thomas
4837 f44b1f58 2020-02-02 tracey if (!view->searching) {
4838 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4839 f44b1f58 2020-02-02 tracey return NULL;
4840 f44b1f58 2020-02-02 tracey }
4841 f44b1f58 2020-02-02 tracey
4842 fc2737d5 2022-09-15 thomas if (*match) {
4843 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4844 fc2737d5 2022-09-15 thomas lineno = *match + 1;
4845 f44b1f58 2020-02-02 tracey else
4846 fc2737d5 2022-09-15 thomas lineno = *match - 1;
4847 4b3f9dac 2021-12-17 thomas } else
4848 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
4849 f44b1f58 2020-02-02 tracey
4850 f44b1f58 2020-02-02 tracey while (1) {
4851 f44b1f58 2020-02-02 tracey off_t offset;
4852 f44b1f58 2020-02-02 tracey
4853 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
4854 fc2737d5 2022-09-15 thomas if (*match == 0) {
4855 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4856 f44b1f58 2020-02-02 tracey break;
4857 f44b1f58 2020-02-02 tracey }
4858 f44b1f58 2020-02-02 tracey
4859 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4860 f44b1f58 2020-02-02 tracey lineno = 1;
4861 f44b1f58 2020-02-02 tracey else
4862 fc2737d5 2022-09-15 thomas lineno = nlines;
4863 f44b1f58 2020-02-02 tracey }
4864 f44b1f58 2020-02-02 tracey
4865 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
4866 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
4867 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
4868 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
4869 f44b1f58 2020-02-02 tracey free(line);
4870 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4871 f44b1f58 2020-02-02 tracey }
4872 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
4873 78eecffc 2022-06-23 thomas if (linelen != -1) {
4874 78eecffc 2022-06-23 thomas char *exstr;
4875 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4876 78eecffc 2022-06-23 thomas if (err)
4877 78eecffc 2022-06-23 thomas break;
4878 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4879 78eecffc 2022-06-23 thomas &view->regmatch)) {
4880 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4881 fc2737d5 2022-09-15 thomas *match = lineno;
4882 78eecffc 2022-06-23 thomas free(exstr);
4883 78eecffc 2022-06-23 thomas break;
4884 78eecffc 2022-06-23 thomas }
4885 78eecffc 2022-06-23 thomas free(exstr);
4886 f44b1f58 2020-02-02 tracey }
4887 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4888 f44b1f58 2020-02-02 tracey lineno++;
4889 f44b1f58 2020-02-02 tracey else
4890 f44b1f58 2020-02-02 tracey lineno--;
4891 f44b1f58 2020-02-02 tracey }
4892 826082fe 2020-12-10 stsp free(line);
4893 f44b1f58 2020-02-02 tracey
4894 fc2737d5 2022-09-15 thomas if (*match) {
4895 fc2737d5 2022-09-15 thomas *first = *match;
4896 fc2737d5 2022-09-15 thomas *selected = 1;
4897 f44b1f58 2020-02-02 tracey }
4898 f44b1f58 2020-02-02 tracey
4899 05171be4 2022-06-23 thomas return err;
4900 f5215bb9 2019-02-22 stsp }
4901 f5215bb9 2019-02-22 stsp
4902 48ae06ee 2018-10-18 stsp static const struct got_error *
4903 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4904 a0f32f33 2022-06-13 thomas {
4905 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4906 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4907 a0f32f33 2022-06-13 thomas
4908 a0f32f33 2022-06-13 thomas free(s->id1);
4909 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4910 a0f32f33 2022-06-13 thomas free(s->id2);
4911 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4912 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4913 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4914 a0f32f33 2022-06-13 thomas s->f = NULL;
4915 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4916 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4917 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4918 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4919 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4920 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4921 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4922 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4923 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4924 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4925 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4926 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4927 82c78e96 2022-08-06 thomas free(s->lines);
4928 82c78e96 2022-08-06 thomas s->lines = NULL;
4929 a0f32f33 2022-06-13 thomas s->nlines = 0;
4930 a0f32f33 2022-06-13 thomas return err;
4931 a0f32f33 2022-06-13 thomas }
4932 a0f32f33 2022-06-13 thomas
4933 a0f32f33 2022-06-13 thomas static const struct got_error *
4934 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4935 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4936 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4937 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4938 48ae06ee 2018-10-18 stsp {
4939 48ae06ee 2018-10-18 stsp const struct got_error *err;
4940 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4941 5dc9f4bc 2018-08-04 stsp
4942 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4943 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4944 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4945 a0f32f33 2022-06-13 thomas
4946 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4947 15a94983 2018-12-23 stsp int type1, type2;
4948 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4949 15a94983 2018-12-23 stsp if (err)
4950 15a94983 2018-12-23 stsp return err;
4951 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4952 15a94983 2018-12-23 stsp if (err)
4953 15a94983 2018-12-23 stsp return err;
4954 15a94983 2018-12-23 stsp
4955 15a94983 2018-12-23 stsp if (type1 != type2)
4956 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4957 15a94983 2018-12-23 stsp }
4958 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4959 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4960 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4961 f44b1f58 2020-02-02 tracey s->repo = repo;
4962 f44b1f58 2020-02-02 tracey s->id1 = id1;
4963 f44b1f58 2020-02-02 tracey s->id2 = id2;
4964 3dbaef42 2020-11-24 stsp s->label1 = label1;
4965 3dbaef42 2020-11-24 stsp s->label2 = label2;
4966 48ae06ee 2018-10-18 stsp
4967 15a94983 2018-12-23 stsp if (id1) {
4968 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4969 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4970 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4971 48ae06ee 2018-10-18 stsp } else
4972 5465d566 2020-02-01 tracey s->id1 = NULL;
4973 48ae06ee 2018-10-18 stsp
4974 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4975 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4976 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4977 a0f32f33 2022-06-13 thomas goto done;
4978 48ae06ee 2018-10-18 stsp }
4979 a0f32f33 2022-06-13 thomas
4980 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4981 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4982 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4983 9a1d7435 2022-06-23 thomas goto done;
4984 9a1d7435 2022-06-23 thomas }
4985 9a1d7435 2022-06-23 thomas
4986 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4987 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4988 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4989 a0f32f33 2022-06-13 thomas goto done;
4990 a0f32f33 2022-06-13 thomas }
4991 a0f32f33 2022-06-13 thomas
4992 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4993 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4994 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4995 19a6a6b5 2022-07-01 thomas goto done;
4996 19a6a6b5 2022-07-01 thomas }
4997 19a6a6b5 2022-07-01 thomas
4998 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4999 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5000 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5001 19a6a6b5 2022-07-01 thomas goto done;
5002 19a6a6b5 2022-07-01 thomas }
5003 19a6a6b5 2022-07-01 thomas
5004 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
5005 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
5006 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5007 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5008 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5009 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5010 5465d566 2020-02-01 tracey s->repo = repo;
5011 6d17833f 2019-11-08 stsp
5012 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5013 6568f0aa 2022-08-06 thomas int rc;
5014 6d17833f 2019-11-08 stsp
5015 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5016 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5017 6568f0aa 2022-08-06 thomas if (rc != ERR)
5018 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5019 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5020 6568f0aa 2022-08-06 thomas if (rc != ERR)
5021 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5022 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5023 6568f0aa 2022-08-06 thomas if (rc != ERR)
5024 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5025 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5026 6568f0aa 2022-08-06 thomas if (rc != ERR)
5027 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5028 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5029 6568f0aa 2022-08-06 thomas if (rc != ERR)
5030 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5031 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5032 6568f0aa 2022-08-06 thomas if (rc != ERR)
5033 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5034 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5035 6568f0aa 2022-08-06 thomas if (rc != ERR)
5036 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5037 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5038 6568f0aa 2022-08-06 thomas if (rc != ERR)
5039 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5040 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5041 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5042 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5043 a0f32f33 2022-06-13 thomas goto done;
5044 6568f0aa 2022-08-06 thomas }
5045 6d17833f 2019-11-08 stsp }
5046 5dc9f4bc 2018-08-04 stsp
5047 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5048 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5049 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5050 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5051 f5215bb9 2019-02-22 stsp
5052 5465d566 2020-02-01 tracey err = create_diff(s);
5053 48ae06ee 2018-10-18 stsp
5054 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5055 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5056 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5057 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5058 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5059 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5060 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5061 a0f32f33 2022-06-13 thomas done:
5062 a0f32f33 2022-06-13 thomas if (err)
5063 a0f32f33 2022-06-13 thomas close_diff_view(view);
5064 e5a0f69f 2018-08-18 stsp return err;
5065 5dc9f4bc 2018-08-04 stsp }
5066 5dc9f4bc 2018-08-04 stsp
5067 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5068 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5069 5dc9f4bc 2018-08-04 stsp {
5070 a3404814 2018-09-02 stsp const struct got_error *err;
5071 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5072 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5073 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5074 a3404814 2018-09-02 stsp
5075 a3404814 2018-09-02 stsp if (s->id1) {
5076 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5077 a3404814 2018-09-02 stsp if (err)
5078 a3404814 2018-09-02 stsp return err;
5079 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5080 3dbaef42 2020-11-24 stsp } else
5081 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5082 3dbaef42 2020-11-24 stsp
5083 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5084 a3404814 2018-09-02 stsp if (err)
5085 a3404814 2018-09-02 stsp return err;
5086 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5087 26ed57b2 2018-05-19 stsp
5088 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5089 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5090 a3404814 2018-09-02 stsp free(id_str1);
5091 a3404814 2018-09-02 stsp free(id_str2);
5092 a3404814 2018-09-02 stsp return err;
5093 a3404814 2018-09-02 stsp }
5094 a3404814 2018-09-02 stsp free(id_str1);
5095 a3404814 2018-09-02 stsp free(id_str2);
5096 a3404814 2018-09-02 stsp
5097 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5098 267bb3b8 2021-08-01 stsp free(header);
5099 267bb3b8 2021-08-01 stsp return err;
5100 15a087fe 2019-02-21 stsp }
5101 15a087fe 2019-02-21 stsp
5102 15a087fe 2019-02-21 stsp static const struct got_error *
5103 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5104 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5105 15a087fe 2019-02-21 stsp {
5106 d7a04538 2019-02-21 stsp const struct got_error *err;
5107 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5108 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5109 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5110 15a087fe 2019-02-21 stsp
5111 15a087fe 2019-02-21 stsp free(s->id2);
5112 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5113 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5114 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5115 15a087fe 2019-02-21 stsp
5116 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5117 d7a04538 2019-02-21 stsp if (err)
5118 d7a04538 2019-02-21 stsp return err;
5119 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5120 15a087fe 2019-02-21 stsp free(s->id1);
5121 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5122 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5123 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5124 15a087fe 2019-02-21 stsp return NULL;
5125 0cf4efb1 2018-09-29 stsp }
5126 0cf4efb1 2018-09-29 stsp
5127 0cf4efb1 2018-09-29 stsp static const struct got_error *
5128 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5129 adf4c9e0 2022-07-03 thomas {
5130 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5131 adf4c9e0 2022-07-03 thomas
5132 adf4c9e0 2022-07-03 thomas view->count = 0;
5133 adf4c9e0 2022-07-03 thomas wclear(view->window);
5134 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5135 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5136 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5137 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5138 adf4c9e0 2022-07-03 thomas return create_diff(s);
5139 82c78e96 2022-08-06 thomas }
5140 82c78e96 2022-08-06 thomas
5141 82c78e96 2022-08-06 thomas static void
5142 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5143 82c78e96 2022-08-06 thomas {
5144 82c78e96 2022-08-06 thomas int start, i;
5145 82c78e96 2022-08-06 thomas
5146 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5147 82c78e96 2022-08-06 thomas
5148 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5149 82c78e96 2022-08-06 thomas if (i == 0)
5150 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5151 82c78e96 2022-08-06 thomas if (--i == start)
5152 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5153 82c78e96 2022-08-06 thomas }
5154 82c78e96 2022-08-06 thomas
5155 82c78e96 2022-08-06 thomas s->selected_line = 1;
5156 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5157 adf4c9e0 2022-07-03 thomas }
5158 adf4c9e0 2022-07-03 thomas
5159 82c78e96 2022-08-06 thomas static void
5160 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5161 82c78e96 2022-08-06 thomas {
5162 82c78e96 2022-08-06 thomas int start, i;
5163 82c78e96 2022-08-06 thomas
5164 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5165 82c78e96 2022-08-06 thomas
5166 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5167 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5168 82c78e96 2022-08-06 thomas i = 0;
5169 82c78e96 2022-08-06 thomas if (++i == start)
5170 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5171 82c78e96 2022-08-06 thomas }
5172 82c78e96 2022-08-06 thomas
5173 82c78e96 2022-08-06 thomas s->selected_line = 1;
5174 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5175 82c78e96 2022-08-06 thomas }
5176 82c78e96 2022-08-06 thomas
5177 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5178 4fc71f3b 2022-07-12 thomas int, int, int);
5179 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5180 4fc71f3b 2022-07-12 thomas int, int);
5181 4fc71f3b 2022-07-12 thomas
5182 adf4c9e0 2022-07-03 thomas static const struct got_error *
5183 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5184 e5a0f69f 2018-08-18 stsp {
5185 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5186 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5187 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5188 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5189 826082fe 2020-12-10 stsp char *line = NULL;
5190 826082fe 2020-12-10 stsp size_t linesize = 0;
5191 826082fe 2020-12-10 stsp ssize_t linelen;
5192 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5193 e5a0f69f 2018-08-18 stsp
5194 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5195 82c78e96 2022-08-06 thomas
5196 e5a0f69f 2018-08-18 stsp switch (ch) {
5197 05171be4 2022-06-23 thomas case '0':
5198 05171be4 2022-06-23 thomas view->x = 0;
5199 05171be4 2022-06-23 thomas break;
5200 05171be4 2022-06-23 thomas case '$':
5201 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5202 07b0611c 2022-06-23 thomas view->count = 0;
5203 05171be4 2022-06-23 thomas break;
5204 05171be4 2022-06-23 thomas case KEY_RIGHT:
5205 05171be4 2022-06-23 thomas case 'l':
5206 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5207 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5208 07b0611c 2022-06-23 thomas else
5209 07b0611c 2022-06-23 thomas view->count = 0;
5210 05171be4 2022-06-23 thomas break;
5211 05171be4 2022-06-23 thomas case KEY_LEFT:
5212 05171be4 2022-06-23 thomas case 'h':
5213 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5214 07b0611c 2022-06-23 thomas if (view->x <= 0)
5215 07b0611c 2022-06-23 thomas view->count = 0;
5216 05171be4 2022-06-23 thomas break;
5217 64453f7e 2020-11-21 stsp case 'a':
5218 3dbaef42 2020-11-24 stsp case 'w':
5219 3dbaef42 2020-11-24 stsp if (ch == 'a')
5220 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5221 3dbaef42 2020-11-24 stsp if (ch == 'w')
5222 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5223 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5224 912a3f79 2021-08-30 j break;
5225 912a3f79 2021-08-30 j case 'g':
5226 912a3f79 2021-08-30 j case KEY_HOME:
5227 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5228 07b0611c 2022-06-23 thomas view->count = 0;
5229 64453f7e 2020-11-21 stsp break;
5230 912a3f79 2021-08-30 j case 'G':
5231 912a3f79 2021-08-30 j case KEY_END:
5232 07b0611c 2022-06-23 thomas view->count = 0;
5233 912a3f79 2021-08-30 j if (s->eof)
5234 912a3f79 2021-08-30 j break;
5235 912a3f79 2021-08-30 j
5236 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5237 912a3f79 2021-08-30 j s->eof = 1;
5238 912a3f79 2021-08-30 j break;
5239 1e37a5c2 2019-05-12 jcs case 'k':
5240 1e37a5c2 2019-05-12 jcs case KEY_UP:
5241 f7140bf5 2021-10-17 thomas case CTRL('p'):
5242 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5243 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5244 07b0611c 2022-06-23 thomas else
5245 07b0611c 2022-06-23 thomas view->count = 0;
5246 1e37a5c2 2019-05-12 jcs break;
5247 70f17a53 2022-06-13 thomas case CTRL('u'):
5248 23427b14 2022-06-23 thomas case 'u':
5249 70f17a53 2022-06-13 thomas nscroll /= 2;
5250 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5251 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5252 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5253 1c5e5faa 2022-06-23 thomas case 'b':
5254 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5255 07b0611c 2022-06-23 thomas view->count = 0;
5256 26ed57b2 2018-05-19 stsp break;
5257 07b0611c 2022-06-23 thomas }
5258 1e37a5c2 2019-05-12 jcs i = 0;
5259 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5260 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5261 1e37a5c2 2019-05-12 jcs break;
5262 1e37a5c2 2019-05-12 jcs case 'j':
5263 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5264 f7140bf5 2021-10-17 thomas case CTRL('n'):
5265 1e37a5c2 2019-05-12 jcs if (!s->eof)
5266 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5267 07b0611c 2022-06-23 thomas else
5268 07b0611c 2022-06-23 thomas view->count = 0;
5269 1e37a5c2 2019-05-12 jcs break;
5270 70f17a53 2022-06-13 thomas case CTRL('d'):
5271 23427b14 2022-06-23 thomas case 'd':
5272 70f17a53 2022-06-13 thomas nscroll /= 2;
5273 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5274 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5275 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5276 1c5e5faa 2022-06-23 thomas case 'f':
5277 1e37a5c2 2019-05-12 jcs case ' ':
5278 07b0611c 2022-06-23 thomas if (s->eof) {
5279 07b0611c 2022-06-23 thomas view->count = 0;
5280 1e37a5c2 2019-05-12 jcs break;
5281 07b0611c 2022-06-23 thomas }
5282 1e37a5c2 2019-05-12 jcs i = 0;
5283 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5284 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5285 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5286 826082fe 2020-12-10 stsp if (linelen == -1) {
5287 826082fe 2020-12-10 stsp if (feof(s->f)) {
5288 826082fe 2020-12-10 stsp s->eof = 1;
5289 826082fe 2020-12-10 stsp } else
5290 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5291 34bc9ec9 2019-02-22 stsp break;
5292 826082fe 2020-12-10 stsp }
5293 1e37a5c2 2019-05-12 jcs }
5294 826082fe 2020-12-10 stsp free(line);
5295 1e37a5c2 2019-05-12 jcs break;
5296 82c78e96 2022-08-06 thomas case '(':
5297 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5298 82c78e96 2022-08-06 thomas break;
5299 82c78e96 2022-08-06 thomas case ')':
5300 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5301 82c78e96 2022-08-06 thomas break;
5302 82c78e96 2022-08-06 thomas case '{':
5303 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5304 82c78e96 2022-08-06 thomas break;
5305 82c78e96 2022-08-06 thomas case '}':
5306 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5307 82c78e96 2022-08-06 thomas break;
5308 1e37a5c2 2019-05-12 jcs case '[':
5309 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5310 1e37a5c2 2019-05-12 jcs s->diff_context--;
5311 aa61903a 2021-12-31 thomas s->matched_line = 0;
5312 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5313 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5314 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5315 27829c9e 2020-11-21 stsp s->nlines) {
5316 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5317 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5318 27829c9e 2020-11-21 stsp }
5319 07b0611c 2022-06-23 thomas } else
5320 07b0611c 2022-06-23 thomas view->count = 0;
5321 1e37a5c2 2019-05-12 jcs break;
5322 1e37a5c2 2019-05-12 jcs case ']':
5323 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5324 1e37a5c2 2019-05-12 jcs s->diff_context++;
5325 aa61903a 2021-12-31 thomas s->matched_line = 0;
5326 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5327 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5328 07b0611c 2022-06-23 thomas } else
5329 07b0611c 2022-06-23 thomas view->count = 0;
5330 1e37a5c2 2019-05-12 jcs break;
5331 1e37a5c2 2019-05-12 jcs case '<':
5332 1e37a5c2 2019-05-12 jcs case ',':
5333 777aae21 2022-07-20 thomas case 'K':
5334 4fc71f3b 2022-07-12 thomas up = 1;
5335 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5336 4fc71f3b 2022-07-12 thomas case '>':
5337 4fc71f3b 2022-07-12 thomas case '.':
5338 777aae21 2022-07-20 thomas case 'J':
5339 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5340 07b0611c 2022-06-23 thomas view->count = 0;
5341 48ae06ee 2018-10-18 stsp break;
5342 07b0611c 2022-06-23 thomas }
5343 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5344 6524637e 2019-02-21 stsp
5345 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5346 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5347 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5348 15a087fe 2019-02-21 stsp
5349 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5350 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5351 4fc71f3b 2022-07-12 thomas if (err)
5352 4fc71f3b 2022-07-12 thomas break;
5353 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5354 15a087fe 2019-02-21 stsp
5355 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5356 4fc71f3b 2022-07-12 thomas break;
5357 15a087fe 2019-02-21 stsp
5358 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5359 4fc71f3b 2022-07-12 thomas if (err)
5360 4fc71f3b 2022-07-12 thomas break;
5361 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5362 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5363 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5364 5e224a3e 2019-02-22 stsp
5365 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5366 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5367 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5368 4fc71f3b 2022-07-12 thomas
5369 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5370 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5371 4fc71f3b 2022-07-12 thomas if (err)
5372 4fc71f3b 2022-07-12 thomas break;
5373 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5374 5a8b5076 2020-12-05 stsp
5375 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5376 4fc71f3b 2022-07-12 thomas break;
5377 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5378 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5379 4fc71f3b 2022-07-12 thomas bs->selected_line);
5380 4fc71f3b 2022-07-12 thomas if (id == NULL)
5381 4fc71f3b 2022-07-12 thomas break;
5382 15a087fe 2019-02-21 stsp
5383 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5384 4fc71f3b 2022-07-12 thomas break;
5385 15a087fe 2019-02-21 stsp
5386 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5387 4fc71f3b 2022-07-12 thomas if (err)
5388 4fc71f3b 2022-07-12 thomas break;
5389 4fc71f3b 2022-07-12 thomas }
5390 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5391 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5392 aa61903a 2021-12-31 thomas s->matched_line = 0;
5393 05171be4 2022-06-23 thomas view->x = 0;
5394 1e37a5c2 2019-05-12 jcs
5395 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5396 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5397 1e37a5c2 2019-05-12 jcs break;
5398 1e37a5c2 2019-05-12 jcs default:
5399 07b0611c 2022-06-23 thomas view->count = 0;
5400 1e37a5c2 2019-05-12 jcs break;
5401 26ed57b2 2018-05-19 stsp }
5402 e5a0f69f 2018-08-18 stsp
5403 bcbd79e2 2018-08-19 stsp return err;
5404 26ed57b2 2018-05-19 stsp }
5405 26ed57b2 2018-05-19 stsp
5406 4ed7e80c 2018-05-20 stsp static const struct got_error *
5407 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5408 9f7d7167 2018-04-29 stsp {
5409 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5410 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5411 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5412 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5413 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5414 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5415 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5416 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5417 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5418 3dbaef42 2020-11-24 stsp const char *errstr;
5419 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5420 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5421 70ac5f84 2019-03-28 stsp
5422 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5423 26ed57b2 2018-05-19 stsp switch (ch) {
5424 64453f7e 2020-11-21 stsp case 'a':
5425 64453f7e 2020-11-21 stsp force_text_diff = 1;
5426 3dbaef42 2020-11-24 stsp break;
5427 3dbaef42 2020-11-24 stsp case 'C':
5428 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5429 3dbaef42 2020-11-24 stsp &errstr);
5430 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5431 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5432 8f666e67 2022-02-12 thomas errstr, errstr);
5433 64453f7e 2020-11-21 stsp break;
5434 09b5bff8 2020-02-23 naddy case 'r':
5435 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5436 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5437 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5438 09b5bff8 2020-02-23 naddy optarg);
5439 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5440 09b5bff8 2020-02-23 naddy break;
5441 3dbaef42 2020-11-24 stsp case 'w':
5442 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5443 3dbaef42 2020-11-24 stsp break;
5444 26ed57b2 2018-05-19 stsp default:
5445 17020d27 2019-03-07 stsp usage_diff();
5446 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5447 26ed57b2 2018-05-19 stsp }
5448 26ed57b2 2018-05-19 stsp }
5449 26ed57b2 2018-05-19 stsp
5450 26ed57b2 2018-05-19 stsp argc -= optind;
5451 26ed57b2 2018-05-19 stsp argv += optind;
5452 26ed57b2 2018-05-19 stsp
5453 26ed57b2 2018-05-19 stsp if (argc == 0) {
5454 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5455 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5456 15a94983 2018-12-23 stsp id_str1 = argv[0];
5457 15a94983 2018-12-23 stsp id_str2 = argv[1];
5458 26ed57b2 2018-05-19 stsp } else
5459 26ed57b2 2018-05-19 stsp usage_diff();
5460 eb6600df 2019-01-04 stsp
5461 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5462 7cd52833 2022-06-23 thomas if (error)
5463 7cd52833 2022-06-23 thomas goto done;
5464 7cd52833 2022-06-23 thomas
5465 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5466 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5467 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5468 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5469 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5470 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5471 c156c7a4 2020-12-18 stsp goto done;
5472 a273ac94 2020-02-23 naddy if (worktree)
5473 a273ac94 2020-02-23 naddy repo_path =
5474 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5475 a273ac94 2020-02-23 naddy else
5476 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5477 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5478 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5479 c156c7a4 2020-12-18 stsp goto done;
5480 c156c7a4 2020-12-18 stsp }
5481 a273ac94 2020-02-23 naddy }
5482 a273ac94 2020-02-23 naddy
5483 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5484 eb6600df 2019-01-04 stsp if (error)
5485 eb6600df 2019-01-04 stsp goto done;
5486 26ed57b2 2018-05-19 stsp
5487 a273ac94 2020-02-23 naddy init_curses();
5488 a273ac94 2020-02-23 naddy
5489 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5490 51a10b52 2020-12-26 stsp if (error)
5491 51a10b52 2020-12-26 stsp goto done;
5492 51a10b52 2020-12-26 stsp
5493 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5494 26ed57b2 2018-05-19 stsp if (error)
5495 26ed57b2 2018-05-19 stsp goto done;
5496 26ed57b2 2018-05-19 stsp
5497 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5498 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5499 26ed57b2 2018-05-19 stsp if (error)
5500 26ed57b2 2018-05-19 stsp goto done;
5501 26ed57b2 2018-05-19 stsp
5502 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5503 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5504 26ed57b2 2018-05-19 stsp if (error)
5505 26ed57b2 2018-05-19 stsp goto done;
5506 26ed57b2 2018-05-19 stsp
5507 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5508 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5509 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5510 ea5e7bb5 2018-08-01 stsp goto done;
5511 ea5e7bb5 2018-08-01 stsp }
5512 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5513 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5514 5dc9f4bc 2018-08-04 stsp if (error)
5515 5dc9f4bc 2018-08-04 stsp goto done;
5516 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5517 26ed57b2 2018-05-19 stsp done:
5518 3dbaef42 2020-11-24 stsp free(label1);
5519 3dbaef42 2020-11-24 stsp free(label2);
5520 c02c541e 2019-03-29 stsp free(repo_path);
5521 a273ac94 2020-02-23 naddy free(cwd);
5522 1d0f4054 2021-06-17 stsp if (repo) {
5523 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5524 1d0f4054 2021-06-17 stsp if (error == NULL)
5525 1d0f4054 2021-06-17 stsp error = close_err;
5526 1d0f4054 2021-06-17 stsp }
5527 a273ac94 2020-02-23 naddy if (worktree)
5528 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5529 7cd52833 2022-06-23 thomas if (pack_fds) {
5530 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5531 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5532 7cd52833 2022-06-23 thomas if (error == NULL)
5533 7cd52833 2022-06-23 thomas error = pack_err;
5534 7cd52833 2022-06-23 thomas }
5535 51a10b52 2020-12-26 stsp tog_free_refs();
5536 26ed57b2 2018-05-19 stsp return error;
5537 9f7d7167 2018-04-29 stsp }
5538 9f7d7167 2018-04-29 stsp
5539 4ed7e80c 2018-05-20 stsp __dead static void
5540 9f7d7167 2018-04-29 stsp usage_blame(void)
5541 9f7d7167 2018-04-29 stsp {
5542 80ddbec8 2018-04-29 stsp endwin();
5543 91198554 2022-06-23 thomas fprintf(stderr,
5544 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5545 9f7d7167 2018-04-29 stsp getprogname());
5546 9f7d7167 2018-04-29 stsp exit(1);
5547 9f7d7167 2018-04-29 stsp }
5548 84451b3e 2018-07-10 stsp
5549 84451b3e 2018-07-10 stsp struct tog_blame_line {
5550 84451b3e 2018-07-10 stsp int annotated;
5551 84451b3e 2018-07-10 stsp struct got_object_id *id;
5552 84451b3e 2018-07-10 stsp };
5553 9f7d7167 2018-04-29 stsp
5554 4ed7e80c 2018-05-20 stsp static const struct got_error *
5555 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5556 84451b3e 2018-07-10 stsp {
5557 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5558 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5559 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5560 84451b3e 2018-07-10 stsp const struct got_error *err;
5561 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5562 826082fe 2020-12-10 stsp char *line = NULL;
5563 826082fe 2020-12-10 stsp size_t linesize = 0;
5564 826082fe 2020-12-10 stsp ssize_t linelen;
5565 84451b3e 2018-07-10 stsp wchar_t *wline;
5566 27a741e5 2019-09-11 stsp int width;
5567 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5568 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5569 ab089a2a 2018-07-12 stsp char *id_str;
5570 11b20872 2019-11-08 stsp struct tog_color *tc;
5571 ab089a2a 2018-07-12 stsp
5572 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5573 ab089a2a 2018-07-12 stsp if (err)
5574 ab089a2a 2018-07-12 stsp return err;
5575 84451b3e 2018-07-10 stsp
5576 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5577 f7d12f7e 2018-08-01 stsp werase(view->window);
5578 84451b3e 2018-07-10 stsp
5579 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5580 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5581 ab089a2a 2018-07-12 stsp free(id_str);
5582 ab089a2a 2018-07-12 stsp return err;
5583 ab089a2a 2018-07-12 stsp }
5584 ab089a2a 2018-07-12 stsp
5585 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5586 ab089a2a 2018-07-12 stsp free(line);
5587 2550e4c3 2018-07-13 stsp line = NULL;
5588 1cae65b4 2019-09-22 stsp if (err)
5589 1cae65b4 2019-09-22 stsp return err;
5590 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5591 a3404814 2018-09-02 stsp wstandout(view->window);
5592 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5593 11b20872 2019-11-08 stsp if (tc)
5594 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5595 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5596 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5597 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5598 11b20872 2019-11-08 stsp if (tc)
5599 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5600 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5601 a3404814 2018-09-02 stsp wstandend(view->window);
5602 2550e4c3 2018-07-13 stsp free(wline);
5603 2550e4c3 2018-07-13 stsp wline = NULL;
5604 ab089a2a 2018-07-12 stsp
5605 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5606 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5607 07dd3ed3 2022-08-06 thomas
5608 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5609 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5610 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5611 ab089a2a 2018-07-12 stsp free(id_str);
5612 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5613 ab089a2a 2018-07-12 stsp }
5614 ab089a2a 2018-07-12 stsp free(id_str);
5615 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5616 3f60a8ef 2018-07-10 stsp free(line);
5617 2550e4c3 2018-07-13 stsp line = NULL;
5618 3f60a8ef 2018-07-10 stsp if (err)
5619 3f60a8ef 2018-07-10 stsp return err;
5620 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5621 2550e4c3 2018-07-13 stsp free(wline);
5622 2550e4c3 2018-07-13 stsp wline = NULL;
5623 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5624 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5625 3f60a8ef 2018-07-10 stsp
5626 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5627 05171be4 2022-06-23 thomas view->maxx = 0;
5628 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5629 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5630 826082fe 2020-12-10 stsp if (linelen == -1) {
5631 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5632 826082fe 2020-12-10 stsp s->eof = 1;
5633 826082fe 2020-12-10 stsp break;
5634 826082fe 2020-12-10 stsp }
5635 84451b3e 2018-07-10 stsp free(line);
5636 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5637 84451b3e 2018-07-10 stsp }
5638 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5639 07dd3ed3 2022-08-06 thomas continue;
5640 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5641 826082fe 2020-12-10 stsp continue;
5642 cbea3800 2022-06-23 thomas
5643 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5644 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5645 cbea3800 2022-06-23 thomas if (err) {
5646 cbea3800 2022-06-23 thomas free(line);
5647 cbea3800 2022-06-23 thomas return err;
5648 cbea3800 2022-06-23 thomas }
5649 cbea3800 2022-06-23 thomas free(wline);
5650 cbea3800 2022-06-23 thomas wline = NULL;
5651 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5652 84451b3e 2018-07-10 stsp
5653 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5654 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5655 b700b5d6 2018-07-10 stsp
5656 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5657 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5658 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5659 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5660 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5661 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5662 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5663 8d0fe45a 2019-08-12 stsp char *id_str;
5664 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5665 91198554 2022-06-23 thomas blame_line->id);
5666 8d0fe45a 2019-08-12 stsp if (err) {
5667 8d0fe45a 2019-08-12 stsp free(line);
5668 8d0fe45a 2019-08-12 stsp return err;
5669 8d0fe45a 2019-08-12 stsp }
5670 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5671 11b20872 2019-11-08 stsp if (tc)
5672 11b20872 2019-11-08 stsp wattr_on(view->window,
5673 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5674 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5675 11b20872 2019-11-08 stsp if (tc)
5676 11b20872 2019-11-08 stsp wattr_off(view->window,
5677 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5678 8d0fe45a 2019-08-12 stsp free(id_str);
5679 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5680 8d0fe45a 2019-08-12 stsp } else {
5681 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5682 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5683 84451b3e 2018-07-10 stsp }
5684 ee41ec32 2018-07-10 stsp } else {
5685 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5686 ee41ec32 2018-07-10 stsp prev_id = NULL;
5687 ee41ec32 2018-07-10 stsp }
5688 84451b3e 2018-07-10 stsp
5689 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5690 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5691 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5692 27a741e5 2019-09-11 stsp
5693 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5694 41605754 2020-11-12 stsp width = 9;
5695 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5696 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5697 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5698 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5699 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5700 41605754 2020-11-12 stsp if (err) {
5701 41605754 2020-11-12 stsp free(line);
5702 41605754 2020-11-12 stsp return err;
5703 41605754 2020-11-12 stsp }
5704 41605754 2020-11-12 stsp width += 9;
5705 41605754 2020-11-12 stsp } else {
5706 923086fd 2022-06-23 thomas int skip;
5707 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5708 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5709 923086fd 2022-06-23 thomas if (err) {
5710 923086fd 2022-06-23 thomas free(line);
5711 923086fd 2022-06-23 thomas return err;
5712 05171be4 2022-06-23 thomas }
5713 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5714 02bce7e2 2022-06-23 thomas width += 9;
5715 41605754 2020-11-12 stsp free(wline);
5716 41605754 2020-11-12 stsp wline = NULL;
5717 41605754 2020-11-12 stsp }
5718 41605754 2020-11-12 stsp
5719 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5720 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5721 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5722 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5723 84451b3e 2018-07-10 stsp }
5724 826082fe 2020-12-10 stsp free(line);
5725 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5726 84451b3e 2018-07-10 stsp
5727 a5d43cac 2022-07-01 thomas view_border(view);
5728 84451b3e 2018-07-10 stsp
5729 84451b3e 2018-07-10 stsp return NULL;
5730 84451b3e 2018-07-10 stsp }
5731 84451b3e 2018-07-10 stsp
5732 84451b3e 2018-07-10 stsp static const struct got_error *
5733 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5734 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5735 84451b3e 2018-07-10 stsp {
5736 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5737 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5738 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5739 1a76625f 2018-10-22 stsp int errcode;
5740 84451b3e 2018-07-10 stsp
5741 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5742 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5743 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5744 84451b3e 2018-07-10 stsp
5745 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5746 1a76625f 2018-10-22 stsp if (errcode)
5747 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5748 84451b3e 2018-07-10 stsp
5749 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5750 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5751 d68a0a7d 2018-07-10 stsp goto done;
5752 d68a0a7d 2018-07-10 stsp }
5753 d68a0a7d 2018-07-10 stsp
5754 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5755 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5756 d68a0a7d 2018-07-10 stsp
5757 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5758 d68a0a7d 2018-07-10 stsp if (line->annotated)
5759 d68a0a7d 2018-07-10 stsp goto done;
5760 d68a0a7d 2018-07-10 stsp
5761 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5762 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5763 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5764 84451b3e 2018-07-10 stsp goto done;
5765 84451b3e 2018-07-10 stsp }
5766 84451b3e 2018-07-10 stsp line->annotated = 1;
5767 84451b3e 2018-07-10 stsp done:
5768 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5769 1a76625f 2018-10-22 stsp if (errcode)
5770 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5771 84451b3e 2018-07-10 stsp return err;
5772 84451b3e 2018-07-10 stsp }
5773 84451b3e 2018-07-10 stsp
5774 84451b3e 2018-07-10 stsp static void *
5775 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5776 84451b3e 2018-07-10 stsp {
5777 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5778 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5779 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5780 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5781 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5782 00a8878e 2022-07-01 thomas
5783 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5784 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5785 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5786 9117a7b7 2022-07-01 thomas
5787 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5788 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5789 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5790 9117a7b7 2022-07-01 thomas goto done;
5791 9117a7b7 2022-07-01 thomas }
5792 18430de3 2018-07-10 stsp
5793 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5794 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5795 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5796 9117a7b7 2022-07-01 thomas goto done;
5797 9117a7b7 2022-07-01 thomas }
5798 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5799 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5800 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5801 9117a7b7 2022-07-01 thomas goto done;
5802 9117a7b7 2022-07-01 thomas }
5803 9117a7b7 2022-07-01 thomas
5804 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5805 61266923 2020-01-14 stsp if (err)
5806 9117a7b7 2022-07-01 thomas goto done;
5807 61266923 2020-01-14 stsp
5808 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5809 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5810 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5811 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5812 fc06ba56 2019-08-22 stsp err = NULL;
5813 18430de3 2018-07-10 stsp
5814 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5815 9117a7b7 2022-07-01 thomas if (errcode) {
5816 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5817 9117a7b7 2022-07-01 thomas goto done;
5818 9117a7b7 2022-07-01 thomas }
5819 18430de3 2018-07-10 stsp
5820 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5821 1d0f4054 2021-06-17 stsp if (err == NULL)
5822 1d0f4054 2021-06-17 stsp err = close_err;
5823 c9beca56 2018-07-22 stsp ta->repo = NULL;
5824 c9beca56 2018-07-22 stsp *ta->complete = 1;
5825 18430de3 2018-07-10 stsp
5826 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5827 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5828 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5829 18430de3 2018-07-10 stsp
5830 9117a7b7 2022-07-01 thomas done:
5831 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5832 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5833 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5834 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5835 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5836 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5837 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5838 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5839 00a8878e 2022-07-01 thomas
5840 18430de3 2018-07-10 stsp return (void *)err;
5841 84451b3e 2018-07-10 stsp }
5842 84451b3e 2018-07-10 stsp
5843 245d91c1 2018-07-12 stsp static struct got_object_id *
5844 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5845 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5846 245d91c1 2018-07-12 stsp {
5847 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5848 8d0fe45a 2019-08-12 stsp
5849 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5850 8d0fe45a 2019-08-12 stsp return NULL;
5851 b880a918 2018-07-10 stsp
5852 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5853 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5854 4fc71f3b 2022-07-12 thomas return NULL;
5855 4fc71f3b 2022-07-12 thomas
5856 4fc71f3b 2022-07-12 thomas return line->id;
5857 4fc71f3b 2022-07-12 thomas }
5858 4fc71f3b 2022-07-12 thomas
5859 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5860 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5861 4fc71f3b 2022-07-12 thomas int lineno)
5862 4fc71f3b 2022-07-12 thomas {
5863 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5864 4fc71f3b 2022-07-12 thomas
5865 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5866 4fc71f3b 2022-07-12 thomas return NULL;
5867 4fc71f3b 2022-07-12 thomas
5868 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5869 245d91c1 2018-07-12 stsp if (!line->annotated)
5870 245d91c1 2018-07-12 stsp return NULL;
5871 245d91c1 2018-07-12 stsp
5872 245d91c1 2018-07-12 stsp return line->id;
5873 b880a918 2018-07-10 stsp }
5874 245d91c1 2018-07-12 stsp
5875 b880a918 2018-07-10 stsp static const struct got_error *
5876 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5877 a70480e0 2018-06-23 stsp {
5878 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5879 245d91c1 2018-07-12 stsp int i;
5880 245d91c1 2018-07-12 stsp
5881 245d91c1 2018-07-12 stsp if (blame->thread) {
5882 1a76625f 2018-10-22 stsp int errcode;
5883 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5884 1a76625f 2018-10-22 stsp if (errcode)
5885 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5886 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5887 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5888 1a76625f 2018-10-22 stsp if (errcode)
5889 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5890 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5891 1a76625f 2018-10-22 stsp if (errcode)
5892 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5893 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5894 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5895 245d91c1 2018-07-12 stsp err = NULL;
5896 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5897 245d91c1 2018-07-12 stsp }
5898 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5899 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5900 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5901 1d0f4054 2021-06-17 stsp if (err == NULL)
5902 1d0f4054 2021-06-17 stsp err = close_err;
5903 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5904 245d91c1 2018-07-12 stsp }
5905 245d91c1 2018-07-12 stsp if (blame->f) {
5906 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5907 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5908 245d91c1 2018-07-12 stsp blame->f = NULL;
5909 245d91c1 2018-07-12 stsp }
5910 57670559 2018-12-23 stsp if (blame->lines) {
5911 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5912 57670559 2018-12-23 stsp free(blame->lines[i].id);
5913 57670559 2018-12-23 stsp free(blame->lines);
5914 57670559 2018-12-23 stsp blame->lines = NULL;
5915 57670559 2018-12-23 stsp }
5916 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5917 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5918 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5919 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5920 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5921 7cd52833 2022-06-23 thomas if (err == NULL)
5922 7cd52833 2022-06-23 thomas err = pack_err;
5923 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5924 7cd52833 2022-06-23 thomas }
5925 245d91c1 2018-07-12 stsp return err;
5926 245d91c1 2018-07-12 stsp }
5927 245d91c1 2018-07-12 stsp
5928 245d91c1 2018-07-12 stsp static const struct got_error *
5929 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5930 fc06ba56 2019-08-22 stsp {
5931 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5932 fc06ba56 2019-08-22 stsp int *done = arg;
5933 fc06ba56 2019-08-22 stsp int errcode;
5934 fc06ba56 2019-08-22 stsp
5935 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5936 fc06ba56 2019-08-22 stsp if (errcode)
5937 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5938 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5939 fc06ba56 2019-08-22 stsp
5940 fc06ba56 2019-08-22 stsp if (*done)
5941 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5942 fc06ba56 2019-08-22 stsp
5943 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5944 fc06ba56 2019-08-22 stsp if (errcode)
5945 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5946 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5947 fc06ba56 2019-08-22 stsp
5948 fc06ba56 2019-08-22 stsp return err;
5949 fc06ba56 2019-08-22 stsp }
5950 fc06ba56 2019-08-22 stsp
5951 fc06ba56 2019-08-22 stsp static const struct got_error *
5952 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5953 245d91c1 2018-07-12 stsp {
5954 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5955 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5956 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5957 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5958 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5959 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5960 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5961 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5962 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5963 a70480e0 2018-06-23 stsp
5964 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5965 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5966 27d434c2 2018-09-15 stsp if (err)
5967 15a94983 2018-12-23 stsp return err;
5968 f4ae6ddb 2022-07-01 thomas
5969 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5970 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5971 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5972 f4ae6ddb 2022-07-01 thomas goto done;
5973 f4ae6ddb 2022-07-01 thomas }
5974 945f9229 2022-04-16 thomas
5975 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5976 945f9229 2022-04-16 thomas if (err)
5977 945f9229 2022-04-16 thomas goto done;
5978 27d434c2 2018-09-15 stsp
5979 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5980 84451b3e 2018-07-10 stsp if (err)
5981 84451b3e 2018-07-10 stsp goto done;
5982 27d434c2 2018-09-15 stsp
5983 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5984 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5985 84451b3e 2018-07-10 stsp goto done;
5986 84451b3e 2018-07-10 stsp }
5987 a70480e0 2018-06-23 stsp
5988 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5989 a70480e0 2018-06-23 stsp if (err)
5990 a70480e0 2018-06-23 stsp goto done;
5991 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5992 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5993 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5994 84451b3e 2018-07-10 stsp goto done;
5995 84451b3e 2018-07-10 stsp }
5996 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5997 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5998 1fddf795 2021-01-20 stsp if (err)
5999 1fddf795 2021-01-20 stsp goto done;
6000 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6001 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6002 84451b3e 2018-07-10 stsp goto done;
6003 1fddf795 2021-01-20 stsp }
6004 b02560ec 2019-08-19 stsp
6005 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6006 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6007 b02560ec 2019-08-19 stsp blame->nlines--;
6008 a70480e0 2018-06-23 stsp
6009 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6010 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6011 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6012 84451b3e 2018-07-10 stsp goto done;
6013 84451b3e 2018-07-10 stsp }
6014 a70480e0 2018-06-23 stsp
6015 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6016 bd24772e 2018-07-11 stsp if (err)
6017 bd24772e 2018-07-11 stsp goto done;
6018 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6019 7cd52833 2022-06-23 thomas pack_fds);
6020 7cd52833 2022-06-23 thomas if (err)
6021 7cd52833 2022-06-23 thomas goto done;
6022 bd24772e 2018-07-11 stsp
6023 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6024 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6025 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6026 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6027 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6028 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6029 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6030 245d91c1 2018-07-12 stsp goto done;
6031 245d91c1 2018-07-12 stsp }
6032 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6033 245d91c1 2018-07-12 stsp
6034 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6035 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6036 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6037 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6038 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6039 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6040 a5388363 2020-12-01 naddy s->blame_complete = 0;
6041 f5a09613 2020-12-13 naddy
6042 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6043 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6044 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6045 f5a09613 2020-12-13 naddy s->selected_line = 1;
6046 f5a09613 2020-12-13 naddy }
6047 aa61903a 2021-12-31 thomas s->matched_line = 0;
6048 245d91c1 2018-07-12 stsp
6049 245d91c1 2018-07-12 stsp done:
6050 945f9229 2022-04-16 thomas if (commit)
6051 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6052 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6053 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6054 245d91c1 2018-07-12 stsp if (blob)
6055 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6056 27d434c2 2018-09-15 stsp free(obj_id);
6057 245d91c1 2018-07-12 stsp if (err)
6058 245d91c1 2018-07-12 stsp stop_blame(blame);
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 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6064 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6065 245d91c1 2018-07-12 stsp {
6066 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6067 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6068 dbc6a6b6 2018-07-12 stsp
6069 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6070 245d91c1 2018-07-12 stsp
6071 c4843652 2019-08-12 stsp s->path = strdup(path);
6072 c4843652 2019-08-12 stsp if (s->path == NULL)
6073 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6074 c4843652 2019-08-12 stsp
6075 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6076 c4843652 2019-08-12 stsp if (err) {
6077 c4843652 2019-08-12 stsp free(s->path);
6078 7cbe629d 2018-08-04 stsp return err;
6079 c4843652 2019-08-12 stsp }
6080 245d91c1 2018-07-12 stsp
6081 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6082 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6083 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6084 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6085 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6086 fb2756b9 2018-08-04 stsp s->repo = repo;
6087 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6088 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6089 7cbe629d 2018-08-04 stsp
6090 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6091 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6092 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6093 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6094 11b20872 2019-11-08 stsp if (err)
6095 11b20872 2019-11-08 stsp return err;
6096 11b20872 2019-11-08 stsp }
6097 11b20872 2019-11-08 stsp
6098 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6099 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6100 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6101 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6102 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6103 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6104 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6105 e5a0f69f 2018-08-18 stsp
6106 a5388363 2020-12-01 naddy return run_blame(view);
6107 7cbe629d 2018-08-04 stsp }
6108 7cbe629d 2018-08-04 stsp
6109 e5a0f69f 2018-08-18 stsp static const struct got_error *
6110 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6111 7cbe629d 2018-08-04 stsp {
6112 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6113 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6114 7cbe629d 2018-08-04 stsp
6115 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6116 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6117 e5a0f69f 2018-08-18 stsp
6118 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6119 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6120 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6121 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6122 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6123 7cbe629d 2018-08-04 stsp }
6124 e5a0f69f 2018-08-18 stsp
6125 e5a0f69f 2018-08-18 stsp free(s->path);
6126 11b20872 2019-11-08 stsp free_colors(&s->colors);
6127 e5a0f69f 2018-08-18 stsp return err;
6128 7cbe629d 2018-08-04 stsp }
6129 7cbe629d 2018-08-04 stsp
6130 7cbe629d 2018-08-04 stsp static const struct got_error *
6131 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6132 6c4c42e0 2019-06-24 stsp {
6133 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6134 6c4c42e0 2019-06-24 stsp
6135 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6136 6c4c42e0 2019-06-24 stsp return NULL;
6137 2a7d3cd7 2022-09-17 thomas }
6138 2a7d3cd7 2022-09-17 thomas
6139 2a7d3cd7 2022-09-17 thomas static void
6140 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6141 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6142 2a7d3cd7 2022-09-17 thomas {
6143 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6144 2a7d3cd7 2022-09-17 thomas
6145 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6146 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6147 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6148 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6149 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6150 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6151 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6152 6c4c42e0 2019-06-24 stsp }
6153 6c4c42e0 2019-06-24 stsp
6154 6c4c42e0 2019-06-24 stsp static const struct got_error *
6155 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6156 7cbe629d 2018-08-04 stsp {
6157 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6158 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6159 2b380cc8 2018-10-24 stsp int errcode;
6160 2b380cc8 2018-10-24 stsp
6161 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6162 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6163 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6164 2b380cc8 2018-10-24 stsp if (errcode)
6165 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6166 51fe7530 2019-08-19 stsp
6167 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6168 2b380cc8 2018-10-24 stsp }
6169 e5a0f69f 2018-08-18 stsp
6170 51fe7530 2019-08-19 stsp if (s->blame_complete)
6171 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6172 51fe7530 2019-08-19 stsp
6173 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6174 e5a0f69f 2018-08-18 stsp
6175 a5d43cac 2022-07-01 thomas view_border(view);
6176 e5a0f69f 2018-08-18 stsp return err;
6177 e5a0f69f 2018-08-18 stsp }
6178 e5a0f69f 2018-08-18 stsp
6179 e5a0f69f 2018-08-18 stsp static const struct got_error *
6180 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6181 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6182 eaeaa612 2022-07-20 thomas {
6183 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6184 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6185 eaeaa612 2022-07-20 thomas
6186 eaeaa612 2022-07-20 thomas *new_view = NULL;
6187 eaeaa612 2022-07-20 thomas
6188 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6189 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6190 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6191 eaeaa612 2022-07-20 thomas
6192 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6193 eaeaa612 2022-07-20 thomas if (err)
6194 eaeaa612 2022-07-20 thomas view_close(log_view);
6195 eaeaa612 2022-07-20 thomas else
6196 eaeaa612 2022-07-20 thomas *new_view = log_view;
6197 eaeaa612 2022-07-20 thomas
6198 eaeaa612 2022-07-20 thomas return err;
6199 eaeaa612 2022-07-20 thomas }
6200 eaeaa612 2022-07-20 thomas
6201 eaeaa612 2022-07-20 thomas static const struct got_error *
6202 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6203 e5a0f69f 2018-08-18 stsp {
6204 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6205 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6206 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6207 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6208 a5d43cac 2022-07-01 thomas
6209 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6210 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6211 a5d43cac 2022-07-01 thomas --eos; /* border */
6212 7cbe629d 2018-08-04 stsp
6213 e5a0f69f 2018-08-18 stsp switch (ch) {
6214 05171be4 2022-06-23 thomas case '0':
6215 05171be4 2022-06-23 thomas view->x = 0;
6216 05171be4 2022-06-23 thomas break;
6217 05171be4 2022-06-23 thomas case '$':
6218 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
6219 07b0611c 2022-06-23 thomas view->count = 0;
6220 05171be4 2022-06-23 thomas break;
6221 05171be4 2022-06-23 thomas case KEY_RIGHT:
6222 05171be4 2022-06-23 thomas case 'l':
6223 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
6224 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
6225 07b0611c 2022-06-23 thomas else
6226 07b0611c 2022-06-23 thomas view->count = 0;
6227 05171be4 2022-06-23 thomas break;
6228 05171be4 2022-06-23 thomas case KEY_LEFT:
6229 05171be4 2022-06-23 thomas case 'h':
6230 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
6231 07b0611c 2022-06-23 thomas if (view->x <= 0)
6232 07b0611c 2022-06-23 thomas view->count = 0;
6233 05171be4 2022-06-23 thomas break;
6234 1e37a5c2 2019-05-12 jcs case 'q':
6235 1e37a5c2 2019-05-12 jcs s->done = 1;
6236 4deef56f 2021-09-02 naddy break;
6237 4deef56f 2021-09-02 naddy case 'g':
6238 4deef56f 2021-09-02 naddy case KEY_HOME:
6239 4deef56f 2021-09-02 naddy s->selected_line = 1;
6240 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6241 07b0611c 2022-06-23 thomas view->count = 0;
6242 4deef56f 2021-09-02 naddy break;
6243 4deef56f 2021-09-02 naddy case 'G':
6244 4deef56f 2021-09-02 naddy case KEY_END:
6245 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6246 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6247 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6248 4deef56f 2021-09-02 naddy } else {
6249 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6250 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6251 4deef56f 2021-09-02 naddy }
6252 07b0611c 2022-06-23 thomas view->count = 0;
6253 1e37a5c2 2019-05-12 jcs break;
6254 1e37a5c2 2019-05-12 jcs case 'k':
6255 1e37a5c2 2019-05-12 jcs case KEY_UP:
6256 f7140bf5 2021-10-17 thomas case CTRL('p'):
6257 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6258 1e37a5c2 2019-05-12 jcs s->selected_line--;
6259 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6260 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6261 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6262 07b0611c 2022-06-23 thomas else
6263 07b0611c 2022-06-23 thomas view->count = 0;
6264 1e37a5c2 2019-05-12 jcs break;
6265 70f17a53 2022-06-13 thomas case CTRL('u'):
6266 23427b14 2022-06-23 thomas case 'u':
6267 70f17a53 2022-06-13 thomas nscroll /= 2;
6268 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6269 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6270 ea025d1d 2020-02-22 naddy case CTRL('b'):
6271 1c5e5faa 2022-06-23 thomas case 'b':
6272 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6273 07b0611c 2022-06-23 thomas if (view->count > 1)
6274 07b0611c 2022-06-23 thomas nscroll += nscroll;
6275 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6276 07b0611c 2022-06-23 thomas view->count = 0;
6277 e5a0f69f 2018-08-18 stsp break;
6278 1e37a5c2 2019-05-12 jcs }
6279 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6280 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6281 1e37a5c2 2019-05-12 jcs else
6282 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6283 1e37a5c2 2019-05-12 jcs break;
6284 1e37a5c2 2019-05-12 jcs case 'j':
6285 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6286 f7140bf5 2021-10-17 thomas case CTRL('n'):
6287 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6288 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6289 1e37a5c2 2019-05-12 jcs s->selected_line++;
6290 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6291 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6292 07b0611c 2022-06-23 thomas else
6293 07b0611c 2022-06-23 thomas view->count = 0;
6294 1e37a5c2 2019-05-12 jcs break;
6295 1c5e5faa 2022-06-23 thomas case 'c':
6296 1e37a5c2 2019-05-12 jcs case 'p': {
6297 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6298 07b0611c 2022-06-23 thomas
6299 07b0611c 2022-06-23 thomas view->count = 0;
6300 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6301 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6302 1e37a5c2 2019-05-12 jcs if (id == NULL)
6303 e5a0f69f 2018-08-18 stsp break;
6304 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6305 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6306 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6307 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6308 1e37a5c2 2019-05-12 jcs int obj_type;
6309 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6310 1e37a5c2 2019-05-12 jcs s->repo, id);
6311 e5a0f69f 2018-08-18 stsp if (err)
6312 e5a0f69f 2018-08-18 stsp break;
6313 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6314 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6315 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6316 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6317 e5a0f69f 2018-08-18 stsp break;
6318 e5a0f69f 2018-08-18 stsp }
6319 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6320 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6321 ec242592 2022-04-22 thomas s->repo, &pid->id);
6322 945f9229 2022-04-16 thomas if (err)
6323 945f9229 2022-04-16 thomas break;
6324 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6325 945f9229 2022-04-16 thomas pcommit, s->path);
6326 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6327 e5a0f69f 2018-08-18 stsp if (err) {
6328 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6329 1e37a5c2 2019-05-12 jcs err = NULL;
6330 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6331 e5a0f69f 2018-08-18 stsp break;
6332 e5a0f69f 2018-08-18 stsp }
6333 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6334 1e37a5c2 2019-05-12 jcs blob_id);
6335 1e37a5c2 2019-05-12 jcs free(blob_id);
6336 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6337 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6338 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6339 e5a0f69f 2018-08-18 stsp break;
6340 1e37a5c2 2019-05-12 jcs }
6341 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6342 ec242592 2022-04-22 thomas &pid->id);
6343 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6344 1e37a5c2 2019-05-12 jcs } else {
6345 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6346 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6347 1e37a5c2 2019-05-12 jcs break;
6348 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6349 1e37a5c2 2019-05-12 jcs id);
6350 1e37a5c2 2019-05-12 jcs }
6351 1e37a5c2 2019-05-12 jcs if (err)
6352 e5a0f69f 2018-08-18 stsp break;
6353 1e37a5c2 2019-05-12 jcs s->done = 1;
6354 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6355 1e37a5c2 2019-05-12 jcs s->done = 0;
6356 1e37a5c2 2019-05-12 jcs if (thread_err)
6357 1e37a5c2 2019-05-12 jcs break;
6358 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6359 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6360 a5388363 2020-12-01 naddy err = run_blame(view);
6361 1e37a5c2 2019-05-12 jcs if (err)
6362 1e37a5c2 2019-05-12 jcs break;
6363 1e37a5c2 2019-05-12 jcs break;
6364 1e37a5c2 2019-05-12 jcs }
6365 1c5e5faa 2022-06-23 thomas case 'C': {
6366 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6367 07b0611c 2022-06-23 thomas
6368 07b0611c 2022-06-23 thomas view->count = 0;
6369 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6370 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6371 1e37a5c2 2019-05-12 jcs break;
6372 1e37a5c2 2019-05-12 jcs s->done = 1;
6373 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6374 1e37a5c2 2019-05-12 jcs s->done = 0;
6375 1e37a5c2 2019-05-12 jcs if (thread_err)
6376 1e37a5c2 2019-05-12 jcs break;
6377 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6378 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6379 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6380 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6381 a5388363 2020-12-01 naddy err = run_blame(view);
6382 1e37a5c2 2019-05-12 jcs if (err)
6383 1e37a5c2 2019-05-12 jcs break;
6384 1e37a5c2 2019-05-12 jcs break;
6385 1e37a5c2 2019-05-12 jcs }
6386 2a31b33b 2022-07-23 thomas case 'L':
6387 eaeaa612 2022-07-20 thomas view->count = 0;
6388 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6389 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6390 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6391 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6392 eaeaa612 2022-07-20 thomas break;
6393 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6394 1e37a5c2 2019-05-12 jcs case '\r': {
6395 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6396 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6397 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6398 07b0611c 2022-06-23 thomas
6399 07b0611c 2022-06-23 thomas view->count = 0;
6400 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6401 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6402 1e37a5c2 2019-05-12 jcs if (id == NULL)
6403 1e37a5c2 2019-05-12 jcs break;
6404 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6405 1e37a5c2 2019-05-12 jcs if (err)
6406 1e37a5c2 2019-05-12 jcs break;
6407 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6408 4fc71f3b 2022-07-12 thomas if (*new_view) {
6409 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6410 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6411 4fc71f3b 2022-07-12 thomas if (err)
6412 4fc71f3b 2022-07-12 thomas break;
6413 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6414 4fc71f3b 2022-07-12 thomas } else {
6415 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6416 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6417 a5d43cac 2022-07-01 thomas
6418 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6419 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6420 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6421 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6422 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6423 4fc71f3b 2022-07-12 thomas break;
6424 4fc71f3b 2022-07-12 thomas }
6425 15a94983 2018-12-23 stsp }
6426 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6427 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6428 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6429 1e37a5c2 2019-05-12 jcs if (err) {
6430 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6431 1e37a5c2 2019-05-12 jcs break;
6432 1e37a5c2 2019-05-12 jcs }
6433 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6434 4fc71f3b 2022-07-12 thomas s->selected_line;
6435 4fc71f3b 2022-07-12 thomas if (*new_view)
6436 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6437 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6438 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6439 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6440 a5d43cac 2022-07-01 thomas if (err)
6441 a5d43cac 2022-07-01 thomas break;
6442 a5d43cac 2022-07-01 thomas }
6443 a5d43cac 2022-07-01 thomas
6444 e78dc838 2020-12-04 stsp view->focussed = 0;
6445 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6446 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6447 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6448 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6449 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6450 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6451 1e37a5c2 2019-05-12 jcs if (err)
6452 34bc9ec9 2019-02-22 stsp break;
6453 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6454 40236d76 2022-06-23 thomas if (err)
6455 40236d76 2022-06-23 thomas break;
6456 e78dc838 2020-12-04 stsp view->focus_child = 1;
6457 1e37a5c2 2019-05-12 jcs } else
6458 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6459 1e37a5c2 2019-05-12 jcs if (err)
6460 e5a0f69f 2018-08-18 stsp break;
6461 1e37a5c2 2019-05-12 jcs break;
6462 1e37a5c2 2019-05-12 jcs }
6463 70f17a53 2022-06-13 thomas case CTRL('d'):
6464 23427b14 2022-06-23 thomas case 'd':
6465 70f17a53 2022-06-13 thomas nscroll /= 2;
6466 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6467 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6468 ea025d1d 2020-02-22 naddy case CTRL('f'):
6469 1c5e5faa 2022-06-23 thomas case 'f':
6470 1e37a5c2 2019-05-12 jcs case ' ':
6471 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6472 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6473 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6474 07b0611c 2022-06-23 thomas view->count = 0;
6475 e5a0f69f 2018-08-18 stsp break;
6476 1e37a5c2 2019-05-12 jcs }
6477 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6478 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6479 70f17a53 2022-06-13 thomas s->selected_line +=
6480 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6481 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6482 1e37a5c2 2019-05-12 jcs }
6483 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6484 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6485 1e37a5c2 2019-05-12 jcs else
6486 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6487 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6488 1e37a5c2 2019-05-12 jcs break;
6489 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6490 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6491 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6492 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6493 1e37a5c2 2019-05-12 jcs }
6494 1e37a5c2 2019-05-12 jcs break;
6495 1e37a5c2 2019-05-12 jcs default:
6496 07b0611c 2022-06-23 thomas view->count = 0;
6497 1e37a5c2 2019-05-12 jcs break;
6498 a70480e0 2018-06-23 stsp }
6499 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6500 adf4c9e0 2022-07-03 thomas }
6501 adf4c9e0 2022-07-03 thomas
6502 adf4c9e0 2022-07-03 thomas static const struct got_error *
6503 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6504 adf4c9e0 2022-07-03 thomas {
6505 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6506 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6507 adf4c9e0 2022-07-03 thomas
6508 adf4c9e0 2022-07-03 thomas view->count = 0;
6509 adf4c9e0 2022-07-03 thomas s->done = 1;
6510 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6511 adf4c9e0 2022-07-03 thomas s->done = 0;
6512 adf4c9e0 2022-07-03 thomas if (err)
6513 adf4c9e0 2022-07-03 thomas return err;
6514 adf4c9e0 2022-07-03 thomas return run_blame(view);
6515 a70480e0 2018-06-23 stsp }
6516 a70480e0 2018-06-23 stsp
6517 a70480e0 2018-06-23 stsp static const struct got_error *
6518 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6519 9f7d7167 2018-04-29 stsp {
6520 a70480e0 2018-06-23 stsp const struct got_error *error;
6521 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6522 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6523 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6524 0587e10c 2020-07-23 stsp char *link_target = NULL;
6525 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6526 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6527 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6528 a70480e0 2018-06-23 stsp int ch;
6529 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6530 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6531 a70480e0 2018-06-23 stsp
6532 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6533 a70480e0 2018-06-23 stsp switch (ch) {
6534 a70480e0 2018-06-23 stsp case 'c':
6535 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6536 a70480e0 2018-06-23 stsp break;
6537 69069811 2018-08-02 stsp case 'r':
6538 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6539 69069811 2018-08-02 stsp if (repo_path == NULL)
6540 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6541 9ba1d308 2019-10-21 stsp optarg);
6542 69069811 2018-08-02 stsp break;
6543 a70480e0 2018-06-23 stsp default:
6544 17020d27 2019-03-07 stsp usage_blame();
6545 a70480e0 2018-06-23 stsp /* NOTREACHED */
6546 a70480e0 2018-06-23 stsp }
6547 a70480e0 2018-06-23 stsp }
6548 a70480e0 2018-06-23 stsp
6549 a70480e0 2018-06-23 stsp argc -= optind;
6550 a70480e0 2018-06-23 stsp argv += optind;
6551 a70480e0 2018-06-23 stsp
6552 f135c941 2020-02-20 stsp if (argc != 1)
6553 a70480e0 2018-06-23 stsp usage_blame();
6554 6962eb72 2020-02-20 stsp
6555 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6556 7cd52833 2022-06-23 thomas if (error != NULL)
6557 7cd52833 2022-06-23 thomas goto done;
6558 7cd52833 2022-06-23 thomas
6559 69069811 2018-08-02 stsp if (repo_path == NULL) {
6560 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6561 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6562 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6563 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6564 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6565 c156c7a4 2020-12-18 stsp goto done;
6566 f135c941 2020-02-20 stsp if (worktree)
6567 eb41ed75 2019-02-05 stsp repo_path =
6568 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6569 f135c941 2020-02-20 stsp else
6570 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6571 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6572 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6573 c156c7a4 2020-12-18 stsp goto done;
6574 c156c7a4 2020-12-18 stsp }
6575 f135c941 2020-02-20 stsp }
6576 a915003a 2019-02-05 stsp
6577 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6578 c02c541e 2019-03-29 stsp if (error != NULL)
6579 8e94dd5b 2019-01-04 stsp goto done;
6580 69069811 2018-08-02 stsp
6581 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6582 f135c941 2020-02-20 stsp worktree);
6583 c02c541e 2019-03-29 stsp if (error)
6584 92205607 2019-01-04 stsp goto done;
6585 69069811 2018-08-02 stsp
6586 f135c941 2020-02-20 stsp init_curses();
6587 f135c941 2020-02-20 stsp
6588 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6589 51a10b52 2020-12-26 stsp if (error)
6590 51a10b52 2020-12-26 stsp goto done;
6591 51a10b52 2020-12-26 stsp
6592 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6593 eb41ed75 2019-02-05 stsp if (error)
6594 69069811 2018-08-02 stsp goto done;
6595 a70480e0 2018-06-23 stsp
6596 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6597 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6598 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6599 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6600 a70480e0 2018-06-23 stsp if (error != NULL)
6601 66b4983c 2018-06-23 stsp goto done;
6602 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6603 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6604 a70480e0 2018-06-23 stsp } else {
6605 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6606 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6607 a70480e0 2018-06-23 stsp }
6608 a19e88aa 2018-06-23 stsp if (error != NULL)
6609 8b473291 2019-02-21 stsp goto done;
6610 8b473291 2019-02-21 stsp
6611 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6612 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6613 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6614 e1cd8fed 2018-08-01 stsp goto done;
6615 e1cd8fed 2018-08-01 stsp }
6616 0587e10c 2020-07-23 stsp
6617 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6618 945f9229 2022-04-16 thomas if (error)
6619 945f9229 2022-04-16 thomas goto done;
6620 945f9229 2022-04-16 thomas
6621 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6622 945f9229 2022-04-16 thomas commit, repo);
6623 7cbe629d 2018-08-04 stsp if (error)
6624 7cbe629d 2018-08-04 stsp goto done;
6625 0587e10c 2020-07-23 stsp
6626 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6627 78756c87 2020-11-24 stsp commit_id, repo);
6628 0587e10c 2020-07-23 stsp if (error)
6629 0587e10c 2020-07-23 stsp goto done;
6630 12314ad4 2019-08-31 stsp if (worktree) {
6631 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6632 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6633 12314ad4 2019-08-31 stsp worktree = NULL;
6634 12314ad4 2019-08-31 stsp }
6635 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6636 a70480e0 2018-06-23 stsp done:
6637 69069811 2018-08-02 stsp free(repo_path);
6638 f135c941 2020-02-20 stsp free(in_repo_path);
6639 0587e10c 2020-07-23 stsp free(link_target);
6640 69069811 2018-08-02 stsp free(cwd);
6641 a70480e0 2018-06-23 stsp free(commit_id);
6642 945f9229 2022-04-16 thomas if (commit)
6643 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6644 eb41ed75 2019-02-05 stsp if (worktree)
6645 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6646 1d0f4054 2021-06-17 stsp if (repo) {
6647 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6648 1d0f4054 2021-06-17 stsp if (error == NULL)
6649 1d0f4054 2021-06-17 stsp error = close_err;
6650 1d0f4054 2021-06-17 stsp }
6651 7cd52833 2022-06-23 thomas if (pack_fds) {
6652 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6653 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6654 7cd52833 2022-06-23 thomas if (error == NULL)
6655 7cd52833 2022-06-23 thomas error = pack_err;
6656 7cd52833 2022-06-23 thomas }
6657 51a10b52 2020-12-26 stsp tog_free_refs();
6658 a70480e0 2018-06-23 stsp return error;
6659 ffd1d5e5 2018-06-23 stsp }
6660 ffd1d5e5 2018-06-23 stsp
6661 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6662 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6663 ffd1d5e5 2018-06-23 stsp {
6664 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6665 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6666 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6667 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6668 86f4aab9 2022-09-09 thomas char *index = NULL;
6669 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6670 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6671 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6672 ffd1d5e5 2018-06-23 stsp
6673 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6674 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6675 a5d43cac 2022-07-01 thomas --limit; /* border */
6676 ffd1d5e5 2018-06-23 stsp
6677 f7d12f7e 2018-08-01 stsp werase(view->window);
6678 ffd1d5e5 2018-06-23 stsp
6679 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6680 ffd1d5e5 2018-06-23 stsp return NULL;
6681 ffd1d5e5 2018-06-23 stsp
6682 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6683 f91a2b48 2022-06-23 thomas 0, 0);
6684 ffd1d5e5 2018-06-23 stsp if (err)
6685 ffd1d5e5 2018-06-23 stsp return err;
6686 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6687 a3404814 2018-09-02 stsp wstandout(view->window);
6688 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6689 11b20872 2019-11-08 stsp if (tc)
6690 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6691 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6692 86f4aab9 2022-09-09 thomas free(wline);
6693 86f4aab9 2022-09-09 thomas wline = NULL;
6694 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6695 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6696 11b20872 2019-11-08 stsp if (tc)
6697 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6698 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6699 a3404814 2018-09-02 stsp wstandend(view->window);
6700 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6701 86f4aab9 2022-09-09 thomas return NULL;
6702 07dd3ed3 2022-08-06 thomas
6703 6f5f393a 2022-08-12 thomas i += s->selected;
6704 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6705 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6706 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6707 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6708 07dd3ed3 2022-08-06 thomas }
6709 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6710 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6711 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6712 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6713 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6714 86f4aab9 2022-09-09 thomas free(index);
6715 ce52c690 2018-06-23 stsp if (err)
6716 ce52c690 2018-06-23 stsp return err;
6717 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6718 2550e4c3 2018-07-13 stsp free(wline);
6719 2550e4c3 2018-07-13 stsp wline = NULL;
6720 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6721 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6722 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6723 ffd1d5e5 2018-06-23 stsp return NULL;
6724 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6725 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6726 a1eca9bb 2018-06-23 stsp return NULL;
6727 ffd1d5e5 2018-06-23 stsp
6728 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6729 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6730 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6731 0cf4efb1 2018-09-29 stsp if (view->focussed)
6732 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6733 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6734 ffd1d5e5 2018-06-23 stsp }
6735 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6736 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6737 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6738 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6739 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6740 ffd1d5e5 2018-06-23 stsp return NULL;
6741 ffd1d5e5 2018-06-23 stsp n = 1;
6742 ffd1d5e5 2018-06-23 stsp } else {
6743 ffd1d5e5 2018-06-23 stsp n = 0;
6744 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6745 ffd1d5e5 2018-06-23 stsp }
6746 ffd1d5e5 2018-06-23 stsp
6747 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6748 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6749 848d6979 2019-08-12 stsp const char *modestr = "";
6750 56e0773d 2019-11-28 stsp mode_t mode;
6751 1d13200f 2018-07-12 stsp
6752 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6753 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6754 56e0773d 2019-11-28 stsp
6755 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6756 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6757 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6758 1d13200f 2018-07-12 stsp if (err)
6759 638f9024 2019-05-13 stsp return got_error_from_errno(
6760 230a42bd 2019-05-11 jcs "got_object_id_str");
6761 1d13200f 2018-07-12 stsp }
6762 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6763 63c5ca5d 2019-08-24 stsp modestr = "$";
6764 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6765 0d6c6ee3 2020-05-20 stsp int i;
6766 0d6c6ee3 2020-05-20 stsp
6767 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6768 d86d3b18 2020-12-01 naddy te, s->repo);
6769 0d6c6ee3 2020-05-20 stsp if (err) {
6770 0d6c6ee3 2020-05-20 stsp free(id_str);
6771 0d6c6ee3 2020-05-20 stsp return err;
6772 0d6c6ee3 2020-05-20 stsp }
6773 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6774 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6775 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6776 0d6c6ee3 2020-05-20 stsp }
6777 848d6979 2019-08-12 stsp modestr = "@";
6778 0d6c6ee3 2020-05-20 stsp }
6779 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6780 848d6979 2019-08-12 stsp modestr = "/";
6781 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6782 848d6979 2019-08-12 stsp modestr = "*";
6783 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6784 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6785 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6786 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6787 1d13200f 2018-07-12 stsp free(id_str);
6788 0d6c6ee3 2020-05-20 stsp free(link_target);
6789 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6790 1d13200f 2018-07-12 stsp }
6791 1d13200f 2018-07-12 stsp free(id_str);
6792 0d6c6ee3 2020-05-20 stsp free(link_target);
6793 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6794 f91a2b48 2022-06-23 thomas 0, 0);
6795 ffd1d5e5 2018-06-23 stsp if (err) {
6796 ffd1d5e5 2018-06-23 stsp free(line);
6797 ffd1d5e5 2018-06-23 stsp break;
6798 ffd1d5e5 2018-06-23 stsp }
6799 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6800 0cf4efb1 2018-09-29 stsp if (view->focussed)
6801 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6802 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6803 ffd1d5e5 2018-06-23 stsp }
6804 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6805 f26dddb7 2019-11-08 stsp if (tc)
6806 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6807 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6808 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6809 f26dddb7 2019-11-08 stsp if (tc)
6810 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6811 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6812 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6813 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6814 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6815 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6816 ffd1d5e5 2018-06-23 stsp free(line);
6817 2550e4c3 2018-07-13 stsp free(wline);
6818 2550e4c3 2018-07-13 stsp wline = NULL;
6819 ffd1d5e5 2018-06-23 stsp n++;
6820 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6821 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6822 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6823 ffd1d5e5 2018-06-23 stsp break;
6824 ffd1d5e5 2018-06-23 stsp }
6825 ffd1d5e5 2018-06-23 stsp
6826 ffd1d5e5 2018-06-23 stsp return err;
6827 ffd1d5e5 2018-06-23 stsp }
6828 ffd1d5e5 2018-06-23 stsp
6829 ffd1d5e5 2018-06-23 stsp static void
6830 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6831 ffd1d5e5 2018-06-23 stsp {
6832 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6833 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6834 fa86c4bf 2020-11-29 stsp int i = 0;
6835 ffd1d5e5 2018-06-23 stsp
6836 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6837 ffd1d5e5 2018-06-23 stsp return;
6838 ffd1d5e5 2018-06-23 stsp
6839 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6840 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6841 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6842 fa86c4bf 2020-11-29 stsp if (!isroot)
6843 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6844 fa86c4bf 2020-11-29 stsp break;
6845 fa86c4bf 2020-11-29 stsp }
6846 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6847 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6848 ffd1d5e5 2018-06-23 stsp }
6849 ffd1d5e5 2018-06-23 stsp }
6850 ffd1d5e5 2018-06-23 stsp
6851 a5d43cac 2022-07-01 thomas static const struct got_error *
6852 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6853 ffd1d5e5 2018-06-23 stsp {
6854 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6855 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6856 ffd1d5e5 2018-06-23 stsp int n = 0;
6857 ffd1d5e5 2018-06-23 stsp
6858 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6859 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6860 694d3271 2020-12-01 naddy s->first_displayed_entry);
6861 694d3271 2020-12-01 naddy else
6862 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6863 56e0773d 2019-11-28 stsp
6864 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6865 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6866 07dd3ed3 2022-08-06 thomas if (last) {
6867 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6868 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6869 07dd3ed3 2022-08-06 thomas }
6870 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6871 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6872 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6873 768394f3 2019-01-24 stsp }
6874 ffd1d5e5 2018-06-23 stsp }
6875 a5d43cac 2022-07-01 thomas
6876 a5d43cac 2022-07-01 thomas return NULL;
6877 ffd1d5e5 2018-06-23 stsp }
6878 ffd1d5e5 2018-06-23 stsp
6879 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6880 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6881 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6882 ffd1d5e5 2018-06-23 stsp {
6883 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6884 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6885 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6886 ffd1d5e5 2018-06-23 stsp
6887 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6888 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6889 56e0773d 2019-11-28 stsp + 1 /* slash */;
6890 ce52c690 2018-06-23 stsp if (te)
6891 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6892 ce52c690 2018-06-23 stsp
6893 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6894 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6895 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6896 ffd1d5e5 2018-06-23 stsp
6897 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6898 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6899 d9765a41 2018-06-23 stsp while (pt) {
6900 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6901 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6902 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6903 cb2ebc8a 2018-06-23 stsp goto done;
6904 cb2ebc8a 2018-06-23 stsp }
6905 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6906 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6907 cb2ebc8a 2018-06-23 stsp goto done;
6908 cb2ebc8a 2018-06-23 stsp }
6909 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6910 ffd1d5e5 2018-06-23 stsp }
6911 ce52c690 2018-06-23 stsp if (te) {
6912 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6913 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6914 ce52c690 2018-06-23 stsp goto done;
6915 ce52c690 2018-06-23 stsp }
6916 cb2ebc8a 2018-06-23 stsp }
6917 ce52c690 2018-06-23 stsp done:
6918 ce52c690 2018-06-23 stsp if (err) {
6919 ce52c690 2018-06-23 stsp free(*path);
6920 ce52c690 2018-06-23 stsp *path = NULL;
6921 ce52c690 2018-06-23 stsp }
6922 ce52c690 2018-06-23 stsp return err;
6923 ce52c690 2018-06-23 stsp }
6924 ce52c690 2018-06-23 stsp
6925 ce52c690 2018-06-23 stsp static const struct got_error *
6926 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6927 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6928 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6929 ce52c690 2018-06-23 stsp {
6930 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6931 ce52c690 2018-06-23 stsp char *path;
6932 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6933 a0de39f3 2019-08-09 stsp
6934 a0de39f3 2019-08-09 stsp *new_view = NULL;
6935 69efd4c4 2018-07-18 stsp
6936 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6937 ce52c690 2018-06-23 stsp if (err)
6938 ce52c690 2018-06-23 stsp return err;
6939 ffd1d5e5 2018-06-23 stsp
6940 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6941 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6942 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6943 83ce39e3 2019-08-12 stsp goto done;
6944 83ce39e3 2019-08-12 stsp }
6945 cdf1ee82 2018-08-01 stsp
6946 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6947 e5a0f69f 2018-08-18 stsp if (err) {
6948 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6949 fc06ba56 2019-08-22 stsp err = NULL;
6950 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6951 e5a0f69f 2018-08-18 stsp } else
6952 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6953 83ce39e3 2019-08-12 stsp done:
6954 83ce39e3 2019-08-12 stsp free(path);
6955 69efd4c4 2018-07-18 stsp return err;
6956 69efd4c4 2018-07-18 stsp }
6957 69efd4c4 2018-07-18 stsp
6958 69efd4c4 2018-07-18 stsp static const struct got_error *
6959 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6960 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6961 69efd4c4 2018-07-18 stsp {
6962 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6963 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6964 69efd4c4 2018-07-18 stsp char *path;
6965 a0de39f3 2019-08-09 stsp
6966 a0de39f3 2019-08-09 stsp *new_view = NULL;
6967 69efd4c4 2018-07-18 stsp
6968 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6969 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6970 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6971 e5a0f69f 2018-08-18 stsp
6972 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6973 69efd4c4 2018-07-18 stsp if (err)
6974 69efd4c4 2018-07-18 stsp return err;
6975 69efd4c4 2018-07-18 stsp
6976 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6977 4e97c21c 2020-12-06 stsp path, 0);
6978 ba4f502b 2018-08-04 stsp if (err)
6979 e5a0f69f 2018-08-18 stsp view_close(log_view);
6980 e5a0f69f 2018-08-18 stsp else
6981 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6982 cb2ebc8a 2018-06-23 stsp free(path);
6983 cb2ebc8a 2018-06-23 stsp return err;
6984 ffd1d5e5 2018-06-23 stsp }
6985 ffd1d5e5 2018-06-23 stsp
6986 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6987 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6988 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6989 ffd1d5e5 2018-06-23 stsp {
6990 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6991 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6992 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6993 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6994 ffd1d5e5 2018-06-23 stsp
6995 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6996 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6997 bc573f3b 2021-07-10 stsp
6998 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6999 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7000 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7001 ffd1d5e5 2018-06-23 stsp
7002 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7003 bc573f3b 2021-07-10 stsp if (err)
7004 bc573f3b 2021-07-10 stsp goto done;
7005 bc573f3b 2021-07-10 stsp
7006 bc573f3b 2021-07-10 stsp /*
7007 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7008 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7009 bc573f3b 2021-07-10 stsp * closed on demand.
7010 bc573f3b 2021-07-10 stsp */
7011 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7012 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7013 bc573f3b 2021-07-10 stsp if (err)
7014 bc573f3b 2021-07-10 stsp goto done;
7015 bc573f3b 2021-07-10 stsp s->tree = s->root;
7016 bc573f3b 2021-07-10 stsp
7017 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7018 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7019 ffd1d5e5 2018-06-23 stsp goto done;
7020 ffd1d5e5 2018-06-23 stsp
7021 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7022 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7023 ffd1d5e5 2018-06-23 stsp goto done;
7024 ffd1d5e5 2018-06-23 stsp }
7025 ffd1d5e5 2018-06-23 stsp
7026 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7027 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7028 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7029 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7030 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7031 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7032 9cd7cbd1 2020-12-07 stsp goto done;
7033 9cd7cbd1 2020-12-07 stsp }
7034 9cd7cbd1 2020-12-07 stsp }
7035 fb2756b9 2018-08-04 stsp s->repo = repo;
7036 c0b01bdb 2019-11-08 stsp
7037 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7038 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7039 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7040 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7041 c0b01bdb 2019-11-08 stsp if (err)
7042 c0b01bdb 2019-11-08 stsp goto done;
7043 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7044 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7045 bc573f3b 2021-07-10 stsp if (err)
7046 c0b01bdb 2019-11-08 stsp goto done;
7047 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7048 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7049 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7050 bc573f3b 2021-07-10 stsp if (err)
7051 c0b01bdb 2019-11-08 stsp goto done;
7052 e5a0f69f 2018-08-18 stsp
7053 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7054 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7055 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7056 bc573f3b 2021-07-10 stsp if (err)
7057 11b20872 2019-11-08 stsp goto done;
7058 11b20872 2019-11-08 stsp
7059 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7060 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7061 bc573f3b 2021-07-10 stsp if (err)
7062 c0b01bdb 2019-11-08 stsp goto done;
7063 c0b01bdb 2019-11-08 stsp }
7064 c0b01bdb 2019-11-08 stsp
7065 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7066 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7067 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7068 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7069 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7070 ad80ab7b 2018-08-04 stsp done:
7071 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7072 bc573f3b 2021-07-10 stsp if (commit)
7073 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7074 bc573f3b 2021-07-10 stsp if (err)
7075 bc573f3b 2021-07-10 stsp close_tree_view(view);
7076 ad80ab7b 2018-08-04 stsp return err;
7077 ad80ab7b 2018-08-04 stsp }
7078 ad80ab7b 2018-08-04 stsp
7079 e5a0f69f 2018-08-18 stsp static const struct got_error *
7080 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7081 ad80ab7b 2018-08-04 stsp {
7082 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7083 ad80ab7b 2018-08-04 stsp
7084 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7085 fb2756b9 2018-08-04 stsp free(s->tree_label);
7086 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7087 6484ec90 2018-09-29 stsp free(s->commit_id);
7088 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7089 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7090 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7091 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7092 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7093 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7094 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7095 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7096 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7097 ad80ab7b 2018-08-04 stsp free(parent);
7098 ad80ab7b 2018-08-04 stsp
7099 ad80ab7b 2018-08-04 stsp }
7100 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7101 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7102 bc573f3b 2021-07-10 stsp if (s->root)
7103 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7104 7c32bd05 2019-06-22 stsp return NULL;
7105 7c32bd05 2019-06-22 stsp }
7106 7c32bd05 2019-06-22 stsp
7107 7c32bd05 2019-06-22 stsp static const struct got_error *
7108 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7109 7c32bd05 2019-06-22 stsp {
7110 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7111 7c32bd05 2019-06-22 stsp
7112 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7113 7c32bd05 2019-06-22 stsp return NULL;
7114 7c32bd05 2019-06-22 stsp }
7115 7c32bd05 2019-06-22 stsp
7116 7c32bd05 2019-06-22 stsp static int
7117 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7118 7c32bd05 2019-06-22 stsp {
7119 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7120 7c32bd05 2019-06-22 stsp
7121 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7122 56e0773d 2019-11-28 stsp 0) == 0;
7123 7c32bd05 2019-06-22 stsp }
7124 7c32bd05 2019-06-22 stsp
7125 7c32bd05 2019-06-22 stsp static const struct got_error *
7126 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7127 7c32bd05 2019-06-22 stsp {
7128 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7129 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7130 7c32bd05 2019-06-22 stsp
7131 7c32bd05 2019-06-22 stsp if (!view->searching) {
7132 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7133 7c32bd05 2019-06-22 stsp return NULL;
7134 7c32bd05 2019-06-22 stsp }
7135 7c32bd05 2019-06-22 stsp
7136 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7137 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7138 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7139 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7140 56e0773d 2019-11-28 stsp s->selected_entry);
7141 7c32bd05 2019-06-22 stsp else
7142 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7143 56e0773d 2019-11-28 stsp } else {
7144 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7145 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7146 56e0773d 2019-11-28 stsp else
7147 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7148 56e0773d 2019-11-28 stsp s->selected_entry);
7149 7c32bd05 2019-06-22 stsp }
7150 7c32bd05 2019-06-22 stsp } else {
7151 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7152 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7153 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7154 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7155 56e0773d 2019-11-28 stsp else
7156 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7157 7c32bd05 2019-06-22 stsp }
7158 7c32bd05 2019-06-22 stsp
7159 7c32bd05 2019-06-22 stsp while (1) {
7160 56e0773d 2019-11-28 stsp if (te == NULL) {
7161 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7162 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7163 ac66afb8 2019-06-24 stsp return NULL;
7164 ac66afb8 2019-06-24 stsp }
7165 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7166 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7167 56e0773d 2019-11-28 stsp else
7168 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7169 7c32bd05 2019-06-22 stsp }
7170 7c32bd05 2019-06-22 stsp
7171 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7172 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7173 56e0773d 2019-11-28 stsp s->matched_entry = te;
7174 7c32bd05 2019-06-22 stsp break;
7175 7c32bd05 2019-06-22 stsp }
7176 7c32bd05 2019-06-22 stsp
7177 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7178 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7179 56e0773d 2019-11-28 stsp else
7180 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7181 7c32bd05 2019-06-22 stsp }
7182 e5a0f69f 2018-08-18 stsp
7183 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7184 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7185 7c32bd05 2019-06-22 stsp s->selected = 0;
7186 7c32bd05 2019-06-22 stsp }
7187 7c32bd05 2019-06-22 stsp
7188 e5a0f69f 2018-08-18 stsp return NULL;
7189 ad80ab7b 2018-08-04 stsp }
7190 ad80ab7b 2018-08-04 stsp
7191 ad80ab7b 2018-08-04 stsp static const struct got_error *
7192 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7193 ad80ab7b 2018-08-04 stsp {
7194 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7195 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7196 e5a0f69f 2018-08-18 stsp char *parent_path;
7197 ad80ab7b 2018-08-04 stsp
7198 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7199 e5a0f69f 2018-08-18 stsp if (err)
7200 e5a0f69f 2018-08-18 stsp return err;
7201 ffd1d5e5 2018-06-23 stsp
7202 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7203 e5a0f69f 2018-08-18 stsp free(parent_path);
7204 669b5ffa 2018-10-07 stsp
7205 a5d43cac 2022-07-01 thomas view_border(view);
7206 e5a0f69f 2018-08-18 stsp return err;
7207 07dd3ed3 2022-08-06 thomas }
7208 07dd3ed3 2022-08-06 thomas
7209 07dd3ed3 2022-08-06 thomas static const struct got_error *
7210 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7211 07dd3ed3 2022-08-06 thomas {
7212 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7213 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7214 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7215 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7216 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7217 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7218 07dd3ed3 2022-08-06 thomas
7219 07dd3ed3 2022-08-06 thomas g = view->gline;
7220 07dd3ed3 2022-08-06 thomas view->gline = 0;
7221 07dd3ed3 2022-08-06 thomas
7222 07dd3ed3 2022-08-06 thomas if (g == 0)
7223 07dd3ed3 2022-08-06 thomas g = 1;
7224 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7225 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7226 07dd3ed3 2022-08-06 thomas
7227 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7228 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7229 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7230 07dd3ed3 2022-08-06 thomas
7231 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7232 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7233 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7234 07dd3ed3 2022-08-06 thomas }
7235 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7236 07dd3ed3 2022-08-06 thomas last += off;
7237 07dd3ed3 2022-08-06 thomas
7238 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7239 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7240 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7241 07dd3ed3 2022-08-06 thomas }
7242 07dd3ed3 2022-08-06 thomas
7243 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7244 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7245 07dd3ed3 2022-08-06 thomas i += off;
7246 07dd3ed3 2022-08-06 thomas }
7247 07dd3ed3 2022-08-06 thomas
7248 07dd3ed3 2022-08-06 thomas if (i < g) {
7249 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7250 07dd3ed3 2022-08-06 thomas if (err)
7251 07dd3ed3 2022-08-06 thomas return err;
7252 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7253 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7254 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7255 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7256 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7257 07dd3ed3 2022-08-06 thomas first += off;
7258 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7259 07dd3ed3 2022-08-06 thomas }
7260 07dd3ed3 2022-08-06 thomas } else if (i > g)
7261 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7262 07dd3ed3 2022-08-06 thomas
7263 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7264 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7265 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7266 07dd3ed3 2022-08-06 thomas
7267 07dd3ed3 2022-08-06 thomas return NULL;
7268 e5a0f69f 2018-08-18 stsp }
7269 ce52c690 2018-06-23 stsp
7270 e5a0f69f 2018-08-18 stsp static const struct got_error *
7271 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7272 e5a0f69f 2018-08-18 stsp {
7273 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7274 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7275 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7276 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7277 ffd1d5e5 2018-06-23 stsp
7278 07dd3ed3 2022-08-06 thomas if (view->gline)
7279 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7280 07dd3ed3 2022-08-06 thomas
7281 e5a0f69f 2018-08-18 stsp switch (ch) {
7282 1e37a5c2 2019-05-12 jcs case 'i':
7283 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7284 07b0611c 2022-06-23 thomas view->count = 0;
7285 1e37a5c2 2019-05-12 jcs break;
7286 1be4947a 2022-07-22 thomas case 'L':
7287 07b0611c 2022-06-23 thomas view->count = 0;
7288 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7289 ffd1d5e5 2018-06-23 stsp break;
7290 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7291 152c1c93 2020-11-29 stsp break;
7292 1be4947a 2022-07-22 thomas case 'R':
7293 07b0611c 2022-06-23 thomas view->count = 0;
7294 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7295 e4526bf5 2021-09-03 naddy break;
7296 e4526bf5 2021-09-03 naddy case 'g':
7297 e4526bf5 2021-09-03 naddy case KEY_HOME:
7298 e4526bf5 2021-09-03 naddy s->selected = 0;
7299 07b0611c 2022-06-23 thomas view->count = 0;
7300 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7301 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7302 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7303 e4526bf5 2021-09-03 naddy else
7304 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7305 1e37a5c2 2019-05-12 jcs break;
7306 e4526bf5 2021-09-03 naddy case 'G':
7307 a5d43cac 2022-07-01 thomas case KEY_END: {
7308 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7309 a5d43cac 2022-07-01 thomas
7310 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7311 a5d43cac 2022-07-01 thomas --eos; /* border */
7312 e4526bf5 2021-09-03 naddy s->selected = 0;
7313 07b0611c 2022-06-23 thomas view->count = 0;
7314 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7315 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7316 e4526bf5 2021-09-03 naddy if (te == NULL) {
7317 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7318 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7319 e4526bf5 2021-09-03 naddy n++;
7320 e4526bf5 2021-09-03 naddy }
7321 e4526bf5 2021-09-03 naddy break;
7322 e4526bf5 2021-09-03 naddy }
7323 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7324 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7325 e4526bf5 2021-09-03 naddy }
7326 e4526bf5 2021-09-03 naddy if (n > 0)
7327 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7328 e4526bf5 2021-09-03 naddy break;
7329 a5d43cac 2022-07-01 thomas }
7330 1e37a5c2 2019-05-12 jcs case 'k':
7331 1e37a5c2 2019-05-12 jcs case KEY_UP:
7332 f7140bf5 2021-10-17 thomas case CTRL('p'):
7333 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7334 1e37a5c2 2019-05-12 jcs s->selected--;
7335 fa86c4bf 2020-11-29 stsp break;
7336 1e37a5c2 2019-05-12 jcs }
7337 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7338 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7339 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7340 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7341 07b0611c 2022-06-23 thomas view->count = 0;
7342 1e37a5c2 2019-05-12 jcs break;
7343 70f17a53 2022-06-13 thomas case CTRL('u'):
7344 23427b14 2022-06-23 thomas case 'u':
7345 70f17a53 2022-06-13 thomas nscroll /= 2;
7346 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7347 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7348 ea025d1d 2020-02-22 naddy case CTRL('b'):
7349 1c5e5faa 2022-06-23 thomas case 'b':
7350 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7351 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7352 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7353 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7354 fa86c4bf 2020-11-29 stsp } else {
7355 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7356 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7357 fa86c4bf 2020-11-29 stsp }
7358 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7359 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7360 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7361 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7362 07b0611c 2022-06-23 thomas view->count = 0;
7363 1e37a5c2 2019-05-12 jcs break;
7364 1e37a5c2 2019-05-12 jcs case 'j':
7365 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7366 f7140bf5 2021-10-17 thomas case CTRL('n'):
7367 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7368 1e37a5c2 2019-05-12 jcs s->selected++;
7369 1e37a5c2 2019-05-12 jcs break;
7370 1e37a5c2 2019-05-12 jcs }
7371 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7372 07b0611c 2022-06-23 thomas == NULL) {
7373 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7374 07b0611c 2022-06-23 thomas view->count = 0;
7375 1e37a5c2 2019-05-12 jcs break;
7376 07b0611c 2022-06-23 thomas }
7377 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7378 1e37a5c2 2019-05-12 jcs break;
7379 70f17a53 2022-06-13 thomas case CTRL('d'):
7380 23427b14 2022-06-23 thomas case 'd':
7381 70f17a53 2022-06-13 thomas nscroll /= 2;
7382 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7383 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7384 ea025d1d 2020-02-22 naddy case CTRL('f'):
7385 1c5e5faa 2022-06-23 thomas case 'f':
7386 4c2d69cb 2022-06-23 thomas case ' ':
7387 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7388 1e37a5c2 2019-05-12 jcs == NULL) {
7389 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7390 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7391 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7392 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7393 07b0611c 2022-06-23 thomas else
7394 07b0611c 2022-06-23 thomas view->count = 0;
7395 1e37a5c2 2019-05-12 jcs break;
7396 1e37a5c2 2019-05-12 jcs }
7397 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7398 1e37a5c2 2019-05-12 jcs break;
7399 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7400 1e37a5c2 2019-05-12 jcs case '\r':
7401 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7402 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7403 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7404 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7405 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7406 07b0611c 2022-06-23 thomas view->count = 0;
7407 1e37a5c2 2019-05-12 jcs break;
7408 07b0611c 2022-06-23 thomas }
7409 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7410 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7411 1e37a5c2 2019-05-12 jcs entry);
7412 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7413 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7414 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7415 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7416 1e37a5c2 2019-05-12 jcs s->selected_entry =
7417 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7418 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7419 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7420 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7421 a5d43cac 2022-07-01 thomas if (err)
7422 a5d43cac 2022-07-01 thomas break;
7423 a5d43cac 2022-07-01 thomas }
7424 1e37a5c2 2019-05-12 jcs free(parent);
7425 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7426 56e0773d 2019-11-28 stsp s->selected_entry))) {
7427 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7428 07b0611c 2022-06-23 thomas view->count = 0;
7429 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7430 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7431 1e37a5c2 2019-05-12 jcs if (err)
7432 1e37a5c2 2019-05-12 jcs break;
7433 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7434 941e9f74 2019-05-21 stsp if (err) {
7435 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7436 1e37a5c2 2019-05-12 jcs break;
7437 1e37a5c2 2019-05-12 jcs }
7438 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7439 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7440 1e37a5c2 2019-05-12 jcs break;
7441 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7442 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7443 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7444 07b0611c 2022-06-23 thomas view->count = 0;
7445 1e37a5c2 2019-05-12 jcs break;
7446 1e37a5c2 2019-05-12 jcs default:
7447 07b0611c 2022-06-23 thomas view->count = 0;
7448 1e37a5c2 2019-05-12 jcs break;
7449 ffd1d5e5 2018-06-23 stsp }
7450 e5a0f69f 2018-08-18 stsp
7451 ffd1d5e5 2018-06-23 stsp return err;
7452 9f7d7167 2018-04-29 stsp }
7453 9f7d7167 2018-04-29 stsp
7454 ffd1d5e5 2018-06-23 stsp __dead static void
7455 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7456 ffd1d5e5 2018-06-23 stsp {
7457 ffd1d5e5 2018-06-23 stsp endwin();
7458 91198554 2022-06-23 thomas fprintf(stderr,
7459 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7460 ffd1d5e5 2018-06-23 stsp getprogname());
7461 ffd1d5e5 2018-06-23 stsp exit(1);
7462 ffd1d5e5 2018-06-23 stsp }
7463 ffd1d5e5 2018-06-23 stsp
7464 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7465 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7466 ffd1d5e5 2018-06-23 stsp {
7467 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7468 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7469 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7470 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7471 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7472 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7473 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7474 4e97c21c 2020-12-06 stsp char *label = NULL;
7475 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7476 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7477 ffd1d5e5 2018-06-23 stsp int ch;
7478 5221c383 2018-08-01 stsp struct tog_view *view;
7479 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7480 70ac5f84 2019-03-28 stsp
7481 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7482 ffd1d5e5 2018-06-23 stsp switch (ch) {
7483 ffd1d5e5 2018-06-23 stsp case 'c':
7484 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7485 74283ab8 2020-02-07 stsp break;
7486 74283ab8 2020-02-07 stsp case 'r':
7487 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7488 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7489 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7490 74283ab8 2020-02-07 stsp optarg);
7491 ffd1d5e5 2018-06-23 stsp break;
7492 ffd1d5e5 2018-06-23 stsp default:
7493 e99e2d15 2020-11-24 naddy usage_tree();
7494 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7495 ffd1d5e5 2018-06-23 stsp }
7496 ffd1d5e5 2018-06-23 stsp }
7497 ffd1d5e5 2018-06-23 stsp
7498 ffd1d5e5 2018-06-23 stsp argc -= optind;
7499 ffd1d5e5 2018-06-23 stsp argv += optind;
7500 ffd1d5e5 2018-06-23 stsp
7501 55cccc34 2020-02-20 stsp if (argc > 1)
7502 e99e2d15 2020-11-24 naddy usage_tree();
7503 7cd52833 2022-06-23 thomas
7504 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7505 7cd52833 2022-06-23 thomas if (error != NULL)
7506 7cd52833 2022-06-23 thomas goto done;
7507 74283ab8 2020-02-07 stsp
7508 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7509 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7510 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7511 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7512 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7513 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7514 c156c7a4 2020-12-18 stsp goto done;
7515 55cccc34 2020-02-20 stsp if (worktree)
7516 52185f70 2019-02-05 stsp repo_path =
7517 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7518 55cccc34 2020-02-20 stsp else
7519 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7520 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7521 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7522 c156c7a4 2020-12-18 stsp goto done;
7523 c156c7a4 2020-12-18 stsp }
7524 55cccc34 2020-02-20 stsp }
7525 a915003a 2019-02-05 stsp
7526 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7527 c02c541e 2019-03-29 stsp if (error != NULL)
7528 52185f70 2019-02-05 stsp goto done;
7529 d188b9a6 2019-01-04 stsp
7530 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7531 55cccc34 2020-02-20 stsp repo, worktree);
7532 55cccc34 2020-02-20 stsp if (error)
7533 55cccc34 2020-02-20 stsp goto done;
7534 55cccc34 2020-02-20 stsp
7535 55cccc34 2020-02-20 stsp init_curses();
7536 55cccc34 2020-02-20 stsp
7537 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7538 c02c541e 2019-03-29 stsp if (error)
7539 52185f70 2019-02-05 stsp goto done;
7540 ffd1d5e5 2018-06-23 stsp
7541 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7542 51a10b52 2020-12-26 stsp if (error)
7543 51a10b52 2020-12-26 stsp goto done;
7544 51a10b52 2020-12-26 stsp
7545 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7546 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7547 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7548 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7549 4e97c21c 2020-12-06 stsp if (error)
7550 4e97c21c 2020-12-06 stsp goto done;
7551 4e97c21c 2020-12-06 stsp head_ref_name = label;
7552 4e97c21c 2020-12-06 stsp } else {
7553 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7554 4e97c21c 2020-12-06 stsp if (error == NULL)
7555 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7556 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7557 4e97c21c 2020-12-06 stsp goto done;
7558 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7559 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7560 4e97c21c 2020-12-06 stsp if (error)
7561 4e97c21c 2020-12-06 stsp goto done;
7562 4e97c21c 2020-12-06 stsp }
7563 ffd1d5e5 2018-06-23 stsp
7564 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7565 945f9229 2022-04-16 thomas if (error)
7566 945f9229 2022-04-16 thomas goto done;
7567 945f9229 2022-04-16 thomas
7568 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7569 5221c383 2018-08-01 stsp if (view == NULL) {
7570 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7571 5221c383 2018-08-01 stsp goto done;
7572 5221c383 2018-08-01 stsp }
7573 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7574 ad80ab7b 2018-08-04 stsp if (error)
7575 ad80ab7b 2018-08-04 stsp goto done;
7576 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7577 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7578 d91faf3b 2020-12-01 naddy in_repo_path);
7579 55cccc34 2020-02-20 stsp if (error)
7580 55cccc34 2020-02-20 stsp goto done;
7581 55cccc34 2020-02-20 stsp }
7582 55cccc34 2020-02-20 stsp
7583 55cccc34 2020-02-20 stsp if (worktree) {
7584 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7585 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7586 55cccc34 2020-02-20 stsp worktree = NULL;
7587 55cccc34 2020-02-20 stsp }
7588 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7589 ffd1d5e5 2018-06-23 stsp done:
7590 52185f70 2019-02-05 stsp free(repo_path);
7591 e4a0e26d 2020-02-20 stsp free(cwd);
7592 ffd1d5e5 2018-06-23 stsp free(commit_id);
7593 4e97c21c 2020-12-06 stsp free(label);
7594 486cd271 2020-12-06 stsp if (ref)
7595 486cd271 2020-12-06 stsp got_ref_close(ref);
7596 1d0f4054 2021-06-17 stsp if (repo) {
7597 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7598 1d0f4054 2021-06-17 stsp if (error == NULL)
7599 1d0f4054 2021-06-17 stsp error = close_err;
7600 1d0f4054 2021-06-17 stsp }
7601 7cd52833 2022-06-23 thomas if (pack_fds) {
7602 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7603 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7604 7cd52833 2022-06-23 thomas if (error == NULL)
7605 7cd52833 2022-06-23 thomas error = pack_err;
7606 7cd52833 2022-06-23 thomas }
7607 51a10b52 2020-12-26 stsp tog_free_refs();
7608 ffd1d5e5 2018-06-23 stsp return error;
7609 6458efa5 2020-11-24 stsp }
7610 6458efa5 2020-11-24 stsp
7611 6458efa5 2020-11-24 stsp static const struct got_error *
7612 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7613 6458efa5 2020-11-24 stsp {
7614 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7615 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7616 6458efa5 2020-11-24 stsp
7617 6458efa5 2020-11-24 stsp s->nrefs = 0;
7618 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7619 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7620 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7621 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7622 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7623 6458efa5 2020-11-24 stsp continue;
7624 6458efa5 2020-11-24 stsp
7625 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7626 6458efa5 2020-11-24 stsp if (re == NULL)
7627 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7628 6458efa5 2020-11-24 stsp
7629 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7630 8924d611 2020-12-26 stsp if (re->ref == NULL)
7631 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7632 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7633 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7634 6458efa5 2020-11-24 stsp }
7635 6458efa5 2020-11-24 stsp
7636 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7637 6458efa5 2020-11-24 stsp return NULL;
7638 6458efa5 2020-11-24 stsp }
7639 6458efa5 2020-11-24 stsp
7640 ef20f542 2022-06-26 thomas static void
7641 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7642 6458efa5 2020-11-24 stsp {
7643 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7644 6458efa5 2020-11-24 stsp
7645 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7646 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7647 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7648 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7649 6458efa5 2020-11-24 stsp free(re);
7650 6458efa5 2020-11-24 stsp }
7651 6458efa5 2020-11-24 stsp }
7652 6458efa5 2020-11-24 stsp
7653 6458efa5 2020-11-24 stsp static const struct got_error *
7654 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7655 6458efa5 2020-11-24 stsp {
7656 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7657 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7658 6458efa5 2020-11-24 stsp
7659 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7660 6458efa5 2020-11-24 stsp s->repo = repo;
7661 6458efa5 2020-11-24 stsp
7662 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7663 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7664 6458efa5 2020-11-24 stsp
7665 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7666 6458efa5 2020-11-24 stsp if (err)
7667 6458efa5 2020-11-24 stsp return err;
7668 34ba6917 2020-11-30 stsp
7669 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7670 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7671 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7672 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7673 6458efa5 2020-11-24 stsp if (err)
7674 6458efa5 2020-11-24 stsp goto done;
7675 6458efa5 2020-11-24 stsp
7676 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7677 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7678 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7679 6458efa5 2020-11-24 stsp if (err)
7680 6458efa5 2020-11-24 stsp goto done;
7681 6458efa5 2020-11-24 stsp
7682 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7683 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7684 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7685 2183bbf6 2022-01-23 thomas if (err)
7686 2183bbf6 2022-01-23 thomas goto done;
7687 2183bbf6 2022-01-23 thomas
7688 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7689 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7690 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7691 6458efa5 2020-11-24 stsp if (err)
7692 6458efa5 2020-11-24 stsp goto done;
7693 6458efa5 2020-11-24 stsp }
7694 6458efa5 2020-11-24 stsp
7695 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7696 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7697 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7698 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7699 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7700 6458efa5 2020-11-24 stsp done:
7701 6458efa5 2020-11-24 stsp if (err)
7702 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7703 6458efa5 2020-11-24 stsp return err;
7704 6458efa5 2020-11-24 stsp }
7705 6458efa5 2020-11-24 stsp
7706 6458efa5 2020-11-24 stsp static const struct got_error *
7707 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7708 6458efa5 2020-11-24 stsp {
7709 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7710 6458efa5 2020-11-24 stsp
7711 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7712 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7713 6458efa5 2020-11-24 stsp
7714 6458efa5 2020-11-24 stsp return NULL;
7715 9f7d7167 2018-04-29 stsp }
7716 ce5b7c56 2019-07-09 stsp
7717 6458efa5 2020-11-24 stsp static const struct got_error *
7718 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7719 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7720 6458efa5 2020-11-24 stsp {
7721 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7722 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7723 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7724 6458efa5 2020-11-24 stsp int obj_type;
7725 6458efa5 2020-11-24 stsp
7726 c42c9805 2020-11-24 stsp *commit_id = NULL;
7727 6458efa5 2020-11-24 stsp
7728 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7729 6458efa5 2020-11-24 stsp if (err)
7730 6458efa5 2020-11-24 stsp return err;
7731 6458efa5 2020-11-24 stsp
7732 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7733 6458efa5 2020-11-24 stsp if (err)
7734 6458efa5 2020-11-24 stsp goto done;
7735 6458efa5 2020-11-24 stsp
7736 6458efa5 2020-11-24 stsp switch (obj_type) {
7737 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7738 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7739 6458efa5 2020-11-24 stsp break;
7740 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7741 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7742 6458efa5 2020-11-24 stsp if (err)
7743 6458efa5 2020-11-24 stsp goto done;
7744 c42c9805 2020-11-24 stsp free(obj_id);
7745 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7746 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7747 c42c9805 2020-11-24 stsp if (err)
7748 6458efa5 2020-11-24 stsp goto done;
7749 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7750 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7751 c42c9805 2020-11-24 stsp goto done;
7752 c42c9805 2020-11-24 stsp }
7753 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7754 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7755 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7756 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7757 c42c9805 2020-11-24 stsp goto done;
7758 c42c9805 2020-11-24 stsp }
7759 6458efa5 2020-11-24 stsp break;
7760 6458efa5 2020-11-24 stsp default:
7761 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7762 c42c9805 2020-11-24 stsp break;
7763 6458efa5 2020-11-24 stsp }
7764 6458efa5 2020-11-24 stsp
7765 c42c9805 2020-11-24 stsp done:
7766 c42c9805 2020-11-24 stsp if (tag)
7767 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7768 c42c9805 2020-11-24 stsp if (err) {
7769 c42c9805 2020-11-24 stsp free(*commit_id);
7770 c42c9805 2020-11-24 stsp *commit_id = NULL;
7771 c42c9805 2020-11-24 stsp }
7772 c42c9805 2020-11-24 stsp return err;
7773 c42c9805 2020-11-24 stsp }
7774 c42c9805 2020-11-24 stsp
7775 c42c9805 2020-11-24 stsp static const struct got_error *
7776 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7777 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7778 c42c9805 2020-11-24 stsp {
7779 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7780 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7781 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7782 c42c9805 2020-11-24 stsp
7783 c42c9805 2020-11-24 stsp *new_view = NULL;
7784 c42c9805 2020-11-24 stsp
7785 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7786 c42c9805 2020-11-24 stsp if (err) {
7787 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7788 c42c9805 2020-11-24 stsp return err;
7789 c42c9805 2020-11-24 stsp else
7790 c42c9805 2020-11-24 stsp return NULL;
7791 c42c9805 2020-11-24 stsp }
7792 c42c9805 2020-11-24 stsp
7793 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7794 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7795 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7796 6458efa5 2020-11-24 stsp goto done;
7797 6458efa5 2020-11-24 stsp }
7798 6458efa5 2020-11-24 stsp
7799 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7800 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7801 6458efa5 2020-11-24 stsp done:
7802 6458efa5 2020-11-24 stsp if (err)
7803 6458efa5 2020-11-24 stsp view_close(log_view);
7804 6458efa5 2020-11-24 stsp else
7805 6458efa5 2020-11-24 stsp *new_view = log_view;
7806 c42c9805 2020-11-24 stsp free(commit_id);
7807 6458efa5 2020-11-24 stsp return err;
7808 6458efa5 2020-11-24 stsp }
7809 6458efa5 2020-11-24 stsp
7810 ce5b7c56 2019-07-09 stsp static void
7811 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7812 6458efa5 2020-11-24 stsp {
7813 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7814 34ba6917 2020-11-30 stsp int i = 0;
7815 6458efa5 2020-11-24 stsp
7816 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7817 6458efa5 2020-11-24 stsp return;
7818 6458efa5 2020-11-24 stsp
7819 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7820 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7821 34ba6917 2020-11-30 stsp if (re == NULL)
7822 34ba6917 2020-11-30 stsp break;
7823 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7824 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7825 6458efa5 2020-11-24 stsp }
7826 6458efa5 2020-11-24 stsp }
7827 6458efa5 2020-11-24 stsp
7828 a5d43cac 2022-07-01 thomas static const struct got_error *
7829 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7830 6458efa5 2020-11-24 stsp {
7831 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7832 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7833 6458efa5 2020-11-24 stsp int n = 0;
7834 6458efa5 2020-11-24 stsp
7835 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7836 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7837 6458efa5 2020-11-24 stsp else
7838 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7839 6458efa5 2020-11-24 stsp
7840 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7841 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7842 07dd3ed3 2022-08-06 thomas if (last) {
7843 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7844 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7845 07dd3ed3 2022-08-06 thomas }
7846 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7847 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7848 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7849 6458efa5 2020-11-24 stsp }
7850 6458efa5 2020-11-24 stsp }
7851 a5d43cac 2022-07-01 thomas
7852 a5d43cac 2022-07-01 thomas return NULL;
7853 6458efa5 2020-11-24 stsp }
7854 6458efa5 2020-11-24 stsp
7855 6458efa5 2020-11-24 stsp static const struct got_error *
7856 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7857 6458efa5 2020-11-24 stsp {
7858 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7859 6458efa5 2020-11-24 stsp
7860 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7861 6458efa5 2020-11-24 stsp return NULL;
7862 6458efa5 2020-11-24 stsp }
7863 6458efa5 2020-11-24 stsp
7864 6458efa5 2020-11-24 stsp static int
7865 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7866 6458efa5 2020-11-24 stsp {
7867 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7868 6458efa5 2020-11-24 stsp
7869 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7870 6458efa5 2020-11-24 stsp 0) == 0;
7871 6458efa5 2020-11-24 stsp }
7872 6458efa5 2020-11-24 stsp
7873 6458efa5 2020-11-24 stsp static const struct got_error *
7874 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7875 6458efa5 2020-11-24 stsp {
7876 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7877 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7878 6458efa5 2020-11-24 stsp
7879 6458efa5 2020-11-24 stsp if (!view->searching) {
7880 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7881 6458efa5 2020-11-24 stsp return NULL;
7882 6458efa5 2020-11-24 stsp }
7883 6458efa5 2020-11-24 stsp
7884 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7885 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7886 6458efa5 2020-11-24 stsp if (s->selected_entry)
7887 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7888 6458efa5 2020-11-24 stsp else
7889 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7890 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7891 6458efa5 2020-11-24 stsp } else {
7892 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7893 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7894 6458efa5 2020-11-24 stsp else
7895 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7896 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7897 6458efa5 2020-11-24 stsp }
7898 6458efa5 2020-11-24 stsp } else {
7899 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7900 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7901 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7902 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7903 6458efa5 2020-11-24 stsp else
7904 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7905 6458efa5 2020-11-24 stsp }
7906 6458efa5 2020-11-24 stsp
7907 6458efa5 2020-11-24 stsp while (1) {
7908 6458efa5 2020-11-24 stsp if (re == NULL) {
7909 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7910 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7911 6458efa5 2020-11-24 stsp return NULL;
7912 6458efa5 2020-11-24 stsp }
7913 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7914 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7915 6458efa5 2020-11-24 stsp else
7916 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7917 6458efa5 2020-11-24 stsp }
7918 6458efa5 2020-11-24 stsp
7919 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7920 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7921 6458efa5 2020-11-24 stsp s->matched_entry = re;
7922 6458efa5 2020-11-24 stsp break;
7923 6458efa5 2020-11-24 stsp }
7924 6458efa5 2020-11-24 stsp
7925 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7926 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7927 6458efa5 2020-11-24 stsp else
7928 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7929 6458efa5 2020-11-24 stsp }
7930 6458efa5 2020-11-24 stsp
7931 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7932 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7933 6458efa5 2020-11-24 stsp s->selected = 0;
7934 6458efa5 2020-11-24 stsp }
7935 6458efa5 2020-11-24 stsp
7936 6458efa5 2020-11-24 stsp return NULL;
7937 6458efa5 2020-11-24 stsp }
7938 6458efa5 2020-11-24 stsp
7939 6458efa5 2020-11-24 stsp static const struct got_error *
7940 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7941 6458efa5 2020-11-24 stsp {
7942 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7943 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7944 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7945 6458efa5 2020-11-24 stsp char *line = NULL;
7946 6458efa5 2020-11-24 stsp wchar_t *wline;
7947 6458efa5 2020-11-24 stsp struct tog_color *tc;
7948 6458efa5 2020-11-24 stsp int width, n;
7949 6458efa5 2020-11-24 stsp int limit = view->nlines;
7950 6458efa5 2020-11-24 stsp
7951 6458efa5 2020-11-24 stsp werase(view->window);
7952 6458efa5 2020-11-24 stsp
7953 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7954 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7955 a5d43cac 2022-07-01 thomas --limit; /* border */
7956 6458efa5 2020-11-24 stsp
7957 6458efa5 2020-11-24 stsp if (limit == 0)
7958 6458efa5 2020-11-24 stsp return NULL;
7959 6458efa5 2020-11-24 stsp
7960 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7961 6458efa5 2020-11-24 stsp
7962 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7963 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7964 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7965 6458efa5 2020-11-24 stsp
7966 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7967 6458efa5 2020-11-24 stsp if (err) {
7968 6458efa5 2020-11-24 stsp free(line);
7969 6458efa5 2020-11-24 stsp return err;
7970 6458efa5 2020-11-24 stsp }
7971 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7972 6458efa5 2020-11-24 stsp wstandout(view->window);
7973 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7974 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7975 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7976 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7977 6458efa5 2020-11-24 stsp wstandend(view->window);
7978 6458efa5 2020-11-24 stsp free(wline);
7979 6458efa5 2020-11-24 stsp wline = NULL;
7980 6458efa5 2020-11-24 stsp free(line);
7981 6458efa5 2020-11-24 stsp line = NULL;
7982 6458efa5 2020-11-24 stsp if (--limit <= 0)
7983 6458efa5 2020-11-24 stsp return NULL;
7984 6458efa5 2020-11-24 stsp
7985 6458efa5 2020-11-24 stsp n = 0;
7986 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7987 6458efa5 2020-11-24 stsp char *line = NULL;
7988 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7989 6458efa5 2020-11-24 stsp
7990 84227eb1 2022-06-23 thomas if (s->show_date) {
7991 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7992 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7993 84227eb1 2022-06-23 thomas struct got_object_id *id;
7994 84227eb1 2022-06-23 thomas struct tm tm;
7995 84227eb1 2022-06-23 thomas time_t t;
7996 84227eb1 2022-06-23 thomas
7997 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7998 84227eb1 2022-06-23 thomas if (err)
7999 84227eb1 2022-06-23 thomas return err;
8000 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8001 84227eb1 2022-06-23 thomas if (err) {
8002 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8003 84227eb1 2022-06-23 thomas free(id);
8004 84227eb1 2022-06-23 thomas return err;
8005 84227eb1 2022-06-23 thomas }
8006 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8007 84227eb1 2022-06-23 thomas id);
8008 84227eb1 2022-06-23 thomas if (err) {
8009 84227eb1 2022-06-23 thomas free(id);
8010 84227eb1 2022-06-23 thomas return err;
8011 84227eb1 2022-06-23 thomas }
8012 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8013 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8014 84227eb1 2022-06-23 thomas } else {
8015 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8016 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8017 84227eb1 2022-06-23 thomas }
8018 84227eb1 2022-06-23 thomas free(id);
8019 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8020 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8021 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8022 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8023 84227eb1 2022-06-23 thomas }
8024 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8025 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8026 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8027 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8028 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8029 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8030 6458efa5 2020-11-24 stsp struct got_object_id *id;
8031 6458efa5 2020-11-24 stsp char *id_str;
8032 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8033 6458efa5 2020-11-24 stsp if (err)
8034 6458efa5 2020-11-24 stsp return err;
8035 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8036 6458efa5 2020-11-24 stsp if (err) {
8037 6458efa5 2020-11-24 stsp free(id);
8038 6458efa5 2020-11-24 stsp return err;
8039 6458efa5 2020-11-24 stsp }
8040 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8041 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8042 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8043 6458efa5 2020-11-24 stsp free(id);
8044 6458efa5 2020-11-24 stsp free(id_str);
8045 6458efa5 2020-11-24 stsp return err;
8046 6458efa5 2020-11-24 stsp }
8047 6458efa5 2020-11-24 stsp free(id);
8048 6458efa5 2020-11-24 stsp free(id_str);
8049 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8050 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8051 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8052 6458efa5 2020-11-24 stsp
8053 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8054 f91a2b48 2022-06-23 thomas 0, 0);
8055 6458efa5 2020-11-24 stsp if (err) {
8056 6458efa5 2020-11-24 stsp free(line);
8057 6458efa5 2020-11-24 stsp return err;
8058 6458efa5 2020-11-24 stsp }
8059 6458efa5 2020-11-24 stsp if (n == s->selected) {
8060 6458efa5 2020-11-24 stsp if (view->focussed)
8061 6458efa5 2020-11-24 stsp wstandout(view->window);
8062 6458efa5 2020-11-24 stsp s->selected_entry = re;
8063 6458efa5 2020-11-24 stsp }
8064 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8065 6458efa5 2020-11-24 stsp if (tc)
8066 6458efa5 2020-11-24 stsp wattr_on(view->window,
8067 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8068 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8069 6458efa5 2020-11-24 stsp if (tc)
8070 6458efa5 2020-11-24 stsp wattr_off(view->window,
8071 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8072 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8073 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8074 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8075 6458efa5 2020-11-24 stsp wstandend(view->window);
8076 6458efa5 2020-11-24 stsp free(line);
8077 6458efa5 2020-11-24 stsp free(wline);
8078 6458efa5 2020-11-24 stsp wline = NULL;
8079 6458efa5 2020-11-24 stsp n++;
8080 6458efa5 2020-11-24 stsp s->ndisplayed++;
8081 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8082 6458efa5 2020-11-24 stsp
8083 6458efa5 2020-11-24 stsp limit--;
8084 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8085 6458efa5 2020-11-24 stsp }
8086 6458efa5 2020-11-24 stsp
8087 a5d43cac 2022-07-01 thomas view_border(view);
8088 6458efa5 2020-11-24 stsp return err;
8089 6458efa5 2020-11-24 stsp }
8090 6458efa5 2020-11-24 stsp
8091 6458efa5 2020-11-24 stsp static const struct got_error *
8092 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8093 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8094 c42c9805 2020-11-24 stsp {
8095 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8096 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8097 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8098 c42c9805 2020-11-24 stsp
8099 c42c9805 2020-11-24 stsp *new_view = NULL;
8100 c42c9805 2020-11-24 stsp
8101 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8102 c42c9805 2020-11-24 stsp if (err) {
8103 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8104 c42c9805 2020-11-24 stsp return err;
8105 c42c9805 2020-11-24 stsp else
8106 c42c9805 2020-11-24 stsp return NULL;
8107 c42c9805 2020-11-24 stsp }
8108 c42c9805 2020-11-24 stsp
8109 c42c9805 2020-11-24 stsp
8110 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8111 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8112 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8113 c42c9805 2020-11-24 stsp goto done;
8114 c42c9805 2020-11-24 stsp }
8115 c42c9805 2020-11-24 stsp
8116 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8117 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8118 c42c9805 2020-11-24 stsp if (err)
8119 c42c9805 2020-11-24 stsp goto done;
8120 c42c9805 2020-11-24 stsp
8121 c42c9805 2020-11-24 stsp *new_view = tree_view;
8122 c42c9805 2020-11-24 stsp done:
8123 c42c9805 2020-11-24 stsp free(commit_id);
8124 c42c9805 2020-11-24 stsp return err;
8125 07dd3ed3 2022-08-06 thomas }
8126 07dd3ed3 2022-08-06 thomas
8127 07dd3ed3 2022-08-06 thomas static const struct got_error *
8128 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8129 07dd3ed3 2022-08-06 thomas {
8130 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8131 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8132 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8133 07dd3ed3 2022-08-06 thomas
8134 07dd3ed3 2022-08-06 thomas g = view->gline;
8135 07dd3ed3 2022-08-06 thomas view->gline = 0;
8136 07dd3ed3 2022-08-06 thomas
8137 07dd3ed3 2022-08-06 thomas if (g == 0)
8138 07dd3ed3 2022-08-06 thomas g = 1;
8139 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8140 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8141 07dd3ed3 2022-08-06 thomas
8142 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8143 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8144 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8145 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8146 07dd3ed3 2022-08-06 thomas return NULL;
8147 07dd3ed3 2022-08-06 thomas }
8148 07dd3ed3 2022-08-06 thomas
8149 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8150 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8151 07dd3ed3 2022-08-06 thomas if (err)
8152 07dd3ed3 2022-08-06 thomas return err;
8153 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8154 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8155 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8156 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8157 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8158 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8159 07dd3ed3 2022-08-06 thomas
8160 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8161 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8162 07dd3ed3 2022-08-06 thomas
8163 07dd3ed3 2022-08-06 thomas return NULL;
8164 07dd3ed3 2022-08-06 thomas
8165 c42c9805 2020-11-24 stsp }
8166 07dd3ed3 2022-08-06 thomas
8167 c42c9805 2020-11-24 stsp static const struct got_error *
8168 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8169 6458efa5 2020-11-24 stsp {
8170 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8171 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8172 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8173 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8174 6458efa5 2020-11-24 stsp
8175 07dd3ed3 2022-08-06 thomas if (view->gline)
8176 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8177 07dd3ed3 2022-08-06 thomas
8178 6458efa5 2020-11-24 stsp switch (ch) {
8179 6458efa5 2020-11-24 stsp case 'i':
8180 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8181 07b0611c 2022-06-23 thomas view->count = 0;
8182 3bfadbd4 2021-11-20 thomas break;
8183 84227eb1 2022-06-23 thomas case 'm':
8184 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8185 07b0611c 2022-06-23 thomas view->count = 0;
8186 84227eb1 2022-06-23 thomas break;
8187 98182bd0 2021-11-20 thomas case 'o':
8188 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8189 07b0611c 2022-06-23 thomas view->count = 0;
8190 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8191 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8192 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8193 c591440f 2021-11-20 thomas if (err)
8194 c591440f 2021-11-20 thomas break;
8195 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8196 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8197 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8198 3bfadbd4 2021-11-20 thomas if (err)
8199 3bfadbd4 2021-11-20 thomas break;
8200 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8201 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8202 6458efa5 2020-11-24 stsp break;
8203 6458efa5 2020-11-24 stsp case KEY_ENTER:
8204 6458efa5 2020-11-24 stsp case '\r':
8205 07b0611c 2022-06-23 thomas view->count = 0;
8206 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8207 a5d43cac 2022-07-01 thomas break;
8208 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8209 6458efa5 2020-11-24 stsp break;
8210 1be4947a 2022-07-22 thomas case 'T':
8211 07b0611c 2022-06-23 thomas view->count = 0;
8212 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8213 c42c9805 2020-11-24 stsp break;
8214 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8215 c42c9805 2020-11-24 stsp break;
8216 e4526bf5 2021-09-03 naddy case 'g':
8217 e4526bf5 2021-09-03 naddy case KEY_HOME:
8218 e4526bf5 2021-09-03 naddy s->selected = 0;
8219 07b0611c 2022-06-23 thomas view->count = 0;
8220 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8221 e4526bf5 2021-09-03 naddy break;
8222 e4526bf5 2021-09-03 naddy case 'G':
8223 a5d43cac 2022-07-01 thomas case KEY_END: {
8224 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8225 a5d43cac 2022-07-01 thomas
8226 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8227 a5d43cac 2022-07-01 thomas --eos; /* border */
8228 e4526bf5 2021-09-03 naddy s->selected = 0;
8229 07b0611c 2022-06-23 thomas view->count = 0;
8230 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8231 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8232 e4526bf5 2021-09-03 naddy if (re == NULL)
8233 e4526bf5 2021-09-03 naddy break;
8234 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8235 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8236 e4526bf5 2021-09-03 naddy }
8237 e4526bf5 2021-09-03 naddy if (n > 0)
8238 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8239 e4526bf5 2021-09-03 naddy break;
8240 a5d43cac 2022-07-01 thomas }
8241 6458efa5 2020-11-24 stsp case 'k':
8242 6458efa5 2020-11-24 stsp case KEY_UP:
8243 f7140bf5 2021-10-17 thomas case CTRL('p'):
8244 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8245 6458efa5 2020-11-24 stsp s->selected--;
8246 6458efa5 2020-11-24 stsp break;
8247 34ba6917 2020-11-30 stsp }
8248 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8249 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8250 07b0611c 2022-06-23 thomas view->count = 0;
8251 6458efa5 2020-11-24 stsp break;
8252 70f17a53 2022-06-13 thomas case CTRL('u'):
8253 23427b14 2022-06-23 thomas case 'u':
8254 70f17a53 2022-06-13 thomas nscroll /= 2;
8255 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8256 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8257 6458efa5 2020-11-24 stsp case CTRL('b'):
8258 1c5e5faa 2022-06-23 thomas case 'b':
8259 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8260 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8261 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8262 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8263 07b0611c 2022-06-23 thomas view->count = 0;
8264 6458efa5 2020-11-24 stsp break;
8265 6458efa5 2020-11-24 stsp case 'j':
8266 6458efa5 2020-11-24 stsp case KEY_DOWN:
8267 f7140bf5 2021-10-17 thomas case CTRL('n'):
8268 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8269 6458efa5 2020-11-24 stsp s->selected++;
8270 6458efa5 2020-11-24 stsp break;
8271 6458efa5 2020-11-24 stsp }
8272 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8273 6458efa5 2020-11-24 stsp /* can't scroll any further */
8274 07b0611c 2022-06-23 thomas view->count = 0;
8275 6458efa5 2020-11-24 stsp break;
8276 07b0611c 2022-06-23 thomas }
8277 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8278 6458efa5 2020-11-24 stsp break;
8279 70f17a53 2022-06-13 thomas case CTRL('d'):
8280 23427b14 2022-06-23 thomas case 'd':
8281 70f17a53 2022-06-13 thomas nscroll /= 2;
8282 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8283 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8284 6458efa5 2020-11-24 stsp case CTRL('f'):
8285 1c5e5faa 2022-06-23 thomas case 'f':
8286 4c2d69cb 2022-06-23 thomas case ' ':
8287 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8288 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8289 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8290 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8291 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8292 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8293 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8294 07b0611c 2022-06-23 thomas view->count = 0;
8295 6458efa5 2020-11-24 stsp break;
8296 6458efa5 2020-11-24 stsp }
8297 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8298 6458efa5 2020-11-24 stsp break;
8299 6458efa5 2020-11-24 stsp case CTRL('l'):
8300 07b0611c 2022-06-23 thomas view->count = 0;
8301 8924d611 2020-12-26 stsp tog_free_refs();
8302 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8303 8924d611 2020-12-26 stsp if (err)
8304 8924d611 2020-12-26 stsp break;
8305 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8306 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8307 6458efa5 2020-11-24 stsp break;
8308 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8309 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8310 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8311 6458efa5 2020-11-24 stsp break;
8312 6458efa5 2020-11-24 stsp default:
8313 07b0611c 2022-06-23 thomas view->count = 0;
8314 6458efa5 2020-11-24 stsp break;
8315 6458efa5 2020-11-24 stsp }
8316 6458efa5 2020-11-24 stsp
8317 6458efa5 2020-11-24 stsp return err;
8318 6458efa5 2020-11-24 stsp }
8319 6458efa5 2020-11-24 stsp
8320 6458efa5 2020-11-24 stsp __dead static void
8321 6458efa5 2020-11-24 stsp usage_ref(void)
8322 6458efa5 2020-11-24 stsp {
8323 6458efa5 2020-11-24 stsp endwin();
8324 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8325 6458efa5 2020-11-24 stsp getprogname());
8326 6458efa5 2020-11-24 stsp exit(1);
8327 6458efa5 2020-11-24 stsp }
8328 6458efa5 2020-11-24 stsp
8329 6458efa5 2020-11-24 stsp static const struct got_error *
8330 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8331 6458efa5 2020-11-24 stsp {
8332 6458efa5 2020-11-24 stsp const struct got_error *error;
8333 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8334 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8335 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8336 6458efa5 2020-11-24 stsp int ch;
8337 6458efa5 2020-11-24 stsp struct tog_view *view;
8338 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8339 6458efa5 2020-11-24 stsp
8340 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8341 6458efa5 2020-11-24 stsp switch (ch) {
8342 6458efa5 2020-11-24 stsp case 'r':
8343 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8344 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8345 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8346 6458efa5 2020-11-24 stsp optarg);
8347 6458efa5 2020-11-24 stsp break;
8348 6458efa5 2020-11-24 stsp default:
8349 e99e2d15 2020-11-24 naddy usage_ref();
8350 6458efa5 2020-11-24 stsp /* NOTREACHED */
8351 6458efa5 2020-11-24 stsp }
8352 6458efa5 2020-11-24 stsp }
8353 6458efa5 2020-11-24 stsp
8354 6458efa5 2020-11-24 stsp argc -= optind;
8355 6458efa5 2020-11-24 stsp argv += optind;
8356 6458efa5 2020-11-24 stsp
8357 6458efa5 2020-11-24 stsp if (argc > 1)
8358 e99e2d15 2020-11-24 naddy usage_ref();
8359 7cd52833 2022-06-23 thomas
8360 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8361 7cd52833 2022-06-23 thomas if (error != NULL)
8362 7cd52833 2022-06-23 thomas goto done;
8363 6458efa5 2020-11-24 stsp
8364 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8365 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8366 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8367 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8368 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8369 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8370 c156c7a4 2020-12-18 stsp goto done;
8371 6458efa5 2020-11-24 stsp if (worktree)
8372 6458efa5 2020-11-24 stsp repo_path =
8373 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8374 6458efa5 2020-11-24 stsp else
8375 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8376 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8377 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8378 c156c7a4 2020-12-18 stsp goto done;
8379 c156c7a4 2020-12-18 stsp }
8380 6458efa5 2020-11-24 stsp }
8381 6458efa5 2020-11-24 stsp
8382 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8383 6458efa5 2020-11-24 stsp if (error != NULL)
8384 6458efa5 2020-11-24 stsp goto done;
8385 6458efa5 2020-11-24 stsp
8386 6458efa5 2020-11-24 stsp init_curses();
8387 6458efa5 2020-11-24 stsp
8388 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8389 51a10b52 2020-12-26 stsp if (error)
8390 51a10b52 2020-12-26 stsp goto done;
8391 51a10b52 2020-12-26 stsp
8392 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8393 6458efa5 2020-11-24 stsp if (error)
8394 6458efa5 2020-11-24 stsp goto done;
8395 6458efa5 2020-11-24 stsp
8396 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8397 6458efa5 2020-11-24 stsp if (view == NULL) {
8398 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8399 6458efa5 2020-11-24 stsp goto done;
8400 6458efa5 2020-11-24 stsp }
8401 6458efa5 2020-11-24 stsp
8402 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8403 6458efa5 2020-11-24 stsp if (error)
8404 6458efa5 2020-11-24 stsp goto done;
8405 6458efa5 2020-11-24 stsp
8406 6458efa5 2020-11-24 stsp if (worktree) {
8407 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8408 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8409 6458efa5 2020-11-24 stsp worktree = NULL;
8410 6458efa5 2020-11-24 stsp }
8411 6458efa5 2020-11-24 stsp error = view_loop(view);
8412 6458efa5 2020-11-24 stsp done:
8413 6458efa5 2020-11-24 stsp free(repo_path);
8414 6458efa5 2020-11-24 stsp free(cwd);
8415 1d0f4054 2021-06-17 stsp if (repo) {
8416 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8417 1d0f4054 2021-06-17 stsp if (close_err)
8418 1d0f4054 2021-06-17 stsp error = close_err;
8419 1d0f4054 2021-06-17 stsp }
8420 7cd52833 2022-06-23 thomas if (pack_fds) {
8421 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8422 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8423 7cd52833 2022-06-23 thomas if (error == NULL)
8424 7cd52833 2022-06-23 thomas error = pack_err;
8425 7cd52833 2022-06-23 thomas }
8426 51a10b52 2020-12-26 stsp tog_free_refs();
8427 6458efa5 2020-11-24 stsp return error;
8428 6458efa5 2020-11-24 stsp }
8429 6458efa5 2020-11-24 stsp
8430 fc2737d5 2022-09-15 thomas static const struct got_error*
8431 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8432 fc2737d5 2022-09-15 thomas const char *str)
8433 fc2737d5 2022-09-15 thomas {
8434 fc2737d5 2022-09-15 thomas size_t len;
8435 fc2737d5 2022-09-15 thomas
8436 fc2737d5 2022-09-15 thomas if (win == NULL)
8437 fc2737d5 2022-09-15 thomas win = stdscr;
8438 fc2737d5 2022-09-15 thomas
8439 fc2737d5 2022-09-15 thomas len = strlen(str);
8440 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8441 fc2737d5 2022-09-15 thomas
8442 fc2737d5 2022-09-15 thomas if (focus)
8443 fc2737d5 2022-09-15 thomas wstandout(win);
8444 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8445 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8446 fc2737d5 2022-09-15 thomas if (focus)
8447 fc2737d5 2022-09-15 thomas wstandend(win);
8448 fc2737d5 2022-09-15 thomas
8449 fc2737d5 2022-09-15 thomas return NULL;
8450 fc2737d5 2022-09-15 thomas }
8451 fc2737d5 2022-09-15 thomas
8452 2a31b33b 2022-07-23 thomas static const struct got_error *
8453 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8454 fc2737d5 2022-09-15 thomas {
8455 fc2737d5 2022-09-15 thomas off_t *p;
8456 fc2737d5 2022-09-15 thomas
8457 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8458 fc2737d5 2022-09-15 thomas if (p == NULL) {
8459 fc2737d5 2022-09-15 thomas free(*line_offsets);
8460 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8461 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8462 fc2737d5 2022-09-15 thomas }
8463 fc2737d5 2022-09-15 thomas
8464 fc2737d5 2022-09-15 thomas *line_offsets = p;
8465 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8466 fc2737d5 2022-09-15 thomas ++(*nlines);
8467 fc2737d5 2022-09-15 thomas return NULL;
8468 fc2737d5 2022-09-15 thomas }
8469 fc2737d5 2022-09-15 thomas
8470 fc2737d5 2022-09-15 thomas static const struct got_error *
8471 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8472 fc2737d5 2022-09-15 thomas {
8473 fc2737d5 2022-09-15 thomas *ret = 0;
8474 fc2737d5 2022-09-15 thomas
8475 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8476 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8477 fc2737d5 2022-09-15 thomas size_t len = 1;
8478 fc2737d5 2022-09-15 thomas
8479 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8480 fc2737d5 2022-09-15 thomas continue;
8481 fc2737d5 2022-09-15 thomas
8482 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8483 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8484 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8485 fc2737d5 2022-09-15 thomas
8486 fc2737d5 2022-09-15 thomas len += strlen(t);
8487 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8488 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8489 fc2737d5 2022-09-15 thomas free(t0);
8490 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8491 fc2737d5 2022-09-15 thomas }
8492 fc2737d5 2022-09-15 thomas
8493 fc2737d5 2022-09-15 thomas return NULL;
8494 fc2737d5 2022-09-15 thomas }
8495 fc2737d5 2022-09-15 thomas
8496 fc2737d5 2022-09-15 thomas /*
8497 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8498 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8499 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8500 fc2737d5 2022-09-15 thomas */
8501 fc2737d5 2022-09-15 thomas static const struct got_error *
8502 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8503 fc2737d5 2022-09-15 thomas {
8504 fc2737d5 2022-09-15 thomas int n, len = width;
8505 fc2737d5 2022-09-15 thomas
8506 fc2737d5 2022-09-15 thomas if (km->keys) {
8507 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8508 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8509 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8510 30e77f6a 2022-09-18 thomas };
8511 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8512 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8513 fc2737d5 2022-09-15 thomas
8514 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8515 fc2737d5 2022-09-15 thomas
8516 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8517 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8518 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8519 fc2737d5 2022-09-15 thomas
8520 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8521 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8522 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8523 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8524 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8525 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8526 fc2737d5 2022-09-15 thomas if (n < 0) {
8527 fc2737d5 2022-09-15 thomas free(t0);
8528 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8529 fc2737d5 2022-09-15 thomas }
8530 fc2737d5 2022-09-15 thomas first = 0;
8531 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8532 fc2737d5 2022-09-15 thomas *off += n;
8533 fc2737d5 2022-09-15 thomas }
8534 fc2737d5 2022-09-15 thomas free(t0);
8535 fc2737d5 2022-09-15 thomas }
8536 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8537 fc2737d5 2022-09-15 thomas if (n < 0)
8538 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8539 fc2737d5 2022-09-15 thomas *off += n;
8540 fc2737d5 2022-09-15 thomas
8541 fc2737d5 2022-09-15 thomas return NULL;
8542 fc2737d5 2022-09-15 thomas }
8543 fc2737d5 2022-09-15 thomas
8544 fc2737d5 2022-09-15 thomas static const struct got_error *
8545 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
8546 fc2737d5 2022-09-15 thomas {
8547 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8548 fc2737d5 2022-09-15 thomas off_t off = 0;
8549 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
8550 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
8551 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
8552 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8553 fc2737d5 2022-09-15 thomas GENERATE_HELP
8554 fc2737d5 2022-09-15 thomas #undef KEYMAP_
8555 fc2737d5 2022-09-15 thomas #undef KEY_
8556 fc2737d5 2022-09-15 thomas };
8557 fc2737d5 2022-09-15 thomas
8558 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8559 fc2737d5 2022-09-15 thomas if (err)
8560 fc2737d5 2022-09-15 thomas return err;
8561 fc2737d5 2022-09-15 thomas
8562 fc2737d5 2022-09-15 thomas n = nitems(km);
8563 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
8564 fc2737d5 2022-09-15 thomas if (err)
8565 fc2737d5 2022-09-15 thomas return err;
8566 fc2737d5 2022-09-15 thomas
8567 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
8568 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
8569 fc2737d5 2022-09-15 thomas show = s->all;
8570 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
8571 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
8572 fc2737d5 2022-09-15 thomas show = 1;
8573 fc2737d5 2022-09-15 thomas }
8574 fc2737d5 2022-09-15 thomas if (show) {
8575 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
8576 fc2737d5 2022-09-15 thomas if (err)
8577 fc2737d5 2022-09-15 thomas return err;
8578 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8579 fc2737d5 2022-09-15 thomas if (err)
8580 fc2737d5 2022-09-15 thomas return err;
8581 fc2737d5 2022-09-15 thomas }
8582 fc2737d5 2022-09-15 thomas }
8583 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
8584 fc2737d5 2022-09-15 thomas ++off;
8585 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8586 fc2737d5 2022-09-15 thomas return err;
8587 fc2737d5 2022-09-15 thomas }
8588 fc2737d5 2022-09-15 thomas
8589 fc2737d5 2022-09-15 thomas static const struct got_error *
8590 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
8591 fc2737d5 2022-09-15 thomas {
8592 fc2737d5 2022-09-15 thomas FILE *f;
8593 fc2737d5 2022-09-15 thomas const struct got_error *err;
8594 fc2737d5 2022-09-15 thomas
8595 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8596 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8597 fc2737d5 2022-09-15 thomas s->nlines = 0;
8598 fc2737d5 2022-09-15 thomas
8599 fc2737d5 2022-09-15 thomas f = got_opentemp();
8600 fc2737d5 2022-09-15 thomas if (f == NULL)
8601 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
8602 fc2737d5 2022-09-15 thomas s->f = f;
8603 fc2737d5 2022-09-15 thomas
8604 fc2737d5 2022-09-15 thomas err = format_help(s);
8605 fc2737d5 2022-09-15 thomas if (err)
8606 fc2737d5 2022-09-15 thomas return err;
8607 fc2737d5 2022-09-15 thomas
8608 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
8609 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
8610 fc2737d5 2022-09-15 thomas
8611 fc2737d5 2022-09-15 thomas return NULL;
8612 fc2737d5 2022-09-15 thomas }
8613 fc2737d5 2022-09-15 thomas
8614 fc2737d5 2022-09-15 thomas static const struct got_error *
8615 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
8616 fc2737d5 2022-09-15 thomas {
8617 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
8618 fc2737d5 2022-09-15 thomas return NULL;
8619 fc2737d5 2022-09-15 thomas }
8620 fc2737d5 2022-09-15 thomas
8621 2a7d3cd7 2022-09-17 thomas static void
8622 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8623 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
8624 2a7d3cd7 2022-09-17 thomas {
8625 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
8626 2a7d3cd7 2022-09-17 thomas
8627 2a7d3cd7 2022-09-17 thomas *f = s->f;
8628 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
8629 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
8630 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
8631 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
8632 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
8633 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
8634 2a7d3cd7 2022-09-17 thomas }
8635 2a7d3cd7 2022-09-17 thomas
8636 fc2737d5 2022-09-15 thomas static const struct got_error *
8637 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
8638 fc2737d5 2022-09-15 thomas {
8639 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8640 fc2737d5 2022-09-15 thomas const struct got_error *err;
8641 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
8642 fc2737d5 2022-09-15 thomas wchar_t *wline;
8643 fc2737d5 2022-09-15 thomas char *line;
8644 fc2737d5 2022-09-15 thomas ssize_t linelen;
8645 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8646 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
8647 fc2737d5 2022-09-15 thomas int eos = view->nlines;
8648 fc2737d5 2022-09-15 thomas
8649 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8650 fc2737d5 2022-09-15 thomas --eos; /* account for border */
8651 fc2737d5 2022-09-15 thomas
8652 fc2737d5 2022-09-15 thomas s->lineno = 0;
8653 fc2737d5 2022-09-15 thomas rewind(s->f);
8654 fc2737d5 2022-09-15 thomas werase(view->window);
8655 fc2737d5 2022-09-15 thomas
8656 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
8657 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
8658 fc2737d5 2022-09-15 thomas
8659 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
8660 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
8661 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
8662 fc2737d5 2022-09-15 thomas if (err)
8663 fc2737d5 2022-09-15 thomas return err;
8664 fc2737d5 2022-09-15 thomas if (eos <= 1)
8665 fc2737d5 2022-09-15 thomas return NULL;
8666 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
8667 fc2737d5 2022-09-15 thomas eos -= 2;
8668 fc2737d5 2022-09-15 thomas
8669 fc2737d5 2022-09-15 thomas s->eof = 0;
8670 fc2737d5 2022-09-15 thomas view->maxx = 0;
8671 fc2737d5 2022-09-15 thomas line = NULL;
8672 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
8673 fc2737d5 2022-09-15 thomas attr_t attr = 0;
8674 fc2737d5 2022-09-15 thomas
8675 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8676 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8677 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
8678 fc2737d5 2022-09-15 thomas free(line);
8679 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
8680 fc2737d5 2022-09-15 thomas }
8681 fc2737d5 2022-09-15 thomas s->eof = 1;
8682 fc2737d5 2022-09-15 thomas break;
8683 fc2737d5 2022-09-15 thomas }
8684 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
8685 fc2737d5 2022-09-15 thomas continue;
8686 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8687 fc2737d5 2022-09-15 thomas continue;
8688 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
8689 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
8690 fc2737d5 2022-09-15 thomas
8691 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8692 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
8693 fc2737d5 2022-09-15 thomas if (err) {
8694 fc2737d5 2022-09-15 thomas free(line);
8695 fc2737d5 2022-09-15 thomas return err;
8696 fc2737d5 2022-09-15 thomas }
8697 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
8698 fc2737d5 2022-09-15 thomas free(wline);
8699 fc2737d5 2022-09-15 thomas wline = NULL;
8700 fc2737d5 2022-09-15 thomas
8701 fc2737d5 2022-09-15 thomas if (attr)
8702 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
8703 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
8704 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8705 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
8706 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
8707 fc2737d5 2022-09-15 thomas if (err) {
8708 fc2737d5 2022-09-15 thomas free(line);
8709 fc2737d5 2022-09-15 thomas return err;
8710 fc2737d5 2022-09-15 thomas }
8711 fc2737d5 2022-09-15 thomas } else {
8712 fc2737d5 2022-09-15 thomas int skip;
8713 fc2737d5 2022-09-15 thomas
8714 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
8715 fc2737d5 2022-09-15 thomas view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8716 fc2737d5 2022-09-15 thomas if (err) {
8717 fc2737d5 2022-09-15 thomas free(line);
8718 fc2737d5 2022-09-15 thomas return err;
8719 fc2737d5 2022-09-15 thomas }
8720 fc2737d5 2022-09-15 thomas rc = waddwstr(view->window, &wline[skip]);
8721 fc2737d5 2022-09-15 thomas free(wline);
8722 fc2737d5 2022-09-15 thomas wline = NULL;
8723 fc2737d5 2022-09-15 thomas if (rc == ERR)
8724 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "waddwstr");
8725 fc2737d5 2022-09-15 thomas }
8726 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
8727 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
8728 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
8729 fc2737d5 2022-09-15 thomas } else {
8730 fc2737d5 2022-09-15 thomas if (width <= view->ncols)
8731 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
8732 fc2737d5 2022-09-15 thomas }
8733 fc2737d5 2022-09-15 thomas if (attr)
8734 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
8735 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
8736 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
8737 fc2737d5 2022-09-15 thomas }
8738 fc2737d5 2022-09-15 thomas free(line);
8739 fc2737d5 2022-09-15 thomas if (nprinted > 0)
8740 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8741 fc2737d5 2022-09-15 thomas else
8742 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
8743 fc2737d5 2022-09-15 thomas
8744 fc2737d5 2022-09-15 thomas view_border(view);
8745 fc2737d5 2022-09-15 thomas
8746 fc2737d5 2022-09-15 thomas if (s->eof) {
8747 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
8748 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
8749 fc2737d5 2022-09-15 thomas view->ncols - 1);
8750 fc2737d5 2022-09-15 thomas if (rc == ERR)
8751 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8752 fc2737d5 2022-09-15 thomas } else {
8753 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
8754 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
8755 fc2737d5 2022-09-15 thomas wstandout(view->window);
8756 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
8757 fc2737d5 2022-09-15 thomas view->ncols - 1);
8758 fc2737d5 2022-09-15 thomas if (rc == ERR)
8759 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8760 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
8761 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
8762 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
8763 fc2737d5 2022-09-15 thomas if (rc == ERR)
8764 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
8765 fc2737d5 2022-09-15 thomas }
8766 fc2737d5 2022-09-15 thomas wstandend(view->window);
8767 fc2737d5 2022-09-15 thomas }
8768 fc2737d5 2022-09-15 thomas
8769 fc2737d5 2022-09-15 thomas return NULL;
8770 fc2737d5 2022-09-15 thomas }
8771 fc2737d5 2022-09-15 thomas
8772 fc2737d5 2022-09-15 thomas static const struct got_error *
8773 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8774 fc2737d5 2022-09-15 thomas {
8775 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8776 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8777 fc2737d5 2022-09-15 thomas char *line = NULL;
8778 fc2737d5 2022-09-15 thomas ssize_t linelen;
8779 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8780 fc2737d5 2022-09-15 thomas int eos, nscroll;
8781 fc2737d5 2022-09-15 thomas
8782 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
8783 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8784 fc2737d5 2022-09-15 thomas --eos; /* border */
8785 fc2737d5 2022-09-15 thomas
8786 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
8787 fc2737d5 2022-09-15 thomas
8788 fc2737d5 2022-09-15 thomas switch (ch) {
8789 fc2737d5 2022-09-15 thomas case '0':
8790 fc2737d5 2022-09-15 thomas view->x = 0;
8791 fc2737d5 2022-09-15 thomas break;
8792 fc2737d5 2022-09-15 thomas case '$':
8793 fc2737d5 2022-09-15 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
8794 fc2737d5 2022-09-15 thomas view->count = 0;
8795 fc2737d5 2022-09-15 thomas break;
8796 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
8797 fc2737d5 2022-09-15 thomas case 'l':
8798 fc2737d5 2022-09-15 thomas if (view->x + view->ncols / 3 < view->maxx)
8799 fc2737d5 2022-09-15 thomas view->x += 2;
8800 fc2737d5 2022-09-15 thomas else
8801 fc2737d5 2022-09-15 thomas view->count = 0;
8802 fc2737d5 2022-09-15 thomas break;
8803 fc2737d5 2022-09-15 thomas case KEY_LEFT:
8804 fc2737d5 2022-09-15 thomas case 'h':
8805 fc2737d5 2022-09-15 thomas view->x -= MIN(view->x, 2);
8806 fc2737d5 2022-09-15 thomas if (view->x <= 0)
8807 fc2737d5 2022-09-15 thomas view->count = 0;
8808 fc2737d5 2022-09-15 thomas break;
8809 fc2737d5 2022-09-15 thomas case 'g':
8810 fc2737d5 2022-09-15 thomas case KEY_HOME:
8811 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8812 fc2737d5 2022-09-15 thomas view->count = 0;
8813 fc2737d5 2022-09-15 thomas break;
8814 fc2737d5 2022-09-15 thomas case 'G':
8815 fc2737d5 2022-09-15 thomas case KEY_END:
8816 fc2737d5 2022-09-15 thomas view->count = 0;
8817 fc2737d5 2022-09-15 thomas if (s->eof)
8818 fc2737d5 2022-09-15 thomas break;
8819 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
8820 fc2737d5 2022-09-15 thomas s->eof = 1;
8821 fc2737d5 2022-09-15 thomas break;
8822 fc2737d5 2022-09-15 thomas case 'k':
8823 fc2737d5 2022-09-15 thomas case KEY_UP:
8824 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
8825 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
8826 fc2737d5 2022-09-15 thomas else
8827 fc2737d5 2022-09-15 thomas view->count = 0;
8828 fc2737d5 2022-09-15 thomas break;
8829 fc2737d5 2022-09-15 thomas case CTRL('u'):
8830 fc2737d5 2022-09-15 thomas case 'u':
8831 fc2737d5 2022-09-15 thomas nscroll /= 2;
8832 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8833 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
8834 fc2737d5 2022-09-15 thomas case CTRL('b'):
8835 fc2737d5 2022-09-15 thomas case 'b':
8836 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
8837 fc2737d5 2022-09-15 thomas view->count = 0;
8838 fc2737d5 2022-09-15 thomas break;
8839 fc2737d5 2022-09-15 thomas }
8840 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
8841 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
8842 fc2737d5 2022-09-15 thomas break;
8843 fc2737d5 2022-09-15 thomas case 'j':
8844 fc2737d5 2022-09-15 thomas case KEY_DOWN:
8845 fc2737d5 2022-09-15 thomas case CTRL('n'):
8846 fc2737d5 2022-09-15 thomas if (!s->eof)
8847 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
8848 fc2737d5 2022-09-15 thomas else
8849 fc2737d5 2022-09-15 thomas view->count = 0;
8850 fc2737d5 2022-09-15 thomas break;
8851 fc2737d5 2022-09-15 thomas case CTRL('d'):
8852 fc2737d5 2022-09-15 thomas case 'd':
8853 fc2737d5 2022-09-15 thomas nscroll /= 2;
8854 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8855 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
8856 fc2737d5 2022-09-15 thomas case CTRL('f'):
8857 fc2737d5 2022-09-15 thomas case 'f':
8858 fc2737d5 2022-09-15 thomas case ' ':
8859 fc2737d5 2022-09-15 thomas if (s->eof) {
8860 fc2737d5 2022-09-15 thomas view->count = 0;
8861 fc2737d5 2022-09-15 thomas break;
8862 fc2737d5 2022-09-15 thomas }
8863 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
8864 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8865 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
8866 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8867 fc2737d5 2022-09-15 thomas if (feof(s->f))
8868 fc2737d5 2022-09-15 thomas s->eof = 1;
8869 fc2737d5 2022-09-15 thomas else
8870 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
8871 fc2737d5 2022-09-15 thomas break;
8872 fc2737d5 2022-09-15 thomas }
8873 fc2737d5 2022-09-15 thomas }
8874 fc2737d5 2022-09-15 thomas free(line);
8875 fc2737d5 2022-09-15 thomas break;
8876 fc2737d5 2022-09-15 thomas default:
8877 fc2737d5 2022-09-15 thomas view->count = 0;
8878 fc2737d5 2022-09-15 thomas break;
8879 fc2737d5 2022-09-15 thomas }
8880 fc2737d5 2022-09-15 thomas
8881 fc2737d5 2022-09-15 thomas return err;
8882 fc2737d5 2022-09-15 thomas }
8883 fc2737d5 2022-09-15 thomas
8884 fc2737d5 2022-09-15 thomas static const struct got_error *
8885 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
8886 fc2737d5 2022-09-15 thomas {
8887 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8888 fc2737d5 2022-09-15 thomas
8889 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8890 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8891 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
8892 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8893 fc2737d5 2022-09-15 thomas
8894 fc2737d5 2022-09-15 thomas return NULL;
8895 fc2737d5 2022-09-15 thomas }
8896 fc2737d5 2022-09-15 thomas
8897 fc2737d5 2022-09-15 thomas static const struct got_error *
8898 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
8899 fc2737d5 2022-09-15 thomas {
8900 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8901 fc2737d5 2022-09-15 thomas
8902 fc2737d5 2022-09-15 thomas
8903 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
8904 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8905 fc2737d5 2022-09-15 thomas
8906 fc2737d5 2022-09-15 thomas wclear(view->window);
8907 fc2737d5 2022-09-15 thomas view->count = 0;
8908 fc2737d5 2022-09-15 thomas view->x = 0;
8909 fc2737d5 2022-09-15 thomas s->all = !s->all;
8910 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8911 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
8912 fc2737d5 2022-09-15 thomas s->matched_line = 0;
8913 fc2737d5 2022-09-15 thomas
8914 fc2737d5 2022-09-15 thomas return create_help(s);
8915 fc2737d5 2022-09-15 thomas }
8916 fc2737d5 2022-09-15 thomas
8917 fc2737d5 2022-09-15 thomas static const struct got_error *
8918 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
8919 fc2737d5 2022-09-15 thomas {
8920 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8921 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8922 fc2737d5 2022-09-15 thomas
8923 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
8924 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8925 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
8926 fc2737d5 2022-09-15 thomas s->selected_line = 1;
8927 fc2737d5 2022-09-15 thomas
8928 fc2737d5 2022-09-15 thomas view->show = show_help_view;
8929 fc2737d5 2022-09-15 thomas view->input = input_help_view;
8930 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
8931 fc2737d5 2022-09-15 thomas view->close = close_help_view;
8932 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
8933 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
8934 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
8935 fc2737d5 2022-09-15 thomas
8936 fc2737d5 2022-09-15 thomas err = create_help(s);
8937 fc2737d5 2022-09-15 thomas return err;
8938 fc2737d5 2022-09-15 thomas }
8939 fc2737d5 2022-09-15 thomas
8940 fc2737d5 2022-09-15 thomas static const struct got_error *
8941 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8942 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8943 2a31b33b 2022-07-23 thomas {
8944 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8945 2a31b33b 2022-07-23 thomas
8946 2a31b33b 2022-07-23 thomas *new_view = NULL;
8947 2a31b33b 2022-07-23 thomas
8948 2a31b33b 2022-07-23 thomas switch (request) {
8949 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8950 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8951 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8952 2a31b33b 2022-07-23 thomas
8953 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8954 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8955 2a31b33b 2022-07-23 thomas view, s->repo);
8956 2a31b33b 2022-07-23 thomas } else
8957 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8958 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8959 2a31b33b 2022-07-23 thomas break;
8960 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8961 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8962 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8963 2a31b33b 2022-07-23 thomas
8964 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8965 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8966 2a31b33b 2022-07-23 thomas s->repo);
8967 2a31b33b 2022-07-23 thomas } else
8968 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8969 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8970 2a31b33b 2022-07-23 thomas break;
8971 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8972 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8973 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8974 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8975 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8976 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8977 2a31b33b 2022-07-23 thomas &view->state.tree);
8978 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8979 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8980 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8981 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8982 2a31b33b 2022-07-23 thomas else
8983 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8984 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8985 2a31b33b 2022-07-23 thomas break;
8986 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8987 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8988 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8989 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8990 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8991 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8992 2a31b33b 2022-07-23 thomas view->state.log.repo);
8993 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8994 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8995 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8996 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8997 2a31b33b 2022-07-23 thomas else
8998 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8999 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9000 2a31b33b 2022-07-23 thomas break;
9001 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9002 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9003 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9004 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9005 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9006 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9007 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9008 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9009 2a31b33b 2022-07-23 thomas else
9010 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9011 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9012 2a31b33b 2022-07-23 thomas if (err)
9013 2a31b33b 2022-07-23 thomas view_close(*new_view);
9014 2a31b33b 2022-07-23 thomas break;
9015 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9016 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9017 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9018 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9019 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9020 fc2737d5 2022-09-15 thomas if (err)
9021 fc2737d5 2022-09-15 thomas view_close(*new_view);
9022 fc2737d5 2022-09-15 thomas break;
9023 2a31b33b 2022-07-23 thomas default:
9024 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9025 2a31b33b 2022-07-23 thomas }
9026 2a31b33b 2022-07-23 thomas
9027 2a31b33b 2022-07-23 thomas return err;
9028 2a31b33b 2022-07-23 thomas }
9029 2a31b33b 2022-07-23 thomas
9030 a5d43cac 2022-07-01 thomas /*
9031 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9032 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9033 a5d43cac 2022-07-01 thomas */
9034 6458efa5 2020-11-24 stsp static void
9035 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9036 a5d43cac 2022-07-01 thomas {
9037 a5d43cac 2022-07-01 thomas switch (view->type) {
9038 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9039 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9040 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9041 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9042 a5d43cac 2022-07-01 thomas 1);
9043 a5d43cac 2022-07-01 thomas break;
9044 a5d43cac 2022-07-01 thomas }
9045 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9046 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9047 a5d43cac 2022-07-01 thomas else
9048 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9049 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9050 a5d43cac 2022-07-01 thomas break;
9051 a5d43cac 2022-07-01 thomas }
9052 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9053 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9054 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9055 a5d43cac 2022-07-01 thomas break;
9056 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9057 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9058 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9059 a5d43cac 2022-07-01 thomas break;
9060 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9061 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9062 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9063 a5d43cac 2022-07-01 thomas break;
9064 a5d43cac 2022-07-01 thomas default:
9065 a5d43cac 2022-07-01 thomas break;
9066 a5d43cac 2022-07-01 thomas }
9067 a5d43cac 2022-07-01 thomas
9068 a5d43cac 2022-07-01 thomas view->offset = 0;
9069 a5d43cac 2022-07-01 thomas }
9070 a5d43cac 2022-07-01 thomas
9071 a5d43cac 2022-07-01 thomas /*
9072 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9073 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9074 a5d43cac 2022-07-01 thomas */
9075 a5d43cac 2022-07-01 thomas static const struct got_error *
9076 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9077 a5d43cac 2022-07-01 thomas {
9078 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9079 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9080 a5d43cac 2022-07-01 thomas int *selected = NULL;
9081 a5d43cac 2022-07-01 thomas int header, offset;
9082 a5d43cac 2022-07-01 thomas
9083 a5d43cac 2022-07-01 thomas switch (view->type) {
9084 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9085 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9086 a5d43cac 2022-07-01 thomas header = 3;
9087 a5d43cac 2022-07-01 thomas scrolld = NULL;
9088 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9089 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9090 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9091 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9092 a5d43cac 2022-07-01 thomas view->offset = offset;
9093 a5d43cac 2022-07-01 thomas }
9094 a5d43cac 2022-07-01 thomas break;
9095 a5d43cac 2022-07-01 thomas }
9096 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9097 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9098 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9099 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9100 a5d43cac 2022-07-01 thomas selected = &s->selected;
9101 a5d43cac 2022-07-01 thomas break;
9102 a5d43cac 2022-07-01 thomas }
9103 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9104 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9105 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9106 a5d43cac 2022-07-01 thomas header = 3;
9107 a5d43cac 2022-07-01 thomas selected = &s->selected;
9108 a5d43cac 2022-07-01 thomas break;
9109 a5d43cac 2022-07-01 thomas }
9110 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9111 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9112 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9113 a5d43cac 2022-07-01 thomas header = 5;
9114 a5d43cac 2022-07-01 thomas selected = &s->selected;
9115 a5d43cac 2022-07-01 thomas break;
9116 a5d43cac 2022-07-01 thomas }
9117 a5d43cac 2022-07-01 thomas default:
9118 a5d43cac 2022-07-01 thomas selected = NULL;
9119 a5d43cac 2022-07-01 thomas scrolld = NULL;
9120 a5d43cac 2022-07-01 thomas header = 0;
9121 a5d43cac 2022-07-01 thomas break;
9122 a5d43cac 2022-07-01 thomas }
9123 a5d43cac 2022-07-01 thomas
9124 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9125 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9126 a5d43cac 2022-07-01 thomas view->offset = offset;
9127 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9128 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9129 a5d43cac 2022-07-01 thomas *selected -= offset;
9130 a5d43cac 2022-07-01 thomas }
9131 a5d43cac 2022-07-01 thomas }
9132 a5d43cac 2022-07-01 thomas
9133 a5d43cac 2022-07-01 thomas return err;
9134 a5d43cac 2022-07-01 thomas }
9135 a5d43cac 2022-07-01 thomas
9136 a5d43cac 2022-07-01 thomas static void
9137 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9138 ce5b7c56 2019-07-09 stsp {
9139 6059809a 2020-12-17 stsp size_t i;
9140 9f7d7167 2018-04-29 stsp
9141 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9142 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9143 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9144 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9145 ce5b7c56 2019-07-09 stsp }
9146 6879ba42 2020-10-01 naddy fputc('\n', fp);
9147 ce5b7c56 2019-07-09 stsp }
9148 ce5b7c56 2019-07-09 stsp
9149 4ed7e80c 2018-05-20 stsp __dead static void
9150 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9151 9f7d7167 2018-04-29 stsp {
9152 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9153 6879ba42 2020-10-01 naddy
9154 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9155 6879ba42 2020-10-01 naddy getprogname());
9156 6879ba42 2020-10-01 naddy if (hflag) {
9157 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9158 6879ba42 2020-10-01 naddy list_commands(fp);
9159 ee85c5e8 2020-02-29 stsp }
9160 6879ba42 2020-10-01 naddy exit(status);
9161 9f7d7167 2018-04-29 stsp }
9162 9f7d7167 2018-04-29 stsp
9163 c2301be8 2018-04-30 stsp static char **
9164 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9165 c2301be8 2018-04-30 stsp {
9166 ee85c5e8 2020-02-29 stsp va_list ap;
9167 c2301be8 2018-04-30 stsp char **argv;
9168 ee85c5e8 2020-02-29 stsp int i;
9169 c2301be8 2018-04-30 stsp
9170 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9171 ee85c5e8 2020-02-29 stsp
9172 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9173 c2301be8 2018-04-30 stsp if (argv == NULL)
9174 c2301be8 2018-04-30 stsp err(1, "calloc");
9175 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9176 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9177 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9178 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9179 c2301be8 2018-04-30 stsp }
9180 c2301be8 2018-04-30 stsp
9181 ee85c5e8 2020-02-29 stsp va_end(ap);
9182 c2301be8 2018-04-30 stsp return argv;
9183 ee85c5e8 2020-02-29 stsp }
9184 ee85c5e8 2020-02-29 stsp
9185 ee85c5e8 2020-02-29 stsp /*
9186 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9187 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9188 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9189 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9190 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9191 ee85c5e8 2020-02-29 stsp */
9192 ee85c5e8 2020-02-29 stsp static const struct got_error *
9193 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9194 ee85c5e8 2020-02-29 stsp {
9195 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9196 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9197 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9198 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9199 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9200 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9201 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9202 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9203 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9204 84de9106 2020-12-26 stsp
9205 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9206 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9207 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9208 ee85c5e8 2020-02-29 stsp
9209 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9210 7cd52833 2022-06-23 thomas if (error != NULL)
9211 7cd52833 2022-06-23 thomas goto done;
9212 7cd52833 2022-06-23 thomas
9213 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9214 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9215 ee85c5e8 2020-02-29 stsp goto done;
9216 ee85c5e8 2020-02-29 stsp
9217 ee85c5e8 2020-02-29 stsp if (worktree)
9218 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9219 ee85c5e8 2020-02-29 stsp else
9220 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9221 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9222 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9223 ee85c5e8 2020-02-29 stsp goto done;
9224 ee85c5e8 2020-02-29 stsp }
9225 ee85c5e8 2020-02-29 stsp
9226 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9227 ee85c5e8 2020-02-29 stsp if (error != NULL)
9228 ee85c5e8 2020-02-29 stsp goto done;
9229 ee85c5e8 2020-02-29 stsp
9230 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9231 ee85c5e8 2020-02-29 stsp repo, worktree);
9232 ee85c5e8 2020-02-29 stsp if (error)
9233 ee85c5e8 2020-02-29 stsp goto done;
9234 ee85c5e8 2020-02-29 stsp
9235 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9236 84de9106 2020-12-26 stsp if (error)
9237 84de9106 2020-12-26 stsp goto done;
9238 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9239 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9240 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9241 ee85c5e8 2020-02-29 stsp if (error)
9242 ee85c5e8 2020-02-29 stsp goto done;
9243 ee85c5e8 2020-02-29 stsp
9244 ee85c5e8 2020-02-29 stsp if (worktree) {
9245 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9246 ee85c5e8 2020-02-29 stsp worktree = NULL;
9247 ee85c5e8 2020-02-29 stsp }
9248 ee85c5e8 2020-02-29 stsp
9249 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9250 945f9229 2022-04-16 thomas if (error)
9251 945f9229 2022-04-16 thomas goto done;
9252 945f9229 2022-04-16 thomas
9253 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9254 ee85c5e8 2020-02-29 stsp if (error) {
9255 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9256 ee85c5e8 2020-02-29 stsp goto done;
9257 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9258 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9259 6879ba42 2020-10-01 naddy usage(1, 1);
9260 ee85c5e8 2020-02-29 stsp /* not reached */
9261 ee85c5e8 2020-02-29 stsp }
9262 ee85c5e8 2020-02-29 stsp
9263 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9264 ee85c5e8 2020-02-29 stsp if (error)
9265 ee85c5e8 2020-02-29 stsp goto done;
9266 ee85c5e8 2020-02-29 stsp
9267 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9268 ee85c5e8 2020-02-29 stsp argc = 4;
9269 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9270 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9271 ee85c5e8 2020-02-29 stsp done:
9272 1d0f4054 2021-06-17 stsp if (repo) {
9273 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9274 1d0f4054 2021-06-17 stsp if (error == NULL)
9275 1d0f4054 2021-06-17 stsp error = close_err;
9276 1d0f4054 2021-06-17 stsp }
9277 945f9229 2022-04-16 thomas if (commit)
9278 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9279 ee85c5e8 2020-02-29 stsp if (worktree)
9280 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9281 7cd52833 2022-06-23 thomas if (pack_fds) {
9282 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9283 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9284 7cd52833 2022-06-23 thomas if (error == NULL)
9285 7cd52833 2022-06-23 thomas error = pack_err;
9286 7cd52833 2022-06-23 thomas }
9287 ee85c5e8 2020-02-29 stsp free(id);
9288 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9289 ee85c5e8 2020-02-29 stsp free(commit_id);
9290 ee85c5e8 2020-02-29 stsp free(cwd);
9291 ee85c5e8 2020-02-29 stsp free(repo_path);
9292 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9293 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9294 ee85c5e8 2020-02-29 stsp int i;
9295 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9296 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9297 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9298 ee85c5e8 2020-02-29 stsp }
9299 87670572 2020-12-26 naddy tog_free_refs();
9300 ee85c5e8 2020-02-29 stsp return error;
9301 c2301be8 2018-04-30 stsp }
9302 c2301be8 2018-04-30 stsp
9303 9f7d7167 2018-04-29 stsp int
9304 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9305 9f7d7167 2018-04-29 stsp {
9306 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9307 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9308 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9309 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9310 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9311 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9312 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9313 83cd27f8 2020-01-13 stsp };
9314 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9315 9f7d7167 2018-04-29 stsp
9316 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9317 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9318 cacf1690 2022-09-06 thomas
9319 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9320 9f7d7167 2018-04-29 stsp
9321 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9322 9f7d7167 2018-04-29 stsp switch (ch) {
9323 9f7d7167 2018-04-29 stsp case 'h':
9324 9f7d7167 2018-04-29 stsp hflag = 1;
9325 9f7d7167 2018-04-29 stsp break;
9326 53ccebc2 2019-07-30 stsp case 'V':
9327 53ccebc2 2019-07-30 stsp Vflag = 1;
9328 53ccebc2 2019-07-30 stsp break;
9329 9f7d7167 2018-04-29 stsp default:
9330 6879ba42 2020-10-01 naddy usage(hflag, 1);
9331 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9332 9f7d7167 2018-04-29 stsp }
9333 9f7d7167 2018-04-29 stsp }
9334 9f7d7167 2018-04-29 stsp
9335 9f7d7167 2018-04-29 stsp argc -= optind;
9336 9f7d7167 2018-04-29 stsp argv += optind;
9337 9814e6a3 2020-09-27 naddy optind = 1;
9338 c2301be8 2018-04-30 stsp optreset = 1;
9339 9f7d7167 2018-04-29 stsp
9340 53ccebc2 2019-07-30 stsp if (Vflag) {
9341 53ccebc2 2019-07-30 stsp got_version_print_str();
9342 6879ba42 2020-10-01 naddy return 0;
9343 53ccebc2 2019-07-30 stsp }
9344 4010e238 2020-12-04 stsp
9345 4010e238 2020-12-04 stsp #ifndef PROFILE
9346 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9347 4010e238 2020-12-04 stsp NULL) == -1)
9348 4010e238 2020-12-04 stsp err(1, "pledge");
9349 4010e238 2020-12-04 stsp #endif
9350 53ccebc2 2019-07-30 stsp
9351 c2301be8 2018-04-30 stsp if (argc == 0) {
9352 f29d3e89 2018-06-23 stsp if (hflag)
9353 6879ba42 2020-10-01 naddy usage(hflag, 0);
9354 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9355 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9356 c2301be8 2018-04-30 stsp argc = 1;
9357 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9358 c2301be8 2018-04-30 stsp } else {
9359 6059809a 2020-12-17 stsp size_t i;
9360 9f7d7167 2018-04-29 stsp
9361 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9362 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9363 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9364 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9365 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9366 9f7d7167 2018-04-29 stsp break;
9367 9f7d7167 2018-04-29 stsp }
9368 9f7d7167 2018-04-29 stsp }
9369 ee85c5e8 2020-02-29 stsp }
9370 3642c4c6 2019-07-09 stsp
9371 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9372 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9373 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9374 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9375 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9376 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9377 adf4c9e0 2022-07-03 thomas }
9378 adf4c9e0 2022-07-03 thomas
9379 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9380 ee85c5e8 2020-02-29 stsp if (argc != 1)
9381 6879ba42 2020-10-01 naddy usage(0, 1);
9382 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9383 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9384 ee85c5e8 2020-02-29 stsp } else {
9385 ee85c5e8 2020-02-29 stsp if (hflag)
9386 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9387 ee85c5e8 2020-02-29 stsp else
9388 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9389 9f7d7167 2018-04-29 stsp }
9390 9f7d7167 2018-04-29 stsp
9391 9f7d7167 2018-04-29 stsp endwin();
9392 b46c1e04 2020-09-20 naddy putchar('\n');
9393 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9394 a2f4a359 2020-02-28 stsp int i;
9395 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9396 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9397 a2f4a359 2020-02-28 stsp free(cmd_argv);
9398 a2f4a359 2020-02-28 stsp }
9399 a2f4a359 2020-02-28 stsp
9400 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9401 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9402 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9403 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9404 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9405 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9406 9f7d7167 2018-04-29 stsp return 0;
9407 9f7d7167 2018-04-29 stsp }