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 2b49a8ae 2019-06-22 stsp
1313 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1314 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1315 a5d43cac 2022-07-01 thomas
1316 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1317 2b49a8ae 2019-06-22 stsp nocbreak();
1318 2b49a8ae 2019-06-22 stsp echo();
1319 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1320 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1321 2b49a8ae 2019-06-22 stsp cbreak();
1322 2b49a8ae 2019-06-22 stsp noecho();
1323 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1324 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1325 2b49a8ae 2019-06-22 stsp return NULL;
1326 2b49a8ae 2019-06-22 stsp
1327 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1328 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1329 7c32bd05 2019-06-22 stsp if (err) {
1330 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1331 7c32bd05 2019-06-22 stsp return err;
1332 7c32bd05 2019-06-22 stsp }
1333 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1334 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1335 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1336 2b49a8ae 2019-06-22 stsp view->search_next(view);
1337 2b49a8ae 2019-06-22 stsp }
1338 2b49a8ae 2019-06-22 stsp
1339 2b49a8ae 2019-06-22 stsp return NULL;
1340 64486692 2022-07-07 thomas }
1341 64486692 2022-07-07 thomas
1342 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1343 64486692 2022-07-07 thomas static const struct got_error *
1344 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1345 64486692 2022-07-07 thomas {
1346 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1347 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1348 64486692 2022-07-07 thomas
1349 64486692 2022-07-07 thomas if (view->parent)
1350 64486692 2022-07-07 thomas v = view->parent;
1351 64486692 2022-07-07 thomas else
1352 64486692 2022-07-07 thomas v = view;
1353 64486692 2022-07-07 thomas
1354 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1355 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1356 ddbc4d37 2022-07-12 thomas else
1357 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1358 64486692 2022-07-07 thomas
1359 ddbc4d37 2022-07-12 thomas if (!v->child)
1360 ddbc4d37 2022-07-12 thomas return NULL;
1361 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1362 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1363 ddbc4d37 2022-07-12 thomas
1364 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1365 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1366 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1367 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1368 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1369 64486692 2022-07-07 thomas
1370 ddbc4d37 2022-07-12 thomas
1371 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1372 64486692 2022-07-07 thomas v->ncols = COLS;
1373 64486692 2022-07-07 thomas v->child->ncols = COLS;
1374 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1375 64486692 2022-07-07 thomas
1376 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1377 64486692 2022-07-07 thomas if (err)
1378 64486692 2022-07-07 thomas return err;
1379 64486692 2022-07-07 thomas }
1380 64486692 2022-07-07 thomas v->child->mode = v->mode;
1381 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1382 64486692 2022-07-07 thomas v->focus_child = 1;
1383 64486692 2022-07-07 thomas
1384 64486692 2022-07-07 thomas err = view_fullscreen(v);
1385 64486692 2022-07-07 thomas if (err)
1386 64486692 2022-07-07 thomas return err;
1387 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1388 64486692 2022-07-07 thomas if (err)
1389 64486692 2022-07-07 thomas return err;
1390 64486692 2022-07-07 thomas
1391 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1392 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1393 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1394 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1395 cf1fe301 2022-07-29 thomas if (err)
1396 cf1fe301 2022-07-29 thomas return err;
1397 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1398 cf1fe301 2022-07-29 thomas if (err)
1399 cf1fe301 2022-07-29 thomas return err;
1400 ddbc4d37 2022-07-12 thomas } else {
1401 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1402 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1403 64486692 2022-07-07 thomas }
1404 f4e6231a 2022-07-22 thomas if (v->resize)
1405 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1406 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1407 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1408 64486692 2022-07-07 thomas
1409 64486692 2022-07-07 thomas return err;
1410 0cf4efb1 2018-09-29 stsp }
1411 6d0fee91 2018-08-01 stsp
1412 07b0611c 2022-06-23 thomas /*
1413 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1414 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1415 07b0611c 2022-06-23 thomas */
1416 07b0611c 2022-06-23 thomas static int
1417 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1418 07b0611c 2022-06-23 thomas {
1419 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1420 a5d43cac 2022-07-01 thomas int x, n = 0;
1421 07b0611c 2022-06-23 thomas
1422 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1423 a5d43cac 2022-07-01 thomas v = view->child;
1424 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1425 a5d43cac 2022-07-01 thomas v = view->parent;
1426 a5d43cac 2022-07-01 thomas
1427 07b0611c 2022-06-23 thomas view->count = 0;
1428 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1429 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1430 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1431 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1432 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1433 07b0611c 2022-06-23 thomas
1434 07b0611c 2022-06-23 thomas do {
1435 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1436 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1437 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1438 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1439 a5d43cac 2022-07-01 thomas }
1440 a5d43cac 2022-07-01 thomas
1441 07b0611c 2022-06-23 thomas /*
1442 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1443 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1444 07b0611c 2022-06-23 thomas */
1445 07b0611c 2022-06-23 thomas if (n >= 9999999)
1446 07b0611c 2022-06-23 thomas n = 9999999;
1447 07b0611c 2022-06-23 thomas else
1448 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1449 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1450 07b0611c 2022-06-23 thomas
1451 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1452 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1453 07dd3ed3 2022-08-06 thomas n = 0;
1454 07dd3ed3 2022-08-06 thomas c = 0;
1455 07dd3ed3 2022-08-06 thomas }
1456 07dd3ed3 2022-08-06 thomas
1457 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1458 07b0611c 2022-06-23 thomas view->count = n;
1459 07b0611c 2022-06-23 thomas
1460 07b0611c 2022-06-23 thomas return c;
1461 07b0611c 2022-06-23 thomas }
1462 07b0611c 2022-06-23 thomas
1463 0cf4efb1 2018-09-29 stsp static const struct got_error *
1464 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1465 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1466 e5a0f69f 2018-08-18 stsp {
1467 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1468 669b5ffa 2018-10-07 stsp struct tog_view *v;
1469 1a76625f 2018-10-22 stsp int ch, errcode;
1470 e5a0f69f 2018-08-18 stsp
1471 e5a0f69f 2018-08-18 stsp *new = NULL;
1472 8f4ed634 2020-03-26 stsp
1473 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1474 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1475 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1476 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1477 07b0611c 2022-06-23 thomas view->count = 0;
1478 07b0611c 2022-06-23 thomas }
1479 e5a0f69f 2018-08-18 stsp
1480 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1481 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1482 82954512 2020-02-03 stsp if (errcode)
1483 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1484 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1485 3da8ef85 2021-09-21 stsp sched_yield();
1486 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1487 82954512 2020-02-03 stsp if (errcode)
1488 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1489 82954512 2020-02-03 stsp "pthread_mutex_lock");
1490 60493ae3 2019-06-20 stsp view->search_next(view);
1491 60493ae3 2019-06-20 stsp return NULL;
1492 60493ae3 2019-06-20 stsp }
1493 60493ae3 2019-06-20 stsp
1494 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1495 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1496 1a76625f 2018-10-22 stsp if (errcode)
1497 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1498 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1499 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1500 634cb454 2022-07-03 thomas cbreak();
1501 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1502 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1503 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1504 634cb454 2022-07-03 thomas view->count = 0;
1505 634cb454 2022-07-03 thomas else
1506 634cb454 2022-07-03 thomas ch = view->ch;
1507 634cb454 2022-07-03 thomas } else {
1508 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1509 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1510 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1511 07b0611c 2022-06-23 thomas }
1512 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1513 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1514 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1515 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1516 1a76625f 2018-10-22 stsp if (errcode)
1517 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1518 25791caa 2018-10-24 stsp
1519 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1520 25791caa 2018-10-24 stsp tog_resizeterm();
1521 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1522 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1523 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1524 25791caa 2018-10-24 stsp err = view_resize(v);
1525 25791caa 2018-10-24 stsp if (err)
1526 25791caa 2018-10-24 stsp return err;
1527 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1528 25791caa 2018-10-24 stsp if (err)
1529 25791caa 2018-10-24 stsp return err;
1530 cdfcfb03 2020-12-06 stsp if (v->child) {
1531 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1532 cdfcfb03 2020-12-06 stsp if (err)
1533 cdfcfb03 2020-12-06 stsp return err;
1534 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1535 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1536 cdfcfb03 2020-12-06 stsp if (err)
1537 cdfcfb03 2020-12-06 stsp return err;
1538 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1539 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1540 53d2bdd3 2022-07-10 thomas if (err)
1541 53d2bdd3 2022-07-10 thomas return err;
1542 53d2bdd3 2022-07-10 thomas }
1543 cdfcfb03 2020-12-06 stsp }
1544 25791caa 2018-10-24 stsp }
1545 25791caa 2018-10-24 stsp }
1546 25791caa 2018-10-24 stsp
1547 e5a0f69f 2018-08-18 stsp switch (ch) {
1548 fc2737d5 2022-09-15 thomas case '?':
1549 fc2737d5 2022-09-15 thomas case 'H':
1550 fc2737d5 2022-09-15 thomas case KEY_F(1):
1551 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1552 fc2737d5 2022-09-15 thomas err = view->reset(view);
1553 fc2737d5 2022-09-15 thomas else
1554 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1555 fc2737d5 2022-09-15 thomas break;
1556 1e37a5c2 2019-05-12 jcs case '\t':
1557 07b0611c 2022-06-23 thomas view->count = 0;
1558 1e37a5c2 2019-05-12 jcs if (view->child) {
1559 e78dc838 2020-12-04 stsp view->focussed = 0;
1560 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1561 e78dc838 2020-12-04 stsp view->focus_child = 1;
1562 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1563 e78dc838 2020-12-04 stsp view->focussed = 0;
1564 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1565 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1566 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1567 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1568 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1569 3a0139e8 2022-07-22 thomas 0);
1570 a5d43cac 2022-07-01 thomas if (err)
1571 a5d43cac 2022-07-01 thomas return err;
1572 a5d43cac 2022-07-01 thomas }
1573 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1574 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1575 a5d43cac 2022-07-01 thomas if (err)
1576 a5d43cac 2022-07-01 thomas return err;
1577 a5d43cac 2022-07-01 thomas }
1578 1e37a5c2 2019-05-12 jcs }
1579 1e37a5c2 2019-05-12 jcs break;
1580 1e37a5c2 2019-05-12 jcs case 'q':
1581 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1582 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1583 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1584 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1585 a5d43cac 2022-07-01 thomas if (err)
1586 a5d43cac 2022-07-01 thomas break;
1587 a5d43cac 2022-07-01 thomas }
1588 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1589 a5d43cac 2022-07-01 thomas }
1590 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1591 9970f7fc 2020-12-03 stsp view->dying = 1;
1592 1e37a5c2 2019-05-12 jcs break;
1593 1e37a5c2 2019-05-12 jcs case 'Q':
1594 1e37a5c2 2019-05-12 jcs *done = 1;
1595 1e37a5c2 2019-05-12 jcs break;
1596 1c5e5faa 2022-06-23 thomas case 'F':
1597 07b0611c 2022-06-23 thomas view->count = 0;
1598 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1599 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1600 1e37a5c2 2019-05-12 jcs break;
1601 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1602 e78dc838 2020-12-04 stsp view->focussed = 0;
1603 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1604 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1605 53d2bdd3 2022-07-10 thomas } else {
1606 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1607 53d2bdd3 2022-07-10 thomas if (!err)
1608 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1609 53d2bdd3 2022-07-10 thomas }
1610 1e37a5c2 2019-05-12 jcs if (err)
1611 1e37a5c2 2019-05-12 jcs break;
1612 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1613 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1614 1e37a5c2 2019-05-12 jcs } else {
1615 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1616 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1617 e78dc838 2020-12-04 stsp view->focussed = 1;
1618 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1619 1e37a5c2 2019-05-12 jcs } else {
1620 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1621 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1622 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1623 53d2bdd3 2022-07-10 thomas if (!err)
1624 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1625 669b5ffa 2018-10-07 stsp }
1626 1e37a5c2 2019-05-12 jcs if (err)
1627 1e37a5c2 2019-05-12 jcs break;
1628 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1629 a5d43cac 2022-07-01 thomas }
1630 a5d43cac 2022-07-01 thomas if (err)
1631 a5d43cac 2022-07-01 thomas break;
1632 3a0139e8 2022-07-22 thomas if (view->resize) {
1633 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1634 a5d43cac 2022-07-01 thomas if (err)
1635 a5d43cac 2022-07-01 thomas break;
1636 1e37a5c2 2019-05-12 jcs }
1637 a5d43cac 2022-07-01 thomas if (view->parent)
1638 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1639 a5d43cac 2022-07-01 thomas if (!err)
1640 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1641 1e37a5c2 2019-05-12 jcs break;
1642 64486692 2022-07-07 thomas case 'S':
1643 53d2bdd3 2022-07-10 thomas view->count = 0;
1644 64486692 2022-07-07 thomas err = switch_split(view);
1645 53d2bdd3 2022-07-10 thomas break;
1646 53d2bdd3 2022-07-10 thomas case '-':
1647 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1648 64486692 2022-07-07 thomas break;
1649 53d2bdd3 2022-07-10 thomas case '+':
1650 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1651 53d2bdd3 2022-07-10 thomas break;
1652 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1653 60493ae3 2019-06-20 stsp break;
1654 60493ae3 2019-06-20 stsp case '/':
1655 07b0611c 2022-06-23 thomas view->count = 0;
1656 60493ae3 2019-06-20 stsp if (view->search_start)
1657 2b49a8ae 2019-06-22 stsp view_search_start(view);
1658 60493ae3 2019-06-20 stsp else
1659 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1660 1e37a5c2 2019-05-12 jcs break;
1661 b1bf1435 2019-06-21 stsp case 'N':
1662 60493ae3 2019-06-20 stsp case 'n':
1663 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1664 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1665 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1666 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1667 60493ae3 2019-06-20 stsp view->search_next(view);
1668 60493ae3 2019-06-20 stsp } else
1669 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1670 adf4c9e0 2022-07-03 thomas break;
1671 adf4c9e0 2022-07-03 thomas case 'A':
1672 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1673 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1674 adf4c9e0 2022-07-03 thomas else
1675 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1676 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1677 adf4c9e0 2022-07-03 thomas if (v->reset) {
1678 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1679 adf4c9e0 2022-07-03 thomas if (err)
1680 adf4c9e0 2022-07-03 thomas return err;
1681 adf4c9e0 2022-07-03 thomas }
1682 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1683 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1684 adf4c9e0 2022-07-03 thomas if (err)
1685 adf4c9e0 2022-07-03 thomas return err;
1686 adf4c9e0 2022-07-03 thomas }
1687 adf4c9e0 2022-07-03 thomas }
1688 60493ae3 2019-06-20 stsp break;
1689 1e37a5c2 2019-05-12 jcs default:
1690 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1691 1e37a5c2 2019-05-12 jcs break;
1692 e5a0f69f 2018-08-18 stsp }
1693 e5a0f69f 2018-08-18 stsp
1694 e5a0f69f 2018-08-18 stsp return err;
1695 bcbd79e2 2018-08-19 stsp }
1696 bcbd79e2 2018-08-19 stsp
1697 ef20f542 2022-06-26 thomas static int
1698 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1699 a3404814 2018-09-02 stsp {
1700 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1701 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1702 669b5ffa 2018-10-07 stsp return 0;
1703 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1704 669b5ffa 2018-10-07 stsp return 0;
1705 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1706 a3404814 2018-09-02 stsp return 0;
1707 a3404814 2018-09-02 stsp
1708 669b5ffa 2018-10-07 stsp return view->focussed;
1709 a3404814 2018-09-02 stsp }
1710 a3404814 2018-09-02 stsp
1711 bcbd79e2 2018-08-19 stsp static const struct got_error *
1712 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1713 e5a0f69f 2018-08-18 stsp {
1714 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1715 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1716 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1717 64486692 2022-07-07 thomas char *mode;
1718 fd823528 2018-10-22 stsp int fast_refresh = 10;
1719 1a76625f 2018-10-22 stsp int done = 0, errcode;
1720 e5a0f69f 2018-08-18 stsp
1721 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1722 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1723 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1724 64486692 2022-07-07 thomas else
1725 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1726 64486692 2022-07-07 thomas
1727 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1728 1a76625f 2018-10-22 stsp if (errcode)
1729 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1730 1a76625f 2018-10-22 stsp
1731 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1732 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1733 e5a0f69f 2018-08-18 stsp
1734 1004088d 2018-09-29 stsp view->focussed = 1;
1735 878940b7 2018-09-29 stsp err = view->show(view);
1736 0cf4efb1 2018-09-29 stsp if (err)
1737 0cf4efb1 2018-09-29 stsp return err;
1738 0cf4efb1 2018-09-29 stsp update_panels();
1739 0cf4efb1 2018-09-29 stsp doupdate();
1740 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1741 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1742 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1743 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1744 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1745 fd823528 2018-10-22 stsp
1746 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1747 e5a0f69f 2018-08-18 stsp if (err)
1748 e5a0f69f 2018-08-18 stsp break;
1749 9970f7fc 2020-12-03 stsp if (view->dying) {
1750 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1751 669b5ffa 2018-10-07 stsp
1752 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1753 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1754 9970f7fc 2020-12-03 stsp entry);
1755 e78dc838 2020-12-04 stsp else if (view->parent)
1756 669b5ffa 2018-10-07 stsp prev = view->parent;
1757 669b5ffa 2018-10-07 stsp
1758 e78dc838 2020-12-04 stsp if (view->parent) {
1759 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1760 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1761 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1762 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1763 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1764 40236d76 2022-06-23 thomas if (err)
1765 40236d76 2022-06-23 thomas break;
1766 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1767 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1768 e78dc838 2020-12-04 stsp } else
1769 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1770 669b5ffa 2018-10-07 stsp
1771 9970f7fc 2020-12-03 stsp err = view_close(view);
1772 fb59748f 2020-12-05 stsp if (err)
1773 e5a0f69f 2018-08-18 stsp goto done;
1774 669b5ffa 2018-10-07 stsp
1775 e78dc838 2020-12-04 stsp view = NULL;
1776 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1777 e78dc838 2020-12-04 stsp if (v->focussed)
1778 e78dc838 2020-12-04 stsp break;
1779 0cf4efb1 2018-09-29 stsp }
1780 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1781 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1782 e78dc838 2020-12-04 stsp if (prev)
1783 e78dc838 2020-12-04 stsp view = prev;
1784 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1785 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1786 e78dc838 2020-12-04 stsp tog_view_list_head);
1787 e78dc838 2020-12-04 stsp }
1788 e78dc838 2020-12-04 stsp if (view) {
1789 e78dc838 2020-12-04 stsp if (view->focus_child) {
1790 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1791 e78dc838 2020-12-04 stsp view = view->child;
1792 e78dc838 2020-12-04 stsp } else
1793 e78dc838 2020-12-04 stsp view->focussed = 1;
1794 e78dc838 2020-12-04 stsp }
1795 e78dc838 2020-12-04 stsp }
1796 e5a0f69f 2018-08-18 stsp }
1797 bcbd79e2 2018-08-19 stsp if (new_view) {
1798 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1799 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1800 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1801 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1802 86c66b02 2018-10-18 stsp continue;
1803 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1804 86c66b02 2018-10-18 stsp err = view_close(v);
1805 86c66b02 2018-10-18 stsp if (err)
1806 86c66b02 2018-10-18 stsp goto done;
1807 86c66b02 2018-10-18 stsp break;
1808 86c66b02 2018-10-18 stsp }
1809 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1810 fed7eaa8 2018-10-24 stsp view = new_view;
1811 7cd52833 2022-06-23 thomas }
1812 669b5ffa 2018-10-07 stsp if (view) {
1813 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1814 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1815 e78dc838 2020-12-04 stsp view = view->child;
1816 e78dc838 2020-12-04 stsp } else {
1817 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1818 e78dc838 2020-12-04 stsp view = view->parent;
1819 1a76625f 2018-10-22 stsp }
1820 e78dc838 2020-12-04 stsp show_panel(view->panel);
1821 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1822 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1823 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1824 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1825 669b5ffa 2018-10-07 stsp if (err)
1826 1a76625f 2018-10-22 stsp goto done;
1827 669b5ffa 2018-10-07 stsp }
1828 669b5ffa 2018-10-07 stsp err = view->show(view);
1829 0cf4efb1 2018-09-29 stsp if (err)
1830 1a76625f 2018-10-22 stsp goto done;
1831 669b5ffa 2018-10-07 stsp if (view->child) {
1832 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1833 669b5ffa 2018-10-07 stsp if (err)
1834 1a76625f 2018-10-22 stsp goto done;
1835 669b5ffa 2018-10-07 stsp }
1836 1a76625f 2018-10-22 stsp update_panels();
1837 1a76625f 2018-10-22 stsp doupdate();
1838 0cf4efb1 2018-09-29 stsp }
1839 e5a0f69f 2018-08-18 stsp }
1840 e5a0f69f 2018-08-18 stsp done:
1841 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1842 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1843 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1844 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1845 f2d749db 2022-07-12 thomas close_err = view_close(view);
1846 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1847 f2d749db 2022-07-12 thomas err = close_err;
1848 e5a0f69f 2018-08-18 stsp }
1849 1a76625f 2018-10-22 stsp
1850 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1851 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1852 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1853 1a76625f 2018-10-22 stsp
1854 e5a0f69f 2018-08-18 stsp return err;
1855 ea5e7bb5 2018-08-01 stsp }
1856 ea5e7bb5 2018-08-01 stsp
1857 4ed7e80c 2018-05-20 stsp __dead static void
1858 9f7d7167 2018-04-29 stsp usage_log(void)
1859 9f7d7167 2018-04-29 stsp {
1860 80ddbec8 2018-04-29 stsp endwin();
1861 c70c5802 2018-08-01 stsp fprintf(stderr,
1862 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1863 9f7d7167 2018-04-29 stsp getprogname());
1864 9f7d7167 2018-04-29 stsp exit(1);
1865 80ddbec8 2018-04-29 stsp }
1866 80ddbec8 2018-04-29 stsp
1867 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1868 80ddbec8 2018-04-29 stsp static const struct got_error *
1869 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1870 963b370f 2018-05-20 stsp {
1871 00dfcb92 2018-06-11 stsp char *vis = NULL;
1872 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1873 963b370f 2018-05-20 stsp
1874 963b370f 2018-05-20 stsp *ws = NULL;
1875 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1876 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1877 00dfcb92 2018-06-11 stsp int vislen;
1878 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1879 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1880 00dfcb92 2018-06-11 stsp
1881 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1882 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1883 00dfcb92 2018-06-11 stsp if (err)
1884 00dfcb92 2018-06-11 stsp return err;
1885 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1886 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1887 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1888 a7f50699 2018-06-11 stsp goto done;
1889 a7f50699 2018-06-11 stsp }
1890 00dfcb92 2018-06-11 stsp }
1891 963b370f 2018-05-20 stsp
1892 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1893 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1894 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1895 a7f50699 2018-06-11 stsp goto done;
1896 a7f50699 2018-06-11 stsp }
1897 963b370f 2018-05-20 stsp
1898 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1899 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1900 a7f50699 2018-06-11 stsp done:
1901 00dfcb92 2018-06-11 stsp free(vis);
1902 963b370f 2018-05-20 stsp if (err) {
1903 963b370f 2018-05-20 stsp free(*ws);
1904 963b370f 2018-05-20 stsp *ws = NULL;
1905 963b370f 2018-05-20 stsp *wlen = 0;
1906 963b370f 2018-05-20 stsp }
1907 963b370f 2018-05-20 stsp return err;
1908 05171be4 2022-06-23 thomas }
1909 05171be4 2022-06-23 thomas
1910 05171be4 2022-06-23 thomas static const struct got_error *
1911 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1912 05171be4 2022-06-23 thomas {
1913 05171be4 2022-06-23 thomas char *dst;
1914 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1915 05171be4 2022-06-23 thomas
1916 05171be4 2022-06-23 thomas *ptr = NULL;
1917 05171be4 2022-06-23 thomas n = len = strlen(src);
1918 83316834 2022-06-23 thomas dst = malloc(n + 1);
1919 05171be4 2022-06-23 thomas if (dst == NULL)
1920 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1921 05171be4 2022-06-23 thomas
1922 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1923 05171be4 2022-06-23 thomas const char c = src[idx];
1924 05171be4 2022-06-23 thomas
1925 05171be4 2022-06-23 thomas if (c == '\t') {
1926 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1927 b235ad5d 2022-06-23 thomas char *p;
1928 b235ad5d 2022-06-23 thomas
1929 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1930 83316834 2022-06-23 thomas if (p == NULL) {
1931 83316834 2022-06-23 thomas free(dst);
1932 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1933 83316834 2022-06-23 thomas
1934 83316834 2022-06-23 thomas }
1935 83316834 2022-06-23 thomas dst = p;
1936 05171be4 2022-06-23 thomas n += nb;
1937 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1938 05171be4 2022-06-23 thomas sz += nb;
1939 05171be4 2022-06-23 thomas } else
1940 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1941 05171be4 2022-06-23 thomas ++idx;
1942 05171be4 2022-06-23 thomas }
1943 05171be4 2022-06-23 thomas
1944 05171be4 2022-06-23 thomas dst[sz] = '\0';
1945 05171be4 2022-06-23 thomas *ptr = dst;
1946 05171be4 2022-06-23 thomas return NULL;
1947 963b370f 2018-05-20 stsp }
1948 963b370f 2018-05-20 stsp
1949 8d208d34 2022-06-23 thomas /*
1950 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1951 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1952 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1953 8d208d34 2022-06-23 thomas * *rcol.
1954 f91a2b48 2022-06-23 thomas */
1955 8d208d34 2022-06-23 thomas static int
1956 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1957 8d208d34 2022-06-23 thomas {
1958 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1959 f91a2b48 2022-06-23 thomas
1960 8d208d34 2022-06-23 thomas if (n == 0) {
1961 8d208d34 2022-06-23 thomas *rcol = cols;
1962 8d208d34 2022-06-23 thomas return off;
1963 8d208d34 2022-06-23 thomas }
1964 f91a2b48 2022-06-23 thomas
1965 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1966 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1967 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1968 8d208d34 2022-06-23 thomas else
1969 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1970 f91a2b48 2022-06-23 thomas
1971 8d208d34 2022-06-23 thomas if (width == -1) {
1972 8d208d34 2022-06-23 thomas width = 1;
1973 8d208d34 2022-06-23 thomas wline[i] = L'.';
1974 f91a2b48 2022-06-23 thomas }
1975 f91a2b48 2022-06-23 thomas
1976 8d208d34 2022-06-23 thomas if (cols + width > n)
1977 8d208d34 2022-06-23 thomas break;
1978 8d208d34 2022-06-23 thomas cols += width;
1979 f91a2b48 2022-06-23 thomas }
1980 f91a2b48 2022-06-23 thomas
1981 8d208d34 2022-06-23 thomas *rcol = cols;
1982 8d208d34 2022-06-23 thomas return i;
1983 f91a2b48 2022-06-23 thomas }
1984 f91a2b48 2022-06-23 thomas
1985 f91a2b48 2022-06-23 thomas /*
1986 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1987 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1988 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1989 f91a2b48 2022-06-23 thomas */
1990 f91a2b48 2022-06-23 thomas static const struct got_error *
1991 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1992 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1993 f91a2b48 2022-06-23 thomas {
1994 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1995 8d208d34 2022-06-23 thomas int cols;
1996 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1997 05171be4 2022-06-23 thomas char *exstr = NULL;
1998 963b370f 2018-05-20 stsp size_t wlen;
1999 8d208d34 2022-06-23 thomas int i, scrollx;
2000 963b370f 2018-05-20 stsp
2001 963b370f 2018-05-20 stsp *wlinep = NULL;
2002 b700b5d6 2018-07-10 stsp *widthp = 0;
2003 963b370f 2018-05-20 stsp
2004 05171be4 2022-06-23 thomas if (expand) {
2005 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2006 05171be4 2022-06-23 thomas if (err)
2007 05171be4 2022-06-23 thomas return err;
2008 05171be4 2022-06-23 thomas }
2009 05171be4 2022-06-23 thomas
2010 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2011 05171be4 2022-06-23 thomas free(exstr);
2012 963b370f 2018-05-20 stsp if (err)
2013 963b370f 2018-05-20 stsp return err;
2014 963b370f 2018-05-20 stsp
2015 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2016 f91a2b48 2022-06-23 thomas
2017 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2018 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2019 3f670bfb 2020-12-10 stsp wlen--;
2020 3f670bfb 2020-12-10 stsp }
2021 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2022 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2023 3f670bfb 2020-12-10 stsp wlen--;
2024 3f670bfb 2020-12-10 stsp }
2025 3f670bfb 2020-12-10 stsp
2026 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2027 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2028 27a741e5 2019-09-11 stsp
2029 b700b5d6 2018-07-10 stsp if (widthp)
2030 b700b5d6 2018-07-10 stsp *widthp = cols;
2031 f91a2b48 2022-06-23 thomas if (scrollxp)
2032 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2033 963b370f 2018-05-20 stsp if (err)
2034 963b370f 2018-05-20 stsp free(wline);
2035 963b370f 2018-05-20 stsp else
2036 963b370f 2018-05-20 stsp *wlinep = wline;
2037 963b370f 2018-05-20 stsp return err;
2038 963b370f 2018-05-20 stsp }
2039 963b370f 2018-05-20 stsp
2040 8b473291 2019-02-21 stsp static const struct got_error*
2041 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2042 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2043 8b473291 2019-02-21 stsp {
2044 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2045 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2046 8b473291 2019-02-21 stsp char *s;
2047 8b473291 2019-02-21 stsp const char *name;
2048 8b473291 2019-02-21 stsp
2049 8b473291 2019-02-21 stsp *refs_str = NULL;
2050 8b473291 2019-02-21 stsp
2051 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2052 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2053 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2054 52b5abe1 2019-08-13 stsp int cmp;
2055 52b5abe1 2019-08-13 stsp
2056 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2057 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2058 8b473291 2019-02-21 stsp continue;
2059 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2060 8b473291 2019-02-21 stsp name += 5;
2061 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2062 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2063 7143d404 2019-03-12 stsp continue;
2064 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2065 8b473291 2019-02-21 stsp name += 6;
2066 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2067 8b473291 2019-02-21 stsp name += 8;
2068 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2069 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2070 79cc719f 2020-04-24 stsp continue;
2071 79cc719f 2020-04-24 stsp }
2072 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2073 48cae60d 2020-09-22 stsp if (err)
2074 48cae60d 2020-09-22 stsp break;
2075 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2076 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2077 5d844a1e 2019-08-13 stsp if (err) {
2078 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2079 48cae60d 2020-09-22 stsp free(ref_id);
2080 5d844a1e 2019-08-13 stsp break;
2081 48cae60d 2020-09-22 stsp }
2082 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2083 5d844a1e 2019-08-13 stsp err = NULL;
2084 5d844a1e 2019-08-13 stsp tag = NULL;
2085 5d844a1e 2019-08-13 stsp }
2086 52b5abe1 2019-08-13 stsp }
2087 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2088 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2089 48cae60d 2020-09-22 stsp free(ref_id);
2090 52b5abe1 2019-08-13 stsp if (tag)
2091 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2092 52b5abe1 2019-08-13 stsp if (cmp != 0)
2093 52b5abe1 2019-08-13 stsp continue;
2094 8b473291 2019-02-21 stsp s = *refs_str;
2095 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2096 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2097 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2098 8b473291 2019-02-21 stsp free(s);
2099 8b473291 2019-02-21 stsp *refs_str = NULL;
2100 8b473291 2019-02-21 stsp break;
2101 8b473291 2019-02-21 stsp }
2102 8b473291 2019-02-21 stsp free(s);
2103 8b473291 2019-02-21 stsp }
2104 8b473291 2019-02-21 stsp
2105 8b473291 2019-02-21 stsp return err;
2106 8b473291 2019-02-21 stsp }
2107 8b473291 2019-02-21 stsp
2108 963b370f 2018-05-20 stsp static const struct got_error *
2109 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2110 27a741e5 2019-09-11 stsp int col_tab_align)
2111 5813d178 2019-03-09 stsp {
2112 e6b8b890 2020-12-29 naddy char *smallerthan;
2113 5813d178 2019-03-09 stsp
2114 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2115 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2116 5813d178 2019-03-09 stsp author = smallerthan + 1;
2117 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2118 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2119 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2120 5813d178 2019-03-09 stsp }
2121 5813d178 2019-03-09 stsp
2122 5813d178 2019-03-09 stsp static const struct got_error *
2123 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2124 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2125 8fdc79fe 2020-12-01 naddy int author_display_cols)
2126 80ddbec8 2018-04-29 stsp {
2127 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2128 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2129 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2130 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2131 5813d178 2019-03-09 stsp char *author = NULL;
2132 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2133 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2134 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2135 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2136 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2137 ccb26ccd 2018-11-05 stsp struct tm tm;
2138 45d799e2 2018-12-23 stsp time_t committer_time;
2139 11b20872 2019-11-08 stsp struct tog_color *tc;
2140 80ddbec8 2018-04-29 stsp
2141 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2142 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2143 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2144 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2145 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2146 b39d25c7 2018-07-10 stsp
2147 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2148 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2149 b39d25c7 2018-07-10 stsp else
2150 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2151 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2152 11b20872 2019-11-08 stsp if (tc)
2153 11b20872 2019-11-08 stsp wattr_on(view->window,
2154 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2155 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2156 11b20872 2019-11-08 stsp if (tc)
2157 11b20872 2019-11-08 stsp wattr_off(view->window,
2158 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2159 27a741e5 2019-09-11 stsp col = limit;
2160 b39d25c7 2018-07-10 stsp if (col > avail)
2161 b39d25c7 2018-07-10 stsp goto done;
2162 6570a66d 2019-11-08 stsp
2163 6570a66d 2019-11-08 stsp if (avail >= 120) {
2164 6570a66d 2019-11-08 stsp char *id_str;
2165 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2166 6570a66d 2019-11-08 stsp if (err)
2167 6570a66d 2019-11-08 stsp goto done;
2168 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2169 11b20872 2019-11-08 stsp if (tc)
2170 11b20872 2019-11-08 stsp wattr_on(view->window,
2171 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2172 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2173 11b20872 2019-11-08 stsp if (tc)
2174 11b20872 2019-11-08 stsp wattr_off(view->window,
2175 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2176 6570a66d 2019-11-08 stsp free(id_str);
2177 6570a66d 2019-11-08 stsp col += 9;
2178 6570a66d 2019-11-08 stsp if (col > avail)
2179 6570a66d 2019-11-08 stsp goto done;
2180 6570a66d 2019-11-08 stsp }
2181 b39d25c7 2018-07-10 stsp
2182 f69c5a46 2022-07-19 thomas if (s->use_committer)
2183 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2184 f69c5a46 2022-07-19 thomas else
2185 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2186 5813d178 2019-03-09 stsp if (author == NULL) {
2187 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2188 80ddbec8 2018-04-29 stsp goto done;
2189 80ddbec8 2018-04-29 stsp }
2190 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2191 bb737323 2018-05-20 stsp if (err)
2192 bb737323 2018-05-20 stsp goto done;
2193 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2194 11b20872 2019-11-08 stsp if (tc)
2195 11b20872 2019-11-08 stsp wattr_on(view->window,
2196 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2197 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2198 bb737323 2018-05-20 stsp col += author_width;
2199 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2200 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2201 bb737323 2018-05-20 stsp col++;
2202 bb737323 2018-05-20 stsp author_width++;
2203 bb737323 2018-05-20 stsp }
2204 f31d6c3b 2022-09-09 thomas if (tc)
2205 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2206 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2207 9c2eaf34 2018-05-20 stsp if (col > avail)
2208 9c2eaf34 2018-05-20 stsp goto done;
2209 80ddbec8 2018-04-29 stsp
2210 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2211 5943eee2 2019-08-13 stsp if (err)
2212 6d9fbc00 2018-04-29 stsp goto done;
2213 bb737323 2018-05-20 stsp logmsg = logmsg0;
2214 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2215 bb737323 2018-05-20 stsp logmsg++;
2216 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2217 bb737323 2018-05-20 stsp if (newline)
2218 bb737323 2018-05-20 stsp *newline = '\0';
2219 f91a2b48 2022-06-23 thomas limit = avail - col;
2220 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2221 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2222 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2223 f91a2b48 2022-06-23 thomas limit, col, 1);
2224 331b1a16 2022-06-23 thomas if (err)
2225 331b1a16 2022-06-23 thomas goto done;
2226 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2227 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2228 27a741e5 2019-09-11 stsp while (col < avail) {
2229 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2230 bb737323 2018-05-20 stsp col++;
2231 881b2d3e 2018-04-30 stsp }
2232 80ddbec8 2018-04-29 stsp done:
2233 80ddbec8 2018-04-29 stsp free(logmsg0);
2234 bb737323 2018-05-20 stsp free(wlogmsg);
2235 5813d178 2019-03-09 stsp free(author);
2236 bb737323 2018-05-20 stsp free(wauthor);
2237 80ddbec8 2018-04-29 stsp free(line);
2238 80ddbec8 2018-04-29 stsp return err;
2239 80ddbec8 2018-04-29 stsp }
2240 26ed57b2 2018-05-19 stsp
2241 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2242 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2243 899d86c2 2018-05-10 stsp struct got_object_id *id)
2244 80ddbec8 2018-04-29 stsp {
2245 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2246 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2247 80ddbec8 2018-04-29 stsp
2248 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2249 80ddbec8 2018-04-29 stsp if (entry == NULL)
2250 899d86c2 2018-05-10 stsp return NULL;
2251 99db9666 2018-05-07 stsp
2252 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2253 d68b9737 2022-09-05 thomas if (dup == NULL) {
2254 d68b9737 2022-09-05 thomas free(entry);
2255 d68b9737 2022-09-05 thomas return NULL;
2256 d68b9737 2022-09-05 thomas }
2257 d68b9737 2022-09-05 thomas
2258 d68b9737 2022-09-05 thomas entry->id = dup;
2259 99db9666 2018-05-07 stsp entry->commit = commit;
2260 899d86c2 2018-05-10 stsp return entry;
2261 99db9666 2018-05-07 stsp }
2262 80ddbec8 2018-04-29 stsp
2263 99db9666 2018-05-07 stsp static void
2264 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2265 99db9666 2018-05-07 stsp {
2266 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2267 99db9666 2018-05-07 stsp
2268 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2269 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2270 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2271 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2272 d68b9737 2022-09-05 thomas free(entry->id);
2273 99db9666 2018-05-07 stsp free(entry);
2274 99db9666 2018-05-07 stsp }
2275 99db9666 2018-05-07 stsp
2276 99db9666 2018-05-07 stsp static void
2277 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2278 99db9666 2018-05-07 stsp {
2279 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2280 99db9666 2018-05-07 stsp pop_commit(commits);
2281 c4972b91 2018-05-07 stsp }
2282 c4972b91 2018-05-07 stsp
2283 c4972b91 2018-05-07 stsp static const struct got_error *
2284 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2285 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2286 13add988 2019-10-15 stsp {
2287 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2288 13add988 2019-10-15 stsp regmatch_t regmatch;
2289 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2290 13add988 2019-10-15 stsp
2291 13add988 2019-10-15 stsp *have_match = 0;
2292 13add988 2019-10-15 stsp
2293 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2294 13add988 2019-10-15 stsp if (err)
2295 13add988 2019-10-15 stsp return err;
2296 13add988 2019-10-15 stsp
2297 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2298 13add988 2019-10-15 stsp if (err)
2299 13add988 2019-10-15 stsp goto done;
2300 13add988 2019-10-15 stsp
2301 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2302 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2303 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2304 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2305 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2306 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2307 13add988 2019-10-15 stsp *have_match = 1;
2308 13add988 2019-10-15 stsp done:
2309 13add988 2019-10-15 stsp free(id_str);
2310 13add988 2019-10-15 stsp free(logmsg);
2311 13add988 2019-10-15 stsp return err;
2312 13add988 2019-10-15 stsp }
2313 13add988 2019-10-15 stsp
2314 13add988 2019-10-15 stsp static const struct got_error *
2315 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2316 c4972b91 2018-05-07 stsp {
2317 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2318 9ba79e04 2018-06-11 stsp
2319 1a76625f 2018-10-22 stsp /*
2320 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2321 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2322 1a76625f 2018-10-22 stsp * while updating the display.
2323 1a76625f 2018-10-22 stsp */
2324 4e0d2870 2020-12-07 naddy do {
2325 7210b715 2022-09-11 thomas struct got_object_id id;
2326 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2327 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2328 7e8004ba 2022-09-11 thomas int limit_match = 0;
2329 1a76625f 2018-10-22 stsp int errcode;
2330 899d86c2 2018-05-10 stsp
2331 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2332 4e0d2870 2020-12-07 naddy NULL, NULL);
2333 7210b715 2022-09-11 thomas if (err)
2334 ecb28ae0 2018-07-16 stsp break;
2335 899d86c2 2018-05-10 stsp
2336 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2337 9ba79e04 2018-06-11 stsp if (err)
2338 9ba79e04 2018-06-11 stsp break;
2339 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2340 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2341 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2342 9ba79e04 2018-06-11 stsp break;
2343 9ba79e04 2018-06-11 stsp }
2344 93e45b7c 2018-09-24 stsp
2345 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2346 1a76625f 2018-10-22 stsp if (errcode) {
2347 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2348 13add988 2019-10-15 stsp "pthread_mutex_lock");
2349 1a76625f 2018-10-22 stsp break;
2350 1a76625f 2018-10-22 stsp }
2351 1a76625f 2018-10-22 stsp
2352 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2353 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2354 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2355 1a76625f 2018-10-22 stsp
2356 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2357 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2358 7e8004ba 2022-09-11 thomas a->limit_regex);
2359 7e8004ba 2022-09-11 thomas if (err)
2360 7e8004ba 2022-09-11 thomas break;
2361 7e8004ba 2022-09-11 thomas
2362 7e8004ba 2022-09-11 thomas if (limit_match) {
2363 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2364 7e8004ba 2022-09-11 thomas
2365 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2366 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2367 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2368 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2369 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2370 7e8004ba 2022-09-11 thomas break;
2371 7e8004ba 2022-09-11 thomas }
2372 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2373 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2374 7e8004ba 2022-09-11 thomas
2375 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2376 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2377 7e8004ba 2022-09-11 thomas matched, entry);
2378 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2379 7e8004ba 2022-09-11 thomas }
2380 7e8004ba 2022-09-11 thomas
2381 7e8004ba 2022-09-11 thomas /*
2382 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2383 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2384 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2385 7e8004ba 2022-09-11 thomas */
2386 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2387 7e8004ba 2022-09-11 thomas }
2388 7e8004ba 2022-09-11 thomas
2389 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2390 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2391 7c1452c1 2020-03-26 stsp int have_match;
2392 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2393 7c1452c1 2020-03-26 stsp if (err)
2394 7c1452c1 2020-03-26 stsp break;
2395 7e8004ba 2022-09-11 thomas
2396 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2397 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2398 7e8004ba 2022-09-11 thomas *a->search_next_done =
2399 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2400 7e8004ba 2022-09-11 thomas } else if (have_match)
2401 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2402 13add988 2019-10-15 stsp }
2403 13add988 2019-10-15 stsp
2404 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2405 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2406 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2407 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2408 7c1452c1 2020-03-26 stsp if (err)
2409 13add988 2019-10-15 stsp break;
2410 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2411 899d86c2 2018-05-10 stsp
2412 9ba79e04 2018-06-11 stsp return err;
2413 0553a4e3 2018-04-30 stsp }
2414 0553a4e3 2018-04-30 stsp
2415 2b779855 2020-12-05 naddy static void
2416 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2417 2b779855 2020-12-05 naddy {
2418 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2419 2b779855 2020-12-05 naddy int ncommits = 0;
2420 2b779855 2020-12-05 naddy
2421 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2422 2b779855 2020-12-05 naddy while (entry) {
2423 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2424 2b779855 2020-12-05 naddy s->selected_entry = entry;
2425 2b779855 2020-12-05 naddy break;
2426 2b779855 2020-12-05 naddy }
2427 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2428 2b779855 2020-12-05 naddy ncommits++;
2429 2b779855 2020-12-05 naddy }
2430 2b779855 2020-12-05 naddy }
2431 2b779855 2020-12-05 naddy
2432 0553a4e3 2018-04-30 stsp static const struct got_error *
2433 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2434 0553a4e3 2018-04-30 stsp {
2435 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2436 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2437 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2438 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2439 60493ae3 2019-06-20 stsp int width;
2440 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2441 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2442 8b473291 2019-02-21 stsp char *refs_str = NULL;
2443 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2444 11b20872 2019-11-08 stsp struct tog_color *tc;
2445 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2446 bd3f8225 2022-08-12 thomas
2447 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2448 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2449 0553a4e3 2018-04-30 stsp
2450 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2451 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2452 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2453 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2454 1a76625f 2018-10-22 stsp if (err)
2455 ecb28ae0 2018-07-16 stsp return err;
2456 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2457 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2458 d2075bf3 2020-12-25 stsp if (refs) {
2459 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2460 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2461 d2075bf3 2020-12-25 stsp if (err)
2462 d2075bf3 2020-12-25 stsp goto done;
2463 d2075bf3 2020-12-25 stsp }
2464 867c6645 2018-07-10 stsp }
2465 359bfafd 2019-02-22 stsp
2466 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2467 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2468 1a76625f 2018-10-22 stsp
2469 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2470 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2471 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2472 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2473 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2474 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2475 8f4ed634 2020-03-26 stsp goto done;
2476 8f4ed634 2020-03-26 stsp }
2477 8f4ed634 2020-03-26 stsp } else {
2478 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2479 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2480 f9686aa5 2020-03-27 stsp
2481 f9686aa5 2020-03-27 stsp if (view->searching) {
2482 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2483 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2484 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2485 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2486 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2487 f9686aa5 2020-03-27 stsp search_str = "searching...";
2488 f9686aa5 2020-03-27 stsp }
2489 f9686aa5 2020-03-27 stsp
2490 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2491 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2492 7e8004ba 2022-09-11 thomas
2493 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2494 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2495 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2496 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2497 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2498 8f4ed634 2020-03-26 stsp goto done;
2499 8f4ed634 2020-03-26 stsp }
2500 8b473291 2019-02-21 stsp }
2501 1a76625f 2018-10-22 stsp
2502 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2503 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2504 91198554 2022-06-23 thomas "........................................",
2505 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2506 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2507 1a76625f 2018-10-22 stsp header = NULL;
2508 1a76625f 2018-10-22 stsp goto done;
2509 1a76625f 2018-10-22 stsp }
2510 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2511 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2512 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2513 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2514 1a76625f 2018-10-22 stsp header = NULL;
2515 1a76625f 2018-10-22 stsp goto done;
2516 ecb28ae0 2018-07-16 stsp }
2517 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2518 1a76625f 2018-10-22 stsp if (err)
2519 1a76625f 2018-10-22 stsp goto done;
2520 867c6645 2018-07-10 stsp
2521 2814baeb 2018-08-01 stsp werase(view->window);
2522 867c6645 2018-07-10 stsp
2523 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2524 a3404814 2018-09-02 stsp wstandout(view->window);
2525 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2526 11b20872 2019-11-08 stsp if (tc)
2527 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2528 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2529 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2530 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2531 1a76625f 2018-10-22 stsp width++;
2532 1a76625f 2018-10-22 stsp }
2533 86f4aab9 2022-09-09 thomas if (tc)
2534 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2535 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2536 a3404814 2018-09-02 stsp wstandend(view->window);
2537 ecb28ae0 2018-07-16 stsp free(wline);
2538 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2539 1a76625f 2018-10-22 stsp goto done;
2540 0553a4e3 2018-04-30 stsp
2541 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2542 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2543 5813d178 2019-03-09 stsp ncommits = 0;
2544 05171be4 2022-06-23 thomas view->maxx = 0;
2545 5813d178 2019-03-09 stsp while (entry) {
2546 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2547 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2548 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2549 5813d178 2019-03-09 stsp int width;
2550 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2551 5813d178 2019-03-09 stsp break;
2552 f69c5a46 2022-07-19 thomas if (s->use_committer)
2553 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2554 f69c5a46 2022-07-19 thomas else
2555 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2556 5813d178 2019-03-09 stsp if (author == NULL) {
2557 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2558 5813d178 2019-03-09 stsp goto done;
2559 5813d178 2019-03-09 stsp }
2560 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2561 27a741e5 2019-09-11 stsp date_display_cols);
2562 5813d178 2019-03-09 stsp if (author_cols < width)
2563 5813d178 2019-03-09 stsp author_cols = width;
2564 5813d178 2019-03-09 stsp free(wauthor);
2565 5813d178 2019-03-09 stsp free(author);
2566 ef944b8b 2022-07-21 thomas if (err)
2567 ef944b8b 2022-07-21 thomas goto done;
2568 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2569 05171be4 2022-06-23 thomas if (err)
2570 05171be4 2022-06-23 thomas goto done;
2571 05171be4 2022-06-23 thomas msg = msg0;
2572 05171be4 2022-06-23 thomas while (*msg == '\n')
2573 05171be4 2022-06-23 thomas ++msg;
2574 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2575 331b1a16 2022-06-23 thomas *eol = '\0';
2576 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2577 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2578 331b1a16 2022-06-23 thomas if (err)
2579 331b1a16 2022-06-23 thomas goto done;
2580 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2581 05171be4 2022-06-23 thomas free(msg0);
2582 331b1a16 2022-06-23 thomas free(wmsg);
2583 7ca04879 2019-10-19 stsp ncommits++;
2584 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2585 5813d178 2019-03-09 stsp }
2586 5813d178 2019-03-09 stsp
2587 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2588 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2589 867c6645 2018-07-10 stsp ncommits = 0;
2590 899d86c2 2018-05-10 stsp while (entry) {
2591 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2592 899d86c2 2018-05-10 stsp break;
2593 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2594 2814baeb 2018-08-01 stsp wstandout(view->window);
2595 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2596 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2597 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2598 2814baeb 2018-08-01 stsp wstandend(view->window);
2599 0553a4e3 2018-04-30 stsp if (err)
2600 60493ae3 2019-06-20 stsp goto done;
2601 0553a4e3 2018-04-30 stsp ncommits++;
2602 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2603 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2604 80ddbec8 2018-04-29 stsp }
2605 80ddbec8 2018-04-29 stsp
2606 a5d43cac 2022-07-01 thomas view_border(view);
2607 1a76625f 2018-10-22 stsp done:
2608 1a76625f 2018-10-22 stsp free(id_str);
2609 8b473291 2019-02-21 stsp free(refs_str);
2610 1a76625f 2018-10-22 stsp free(ncommits_str);
2611 1a76625f 2018-10-22 stsp free(header);
2612 80ddbec8 2018-04-29 stsp return err;
2613 9f7d7167 2018-04-29 stsp }
2614 07b55e75 2018-05-10 stsp
2615 07b55e75 2018-05-10 stsp static void
2616 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2617 07b55e75 2018-05-10 stsp {
2618 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2619 07b55e75 2018-05-10 stsp int nscrolled = 0;
2620 07b55e75 2018-05-10 stsp
2621 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2622 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2623 07b55e75 2018-05-10 stsp return;
2624 9f7d7167 2018-04-29 stsp
2625 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2626 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2627 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2628 07b55e75 2018-05-10 stsp if (entry) {
2629 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2630 07b55e75 2018-05-10 stsp nscrolled++;
2631 07b55e75 2018-05-10 stsp }
2632 07b55e75 2018-05-10 stsp }
2633 aa075928 2018-05-10 stsp }
2634 aa075928 2018-05-10 stsp
2635 aa075928 2018-05-10 stsp static const struct got_error *
2636 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2637 aa075928 2018-05-10 stsp {
2638 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2639 5e224a3e 2019-02-22 stsp int errcode;
2640 8a42fca8 2019-02-22 stsp
2641 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2642 aa075928 2018-05-10 stsp
2643 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2644 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2645 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2646 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2647 7aafa0d1 2019-02-22 stsp if (errcode)
2648 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2649 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2650 7c1452c1 2020-03-26 stsp
2651 7c1452c1 2020-03-26 stsp /*
2652 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2653 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2654 7c1452c1 2020-03-26 stsp */
2655 7c1452c1 2020-03-26 stsp if (!wait)
2656 7c1452c1 2020-03-26 stsp break;
2657 7c1452c1 2020-03-26 stsp
2658 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2659 ffe38506 2020-12-01 naddy show_log_view(view);
2660 7c1452c1 2020-03-26 stsp update_panels();
2661 7c1452c1 2020-03-26 stsp doupdate();
2662 7c1452c1 2020-03-26 stsp
2663 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2664 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2665 82954512 2020-02-03 stsp if (errcode)
2666 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2667 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2668 82954512 2020-02-03 stsp
2669 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2670 ffe38506 2020-12-01 naddy show_log_view(view);
2671 7c1452c1 2020-03-26 stsp update_panels();
2672 7c1452c1 2020-03-26 stsp doupdate();
2673 5e224a3e 2019-02-22 stsp }
2674 5e224a3e 2019-02-22 stsp
2675 5e224a3e 2019-02-22 stsp return NULL;
2676 a5d43cac 2022-07-01 thomas }
2677 a5d43cac 2022-07-01 thomas
2678 a5d43cac 2022-07-01 thomas static const struct got_error *
2679 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2680 a5d43cac 2022-07-01 thomas {
2681 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2682 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2683 fe731b51 2022-07-14 thomas
2684 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2685 fe731b51 2022-07-14 thomas return NULL;
2686 a5d43cac 2022-07-01 thomas
2687 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2688 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2689 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2690 a5d43cac 2022-07-01 thomas
2691 a5d43cac 2022-07-01 thomas return err;
2692 5e224a3e 2019-02-22 stsp }
2693 5e224a3e 2019-02-22 stsp
2694 5e224a3e 2019-02-22 stsp static const struct got_error *
2695 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2696 5e224a3e 2019-02-22 stsp {
2697 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2698 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2699 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2700 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2701 5e224a3e 2019-02-22 stsp
2702 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2703 5e224a3e 2019-02-22 stsp return NULL;
2704 5e224a3e 2019-02-22 stsp
2705 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2706 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2707 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2708 08ebd0a9 2019-02-22 stsp /*
2709 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2710 08ebd0a9 2019-02-22 stsp */
2711 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2712 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2713 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2714 5e224a3e 2019-02-22 stsp if (err)
2715 5e224a3e 2019-02-22 stsp return err;
2716 7aafa0d1 2019-02-22 stsp }
2717 b295e71b 2019-02-22 stsp
2718 7aafa0d1 2019-02-22 stsp do {
2719 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2720 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2721 88048b54 2019-02-21 stsp break;
2722 88048b54 2019-02-21 stsp
2723 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2724 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2725 aa075928 2018-05-10 stsp
2726 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2727 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2728 dd0a52c1 2018-05-20 stsp break;
2729 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2730 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2731 aa075928 2018-05-10 stsp
2732 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2733 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2734 a5d43cac 2022-07-01 thomas else
2735 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2736 a5d43cac 2022-07-01 thomas
2737 dd0a52c1 2018-05-20 stsp return err;
2738 07b55e75 2018-05-10 stsp }
2739 4a7f7875 2018-05-10 stsp
2740 cd0acaa7 2018-05-20 stsp static const struct got_error *
2741 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2742 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2743 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2744 cd0acaa7 2018-05-20 stsp {
2745 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2746 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2747 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2748 cd0acaa7 2018-05-20 stsp
2749 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2750 15a94983 2018-12-23 stsp if (diff_view == NULL)
2751 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2752 ea5e7bb5 2018-08-01 stsp
2753 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2754 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2755 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2756 e5a0f69f 2018-08-18 stsp if (err == NULL)
2757 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2758 cd0acaa7 2018-05-20 stsp return err;
2759 4a7f7875 2018-05-10 stsp }
2760 4a7f7875 2018-05-10 stsp
2761 80ddbec8 2018-04-29 stsp static const struct got_error *
2762 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2763 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2764 9343a5fb 2018-06-23 stsp {
2765 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2766 941e9f74 2019-05-21 stsp
2767 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2768 941e9f74 2019-05-21 stsp if (parent == NULL)
2769 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2770 941e9f74 2019-05-21 stsp
2771 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2772 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2773 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2774 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2775 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2776 941e9f74 2019-05-21 stsp s->tree = subtree;
2777 941e9f74 2019-05-21 stsp s->selected = 0;
2778 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2779 941e9f74 2019-05-21 stsp return NULL;
2780 941e9f74 2019-05-21 stsp }
2781 941e9f74 2019-05-21 stsp
2782 941e9f74 2019-05-21 stsp static const struct got_error *
2783 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2784 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2785 941e9f74 2019-05-21 stsp {
2786 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2787 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2788 941e9f74 2019-05-21 stsp const char *p;
2789 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2790 9343a5fb 2018-06-23 stsp
2791 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2792 941e9f74 2019-05-21 stsp p = path;
2793 941e9f74 2019-05-21 stsp while (*p) {
2794 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2795 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2796 56e0773d 2019-11-28 stsp char *te_name;
2797 33cbf02b 2020-01-12 stsp
2798 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2799 33cbf02b 2020-01-12 stsp p++;
2800 941e9f74 2019-05-21 stsp
2801 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2802 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2803 941e9f74 2019-05-21 stsp if (slash == NULL)
2804 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2805 33cbf02b 2020-01-12 stsp else
2806 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2807 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2808 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2809 56e0773d 2019-11-28 stsp break;
2810 941e9f74 2019-05-21 stsp }
2811 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2812 56e0773d 2019-11-28 stsp if (te == NULL) {
2813 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2814 56e0773d 2019-11-28 stsp free(te_name);
2815 941e9f74 2019-05-21 stsp break;
2816 941e9f74 2019-05-21 stsp }
2817 56e0773d 2019-11-28 stsp free(te_name);
2818 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2819 941e9f74 2019-05-21 stsp
2820 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2821 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2822 b03c880f 2019-05-21 stsp
2823 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2824 941e9f74 2019-05-21 stsp if (slash)
2825 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2826 941e9f74 2019-05-21 stsp else
2827 941e9f74 2019-05-21 stsp subpath = strdup(path);
2828 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2829 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2830 941e9f74 2019-05-21 stsp break;
2831 941e9f74 2019-05-21 stsp }
2832 941e9f74 2019-05-21 stsp
2833 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2834 941e9f74 2019-05-21 stsp subpath);
2835 941e9f74 2019-05-21 stsp if (err)
2836 941e9f74 2019-05-21 stsp break;
2837 941e9f74 2019-05-21 stsp
2838 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2839 941e9f74 2019-05-21 stsp free(tree_id);
2840 941e9f74 2019-05-21 stsp if (err)
2841 941e9f74 2019-05-21 stsp break;
2842 941e9f74 2019-05-21 stsp
2843 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2844 941e9f74 2019-05-21 stsp if (err) {
2845 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2846 941e9f74 2019-05-21 stsp break;
2847 941e9f74 2019-05-21 stsp }
2848 941e9f74 2019-05-21 stsp if (slash == NULL)
2849 941e9f74 2019-05-21 stsp break;
2850 941e9f74 2019-05-21 stsp free(subpath);
2851 941e9f74 2019-05-21 stsp subpath = NULL;
2852 941e9f74 2019-05-21 stsp p = slash;
2853 941e9f74 2019-05-21 stsp }
2854 941e9f74 2019-05-21 stsp
2855 941e9f74 2019-05-21 stsp free(subpath);
2856 1a76625f 2018-10-22 stsp return err;
2857 61266923 2020-01-14 stsp }
2858 61266923 2020-01-14 stsp
2859 61266923 2020-01-14 stsp static const struct got_error *
2860 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2861 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2862 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2863 55cccc34 2020-02-20 stsp {
2864 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2865 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2866 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2867 55cccc34 2020-02-20 stsp
2868 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2869 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2870 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2871 55cccc34 2020-02-20 stsp
2872 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2873 bc573f3b 2021-07-10 stsp if (err)
2874 55cccc34 2020-02-20 stsp return err;
2875 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2876 55cccc34 2020-02-20 stsp
2877 55cccc34 2020-02-20 stsp *new_view = tree_view;
2878 55cccc34 2020-02-20 stsp
2879 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2880 55cccc34 2020-02-20 stsp return NULL;
2881 55cccc34 2020-02-20 stsp
2882 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2883 55cccc34 2020-02-20 stsp }
2884 55cccc34 2020-02-20 stsp
2885 55cccc34 2020-02-20 stsp static const struct got_error *
2886 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2887 61266923 2020-01-14 stsp {
2888 61266923 2020-01-14 stsp sigset_t sigset;
2889 61266923 2020-01-14 stsp int errcode;
2890 61266923 2020-01-14 stsp
2891 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2892 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2893 61266923 2020-01-14 stsp
2894 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2895 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2896 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2897 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2898 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2899 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2900 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2901 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2902 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2903 61266923 2020-01-14 stsp
2904 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2905 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2906 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2907 61266923 2020-01-14 stsp
2908 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2909 61266923 2020-01-14 stsp if (errcode)
2910 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2911 61266923 2020-01-14 stsp
2912 61266923 2020-01-14 stsp return NULL;
2913 1a76625f 2018-10-22 stsp }
2914 1a76625f 2018-10-22 stsp
2915 1a76625f 2018-10-22 stsp static void *
2916 1a76625f 2018-10-22 stsp log_thread(void *arg)
2917 1a76625f 2018-10-22 stsp {
2918 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2919 1a76625f 2018-10-22 stsp int errcode = 0;
2920 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2921 1a76625f 2018-10-22 stsp int done = 0;
2922 61266923 2020-01-14 stsp
2923 f2d749db 2022-07-12 thomas /*
2924 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2925 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2926 f2d749db 2022-07-12 thomas */
2927 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2928 f2d749db 2022-07-12 thomas if (errcode) {
2929 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2930 61266923 2020-01-14 stsp return (void *)err;
2931 f2d749db 2022-07-12 thomas }
2932 1a76625f 2018-10-22 stsp
2933 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2934 f2d749db 2022-07-12 thomas if (err) {
2935 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2936 f2d749db 2022-07-12 thomas goto done;
2937 f2d749db 2022-07-12 thomas }
2938 f2d749db 2022-07-12 thomas
2939 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2940 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2941 f2d749db 2022-07-12 thomas if (errcode) {
2942 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2943 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2944 f2d749db 2022-07-12 thomas goto done;
2945 f2d749db 2022-07-12 thomas }
2946 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2947 1a76625f 2018-10-22 stsp if (err) {
2948 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2949 f2d749db 2022-07-12 thomas goto done;
2950 1a76625f 2018-10-22 stsp err = NULL;
2951 1a76625f 2018-10-22 stsp done = 1;
2952 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
2953 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2954 7e8004ba 2022-09-11 thomas if (a->limit_match)
2955 7e8004ba 2022-09-11 thomas a->commits_needed--;
2956 7e8004ba 2022-09-11 thomas } else
2957 7e8004ba 2022-09-11 thomas a->commits_needed--;
2958 7e8004ba 2022-09-11 thomas }
2959 1a76625f 2018-10-22 stsp
2960 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2961 3abe8080 2019-04-10 stsp if (errcode) {
2962 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2963 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2964 f2d749db 2022-07-12 thomas goto done;
2965 3abe8080 2019-04-10 stsp } else if (*a->quit)
2966 1a76625f 2018-10-22 stsp done = 1;
2967 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
2968 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2969 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
2970 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2971 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
2972 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
2973 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
2974 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
2975 1a76625f 2018-10-22 stsp }
2976 1a76625f 2018-10-22 stsp
2977 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2978 7c1452c1 2020-03-26 stsp if (errcode) {
2979 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2980 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2981 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2982 f2d749db 2022-07-12 thomas goto done;
2983 7c1452c1 2020-03-26 stsp }
2984 7c1452c1 2020-03-26 stsp
2985 1a76625f 2018-10-22 stsp if (done)
2986 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2987 7c1452c1 2020-03-26 stsp else {
2988 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2989 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2990 7c1452c1 2020-03-26 stsp &tog_mutex);
2991 f2d749db 2022-07-12 thomas if (errcode) {
2992 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2993 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2994 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2995 f2d749db 2022-07-12 thomas goto done;
2996 f2d749db 2022-07-12 thomas }
2997 21355643 2020-12-06 stsp if (*a->quit)
2998 21355643 2020-12-06 stsp done = 1;
2999 7c1452c1 2020-03-26 stsp }
3000 1a76625f 2018-10-22 stsp }
3001 1a76625f 2018-10-22 stsp }
3002 3abe8080 2019-04-10 stsp a->log_complete = 1;
3003 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3004 f2d749db 2022-07-12 thomas if (errcode)
3005 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3006 f2d749db 2022-07-12 thomas done:
3007 f2d749db 2022-07-12 thomas if (err) {
3008 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3009 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3010 f2d749db 2022-07-12 thomas }
3011 1a76625f 2018-10-22 stsp return (void *)err;
3012 1a76625f 2018-10-22 stsp }
3013 1a76625f 2018-10-22 stsp
3014 1a76625f 2018-10-22 stsp static const struct got_error *
3015 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3016 1a76625f 2018-10-22 stsp {
3017 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3018 1a76625f 2018-10-22 stsp int errcode;
3019 1a76625f 2018-10-22 stsp
3020 1a76625f 2018-10-22 stsp if (s->thread) {
3021 1a76625f 2018-10-22 stsp s->quit = 1;
3022 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3023 1a76625f 2018-10-22 stsp if (errcode)
3024 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3025 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3026 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3027 1a76625f 2018-10-22 stsp if (errcode)
3028 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3029 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3030 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3031 1a76625f 2018-10-22 stsp if (errcode)
3032 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3033 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3034 1a76625f 2018-10-22 stsp if (errcode)
3035 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3036 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3037 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3038 1a76625f 2018-10-22 stsp }
3039 1a76625f 2018-10-22 stsp
3040 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3041 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3042 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3043 1af5eddf 2022-06-23 thomas }
3044 1af5eddf 2022-06-23 thomas
3045 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3046 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3047 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3048 1af5eddf 2022-06-23 thomas if (err == NULL)
3049 1af5eddf 2022-06-23 thomas err = pack_err;
3050 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3051 1a76625f 2018-10-22 stsp }
3052 1a76625f 2018-10-22 stsp
3053 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3054 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3055 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3056 1a76625f 2018-10-22 stsp }
3057 1a76625f 2018-10-22 stsp
3058 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3059 9343a5fb 2018-06-23 stsp }
3060 9343a5fb 2018-06-23 stsp
3061 9343a5fb 2018-06-23 stsp static const struct got_error *
3062 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3063 1a76625f 2018-10-22 stsp {
3064 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3065 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3066 276b94a1 2020-11-13 naddy int errcode;
3067 1a76625f 2018-10-22 stsp
3068 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3069 276b94a1 2020-11-13 naddy
3070 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3071 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3072 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3073 276b94a1 2020-11-13 naddy
3074 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3075 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3076 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3077 276b94a1 2020-11-13 naddy
3078 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3079 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3080 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3081 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3082 1a76625f 2018-10-22 stsp free(s->start_id);
3083 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3084 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3085 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3086 1a76625f 2018-10-22 stsp return err;
3087 1a76625f 2018-10-22 stsp }
3088 1a76625f 2018-10-22 stsp
3089 7e8004ba 2022-09-11 thomas /*
3090 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3091 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3092 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3093 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3094 7e8004ba 2022-09-11 thomas * works with very slight change.
3095 7e8004ba 2022-09-11 thomas */
3096 1a76625f 2018-10-22 stsp static const struct got_error *
3097 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3098 7e8004ba 2022-09-11 thomas {
3099 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3100 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3101 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3102 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3103 7e8004ba 2022-09-11 thomas char pattern[1024];
3104 7e8004ba 2022-09-11 thomas int ret;
3105 7e8004ba 2022-09-11 thomas
3106 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3107 7e8004ba 2022-09-11 thomas v = view->child;
3108 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3109 7e8004ba 2022-09-11 thomas v = view->parent;
3110 7e8004ba 2022-09-11 thomas
3111 7e8004ba 2022-09-11 thomas /* Get the pattern */
3112 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3113 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3114 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3115 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3116 7e8004ba 2022-09-11 thomas nocbreak();
3117 7e8004ba 2022-09-11 thomas echo();
3118 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3119 7e8004ba 2022-09-11 thomas cbreak();
3120 7e8004ba 2022-09-11 thomas noecho();
3121 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3122 7e8004ba 2022-09-11 thomas if (ret == ERR)
3123 7e8004ba 2022-09-11 thomas return NULL;
3124 7e8004ba 2022-09-11 thomas
3125 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3126 7e8004ba 2022-09-11 thomas /*
3127 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3128 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3129 7e8004ba 2022-09-11 thomas */
3130 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3131 7e8004ba 2022-09-11 thomas return NULL;
3132 7e8004ba 2022-09-11 thomas
3133 7e8004ba 2022-09-11 thomas /*
3134 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3135 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3136 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3137 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3138 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3139 7e8004ba 2022-09-11 thomas * the queue.
3140 7e8004ba 2022-09-11 thomas */
3141 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3142 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3143 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3144 7e8004ba 2022-09-11 thomas s->selected = 0;
3145 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3146 7e8004ba 2022-09-11 thomas
3147 7e8004ba 2022-09-11 thomas return NULL;
3148 7e8004ba 2022-09-11 thomas }
3149 7e8004ba 2022-09-11 thomas
3150 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3151 7e8004ba 2022-09-11 thomas return NULL;
3152 7e8004ba 2022-09-11 thomas
3153 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3154 7e8004ba 2022-09-11 thomas
3155 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3156 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3157 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3158 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3159 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3160 7e8004ba 2022-09-11 thomas
3161 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3162 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3163 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3164 7e8004ba 2022-09-11 thomas
3165 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3166 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3167 7e8004ba 2022-09-11 thomas int have_match = 0;
3168 7e8004ba 2022-09-11 thomas
3169 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3170 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3171 7e8004ba 2022-09-11 thomas if (err)
3172 7e8004ba 2022-09-11 thomas return err;
3173 7e8004ba 2022-09-11 thomas
3174 7e8004ba 2022-09-11 thomas if (have_match) {
3175 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3176 7e8004ba 2022-09-11 thomas
3177 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3178 7e8004ba 2022-09-11 thomas entry->id);
3179 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3180 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3181 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3182 7e8004ba 2022-09-11 thomas break;
3183 7e8004ba 2022-09-11 thomas }
3184 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3185 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3186 7e8004ba 2022-09-11 thomas
3187 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3188 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3189 7e8004ba 2022-09-11 thomas matched, entry);
3190 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3191 7e8004ba 2022-09-11 thomas }
3192 7e8004ba 2022-09-11 thomas }
3193 7e8004ba 2022-09-11 thomas
3194 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3195 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3196 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3197 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3198 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3199 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3200 7e8004ba 2022-09-11 thomas if (err)
3201 7e8004ba 2022-09-11 thomas return err;
3202 7e8004ba 2022-09-11 thomas }
3203 7e8004ba 2022-09-11 thomas
3204 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3205 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3206 7e8004ba 2022-09-11 thomas s->selected = 0;
3207 7e8004ba 2022-09-11 thomas
3208 7e8004ba 2022-09-11 thomas return NULL;
3209 7e8004ba 2022-09-11 thomas }
3210 7e8004ba 2022-09-11 thomas
3211 7e8004ba 2022-09-11 thomas static const struct got_error *
3212 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3213 60493ae3 2019-06-20 stsp {
3214 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3215 60493ae3 2019-06-20 stsp
3216 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3217 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3218 60493ae3 2019-06-20 stsp return NULL;
3219 60493ae3 2019-06-20 stsp }
3220 60493ae3 2019-06-20 stsp
3221 60493ae3 2019-06-20 stsp static const struct got_error *
3222 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3223 60493ae3 2019-06-20 stsp {
3224 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3225 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3226 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3227 60493ae3 2019-06-20 stsp
3228 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3229 f9686aa5 2020-03-27 stsp show_log_view(view);
3230 f9686aa5 2020-03-27 stsp update_panels();
3231 f9686aa5 2020-03-27 stsp doupdate();
3232 f9686aa5 2020-03-27 stsp
3233 96e2b566 2019-07-08 stsp if (s->search_entry) {
3234 13add988 2019-10-15 stsp int errcode, ch;
3235 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3236 13add988 2019-10-15 stsp if (errcode)
3237 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3238 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3239 13add988 2019-10-15 stsp ch = wgetch(view->window);
3240 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3241 13add988 2019-10-15 stsp if (errcode)
3242 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3243 13add988 2019-10-15 stsp "pthread_mutex_lock");
3244 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3245 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3246 678cbce5 2019-07-28 stsp return NULL;
3247 678cbce5 2019-07-28 stsp }
3248 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3249 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3250 96e2b566 2019-07-08 stsp else
3251 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3252 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3253 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3254 df5e841d 2022-06-23 thomas /*
3255 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3256 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3257 1f4d5162 2022-06-23 thomas * might have changed.
3258 df5e841d 2022-06-23 thomas */
3259 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3260 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3261 e7baaec8 2022-09-13 thomas else
3262 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3263 e7baaec8 2022-09-13 thomas entry);
3264 20be8d96 2019-06-21 stsp } else {
3265 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3266 20be8d96 2019-06-21 stsp }
3267 60493ae3 2019-06-20 stsp
3268 60493ae3 2019-06-20 stsp while (1) {
3269 13add988 2019-10-15 stsp int have_match = 0;
3270 13add988 2019-10-15 stsp
3271 60493ae3 2019-06-20 stsp if (entry == NULL) {
3272 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3273 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3274 f9967bca 2020-03-27 stsp view->search_next_done =
3275 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3276 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3277 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3278 f801134a 2019-06-25 stsp return NULL;
3279 60493ae3 2019-06-20 stsp }
3280 96e2b566 2019-07-08 stsp /*
3281 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3282 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3283 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3284 96e2b566 2019-07-08 stsp */
3285 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3286 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3287 60493ae3 2019-06-20 stsp }
3288 60493ae3 2019-06-20 stsp
3289 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3290 13add988 2019-10-15 stsp &view->regex);
3291 5943eee2 2019-08-13 stsp if (err)
3292 13add988 2019-10-15 stsp break;
3293 13add988 2019-10-15 stsp if (have_match) {
3294 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3295 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3296 60493ae3 2019-06-20 stsp break;
3297 60493ae3 2019-06-20 stsp }
3298 13add988 2019-10-15 stsp
3299 96e2b566 2019-07-08 stsp s->search_entry = entry;
3300 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3301 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3302 b1bf1435 2019-06-21 stsp else
3303 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3304 60493ae3 2019-06-20 stsp }
3305 60493ae3 2019-06-20 stsp
3306 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3307 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3308 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3309 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3310 60493ae3 2019-06-20 stsp if (err)
3311 60493ae3 2019-06-20 stsp return err;
3312 ead14cbe 2019-06-21 stsp cur++;
3313 ead14cbe 2019-06-21 stsp }
3314 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3315 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3316 60493ae3 2019-06-20 stsp if (err)
3317 60493ae3 2019-06-20 stsp return err;
3318 ead14cbe 2019-06-21 stsp cur--;
3319 60493ae3 2019-06-20 stsp }
3320 60493ae3 2019-06-20 stsp }
3321 60493ae3 2019-06-20 stsp
3322 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3323 96e2b566 2019-07-08 stsp
3324 60493ae3 2019-06-20 stsp return NULL;
3325 60493ae3 2019-06-20 stsp }
3326 60493ae3 2019-06-20 stsp
3327 60493ae3 2019-06-20 stsp static const struct got_error *
3328 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3329 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3330 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3331 80ddbec8 2018-04-29 stsp {
3332 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3333 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3334 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3335 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3336 1a76625f 2018-10-22 stsp int errcode;
3337 80ddbec8 2018-04-29 stsp
3338 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3339 f135c941 2020-02-20 stsp free(s->in_repo_path);
3340 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3341 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3342 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3343 f135c941 2020-02-20 stsp }
3344 ecb28ae0 2018-07-16 stsp
3345 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3346 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3347 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3348 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3349 78756c87 2020-11-24 stsp
3350 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3351 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3352 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3353 7e8004ba 2022-09-11 thomas
3354 fb2756b9 2018-08-04 stsp s->repo = repo;
3355 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3356 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3357 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3358 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3359 9cd7cbd1 2020-12-07 stsp goto done;
3360 9cd7cbd1 2020-12-07 stsp }
3361 9cd7cbd1 2020-12-07 stsp }
3362 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3363 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3364 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3365 5036bf37 2018-09-24 stsp goto done;
3366 5036bf37 2018-09-24 stsp }
3367 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3368 e5a0f69f 2018-08-18 stsp
3369 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3370 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3371 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3372 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3373 11b20872 2019-11-08 stsp if (err)
3374 11b20872 2019-11-08 stsp goto done;
3375 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3376 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3377 11b20872 2019-11-08 stsp if (err) {
3378 11b20872 2019-11-08 stsp free_colors(&s->colors);
3379 11b20872 2019-11-08 stsp goto done;
3380 11b20872 2019-11-08 stsp }
3381 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3382 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3383 11b20872 2019-11-08 stsp if (err) {
3384 11b20872 2019-11-08 stsp free_colors(&s->colors);
3385 11b20872 2019-11-08 stsp goto done;
3386 11b20872 2019-11-08 stsp }
3387 11b20872 2019-11-08 stsp }
3388 11b20872 2019-11-08 stsp
3389 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3390 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3391 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3392 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3393 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3394 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3395 1a76625f 2018-10-22 stsp
3396 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3397 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3398 1af5eddf 2022-06-23 thomas if (err)
3399 1af5eddf 2022-06-23 thomas goto done;
3400 1af5eddf 2022-06-23 thomas }
3401 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3402 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3403 7cd52833 2022-06-23 thomas if (err)
3404 7cd52833 2022-06-23 thomas goto done;
3405 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3406 b672a97a 2020-01-27 stsp !s->log_branches);
3407 1a76625f 2018-10-22 stsp if (err)
3408 1a76625f 2018-10-22 stsp goto done;
3409 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3410 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3411 c5b78334 2020-01-12 stsp if (err)
3412 c5b78334 2020-01-12 stsp goto done;
3413 1a76625f 2018-10-22 stsp
3414 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3415 1a76625f 2018-10-22 stsp if (errcode) {
3416 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3417 1a76625f 2018-10-22 stsp goto done;
3418 1a76625f 2018-10-22 stsp }
3419 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3420 7c1452c1 2020-03-26 stsp if (errcode) {
3421 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3422 7c1452c1 2020-03-26 stsp goto done;
3423 7c1452c1 2020-03-26 stsp }
3424 1a76625f 2018-10-22 stsp
3425 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3426 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3427 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3428 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3429 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3430 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3431 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3432 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3433 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3434 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3435 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3436 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3437 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3438 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3439 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3440 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3441 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3442 ba4f502b 2018-08-04 stsp done:
3443 1a76625f 2018-10-22 stsp if (err)
3444 1a76625f 2018-10-22 stsp close_log_view(view);
3445 ba4f502b 2018-08-04 stsp return err;
3446 ba4f502b 2018-08-04 stsp }
3447 ba4f502b 2018-08-04 stsp
3448 e5a0f69f 2018-08-18 stsp static const struct got_error *
3449 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3450 ba4f502b 2018-08-04 stsp {
3451 f2f6d207 2020-11-24 stsp const struct got_error *err;
3452 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3453 ba4f502b 2018-08-04 stsp
3454 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3455 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3456 2b380cc8 2018-10-24 stsp &s->thread_args);
3457 2b380cc8 2018-10-24 stsp if (errcode)
3458 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3459 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3460 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3461 f2f6d207 2020-11-24 stsp if (err)
3462 f2f6d207 2020-11-24 stsp return err;
3463 f2f6d207 2020-11-24 stsp }
3464 2b380cc8 2018-10-24 stsp }
3465 2b380cc8 2018-10-24 stsp
3466 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3467 b31f89ff 2022-07-01 thomas }
3468 b31f89ff 2022-07-01 thomas
3469 b31f89ff 2022-07-01 thomas static void
3470 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3471 b31f89ff 2022-07-01 thomas {
3472 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3473 b31f89ff 2022-07-01 thomas
3474 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3475 b31f89ff 2022-07-01 thomas return;
3476 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3477 7e8004ba 2022-09-11 thomas view->count = 0;
3478 b31f89ff 2022-07-01 thomas
3479 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3480 b31f89ff 2022-07-01 thomas || home)
3481 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3482 b31f89ff 2022-07-01 thomas
3483 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3484 b31f89ff 2022-07-01 thomas --s->selected;
3485 b31f89ff 2022-07-01 thomas else
3486 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3487 b31f89ff 2022-07-01 thomas
3488 b31f89ff 2022-07-01 thomas select_commit(s);
3489 b31f89ff 2022-07-01 thomas return;
3490 b31f89ff 2022-07-01 thomas }
3491 b31f89ff 2022-07-01 thomas
3492 b31f89ff 2022-07-01 thomas static const struct got_error *
3493 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3494 b31f89ff 2022-07-01 thomas {
3495 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3496 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3497 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3498 b31f89ff 2022-07-01 thomas
3499 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3500 7e8004ba 2022-09-11 thomas return NULL;
3501 7e8004ba 2022-09-11 thomas
3502 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3503 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3504 b31f89ff 2022-07-01 thomas return NULL;
3505 b31f89ff 2022-07-01 thomas
3506 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3507 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3508 b31f89ff 2022-07-01 thomas
3509 7ed048bd 2022-08-12 thomas if (!page) {
3510 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3511 b31f89ff 2022-07-01 thomas ++s->selected;
3512 b31f89ff 2022-07-01 thomas else
3513 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3514 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3515 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3516 7ed048bd 2022-08-12 thomas int n;
3517 7ed048bd 2022-08-12 thomas
3518 7ed048bd 2022-08-12 thomas s->selected = 0;
3519 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3520 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3521 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3522 7ed048bd 2022-08-12 thomas if (entry == NULL)
3523 7ed048bd 2022-08-12 thomas break;
3524 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3525 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3526 7ed048bd 2022-08-12 thomas }
3527 7ed048bd 2022-08-12 thomas if (n > 0)
3528 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3529 b31f89ff 2022-07-01 thomas } else {
3530 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3531 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3532 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3533 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3534 bd3f8225 2022-08-12 thomas else
3535 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3536 b31f89ff 2022-07-01 thomas }
3537 b31f89ff 2022-07-01 thomas if (err)
3538 b31f89ff 2022-07-01 thomas return err;
3539 b31f89ff 2022-07-01 thomas
3540 a5d43cac 2022-07-01 thomas /*
3541 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3542 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3543 a5d43cac 2022-07-01 thomas */
3544 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3545 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3546 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3547 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3548 25100026 2022-08-13 thomas }
3549 a5d43cac 2022-07-01 thomas
3550 b31f89ff 2022-07-01 thomas select_commit(s);
3551 b31f89ff 2022-07-01 thomas
3552 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3553 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3554 b31f89ff 2022-07-01 thomas view->count = 0;
3555 b31f89ff 2022-07-01 thomas
3556 b31f89ff 2022-07-01 thomas return NULL;
3557 e5a0f69f 2018-08-18 stsp }
3558 04cc582a 2018-08-01 stsp
3559 a5d43cac 2022-07-01 thomas static void
3560 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3561 a5d43cac 2022-07-01 thomas {
3562 24415785 2022-07-03 thomas *x = 0;
3563 24415785 2022-07-03 thomas *y = 0;
3564 24415785 2022-07-03 thomas
3565 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3566 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3567 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3568 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3569 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3570 53d2bdd3 2022-07-10 thomas else
3571 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3572 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3573 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3574 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3575 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3576 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3577 53d2bdd3 2022-07-10 thomas else
3578 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3579 53d2bdd3 2022-07-10 thomas }
3580 a5d43cac 2022-07-01 thomas }
3581 a5d43cac 2022-07-01 thomas
3582 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3583 e5a0f69f 2018-08-18 stsp static const struct got_error *
3584 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3585 a5d43cac 2022-07-01 thomas {
3586 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3587 a5d43cac 2022-07-01 thomas
3588 a5d43cac 2022-07-01 thomas view->nlines = y;
3589 64486692 2022-07-07 thomas view->ncols = COLS;
3590 a5d43cac 2022-07-01 thomas err = view_resize(view);
3591 a5d43cac 2022-07-01 thomas if (err)
3592 a5d43cac 2022-07-01 thomas return err;
3593 a5d43cac 2022-07-01 thomas
3594 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3595 a5d43cac 2022-07-01 thomas
3596 a5d43cac 2022-07-01 thomas return err;
3597 07dd3ed3 2022-08-06 thomas }
3598 07dd3ed3 2022-08-06 thomas
3599 07dd3ed3 2022-08-06 thomas static const struct got_error *
3600 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3601 07dd3ed3 2022-08-06 thomas {
3602 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3603 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3604 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3605 25100026 2022-08-13 thomas
3606 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3607 25100026 2022-08-13 thomas return NULL;
3608 07dd3ed3 2022-08-06 thomas
3609 07dd3ed3 2022-08-06 thomas g = view->gline;
3610 07dd3ed3 2022-08-06 thomas view->gline = 0;
3611 07dd3ed3 2022-08-06 thomas
3612 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3613 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3614 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3615 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3616 07dd3ed3 2022-08-06 thomas select_commit(s);
3617 07dd3ed3 2022-08-06 thomas return NULL;
3618 07dd3ed3 2022-08-06 thomas }
3619 07dd3ed3 2022-08-06 thomas
3620 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3621 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3622 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3623 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3624 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3625 07dd3ed3 2022-08-06 thomas if (err)
3626 07dd3ed3 2022-08-06 thomas return err;
3627 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3628 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3629 07dd3ed3 2022-08-06 thomas
3630 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3631 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3632 07dd3ed3 2022-08-06 thomas
3633 07dd3ed3 2022-08-06 thomas select_commit(s);
3634 07dd3ed3 2022-08-06 thomas return NULL;
3635 07dd3ed3 2022-08-06 thomas
3636 a5d43cac 2022-07-01 thomas }
3637 a5d43cac 2022-07-01 thomas
3638 a5d43cac 2022-07-01 thomas static const struct got_error *
3639 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3640 e5a0f69f 2018-08-18 stsp {
3641 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3642 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3643 7ed048bd 2022-08-12 thomas int eos, nscroll;
3644 80ddbec8 2018-04-29 stsp
3645 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3646 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3647 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3648 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3649 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3650 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3651 fb280deb 2021-08-30 stsp }
3652 c0be8933 2022-08-12 thomas if (err)
3653 c0be8933 2022-08-12 thomas return err;
3654 528dedf3 2021-08-30 stsp }
3655 068ab281 2022-07-01 thomas
3656 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3657 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3658 068ab281 2022-07-01 thomas --eos; /* border */
3659 07dd3ed3 2022-08-06 thomas
3660 07dd3ed3 2022-08-06 thomas if (view->gline)
3661 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3662 068ab281 2022-07-01 thomas
3663 528dedf3 2021-08-30 stsp switch (ch) {
3664 7e8004ba 2022-09-11 thomas case '&':
3665 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3666 7e8004ba 2022-09-11 thomas break;
3667 1e37a5c2 2019-05-12 jcs case 'q':
3668 1e37a5c2 2019-05-12 jcs s->quit = 1;
3669 05171be4 2022-06-23 thomas break;
3670 05171be4 2022-06-23 thomas case '0':
3671 05171be4 2022-06-23 thomas view->x = 0;
3672 05171be4 2022-06-23 thomas break;
3673 05171be4 2022-06-23 thomas case '$':
3674 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3675 07b0611c 2022-06-23 thomas view->count = 0;
3676 05171be4 2022-06-23 thomas break;
3677 05171be4 2022-06-23 thomas case KEY_RIGHT:
3678 05171be4 2022-06-23 thomas case 'l':
3679 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3680 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3681 07b0611c 2022-06-23 thomas else
3682 07b0611c 2022-06-23 thomas view->count = 0;
3683 1e37a5c2 2019-05-12 jcs break;
3684 05171be4 2022-06-23 thomas case KEY_LEFT:
3685 05171be4 2022-06-23 thomas case 'h':
3686 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3687 07b0611c 2022-06-23 thomas if (view->x <= 0)
3688 07b0611c 2022-06-23 thomas view->count = 0;
3689 05171be4 2022-06-23 thomas break;
3690 1e37a5c2 2019-05-12 jcs case 'k':
3691 1e37a5c2 2019-05-12 jcs case KEY_UP:
3692 1e37a5c2 2019-05-12 jcs case '<':
3693 1e37a5c2 2019-05-12 jcs case ',':
3694 f7140bf5 2021-10-17 thomas case CTRL('p'):
3695 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3696 912a3f79 2021-08-30 j break;
3697 912a3f79 2021-08-30 j case 'g':
3698 912a3f79 2021-08-30 j case KEY_HOME:
3699 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3700 07b0611c 2022-06-23 thomas view->count = 0;
3701 1e37a5c2 2019-05-12 jcs break;
3702 70f17a53 2022-06-13 thomas case CTRL('u'):
3703 23427b14 2022-06-23 thomas case 'u':
3704 70f17a53 2022-06-13 thomas nscroll /= 2;
3705 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3706 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3707 a4292ac5 2019-05-12 jcs case CTRL('b'):
3708 1c5e5faa 2022-06-23 thomas case 'b':
3709 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3710 1e37a5c2 2019-05-12 jcs break;
3711 1e37a5c2 2019-05-12 jcs case 'j':
3712 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3713 1e37a5c2 2019-05-12 jcs case '>':
3714 1e37a5c2 2019-05-12 jcs case '.':
3715 f7140bf5 2021-10-17 thomas case CTRL('n'):
3716 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3717 912a3f79 2021-08-30 j break;
3718 f69c5a46 2022-07-19 thomas case '@':
3719 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3720 f69c5a46 2022-07-19 thomas break;
3721 912a3f79 2021-08-30 j case 'G':
3722 912a3f79 2021-08-30 j case KEY_END: {
3723 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3724 912a3f79 2021-08-30 j * traverse them all. */
3725 07b0611c 2022-06-23 thomas view->count = 0;
3726 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3727 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3728 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3729 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3730 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3731 1e37a5c2 2019-05-12 jcs break;
3732 912a3f79 2021-08-30 j }
3733 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3734 23427b14 2022-06-23 thomas case 'd':
3735 70f17a53 2022-06-13 thomas nscroll /= 2;
3736 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3737 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3738 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3739 4c2d69cb 2022-06-23 thomas case 'f':
3740 b31f89ff 2022-07-01 thomas case ' ':
3741 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3742 1e37a5c2 2019-05-12 jcs break;
3743 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3744 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3745 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3746 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3747 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3748 2b779855 2020-12-05 naddy select_commit(s);
3749 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3750 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3751 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3752 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3753 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3754 0bf7f153 2020-12-02 naddy }
3755 1e37a5c2 2019-05-12 jcs break;
3756 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3757 444d5325 2022-07-03 thomas case '\r':
3758 07b0611c 2022-06-23 thomas view->count = 0;
3759 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3760 e5a0f69f 2018-08-18 stsp break;
3761 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3762 1e37a5c2 2019-05-12 jcs break;
3763 1be4947a 2022-07-22 thomas case 'T':
3764 07b0611c 2022-06-23 thomas view->count = 0;
3765 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3766 5036bf37 2018-09-24 stsp break;
3767 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3768 1e37a5c2 2019-05-12 jcs break;
3769 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3770 21355643 2020-12-06 stsp case CTRL('l'):
3771 21355643 2020-12-06 stsp case 'B':
3772 07b0611c 2022-06-23 thomas view->count = 0;
3773 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3774 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3775 1e37a5c2 2019-05-12 jcs break;
3776 21355643 2020-12-06 stsp err = stop_log_thread(s);
3777 74cfe85e 2020-10-20 stsp if (err)
3778 74cfe85e 2020-10-20 stsp return err;
3779 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3780 21355643 2020-12-06 stsp char *parent_path;
3781 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3782 21355643 2020-12-06 stsp if (err)
3783 21355643 2020-12-06 stsp return err;
3784 21355643 2020-12-06 stsp free(s->in_repo_path);
3785 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3786 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3787 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3788 21355643 2020-12-06 stsp struct got_object_id *start_id;
3789 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3790 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3791 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3792 21355643 2020-12-06 stsp if (err)
3793 21355643 2020-12-06 stsp return err;
3794 21355643 2020-12-06 stsp free(s->start_id);
3795 21355643 2020-12-06 stsp s->start_id = start_id;
3796 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3797 21355643 2020-12-06 stsp } else /* 'B' */
3798 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3799 21355643 2020-12-06 stsp
3800 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3801 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3802 bf7e79b3 2022-06-23 thomas if (err)
3803 bf7e79b3 2022-06-23 thomas return err;
3804 bf7e79b3 2022-06-23 thomas }
3805 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3806 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3807 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3808 74cfe85e 2020-10-20 stsp if (err)
3809 21355643 2020-12-06 stsp return err;
3810 51a10b52 2020-12-26 stsp tog_free_refs();
3811 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3812 ca51c541 2020-12-07 stsp if (err)
3813 ca51c541 2020-12-07 stsp return err;
3814 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3815 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3816 d01904d4 2019-06-25 stsp if (err)
3817 d01904d4 2019-06-25 stsp return err;
3818 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3819 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3820 b672a97a 2020-01-27 stsp if (err)
3821 b672a97a 2020-01-27 stsp return err;
3822 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3823 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3824 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3825 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3826 21355643 2020-12-06 stsp s->selected_entry = NULL;
3827 21355643 2020-12-06 stsp s->selected = 0;
3828 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3829 21355643 2020-12-06 stsp s->quit = 0;
3830 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3831 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3832 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3833 94ecf40d 2022-07-22 thomas view->offset = 0;
3834 d01904d4 2019-06-25 stsp break;
3835 1be4947a 2022-07-22 thomas case 'R':
3836 07b0611c 2022-06-23 thomas view->count = 0;
3837 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3838 6458efa5 2020-11-24 stsp break;
3839 1e37a5c2 2019-05-12 jcs default:
3840 07b0611c 2022-06-23 thomas view->count = 0;
3841 1e37a5c2 2019-05-12 jcs break;
3842 899d86c2 2018-05-10 stsp }
3843 e5a0f69f 2018-08-18 stsp
3844 80ddbec8 2018-04-29 stsp return err;
3845 80ddbec8 2018-04-29 stsp }
3846 80ddbec8 2018-04-29 stsp
3847 4ed7e80c 2018-05-20 stsp static const struct got_error *
3848 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3849 c2db6724 2019-01-04 stsp {
3850 c2db6724 2019-01-04 stsp const struct got_error *error;
3851 c2db6724 2019-01-04 stsp
3852 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3853 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3854 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3855 37c06ea4 2019-07-15 stsp #endif
3856 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3857 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3858 c2db6724 2019-01-04 stsp
3859 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3860 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3861 c2db6724 2019-01-04 stsp
3862 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3863 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3864 c2db6724 2019-01-04 stsp
3865 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3866 c2db6724 2019-01-04 stsp if (error != NULL)
3867 c2db6724 2019-01-04 stsp return error;
3868 c2db6724 2019-01-04 stsp
3869 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3870 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3871 c2db6724 2019-01-04 stsp
3872 c2db6724 2019-01-04 stsp return NULL;
3873 c2db6724 2019-01-04 stsp }
3874 c2db6724 2019-01-04 stsp
3875 a915003a 2019-02-05 stsp static void
3876 a915003a 2019-02-05 stsp init_curses(void)
3877 a915003a 2019-02-05 stsp {
3878 296152d1 2022-05-31 thomas /*
3879 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3880 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3881 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3882 296152d1 2022-05-31 thomas */
3883 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3884 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3885 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3886 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3887 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3888 296152d1 2022-05-31 thomas
3889 a915003a 2019-02-05 stsp initscr();
3890 a915003a 2019-02-05 stsp cbreak();
3891 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3892 a915003a 2019-02-05 stsp noecho();
3893 a915003a 2019-02-05 stsp nonl();
3894 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3895 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3896 a915003a 2019-02-05 stsp curs_set(0);
3897 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3898 6d17833f 2019-11-08 stsp start_color();
3899 6d17833f 2019-11-08 stsp use_default_colors();
3900 6d17833f 2019-11-08 stsp }
3901 a915003a 2019-02-05 stsp }
3902 a915003a 2019-02-05 stsp
3903 c2db6724 2019-01-04 stsp static const struct got_error *
3904 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3905 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3906 f135c941 2020-02-20 stsp {
3907 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3908 f135c941 2020-02-20 stsp
3909 f135c941 2020-02-20 stsp if (argc == 0) {
3910 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3911 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3912 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3913 f135c941 2020-02-20 stsp return NULL;
3914 f135c941 2020-02-20 stsp }
3915 f135c941 2020-02-20 stsp
3916 f135c941 2020-02-20 stsp if (worktree) {
3917 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3918 bfd61697 2020-11-14 stsp char *p;
3919 f135c941 2020-02-20 stsp
3920 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3921 f135c941 2020-02-20 stsp if (err)
3922 f135c941 2020-02-20 stsp return err;
3923 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3924 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3925 bfd61697 2020-11-14 stsp p) == -1) {
3926 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3927 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3928 f135c941 2020-02-20 stsp }
3929 f135c941 2020-02-20 stsp free(p);
3930 f135c941 2020-02-20 stsp } else
3931 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3932 f135c941 2020-02-20 stsp
3933 f135c941 2020-02-20 stsp return err;
3934 f135c941 2020-02-20 stsp }
3935 f135c941 2020-02-20 stsp
3936 f135c941 2020-02-20 stsp static const struct got_error *
3937 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3938 9f7d7167 2018-04-29 stsp {
3939 80ddbec8 2018-04-29 stsp const struct got_error *error;
3940 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3941 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3942 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3943 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3944 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3945 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3946 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3947 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3948 04cc582a 2018-08-01 stsp struct tog_view *view;
3949 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3950 80ddbec8 2018-04-29 stsp
3951 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3952 80ddbec8 2018-04-29 stsp switch (ch) {
3953 b672a97a 2020-01-27 stsp case 'b':
3954 b672a97a 2020-01-27 stsp log_branches = 1;
3955 b672a97a 2020-01-27 stsp break;
3956 80ddbec8 2018-04-29 stsp case 'c':
3957 80ddbec8 2018-04-29 stsp start_commit = optarg;
3958 80ddbec8 2018-04-29 stsp break;
3959 ecb28ae0 2018-07-16 stsp case 'r':
3960 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3961 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3962 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3963 9ba1d308 2019-10-21 stsp optarg);
3964 ecb28ae0 2018-07-16 stsp break;
3965 80ddbec8 2018-04-29 stsp default:
3966 17020d27 2019-03-07 stsp usage_log();
3967 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3968 80ddbec8 2018-04-29 stsp }
3969 80ddbec8 2018-04-29 stsp }
3970 80ddbec8 2018-04-29 stsp
3971 80ddbec8 2018-04-29 stsp argc -= optind;
3972 80ddbec8 2018-04-29 stsp argv += optind;
3973 80ddbec8 2018-04-29 stsp
3974 f135c941 2020-02-20 stsp if (argc > 1)
3975 f135c941 2020-02-20 stsp usage_log();
3976 963f97a1 2019-03-18 stsp
3977 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3978 7cd52833 2022-06-23 thomas if (error != NULL)
3979 7cd52833 2022-06-23 thomas goto done;
3980 7cd52833 2022-06-23 thomas
3981 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3982 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3983 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3984 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3985 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3986 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3987 c156c7a4 2020-12-18 stsp goto done;
3988 a1fbf39a 2019-08-11 stsp if (worktree)
3989 6962eb72 2020-02-20 stsp repo_path =
3990 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3991 a1fbf39a 2019-08-11 stsp else
3992 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3993 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3994 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3995 c156c7a4 2020-12-18 stsp goto done;
3996 c156c7a4 2020-12-18 stsp }
3997 ecb28ae0 2018-07-16 stsp }
3998 ecb28ae0 2018-07-16 stsp
3999 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4000 80ddbec8 2018-04-29 stsp if (error != NULL)
4001 ecb28ae0 2018-07-16 stsp goto done;
4002 80ddbec8 2018-04-29 stsp
4003 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4004 f135c941 2020-02-20 stsp repo, worktree);
4005 f135c941 2020-02-20 stsp if (error)
4006 f135c941 2020-02-20 stsp goto done;
4007 f135c941 2020-02-20 stsp
4008 f135c941 2020-02-20 stsp init_curses();
4009 f135c941 2020-02-20 stsp
4010 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4011 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4012 c02c541e 2019-03-29 stsp if (error)
4013 c02c541e 2019-03-29 stsp goto done;
4014 c02c541e 2019-03-29 stsp
4015 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4016 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4017 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4018 87670572 2020-12-26 naddy if (error)
4019 87670572 2020-12-26 naddy goto done;
4020 87670572 2020-12-26 naddy }
4021 51a10b52 2020-12-26 stsp
4022 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4023 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4024 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4025 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4026 d8f38dc4 2020-12-05 stsp if (error)
4027 d8f38dc4 2020-12-05 stsp goto done;
4028 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4029 d8f38dc4 2020-12-05 stsp } else {
4030 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4031 d8f38dc4 2020-12-05 stsp if (error == NULL)
4032 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4033 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4034 d8f38dc4 2020-12-05 stsp goto done;
4035 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4036 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4037 d8f38dc4 2020-12-05 stsp if (error)
4038 d8f38dc4 2020-12-05 stsp goto done;
4039 d8f38dc4 2020-12-05 stsp }
4040 8b473291 2019-02-21 stsp
4041 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4042 04cc582a 2018-08-01 stsp if (view == NULL) {
4043 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4044 04cc582a 2018-08-01 stsp goto done;
4045 04cc582a 2018-08-01 stsp }
4046 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4047 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4048 ba4f502b 2018-08-04 stsp if (error)
4049 ba4f502b 2018-08-04 stsp goto done;
4050 2fc00ff4 2019-08-31 stsp if (worktree) {
4051 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4052 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4053 2fc00ff4 2019-08-31 stsp worktree = NULL;
4054 2fc00ff4 2019-08-31 stsp }
4055 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4056 ecb28ae0 2018-07-16 stsp done:
4057 f135c941 2020-02-20 stsp free(in_repo_path);
4058 ecb28ae0 2018-07-16 stsp free(repo_path);
4059 ecb28ae0 2018-07-16 stsp free(cwd);
4060 899d86c2 2018-05-10 stsp free(start_id);
4061 d8f38dc4 2020-12-05 stsp free(label);
4062 d8f38dc4 2020-12-05 stsp if (ref)
4063 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4064 1d0f4054 2021-06-17 stsp if (repo) {
4065 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4066 1d0f4054 2021-06-17 stsp if (error == NULL)
4067 1d0f4054 2021-06-17 stsp error = close_err;
4068 1d0f4054 2021-06-17 stsp }
4069 ec142235 2019-03-07 stsp if (worktree)
4070 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4071 7cd52833 2022-06-23 thomas if (pack_fds) {
4072 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4073 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4074 7cd52833 2022-06-23 thomas if (error == NULL)
4075 7cd52833 2022-06-23 thomas error = pack_err;
4076 7cd52833 2022-06-23 thomas }
4077 51a10b52 2020-12-26 stsp tog_free_refs();
4078 80ddbec8 2018-04-29 stsp return error;
4079 9f7d7167 2018-04-29 stsp }
4080 9f7d7167 2018-04-29 stsp
4081 4ed7e80c 2018-05-20 stsp __dead static void
4082 9f7d7167 2018-04-29 stsp usage_diff(void)
4083 9f7d7167 2018-04-29 stsp {
4084 80ddbec8 2018-04-29 stsp endwin();
4085 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4086 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4087 9f7d7167 2018-04-29 stsp exit(1);
4088 b304db33 2018-05-20 stsp }
4089 b304db33 2018-05-20 stsp
4090 6d17833f 2019-11-08 stsp static int
4091 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4092 41605754 2020-11-12 stsp regmatch_t *regmatch)
4093 6d17833f 2019-11-08 stsp {
4094 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4095 6d17833f 2019-11-08 stsp }
4096 6d17833f 2019-11-08 stsp
4097 ef20f542 2022-06-26 thomas static struct tog_color *
4098 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4099 6d17833f 2019-11-08 stsp {
4100 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4101 6d17833f 2019-11-08 stsp
4102 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4103 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4104 6d17833f 2019-11-08 stsp return tc;
4105 6d17833f 2019-11-08 stsp }
4106 6d17833f 2019-11-08 stsp
4107 6d17833f 2019-11-08 stsp return NULL;
4108 6d17833f 2019-11-08 stsp }
4109 6d17833f 2019-11-08 stsp
4110 4ed7e80c 2018-05-20 stsp static const struct got_error *
4111 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4112 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4113 41605754 2020-11-12 stsp {
4114 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4115 666f7b10 2022-06-23 thomas char *exstr = NULL;
4116 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4117 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4118 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4119 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4120 41605754 2020-11-12 stsp
4121 41605754 2020-11-12 stsp *wtotal = 0;
4122 cbea3800 2022-06-23 thomas
4123 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4124 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4125 41605754 2020-11-12 stsp
4126 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4127 666f7b10 2022-06-23 thomas if (err)
4128 666f7b10 2022-06-23 thomas return err;
4129 666f7b10 2022-06-23 thomas
4130 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4131 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4132 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4133 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4134 666f7b10 2022-06-23 thomas goto done;
4135 666f7b10 2022-06-23 thomas }
4136 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4137 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4138 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4139 cbea3800 2022-06-23 thomas goto done;
4140 cbea3800 2022-06-23 thomas }
4141 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4142 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4143 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4144 cbea3800 2022-06-23 thomas goto done;
4145 cbea3800 2022-06-23 thomas }
4146 05171be4 2022-06-23 thomas
4147 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4148 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4149 cbea3800 2022-06-23 thomas col_tab_align, 1);
4150 cbea3800 2022-06-23 thomas if (err)
4151 cbea3800 2022-06-23 thomas goto done;
4152 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4153 05171be4 2022-06-23 thomas if (n) {
4154 cbea3800 2022-06-23 thomas free(wline);
4155 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4156 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4157 cbea3800 2022-06-23 thomas if (err)
4158 cbea3800 2022-06-23 thomas goto done;
4159 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4160 cbea3800 2022-06-23 thomas wlimit -= width;
4161 cbea3800 2022-06-23 thomas *wtotal += width;
4162 41605754 2020-11-12 stsp }
4163 41605754 2020-11-12 stsp
4164 41605754 2020-11-12 stsp if (wlimit > 0) {
4165 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4166 cbea3800 2022-06-23 thomas size_t wlen;
4167 cbea3800 2022-06-23 thomas
4168 cbea3800 2022-06-23 thomas free(wline);
4169 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4170 cbea3800 2022-06-23 thomas 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 wlen = wcslen(wline);
4174 cbea3800 2022-06-23 thomas while (i < wlen) {
4175 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4176 cbea3800 2022-06-23 thomas if (width == -1) {
4177 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4178 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4179 cbea3800 2022-06-23 thomas goto done;
4180 cbea3800 2022-06-23 thomas }
4181 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4182 cbea3800 2022-06-23 thomas break;
4183 1c5e5faa 2022-06-23 thomas w += width;
4184 cbea3800 2022-06-23 thomas i++;
4185 41605754 2020-11-12 stsp }
4186 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4187 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4188 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4189 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4190 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4191 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4192 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4193 41605754 2020-11-12 stsp }
4194 41605754 2020-11-12 stsp }
4195 41605754 2020-11-12 stsp
4196 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4197 cbea3800 2022-06-23 thomas free(wline);
4198 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4199 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4200 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4201 cbea3800 2022-06-23 thomas col_tab_align, 1);
4202 cbea3800 2022-06-23 thomas if (err)
4203 cbea3800 2022-06-23 thomas goto done;
4204 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4205 cbea3800 2022-06-23 thomas } else {
4206 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4207 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4208 cbea3800 2022-06-23 thomas if (err)
4209 cbea3800 2022-06-23 thomas goto done;
4210 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4211 cbea3800 2022-06-23 thomas }
4212 cbea3800 2022-06-23 thomas *wtotal += width2;
4213 41605754 2020-11-12 stsp }
4214 cbea3800 2022-06-23 thomas done:
4215 05171be4 2022-06-23 thomas free(wline);
4216 666f7b10 2022-06-23 thomas free(exstr);
4217 cbea3800 2022-06-23 thomas free(seg0);
4218 cbea3800 2022-06-23 thomas free(seg1);
4219 cbea3800 2022-06-23 thomas free(seg2);
4220 cbea3800 2022-06-23 thomas return err;
4221 41605754 2020-11-12 stsp }
4222 07dd3ed3 2022-08-06 thomas
4223 07dd3ed3 2022-08-06 thomas static int
4224 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4225 07dd3ed3 2022-08-06 thomas {
4226 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4227 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4228 07dd3ed3 2022-08-06 thomas
4229 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4230 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4231 fc2737d5 2022-09-15 thomas
4232 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4233 fc2737d5 2022-09-15 thomas selected = first;
4234 fc2737d5 2022-09-15 thomas eof = &s->eof;
4235 fc2737d5 2022-09-15 thomas f = s->f;
4236 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4237 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4238 41605754 2020-11-12 stsp
4239 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4240 07dd3ed3 2022-08-06 thomas selected = first;
4241 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4242 07dd3ed3 2022-08-06 thomas f = s->f;
4243 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4244 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4245 07dd3ed3 2022-08-06 thomas
4246 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4247 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4248 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4249 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4250 07dd3ed3 2022-08-06 thomas } else
4251 07dd3ed3 2022-08-06 thomas return 0;
4252 07dd3ed3 2022-08-06 thomas
4253 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4254 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4255 07dd3ed3 2022-08-06 thomas return 0;
4256 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4257 07dd3ed3 2022-08-06 thomas rewind(f);
4258 07dd3ed3 2022-08-06 thomas *eof = 0;
4259 07dd3ed3 2022-08-06 thomas *first = 1;
4260 07dd3ed3 2022-08-06 thomas *lineno = 0;
4261 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4262 07dd3ed3 2022-08-06 thomas return 0;
4263 07dd3ed3 2022-08-06 thomas }
4264 07dd3ed3 2022-08-06 thomas
4265 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4266 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4267 07dd3ed3 2022-08-06 thomas view->gline = 0;
4268 07dd3ed3 2022-08-06 thomas
4269 07dd3ed3 2022-08-06 thomas return 1;
4270 07dd3ed3 2022-08-06 thomas }
4271 07dd3ed3 2022-08-06 thomas
4272 41605754 2020-11-12 stsp static const struct got_error *
4273 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4274 26ed57b2 2018-05-19 stsp {
4275 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4276 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4277 61e69b96 2018-05-20 stsp const struct got_error *err;
4278 82c78e96 2022-08-06 thomas int nprinted = 0;
4279 b304db33 2018-05-20 stsp char *line;
4280 826082fe 2020-12-10 stsp size_t linesize = 0;
4281 826082fe 2020-12-10 stsp ssize_t linelen;
4282 61e69b96 2018-05-20 stsp wchar_t *wline;
4283 e0b650dd 2018-05-20 stsp int width;
4284 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4285 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4286 fe621944 2020-11-10 stsp off_t line_offset;
4287 26ed57b2 2018-05-19 stsp
4288 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4289 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4290 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4291 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4292 fe621944 2020-11-10 stsp
4293 f7d12f7e 2018-08-01 stsp werase(view->window);
4294 a3404814 2018-09-02 stsp
4295 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4296 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4297 07dd3ed3 2022-08-06 thomas
4298 a3404814 2018-09-02 stsp if (header) {
4299 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4300 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4301 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4302 07dd3ed3 2022-08-06 thomas
4303 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4304 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4305 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4306 f91a2b48 2022-06-23 thomas 0, 0);
4307 135a2da0 2020-11-11 stsp free(line);
4308 135a2da0 2020-11-11 stsp if (err)
4309 a3404814 2018-09-02 stsp return err;
4310 a3404814 2018-09-02 stsp
4311 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4312 a3404814 2018-09-02 stsp wstandout(view->window);
4313 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4314 e54cc94a 2020-11-11 stsp free(wline);
4315 e54cc94a 2020-11-11 stsp wline = NULL;
4316 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4317 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4318 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4319 a3404814 2018-09-02 stsp wstandend(view->window);
4320 26ed57b2 2018-05-19 stsp
4321 a3404814 2018-09-02 stsp if (max_lines <= 1)
4322 a3404814 2018-09-02 stsp return NULL;
4323 a3404814 2018-09-02 stsp max_lines--;
4324 a3404814 2018-09-02 stsp }
4325 a3404814 2018-09-02 stsp
4326 89f1a395 2020-12-01 naddy s->eof = 0;
4327 05171be4 2022-06-23 thomas view->maxx = 0;
4328 826082fe 2020-12-10 stsp line = NULL;
4329 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4330 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4331 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4332 6568f0aa 2022-08-06 thomas
4333 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4334 826082fe 2020-12-10 stsp if (linelen == -1) {
4335 826082fe 2020-12-10 stsp if (feof(s->f)) {
4336 826082fe 2020-12-10 stsp s->eof = 1;
4337 826082fe 2020-12-10 stsp break;
4338 826082fe 2020-12-10 stsp }
4339 826082fe 2020-12-10 stsp free(line);
4340 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4341 61e69b96 2018-05-20 stsp }
4342 05171be4 2022-06-23 thomas
4343 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4344 07dd3ed3 2022-08-06 thomas continue;
4345 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4346 07dd3ed3 2022-08-06 thomas continue;
4347 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4348 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4349 07dd3ed3 2022-08-06 thomas
4350 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4351 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4352 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4353 cbea3800 2022-06-23 thomas if (err) {
4354 cbea3800 2022-06-23 thomas free(line);
4355 cbea3800 2022-06-23 thomas return err;
4356 cbea3800 2022-06-23 thomas }
4357 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4358 cbea3800 2022-06-23 thomas free(wline);
4359 cbea3800 2022-06-23 thomas wline = NULL;
4360 cbea3800 2022-06-23 thomas
4361 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4362 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4363 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4364 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4365 07dd3ed3 2022-08-06 thomas if (attr)
4366 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4367 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4368 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4369 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4370 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4371 41605754 2020-11-12 stsp if (err) {
4372 41605754 2020-11-12 stsp free(line);
4373 41605754 2020-11-12 stsp return err;
4374 41605754 2020-11-12 stsp }
4375 41605754 2020-11-12 stsp } else {
4376 f1c0ec19 2022-06-23 thomas int skip;
4377 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4378 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4379 f1c0ec19 2022-06-23 thomas if (err) {
4380 f1c0ec19 2022-06-23 thomas free(line);
4381 f1c0ec19 2022-06-23 thomas return err;
4382 f1c0ec19 2022-06-23 thomas }
4383 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4384 f1c0ec19 2022-06-23 thomas free(wline);
4385 41605754 2020-11-12 stsp wline = NULL;
4386 41605754 2020-11-12 stsp }
4387 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4388 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4389 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4390 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4391 07dd3ed3 2022-08-06 thomas } else {
4392 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4393 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4394 07dd3ed3 2022-08-06 thomas }
4395 07dd3ed3 2022-08-06 thomas if (attr)
4396 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4397 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4398 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4399 826082fe 2020-12-10 stsp }
4400 826082fe 2020-12-10 stsp free(line);
4401 fe621944 2020-11-10 stsp if (nprinted >= 1)
4402 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4403 89f1a395 2020-12-01 naddy (nprinted - 1);
4404 fe621944 2020-11-10 stsp else
4405 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4406 26ed57b2 2018-05-19 stsp
4407 a5d43cac 2022-07-01 thomas view_border(view);
4408 c3e9aa98 2019-05-13 jcs
4409 89f1a395 2020-12-01 naddy if (s->eof) {
4410 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4411 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4412 c3e9aa98 2019-05-13 jcs nprinted++;
4413 c3e9aa98 2019-05-13 jcs }
4414 c3e9aa98 2019-05-13 jcs
4415 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4416 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4417 c3e9aa98 2019-05-13 jcs if (err) {
4418 c3e9aa98 2019-05-13 jcs return err;
4419 c3e9aa98 2019-05-13 jcs }
4420 26ed57b2 2018-05-19 stsp
4421 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4422 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4423 e54cc94a 2020-11-11 stsp free(wline);
4424 e54cc94a 2020-11-11 stsp wline = NULL;
4425 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4426 c3e9aa98 2019-05-13 jcs }
4427 c3e9aa98 2019-05-13 jcs
4428 26ed57b2 2018-05-19 stsp return NULL;
4429 abd2672a 2018-12-23 stsp }
4430 abd2672a 2018-12-23 stsp
4431 abd2672a 2018-12-23 stsp static char *
4432 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4433 abd2672a 2018-12-23 stsp {
4434 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4435 09867e48 2019-08-13 stsp char *p, *s;
4436 09867e48 2019-08-13 stsp
4437 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4438 09867e48 2019-08-13 stsp if (tm == NULL)
4439 09867e48 2019-08-13 stsp return NULL;
4440 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4441 09867e48 2019-08-13 stsp if (s == NULL)
4442 09867e48 2019-08-13 stsp return NULL;
4443 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4444 abd2672a 2018-12-23 stsp if (p)
4445 abd2672a 2018-12-23 stsp *p = '\0';
4446 abd2672a 2018-12-23 stsp return s;
4447 9f7d7167 2018-04-29 stsp }
4448 9f7d7167 2018-04-29 stsp
4449 4ed7e80c 2018-05-20 stsp static const struct got_error *
4450 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4451 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4452 0208f208 2020-05-05 stsp {
4453 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4454 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4455 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4456 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4457 0208f208 2020-05-05 stsp
4458 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4459 0208f208 2020-05-05 stsp if (qid != NULL) {
4460 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4461 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4462 ec242592 2022-04-22 thomas &qid->id);
4463 0208f208 2020-05-05 stsp if (err)
4464 0208f208 2020-05-05 stsp return err;
4465 0208f208 2020-05-05 stsp
4466 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4467 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4468 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4469 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4470 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4471 aa8b5dd0 2021-08-01 stsp }
4472 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4473 0208f208 2020-05-05 stsp
4474 0208f208 2020-05-05 stsp }
4475 0208f208 2020-05-05 stsp
4476 0208f208 2020-05-05 stsp if (tree_id1) {
4477 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4478 0208f208 2020-05-05 stsp if (err)
4479 0208f208 2020-05-05 stsp goto done;
4480 0208f208 2020-05-05 stsp }
4481 0208f208 2020-05-05 stsp
4482 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4483 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4484 0208f208 2020-05-05 stsp if (err)
4485 0208f208 2020-05-05 stsp goto done;
4486 0208f208 2020-05-05 stsp
4487 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4488 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4489 0208f208 2020-05-05 stsp done:
4490 0208f208 2020-05-05 stsp if (tree1)
4491 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4492 0208f208 2020-05-05 stsp if (tree2)
4493 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4494 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4495 0208f208 2020-05-05 stsp return err;
4496 0208f208 2020-05-05 stsp }
4497 0208f208 2020-05-05 stsp
4498 0208f208 2020-05-05 stsp static const struct got_error *
4499 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4500 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4501 abd2672a 2018-12-23 stsp {
4502 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4503 fe621944 2020-11-10 stsp
4504 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4505 fe621944 2020-11-10 stsp if (p == NULL)
4506 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4507 82c78e96 2022-08-06 thomas *lines = p;
4508 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4509 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4510 fe621944 2020-11-10 stsp (*nlines)++;
4511 82c78e96 2022-08-06 thomas
4512 fe621944 2020-11-10 stsp return NULL;
4513 fe621944 2020-11-10 stsp }
4514 fe621944 2020-11-10 stsp
4515 fe621944 2020-11-10 stsp static const struct got_error *
4516 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4517 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4518 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4519 fe621944 2020-11-10 stsp {
4520 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4521 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4522 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4523 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4524 45d799e2 2018-12-23 stsp time_t committer_time;
4525 45d799e2 2018-12-23 stsp const char *author, *committer;
4526 8b473291 2019-02-21 stsp char *refs_str = NULL;
4527 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4528 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4529 fe621944 2020-11-10 stsp off_t outoff = 0;
4530 fe621944 2020-11-10 stsp int n;
4531 abd2672a 2018-12-23 stsp
4532 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4533 0208f208 2020-05-05 stsp
4534 8b473291 2019-02-21 stsp if (refs) {
4535 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4536 8b473291 2019-02-21 stsp if (err)
4537 8b473291 2019-02-21 stsp return err;
4538 8b473291 2019-02-21 stsp }
4539 8b473291 2019-02-21 stsp
4540 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4541 abd2672a 2018-12-23 stsp if (err)
4542 abd2672a 2018-12-23 stsp return err;
4543 abd2672a 2018-12-23 stsp
4544 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4545 15a94983 2018-12-23 stsp if (err) {
4546 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4547 15a94983 2018-12-23 stsp goto done;
4548 15a94983 2018-12-23 stsp }
4549 abd2672a 2018-12-23 stsp
4550 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4551 fe621944 2020-11-10 stsp if (err)
4552 fe621944 2020-11-10 stsp goto done;
4553 fe621944 2020-11-10 stsp
4554 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4555 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4556 fe621944 2020-11-10 stsp if (n < 0) {
4557 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4558 abd2672a 2018-12-23 stsp goto done;
4559 abd2672a 2018-12-23 stsp }
4560 fe621944 2020-11-10 stsp outoff += n;
4561 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4562 fe621944 2020-11-10 stsp if (err)
4563 fe621944 2020-11-10 stsp goto done;
4564 fe621944 2020-11-10 stsp
4565 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4566 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4567 fe621944 2020-11-10 stsp if (n < 0) {
4568 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4569 abd2672a 2018-12-23 stsp goto done;
4570 abd2672a 2018-12-23 stsp }
4571 fe621944 2020-11-10 stsp outoff += n;
4572 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4573 fe621944 2020-11-10 stsp if (err)
4574 fe621944 2020-11-10 stsp goto done;
4575 fe621944 2020-11-10 stsp
4576 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4577 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4578 fe621944 2020-11-10 stsp if (datestr) {
4579 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4580 fe621944 2020-11-10 stsp if (n < 0) {
4581 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4582 fe621944 2020-11-10 stsp goto done;
4583 fe621944 2020-11-10 stsp }
4584 fe621944 2020-11-10 stsp outoff += n;
4585 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4586 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4587 fe621944 2020-11-10 stsp if (err)
4588 fe621944 2020-11-10 stsp goto done;
4589 abd2672a 2018-12-23 stsp }
4590 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4591 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4592 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4593 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
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_AUTHOR);
4601 fe621944 2020-11-10 stsp if (err)
4602 fe621944 2020-11-10 stsp goto done;
4603 abd2672a 2018-12-23 stsp }
4604 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4605 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4606 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4607 ac4dc263 2021-09-24 thomas int pn = 1;
4608 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4609 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4610 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4611 ac4dc263 2021-09-24 thomas if (err)
4612 ac4dc263 2021-09-24 thomas goto done;
4613 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4614 ac4dc263 2021-09-24 thomas if (n < 0) {
4615 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4616 ac4dc263 2021-09-24 thomas goto done;
4617 ac4dc263 2021-09-24 thomas }
4618 ac4dc263 2021-09-24 thomas outoff += n;
4619 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4620 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4621 ac4dc263 2021-09-24 thomas if (err)
4622 ac4dc263 2021-09-24 thomas goto done;
4623 ac4dc263 2021-09-24 thomas free(id_str);
4624 ac4dc263 2021-09-24 thomas id_str = NULL;
4625 ac4dc263 2021-09-24 thomas }
4626 ac4dc263 2021-09-24 thomas }
4627 ac4dc263 2021-09-24 thomas
4628 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4629 5943eee2 2019-08-13 stsp if (err)
4630 5943eee2 2019-08-13 stsp goto done;
4631 fe621944 2020-11-10 stsp s = logmsg;
4632 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4633 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4634 fe621944 2020-11-10 stsp if (n < 0) {
4635 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4636 fe621944 2020-11-10 stsp goto done;
4637 fe621944 2020-11-10 stsp }
4638 fe621944 2020-11-10 stsp outoff += n;
4639 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4640 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4641 fe621944 2020-11-10 stsp if (err)
4642 fe621944 2020-11-10 stsp goto done;
4643 abd2672a 2018-12-23 stsp }
4644 fe621944 2020-11-10 stsp
4645 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4646 0208f208 2020-05-05 stsp if (err)
4647 0208f208 2020-05-05 stsp goto done;
4648 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4649 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4650 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4651 fe621944 2020-11-10 stsp if (n < 0) {
4652 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4653 fe621944 2020-11-10 stsp goto done;
4654 fe621944 2020-11-10 stsp }
4655 fe621944 2020-11-10 stsp outoff += n;
4656 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4657 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4658 fe621944 2020-11-10 stsp if (err)
4659 fe621944 2020-11-10 stsp goto done;
4660 0208f208 2020-05-05 stsp free((char *)pe->path);
4661 0208f208 2020-05-05 stsp free(pe->data);
4662 0208f208 2020-05-05 stsp }
4663 fe621944 2020-11-10 stsp
4664 0208f208 2020-05-05 stsp fputc('\n', outfile);
4665 fe621944 2020-11-10 stsp outoff++;
4666 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4667 abd2672a 2018-12-23 stsp done:
4668 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4669 abd2672a 2018-12-23 stsp free(id_str);
4670 5943eee2 2019-08-13 stsp free(logmsg);
4671 8b473291 2019-02-21 stsp free(refs_str);
4672 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4673 7510f233 2020-08-09 stsp if (err) {
4674 82c78e96 2022-08-06 thomas free(*lines);
4675 82c78e96 2022-08-06 thomas *lines = NULL;
4676 ae6a6978 2020-08-09 stsp *nlines = 0;
4677 7510f233 2020-08-09 stsp }
4678 fe621944 2020-11-10 stsp return err;
4679 abd2672a 2018-12-23 stsp }
4680 abd2672a 2018-12-23 stsp
4681 abd2672a 2018-12-23 stsp static const struct got_error *
4682 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4683 26ed57b2 2018-05-19 stsp {
4684 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4685 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4686 15a94983 2018-12-23 stsp int obj_type;
4687 fe621944 2020-11-10 stsp
4688 82c78e96 2022-08-06 thomas free(s->lines);
4689 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4690 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4691 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4692 fe621944 2020-11-10 stsp s->nlines = 0;
4693 26ed57b2 2018-05-19 stsp
4694 511a516b 2018-05-19 stsp f = got_opentemp();
4695 48ae06ee 2018-10-18 stsp if (f == NULL) {
4696 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4697 48ae06ee 2018-10-18 stsp goto done;
4698 48ae06ee 2018-10-18 stsp }
4699 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4700 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4701 fb43ecf1 2019-02-11 stsp goto done;
4702 fb43ecf1 2019-02-11 stsp }
4703 48ae06ee 2018-10-18 stsp s->f = f;
4704 26ed57b2 2018-05-19 stsp
4705 15a94983 2018-12-23 stsp if (s->id1)
4706 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4707 15a94983 2018-12-23 stsp else
4708 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4709 15a94983 2018-12-23 stsp if (err)
4710 15a94983 2018-12-23 stsp goto done;
4711 15a94983 2018-12-23 stsp
4712 15a94983 2018-12-23 stsp switch (obj_type) {
4713 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4714 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4715 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4716 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4717 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4718 26ed57b2 2018-05-19 stsp break;
4719 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4720 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4721 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4722 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4723 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4724 26ed57b2 2018-05-19 stsp break;
4725 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4726 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4727 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4728 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4729 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4730 abd2672a 2018-12-23 stsp
4731 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4732 abd2672a 2018-12-23 stsp if (err)
4733 3ffacbe1 2020-02-02 tracey goto done;
4734 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4735 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4736 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4737 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4738 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4739 f44b1f58 2020-02-02 tracey if (err)
4740 f44b1f58 2020-02-02 tracey goto done;
4741 f44b1f58 2020-02-02 tracey } else {
4742 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4743 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4744 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4745 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4746 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4747 82c78e96 2022-08-06 thomas s->f);
4748 f44b1f58 2020-02-02 tracey if (err)
4749 f44b1f58 2020-02-02 tracey goto done;
4750 f5404e4e 2020-02-02 tracey break;
4751 15a087fe 2019-02-21 stsp }
4752 abd2672a 2018-12-23 stsp }
4753 abd2672a 2018-12-23 stsp }
4754 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4755 abd2672a 2018-12-23 stsp
4756 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4757 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4758 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4759 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4760 26ed57b2 2018-05-19 stsp break;
4761 abd2672a 2018-12-23 stsp }
4762 26ed57b2 2018-05-19 stsp default:
4763 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4764 48ae06ee 2018-10-18 stsp break;
4765 26ed57b2 2018-05-19 stsp }
4766 48ae06ee 2018-10-18 stsp done:
4767 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4768 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4769 48ae06ee 2018-10-18 stsp return err;
4770 48ae06ee 2018-10-18 stsp }
4771 26ed57b2 2018-05-19 stsp
4772 f5215bb9 2019-02-22 stsp static void
4773 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4774 f5215bb9 2019-02-22 stsp {
4775 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4776 f5215bb9 2019-02-22 stsp update_panels();
4777 f5215bb9 2019-02-22 stsp doupdate();
4778 f44b1f58 2020-02-02 tracey }
4779 f44b1f58 2020-02-02 tracey
4780 f44b1f58 2020-02-02 tracey static const struct got_error *
4781 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4782 f44b1f58 2020-02-02 tracey {
4783 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4784 f44b1f58 2020-02-02 tracey
4785 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4786 f44b1f58 2020-02-02 tracey return NULL;
4787 f44b1f58 2020-02-02 tracey }
4788 f44b1f58 2020-02-02 tracey
4789 2a7d3cd7 2022-09-17 thomas static void
4790 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4791 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
4792 f44b1f58 2020-02-02 tracey {
4793 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
4794 fc2737d5 2022-09-15 thomas
4795 2a7d3cd7 2022-09-17 thomas *f = s->f;
4796 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
4797 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
4798 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
4799 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
4800 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
4801 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
4802 fc2737d5 2022-09-15 thomas }
4803 fc2737d5 2022-09-15 thomas
4804 fc2737d5 2022-09-15 thomas static const struct got_error *
4805 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
4806 fc2737d5 2022-09-15 thomas {
4807 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4808 fc2737d5 2022-09-15 thomas FILE *f;
4809 f44b1f58 2020-02-02 tracey int lineno;
4810 78eecffc 2022-06-23 thomas char *line = NULL;
4811 826082fe 2020-12-10 stsp size_t linesize = 0;
4812 826082fe 2020-12-10 stsp ssize_t linelen;
4813 fc2737d5 2022-09-15 thomas off_t *line_offsets;
4814 fc2737d5 2022-09-15 thomas size_t nlines = 0;
4815 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
4816 f44b1f58 2020-02-02 tracey
4817 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
4818 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
4819 2a7d3cd7 2022-09-17 thomas "view search not supported");
4820 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4821 fc2737d5 2022-09-15 thomas &match, &selected);
4822 fc2737d5 2022-09-15 thomas
4823 f44b1f58 2020-02-02 tracey if (!view->searching) {
4824 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4825 f44b1f58 2020-02-02 tracey return NULL;
4826 f44b1f58 2020-02-02 tracey }
4827 f44b1f58 2020-02-02 tracey
4828 fc2737d5 2022-09-15 thomas if (*match) {
4829 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4830 fc2737d5 2022-09-15 thomas lineno = *match + 1;
4831 f44b1f58 2020-02-02 tracey else
4832 fc2737d5 2022-09-15 thomas lineno = *match - 1;
4833 4b3f9dac 2021-12-17 thomas } else
4834 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
4835 f44b1f58 2020-02-02 tracey
4836 f44b1f58 2020-02-02 tracey while (1) {
4837 f44b1f58 2020-02-02 tracey off_t offset;
4838 f44b1f58 2020-02-02 tracey
4839 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
4840 fc2737d5 2022-09-15 thomas if (*match == 0) {
4841 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4842 f44b1f58 2020-02-02 tracey break;
4843 f44b1f58 2020-02-02 tracey }
4844 f44b1f58 2020-02-02 tracey
4845 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4846 f44b1f58 2020-02-02 tracey lineno = 1;
4847 f44b1f58 2020-02-02 tracey else
4848 fc2737d5 2022-09-15 thomas lineno = nlines;
4849 f44b1f58 2020-02-02 tracey }
4850 f44b1f58 2020-02-02 tracey
4851 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
4852 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
4853 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
4854 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
4855 f44b1f58 2020-02-02 tracey free(line);
4856 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4857 f44b1f58 2020-02-02 tracey }
4858 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
4859 78eecffc 2022-06-23 thomas if (linelen != -1) {
4860 78eecffc 2022-06-23 thomas char *exstr;
4861 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4862 78eecffc 2022-06-23 thomas if (err)
4863 78eecffc 2022-06-23 thomas break;
4864 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4865 78eecffc 2022-06-23 thomas &view->regmatch)) {
4866 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4867 fc2737d5 2022-09-15 thomas *match = lineno;
4868 78eecffc 2022-06-23 thomas free(exstr);
4869 78eecffc 2022-06-23 thomas break;
4870 78eecffc 2022-06-23 thomas }
4871 78eecffc 2022-06-23 thomas free(exstr);
4872 f44b1f58 2020-02-02 tracey }
4873 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4874 f44b1f58 2020-02-02 tracey lineno++;
4875 f44b1f58 2020-02-02 tracey else
4876 f44b1f58 2020-02-02 tracey lineno--;
4877 f44b1f58 2020-02-02 tracey }
4878 826082fe 2020-12-10 stsp free(line);
4879 f44b1f58 2020-02-02 tracey
4880 fc2737d5 2022-09-15 thomas if (*match) {
4881 fc2737d5 2022-09-15 thomas *first = *match;
4882 fc2737d5 2022-09-15 thomas *selected = 1;
4883 f44b1f58 2020-02-02 tracey }
4884 f44b1f58 2020-02-02 tracey
4885 05171be4 2022-06-23 thomas return err;
4886 f5215bb9 2019-02-22 stsp }
4887 f5215bb9 2019-02-22 stsp
4888 48ae06ee 2018-10-18 stsp static const struct got_error *
4889 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4890 a0f32f33 2022-06-13 thomas {
4891 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4892 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4893 a0f32f33 2022-06-13 thomas
4894 a0f32f33 2022-06-13 thomas free(s->id1);
4895 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4896 a0f32f33 2022-06-13 thomas free(s->id2);
4897 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4898 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4899 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4900 a0f32f33 2022-06-13 thomas s->f = NULL;
4901 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4902 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4903 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4904 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4905 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4906 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4907 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4908 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4909 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4910 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4911 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4912 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4913 82c78e96 2022-08-06 thomas free(s->lines);
4914 82c78e96 2022-08-06 thomas s->lines = NULL;
4915 a0f32f33 2022-06-13 thomas s->nlines = 0;
4916 a0f32f33 2022-06-13 thomas return err;
4917 a0f32f33 2022-06-13 thomas }
4918 a0f32f33 2022-06-13 thomas
4919 a0f32f33 2022-06-13 thomas static const struct got_error *
4920 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4921 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4922 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4923 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4924 48ae06ee 2018-10-18 stsp {
4925 48ae06ee 2018-10-18 stsp const struct got_error *err;
4926 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4927 5dc9f4bc 2018-08-04 stsp
4928 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4929 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4930 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4931 a0f32f33 2022-06-13 thomas
4932 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4933 15a94983 2018-12-23 stsp int type1, type2;
4934 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4935 15a94983 2018-12-23 stsp if (err)
4936 15a94983 2018-12-23 stsp return err;
4937 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4938 15a94983 2018-12-23 stsp if (err)
4939 15a94983 2018-12-23 stsp return err;
4940 15a94983 2018-12-23 stsp
4941 15a94983 2018-12-23 stsp if (type1 != type2)
4942 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4943 15a94983 2018-12-23 stsp }
4944 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4945 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4946 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4947 f44b1f58 2020-02-02 tracey s->repo = repo;
4948 f44b1f58 2020-02-02 tracey s->id1 = id1;
4949 f44b1f58 2020-02-02 tracey s->id2 = id2;
4950 3dbaef42 2020-11-24 stsp s->label1 = label1;
4951 3dbaef42 2020-11-24 stsp s->label2 = label2;
4952 48ae06ee 2018-10-18 stsp
4953 15a94983 2018-12-23 stsp if (id1) {
4954 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4955 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4956 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4957 48ae06ee 2018-10-18 stsp } else
4958 5465d566 2020-02-01 tracey s->id1 = NULL;
4959 48ae06ee 2018-10-18 stsp
4960 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4961 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4962 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4963 a0f32f33 2022-06-13 thomas goto done;
4964 48ae06ee 2018-10-18 stsp }
4965 a0f32f33 2022-06-13 thomas
4966 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4967 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4968 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4969 9a1d7435 2022-06-23 thomas goto done;
4970 9a1d7435 2022-06-23 thomas }
4971 9a1d7435 2022-06-23 thomas
4972 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4973 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4974 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4975 a0f32f33 2022-06-13 thomas goto done;
4976 a0f32f33 2022-06-13 thomas }
4977 a0f32f33 2022-06-13 thomas
4978 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4979 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4980 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4981 19a6a6b5 2022-07-01 thomas goto done;
4982 19a6a6b5 2022-07-01 thomas }
4983 19a6a6b5 2022-07-01 thomas
4984 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4985 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4986 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4987 19a6a6b5 2022-07-01 thomas goto done;
4988 19a6a6b5 2022-07-01 thomas }
4989 19a6a6b5 2022-07-01 thomas
4990 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4991 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4992 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4993 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4994 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4995 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4996 5465d566 2020-02-01 tracey s->repo = repo;
4997 6d17833f 2019-11-08 stsp
4998 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4999 6568f0aa 2022-08-06 thomas int rc;
5000 6d17833f 2019-11-08 stsp
5001 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5002 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5003 6568f0aa 2022-08-06 thomas if (rc != ERR)
5004 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5005 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5006 6568f0aa 2022-08-06 thomas if (rc != ERR)
5007 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5008 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5009 6568f0aa 2022-08-06 thomas if (rc != ERR)
5010 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5011 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5012 6568f0aa 2022-08-06 thomas if (rc != ERR)
5013 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5014 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5015 6568f0aa 2022-08-06 thomas if (rc != ERR)
5016 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5017 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5018 6568f0aa 2022-08-06 thomas if (rc != ERR)
5019 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5020 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5021 6568f0aa 2022-08-06 thomas if (rc != ERR)
5022 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5023 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5024 6568f0aa 2022-08-06 thomas if (rc != ERR)
5025 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5026 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5027 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5028 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5029 a0f32f33 2022-06-13 thomas goto done;
5030 6568f0aa 2022-08-06 thomas }
5031 6d17833f 2019-11-08 stsp }
5032 5dc9f4bc 2018-08-04 stsp
5033 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5034 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5035 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5036 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5037 f5215bb9 2019-02-22 stsp
5038 5465d566 2020-02-01 tracey err = create_diff(s);
5039 48ae06ee 2018-10-18 stsp
5040 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5041 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5042 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5043 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5044 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5045 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5046 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5047 a0f32f33 2022-06-13 thomas done:
5048 a0f32f33 2022-06-13 thomas if (err)
5049 a0f32f33 2022-06-13 thomas close_diff_view(view);
5050 e5a0f69f 2018-08-18 stsp return err;
5051 5dc9f4bc 2018-08-04 stsp }
5052 5dc9f4bc 2018-08-04 stsp
5053 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5054 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5055 5dc9f4bc 2018-08-04 stsp {
5056 a3404814 2018-09-02 stsp const struct got_error *err;
5057 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5058 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5059 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5060 a3404814 2018-09-02 stsp
5061 a3404814 2018-09-02 stsp if (s->id1) {
5062 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5063 a3404814 2018-09-02 stsp if (err)
5064 a3404814 2018-09-02 stsp return err;
5065 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5066 3dbaef42 2020-11-24 stsp } else
5067 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5068 3dbaef42 2020-11-24 stsp
5069 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5070 a3404814 2018-09-02 stsp if (err)
5071 a3404814 2018-09-02 stsp return err;
5072 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5073 26ed57b2 2018-05-19 stsp
5074 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5075 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5076 a3404814 2018-09-02 stsp free(id_str1);
5077 a3404814 2018-09-02 stsp free(id_str2);
5078 a3404814 2018-09-02 stsp return err;
5079 a3404814 2018-09-02 stsp }
5080 a3404814 2018-09-02 stsp free(id_str1);
5081 a3404814 2018-09-02 stsp free(id_str2);
5082 a3404814 2018-09-02 stsp
5083 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5084 267bb3b8 2021-08-01 stsp free(header);
5085 267bb3b8 2021-08-01 stsp return err;
5086 15a087fe 2019-02-21 stsp }
5087 15a087fe 2019-02-21 stsp
5088 15a087fe 2019-02-21 stsp static const struct got_error *
5089 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5090 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5091 15a087fe 2019-02-21 stsp {
5092 d7a04538 2019-02-21 stsp const struct got_error *err;
5093 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5094 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5095 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5096 15a087fe 2019-02-21 stsp
5097 15a087fe 2019-02-21 stsp free(s->id2);
5098 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5099 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5100 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5101 15a087fe 2019-02-21 stsp
5102 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5103 d7a04538 2019-02-21 stsp if (err)
5104 d7a04538 2019-02-21 stsp return err;
5105 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5106 15a087fe 2019-02-21 stsp free(s->id1);
5107 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5108 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5109 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5110 15a087fe 2019-02-21 stsp return NULL;
5111 0cf4efb1 2018-09-29 stsp }
5112 0cf4efb1 2018-09-29 stsp
5113 0cf4efb1 2018-09-29 stsp static const struct got_error *
5114 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5115 adf4c9e0 2022-07-03 thomas {
5116 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5117 adf4c9e0 2022-07-03 thomas
5118 adf4c9e0 2022-07-03 thomas view->count = 0;
5119 adf4c9e0 2022-07-03 thomas wclear(view->window);
5120 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5121 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5122 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5123 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5124 adf4c9e0 2022-07-03 thomas return create_diff(s);
5125 82c78e96 2022-08-06 thomas }
5126 82c78e96 2022-08-06 thomas
5127 82c78e96 2022-08-06 thomas static void
5128 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5129 82c78e96 2022-08-06 thomas {
5130 82c78e96 2022-08-06 thomas int start, i;
5131 82c78e96 2022-08-06 thomas
5132 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5133 82c78e96 2022-08-06 thomas
5134 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5135 82c78e96 2022-08-06 thomas if (i == 0)
5136 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5137 82c78e96 2022-08-06 thomas if (--i == start)
5138 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5139 82c78e96 2022-08-06 thomas }
5140 82c78e96 2022-08-06 thomas
5141 82c78e96 2022-08-06 thomas s->selected_line = 1;
5142 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5143 adf4c9e0 2022-07-03 thomas }
5144 adf4c9e0 2022-07-03 thomas
5145 82c78e96 2022-08-06 thomas static void
5146 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5147 82c78e96 2022-08-06 thomas {
5148 82c78e96 2022-08-06 thomas int start, i;
5149 82c78e96 2022-08-06 thomas
5150 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5151 82c78e96 2022-08-06 thomas
5152 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5153 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5154 82c78e96 2022-08-06 thomas i = 0;
5155 82c78e96 2022-08-06 thomas if (++i == start)
5156 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5157 82c78e96 2022-08-06 thomas }
5158 82c78e96 2022-08-06 thomas
5159 82c78e96 2022-08-06 thomas s->selected_line = 1;
5160 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5161 82c78e96 2022-08-06 thomas }
5162 82c78e96 2022-08-06 thomas
5163 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5164 4fc71f3b 2022-07-12 thomas int, int, int);
5165 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5166 4fc71f3b 2022-07-12 thomas int, int);
5167 4fc71f3b 2022-07-12 thomas
5168 adf4c9e0 2022-07-03 thomas static const struct got_error *
5169 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5170 e5a0f69f 2018-08-18 stsp {
5171 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5172 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5173 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5174 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5175 826082fe 2020-12-10 stsp char *line = NULL;
5176 826082fe 2020-12-10 stsp size_t linesize = 0;
5177 826082fe 2020-12-10 stsp ssize_t linelen;
5178 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5179 e5a0f69f 2018-08-18 stsp
5180 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5181 82c78e96 2022-08-06 thomas
5182 e5a0f69f 2018-08-18 stsp switch (ch) {
5183 05171be4 2022-06-23 thomas case '0':
5184 05171be4 2022-06-23 thomas view->x = 0;
5185 05171be4 2022-06-23 thomas break;
5186 05171be4 2022-06-23 thomas case '$':
5187 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5188 07b0611c 2022-06-23 thomas view->count = 0;
5189 05171be4 2022-06-23 thomas break;
5190 05171be4 2022-06-23 thomas case KEY_RIGHT:
5191 05171be4 2022-06-23 thomas case 'l':
5192 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5193 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5194 07b0611c 2022-06-23 thomas else
5195 07b0611c 2022-06-23 thomas view->count = 0;
5196 05171be4 2022-06-23 thomas break;
5197 05171be4 2022-06-23 thomas case KEY_LEFT:
5198 05171be4 2022-06-23 thomas case 'h':
5199 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5200 07b0611c 2022-06-23 thomas if (view->x <= 0)
5201 07b0611c 2022-06-23 thomas view->count = 0;
5202 05171be4 2022-06-23 thomas break;
5203 64453f7e 2020-11-21 stsp case 'a':
5204 3dbaef42 2020-11-24 stsp case 'w':
5205 3dbaef42 2020-11-24 stsp if (ch == 'a')
5206 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5207 3dbaef42 2020-11-24 stsp if (ch == 'w')
5208 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5209 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5210 912a3f79 2021-08-30 j break;
5211 912a3f79 2021-08-30 j case 'g':
5212 912a3f79 2021-08-30 j case KEY_HOME:
5213 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5214 07b0611c 2022-06-23 thomas view->count = 0;
5215 64453f7e 2020-11-21 stsp break;
5216 912a3f79 2021-08-30 j case 'G':
5217 912a3f79 2021-08-30 j case KEY_END:
5218 07b0611c 2022-06-23 thomas view->count = 0;
5219 912a3f79 2021-08-30 j if (s->eof)
5220 912a3f79 2021-08-30 j break;
5221 912a3f79 2021-08-30 j
5222 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5223 912a3f79 2021-08-30 j s->eof = 1;
5224 912a3f79 2021-08-30 j break;
5225 1e37a5c2 2019-05-12 jcs case 'k':
5226 1e37a5c2 2019-05-12 jcs case KEY_UP:
5227 f7140bf5 2021-10-17 thomas case CTRL('p'):
5228 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5229 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5230 07b0611c 2022-06-23 thomas else
5231 07b0611c 2022-06-23 thomas view->count = 0;
5232 1e37a5c2 2019-05-12 jcs break;
5233 70f17a53 2022-06-13 thomas case CTRL('u'):
5234 23427b14 2022-06-23 thomas case 'u':
5235 70f17a53 2022-06-13 thomas nscroll /= 2;
5236 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5237 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5238 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5239 1c5e5faa 2022-06-23 thomas case 'b':
5240 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5241 07b0611c 2022-06-23 thomas view->count = 0;
5242 26ed57b2 2018-05-19 stsp break;
5243 07b0611c 2022-06-23 thomas }
5244 1e37a5c2 2019-05-12 jcs i = 0;
5245 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5246 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5247 1e37a5c2 2019-05-12 jcs break;
5248 1e37a5c2 2019-05-12 jcs case 'j':
5249 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5250 f7140bf5 2021-10-17 thomas case CTRL('n'):
5251 1e37a5c2 2019-05-12 jcs if (!s->eof)
5252 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5253 07b0611c 2022-06-23 thomas else
5254 07b0611c 2022-06-23 thomas view->count = 0;
5255 1e37a5c2 2019-05-12 jcs break;
5256 70f17a53 2022-06-13 thomas case CTRL('d'):
5257 23427b14 2022-06-23 thomas case 'd':
5258 70f17a53 2022-06-13 thomas nscroll /= 2;
5259 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5260 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5261 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5262 1c5e5faa 2022-06-23 thomas case 'f':
5263 1e37a5c2 2019-05-12 jcs case ' ':
5264 07b0611c 2022-06-23 thomas if (s->eof) {
5265 07b0611c 2022-06-23 thomas view->count = 0;
5266 1e37a5c2 2019-05-12 jcs break;
5267 07b0611c 2022-06-23 thomas }
5268 1e37a5c2 2019-05-12 jcs i = 0;
5269 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5270 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5271 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5272 826082fe 2020-12-10 stsp if (linelen == -1) {
5273 826082fe 2020-12-10 stsp if (feof(s->f)) {
5274 826082fe 2020-12-10 stsp s->eof = 1;
5275 826082fe 2020-12-10 stsp } else
5276 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5277 34bc9ec9 2019-02-22 stsp break;
5278 826082fe 2020-12-10 stsp }
5279 1e37a5c2 2019-05-12 jcs }
5280 826082fe 2020-12-10 stsp free(line);
5281 1e37a5c2 2019-05-12 jcs break;
5282 82c78e96 2022-08-06 thomas case '(':
5283 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5284 82c78e96 2022-08-06 thomas break;
5285 82c78e96 2022-08-06 thomas case ')':
5286 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5287 82c78e96 2022-08-06 thomas break;
5288 82c78e96 2022-08-06 thomas case '{':
5289 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5290 82c78e96 2022-08-06 thomas break;
5291 82c78e96 2022-08-06 thomas case '}':
5292 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5293 82c78e96 2022-08-06 thomas break;
5294 1e37a5c2 2019-05-12 jcs case '[':
5295 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5296 1e37a5c2 2019-05-12 jcs s->diff_context--;
5297 aa61903a 2021-12-31 thomas s->matched_line = 0;
5298 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5299 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5300 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5301 27829c9e 2020-11-21 stsp s->nlines) {
5302 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5303 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5304 27829c9e 2020-11-21 stsp }
5305 07b0611c 2022-06-23 thomas } else
5306 07b0611c 2022-06-23 thomas view->count = 0;
5307 1e37a5c2 2019-05-12 jcs break;
5308 1e37a5c2 2019-05-12 jcs case ']':
5309 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
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 07b0611c 2022-06-23 thomas } else
5315 07b0611c 2022-06-23 thomas view->count = 0;
5316 1e37a5c2 2019-05-12 jcs break;
5317 1e37a5c2 2019-05-12 jcs case '<':
5318 1e37a5c2 2019-05-12 jcs case ',':
5319 777aae21 2022-07-20 thomas case 'K':
5320 4fc71f3b 2022-07-12 thomas up = 1;
5321 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5322 4fc71f3b 2022-07-12 thomas case '>':
5323 4fc71f3b 2022-07-12 thomas case '.':
5324 777aae21 2022-07-20 thomas case 'J':
5325 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5326 07b0611c 2022-06-23 thomas view->count = 0;
5327 48ae06ee 2018-10-18 stsp break;
5328 07b0611c 2022-06-23 thomas }
5329 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5330 6524637e 2019-02-21 stsp
5331 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5332 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5333 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5334 15a087fe 2019-02-21 stsp
5335 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5336 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5337 4fc71f3b 2022-07-12 thomas if (err)
5338 4fc71f3b 2022-07-12 thomas break;
5339 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5340 15a087fe 2019-02-21 stsp
5341 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5342 4fc71f3b 2022-07-12 thomas break;
5343 15a087fe 2019-02-21 stsp
5344 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5345 4fc71f3b 2022-07-12 thomas if (err)
5346 4fc71f3b 2022-07-12 thomas break;
5347 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5348 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5349 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5350 5e224a3e 2019-02-22 stsp
5351 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5352 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5353 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5354 4fc71f3b 2022-07-12 thomas
5355 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5356 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5357 4fc71f3b 2022-07-12 thomas if (err)
5358 4fc71f3b 2022-07-12 thomas break;
5359 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5360 5a8b5076 2020-12-05 stsp
5361 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5362 4fc71f3b 2022-07-12 thomas break;
5363 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5364 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5365 4fc71f3b 2022-07-12 thomas bs->selected_line);
5366 4fc71f3b 2022-07-12 thomas if (id == NULL)
5367 4fc71f3b 2022-07-12 thomas break;
5368 15a087fe 2019-02-21 stsp
5369 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5370 4fc71f3b 2022-07-12 thomas break;
5371 15a087fe 2019-02-21 stsp
5372 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5373 4fc71f3b 2022-07-12 thomas if (err)
5374 4fc71f3b 2022-07-12 thomas break;
5375 4fc71f3b 2022-07-12 thomas }
5376 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5377 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5378 aa61903a 2021-12-31 thomas s->matched_line = 0;
5379 05171be4 2022-06-23 thomas view->x = 0;
5380 1e37a5c2 2019-05-12 jcs
5381 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5382 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5383 1e37a5c2 2019-05-12 jcs break;
5384 1e37a5c2 2019-05-12 jcs default:
5385 07b0611c 2022-06-23 thomas view->count = 0;
5386 1e37a5c2 2019-05-12 jcs break;
5387 26ed57b2 2018-05-19 stsp }
5388 e5a0f69f 2018-08-18 stsp
5389 bcbd79e2 2018-08-19 stsp return err;
5390 26ed57b2 2018-05-19 stsp }
5391 26ed57b2 2018-05-19 stsp
5392 4ed7e80c 2018-05-20 stsp static const struct got_error *
5393 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5394 9f7d7167 2018-04-29 stsp {
5395 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5396 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5397 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5398 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5399 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5400 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5401 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5402 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5403 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5404 3dbaef42 2020-11-24 stsp const char *errstr;
5405 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5406 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5407 70ac5f84 2019-03-28 stsp
5408 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5409 26ed57b2 2018-05-19 stsp switch (ch) {
5410 64453f7e 2020-11-21 stsp case 'a':
5411 64453f7e 2020-11-21 stsp force_text_diff = 1;
5412 3dbaef42 2020-11-24 stsp break;
5413 3dbaef42 2020-11-24 stsp case 'C':
5414 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5415 3dbaef42 2020-11-24 stsp &errstr);
5416 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5417 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5418 8f666e67 2022-02-12 thomas errstr, errstr);
5419 64453f7e 2020-11-21 stsp break;
5420 09b5bff8 2020-02-23 naddy case 'r':
5421 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5422 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5423 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5424 09b5bff8 2020-02-23 naddy optarg);
5425 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5426 09b5bff8 2020-02-23 naddy break;
5427 3dbaef42 2020-11-24 stsp case 'w':
5428 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5429 3dbaef42 2020-11-24 stsp break;
5430 26ed57b2 2018-05-19 stsp default:
5431 17020d27 2019-03-07 stsp usage_diff();
5432 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5433 26ed57b2 2018-05-19 stsp }
5434 26ed57b2 2018-05-19 stsp }
5435 26ed57b2 2018-05-19 stsp
5436 26ed57b2 2018-05-19 stsp argc -= optind;
5437 26ed57b2 2018-05-19 stsp argv += optind;
5438 26ed57b2 2018-05-19 stsp
5439 26ed57b2 2018-05-19 stsp if (argc == 0) {
5440 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5441 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5442 15a94983 2018-12-23 stsp id_str1 = argv[0];
5443 15a94983 2018-12-23 stsp id_str2 = argv[1];
5444 26ed57b2 2018-05-19 stsp } else
5445 26ed57b2 2018-05-19 stsp usage_diff();
5446 eb6600df 2019-01-04 stsp
5447 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5448 7cd52833 2022-06-23 thomas if (error)
5449 7cd52833 2022-06-23 thomas goto done;
5450 7cd52833 2022-06-23 thomas
5451 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5452 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5453 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5454 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5455 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5456 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5457 c156c7a4 2020-12-18 stsp goto done;
5458 a273ac94 2020-02-23 naddy if (worktree)
5459 a273ac94 2020-02-23 naddy repo_path =
5460 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5461 a273ac94 2020-02-23 naddy else
5462 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5463 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5464 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5465 c156c7a4 2020-12-18 stsp goto done;
5466 c156c7a4 2020-12-18 stsp }
5467 a273ac94 2020-02-23 naddy }
5468 a273ac94 2020-02-23 naddy
5469 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5470 eb6600df 2019-01-04 stsp if (error)
5471 eb6600df 2019-01-04 stsp goto done;
5472 26ed57b2 2018-05-19 stsp
5473 a273ac94 2020-02-23 naddy init_curses();
5474 a273ac94 2020-02-23 naddy
5475 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5476 51a10b52 2020-12-26 stsp if (error)
5477 51a10b52 2020-12-26 stsp goto done;
5478 51a10b52 2020-12-26 stsp
5479 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5480 26ed57b2 2018-05-19 stsp if (error)
5481 26ed57b2 2018-05-19 stsp goto done;
5482 26ed57b2 2018-05-19 stsp
5483 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5484 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5485 26ed57b2 2018-05-19 stsp if (error)
5486 26ed57b2 2018-05-19 stsp goto done;
5487 26ed57b2 2018-05-19 stsp
5488 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5489 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5490 26ed57b2 2018-05-19 stsp if (error)
5491 26ed57b2 2018-05-19 stsp goto done;
5492 26ed57b2 2018-05-19 stsp
5493 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5494 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5495 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5496 ea5e7bb5 2018-08-01 stsp goto done;
5497 ea5e7bb5 2018-08-01 stsp }
5498 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5499 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5500 5dc9f4bc 2018-08-04 stsp if (error)
5501 5dc9f4bc 2018-08-04 stsp goto done;
5502 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5503 26ed57b2 2018-05-19 stsp done:
5504 3dbaef42 2020-11-24 stsp free(label1);
5505 3dbaef42 2020-11-24 stsp free(label2);
5506 c02c541e 2019-03-29 stsp free(repo_path);
5507 a273ac94 2020-02-23 naddy free(cwd);
5508 1d0f4054 2021-06-17 stsp if (repo) {
5509 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5510 1d0f4054 2021-06-17 stsp if (error == NULL)
5511 1d0f4054 2021-06-17 stsp error = close_err;
5512 1d0f4054 2021-06-17 stsp }
5513 a273ac94 2020-02-23 naddy if (worktree)
5514 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5515 7cd52833 2022-06-23 thomas if (pack_fds) {
5516 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5517 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5518 7cd52833 2022-06-23 thomas if (error == NULL)
5519 7cd52833 2022-06-23 thomas error = pack_err;
5520 7cd52833 2022-06-23 thomas }
5521 51a10b52 2020-12-26 stsp tog_free_refs();
5522 26ed57b2 2018-05-19 stsp return error;
5523 9f7d7167 2018-04-29 stsp }
5524 9f7d7167 2018-04-29 stsp
5525 4ed7e80c 2018-05-20 stsp __dead static void
5526 9f7d7167 2018-04-29 stsp usage_blame(void)
5527 9f7d7167 2018-04-29 stsp {
5528 80ddbec8 2018-04-29 stsp endwin();
5529 91198554 2022-06-23 thomas fprintf(stderr,
5530 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5531 9f7d7167 2018-04-29 stsp getprogname());
5532 9f7d7167 2018-04-29 stsp exit(1);
5533 9f7d7167 2018-04-29 stsp }
5534 84451b3e 2018-07-10 stsp
5535 84451b3e 2018-07-10 stsp struct tog_blame_line {
5536 84451b3e 2018-07-10 stsp int annotated;
5537 84451b3e 2018-07-10 stsp struct got_object_id *id;
5538 84451b3e 2018-07-10 stsp };
5539 9f7d7167 2018-04-29 stsp
5540 4ed7e80c 2018-05-20 stsp static const struct got_error *
5541 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5542 84451b3e 2018-07-10 stsp {
5543 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5544 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5545 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5546 84451b3e 2018-07-10 stsp const struct got_error *err;
5547 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5548 826082fe 2020-12-10 stsp char *line = NULL;
5549 826082fe 2020-12-10 stsp size_t linesize = 0;
5550 826082fe 2020-12-10 stsp ssize_t linelen;
5551 84451b3e 2018-07-10 stsp wchar_t *wline;
5552 27a741e5 2019-09-11 stsp int width;
5553 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5554 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5555 ab089a2a 2018-07-12 stsp char *id_str;
5556 11b20872 2019-11-08 stsp struct tog_color *tc;
5557 ab089a2a 2018-07-12 stsp
5558 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5559 ab089a2a 2018-07-12 stsp if (err)
5560 ab089a2a 2018-07-12 stsp return err;
5561 84451b3e 2018-07-10 stsp
5562 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5563 f7d12f7e 2018-08-01 stsp werase(view->window);
5564 84451b3e 2018-07-10 stsp
5565 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5566 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5567 ab089a2a 2018-07-12 stsp free(id_str);
5568 ab089a2a 2018-07-12 stsp return err;
5569 ab089a2a 2018-07-12 stsp }
5570 ab089a2a 2018-07-12 stsp
5571 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5572 ab089a2a 2018-07-12 stsp free(line);
5573 2550e4c3 2018-07-13 stsp line = NULL;
5574 1cae65b4 2019-09-22 stsp if (err)
5575 1cae65b4 2019-09-22 stsp return err;
5576 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5577 a3404814 2018-09-02 stsp wstandout(view->window);
5578 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5579 11b20872 2019-11-08 stsp if (tc)
5580 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5581 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5582 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5583 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5584 11b20872 2019-11-08 stsp if (tc)
5585 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5586 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5587 a3404814 2018-09-02 stsp wstandend(view->window);
5588 2550e4c3 2018-07-13 stsp free(wline);
5589 2550e4c3 2018-07-13 stsp wline = NULL;
5590 ab089a2a 2018-07-12 stsp
5591 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5592 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5593 07dd3ed3 2022-08-06 thomas
5594 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5595 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5596 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5597 ab089a2a 2018-07-12 stsp free(id_str);
5598 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5599 ab089a2a 2018-07-12 stsp }
5600 ab089a2a 2018-07-12 stsp free(id_str);
5601 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5602 3f60a8ef 2018-07-10 stsp free(line);
5603 2550e4c3 2018-07-13 stsp line = NULL;
5604 3f60a8ef 2018-07-10 stsp if (err)
5605 3f60a8ef 2018-07-10 stsp return err;
5606 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5607 2550e4c3 2018-07-13 stsp free(wline);
5608 2550e4c3 2018-07-13 stsp wline = NULL;
5609 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5610 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5611 3f60a8ef 2018-07-10 stsp
5612 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5613 05171be4 2022-06-23 thomas view->maxx = 0;
5614 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5615 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5616 826082fe 2020-12-10 stsp if (linelen == -1) {
5617 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5618 826082fe 2020-12-10 stsp s->eof = 1;
5619 826082fe 2020-12-10 stsp break;
5620 826082fe 2020-12-10 stsp }
5621 84451b3e 2018-07-10 stsp free(line);
5622 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5623 84451b3e 2018-07-10 stsp }
5624 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5625 07dd3ed3 2022-08-06 thomas continue;
5626 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5627 826082fe 2020-12-10 stsp continue;
5628 cbea3800 2022-06-23 thomas
5629 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5630 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5631 cbea3800 2022-06-23 thomas if (err) {
5632 cbea3800 2022-06-23 thomas free(line);
5633 cbea3800 2022-06-23 thomas return err;
5634 cbea3800 2022-06-23 thomas }
5635 cbea3800 2022-06-23 thomas free(wline);
5636 cbea3800 2022-06-23 thomas wline = NULL;
5637 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5638 84451b3e 2018-07-10 stsp
5639 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5640 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5641 b700b5d6 2018-07-10 stsp
5642 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5643 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5644 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5645 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5646 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5647 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5648 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5649 8d0fe45a 2019-08-12 stsp char *id_str;
5650 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5651 91198554 2022-06-23 thomas blame_line->id);
5652 8d0fe45a 2019-08-12 stsp if (err) {
5653 8d0fe45a 2019-08-12 stsp free(line);
5654 8d0fe45a 2019-08-12 stsp return err;
5655 8d0fe45a 2019-08-12 stsp }
5656 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5657 11b20872 2019-11-08 stsp if (tc)
5658 11b20872 2019-11-08 stsp wattr_on(view->window,
5659 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5660 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5661 11b20872 2019-11-08 stsp if (tc)
5662 11b20872 2019-11-08 stsp wattr_off(view->window,
5663 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5664 8d0fe45a 2019-08-12 stsp free(id_str);
5665 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5666 8d0fe45a 2019-08-12 stsp } else {
5667 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5668 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5669 84451b3e 2018-07-10 stsp }
5670 ee41ec32 2018-07-10 stsp } else {
5671 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5672 ee41ec32 2018-07-10 stsp prev_id = NULL;
5673 ee41ec32 2018-07-10 stsp }
5674 84451b3e 2018-07-10 stsp
5675 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5676 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5677 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5678 27a741e5 2019-09-11 stsp
5679 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5680 41605754 2020-11-12 stsp width = 9;
5681 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5682 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5683 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5684 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5685 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5686 41605754 2020-11-12 stsp if (err) {
5687 41605754 2020-11-12 stsp free(line);
5688 41605754 2020-11-12 stsp return err;
5689 41605754 2020-11-12 stsp }
5690 41605754 2020-11-12 stsp width += 9;
5691 41605754 2020-11-12 stsp } else {
5692 923086fd 2022-06-23 thomas int skip;
5693 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5694 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5695 923086fd 2022-06-23 thomas if (err) {
5696 923086fd 2022-06-23 thomas free(line);
5697 923086fd 2022-06-23 thomas return err;
5698 05171be4 2022-06-23 thomas }
5699 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5700 02bce7e2 2022-06-23 thomas width += 9;
5701 41605754 2020-11-12 stsp free(wline);
5702 41605754 2020-11-12 stsp wline = NULL;
5703 41605754 2020-11-12 stsp }
5704 41605754 2020-11-12 stsp
5705 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5706 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5707 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5708 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5709 84451b3e 2018-07-10 stsp }
5710 826082fe 2020-12-10 stsp free(line);
5711 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5712 84451b3e 2018-07-10 stsp
5713 a5d43cac 2022-07-01 thomas view_border(view);
5714 84451b3e 2018-07-10 stsp
5715 84451b3e 2018-07-10 stsp return NULL;
5716 84451b3e 2018-07-10 stsp }
5717 84451b3e 2018-07-10 stsp
5718 84451b3e 2018-07-10 stsp static const struct got_error *
5719 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5720 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5721 84451b3e 2018-07-10 stsp {
5722 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5723 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5724 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5725 1a76625f 2018-10-22 stsp int errcode;
5726 84451b3e 2018-07-10 stsp
5727 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5728 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5729 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5730 84451b3e 2018-07-10 stsp
5731 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5732 1a76625f 2018-10-22 stsp if (errcode)
5733 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5734 84451b3e 2018-07-10 stsp
5735 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5736 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5737 d68a0a7d 2018-07-10 stsp goto done;
5738 d68a0a7d 2018-07-10 stsp }
5739 d68a0a7d 2018-07-10 stsp
5740 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5741 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5742 d68a0a7d 2018-07-10 stsp
5743 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5744 d68a0a7d 2018-07-10 stsp if (line->annotated)
5745 d68a0a7d 2018-07-10 stsp goto done;
5746 d68a0a7d 2018-07-10 stsp
5747 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5748 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5749 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5750 84451b3e 2018-07-10 stsp goto done;
5751 84451b3e 2018-07-10 stsp }
5752 84451b3e 2018-07-10 stsp line->annotated = 1;
5753 84451b3e 2018-07-10 stsp done:
5754 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5755 1a76625f 2018-10-22 stsp if (errcode)
5756 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5757 84451b3e 2018-07-10 stsp return err;
5758 84451b3e 2018-07-10 stsp }
5759 84451b3e 2018-07-10 stsp
5760 84451b3e 2018-07-10 stsp static void *
5761 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5762 84451b3e 2018-07-10 stsp {
5763 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5764 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5765 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5766 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5767 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5768 00a8878e 2022-07-01 thomas
5769 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5770 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5771 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5772 9117a7b7 2022-07-01 thomas
5773 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5774 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5775 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5776 9117a7b7 2022-07-01 thomas goto done;
5777 9117a7b7 2022-07-01 thomas }
5778 18430de3 2018-07-10 stsp
5779 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5780 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5781 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5782 9117a7b7 2022-07-01 thomas goto done;
5783 9117a7b7 2022-07-01 thomas }
5784 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5785 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5786 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5787 9117a7b7 2022-07-01 thomas goto done;
5788 9117a7b7 2022-07-01 thomas }
5789 9117a7b7 2022-07-01 thomas
5790 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5791 61266923 2020-01-14 stsp if (err)
5792 9117a7b7 2022-07-01 thomas goto done;
5793 61266923 2020-01-14 stsp
5794 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5795 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5796 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5797 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5798 fc06ba56 2019-08-22 stsp err = NULL;
5799 18430de3 2018-07-10 stsp
5800 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5801 9117a7b7 2022-07-01 thomas if (errcode) {
5802 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5803 9117a7b7 2022-07-01 thomas goto done;
5804 9117a7b7 2022-07-01 thomas }
5805 18430de3 2018-07-10 stsp
5806 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5807 1d0f4054 2021-06-17 stsp if (err == NULL)
5808 1d0f4054 2021-06-17 stsp err = close_err;
5809 c9beca56 2018-07-22 stsp ta->repo = NULL;
5810 c9beca56 2018-07-22 stsp *ta->complete = 1;
5811 18430de3 2018-07-10 stsp
5812 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5813 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5814 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5815 18430de3 2018-07-10 stsp
5816 9117a7b7 2022-07-01 thomas done:
5817 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5818 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5819 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5820 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5821 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5822 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5823 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5824 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5825 00a8878e 2022-07-01 thomas
5826 18430de3 2018-07-10 stsp return (void *)err;
5827 84451b3e 2018-07-10 stsp }
5828 84451b3e 2018-07-10 stsp
5829 245d91c1 2018-07-12 stsp static struct got_object_id *
5830 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5831 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5832 245d91c1 2018-07-12 stsp {
5833 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5834 8d0fe45a 2019-08-12 stsp
5835 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5836 8d0fe45a 2019-08-12 stsp return NULL;
5837 b880a918 2018-07-10 stsp
5838 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5839 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5840 4fc71f3b 2022-07-12 thomas return NULL;
5841 4fc71f3b 2022-07-12 thomas
5842 4fc71f3b 2022-07-12 thomas return line->id;
5843 4fc71f3b 2022-07-12 thomas }
5844 4fc71f3b 2022-07-12 thomas
5845 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5846 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5847 4fc71f3b 2022-07-12 thomas int lineno)
5848 4fc71f3b 2022-07-12 thomas {
5849 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5850 4fc71f3b 2022-07-12 thomas
5851 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5852 4fc71f3b 2022-07-12 thomas return NULL;
5853 4fc71f3b 2022-07-12 thomas
5854 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5855 245d91c1 2018-07-12 stsp if (!line->annotated)
5856 245d91c1 2018-07-12 stsp return NULL;
5857 245d91c1 2018-07-12 stsp
5858 245d91c1 2018-07-12 stsp return line->id;
5859 b880a918 2018-07-10 stsp }
5860 245d91c1 2018-07-12 stsp
5861 b880a918 2018-07-10 stsp static const struct got_error *
5862 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5863 a70480e0 2018-06-23 stsp {
5864 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5865 245d91c1 2018-07-12 stsp int i;
5866 245d91c1 2018-07-12 stsp
5867 245d91c1 2018-07-12 stsp if (blame->thread) {
5868 1a76625f 2018-10-22 stsp int errcode;
5869 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5870 1a76625f 2018-10-22 stsp if (errcode)
5871 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5872 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5873 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5874 1a76625f 2018-10-22 stsp if (errcode)
5875 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5876 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5877 1a76625f 2018-10-22 stsp if (errcode)
5878 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5879 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5880 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5881 245d91c1 2018-07-12 stsp err = NULL;
5882 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5883 245d91c1 2018-07-12 stsp }
5884 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5885 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5886 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5887 1d0f4054 2021-06-17 stsp if (err == NULL)
5888 1d0f4054 2021-06-17 stsp err = close_err;
5889 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5890 245d91c1 2018-07-12 stsp }
5891 245d91c1 2018-07-12 stsp if (blame->f) {
5892 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5893 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5894 245d91c1 2018-07-12 stsp blame->f = NULL;
5895 245d91c1 2018-07-12 stsp }
5896 57670559 2018-12-23 stsp if (blame->lines) {
5897 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5898 57670559 2018-12-23 stsp free(blame->lines[i].id);
5899 57670559 2018-12-23 stsp free(blame->lines);
5900 57670559 2018-12-23 stsp blame->lines = NULL;
5901 57670559 2018-12-23 stsp }
5902 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5903 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5904 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5905 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5906 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5907 7cd52833 2022-06-23 thomas if (err == NULL)
5908 7cd52833 2022-06-23 thomas err = pack_err;
5909 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5910 7cd52833 2022-06-23 thomas }
5911 245d91c1 2018-07-12 stsp return err;
5912 245d91c1 2018-07-12 stsp }
5913 245d91c1 2018-07-12 stsp
5914 245d91c1 2018-07-12 stsp static const struct got_error *
5915 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5916 fc06ba56 2019-08-22 stsp {
5917 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5918 fc06ba56 2019-08-22 stsp int *done = arg;
5919 fc06ba56 2019-08-22 stsp int errcode;
5920 fc06ba56 2019-08-22 stsp
5921 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5922 fc06ba56 2019-08-22 stsp if (errcode)
5923 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5924 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5925 fc06ba56 2019-08-22 stsp
5926 fc06ba56 2019-08-22 stsp if (*done)
5927 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5928 fc06ba56 2019-08-22 stsp
5929 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5930 fc06ba56 2019-08-22 stsp if (errcode)
5931 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5932 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5933 fc06ba56 2019-08-22 stsp
5934 fc06ba56 2019-08-22 stsp return err;
5935 fc06ba56 2019-08-22 stsp }
5936 fc06ba56 2019-08-22 stsp
5937 fc06ba56 2019-08-22 stsp static const struct got_error *
5938 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5939 245d91c1 2018-07-12 stsp {
5940 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5941 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5942 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5943 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5944 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5945 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5946 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5947 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5948 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5949 a70480e0 2018-06-23 stsp
5950 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5951 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5952 27d434c2 2018-09-15 stsp if (err)
5953 15a94983 2018-12-23 stsp return err;
5954 f4ae6ddb 2022-07-01 thomas
5955 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5956 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5957 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5958 f4ae6ddb 2022-07-01 thomas goto done;
5959 f4ae6ddb 2022-07-01 thomas }
5960 945f9229 2022-04-16 thomas
5961 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5962 945f9229 2022-04-16 thomas if (err)
5963 945f9229 2022-04-16 thomas goto done;
5964 27d434c2 2018-09-15 stsp
5965 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5966 84451b3e 2018-07-10 stsp if (err)
5967 84451b3e 2018-07-10 stsp goto done;
5968 27d434c2 2018-09-15 stsp
5969 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5970 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5971 84451b3e 2018-07-10 stsp goto done;
5972 84451b3e 2018-07-10 stsp }
5973 a70480e0 2018-06-23 stsp
5974 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5975 a70480e0 2018-06-23 stsp if (err)
5976 a70480e0 2018-06-23 stsp goto done;
5977 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5978 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5979 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5980 84451b3e 2018-07-10 stsp goto done;
5981 84451b3e 2018-07-10 stsp }
5982 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5983 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5984 1fddf795 2021-01-20 stsp if (err)
5985 1fddf795 2021-01-20 stsp goto done;
5986 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5987 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5988 84451b3e 2018-07-10 stsp goto done;
5989 1fddf795 2021-01-20 stsp }
5990 b02560ec 2019-08-19 stsp
5991 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5992 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5993 b02560ec 2019-08-19 stsp blame->nlines--;
5994 a70480e0 2018-06-23 stsp
5995 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5996 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5997 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5998 84451b3e 2018-07-10 stsp goto done;
5999 84451b3e 2018-07-10 stsp }
6000 a70480e0 2018-06-23 stsp
6001 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6002 bd24772e 2018-07-11 stsp if (err)
6003 bd24772e 2018-07-11 stsp goto done;
6004 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6005 7cd52833 2022-06-23 thomas pack_fds);
6006 7cd52833 2022-06-23 thomas if (err)
6007 7cd52833 2022-06-23 thomas goto done;
6008 bd24772e 2018-07-11 stsp
6009 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6010 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6011 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6012 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6013 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6014 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6015 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6016 245d91c1 2018-07-12 stsp goto done;
6017 245d91c1 2018-07-12 stsp }
6018 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6019 245d91c1 2018-07-12 stsp
6020 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6021 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6022 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6023 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6024 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6025 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6026 a5388363 2020-12-01 naddy s->blame_complete = 0;
6027 f5a09613 2020-12-13 naddy
6028 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6029 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6030 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6031 f5a09613 2020-12-13 naddy s->selected_line = 1;
6032 f5a09613 2020-12-13 naddy }
6033 aa61903a 2021-12-31 thomas s->matched_line = 0;
6034 245d91c1 2018-07-12 stsp
6035 245d91c1 2018-07-12 stsp done:
6036 945f9229 2022-04-16 thomas if (commit)
6037 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6038 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6039 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6040 245d91c1 2018-07-12 stsp if (blob)
6041 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6042 27d434c2 2018-09-15 stsp free(obj_id);
6043 245d91c1 2018-07-12 stsp if (err)
6044 245d91c1 2018-07-12 stsp stop_blame(blame);
6045 245d91c1 2018-07-12 stsp return err;
6046 245d91c1 2018-07-12 stsp }
6047 245d91c1 2018-07-12 stsp
6048 245d91c1 2018-07-12 stsp static const struct got_error *
6049 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6050 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6051 245d91c1 2018-07-12 stsp {
6052 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6053 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6054 dbc6a6b6 2018-07-12 stsp
6055 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6056 245d91c1 2018-07-12 stsp
6057 c4843652 2019-08-12 stsp s->path = strdup(path);
6058 c4843652 2019-08-12 stsp if (s->path == NULL)
6059 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6060 c4843652 2019-08-12 stsp
6061 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6062 c4843652 2019-08-12 stsp if (err) {
6063 c4843652 2019-08-12 stsp free(s->path);
6064 7cbe629d 2018-08-04 stsp return err;
6065 c4843652 2019-08-12 stsp }
6066 245d91c1 2018-07-12 stsp
6067 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6068 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6069 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6070 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6071 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6072 fb2756b9 2018-08-04 stsp s->repo = repo;
6073 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6074 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6075 7cbe629d 2018-08-04 stsp
6076 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6077 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6078 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6079 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6080 11b20872 2019-11-08 stsp if (err)
6081 11b20872 2019-11-08 stsp return err;
6082 11b20872 2019-11-08 stsp }
6083 11b20872 2019-11-08 stsp
6084 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6085 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6086 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6087 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6088 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6089 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6090 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6091 e5a0f69f 2018-08-18 stsp
6092 a5388363 2020-12-01 naddy return run_blame(view);
6093 7cbe629d 2018-08-04 stsp }
6094 7cbe629d 2018-08-04 stsp
6095 e5a0f69f 2018-08-18 stsp static const struct got_error *
6096 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6097 7cbe629d 2018-08-04 stsp {
6098 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6099 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6100 7cbe629d 2018-08-04 stsp
6101 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6102 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6103 e5a0f69f 2018-08-18 stsp
6104 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6105 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6106 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6107 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6108 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6109 7cbe629d 2018-08-04 stsp }
6110 e5a0f69f 2018-08-18 stsp
6111 e5a0f69f 2018-08-18 stsp free(s->path);
6112 11b20872 2019-11-08 stsp free_colors(&s->colors);
6113 e5a0f69f 2018-08-18 stsp return err;
6114 7cbe629d 2018-08-04 stsp }
6115 7cbe629d 2018-08-04 stsp
6116 7cbe629d 2018-08-04 stsp static const struct got_error *
6117 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6118 6c4c42e0 2019-06-24 stsp {
6119 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6120 6c4c42e0 2019-06-24 stsp
6121 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6122 6c4c42e0 2019-06-24 stsp return NULL;
6123 2a7d3cd7 2022-09-17 thomas }
6124 2a7d3cd7 2022-09-17 thomas
6125 2a7d3cd7 2022-09-17 thomas static void
6126 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6127 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6128 2a7d3cd7 2022-09-17 thomas {
6129 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6130 2a7d3cd7 2022-09-17 thomas
6131 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6132 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6133 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6134 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6135 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6136 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6137 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6138 6c4c42e0 2019-06-24 stsp }
6139 6c4c42e0 2019-06-24 stsp
6140 6c4c42e0 2019-06-24 stsp static const struct got_error *
6141 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6142 7cbe629d 2018-08-04 stsp {
6143 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6144 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6145 2b380cc8 2018-10-24 stsp int errcode;
6146 2b380cc8 2018-10-24 stsp
6147 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6148 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6149 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6150 2b380cc8 2018-10-24 stsp if (errcode)
6151 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6152 51fe7530 2019-08-19 stsp
6153 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6154 2b380cc8 2018-10-24 stsp }
6155 e5a0f69f 2018-08-18 stsp
6156 51fe7530 2019-08-19 stsp if (s->blame_complete)
6157 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6158 51fe7530 2019-08-19 stsp
6159 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6160 e5a0f69f 2018-08-18 stsp
6161 a5d43cac 2022-07-01 thomas view_border(view);
6162 e5a0f69f 2018-08-18 stsp return err;
6163 e5a0f69f 2018-08-18 stsp }
6164 e5a0f69f 2018-08-18 stsp
6165 e5a0f69f 2018-08-18 stsp static const struct got_error *
6166 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6167 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6168 eaeaa612 2022-07-20 thomas {
6169 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6170 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6171 eaeaa612 2022-07-20 thomas
6172 eaeaa612 2022-07-20 thomas *new_view = NULL;
6173 eaeaa612 2022-07-20 thomas
6174 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6175 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6176 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6177 eaeaa612 2022-07-20 thomas
6178 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6179 eaeaa612 2022-07-20 thomas if (err)
6180 eaeaa612 2022-07-20 thomas view_close(log_view);
6181 eaeaa612 2022-07-20 thomas else
6182 eaeaa612 2022-07-20 thomas *new_view = log_view;
6183 eaeaa612 2022-07-20 thomas
6184 eaeaa612 2022-07-20 thomas return err;
6185 eaeaa612 2022-07-20 thomas }
6186 eaeaa612 2022-07-20 thomas
6187 eaeaa612 2022-07-20 thomas static const struct got_error *
6188 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6189 e5a0f69f 2018-08-18 stsp {
6190 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6191 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6192 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6193 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6194 a5d43cac 2022-07-01 thomas
6195 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6196 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6197 a5d43cac 2022-07-01 thomas --eos; /* border */
6198 7cbe629d 2018-08-04 stsp
6199 e5a0f69f 2018-08-18 stsp switch (ch) {
6200 05171be4 2022-06-23 thomas case '0':
6201 05171be4 2022-06-23 thomas view->x = 0;
6202 05171be4 2022-06-23 thomas break;
6203 05171be4 2022-06-23 thomas case '$':
6204 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
6205 07b0611c 2022-06-23 thomas view->count = 0;
6206 05171be4 2022-06-23 thomas break;
6207 05171be4 2022-06-23 thomas case KEY_RIGHT:
6208 05171be4 2022-06-23 thomas case 'l':
6209 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
6210 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
6211 07b0611c 2022-06-23 thomas else
6212 07b0611c 2022-06-23 thomas view->count = 0;
6213 05171be4 2022-06-23 thomas break;
6214 05171be4 2022-06-23 thomas case KEY_LEFT:
6215 05171be4 2022-06-23 thomas case 'h':
6216 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
6217 07b0611c 2022-06-23 thomas if (view->x <= 0)
6218 07b0611c 2022-06-23 thomas view->count = 0;
6219 05171be4 2022-06-23 thomas break;
6220 1e37a5c2 2019-05-12 jcs case 'q':
6221 1e37a5c2 2019-05-12 jcs s->done = 1;
6222 4deef56f 2021-09-02 naddy break;
6223 4deef56f 2021-09-02 naddy case 'g':
6224 4deef56f 2021-09-02 naddy case KEY_HOME:
6225 4deef56f 2021-09-02 naddy s->selected_line = 1;
6226 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6227 07b0611c 2022-06-23 thomas view->count = 0;
6228 4deef56f 2021-09-02 naddy break;
6229 4deef56f 2021-09-02 naddy case 'G':
6230 4deef56f 2021-09-02 naddy case KEY_END:
6231 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6232 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6233 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6234 4deef56f 2021-09-02 naddy } else {
6235 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6236 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6237 4deef56f 2021-09-02 naddy }
6238 07b0611c 2022-06-23 thomas view->count = 0;
6239 1e37a5c2 2019-05-12 jcs break;
6240 1e37a5c2 2019-05-12 jcs case 'k':
6241 1e37a5c2 2019-05-12 jcs case KEY_UP:
6242 f7140bf5 2021-10-17 thomas case CTRL('p'):
6243 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6244 1e37a5c2 2019-05-12 jcs s->selected_line--;
6245 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6246 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6247 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6248 07b0611c 2022-06-23 thomas else
6249 07b0611c 2022-06-23 thomas view->count = 0;
6250 1e37a5c2 2019-05-12 jcs break;
6251 70f17a53 2022-06-13 thomas case CTRL('u'):
6252 23427b14 2022-06-23 thomas case 'u':
6253 70f17a53 2022-06-13 thomas nscroll /= 2;
6254 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6255 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6256 ea025d1d 2020-02-22 naddy case CTRL('b'):
6257 1c5e5faa 2022-06-23 thomas case 'b':
6258 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6259 07b0611c 2022-06-23 thomas if (view->count > 1)
6260 07b0611c 2022-06-23 thomas nscroll += nscroll;
6261 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6262 07b0611c 2022-06-23 thomas view->count = 0;
6263 e5a0f69f 2018-08-18 stsp break;
6264 1e37a5c2 2019-05-12 jcs }
6265 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6266 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6267 1e37a5c2 2019-05-12 jcs else
6268 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6269 1e37a5c2 2019-05-12 jcs break;
6270 1e37a5c2 2019-05-12 jcs case 'j':
6271 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6272 f7140bf5 2021-10-17 thomas case CTRL('n'):
6273 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6274 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6275 1e37a5c2 2019-05-12 jcs s->selected_line++;
6276 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6277 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6278 07b0611c 2022-06-23 thomas else
6279 07b0611c 2022-06-23 thomas view->count = 0;
6280 1e37a5c2 2019-05-12 jcs break;
6281 1c5e5faa 2022-06-23 thomas case 'c':
6282 1e37a5c2 2019-05-12 jcs case 'p': {
6283 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6284 07b0611c 2022-06-23 thomas
6285 07b0611c 2022-06-23 thomas view->count = 0;
6286 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6287 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6288 1e37a5c2 2019-05-12 jcs if (id == NULL)
6289 e5a0f69f 2018-08-18 stsp break;
6290 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6291 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6292 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6293 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6294 1e37a5c2 2019-05-12 jcs int obj_type;
6295 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6296 1e37a5c2 2019-05-12 jcs s->repo, id);
6297 e5a0f69f 2018-08-18 stsp if (err)
6298 e5a0f69f 2018-08-18 stsp break;
6299 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6300 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6301 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6302 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6303 e5a0f69f 2018-08-18 stsp break;
6304 e5a0f69f 2018-08-18 stsp }
6305 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6306 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6307 ec242592 2022-04-22 thomas s->repo, &pid->id);
6308 945f9229 2022-04-16 thomas if (err)
6309 945f9229 2022-04-16 thomas break;
6310 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6311 945f9229 2022-04-16 thomas pcommit, s->path);
6312 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6313 e5a0f69f 2018-08-18 stsp if (err) {
6314 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6315 1e37a5c2 2019-05-12 jcs err = NULL;
6316 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6317 e5a0f69f 2018-08-18 stsp break;
6318 e5a0f69f 2018-08-18 stsp }
6319 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6320 1e37a5c2 2019-05-12 jcs blob_id);
6321 1e37a5c2 2019-05-12 jcs free(blob_id);
6322 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6323 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6324 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6325 e5a0f69f 2018-08-18 stsp break;
6326 1e37a5c2 2019-05-12 jcs }
6327 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6328 ec242592 2022-04-22 thomas &pid->id);
6329 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6330 1e37a5c2 2019-05-12 jcs } else {
6331 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6332 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6333 1e37a5c2 2019-05-12 jcs break;
6334 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6335 1e37a5c2 2019-05-12 jcs id);
6336 1e37a5c2 2019-05-12 jcs }
6337 1e37a5c2 2019-05-12 jcs if (err)
6338 e5a0f69f 2018-08-18 stsp break;
6339 1e37a5c2 2019-05-12 jcs s->done = 1;
6340 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6341 1e37a5c2 2019-05-12 jcs s->done = 0;
6342 1e37a5c2 2019-05-12 jcs if (thread_err)
6343 1e37a5c2 2019-05-12 jcs break;
6344 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6345 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6346 a5388363 2020-12-01 naddy err = run_blame(view);
6347 1e37a5c2 2019-05-12 jcs if (err)
6348 1e37a5c2 2019-05-12 jcs break;
6349 1e37a5c2 2019-05-12 jcs break;
6350 1e37a5c2 2019-05-12 jcs }
6351 1c5e5faa 2022-06-23 thomas case 'C': {
6352 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6353 07b0611c 2022-06-23 thomas
6354 07b0611c 2022-06-23 thomas view->count = 0;
6355 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6356 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6357 1e37a5c2 2019-05-12 jcs break;
6358 1e37a5c2 2019-05-12 jcs s->done = 1;
6359 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6360 1e37a5c2 2019-05-12 jcs s->done = 0;
6361 1e37a5c2 2019-05-12 jcs if (thread_err)
6362 1e37a5c2 2019-05-12 jcs break;
6363 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6364 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6365 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6366 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6367 a5388363 2020-12-01 naddy err = run_blame(view);
6368 1e37a5c2 2019-05-12 jcs if (err)
6369 1e37a5c2 2019-05-12 jcs break;
6370 1e37a5c2 2019-05-12 jcs break;
6371 1e37a5c2 2019-05-12 jcs }
6372 2a31b33b 2022-07-23 thomas case 'L':
6373 eaeaa612 2022-07-20 thomas view->count = 0;
6374 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6375 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6376 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6377 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6378 eaeaa612 2022-07-20 thomas break;
6379 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6380 1e37a5c2 2019-05-12 jcs case '\r': {
6381 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6382 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6383 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6384 07b0611c 2022-06-23 thomas
6385 07b0611c 2022-06-23 thomas view->count = 0;
6386 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6387 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6388 1e37a5c2 2019-05-12 jcs if (id == NULL)
6389 1e37a5c2 2019-05-12 jcs break;
6390 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6391 1e37a5c2 2019-05-12 jcs if (err)
6392 1e37a5c2 2019-05-12 jcs break;
6393 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6394 4fc71f3b 2022-07-12 thomas if (*new_view) {
6395 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6396 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6397 4fc71f3b 2022-07-12 thomas if (err)
6398 4fc71f3b 2022-07-12 thomas break;
6399 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6400 4fc71f3b 2022-07-12 thomas } else {
6401 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6402 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6403 a5d43cac 2022-07-01 thomas
6404 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6405 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6406 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6407 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6408 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6409 4fc71f3b 2022-07-12 thomas break;
6410 4fc71f3b 2022-07-12 thomas }
6411 15a94983 2018-12-23 stsp }
6412 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6413 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6414 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6415 1e37a5c2 2019-05-12 jcs if (err) {
6416 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6417 1e37a5c2 2019-05-12 jcs break;
6418 1e37a5c2 2019-05-12 jcs }
6419 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6420 4fc71f3b 2022-07-12 thomas s->selected_line;
6421 4fc71f3b 2022-07-12 thomas if (*new_view)
6422 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6423 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6424 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6425 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6426 a5d43cac 2022-07-01 thomas if (err)
6427 a5d43cac 2022-07-01 thomas break;
6428 a5d43cac 2022-07-01 thomas }
6429 a5d43cac 2022-07-01 thomas
6430 e78dc838 2020-12-04 stsp view->focussed = 0;
6431 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6432 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6433 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6434 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6435 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6436 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6437 1e37a5c2 2019-05-12 jcs if (err)
6438 34bc9ec9 2019-02-22 stsp break;
6439 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6440 40236d76 2022-06-23 thomas if (err)
6441 40236d76 2022-06-23 thomas break;
6442 e78dc838 2020-12-04 stsp view->focus_child = 1;
6443 1e37a5c2 2019-05-12 jcs } else
6444 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6445 1e37a5c2 2019-05-12 jcs if (err)
6446 e5a0f69f 2018-08-18 stsp break;
6447 1e37a5c2 2019-05-12 jcs break;
6448 1e37a5c2 2019-05-12 jcs }
6449 70f17a53 2022-06-13 thomas case CTRL('d'):
6450 23427b14 2022-06-23 thomas case 'd':
6451 70f17a53 2022-06-13 thomas nscroll /= 2;
6452 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6453 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6454 ea025d1d 2020-02-22 naddy case CTRL('f'):
6455 1c5e5faa 2022-06-23 thomas case 'f':
6456 1e37a5c2 2019-05-12 jcs case ' ':
6457 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6458 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6459 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6460 07b0611c 2022-06-23 thomas view->count = 0;
6461 e5a0f69f 2018-08-18 stsp break;
6462 1e37a5c2 2019-05-12 jcs }
6463 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6464 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6465 70f17a53 2022-06-13 thomas s->selected_line +=
6466 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6467 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6468 1e37a5c2 2019-05-12 jcs }
6469 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6470 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6471 1e37a5c2 2019-05-12 jcs else
6472 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6473 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6474 1e37a5c2 2019-05-12 jcs break;
6475 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6476 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6477 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6478 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6479 1e37a5c2 2019-05-12 jcs }
6480 1e37a5c2 2019-05-12 jcs break;
6481 1e37a5c2 2019-05-12 jcs default:
6482 07b0611c 2022-06-23 thomas view->count = 0;
6483 1e37a5c2 2019-05-12 jcs break;
6484 a70480e0 2018-06-23 stsp }
6485 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6486 adf4c9e0 2022-07-03 thomas }
6487 adf4c9e0 2022-07-03 thomas
6488 adf4c9e0 2022-07-03 thomas static const struct got_error *
6489 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6490 adf4c9e0 2022-07-03 thomas {
6491 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6492 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6493 adf4c9e0 2022-07-03 thomas
6494 adf4c9e0 2022-07-03 thomas view->count = 0;
6495 adf4c9e0 2022-07-03 thomas s->done = 1;
6496 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6497 adf4c9e0 2022-07-03 thomas s->done = 0;
6498 adf4c9e0 2022-07-03 thomas if (err)
6499 adf4c9e0 2022-07-03 thomas return err;
6500 adf4c9e0 2022-07-03 thomas return run_blame(view);
6501 a70480e0 2018-06-23 stsp }
6502 a70480e0 2018-06-23 stsp
6503 a70480e0 2018-06-23 stsp static const struct got_error *
6504 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6505 9f7d7167 2018-04-29 stsp {
6506 a70480e0 2018-06-23 stsp const struct got_error *error;
6507 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6508 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6509 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6510 0587e10c 2020-07-23 stsp char *link_target = NULL;
6511 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6512 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6513 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6514 a70480e0 2018-06-23 stsp int ch;
6515 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6516 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6517 a70480e0 2018-06-23 stsp
6518 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6519 a70480e0 2018-06-23 stsp switch (ch) {
6520 a70480e0 2018-06-23 stsp case 'c':
6521 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6522 a70480e0 2018-06-23 stsp break;
6523 69069811 2018-08-02 stsp case 'r':
6524 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6525 69069811 2018-08-02 stsp if (repo_path == NULL)
6526 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6527 9ba1d308 2019-10-21 stsp optarg);
6528 69069811 2018-08-02 stsp break;
6529 a70480e0 2018-06-23 stsp default:
6530 17020d27 2019-03-07 stsp usage_blame();
6531 a70480e0 2018-06-23 stsp /* NOTREACHED */
6532 a70480e0 2018-06-23 stsp }
6533 a70480e0 2018-06-23 stsp }
6534 a70480e0 2018-06-23 stsp
6535 a70480e0 2018-06-23 stsp argc -= optind;
6536 a70480e0 2018-06-23 stsp argv += optind;
6537 a70480e0 2018-06-23 stsp
6538 f135c941 2020-02-20 stsp if (argc != 1)
6539 a70480e0 2018-06-23 stsp usage_blame();
6540 6962eb72 2020-02-20 stsp
6541 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6542 7cd52833 2022-06-23 thomas if (error != NULL)
6543 7cd52833 2022-06-23 thomas goto done;
6544 7cd52833 2022-06-23 thomas
6545 69069811 2018-08-02 stsp if (repo_path == NULL) {
6546 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6547 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6548 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6549 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6550 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6551 c156c7a4 2020-12-18 stsp goto done;
6552 f135c941 2020-02-20 stsp if (worktree)
6553 eb41ed75 2019-02-05 stsp repo_path =
6554 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6555 f135c941 2020-02-20 stsp else
6556 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6557 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6558 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6559 c156c7a4 2020-12-18 stsp goto done;
6560 c156c7a4 2020-12-18 stsp }
6561 f135c941 2020-02-20 stsp }
6562 a915003a 2019-02-05 stsp
6563 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6564 c02c541e 2019-03-29 stsp if (error != NULL)
6565 8e94dd5b 2019-01-04 stsp goto done;
6566 69069811 2018-08-02 stsp
6567 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6568 f135c941 2020-02-20 stsp worktree);
6569 c02c541e 2019-03-29 stsp if (error)
6570 92205607 2019-01-04 stsp goto done;
6571 69069811 2018-08-02 stsp
6572 f135c941 2020-02-20 stsp init_curses();
6573 f135c941 2020-02-20 stsp
6574 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6575 51a10b52 2020-12-26 stsp if (error)
6576 51a10b52 2020-12-26 stsp goto done;
6577 51a10b52 2020-12-26 stsp
6578 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6579 eb41ed75 2019-02-05 stsp if (error)
6580 69069811 2018-08-02 stsp goto done;
6581 a70480e0 2018-06-23 stsp
6582 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6583 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6584 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6585 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6586 a70480e0 2018-06-23 stsp if (error != NULL)
6587 66b4983c 2018-06-23 stsp goto done;
6588 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6589 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6590 a70480e0 2018-06-23 stsp } else {
6591 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6592 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6593 a70480e0 2018-06-23 stsp }
6594 a19e88aa 2018-06-23 stsp if (error != NULL)
6595 8b473291 2019-02-21 stsp goto done;
6596 8b473291 2019-02-21 stsp
6597 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6598 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6599 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6600 e1cd8fed 2018-08-01 stsp goto done;
6601 e1cd8fed 2018-08-01 stsp }
6602 0587e10c 2020-07-23 stsp
6603 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6604 945f9229 2022-04-16 thomas if (error)
6605 945f9229 2022-04-16 thomas goto done;
6606 945f9229 2022-04-16 thomas
6607 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6608 945f9229 2022-04-16 thomas commit, repo);
6609 7cbe629d 2018-08-04 stsp if (error)
6610 7cbe629d 2018-08-04 stsp goto done;
6611 0587e10c 2020-07-23 stsp
6612 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6613 78756c87 2020-11-24 stsp commit_id, repo);
6614 0587e10c 2020-07-23 stsp if (error)
6615 0587e10c 2020-07-23 stsp goto done;
6616 12314ad4 2019-08-31 stsp if (worktree) {
6617 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6618 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6619 12314ad4 2019-08-31 stsp worktree = NULL;
6620 12314ad4 2019-08-31 stsp }
6621 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6622 a70480e0 2018-06-23 stsp done:
6623 69069811 2018-08-02 stsp free(repo_path);
6624 f135c941 2020-02-20 stsp free(in_repo_path);
6625 0587e10c 2020-07-23 stsp free(link_target);
6626 69069811 2018-08-02 stsp free(cwd);
6627 a70480e0 2018-06-23 stsp free(commit_id);
6628 945f9229 2022-04-16 thomas if (commit)
6629 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6630 eb41ed75 2019-02-05 stsp if (worktree)
6631 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6632 1d0f4054 2021-06-17 stsp if (repo) {
6633 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6634 1d0f4054 2021-06-17 stsp if (error == NULL)
6635 1d0f4054 2021-06-17 stsp error = close_err;
6636 1d0f4054 2021-06-17 stsp }
6637 7cd52833 2022-06-23 thomas if (pack_fds) {
6638 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6639 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6640 7cd52833 2022-06-23 thomas if (error == NULL)
6641 7cd52833 2022-06-23 thomas error = pack_err;
6642 7cd52833 2022-06-23 thomas }
6643 51a10b52 2020-12-26 stsp tog_free_refs();
6644 a70480e0 2018-06-23 stsp return error;
6645 ffd1d5e5 2018-06-23 stsp }
6646 ffd1d5e5 2018-06-23 stsp
6647 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6648 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6649 ffd1d5e5 2018-06-23 stsp {
6650 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6651 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6652 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6653 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6654 86f4aab9 2022-09-09 thomas char *index = NULL;
6655 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6656 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6657 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6658 ffd1d5e5 2018-06-23 stsp
6659 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6660 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6661 a5d43cac 2022-07-01 thomas --limit; /* border */
6662 ffd1d5e5 2018-06-23 stsp
6663 f7d12f7e 2018-08-01 stsp werase(view->window);
6664 ffd1d5e5 2018-06-23 stsp
6665 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6666 ffd1d5e5 2018-06-23 stsp return NULL;
6667 ffd1d5e5 2018-06-23 stsp
6668 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6669 f91a2b48 2022-06-23 thomas 0, 0);
6670 ffd1d5e5 2018-06-23 stsp if (err)
6671 ffd1d5e5 2018-06-23 stsp return err;
6672 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6673 a3404814 2018-09-02 stsp wstandout(view->window);
6674 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6675 11b20872 2019-11-08 stsp if (tc)
6676 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6677 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6678 86f4aab9 2022-09-09 thomas free(wline);
6679 86f4aab9 2022-09-09 thomas wline = NULL;
6680 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6681 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6682 11b20872 2019-11-08 stsp if (tc)
6683 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6684 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6685 a3404814 2018-09-02 stsp wstandend(view->window);
6686 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6687 86f4aab9 2022-09-09 thomas return NULL;
6688 07dd3ed3 2022-08-06 thomas
6689 6f5f393a 2022-08-12 thomas i += s->selected;
6690 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6691 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6692 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6693 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6694 07dd3ed3 2022-08-06 thomas }
6695 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6696 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6697 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6698 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6699 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6700 86f4aab9 2022-09-09 thomas free(index);
6701 ce52c690 2018-06-23 stsp if (err)
6702 ce52c690 2018-06-23 stsp return err;
6703 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6704 2550e4c3 2018-07-13 stsp free(wline);
6705 2550e4c3 2018-07-13 stsp wline = NULL;
6706 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6707 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6708 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6709 ffd1d5e5 2018-06-23 stsp return NULL;
6710 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6711 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6712 a1eca9bb 2018-06-23 stsp return NULL;
6713 ffd1d5e5 2018-06-23 stsp
6714 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6715 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6716 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6717 0cf4efb1 2018-09-29 stsp if (view->focussed)
6718 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6719 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6720 ffd1d5e5 2018-06-23 stsp }
6721 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6722 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6723 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6724 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6725 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6726 ffd1d5e5 2018-06-23 stsp return NULL;
6727 ffd1d5e5 2018-06-23 stsp n = 1;
6728 ffd1d5e5 2018-06-23 stsp } else {
6729 ffd1d5e5 2018-06-23 stsp n = 0;
6730 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6731 ffd1d5e5 2018-06-23 stsp }
6732 ffd1d5e5 2018-06-23 stsp
6733 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6734 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6735 848d6979 2019-08-12 stsp const char *modestr = "";
6736 56e0773d 2019-11-28 stsp mode_t mode;
6737 1d13200f 2018-07-12 stsp
6738 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6739 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6740 56e0773d 2019-11-28 stsp
6741 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6742 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6743 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6744 1d13200f 2018-07-12 stsp if (err)
6745 638f9024 2019-05-13 stsp return got_error_from_errno(
6746 230a42bd 2019-05-11 jcs "got_object_id_str");
6747 1d13200f 2018-07-12 stsp }
6748 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6749 63c5ca5d 2019-08-24 stsp modestr = "$";
6750 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6751 0d6c6ee3 2020-05-20 stsp int i;
6752 0d6c6ee3 2020-05-20 stsp
6753 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6754 d86d3b18 2020-12-01 naddy te, s->repo);
6755 0d6c6ee3 2020-05-20 stsp if (err) {
6756 0d6c6ee3 2020-05-20 stsp free(id_str);
6757 0d6c6ee3 2020-05-20 stsp return err;
6758 0d6c6ee3 2020-05-20 stsp }
6759 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6760 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6761 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6762 0d6c6ee3 2020-05-20 stsp }
6763 848d6979 2019-08-12 stsp modestr = "@";
6764 0d6c6ee3 2020-05-20 stsp }
6765 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6766 848d6979 2019-08-12 stsp modestr = "/";
6767 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6768 848d6979 2019-08-12 stsp modestr = "*";
6769 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6770 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6771 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6772 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6773 1d13200f 2018-07-12 stsp free(id_str);
6774 0d6c6ee3 2020-05-20 stsp free(link_target);
6775 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6776 1d13200f 2018-07-12 stsp }
6777 1d13200f 2018-07-12 stsp free(id_str);
6778 0d6c6ee3 2020-05-20 stsp free(link_target);
6779 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6780 f91a2b48 2022-06-23 thomas 0, 0);
6781 ffd1d5e5 2018-06-23 stsp if (err) {
6782 ffd1d5e5 2018-06-23 stsp free(line);
6783 ffd1d5e5 2018-06-23 stsp break;
6784 ffd1d5e5 2018-06-23 stsp }
6785 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6786 0cf4efb1 2018-09-29 stsp if (view->focussed)
6787 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6788 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6789 ffd1d5e5 2018-06-23 stsp }
6790 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6791 f26dddb7 2019-11-08 stsp if (tc)
6792 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6793 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6794 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6795 f26dddb7 2019-11-08 stsp if (tc)
6796 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6797 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6798 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6799 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6800 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6801 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6802 ffd1d5e5 2018-06-23 stsp free(line);
6803 2550e4c3 2018-07-13 stsp free(wline);
6804 2550e4c3 2018-07-13 stsp wline = NULL;
6805 ffd1d5e5 2018-06-23 stsp n++;
6806 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6807 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6808 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6809 ffd1d5e5 2018-06-23 stsp break;
6810 ffd1d5e5 2018-06-23 stsp }
6811 ffd1d5e5 2018-06-23 stsp
6812 ffd1d5e5 2018-06-23 stsp return err;
6813 ffd1d5e5 2018-06-23 stsp }
6814 ffd1d5e5 2018-06-23 stsp
6815 ffd1d5e5 2018-06-23 stsp static void
6816 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6817 ffd1d5e5 2018-06-23 stsp {
6818 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6819 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6820 fa86c4bf 2020-11-29 stsp int i = 0;
6821 ffd1d5e5 2018-06-23 stsp
6822 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6823 ffd1d5e5 2018-06-23 stsp return;
6824 ffd1d5e5 2018-06-23 stsp
6825 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6826 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6827 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6828 fa86c4bf 2020-11-29 stsp if (!isroot)
6829 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6830 fa86c4bf 2020-11-29 stsp break;
6831 fa86c4bf 2020-11-29 stsp }
6832 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6833 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6834 ffd1d5e5 2018-06-23 stsp }
6835 ffd1d5e5 2018-06-23 stsp }
6836 ffd1d5e5 2018-06-23 stsp
6837 a5d43cac 2022-07-01 thomas static const struct got_error *
6838 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6839 ffd1d5e5 2018-06-23 stsp {
6840 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6841 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6842 ffd1d5e5 2018-06-23 stsp int n = 0;
6843 ffd1d5e5 2018-06-23 stsp
6844 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6845 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6846 694d3271 2020-12-01 naddy s->first_displayed_entry);
6847 694d3271 2020-12-01 naddy else
6848 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6849 56e0773d 2019-11-28 stsp
6850 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6851 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6852 07dd3ed3 2022-08-06 thomas if (last) {
6853 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6854 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6855 07dd3ed3 2022-08-06 thomas }
6856 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6857 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6858 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6859 768394f3 2019-01-24 stsp }
6860 ffd1d5e5 2018-06-23 stsp }
6861 a5d43cac 2022-07-01 thomas
6862 a5d43cac 2022-07-01 thomas return NULL;
6863 ffd1d5e5 2018-06-23 stsp }
6864 ffd1d5e5 2018-06-23 stsp
6865 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6866 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6867 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6868 ffd1d5e5 2018-06-23 stsp {
6869 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6870 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6871 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6872 ffd1d5e5 2018-06-23 stsp
6873 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6874 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6875 56e0773d 2019-11-28 stsp + 1 /* slash */;
6876 ce52c690 2018-06-23 stsp if (te)
6877 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6878 ce52c690 2018-06-23 stsp
6879 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6880 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6881 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6882 ffd1d5e5 2018-06-23 stsp
6883 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6884 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6885 d9765a41 2018-06-23 stsp while (pt) {
6886 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6887 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6888 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6889 cb2ebc8a 2018-06-23 stsp goto done;
6890 cb2ebc8a 2018-06-23 stsp }
6891 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6892 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6893 cb2ebc8a 2018-06-23 stsp goto done;
6894 cb2ebc8a 2018-06-23 stsp }
6895 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6896 ffd1d5e5 2018-06-23 stsp }
6897 ce52c690 2018-06-23 stsp if (te) {
6898 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6899 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6900 ce52c690 2018-06-23 stsp goto done;
6901 ce52c690 2018-06-23 stsp }
6902 cb2ebc8a 2018-06-23 stsp }
6903 ce52c690 2018-06-23 stsp done:
6904 ce52c690 2018-06-23 stsp if (err) {
6905 ce52c690 2018-06-23 stsp free(*path);
6906 ce52c690 2018-06-23 stsp *path = NULL;
6907 ce52c690 2018-06-23 stsp }
6908 ce52c690 2018-06-23 stsp return err;
6909 ce52c690 2018-06-23 stsp }
6910 ce52c690 2018-06-23 stsp
6911 ce52c690 2018-06-23 stsp static const struct got_error *
6912 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6913 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6914 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6915 ce52c690 2018-06-23 stsp {
6916 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6917 ce52c690 2018-06-23 stsp char *path;
6918 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6919 a0de39f3 2019-08-09 stsp
6920 a0de39f3 2019-08-09 stsp *new_view = NULL;
6921 69efd4c4 2018-07-18 stsp
6922 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6923 ce52c690 2018-06-23 stsp if (err)
6924 ce52c690 2018-06-23 stsp return err;
6925 ffd1d5e5 2018-06-23 stsp
6926 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6927 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6928 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6929 83ce39e3 2019-08-12 stsp goto done;
6930 83ce39e3 2019-08-12 stsp }
6931 cdf1ee82 2018-08-01 stsp
6932 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6933 e5a0f69f 2018-08-18 stsp if (err) {
6934 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6935 fc06ba56 2019-08-22 stsp err = NULL;
6936 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6937 e5a0f69f 2018-08-18 stsp } else
6938 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6939 83ce39e3 2019-08-12 stsp done:
6940 83ce39e3 2019-08-12 stsp free(path);
6941 69efd4c4 2018-07-18 stsp return err;
6942 69efd4c4 2018-07-18 stsp }
6943 69efd4c4 2018-07-18 stsp
6944 69efd4c4 2018-07-18 stsp static const struct got_error *
6945 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6946 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6947 69efd4c4 2018-07-18 stsp {
6948 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6949 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6950 69efd4c4 2018-07-18 stsp char *path;
6951 a0de39f3 2019-08-09 stsp
6952 a0de39f3 2019-08-09 stsp *new_view = NULL;
6953 69efd4c4 2018-07-18 stsp
6954 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6955 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6956 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6957 e5a0f69f 2018-08-18 stsp
6958 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6959 69efd4c4 2018-07-18 stsp if (err)
6960 69efd4c4 2018-07-18 stsp return err;
6961 69efd4c4 2018-07-18 stsp
6962 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6963 4e97c21c 2020-12-06 stsp path, 0);
6964 ba4f502b 2018-08-04 stsp if (err)
6965 e5a0f69f 2018-08-18 stsp view_close(log_view);
6966 e5a0f69f 2018-08-18 stsp else
6967 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6968 cb2ebc8a 2018-06-23 stsp free(path);
6969 cb2ebc8a 2018-06-23 stsp return err;
6970 ffd1d5e5 2018-06-23 stsp }
6971 ffd1d5e5 2018-06-23 stsp
6972 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6973 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6974 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6975 ffd1d5e5 2018-06-23 stsp {
6976 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6977 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6978 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6979 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6980 ffd1d5e5 2018-06-23 stsp
6981 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6982 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6983 bc573f3b 2021-07-10 stsp
6984 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6985 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6986 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6987 ffd1d5e5 2018-06-23 stsp
6988 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6989 bc573f3b 2021-07-10 stsp if (err)
6990 bc573f3b 2021-07-10 stsp goto done;
6991 bc573f3b 2021-07-10 stsp
6992 bc573f3b 2021-07-10 stsp /*
6993 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6994 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6995 bc573f3b 2021-07-10 stsp * closed on demand.
6996 bc573f3b 2021-07-10 stsp */
6997 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6998 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6999 bc573f3b 2021-07-10 stsp if (err)
7000 bc573f3b 2021-07-10 stsp goto done;
7001 bc573f3b 2021-07-10 stsp s->tree = s->root;
7002 bc573f3b 2021-07-10 stsp
7003 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7004 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7005 ffd1d5e5 2018-06-23 stsp goto done;
7006 ffd1d5e5 2018-06-23 stsp
7007 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7008 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7009 ffd1d5e5 2018-06-23 stsp goto done;
7010 ffd1d5e5 2018-06-23 stsp }
7011 ffd1d5e5 2018-06-23 stsp
7012 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7013 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7014 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7015 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7016 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7017 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7018 9cd7cbd1 2020-12-07 stsp goto done;
7019 9cd7cbd1 2020-12-07 stsp }
7020 9cd7cbd1 2020-12-07 stsp }
7021 fb2756b9 2018-08-04 stsp s->repo = repo;
7022 c0b01bdb 2019-11-08 stsp
7023 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7024 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7025 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7026 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7027 c0b01bdb 2019-11-08 stsp if (err)
7028 c0b01bdb 2019-11-08 stsp goto done;
7029 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7030 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7031 bc573f3b 2021-07-10 stsp if (err)
7032 c0b01bdb 2019-11-08 stsp goto done;
7033 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7034 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7035 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7036 bc573f3b 2021-07-10 stsp if (err)
7037 c0b01bdb 2019-11-08 stsp goto done;
7038 e5a0f69f 2018-08-18 stsp
7039 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7040 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7041 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7042 bc573f3b 2021-07-10 stsp if (err)
7043 11b20872 2019-11-08 stsp goto done;
7044 11b20872 2019-11-08 stsp
7045 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7046 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7047 bc573f3b 2021-07-10 stsp if (err)
7048 c0b01bdb 2019-11-08 stsp goto done;
7049 c0b01bdb 2019-11-08 stsp }
7050 c0b01bdb 2019-11-08 stsp
7051 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7052 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7053 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7054 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7055 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7056 ad80ab7b 2018-08-04 stsp done:
7057 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7058 bc573f3b 2021-07-10 stsp if (commit)
7059 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7060 bc573f3b 2021-07-10 stsp if (err)
7061 bc573f3b 2021-07-10 stsp close_tree_view(view);
7062 ad80ab7b 2018-08-04 stsp return err;
7063 ad80ab7b 2018-08-04 stsp }
7064 ad80ab7b 2018-08-04 stsp
7065 e5a0f69f 2018-08-18 stsp static const struct got_error *
7066 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7067 ad80ab7b 2018-08-04 stsp {
7068 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7069 ad80ab7b 2018-08-04 stsp
7070 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7071 fb2756b9 2018-08-04 stsp free(s->tree_label);
7072 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7073 6484ec90 2018-09-29 stsp free(s->commit_id);
7074 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7075 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7076 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7077 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7078 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7079 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7080 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7081 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7082 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7083 ad80ab7b 2018-08-04 stsp free(parent);
7084 ad80ab7b 2018-08-04 stsp
7085 ad80ab7b 2018-08-04 stsp }
7086 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7087 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7088 bc573f3b 2021-07-10 stsp if (s->root)
7089 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7090 7c32bd05 2019-06-22 stsp return NULL;
7091 7c32bd05 2019-06-22 stsp }
7092 7c32bd05 2019-06-22 stsp
7093 7c32bd05 2019-06-22 stsp static const struct got_error *
7094 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7095 7c32bd05 2019-06-22 stsp {
7096 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7097 7c32bd05 2019-06-22 stsp
7098 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7099 7c32bd05 2019-06-22 stsp return NULL;
7100 7c32bd05 2019-06-22 stsp }
7101 7c32bd05 2019-06-22 stsp
7102 7c32bd05 2019-06-22 stsp static int
7103 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7104 7c32bd05 2019-06-22 stsp {
7105 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7106 7c32bd05 2019-06-22 stsp
7107 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7108 56e0773d 2019-11-28 stsp 0) == 0;
7109 7c32bd05 2019-06-22 stsp }
7110 7c32bd05 2019-06-22 stsp
7111 7c32bd05 2019-06-22 stsp static const struct got_error *
7112 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7113 7c32bd05 2019-06-22 stsp {
7114 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7115 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7116 7c32bd05 2019-06-22 stsp
7117 7c32bd05 2019-06-22 stsp if (!view->searching) {
7118 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7119 7c32bd05 2019-06-22 stsp return NULL;
7120 7c32bd05 2019-06-22 stsp }
7121 7c32bd05 2019-06-22 stsp
7122 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7123 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7124 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7125 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7126 56e0773d 2019-11-28 stsp s->selected_entry);
7127 7c32bd05 2019-06-22 stsp else
7128 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7129 56e0773d 2019-11-28 stsp } else {
7130 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7131 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7132 56e0773d 2019-11-28 stsp else
7133 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7134 56e0773d 2019-11-28 stsp s->selected_entry);
7135 7c32bd05 2019-06-22 stsp }
7136 7c32bd05 2019-06-22 stsp } else {
7137 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7138 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7139 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7140 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7141 56e0773d 2019-11-28 stsp else
7142 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7143 7c32bd05 2019-06-22 stsp }
7144 7c32bd05 2019-06-22 stsp
7145 7c32bd05 2019-06-22 stsp while (1) {
7146 56e0773d 2019-11-28 stsp if (te == NULL) {
7147 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7148 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7149 ac66afb8 2019-06-24 stsp return NULL;
7150 ac66afb8 2019-06-24 stsp }
7151 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7152 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7153 56e0773d 2019-11-28 stsp else
7154 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7155 7c32bd05 2019-06-22 stsp }
7156 7c32bd05 2019-06-22 stsp
7157 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7158 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7159 56e0773d 2019-11-28 stsp s->matched_entry = te;
7160 7c32bd05 2019-06-22 stsp break;
7161 7c32bd05 2019-06-22 stsp }
7162 7c32bd05 2019-06-22 stsp
7163 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7164 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7165 56e0773d 2019-11-28 stsp else
7166 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7167 7c32bd05 2019-06-22 stsp }
7168 e5a0f69f 2018-08-18 stsp
7169 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7170 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7171 7c32bd05 2019-06-22 stsp s->selected = 0;
7172 7c32bd05 2019-06-22 stsp }
7173 7c32bd05 2019-06-22 stsp
7174 e5a0f69f 2018-08-18 stsp return NULL;
7175 ad80ab7b 2018-08-04 stsp }
7176 ad80ab7b 2018-08-04 stsp
7177 ad80ab7b 2018-08-04 stsp static const struct got_error *
7178 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7179 ad80ab7b 2018-08-04 stsp {
7180 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7181 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7182 e5a0f69f 2018-08-18 stsp char *parent_path;
7183 ad80ab7b 2018-08-04 stsp
7184 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7185 e5a0f69f 2018-08-18 stsp if (err)
7186 e5a0f69f 2018-08-18 stsp return err;
7187 ffd1d5e5 2018-06-23 stsp
7188 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7189 e5a0f69f 2018-08-18 stsp free(parent_path);
7190 669b5ffa 2018-10-07 stsp
7191 a5d43cac 2022-07-01 thomas view_border(view);
7192 e5a0f69f 2018-08-18 stsp return err;
7193 07dd3ed3 2022-08-06 thomas }
7194 07dd3ed3 2022-08-06 thomas
7195 07dd3ed3 2022-08-06 thomas static const struct got_error *
7196 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7197 07dd3ed3 2022-08-06 thomas {
7198 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7199 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7200 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7201 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7202 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7203 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7204 07dd3ed3 2022-08-06 thomas
7205 07dd3ed3 2022-08-06 thomas g = view->gline;
7206 07dd3ed3 2022-08-06 thomas view->gline = 0;
7207 07dd3ed3 2022-08-06 thomas
7208 07dd3ed3 2022-08-06 thomas if (g == 0)
7209 07dd3ed3 2022-08-06 thomas g = 1;
7210 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7211 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7212 07dd3ed3 2022-08-06 thomas
7213 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7214 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7215 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7216 07dd3ed3 2022-08-06 thomas
7217 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7218 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7219 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7220 07dd3ed3 2022-08-06 thomas }
7221 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7222 07dd3ed3 2022-08-06 thomas last += off;
7223 07dd3ed3 2022-08-06 thomas
7224 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7225 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7226 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7227 07dd3ed3 2022-08-06 thomas }
7228 07dd3ed3 2022-08-06 thomas
7229 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7230 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7231 07dd3ed3 2022-08-06 thomas i += off;
7232 07dd3ed3 2022-08-06 thomas }
7233 07dd3ed3 2022-08-06 thomas
7234 07dd3ed3 2022-08-06 thomas if (i < g) {
7235 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7236 07dd3ed3 2022-08-06 thomas if (err)
7237 07dd3ed3 2022-08-06 thomas return err;
7238 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7239 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7240 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7241 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7242 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7243 07dd3ed3 2022-08-06 thomas first += off;
7244 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7245 07dd3ed3 2022-08-06 thomas }
7246 07dd3ed3 2022-08-06 thomas } else if (i > g)
7247 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7248 07dd3ed3 2022-08-06 thomas
7249 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7250 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7251 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7252 07dd3ed3 2022-08-06 thomas
7253 07dd3ed3 2022-08-06 thomas return NULL;
7254 e5a0f69f 2018-08-18 stsp }
7255 ce52c690 2018-06-23 stsp
7256 e5a0f69f 2018-08-18 stsp static const struct got_error *
7257 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7258 e5a0f69f 2018-08-18 stsp {
7259 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7260 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7261 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7262 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7263 ffd1d5e5 2018-06-23 stsp
7264 07dd3ed3 2022-08-06 thomas if (view->gline)
7265 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7266 07dd3ed3 2022-08-06 thomas
7267 e5a0f69f 2018-08-18 stsp switch (ch) {
7268 1e37a5c2 2019-05-12 jcs case 'i':
7269 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7270 07b0611c 2022-06-23 thomas view->count = 0;
7271 1e37a5c2 2019-05-12 jcs break;
7272 1be4947a 2022-07-22 thomas case 'L':
7273 07b0611c 2022-06-23 thomas view->count = 0;
7274 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7275 ffd1d5e5 2018-06-23 stsp break;
7276 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7277 152c1c93 2020-11-29 stsp break;
7278 1be4947a 2022-07-22 thomas case 'R':
7279 07b0611c 2022-06-23 thomas view->count = 0;
7280 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7281 e4526bf5 2021-09-03 naddy break;
7282 e4526bf5 2021-09-03 naddy case 'g':
7283 e4526bf5 2021-09-03 naddy case KEY_HOME:
7284 e4526bf5 2021-09-03 naddy s->selected = 0;
7285 07b0611c 2022-06-23 thomas view->count = 0;
7286 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7287 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7288 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7289 e4526bf5 2021-09-03 naddy else
7290 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7291 1e37a5c2 2019-05-12 jcs break;
7292 e4526bf5 2021-09-03 naddy case 'G':
7293 a5d43cac 2022-07-01 thomas case KEY_END: {
7294 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7295 a5d43cac 2022-07-01 thomas
7296 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7297 a5d43cac 2022-07-01 thomas --eos; /* border */
7298 e4526bf5 2021-09-03 naddy s->selected = 0;
7299 07b0611c 2022-06-23 thomas view->count = 0;
7300 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7301 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7302 e4526bf5 2021-09-03 naddy if (te == NULL) {
7303 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7304 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7305 e4526bf5 2021-09-03 naddy n++;
7306 e4526bf5 2021-09-03 naddy }
7307 e4526bf5 2021-09-03 naddy break;
7308 e4526bf5 2021-09-03 naddy }
7309 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7310 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7311 e4526bf5 2021-09-03 naddy }
7312 e4526bf5 2021-09-03 naddy if (n > 0)
7313 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7314 e4526bf5 2021-09-03 naddy break;
7315 a5d43cac 2022-07-01 thomas }
7316 1e37a5c2 2019-05-12 jcs case 'k':
7317 1e37a5c2 2019-05-12 jcs case KEY_UP:
7318 f7140bf5 2021-10-17 thomas case CTRL('p'):
7319 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7320 1e37a5c2 2019-05-12 jcs s->selected--;
7321 fa86c4bf 2020-11-29 stsp break;
7322 1e37a5c2 2019-05-12 jcs }
7323 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7324 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7325 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7326 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7327 07b0611c 2022-06-23 thomas view->count = 0;
7328 1e37a5c2 2019-05-12 jcs break;
7329 70f17a53 2022-06-13 thomas case CTRL('u'):
7330 23427b14 2022-06-23 thomas case 'u':
7331 70f17a53 2022-06-13 thomas nscroll /= 2;
7332 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7333 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7334 ea025d1d 2020-02-22 naddy case CTRL('b'):
7335 1c5e5faa 2022-06-23 thomas case 'b':
7336 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7337 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7338 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7339 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7340 fa86c4bf 2020-11-29 stsp } else {
7341 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7342 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7343 fa86c4bf 2020-11-29 stsp }
7344 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7345 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7346 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7347 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7348 07b0611c 2022-06-23 thomas view->count = 0;
7349 1e37a5c2 2019-05-12 jcs break;
7350 1e37a5c2 2019-05-12 jcs case 'j':
7351 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7352 f7140bf5 2021-10-17 thomas case CTRL('n'):
7353 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7354 1e37a5c2 2019-05-12 jcs s->selected++;
7355 1e37a5c2 2019-05-12 jcs break;
7356 1e37a5c2 2019-05-12 jcs }
7357 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7358 07b0611c 2022-06-23 thomas == NULL) {
7359 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7360 07b0611c 2022-06-23 thomas view->count = 0;
7361 1e37a5c2 2019-05-12 jcs break;
7362 07b0611c 2022-06-23 thomas }
7363 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7364 1e37a5c2 2019-05-12 jcs break;
7365 70f17a53 2022-06-13 thomas case CTRL('d'):
7366 23427b14 2022-06-23 thomas case 'd':
7367 70f17a53 2022-06-13 thomas nscroll /= 2;
7368 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7369 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7370 ea025d1d 2020-02-22 naddy case CTRL('f'):
7371 1c5e5faa 2022-06-23 thomas case 'f':
7372 4c2d69cb 2022-06-23 thomas case ' ':
7373 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7374 1e37a5c2 2019-05-12 jcs == NULL) {
7375 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7376 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7377 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7378 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7379 07b0611c 2022-06-23 thomas else
7380 07b0611c 2022-06-23 thomas view->count = 0;
7381 1e37a5c2 2019-05-12 jcs break;
7382 1e37a5c2 2019-05-12 jcs }
7383 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7384 1e37a5c2 2019-05-12 jcs break;
7385 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7386 1e37a5c2 2019-05-12 jcs case '\r':
7387 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7388 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7389 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7390 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7391 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7392 07b0611c 2022-06-23 thomas view->count = 0;
7393 1e37a5c2 2019-05-12 jcs break;
7394 07b0611c 2022-06-23 thomas }
7395 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7396 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7397 1e37a5c2 2019-05-12 jcs entry);
7398 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7399 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7400 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7401 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7402 1e37a5c2 2019-05-12 jcs s->selected_entry =
7403 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7404 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7405 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7406 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7407 a5d43cac 2022-07-01 thomas if (err)
7408 a5d43cac 2022-07-01 thomas break;
7409 a5d43cac 2022-07-01 thomas }
7410 1e37a5c2 2019-05-12 jcs free(parent);
7411 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7412 56e0773d 2019-11-28 stsp s->selected_entry))) {
7413 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7414 07b0611c 2022-06-23 thomas view->count = 0;
7415 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7416 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7417 1e37a5c2 2019-05-12 jcs if (err)
7418 1e37a5c2 2019-05-12 jcs break;
7419 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7420 941e9f74 2019-05-21 stsp if (err) {
7421 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7422 1e37a5c2 2019-05-12 jcs break;
7423 1e37a5c2 2019-05-12 jcs }
7424 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7425 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7426 1e37a5c2 2019-05-12 jcs break;
7427 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7428 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7429 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7430 07b0611c 2022-06-23 thomas view->count = 0;
7431 1e37a5c2 2019-05-12 jcs break;
7432 1e37a5c2 2019-05-12 jcs default:
7433 07b0611c 2022-06-23 thomas view->count = 0;
7434 1e37a5c2 2019-05-12 jcs break;
7435 ffd1d5e5 2018-06-23 stsp }
7436 e5a0f69f 2018-08-18 stsp
7437 ffd1d5e5 2018-06-23 stsp return err;
7438 9f7d7167 2018-04-29 stsp }
7439 9f7d7167 2018-04-29 stsp
7440 ffd1d5e5 2018-06-23 stsp __dead static void
7441 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7442 ffd1d5e5 2018-06-23 stsp {
7443 ffd1d5e5 2018-06-23 stsp endwin();
7444 91198554 2022-06-23 thomas fprintf(stderr,
7445 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7446 ffd1d5e5 2018-06-23 stsp getprogname());
7447 ffd1d5e5 2018-06-23 stsp exit(1);
7448 ffd1d5e5 2018-06-23 stsp }
7449 ffd1d5e5 2018-06-23 stsp
7450 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7451 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7452 ffd1d5e5 2018-06-23 stsp {
7453 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7454 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7455 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7456 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7457 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7458 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7459 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7460 4e97c21c 2020-12-06 stsp char *label = NULL;
7461 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7462 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7463 ffd1d5e5 2018-06-23 stsp int ch;
7464 5221c383 2018-08-01 stsp struct tog_view *view;
7465 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7466 70ac5f84 2019-03-28 stsp
7467 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7468 ffd1d5e5 2018-06-23 stsp switch (ch) {
7469 ffd1d5e5 2018-06-23 stsp case 'c':
7470 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7471 74283ab8 2020-02-07 stsp break;
7472 74283ab8 2020-02-07 stsp case 'r':
7473 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7474 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7475 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7476 74283ab8 2020-02-07 stsp optarg);
7477 ffd1d5e5 2018-06-23 stsp break;
7478 ffd1d5e5 2018-06-23 stsp default:
7479 e99e2d15 2020-11-24 naddy usage_tree();
7480 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7481 ffd1d5e5 2018-06-23 stsp }
7482 ffd1d5e5 2018-06-23 stsp }
7483 ffd1d5e5 2018-06-23 stsp
7484 ffd1d5e5 2018-06-23 stsp argc -= optind;
7485 ffd1d5e5 2018-06-23 stsp argv += optind;
7486 ffd1d5e5 2018-06-23 stsp
7487 55cccc34 2020-02-20 stsp if (argc > 1)
7488 e99e2d15 2020-11-24 naddy usage_tree();
7489 7cd52833 2022-06-23 thomas
7490 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7491 7cd52833 2022-06-23 thomas if (error != NULL)
7492 7cd52833 2022-06-23 thomas goto done;
7493 74283ab8 2020-02-07 stsp
7494 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7495 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7496 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7497 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7498 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7499 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7500 c156c7a4 2020-12-18 stsp goto done;
7501 55cccc34 2020-02-20 stsp if (worktree)
7502 52185f70 2019-02-05 stsp repo_path =
7503 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7504 55cccc34 2020-02-20 stsp else
7505 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7506 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7507 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7508 c156c7a4 2020-12-18 stsp goto done;
7509 c156c7a4 2020-12-18 stsp }
7510 55cccc34 2020-02-20 stsp }
7511 a915003a 2019-02-05 stsp
7512 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7513 c02c541e 2019-03-29 stsp if (error != NULL)
7514 52185f70 2019-02-05 stsp goto done;
7515 d188b9a6 2019-01-04 stsp
7516 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7517 55cccc34 2020-02-20 stsp repo, worktree);
7518 55cccc34 2020-02-20 stsp if (error)
7519 55cccc34 2020-02-20 stsp goto done;
7520 55cccc34 2020-02-20 stsp
7521 55cccc34 2020-02-20 stsp init_curses();
7522 55cccc34 2020-02-20 stsp
7523 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7524 c02c541e 2019-03-29 stsp if (error)
7525 52185f70 2019-02-05 stsp goto done;
7526 ffd1d5e5 2018-06-23 stsp
7527 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7528 51a10b52 2020-12-26 stsp if (error)
7529 51a10b52 2020-12-26 stsp goto done;
7530 51a10b52 2020-12-26 stsp
7531 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7532 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7533 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7534 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7535 4e97c21c 2020-12-06 stsp if (error)
7536 4e97c21c 2020-12-06 stsp goto done;
7537 4e97c21c 2020-12-06 stsp head_ref_name = label;
7538 4e97c21c 2020-12-06 stsp } else {
7539 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7540 4e97c21c 2020-12-06 stsp if (error == NULL)
7541 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7542 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7543 4e97c21c 2020-12-06 stsp goto done;
7544 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7545 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7546 4e97c21c 2020-12-06 stsp if (error)
7547 4e97c21c 2020-12-06 stsp goto done;
7548 4e97c21c 2020-12-06 stsp }
7549 ffd1d5e5 2018-06-23 stsp
7550 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7551 945f9229 2022-04-16 thomas if (error)
7552 945f9229 2022-04-16 thomas goto done;
7553 945f9229 2022-04-16 thomas
7554 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7555 5221c383 2018-08-01 stsp if (view == NULL) {
7556 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7557 5221c383 2018-08-01 stsp goto done;
7558 5221c383 2018-08-01 stsp }
7559 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7560 ad80ab7b 2018-08-04 stsp if (error)
7561 ad80ab7b 2018-08-04 stsp goto done;
7562 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7563 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7564 d91faf3b 2020-12-01 naddy in_repo_path);
7565 55cccc34 2020-02-20 stsp if (error)
7566 55cccc34 2020-02-20 stsp goto done;
7567 55cccc34 2020-02-20 stsp }
7568 55cccc34 2020-02-20 stsp
7569 55cccc34 2020-02-20 stsp if (worktree) {
7570 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7571 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7572 55cccc34 2020-02-20 stsp worktree = NULL;
7573 55cccc34 2020-02-20 stsp }
7574 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7575 ffd1d5e5 2018-06-23 stsp done:
7576 52185f70 2019-02-05 stsp free(repo_path);
7577 e4a0e26d 2020-02-20 stsp free(cwd);
7578 ffd1d5e5 2018-06-23 stsp free(commit_id);
7579 4e97c21c 2020-12-06 stsp free(label);
7580 486cd271 2020-12-06 stsp if (ref)
7581 486cd271 2020-12-06 stsp got_ref_close(ref);
7582 1d0f4054 2021-06-17 stsp if (repo) {
7583 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7584 1d0f4054 2021-06-17 stsp if (error == NULL)
7585 1d0f4054 2021-06-17 stsp error = close_err;
7586 1d0f4054 2021-06-17 stsp }
7587 7cd52833 2022-06-23 thomas if (pack_fds) {
7588 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7589 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7590 7cd52833 2022-06-23 thomas if (error == NULL)
7591 7cd52833 2022-06-23 thomas error = pack_err;
7592 7cd52833 2022-06-23 thomas }
7593 51a10b52 2020-12-26 stsp tog_free_refs();
7594 ffd1d5e5 2018-06-23 stsp return error;
7595 6458efa5 2020-11-24 stsp }
7596 6458efa5 2020-11-24 stsp
7597 6458efa5 2020-11-24 stsp static const struct got_error *
7598 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7599 6458efa5 2020-11-24 stsp {
7600 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7601 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7602 6458efa5 2020-11-24 stsp
7603 6458efa5 2020-11-24 stsp s->nrefs = 0;
7604 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7605 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7606 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7607 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7608 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7609 6458efa5 2020-11-24 stsp continue;
7610 6458efa5 2020-11-24 stsp
7611 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7612 6458efa5 2020-11-24 stsp if (re == NULL)
7613 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7614 6458efa5 2020-11-24 stsp
7615 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7616 8924d611 2020-12-26 stsp if (re->ref == NULL)
7617 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7618 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7619 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7620 6458efa5 2020-11-24 stsp }
7621 6458efa5 2020-11-24 stsp
7622 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7623 6458efa5 2020-11-24 stsp return NULL;
7624 6458efa5 2020-11-24 stsp }
7625 6458efa5 2020-11-24 stsp
7626 ef20f542 2022-06-26 thomas static void
7627 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7628 6458efa5 2020-11-24 stsp {
7629 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7630 6458efa5 2020-11-24 stsp
7631 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7632 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7633 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7634 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7635 6458efa5 2020-11-24 stsp free(re);
7636 6458efa5 2020-11-24 stsp }
7637 6458efa5 2020-11-24 stsp }
7638 6458efa5 2020-11-24 stsp
7639 6458efa5 2020-11-24 stsp static const struct got_error *
7640 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7641 6458efa5 2020-11-24 stsp {
7642 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7643 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7644 6458efa5 2020-11-24 stsp
7645 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7646 6458efa5 2020-11-24 stsp s->repo = repo;
7647 6458efa5 2020-11-24 stsp
7648 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7649 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7650 6458efa5 2020-11-24 stsp
7651 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7652 6458efa5 2020-11-24 stsp if (err)
7653 6458efa5 2020-11-24 stsp return err;
7654 34ba6917 2020-11-30 stsp
7655 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7656 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7657 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7658 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7659 6458efa5 2020-11-24 stsp if (err)
7660 6458efa5 2020-11-24 stsp goto done;
7661 6458efa5 2020-11-24 stsp
7662 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7663 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7664 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7665 6458efa5 2020-11-24 stsp if (err)
7666 6458efa5 2020-11-24 stsp goto done;
7667 6458efa5 2020-11-24 stsp
7668 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7669 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7670 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7671 2183bbf6 2022-01-23 thomas if (err)
7672 2183bbf6 2022-01-23 thomas goto done;
7673 2183bbf6 2022-01-23 thomas
7674 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7675 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7676 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7677 6458efa5 2020-11-24 stsp if (err)
7678 6458efa5 2020-11-24 stsp goto done;
7679 6458efa5 2020-11-24 stsp }
7680 6458efa5 2020-11-24 stsp
7681 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7682 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7683 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7684 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7685 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7686 6458efa5 2020-11-24 stsp done:
7687 6458efa5 2020-11-24 stsp if (err)
7688 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7689 6458efa5 2020-11-24 stsp return err;
7690 6458efa5 2020-11-24 stsp }
7691 6458efa5 2020-11-24 stsp
7692 6458efa5 2020-11-24 stsp static const struct got_error *
7693 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7694 6458efa5 2020-11-24 stsp {
7695 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7696 6458efa5 2020-11-24 stsp
7697 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7698 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7699 6458efa5 2020-11-24 stsp
7700 6458efa5 2020-11-24 stsp return NULL;
7701 9f7d7167 2018-04-29 stsp }
7702 ce5b7c56 2019-07-09 stsp
7703 6458efa5 2020-11-24 stsp static const struct got_error *
7704 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7705 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7706 6458efa5 2020-11-24 stsp {
7707 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7708 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7709 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7710 6458efa5 2020-11-24 stsp int obj_type;
7711 6458efa5 2020-11-24 stsp
7712 c42c9805 2020-11-24 stsp *commit_id = NULL;
7713 6458efa5 2020-11-24 stsp
7714 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7715 6458efa5 2020-11-24 stsp if (err)
7716 6458efa5 2020-11-24 stsp return err;
7717 6458efa5 2020-11-24 stsp
7718 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7719 6458efa5 2020-11-24 stsp if (err)
7720 6458efa5 2020-11-24 stsp goto done;
7721 6458efa5 2020-11-24 stsp
7722 6458efa5 2020-11-24 stsp switch (obj_type) {
7723 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7724 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7725 6458efa5 2020-11-24 stsp break;
7726 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7727 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7728 6458efa5 2020-11-24 stsp if (err)
7729 6458efa5 2020-11-24 stsp goto done;
7730 c42c9805 2020-11-24 stsp free(obj_id);
7731 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7732 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7733 c42c9805 2020-11-24 stsp if (err)
7734 6458efa5 2020-11-24 stsp goto done;
7735 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7736 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7737 c42c9805 2020-11-24 stsp goto done;
7738 c42c9805 2020-11-24 stsp }
7739 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7740 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7741 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7742 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7743 c42c9805 2020-11-24 stsp goto done;
7744 c42c9805 2020-11-24 stsp }
7745 6458efa5 2020-11-24 stsp break;
7746 6458efa5 2020-11-24 stsp default:
7747 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7748 c42c9805 2020-11-24 stsp break;
7749 6458efa5 2020-11-24 stsp }
7750 6458efa5 2020-11-24 stsp
7751 c42c9805 2020-11-24 stsp done:
7752 c42c9805 2020-11-24 stsp if (tag)
7753 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7754 c42c9805 2020-11-24 stsp if (err) {
7755 c42c9805 2020-11-24 stsp free(*commit_id);
7756 c42c9805 2020-11-24 stsp *commit_id = NULL;
7757 c42c9805 2020-11-24 stsp }
7758 c42c9805 2020-11-24 stsp return err;
7759 c42c9805 2020-11-24 stsp }
7760 c42c9805 2020-11-24 stsp
7761 c42c9805 2020-11-24 stsp static const struct got_error *
7762 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7763 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7764 c42c9805 2020-11-24 stsp {
7765 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7766 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7767 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7768 c42c9805 2020-11-24 stsp
7769 c42c9805 2020-11-24 stsp *new_view = NULL;
7770 c42c9805 2020-11-24 stsp
7771 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7772 c42c9805 2020-11-24 stsp if (err) {
7773 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7774 c42c9805 2020-11-24 stsp return err;
7775 c42c9805 2020-11-24 stsp else
7776 c42c9805 2020-11-24 stsp return NULL;
7777 c42c9805 2020-11-24 stsp }
7778 c42c9805 2020-11-24 stsp
7779 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7780 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7781 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7782 6458efa5 2020-11-24 stsp goto done;
7783 6458efa5 2020-11-24 stsp }
7784 6458efa5 2020-11-24 stsp
7785 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7786 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7787 6458efa5 2020-11-24 stsp done:
7788 6458efa5 2020-11-24 stsp if (err)
7789 6458efa5 2020-11-24 stsp view_close(log_view);
7790 6458efa5 2020-11-24 stsp else
7791 6458efa5 2020-11-24 stsp *new_view = log_view;
7792 c42c9805 2020-11-24 stsp free(commit_id);
7793 6458efa5 2020-11-24 stsp return err;
7794 6458efa5 2020-11-24 stsp }
7795 6458efa5 2020-11-24 stsp
7796 ce5b7c56 2019-07-09 stsp static void
7797 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7798 6458efa5 2020-11-24 stsp {
7799 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7800 34ba6917 2020-11-30 stsp int i = 0;
7801 6458efa5 2020-11-24 stsp
7802 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7803 6458efa5 2020-11-24 stsp return;
7804 6458efa5 2020-11-24 stsp
7805 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7806 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7807 34ba6917 2020-11-30 stsp if (re == NULL)
7808 34ba6917 2020-11-30 stsp break;
7809 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7810 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7811 6458efa5 2020-11-24 stsp }
7812 6458efa5 2020-11-24 stsp }
7813 6458efa5 2020-11-24 stsp
7814 a5d43cac 2022-07-01 thomas static const struct got_error *
7815 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7816 6458efa5 2020-11-24 stsp {
7817 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7818 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7819 6458efa5 2020-11-24 stsp int n = 0;
7820 6458efa5 2020-11-24 stsp
7821 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7822 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7823 6458efa5 2020-11-24 stsp else
7824 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7825 6458efa5 2020-11-24 stsp
7826 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7827 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7828 07dd3ed3 2022-08-06 thomas if (last) {
7829 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7830 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7831 07dd3ed3 2022-08-06 thomas }
7832 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7833 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7834 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7835 6458efa5 2020-11-24 stsp }
7836 6458efa5 2020-11-24 stsp }
7837 a5d43cac 2022-07-01 thomas
7838 a5d43cac 2022-07-01 thomas return NULL;
7839 6458efa5 2020-11-24 stsp }
7840 6458efa5 2020-11-24 stsp
7841 6458efa5 2020-11-24 stsp static const struct got_error *
7842 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7843 6458efa5 2020-11-24 stsp {
7844 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7845 6458efa5 2020-11-24 stsp
7846 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7847 6458efa5 2020-11-24 stsp return NULL;
7848 6458efa5 2020-11-24 stsp }
7849 6458efa5 2020-11-24 stsp
7850 6458efa5 2020-11-24 stsp static int
7851 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7852 6458efa5 2020-11-24 stsp {
7853 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7854 6458efa5 2020-11-24 stsp
7855 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7856 6458efa5 2020-11-24 stsp 0) == 0;
7857 6458efa5 2020-11-24 stsp }
7858 6458efa5 2020-11-24 stsp
7859 6458efa5 2020-11-24 stsp static const struct got_error *
7860 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7861 6458efa5 2020-11-24 stsp {
7862 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7863 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7864 6458efa5 2020-11-24 stsp
7865 6458efa5 2020-11-24 stsp if (!view->searching) {
7866 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7867 6458efa5 2020-11-24 stsp return NULL;
7868 6458efa5 2020-11-24 stsp }
7869 6458efa5 2020-11-24 stsp
7870 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7871 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7872 6458efa5 2020-11-24 stsp if (s->selected_entry)
7873 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7874 6458efa5 2020-11-24 stsp else
7875 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7876 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7877 6458efa5 2020-11-24 stsp } else {
7878 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7879 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7880 6458efa5 2020-11-24 stsp else
7881 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7882 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7883 6458efa5 2020-11-24 stsp }
7884 6458efa5 2020-11-24 stsp } else {
7885 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7886 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7887 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7888 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7889 6458efa5 2020-11-24 stsp else
7890 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7891 6458efa5 2020-11-24 stsp }
7892 6458efa5 2020-11-24 stsp
7893 6458efa5 2020-11-24 stsp while (1) {
7894 6458efa5 2020-11-24 stsp if (re == NULL) {
7895 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7896 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7897 6458efa5 2020-11-24 stsp return NULL;
7898 6458efa5 2020-11-24 stsp }
7899 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7900 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7901 6458efa5 2020-11-24 stsp else
7902 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7903 6458efa5 2020-11-24 stsp }
7904 6458efa5 2020-11-24 stsp
7905 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7906 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7907 6458efa5 2020-11-24 stsp s->matched_entry = re;
7908 6458efa5 2020-11-24 stsp break;
7909 6458efa5 2020-11-24 stsp }
7910 6458efa5 2020-11-24 stsp
7911 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7912 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7913 6458efa5 2020-11-24 stsp else
7914 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7915 6458efa5 2020-11-24 stsp }
7916 6458efa5 2020-11-24 stsp
7917 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7918 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7919 6458efa5 2020-11-24 stsp s->selected = 0;
7920 6458efa5 2020-11-24 stsp }
7921 6458efa5 2020-11-24 stsp
7922 6458efa5 2020-11-24 stsp return NULL;
7923 6458efa5 2020-11-24 stsp }
7924 6458efa5 2020-11-24 stsp
7925 6458efa5 2020-11-24 stsp static const struct got_error *
7926 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7927 6458efa5 2020-11-24 stsp {
7928 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7929 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7930 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7931 6458efa5 2020-11-24 stsp char *line = NULL;
7932 6458efa5 2020-11-24 stsp wchar_t *wline;
7933 6458efa5 2020-11-24 stsp struct tog_color *tc;
7934 6458efa5 2020-11-24 stsp int width, n;
7935 6458efa5 2020-11-24 stsp int limit = view->nlines;
7936 6458efa5 2020-11-24 stsp
7937 6458efa5 2020-11-24 stsp werase(view->window);
7938 6458efa5 2020-11-24 stsp
7939 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7940 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7941 a5d43cac 2022-07-01 thomas --limit; /* border */
7942 6458efa5 2020-11-24 stsp
7943 6458efa5 2020-11-24 stsp if (limit == 0)
7944 6458efa5 2020-11-24 stsp return NULL;
7945 6458efa5 2020-11-24 stsp
7946 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7947 6458efa5 2020-11-24 stsp
7948 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7949 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7950 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7951 6458efa5 2020-11-24 stsp
7952 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7953 6458efa5 2020-11-24 stsp if (err) {
7954 6458efa5 2020-11-24 stsp free(line);
7955 6458efa5 2020-11-24 stsp return err;
7956 6458efa5 2020-11-24 stsp }
7957 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7958 6458efa5 2020-11-24 stsp wstandout(view->window);
7959 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7960 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7961 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7962 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7963 6458efa5 2020-11-24 stsp wstandend(view->window);
7964 6458efa5 2020-11-24 stsp free(wline);
7965 6458efa5 2020-11-24 stsp wline = NULL;
7966 6458efa5 2020-11-24 stsp free(line);
7967 6458efa5 2020-11-24 stsp line = NULL;
7968 6458efa5 2020-11-24 stsp if (--limit <= 0)
7969 6458efa5 2020-11-24 stsp return NULL;
7970 6458efa5 2020-11-24 stsp
7971 6458efa5 2020-11-24 stsp n = 0;
7972 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7973 6458efa5 2020-11-24 stsp char *line = NULL;
7974 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7975 6458efa5 2020-11-24 stsp
7976 84227eb1 2022-06-23 thomas if (s->show_date) {
7977 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7978 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7979 84227eb1 2022-06-23 thomas struct got_object_id *id;
7980 84227eb1 2022-06-23 thomas struct tm tm;
7981 84227eb1 2022-06-23 thomas time_t t;
7982 84227eb1 2022-06-23 thomas
7983 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7984 84227eb1 2022-06-23 thomas if (err)
7985 84227eb1 2022-06-23 thomas return err;
7986 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7987 84227eb1 2022-06-23 thomas if (err) {
7988 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7989 84227eb1 2022-06-23 thomas free(id);
7990 84227eb1 2022-06-23 thomas return err;
7991 84227eb1 2022-06-23 thomas }
7992 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7993 84227eb1 2022-06-23 thomas id);
7994 84227eb1 2022-06-23 thomas if (err) {
7995 84227eb1 2022-06-23 thomas free(id);
7996 84227eb1 2022-06-23 thomas return err;
7997 84227eb1 2022-06-23 thomas }
7998 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7999 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8000 84227eb1 2022-06-23 thomas } else {
8001 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8002 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8003 84227eb1 2022-06-23 thomas }
8004 84227eb1 2022-06-23 thomas free(id);
8005 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8006 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8007 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8008 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8009 84227eb1 2022-06-23 thomas }
8010 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8011 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8012 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8013 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8014 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8015 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8016 6458efa5 2020-11-24 stsp struct got_object_id *id;
8017 6458efa5 2020-11-24 stsp char *id_str;
8018 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8019 6458efa5 2020-11-24 stsp if (err)
8020 6458efa5 2020-11-24 stsp return err;
8021 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8022 6458efa5 2020-11-24 stsp if (err) {
8023 6458efa5 2020-11-24 stsp free(id);
8024 6458efa5 2020-11-24 stsp return err;
8025 6458efa5 2020-11-24 stsp }
8026 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8027 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8028 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8029 6458efa5 2020-11-24 stsp free(id);
8030 6458efa5 2020-11-24 stsp free(id_str);
8031 6458efa5 2020-11-24 stsp return err;
8032 6458efa5 2020-11-24 stsp }
8033 6458efa5 2020-11-24 stsp free(id);
8034 6458efa5 2020-11-24 stsp free(id_str);
8035 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8036 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8037 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8038 6458efa5 2020-11-24 stsp
8039 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8040 f91a2b48 2022-06-23 thomas 0, 0);
8041 6458efa5 2020-11-24 stsp if (err) {
8042 6458efa5 2020-11-24 stsp free(line);
8043 6458efa5 2020-11-24 stsp return err;
8044 6458efa5 2020-11-24 stsp }
8045 6458efa5 2020-11-24 stsp if (n == s->selected) {
8046 6458efa5 2020-11-24 stsp if (view->focussed)
8047 6458efa5 2020-11-24 stsp wstandout(view->window);
8048 6458efa5 2020-11-24 stsp s->selected_entry = re;
8049 6458efa5 2020-11-24 stsp }
8050 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8051 6458efa5 2020-11-24 stsp if (tc)
8052 6458efa5 2020-11-24 stsp wattr_on(view->window,
8053 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8054 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8055 6458efa5 2020-11-24 stsp if (tc)
8056 6458efa5 2020-11-24 stsp wattr_off(view->window,
8057 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8058 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
8059 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8060 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8061 6458efa5 2020-11-24 stsp wstandend(view->window);
8062 6458efa5 2020-11-24 stsp free(line);
8063 6458efa5 2020-11-24 stsp free(wline);
8064 6458efa5 2020-11-24 stsp wline = NULL;
8065 6458efa5 2020-11-24 stsp n++;
8066 6458efa5 2020-11-24 stsp s->ndisplayed++;
8067 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8068 6458efa5 2020-11-24 stsp
8069 6458efa5 2020-11-24 stsp limit--;
8070 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8071 6458efa5 2020-11-24 stsp }
8072 6458efa5 2020-11-24 stsp
8073 a5d43cac 2022-07-01 thomas view_border(view);
8074 6458efa5 2020-11-24 stsp return err;
8075 6458efa5 2020-11-24 stsp }
8076 6458efa5 2020-11-24 stsp
8077 6458efa5 2020-11-24 stsp static const struct got_error *
8078 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8079 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8080 c42c9805 2020-11-24 stsp {
8081 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8082 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8083 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8084 c42c9805 2020-11-24 stsp
8085 c42c9805 2020-11-24 stsp *new_view = NULL;
8086 c42c9805 2020-11-24 stsp
8087 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8088 c42c9805 2020-11-24 stsp if (err) {
8089 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8090 c42c9805 2020-11-24 stsp return err;
8091 c42c9805 2020-11-24 stsp else
8092 c42c9805 2020-11-24 stsp return NULL;
8093 c42c9805 2020-11-24 stsp }
8094 c42c9805 2020-11-24 stsp
8095 c42c9805 2020-11-24 stsp
8096 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8097 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8098 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8099 c42c9805 2020-11-24 stsp goto done;
8100 c42c9805 2020-11-24 stsp }
8101 c42c9805 2020-11-24 stsp
8102 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8103 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8104 c42c9805 2020-11-24 stsp if (err)
8105 c42c9805 2020-11-24 stsp goto done;
8106 c42c9805 2020-11-24 stsp
8107 c42c9805 2020-11-24 stsp *new_view = tree_view;
8108 c42c9805 2020-11-24 stsp done:
8109 c42c9805 2020-11-24 stsp free(commit_id);
8110 c42c9805 2020-11-24 stsp return err;
8111 07dd3ed3 2022-08-06 thomas }
8112 07dd3ed3 2022-08-06 thomas
8113 07dd3ed3 2022-08-06 thomas static const struct got_error *
8114 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8115 07dd3ed3 2022-08-06 thomas {
8116 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8117 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8118 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8119 07dd3ed3 2022-08-06 thomas
8120 07dd3ed3 2022-08-06 thomas g = view->gline;
8121 07dd3ed3 2022-08-06 thomas view->gline = 0;
8122 07dd3ed3 2022-08-06 thomas
8123 07dd3ed3 2022-08-06 thomas if (g == 0)
8124 07dd3ed3 2022-08-06 thomas g = 1;
8125 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8126 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8127 07dd3ed3 2022-08-06 thomas
8128 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8129 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8130 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8131 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8132 07dd3ed3 2022-08-06 thomas return NULL;
8133 07dd3ed3 2022-08-06 thomas }
8134 07dd3ed3 2022-08-06 thomas
8135 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8136 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8137 07dd3ed3 2022-08-06 thomas if (err)
8138 07dd3ed3 2022-08-06 thomas return err;
8139 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8140 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8141 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8142 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8143 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8144 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8145 07dd3ed3 2022-08-06 thomas
8146 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8147 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8148 07dd3ed3 2022-08-06 thomas
8149 07dd3ed3 2022-08-06 thomas return NULL;
8150 07dd3ed3 2022-08-06 thomas
8151 c42c9805 2020-11-24 stsp }
8152 07dd3ed3 2022-08-06 thomas
8153 c42c9805 2020-11-24 stsp static const struct got_error *
8154 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8155 6458efa5 2020-11-24 stsp {
8156 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8157 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8158 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8159 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8160 6458efa5 2020-11-24 stsp
8161 07dd3ed3 2022-08-06 thomas if (view->gline)
8162 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8163 07dd3ed3 2022-08-06 thomas
8164 6458efa5 2020-11-24 stsp switch (ch) {
8165 6458efa5 2020-11-24 stsp case 'i':
8166 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8167 07b0611c 2022-06-23 thomas view->count = 0;
8168 3bfadbd4 2021-11-20 thomas break;
8169 84227eb1 2022-06-23 thomas case 'm':
8170 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8171 07b0611c 2022-06-23 thomas view->count = 0;
8172 84227eb1 2022-06-23 thomas break;
8173 98182bd0 2021-11-20 thomas case 'o':
8174 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8175 07b0611c 2022-06-23 thomas view->count = 0;
8176 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8177 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8178 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8179 c591440f 2021-11-20 thomas if (err)
8180 c591440f 2021-11-20 thomas break;
8181 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8182 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8183 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8184 3bfadbd4 2021-11-20 thomas if (err)
8185 3bfadbd4 2021-11-20 thomas break;
8186 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8187 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8188 6458efa5 2020-11-24 stsp break;
8189 6458efa5 2020-11-24 stsp case KEY_ENTER:
8190 6458efa5 2020-11-24 stsp case '\r':
8191 07b0611c 2022-06-23 thomas view->count = 0;
8192 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8193 a5d43cac 2022-07-01 thomas break;
8194 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8195 6458efa5 2020-11-24 stsp break;
8196 1be4947a 2022-07-22 thomas case 'T':
8197 07b0611c 2022-06-23 thomas view->count = 0;
8198 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8199 c42c9805 2020-11-24 stsp break;
8200 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8201 c42c9805 2020-11-24 stsp break;
8202 e4526bf5 2021-09-03 naddy case 'g':
8203 e4526bf5 2021-09-03 naddy case KEY_HOME:
8204 e4526bf5 2021-09-03 naddy s->selected = 0;
8205 07b0611c 2022-06-23 thomas view->count = 0;
8206 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8207 e4526bf5 2021-09-03 naddy break;
8208 e4526bf5 2021-09-03 naddy case 'G':
8209 a5d43cac 2022-07-01 thomas case KEY_END: {
8210 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8211 a5d43cac 2022-07-01 thomas
8212 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8213 a5d43cac 2022-07-01 thomas --eos; /* border */
8214 e4526bf5 2021-09-03 naddy s->selected = 0;
8215 07b0611c 2022-06-23 thomas view->count = 0;
8216 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8217 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8218 e4526bf5 2021-09-03 naddy if (re == NULL)
8219 e4526bf5 2021-09-03 naddy break;
8220 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8221 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8222 e4526bf5 2021-09-03 naddy }
8223 e4526bf5 2021-09-03 naddy if (n > 0)
8224 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8225 e4526bf5 2021-09-03 naddy break;
8226 a5d43cac 2022-07-01 thomas }
8227 6458efa5 2020-11-24 stsp case 'k':
8228 6458efa5 2020-11-24 stsp case KEY_UP:
8229 f7140bf5 2021-10-17 thomas case CTRL('p'):
8230 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8231 6458efa5 2020-11-24 stsp s->selected--;
8232 6458efa5 2020-11-24 stsp break;
8233 34ba6917 2020-11-30 stsp }
8234 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8235 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8236 07b0611c 2022-06-23 thomas view->count = 0;
8237 6458efa5 2020-11-24 stsp break;
8238 70f17a53 2022-06-13 thomas case CTRL('u'):
8239 23427b14 2022-06-23 thomas case 'u':
8240 70f17a53 2022-06-13 thomas nscroll /= 2;
8241 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8242 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8243 6458efa5 2020-11-24 stsp case CTRL('b'):
8244 1c5e5faa 2022-06-23 thomas case 'b':
8245 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8246 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8247 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8248 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8249 07b0611c 2022-06-23 thomas view->count = 0;
8250 6458efa5 2020-11-24 stsp break;
8251 6458efa5 2020-11-24 stsp case 'j':
8252 6458efa5 2020-11-24 stsp case KEY_DOWN:
8253 f7140bf5 2021-10-17 thomas case CTRL('n'):
8254 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8255 6458efa5 2020-11-24 stsp s->selected++;
8256 6458efa5 2020-11-24 stsp break;
8257 6458efa5 2020-11-24 stsp }
8258 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8259 6458efa5 2020-11-24 stsp /* can't scroll any further */
8260 07b0611c 2022-06-23 thomas view->count = 0;
8261 6458efa5 2020-11-24 stsp break;
8262 07b0611c 2022-06-23 thomas }
8263 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8264 6458efa5 2020-11-24 stsp break;
8265 70f17a53 2022-06-13 thomas case CTRL('d'):
8266 23427b14 2022-06-23 thomas case 'd':
8267 70f17a53 2022-06-13 thomas nscroll /= 2;
8268 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8269 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8270 6458efa5 2020-11-24 stsp case CTRL('f'):
8271 1c5e5faa 2022-06-23 thomas case 'f':
8272 4c2d69cb 2022-06-23 thomas case ' ':
8273 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8274 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8275 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8276 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8277 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8278 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8279 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8280 07b0611c 2022-06-23 thomas view->count = 0;
8281 6458efa5 2020-11-24 stsp break;
8282 6458efa5 2020-11-24 stsp }
8283 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8284 6458efa5 2020-11-24 stsp break;
8285 6458efa5 2020-11-24 stsp case CTRL('l'):
8286 07b0611c 2022-06-23 thomas view->count = 0;
8287 8924d611 2020-12-26 stsp tog_free_refs();
8288 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8289 8924d611 2020-12-26 stsp if (err)
8290 8924d611 2020-12-26 stsp break;
8291 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8292 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8293 6458efa5 2020-11-24 stsp break;
8294 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8295 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8296 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8297 6458efa5 2020-11-24 stsp break;
8298 6458efa5 2020-11-24 stsp default:
8299 07b0611c 2022-06-23 thomas view->count = 0;
8300 6458efa5 2020-11-24 stsp break;
8301 6458efa5 2020-11-24 stsp }
8302 6458efa5 2020-11-24 stsp
8303 6458efa5 2020-11-24 stsp return err;
8304 6458efa5 2020-11-24 stsp }
8305 6458efa5 2020-11-24 stsp
8306 6458efa5 2020-11-24 stsp __dead static void
8307 6458efa5 2020-11-24 stsp usage_ref(void)
8308 6458efa5 2020-11-24 stsp {
8309 6458efa5 2020-11-24 stsp endwin();
8310 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8311 6458efa5 2020-11-24 stsp getprogname());
8312 6458efa5 2020-11-24 stsp exit(1);
8313 6458efa5 2020-11-24 stsp }
8314 6458efa5 2020-11-24 stsp
8315 6458efa5 2020-11-24 stsp static const struct got_error *
8316 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8317 6458efa5 2020-11-24 stsp {
8318 6458efa5 2020-11-24 stsp const struct got_error *error;
8319 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8320 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8321 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8322 6458efa5 2020-11-24 stsp int ch;
8323 6458efa5 2020-11-24 stsp struct tog_view *view;
8324 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8325 6458efa5 2020-11-24 stsp
8326 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8327 6458efa5 2020-11-24 stsp switch (ch) {
8328 6458efa5 2020-11-24 stsp case 'r':
8329 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8330 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8331 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8332 6458efa5 2020-11-24 stsp optarg);
8333 6458efa5 2020-11-24 stsp break;
8334 6458efa5 2020-11-24 stsp default:
8335 e99e2d15 2020-11-24 naddy usage_ref();
8336 6458efa5 2020-11-24 stsp /* NOTREACHED */
8337 6458efa5 2020-11-24 stsp }
8338 6458efa5 2020-11-24 stsp }
8339 6458efa5 2020-11-24 stsp
8340 6458efa5 2020-11-24 stsp argc -= optind;
8341 6458efa5 2020-11-24 stsp argv += optind;
8342 6458efa5 2020-11-24 stsp
8343 6458efa5 2020-11-24 stsp if (argc > 1)
8344 e99e2d15 2020-11-24 naddy usage_ref();
8345 7cd52833 2022-06-23 thomas
8346 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8347 7cd52833 2022-06-23 thomas if (error != NULL)
8348 7cd52833 2022-06-23 thomas goto done;
8349 6458efa5 2020-11-24 stsp
8350 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8351 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8352 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8353 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8354 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8355 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8356 c156c7a4 2020-12-18 stsp goto done;
8357 6458efa5 2020-11-24 stsp if (worktree)
8358 6458efa5 2020-11-24 stsp repo_path =
8359 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8360 6458efa5 2020-11-24 stsp else
8361 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8362 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8363 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8364 c156c7a4 2020-12-18 stsp goto done;
8365 c156c7a4 2020-12-18 stsp }
8366 6458efa5 2020-11-24 stsp }
8367 6458efa5 2020-11-24 stsp
8368 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8369 6458efa5 2020-11-24 stsp if (error != NULL)
8370 6458efa5 2020-11-24 stsp goto done;
8371 6458efa5 2020-11-24 stsp
8372 6458efa5 2020-11-24 stsp init_curses();
8373 6458efa5 2020-11-24 stsp
8374 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8375 51a10b52 2020-12-26 stsp if (error)
8376 51a10b52 2020-12-26 stsp goto done;
8377 51a10b52 2020-12-26 stsp
8378 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8379 6458efa5 2020-11-24 stsp if (error)
8380 6458efa5 2020-11-24 stsp goto done;
8381 6458efa5 2020-11-24 stsp
8382 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8383 6458efa5 2020-11-24 stsp if (view == NULL) {
8384 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8385 6458efa5 2020-11-24 stsp goto done;
8386 6458efa5 2020-11-24 stsp }
8387 6458efa5 2020-11-24 stsp
8388 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8389 6458efa5 2020-11-24 stsp if (error)
8390 6458efa5 2020-11-24 stsp goto done;
8391 6458efa5 2020-11-24 stsp
8392 6458efa5 2020-11-24 stsp if (worktree) {
8393 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8394 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8395 6458efa5 2020-11-24 stsp worktree = NULL;
8396 6458efa5 2020-11-24 stsp }
8397 6458efa5 2020-11-24 stsp error = view_loop(view);
8398 6458efa5 2020-11-24 stsp done:
8399 6458efa5 2020-11-24 stsp free(repo_path);
8400 6458efa5 2020-11-24 stsp free(cwd);
8401 1d0f4054 2021-06-17 stsp if (repo) {
8402 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8403 1d0f4054 2021-06-17 stsp if (close_err)
8404 1d0f4054 2021-06-17 stsp error = close_err;
8405 1d0f4054 2021-06-17 stsp }
8406 7cd52833 2022-06-23 thomas if (pack_fds) {
8407 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8408 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8409 7cd52833 2022-06-23 thomas if (error == NULL)
8410 7cd52833 2022-06-23 thomas error = pack_err;
8411 7cd52833 2022-06-23 thomas }
8412 51a10b52 2020-12-26 stsp tog_free_refs();
8413 6458efa5 2020-11-24 stsp return error;
8414 6458efa5 2020-11-24 stsp }
8415 6458efa5 2020-11-24 stsp
8416 fc2737d5 2022-09-15 thomas static const struct got_error*
8417 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8418 fc2737d5 2022-09-15 thomas const char *str)
8419 fc2737d5 2022-09-15 thomas {
8420 fc2737d5 2022-09-15 thomas size_t len;
8421 fc2737d5 2022-09-15 thomas
8422 fc2737d5 2022-09-15 thomas if (win == NULL)
8423 fc2737d5 2022-09-15 thomas win = stdscr;
8424 fc2737d5 2022-09-15 thomas
8425 fc2737d5 2022-09-15 thomas len = strlen(str);
8426 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8427 fc2737d5 2022-09-15 thomas
8428 fc2737d5 2022-09-15 thomas if (focus)
8429 fc2737d5 2022-09-15 thomas wstandout(win);
8430 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8431 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8432 fc2737d5 2022-09-15 thomas if (focus)
8433 fc2737d5 2022-09-15 thomas wstandend(win);
8434 fc2737d5 2022-09-15 thomas
8435 fc2737d5 2022-09-15 thomas return NULL;
8436 fc2737d5 2022-09-15 thomas }
8437 fc2737d5 2022-09-15 thomas
8438 2a31b33b 2022-07-23 thomas static const struct got_error *
8439 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8440 fc2737d5 2022-09-15 thomas {
8441 fc2737d5 2022-09-15 thomas off_t *p;
8442 fc2737d5 2022-09-15 thomas
8443 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8444 fc2737d5 2022-09-15 thomas if (p == NULL) {
8445 fc2737d5 2022-09-15 thomas free(*line_offsets);
8446 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8447 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8448 fc2737d5 2022-09-15 thomas }
8449 fc2737d5 2022-09-15 thomas
8450 fc2737d5 2022-09-15 thomas *line_offsets = p;
8451 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8452 fc2737d5 2022-09-15 thomas ++(*nlines);
8453 fc2737d5 2022-09-15 thomas return NULL;
8454 fc2737d5 2022-09-15 thomas }
8455 fc2737d5 2022-09-15 thomas
8456 fc2737d5 2022-09-15 thomas static const struct got_error *
8457 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8458 fc2737d5 2022-09-15 thomas {
8459 fc2737d5 2022-09-15 thomas *ret = 0;
8460 fc2737d5 2022-09-15 thomas
8461 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8462 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8463 fc2737d5 2022-09-15 thomas size_t len = 1;
8464 fc2737d5 2022-09-15 thomas
8465 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8466 fc2737d5 2022-09-15 thomas continue;
8467 fc2737d5 2022-09-15 thomas
8468 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8469 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8470 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8471 fc2737d5 2022-09-15 thomas
8472 fc2737d5 2022-09-15 thomas len += strlen(t);
8473 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8474 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8475 fc2737d5 2022-09-15 thomas free(t0);
8476 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8477 fc2737d5 2022-09-15 thomas }
8478 fc2737d5 2022-09-15 thomas
8479 fc2737d5 2022-09-15 thomas return NULL;
8480 fc2737d5 2022-09-15 thomas }
8481 fc2737d5 2022-09-15 thomas
8482 fc2737d5 2022-09-15 thomas /*
8483 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8484 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8485 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8486 fc2737d5 2022-09-15 thomas */
8487 fc2737d5 2022-09-15 thomas static const struct got_error *
8488 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8489 fc2737d5 2022-09-15 thomas {
8490 fc2737d5 2022-09-15 thomas int n, len = width;
8491 fc2737d5 2022-09-15 thomas
8492 fc2737d5 2022-09-15 thomas if (km->keys) {
8493 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8494 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8495 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8496 30e77f6a 2022-09-18 thomas };
8497 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8498 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8499 fc2737d5 2022-09-15 thomas
8500 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8501 fc2737d5 2022-09-15 thomas
8502 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8503 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8504 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8505 fc2737d5 2022-09-15 thomas
8506 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8507 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8508 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8509 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8510 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8511 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8512 fc2737d5 2022-09-15 thomas if (n < 0) {
8513 fc2737d5 2022-09-15 thomas free(t0);
8514 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8515 fc2737d5 2022-09-15 thomas }
8516 fc2737d5 2022-09-15 thomas first = 0;
8517 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8518 fc2737d5 2022-09-15 thomas *off += n;
8519 fc2737d5 2022-09-15 thomas }
8520 fc2737d5 2022-09-15 thomas free(t0);
8521 fc2737d5 2022-09-15 thomas }
8522 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8523 fc2737d5 2022-09-15 thomas if (n < 0)
8524 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8525 fc2737d5 2022-09-15 thomas *off += n;
8526 fc2737d5 2022-09-15 thomas
8527 fc2737d5 2022-09-15 thomas return NULL;
8528 fc2737d5 2022-09-15 thomas }
8529 fc2737d5 2022-09-15 thomas
8530 fc2737d5 2022-09-15 thomas static const struct got_error *
8531 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
8532 fc2737d5 2022-09-15 thomas {
8533 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8534 fc2737d5 2022-09-15 thomas off_t off = 0;
8535 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
8536 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
8537 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
8538 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8539 fc2737d5 2022-09-15 thomas GENERATE_HELP
8540 fc2737d5 2022-09-15 thomas #undef KEYMAP_
8541 fc2737d5 2022-09-15 thomas #undef KEY_
8542 fc2737d5 2022-09-15 thomas };
8543 fc2737d5 2022-09-15 thomas
8544 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8545 fc2737d5 2022-09-15 thomas if (err)
8546 fc2737d5 2022-09-15 thomas return err;
8547 fc2737d5 2022-09-15 thomas
8548 fc2737d5 2022-09-15 thomas n = nitems(km);
8549 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
8550 fc2737d5 2022-09-15 thomas if (err)
8551 fc2737d5 2022-09-15 thomas return err;
8552 fc2737d5 2022-09-15 thomas
8553 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
8554 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
8555 fc2737d5 2022-09-15 thomas show = s->all;
8556 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
8557 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
8558 fc2737d5 2022-09-15 thomas show = 1;
8559 fc2737d5 2022-09-15 thomas }
8560 fc2737d5 2022-09-15 thomas if (show) {
8561 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
8562 fc2737d5 2022-09-15 thomas if (err)
8563 fc2737d5 2022-09-15 thomas return err;
8564 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8565 fc2737d5 2022-09-15 thomas if (err)
8566 fc2737d5 2022-09-15 thomas return err;
8567 fc2737d5 2022-09-15 thomas }
8568 fc2737d5 2022-09-15 thomas }
8569 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
8570 fc2737d5 2022-09-15 thomas ++off;
8571 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8572 fc2737d5 2022-09-15 thomas return err;
8573 fc2737d5 2022-09-15 thomas }
8574 fc2737d5 2022-09-15 thomas
8575 fc2737d5 2022-09-15 thomas static const struct got_error *
8576 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
8577 fc2737d5 2022-09-15 thomas {
8578 fc2737d5 2022-09-15 thomas FILE *f;
8579 fc2737d5 2022-09-15 thomas const struct got_error *err;
8580 fc2737d5 2022-09-15 thomas
8581 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8582 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8583 fc2737d5 2022-09-15 thomas s->nlines = 0;
8584 fc2737d5 2022-09-15 thomas
8585 fc2737d5 2022-09-15 thomas f = got_opentemp();
8586 fc2737d5 2022-09-15 thomas if (f == NULL)
8587 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
8588 fc2737d5 2022-09-15 thomas s->f = f;
8589 fc2737d5 2022-09-15 thomas
8590 fc2737d5 2022-09-15 thomas err = format_help(s);
8591 fc2737d5 2022-09-15 thomas if (err)
8592 fc2737d5 2022-09-15 thomas return err;
8593 fc2737d5 2022-09-15 thomas
8594 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
8595 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
8596 fc2737d5 2022-09-15 thomas
8597 fc2737d5 2022-09-15 thomas return NULL;
8598 fc2737d5 2022-09-15 thomas }
8599 fc2737d5 2022-09-15 thomas
8600 fc2737d5 2022-09-15 thomas static const struct got_error *
8601 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
8602 fc2737d5 2022-09-15 thomas {
8603 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
8604 fc2737d5 2022-09-15 thomas return NULL;
8605 fc2737d5 2022-09-15 thomas }
8606 fc2737d5 2022-09-15 thomas
8607 2a7d3cd7 2022-09-17 thomas static void
8608 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8609 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
8610 2a7d3cd7 2022-09-17 thomas {
8611 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
8612 2a7d3cd7 2022-09-17 thomas
8613 2a7d3cd7 2022-09-17 thomas *f = s->f;
8614 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
8615 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
8616 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
8617 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
8618 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
8619 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
8620 2a7d3cd7 2022-09-17 thomas }
8621 2a7d3cd7 2022-09-17 thomas
8622 fc2737d5 2022-09-15 thomas static const struct got_error *
8623 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
8624 fc2737d5 2022-09-15 thomas {
8625 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8626 fc2737d5 2022-09-15 thomas const struct got_error *err;
8627 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
8628 fc2737d5 2022-09-15 thomas wchar_t *wline;
8629 fc2737d5 2022-09-15 thomas char *line;
8630 fc2737d5 2022-09-15 thomas ssize_t linelen;
8631 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8632 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
8633 fc2737d5 2022-09-15 thomas int eos = view->nlines;
8634 fc2737d5 2022-09-15 thomas
8635 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8636 fc2737d5 2022-09-15 thomas --eos; /* account for border */
8637 fc2737d5 2022-09-15 thomas
8638 fc2737d5 2022-09-15 thomas s->lineno = 0;
8639 fc2737d5 2022-09-15 thomas rewind(s->f);
8640 fc2737d5 2022-09-15 thomas werase(view->window);
8641 fc2737d5 2022-09-15 thomas
8642 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
8643 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
8644 fc2737d5 2022-09-15 thomas
8645 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
8646 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
8647 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
8648 fc2737d5 2022-09-15 thomas if (err)
8649 fc2737d5 2022-09-15 thomas return err;
8650 fc2737d5 2022-09-15 thomas if (eos <= 1)
8651 fc2737d5 2022-09-15 thomas return NULL;
8652 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
8653 fc2737d5 2022-09-15 thomas eos -= 2;
8654 fc2737d5 2022-09-15 thomas
8655 fc2737d5 2022-09-15 thomas s->eof = 0;
8656 fc2737d5 2022-09-15 thomas view->maxx = 0;
8657 fc2737d5 2022-09-15 thomas line = NULL;
8658 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
8659 fc2737d5 2022-09-15 thomas attr_t attr = 0;
8660 fc2737d5 2022-09-15 thomas
8661 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8662 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8663 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
8664 fc2737d5 2022-09-15 thomas free(line);
8665 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
8666 fc2737d5 2022-09-15 thomas }
8667 fc2737d5 2022-09-15 thomas s->eof = 1;
8668 fc2737d5 2022-09-15 thomas break;
8669 fc2737d5 2022-09-15 thomas }
8670 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
8671 fc2737d5 2022-09-15 thomas continue;
8672 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8673 fc2737d5 2022-09-15 thomas continue;
8674 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
8675 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
8676 fc2737d5 2022-09-15 thomas
8677 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8678 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
8679 fc2737d5 2022-09-15 thomas if (err) {
8680 fc2737d5 2022-09-15 thomas free(line);
8681 fc2737d5 2022-09-15 thomas return err;
8682 fc2737d5 2022-09-15 thomas }
8683 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
8684 fc2737d5 2022-09-15 thomas free(wline);
8685 fc2737d5 2022-09-15 thomas wline = NULL;
8686 fc2737d5 2022-09-15 thomas
8687 fc2737d5 2022-09-15 thomas if (attr)
8688 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
8689 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
8690 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8691 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
8692 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
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 } else {
8698 fc2737d5 2022-09-15 thomas int skip;
8699 fc2737d5 2022-09-15 thomas
8700 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
8701 fc2737d5 2022-09-15 thomas view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8702 fc2737d5 2022-09-15 thomas if (err) {
8703 fc2737d5 2022-09-15 thomas free(line);
8704 fc2737d5 2022-09-15 thomas return err;
8705 fc2737d5 2022-09-15 thomas }
8706 fc2737d5 2022-09-15 thomas rc = waddwstr(view->window, &wline[skip]);
8707 fc2737d5 2022-09-15 thomas free(wline);
8708 fc2737d5 2022-09-15 thomas wline = NULL;
8709 fc2737d5 2022-09-15 thomas if (rc == ERR)
8710 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "waddwstr");
8711 fc2737d5 2022-09-15 thomas }
8712 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
8713 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
8714 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
8715 fc2737d5 2022-09-15 thomas } else {
8716 fc2737d5 2022-09-15 thomas if (width <= view->ncols)
8717 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
8718 fc2737d5 2022-09-15 thomas }
8719 fc2737d5 2022-09-15 thomas if (attr)
8720 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
8721 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
8722 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
8723 fc2737d5 2022-09-15 thomas }
8724 fc2737d5 2022-09-15 thomas free(line);
8725 fc2737d5 2022-09-15 thomas if (nprinted > 0)
8726 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8727 fc2737d5 2022-09-15 thomas else
8728 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
8729 fc2737d5 2022-09-15 thomas
8730 fc2737d5 2022-09-15 thomas view_border(view);
8731 fc2737d5 2022-09-15 thomas
8732 fc2737d5 2022-09-15 thomas if (s->eof) {
8733 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
8734 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
8735 fc2737d5 2022-09-15 thomas view->ncols - 1);
8736 fc2737d5 2022-09-15 thomas if (rc == ERR)
8737 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8738 fc2737d5 2022-09-15 thomas } else {
8739 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
8740 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
8741 fc2737d5 2022-09-15 thomas wstandout(view->window);
8742 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
8743 fc2737d5 2022-09-15 thomas view->ncols - 1);
8744 fc2737d5 2022-09-15 thomas if (rc == ERR)
8745 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8746 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
8747 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
8748 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
8749 fc2737d5 2022-09-15 thomas if (rc == ERR)
8750 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
8751 fc2737d5 2022-09-15 thomas }
8752 fc2737d5 2022-09-15 thomas wstandend(view->window);
8753 fc2737d5 2022-09-15 thomas }
8754 fc2737d5 2022-09-15 thomas
8755 fc2737d5 2022-09-15 thomas return NULL;
8756 fc2737d5 2022-09-15 thomas }
8757 fc2737d5 2022-09-15 thomas
8758 fc2737d5 2022-09-15 thomas static const struct got_error *
8759 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8760 fc2737d5 2022-09-15 thomas {
8761 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8762 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8763 fc2737d5 2022-09-15 thomas char *line = NULL;
8764 fc2737d5 2022-09-15 thomas ssize_t linelen;
8765 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8766 fc2737d5 2022-09-15 thomas int eos, nscroll;
8767 fc2737d5 2022-09-15 thomas
8768 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
8769 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8770 fc2737d5 2022-09-15 thomas --eos; /* border */
8771 fc2737d5 2022-09-15 thomas
8772 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
8773 fc2737d5 2022-09-15 thomas
8774 fc2737d5 2022-09-15 thomas switch (ch) {
8775 fc2737d5 2022-09-15 thomas case '0':
8776 fc2737d5 2022-09-15 thomas view->x = 0;
8777 fc2737d5 2022-09-15 thomas break;
8778 fc2737d5 2022-09-15 thomas case '$':
8779 fc2737d5 2022-09-15 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
8780 fc2737d5 2022-09-15 thomas view->count = 0;
8781 fc2737d5 2022-09-15 thomas break;
8782 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
8783 fc2737d5 2022-09-15 thomas case 'l':
8784 fc2737d5 2022-09-15 thomas if (view->x + view->ncols / 3 < view->maxx)
8785 fc2737d5 2022-09-15 thomas view->x += 2;
8786 fc2737d5 2022-09-15 thomas else
8787 fc2737d5 2022-09-15 thomas view->count = 0;
8788 fc2737d5 2022-09-15 thomas break;
8789 fc2737d5 2022-09-15 thomas case KEY_LEFT:
8790 fc2737d5 2022-09-15 thomas case 'h':
8791 fc2737d5 2022-09-15 thomas view->x -= MIN(view->x, 2);
8792 fc2737d5 2022-09-15 thomas if (view->x <= 0)
8793 fc2737d5 2022-09-15 thomas view->count = 0;
8794 fc2737d5 2022-09-15 thomas break;
8795 fc2737d5 2022-09-15 thomas case 'g':
8796 fc2737d5 2022-09-15 thomas case KEY_HOME:
8797 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8798 fc2737d5 2022-09-15 thomas view->count = 0;
8799 fc2737d5 2022-09-15 thomas break;
8800 fc2737d5 2022-09-15 thomas case 'G':
8801 fc2737d5 2022-09-15 thomas case KEY_END:
8802 fc2737d5 2022-09-15 thomas view->count = 0;
8803 fc2737d5 2022-09-15 thomas if (s->eof)
8804 fc2737d5 2022-09-15 thomas break;
8805 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
8806 fc2737d5 2022-09-15 thomas s->eof = 1;
8807 fc2737d5 2022-09-15 thomas break;
8808 fc2737d5 2022-09-15 thomas case 'k':
8809 fc2737d5 2022-09-15 thomas case KEY_UP:
8810 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
8811 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
8812 fc2737d5 2022-09-15 thomas else
8813 fc2737d5 2022-09-15 thomas view->count = 0;
8814 fc2737d5 2022-09-15 thomas break;
8815 fc2737d5 2022-09-15 thomas case CTRL('u'):
8816 fc2737d5 2022-09-15 thomas case 'u':
8817 fc2737d5 2022-09-15 thomas nscroll /= 2;
8818 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8819 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
8820 fc2737d5 2022-09-15 thomas case CTRL('b'):
8821 fc2737d5 2022-09-15 thomas case 'b':
8822 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
8823 fc2737d5 2022-09-15 thomas view->count = 0;
8824 fc2737d5 2022-09-15 thomas break;
8825 fc2737d5 2022-09-15 thomas }
8826 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
8827 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
8828 fc2737d5 2022-09-15 thomas break;
8829 fc2737d5 2022-09-15 thomas case 'j':
8830 fc2737d5 2022-09-15 thomas case KEY_DOWN:
8831 fc2737d5 2022-09-15 thomas case CTRL('n'):
8832 fc2737d5 2022-09-15 thomas if (!s->eof)
8833 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
8834 fc2737d5 2022-09-15 thomas else
8835 fc2737d5 2022-09-15 thomas view->count = 0;
8836 fc2737d5 2022-09-15 thomas break;
8837 fc2737d5 2022-09-15 thomas case CTRL('d'):
8838 fc2737d5 2022-09-15 thomas case 'd':
8839 fc2737d5 2022-09-15 thomas nscroll /= 2;
8840 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8841 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
8842 fc2737d5 2022-09-15 thomas case CTRL('f'):
8843 fc2737d5 2022-09-15 thomas case 'f':
8844 fc2737d5 2022-09-15 thomas case ' ':
8845 fc2737d5 2022-09-15 thomas if (s->eof) {
8846 fc2737d5 2022-09-15 thomas view->count = 0;
8847 fc2737d5 2022-09-15 thomas break;
8848 fc2737d5 2022-09-15 thomas }
8849 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
8850 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8851 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
8852 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8853 fc2737d5 2022-09-15 thomas if (feof(s->f))
8854 fc2737d5 2022-09-15 thomas s->eof = 1;
8855 fc2737d5 2022-09-15 thomas else
8856 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
8857 fc2737d5 2022-09-15 thomas break;
8858 fc2737d5 2022-09-15 thomas }
8859 fc2737d5 2022-09-15 thomas }
8860 fc2737d5 2022-09-15 thomas free(line);
8861 fc2737d5 2022-09-15 thomas break;
8862 fc2737d5 2022-09-15 thomas default:
8863 fc2737d5 2022-09-15 thomas view->count = 0;
8864 fc2737d5 2022-09-15 thomas break;
8865 fc2737d5 2022-09-15 thomas }
8866 fc2737d5 2022-09-15 thomas
8867 fc2737d5 2022-09-15 thomas return err;
8868 fc2737d5 2022-09-15 thomas }
8869 fc2737d5 2022-09-15 thomas
8870 fc2737d5 2022-09-15 thomas static const struct got_error *
8871 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
8872 fc2737d5 2022-09-15 thomas {
8873 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8874 fc2737d5 2022-09-15 thomas
8875 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8876 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8877 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
8878 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8879 fc2737d5 2022-09-15 thomas
8880 fc2737d5 2022-09-15 thomas return NULL;
8881 fc2737d5 2022-09-15 thomas }
8882 fc2737d5 2022-09-15 thomas
8883 fc2737d5 2022-09-15 thomas static const struct got_error *
8884 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
8885 fc2737d5 2022-09-15 thomas {
8886 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8887 fc2737d5 2022-09-15 thomas
8888 fc2737d5 2022-09-15 thomas
8889 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
8890 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
8891 fc2737d5 2022-09-15 thomas
8892 fc2737d5 2022-09-15 thomas wclear(view->window);
8893 fc2737d5 2022-09-15 thomas view->count = 0;
8894 fc2737d5 2022-09-15 thomas view->x = 0;
8895 fc2737d5 2022-09-15 thomas s->all = !s->all;
8896 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8897 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
8898 fc2737d5 2022-09-15 thomas s->matched_line = 0;
8899 fc2737d5 2022-09-15 thomas
8900 fc2737d5 2022-09-15 thomas return create_help(s);
8901 fc2737d5 2022-09-15 thomas }
8902 fc2737d5 2022-09-15 thomas
8903 fc2737d5 2022-09-15 thomas static const struct got_error *
8904 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
8905 fc2737d5 2022-09-15 thomas {
8906 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8907 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8908 fc2737d5 2022-09-15 thomas
8909 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
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->selected_line = 1;
8913 fc2737d5 2022-09-15 thomas
8914 fc2737d5 2022-09-15 thomas view->show = show_help_view;
8915 fc2737d5 2022-09-15 thomas view->input = input_help_view;
8916 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
8917 fc2737d5 2022-09-15 thomas view->close = close_help_view;
8918 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
8919 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
8920 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
8921 fc2737d5 2022-09-15 thomas
8922 fc2737d5 2022-09-15 thomas err = create_help(s);
8923 fc2737d5 2022-09-15 thomas return err;
8924 fc2737d5 2022-09-15 thomas }
8925 fc2737d5 2022-09-15 thomas
8926 fc2737d5 2022-09-15 thomas static const struct got_error *
8927 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8928 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8929 2a31b33b 2022-07-23 thomas {
8930 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8931 2a31b33b 2022-07-23 thomas
8932 2a31b33b 2022-07-23 thomas *new_view = NULL;
8933 2a31b33b 2022-07-23 thomas
8934 2a31b33b 2022-07-23 thomas switch (request) {
8935 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8936 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8937 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8938 2a31b33b 2022-07-23 thomas
8939 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8940 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8941 2a31b33b 2022-07-23 thomas view, s->repo);
8942 2a31b33b 2022-07-23 thomas } else
8943 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8944 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8945 2a31b33b 2022-07-23 thomas break;
8946 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8947 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8948 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8949 2a31b33b 2022-07-23 thomas
8950 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8951 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8952 2a31b33b 2022-07-23 thomas s->repo);
8953 2a31b33b 2022-07-23 thomas } else
8954 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8955 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8956 2a31b33b 2022-07-23 thomas break;
8957 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8958 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8959 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8960 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8961 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8962 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8963 2a31b33b 2022-07-23 thomas &view->state.tree);
8964 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8965 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8966 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8967 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8968 2a31b33b 2022-07-23 thomas else
8969 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8970 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8971 2a31b33b 2022-07-23 thomas break;
8972 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8973 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8974 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8975 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8976 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8977 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8978 2a31b33b 2022-07-23 thomas view->state.log.repo);
8979 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8980 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8981 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8982 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8983 2a31b33b 2022-07-23 thomas else
8984 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8985 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8986 2a31b33b 2022-07-23 thomas break;
8987 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
8988 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8989 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
8990 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
8991 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8992 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
8993 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8994 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
8995 2a31b33b 2022-07-23 thomas else
8996 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
8997 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8998 2a31b33b 2022-07-23 thomas if (err)
8999 2a31b33b 2022-07-23 thomas view_close(*new_view);
9000 2a31b33b 2022-07-23 thomas break;
9001 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9002 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9003 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9004 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9005 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9006 fc2737d5 2022-09-15 thomas if (err)
9007 fc2737d5 2022-09-15 thomas view_close(*new_view);
9008 fc2737d5 2022-09-15 thomas break;
9009 2a31b33b 2022-07-23 thomas default:
9010 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9011 2a31b33b 2022-07-23 thomas }
9012 2a31b33b 2022-07-23 thomas
9013 2a31b33b 2022-07-23 thomas return err;
9014 2a31b33b 2022-07-23 thomas }
9015 2a31b33b 2022-07-23 thomas
9016 a5d43cac 2022-07-01 thomas /*
9017 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9018 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9019 a5d43cac 2022-07-01 thomas */
9020 6458efa5 2020-11-24 stsp static void
9021 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9022 a5d43cac 2022-07-01 thomas {
9023 a5d43cac 2022-07-01 thomas switch (view->type) {
9024 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9025 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9026 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9027 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9028 a5d43cac 2022-07-01 thomas 1);
9029 a5d43cac 2022-07-01 thomas break;
9030 a5d43cac 2022-07-01 thomas }
9031 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9032 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9033 a5d43cac 2022-07-01 thomas else
9034 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9035 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9036 a5d43cac 2022-07-01 thomas break;
9037 a5d43cac 2022-07-01 thomas }
9038 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9039 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9040 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9041 a5d43cac 2022-07-01 thomas break;
9042 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9043 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9044 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9045 a5d43cac 2022-07-01 thomas break;
9046 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9047 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9048 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9049 a5d43cac 2022-07-01 thomas break;
9050 a5d43cac 2022-07-01 thomas default:
9051 a5d43cac 2022-07-01 thomas break;
9052 a5d43cac 2022-07-01 thomas }
9053 a5d43cac 2022-07-01 thomas
9054 a5d43cac 2022-07-01 thomas view->offset = 0;
9055 a5d43cac 2022-07-01 thomas }
9056 a5d43cac 2022-07-01 thomas
9057 a5d43cac 2022-07-01 thomas /*
9058 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9059 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9060 a5d43cac 2022-07-01 thomas */
9061 a5d43cac 2022-07-01 thomas static const struct got_error *
9062 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9063 a5d43cac 2022-07-01 thomas {
9064 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9065 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9066 a5d43cac 2022-07-01 thomas int *selected = NULL;
9067 a5d43cac 2022-07-01 thomas int header, offset;
9068 a5d43cac 2022-07-01 thomas
9069 a5d43cac 2022-07-01 thomas switch (view->type) {
9070 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9071 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9072 a5d43cac 2022-07-01 thomas header = 3;
9073 a5d43cac 2022-07-01 thomas scrolld = NULL;
9074 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9075 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9076 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9077 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9078 a5d43cac 2022-07-01 thomas view->offset = offset;
9079 a5d43cac 2022-07-01 thomas }
9080 a5d43cac 2022-07-01 thomas break;
9081 a5d43cac 2022-07-01 thomas }
9082 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9083 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9084 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9085 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9086 a5d43cac 2022-07-01 thomas selected = &s->selected;
9087 a5d43cac 2022-07-01 thomas break;
9088 a5d43cac 2022-07-01 thomas }
9089 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9090 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9091 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9092 a5d43cac 2022-07-01 thomas header = 3;
9093 a5d43cac 2022-07-01 thomas selected = &s->selected;
9094 a5d43cac 2022-07-01 thomas break;
9095 a5d43cac 2022-07-01 thomas }
9096 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9097 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9098 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9099 a5d43cac 2022-07-01 thomas header = 5;
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 default:
9104 a5d43cac 2022-07-01 thomas selected = NULL;
9105 a5d43cac 2022-07-01 thomas scrolld = NULL;
9106 a5d43cac 2022-07-01 thomas header = 0;
9107 a5d43cac 2022-07-01 thomas break;
9108 a5d43cac 2022-07-01 thomas }
9109 a5d43cac 2022-07-01 thomas
9110 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9111 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9112 a5d43cac 2022-07-01 thomas view->offset = offset;
9113 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9114 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9115 a5d43cac 2022-07-01 thomas *selected -= offset;
9116 a5d43cac 2022-07-01 thomas }
9117 a5d43cac 2022-07-01 thomas }
9118 a5d43cac 2022-07-01 thomas
9119 a5d43cac 2022-07-01 thomas return err;
9120 a5d43cac 2022-07-01 thomas }
9121 a5d43cac 2022-07-01 thomas
9122 a5d43cac 2022-07-01 thomas static void
9123 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9124 ce5b7c56 2019-07-09 stsp {
9125 6059809a 2020-12-17 stsp size_t i;
9126 9f7d7167 2018-04-29 stsp
9127 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9128 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9129 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9130 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9131 ce5b7c56 2019-07-09 stsp }
9132 6879ba42 2020-10-01 naddy fputc('\n', fp);
9133 ce5b7c56 2019-07-09 stsp }
9134 ce5b7c56 2019-07-09 stsp
9135 4ed7e80c 2018-05-20 stsp __dead static void
9136 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9137 9f7d7167 2018-04-29 stsp {
9138 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9139 6879ba42 2020-10-01 naddy
9140 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9141 6879ba42 2020-10-01 naddy getprogname());
9142 6879ba42 2020-10-01 naddy if (hflag) {
9143 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9144 6879ba42 2020-10-01 naddy list_commands(fp);
9145 ee85c5e8 2020-02-29 stsp }
9146 6879ba42 2020-10-01 naddy exit(status);
9147 9f7d7167 2018-04-29 stsp }
9148 9f7d7167 2018-04-29 stsp
9149 c2301be8 2018-04-30 stsp static char **
9150 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9151 c2301be8 2018-04-30 stsp {
9152 ee85c5e8 2020-02-29 stsp va_list ap;
9153 c2301be8 2018-04-30 stsp char **argv;
9154 ee85c5e8 2020-02-29 stsp int i;
9155 c2301be8 2018-04-30 stsp
9156 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9157 ee85c5e8 2020-02-29 stsp
9158 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9159 c2301be8 2018-04-30 stsp if (argv == NULL)
9160 c2301be8 2018-04-30 stsp err(1, "calloc");
9161 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9162 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9163 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9164 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9165 c2301be8 2018-04-30 stsp }
9166 c2301be8 2018-04-30 stsp
9167 ee85c5e8 2020-02-29 stsp va_end(ap);
9168 c2301be8 2018-04-30 stsp return argv;
9169 ee85c5e8 2020-02-29 stsp }
9170 ee85c5e8 2020-02-29 stsp
9171 ee85c5e8 2020-02-29 stsp /*
9172 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9173 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9174 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9175 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9176 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9177 ee85c5e8 2020-02-29 stsp */
9178 ee85c5e8 2020-02-29 stsp static const struct got_error *
9179 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9180 ee85c5e8 2020-02-29 stsp {
9181 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9182 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9183 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9184 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9185 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9186 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9187 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9188 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9189 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9190 84de9106 2020-12-26 stsp
9191 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9192 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9193 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9194 ee85c5e8 2020-02-29 stsp
9195 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9196 7cd52833 2022-06-23 thomas if (error != NULL)
9197 7cd52833 2022-06-23 thomas goto done;
9198 7cd52833 2022-06-23 thomas
9199 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9200 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9201 ee85c5e8 2020-02-29 stsp goto done;
9202 ee85c5e8 2020-02-29 stsp
9203 ee85c5e8 2020-02-29 stsp if (worktree)
9204 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9205 ee85c5e8 2020-02-29 stsp else
9206 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9207 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9208 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9209 ee85c5e8 2020-02-29 stsp goto done;
9210 ee85c5e8 2020-02-29 stsp }
9211 ee85c5e8 2020-02-29 stsp
9212 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9213 ee85c5e8 2020-02-29 stsp if (error != NULL)
9214 ee85c5e8 2020-02-29 stsp goto done;
9215 ee85c5e8 2020-02-29 stsp
9216 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9217 ee85c5e8 2020-02-29 stsp repo, worktree);
9218 ee85c5e8 2020-02-29 stsp if (error)
9219 ee85c5e8 2020-02-29 stsp goto done;
9220 ee85c5e8 2020-02-29 stsp
9221 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9222 84de9106 2020-12-26 stsp if (error)
9223 84de9106 2020-12-26 stsp goto done;
9224 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9225 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9226 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9227 ee85c5e8 2020-02-29 stsp if (error)
9228 ee85c5e8 2020-02-29 stsp goto done;
9229 ee85c5e8 2020-02-29 stsp
9230 ee85c5e8 2020-02-29 stsp if (worktree) {
9231 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9232 ee85c5e8 2020-02-29 stsp worktree = NULL;
9233 ee85c5e8 2020-02-29 stsp }
9234 ee85c5e8 2020-02-29 stsp
9235 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9236 945f9229 2022-04-16 thomas if (error)
9237 945f9229 2022-04-16 thomas goto done;
9238 945f9229 2022-04-16 thomas
9239 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9240 ee85c5e8 2020-02-29 stsp if (error) {
9241 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9242 ee85c5e8 2020-02-29 stsp goto done;
9243 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9244 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9245 6879ba42 2020-10-01 naddy usage(1, 1);
9246 ee85c5e8 2020-02-29 stsp /* not reached */
9247 ee85c5e8 2020-02-29 stsp }
9248 ee85c5e8 2020-02-29 stsp
9249 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9250 ee85c5e8 2020-02-29 stsp if (error)
9251 ee85c5e8 2020-02-29 stsp goto done;
9252 ee85c5e8 2020-02-29 stsp
9253 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9254 ee85c5e8 2020-02-29 stsp argc = 4;
9255 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9256 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9257 ee85c5e8 2020-02-29 stsp done:
9258 1d0f4054 2021-06-17 stsp if (repo) {
9259 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9260 1d0f4054 2021-06-17 stsp if (error == NULL)
9261 1d0f4054 2021-06-17 stsp error = close_err;
9262 1d0f4054 2021-06-17 stsp }
9263 945f9229 2022-04-16 thomas if (commit)
9264 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9265 ee85c5e8 2020-02-29 stsp if (worktree)
9266 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9267 7cd52833 2022-06-23 thomas if (pack_fds) {
9268 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9269 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9270 7cd52833 2022-06-23 thomas if (error == NULL)
9271 7cd52833 2022-06-23 thomas error = pack_err;
9272 7cd52833 2022-06-23 thomas }
9273 ee85c5e8 2020-02-29 stsp free(id);
9274 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9275 ee85c5e8 2020-02-29 stsp free(commit_id);
9276 ee85c5e8 2020-02-29 stsp free(cwd);
9277 ee85c5e8 2020-02-29 stsp free(repo_path);
9278 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9279 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9280 ee85c5e8 2020-02-29 stsp int i;
9281 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9282 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9283 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9284 ee85c5e8 2020-02-29 stsp }
9285 87670572 2020-12-26 naddy tog_free_refs();
9286 ee85c5e8 2020-02-29 stsp return error;
9287 c2301be8 2018-04-30 stsp }
9288 c2301be8 2018-04-30 stsp
9289 9f7d7167 2018-04-29 stsp int
9290 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9291 9f7d7167 2018-04-29 stsp {
9292 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9293 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9294 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9295 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9296 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9297 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9298 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9299 83cd27f8 2020-01-13 stsp };
9300 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9301 9f7d7167 2018-04-29 stsp
9302 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9303 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9304 cacf1690 2022-09-06 thomas
9305 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9306 9f7d7167 2018-04-29 stsp
9307 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9308 9f7d7167 2018-04-29 stsp switch (ch) {
9309 9f7d7167 2018-04-29 stsp case 'h':
9310 9f7d7167 2018-04-29 stsp hflag = 1;
9311 9f7d7167 2018-04-29 stsp break;
9312 53ccebc2 2019-07-30 stsp case 'V':
9313 53ccebc2 2019-07-30 stsp Vflag = 1;
9314 53ccebc2 2019-07-30 stsp break;
9315 9f7d7167 2018-04-29 stsp default:
9316 6879ba42 2020-10-01 naddy usage(hflag, 1);
9317 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9318 9f7d7167 2018-04-29 stsp }
9319 9f7d7167 2018-04-29 stsp }
9320 9f7d7167 2018-04-29 stsp
9321 9f7d7167 2018-04-29 stsp argc -= optind;
9322 9f7d7167 2018-04-29 stsp argv += optind;
9323 9814e6a3 2020-09-27 naddy optind = 1;
9324 c2301be8 2018-04-30 stsp optreset = 1;
9325 9f7d7167 2018-04-29 stsp
9326 53ccebc2 2019-07-30 stsp if (Vflag) {
9327 53ccebc2 2019-07-30 stsp got_version_print_str();
9328 6879ba42 2020-10-01 naddy return 0;
9329 53ccebc2 2019-07-30 stsp }
9330 4010e238 2020-12-04 stsp
9331 4010e238 2020-12-04 stsp #ifndef PROFILE
9332 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9333 4010e238 2020-12-04 stsp NULL) == -1)
9334 4010e238 2020-12-04 stsp err(1, "pledge");
9335 4010e238 2020-12-04 stsp #endif
9336 53ccebc2 2019-07-30 stsp
9337 c2301be8 2018-04-30 stsp if (argc == 0) {
9338 f29d3e89 2018-06-23 stsp if (hflag)
9339 6879ba42 2020-10-01 naddy usage(hflag, 0);
9340 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9341 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9342 c2301be8 2018-04-30 stsp argc = 1;
9343 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9344 c2301be8 2018-04-30 stsp } else {
9345 6059809a 2020-12-17 stsp size_t i;
9346 9f7d7167 2018-04-29 stsp
9347 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9348 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9349 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9350 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9351 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9352 9f7d7167 2018-04-29 stsp break;
9353 9f7d7167 2018-04-29 stsp }
9354 9f7d7167 2018-04-29 stsp }
9355 ee85c5e8 2020-02-29 stsp }
9356 3642c4c6 2019-07-09 stsp
9357 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9358 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9359 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9360 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9361 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9362 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9363 adf4c9e0 2022-07-03 thomas }
9364 adf4c9e0 2022-07-03 thomas
9365 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9366 ee85c5e8 2020-02-29 stsp if (argc != 1)
9367 6879ba42 2020-10-01 naddy usage(0, 1);
9368 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9369 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9370 ee85c5e8 2020-02-29 stsp } else {
9371 ee85c5e8 2020-02-29 stsp if (hflag)
9372 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9373 ee85c5e8 2020-02-29 stsp else
9374 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9375 9f7d7167 2018-04-29 stsp }
9376 9f7d7167 2018-04-29 stsp
9377 9f7d7167 2018-04-29 stsp endwin();
9378 b46c1e04 2020-09-20 naddy putchar('\n');
9379 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9380 a2f4a359 2020-02-28 stsp int i;
9381 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9382 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9383 a2f4a359 2020-02-28 stsp free(cmd_argv);
9384 a2f4a359 2020-02-28 stsp }
9385 a2f4a359 2020-02-28 stsp
9386 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9387 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9388 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9389 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9390 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9391 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9392 9f7d7167 2018-04-29 stsp return 0;
9393 9f7d7167 2018-04-29 stsp }