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 aa7a1117 2023-01-09 thomas KEY_("g", "Go to line N (default: first line)"), \
542 aa7a1117 2023-01-09 thomas KEY_("Home =", "Go to the first line"), \
543 aa7a1117 2023-01-09 thomas KEY_("G", "Go to line N (default: last line)"), \
544 aa7a1117 2023-01-09 thomas KEY_("End *", "Go to the last line"), \
545 fc2737d5 2022-09-15 thomas KEY_("l Right", "Scroll the view right"), \
546 fc2737d5 2022-09-15 thomas KEY_("h Left", "Scroll the view left"), \
547 fc2737d5 2022-09-15 thomas KEY_("$", "Scroll view to the rightmost position"), \
548 fc2737d5 2022-09-15 thomas KEY_("0", "Scroll view to the leftmost position"), \
549 fc2737d5 2022-09-15 thomas KEY_("-", "Decrease size of the focussed split"), \
550 fc2737d5 2022-09-15 thomas KEY_("+", "Increase size of the focussed split"), \
551 fc2737d5 2022-09-15 thomas KEY_("Tab", "Switch focus between views"), \
552 fc2737d5 2022-09-15 thomas KEY_("F", "Toggle fullscreen mode"), \
553 fc2737d5 2022-09-15 thomas KEY_("/", "Open prompt to enter search term"), \
554 fc2737d5 2022-09-15 thomas KEY_("n", "Find next line/token matching the current search term"), \
555 fc2737d5 2022-09-15 thomas KEY_("N", "Find previous line/token matching the current search term"),\
556 06ff88bb 2022-09-19 thomas KEY_("q", "Quit the focussed view; Quit help screen"), \
557 fc2737d5 2022-09-15 thomas KEY_("Q", "Quit tog"), \
558 fc2737d5 2022-09-15 thomas \
559 d0d3e7a4 2022-09-23 thomas KEYMAP_("Log view", TOG_KEYMAP_LOG), \
560 fc2737d5 2022-09-15 thomas KEY_("< ,", "Move cursor up one commit"), \
561 fc2737d5 2022-09-15 thomas KEY_("> .", "Move cursor down one commit"), \
562 fc2737d5 2022-09-15 thomas KEY_("Enter", "Open diff view of the selected commit"), \
563 fc2737d5 2022-09-15 thomas KEY_("B", "Reload the log view and toggle display of merged commits"), \
564 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
565 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the repository from the selected" \
566 fc2737d5 2022-09-15 thomas " commit"), \
567 fc2737d5 2022-09-15 thomas KEY_("@", "Toggle between displaying author and committer name"), \
568 fc2737d5 2022-09-15 thomas KEY_("&", "Open prompt to enter term to limit commits displayed"), \
569 fc2737d5 2022-09-15 thomas KEY_("C-g Backspace", "Cancel current search or log operation"), \
570 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload the log view with new commits in the repository"), \
571 fc2737d5 2022-09-15 thomas \
572 d0d3e7a4 2022-09-23 thomas KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
573 fc2737d5 2022-09-15 thomas KEY_("K < ,", "Display diff of next line in the file/log entry"), \
574 fc2737d5 2022-09-15 thomas KEY_("J > .", "Display diff of previous line in the file/log entry"), \
575 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
576 fc2737d5 2022-09-15 thomas KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
577 fc2737d5 2022-09-15 thomas " data"), \
578 fc2737d5 2022-09-15 thomas KEY_("(", "Go to the previous file in the diff"), \
579 fc2737d5 2022-09-15 thomas KEY_(")", "Go to the next file in the diff"), \
580 fc2737d5 2022-09-15 thomas KEY_("{", "Go to the previous hunk in the diff"), \
581 fc2737d5 2022-09-15 thomas KEY_("}", "Go to the next hunk in the diff"), \
582 fc2737d5 2022-09-15 thomas KEY_("[", "Decrease the number of context lines"), \
583 fc2737d5 2022-09-15 thomas KEY_("]", "Increase the number of context lines"), \
584 fc2737d5 2022-09-15 thomas KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
585 fc2737d5 2022-09-15 thomas \
586 d0d3e7a4 2022-09-23 thomas KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
587 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display diff view of the selected line's commit"), \
588 fc2737d5 2022-09-15 thomas KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
589 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the currently selected annotated line"), \
590 fc2737d5 2022-09-15 thomas KEY_("C", "Reload view with the previously blamed commit"), \
591 fc2737d5 2022-09-15 thomas KEY_("c", "Reload view with the version of the file found in the" \
592 fc2737d5 2022-09-15 thomas " selected line's commit"), \
593 fc2737d5 2022-09-15 thomas KEY_("p", "Reload view with the version of the file found in the" \
594 fc2737d5 2022-09-15 thomas " selected line's parent commit"), \
595 fc2737d5 2022-09-15 thomas \
596 d0d3e7a4 2022-09-23 thomas KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
597 fc2737d5 2022-09-15 thomas KEY_("Enter", "Enter selected directory or open blame view of the" \
598 fc2737d5 2022-09-15 thomas " selected file"), \
599 fc2737d5 2022-09-15 thomas KEY_("L", "Open log view for the selected entry"), \
600 fc2737d5 2022-09-15 thomas KEY_("R", "Open ref view of all repository references"), \
601 fc2737d5 2022-09-15 thomas KEY_("i", "Show object IDs for all tree entries"), \
602 fc2737d5 2022-09-15 thomas KEY_("Backspace", "Return to the parent directory"), \
603 fc2737d5 2022-09-15 thomas \
604 d0d3e7a4 2022-09-23 thomas KEYMAP_("Ref view", TOG_KEYMAP_REF), \
605 fc2737d5 2022-09-15 thomas KEY_("Enter", "Display log view of the selected reference"), \
606 fc2737d5 2022-09-15 thomas KEY_("T", "Display tree view of the selected reference"), \
607 fc2737d5 2022-09-15 thomas KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
608 fc2737d5 2022-09-15 thomas KEY_("m", "Toggle display of last modified date for each reference"), \
609 fc2737d5 2022-09-15 thomas KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
610 fc2737d5 2022-09-15 thomas KEY_("C-l", "Reload view with all repository references")
611 fc2737d5 2022-09-15 thomas
612 fc2737d5 2022-09-15 thomas struct tog_key_map {
613 fc2737d5 2022-09-15 thomas const char *keys;
614 fc2737d5 2022-09-15 thomas const char *info;
615 fc2737d5 2022-09-15 thomas enum tog_keymap_type type;
616 7cbe629d 2018-08-04 stsp };
617 7cbe629d 2018-08-04 stsp
618 669b5ffa 2018-10-07 stsp /*
619 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
620 669b5ffa 2018-10-07 stsp *
621 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
622 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
623 669b5ffa 2018-10-07 stsp * there is enough screen estate.
624 669b5ffa 2018-10-07 stsp *
625 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
626 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
627 669b5ffa 2018-10-07 stsp *
628 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
629 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
630 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
631 669b5ffa 2018-10-07 stsp *
632 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
633 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
634 669b5ffa 2018-10-07 stsp */
635 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
636 669b5ffa 2018-10-07 stsp
637 cc3c9aac 2018-08-01 stsp struct tog_view {
638 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
639 26ed57b2 2018-05-19 stsp WINDOW *window;
640 26ed57b2 2018-05-19 stsp PANEL *panel;
641 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
642 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
643 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
644 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
645 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
646 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
647 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
648 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
649 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
650 9970f7fc 2020-12-03 stsp int dying;
651 669b5ffa 2018-10-07 stsp struct tog_view *parent;
652 669b5ffa 2018-10-07 stsp struct tog_view *child;
653 5dc9f4bc 2018-08-04 stsp
654 e78dc838 2020-12-04 stsp /*
655 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
656 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
657 e78dc838 2020-12-04 stsp * between parent and child.
658 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
659 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
660 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
661 e78dc838 2020-12-04 stsp * situations.
662 e78dc838 2020-12-04 stsp */
663 e78dc838 2020-12-04 stsp int focus_child;
664 e78dc838 2020-12-04 stsp
665 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
666 5dc9f4bc 2018-08-04 stsp /* type-specific state */
667 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
668 5dc9f4bc 2018-08-04 stsp union {
669 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
670 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
671 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
672 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
673 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
674 fc2737d5 2022-09-15 thomas struct tog_help_view_state help;
675 5dc9f4bc 2018-08-04 stsp } state;
676 e5a0f69f 2018-08-18 stsp
677 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
678 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
679 e78dc838 2020-12-04 stsp struct tog_view *, int);
680 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
681 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
682 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
683 60493ae3 2019-06-20 stsp
684 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
685 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
686 2a7d3cd7 2022-09-17 thomas void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
687 2a7d3cd7 2022-09-17 thomas int **, int **, int **, int **);
688 c0c4acc8 2021-01-24 stsp int search_started;
689 60493ae3 2019-06-20 stsp int searching;
690 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
691 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
692 60493ae3 2019-06-20 stsp int search_next_done;
693 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
694 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
695 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
696 1803e47f 2019-06-22 stsp regex_t regex;
697 41605754 2020-11-12 stsp regmatch_t regmatch;
698 f2d06bef 2023-01-23 thomas const char *action;
699 cc3c9aac 2018-08-01 stsp };
700 cd0acaa7 2018-05-20 stsp
701 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
702 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
703 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
704 78756c87 2020-11-24 stsp struct got_repository *);
705 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
706 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
707 e78dc838 2020-12-04 stsp struct tog_view *, int);
708 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
709 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
710 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
711 2a7d3cd7 2022-09-17 thomas static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
712 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
713 fc2737d5 2022-09-15 thomas static const struct got_error *search_next_view_match(struct tog_view *);
714 e5a0f69f 2018-08-18 stsp
715 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
716 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
717 78756c87 2020-11-24 stsp const char *, const char *, int);
718 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
719 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
720 e78dc838 2020-12-04 stsp struct tog_view *, int);
721 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
722 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
723 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
724 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
725 e5a0f69f 2018-08-18 stsp
726 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
727 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
728 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
729 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
730 e78dc838 2020-12-04 stsp struct tog_view *, int);
731 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
732 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
733 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
734 2a7d3cd7 2022-09-17 thomas static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
735 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
736 e5a0f69f 2018-08-18 stsp
737 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
738 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
739 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
740 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
741 e78dc838 2020-12-04 stsp struct tog_view *, int);
742 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
743 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
744 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
745 6458efa5 2020-11-24 stsp
746 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
747 6458efa5 2020-11-24 stsp struct got_repository *);
748 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
749 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
750 e78dc838 2020-12-04 stsp struct tog_view *, int);
751 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
752 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
753 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
754 2a7d3cd7 2022-09-17 thomas
755 2a7d3cd7 2022-09-17 thomas static const struct got_error *open_help_view(struct tog_view *,
756 2a7d3cd7 2022-09-17 thomas struct tog_view *);
757 2a7d3cd7 2022-09-17 thomas static const struct got_error *show_help_view(struct tog_view *);
758 2a7d3cd7 2022-09-17 thomas static const struct got_error *input_help_view(struct tog_view **,
759 2a7d3cd7 2022-09-17 thomas struct tog_view *, int);
760 2a7d3cd7 2022-09-17 thomas static const struct got_error *reset_help_view(struct tog_view *);
761 2a7d3cd7 2022-09-17 thomas static const struct got_error* close_help_view(struct tog_view *);
762 2a7d3cd7 2022-09-17 thomas static const struct got_error *search_start_help_view(struct tog_view *);
763 2a7d3cd7 2022-09-17 thomas static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
764 2a7d3cd7 2022-09-17 thomas size_t *, int **, int **, int **, int **);
765 25791caa 2018-10-24 stsp
766 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
767 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
768 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
769 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
770 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
771 25791caa 2018-10-24 stsp
772 25791caa 2018-10-24 stsp static void
773 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
774 25791caa 2018-10-24 stsp {
775 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
776 83baff54 2019-08-12 stsp }
777 83baff54 2019-08-12 stsp
778 83baff54 2019-08-12 stsp static void
779 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
780 83baff54 2019-08-12 stsp {
781 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
782 25791caa 2018-10-24 stsp }
783 26ed57b2 2018-05-19 stsp
784 61266923 2020-01-14 stsp static void
785 61266923 2020-01-14 stsp tog_sigcont(int signo)
786 61266923 2020-01-14 stsp {
787 61266923 2020-01-14 stsp tog_sigcont_received = 1;
788 61266923 2020-01-14 stsp }
789 61266923 2020-01-14 stsp
790 296152d1 2022-05-31 thomas static void
791 296152d1 2022-05-31 thomas tog_sigint(int signo)
792 296152d1 2022-05-31 thomas {
793 296152d1 2022-05-31 thomas tog_sigint_received = 1;
794 296152d1 2022-05-31 thomas }
795 296152d1 2022-05-31 thomas
796 296152d1 2022-05-31 thomas static void
797 296152d1 2022-05-31 thomas tog_sigterm(int signo)
798 296152d1 2022-05-31 thomas {
799 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
800 296152d1 2022-05-31 thomas }
801 296152d1 2022-05-31 thomas
802 296152d1 2022-05-31 thomas static int
803 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
804 296152d1 2022-05-31 thomas {
805 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
806 47cc6e77 2022-10-24 thomas tog_sigint_received || tog_sigterm_received);
807 296152d1 2022-05-31 thomas }
808 296152d1 2022-05-31 thomas
809 e5a0f69f 2018-08-18 stsp static const struct got_error *
810 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
811 ea5e7bb5 2018-08-01 stsp {
812 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
813 e5a0f69f 2018-08-18 stsp
814 669b5ffa 2018-10-07 stsp if (view->child) {
815 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
816 669b5ffa 2018-10-07 stsp view->child = NULL;
817 669b5ffa 2018-10-07 stsp }
818 e5a0f69f 2018-08-18 stsp if (view->close)
819 e5a0f69f 2018-08-18 stsp err = view->close(view);
820 ea5e7bb5 2018-08-01 stsp if (view->panel)
821 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
822 ea5e7bb5 2018-08-01 stsp if (view->window)
823 ea5e7bb5 2018-08-01 stsp delwin(view->window);
824 ea5e7bb5 2018-08-01 stsp free(view);
825 f2d749db 2022-07-12 thomas return err ? err : child_err;
826 ea5e7bb5 2018-08-01 stsp }
827 ea5e7bb5 2018-08-01 stsp
828 ea5e7bb5 2018-08-01 stsp static struct tog_view *
829 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
830 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
831 ea5e7bb5 2018-08-01 stsp {
832 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
833 ea5e7bb5 2018-08-01 stsp
834 ea5e7bb5 2018-08-01 stsp if (view == NULL)
835 ea5e7bb5 2018-08-01 stsp return NULL;
836 ea5e7bb5 2018-08-01 stsp
837 d6b05b5b 2018-08-04 stsp view->type = type;
838 f7d12f7e 2018-08-01 stsp view->lines = LINES;
839 f7d12f7e 2018-08-01 stsp view->cols = COLS;
840 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
841 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
842 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
843 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
844 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
845 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
846 96a765a8 2018-08-04 stsp view_close(view);
847 ea5e7bb5 2018-08-01 stsp return NULL;
848 ea5e7bb5 2018-08-01 stsp }
849 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
850 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
851 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
852 96a765a8 2018-08-04 stsp view_close(view);
853 ea5e7bb5 2018-08-01 stsp return NULL;
854 ea5e7bb5 2018-08-01 stsp }
855 ea5e7bb5 2018-08-01 stsp
856 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
857 ea5e7bb5 2018-08-01 stsp return view;
858 cdf1ee82 2018-08-01 stsp }
859 cdf1ee82 2018-08-01 stsp
860 0cf4efb1 2018-09-29 stsp static int
861 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
862 0cf4efb1 2018-09-29 stsp {
863 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
864 0cf4efb1 2018-09-29 stsp return 0;
865 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
866 5c60c32a 2018-10-18 stsp }
867 5c60c32a 2018-10-18 stsp
868 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
869 a5d43cac 2022-07-01 thomas static int
870 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
871 a5d43cac 2022-07-01 thomas {
872 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
873 a5d43cac 2022-07-01 thomas }
874 a5d43cac 2022-07-01 thomas
875 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
876 5c60c32a 2018-10-18 stsp
877 5c60c32a 2018-10-18 stsp static const struct got_error *
878 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
879 5c60c32a 2018-10-18 stsp {
880 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
881 5c60c32a 2018-10-18 stsp
882 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
883 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
884 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
885 53d2bdd3 2022-07-10 thomas else
886 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
887 a5d43cac 2022-07-01 thomas view->begin_x = 0;
888 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
889 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
890 53d2bdd3 2022-07-10 thomas view->cols > 119)
891 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
892 53d2bdd3 2022-07-10 thomas else
893 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
894 a5d43cac 2022-07-01 thomas view->begin_y = 0;
895 a5d43cac 2022-07-01 thomas }
896 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
897 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
898 5c60c32a 2018-10-18 stsp view->lines = LINES;
899 5c60c32a 2018-10-18 stsp view->cols = COLS;
900 5c60c32a 2018-10-18 stsp err = view_resize(view);
901 5c60c32a 2018-10-18 stsp if (err)
902 5c60c32a 2018-10-18 stsp return err;
903 5c60c32a 2018-10-18 stsp
904 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
905 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
906 a5d43cac 2022-07-01 thomas
907 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
908 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
909 5c60c32a 2018-10-18 stsp
910 5c60c32a 2018-10-18 stsp return NULL;
911 5c60c32a 2018-10-18 stsp }
912 5c60c32a 2018-10-18 stsp
913 5c60c32a 2018-10-18 stsp static const struct got_error *
914 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
915 5c60c32a 2018-10-18 stsp {
916 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
917 5c60c32a 2018-10-18 stsp
918 5c60c32a 2018-10-18 stsp view->begin_x = 0;
919 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
920 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
921 5c60c32a 2018-10-18 stsp view->ncols = COLS;
922 5c60c32a 2018-10-18 stsp view->lines = LINES;
923 5c60c32a 2018-10-18 stsp view->cols = COLS;
924 5c60c32a 2018-10-18 stsp err = view_resize(view);
925 5c60c32a 2018-10-18 stsp if (err)
926 5c60c32a 2018-10-18 stsp return err;
927 5c60c32a 2018-10-18 stsp
928 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
929 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
930 5c60c32a 2018-10-18 stsp
931 5c60c32a 2018-10-18 stsp return NULL;
932 0cf4efb1 2018-09-29 stsp }
933 0cf4efb1 2018-09-29 stsp
934 5c60c32a 2018-10-18 stsp static int
935 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
936 5c60c32a 2018-10-18 stsp {
937 5c60c32a 2018-10-18 stsp return view->parent == NULL;
938 5c60c32a 2018-10-18 stsp }
939 5c60c32a 2018-10-18 stsp
940 b65b3ea0 2022-06-23 thomas static int
941 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
942 b65b3ea0 2022-06-23 thomas {
943 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
944 e44940c3 2022-07-01 thomas }
945 e44940c3 2022-07-01 thomas
946 e44940c3 2022-07-01 thomas static int
947 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
948 e44940c3 2022-07-01 thomas {
949 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
950 b65b3ea0 2022-06-23 thomas }
951 b65b3ea0 2022-06-23 thomas
952 444d5325 2022-07-03 thomas static int
953 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
954 444d5325 2022-07-03 thomas {
955 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
956 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
957 444d5325 2022-07-03 thomas }
958 444d5325 2022-07-03 thomas
959 a5d43cac 2022-07-01 thomas static void
960 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
961 a5d43cac 2022-07-01 thomas {
962 a5d43cac 2022-07-01 thomas PANEL *panel;
963 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
964 b65b3ea0 2022-06-23 thomas
965 a5d43cac 2022-07-01 thomas if (view->parent)
966 a5d43cac 2022-07-01 thomas return view_border(view->parent);
967 a5d43cac 2022-07-01 thomas
968 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
969 a5d43cac 2022-07-01 thomas if (panel == NULL)
970 a5d43cac 2022-07-01 thomas return;
971 a5d43cac 2022-07-01 thomas
972 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
973 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
974 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
975 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
976 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
977 a5d43cac 2022-07-01 thomas else
978 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
979 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
980 a5d43cac 2022-07-01 thomas }
981 a5d43cac 2022-07-01 thomas
982 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
983 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
984 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
985 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
986 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
987 a5d43cac 2022-07-01 thomas
988 4d8c2215 2018-08-19 stsp static const struct got_error *
989 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
990 f7d12f7e 2018-08-01 stsp {
991 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
992 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
993 f7d12f7e 2018-08-01 stsp
994 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
995 a5d43cac 2022-07-01 thomas
996 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
997 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
998 0cf4efb1 2018-09-29 stsp else
999 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
1000 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
1001 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
1002 0cf4efb1 2018-09-29 stsp else
1003 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
1004 6d0fee91 2018-08-01 stsp
1005 4918811f 2022-07-01 thomas if (view->child) {
1006 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
1007 a5d43cac 2022-07-01 thomas
1008 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
1009 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
1010 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1011 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
1012 40236d76 2022-06-23 thomas ncols = COLS;
1013 40236d76 2022-06-23 thomas
1014 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
1015 5c60c32a 2018-10-18 stsp if (view->child->focussed)
1016 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1017 5c60c32a 2018-10-18 stsp else
1018 5c60c32a 2018-10-18 stsp show_panel(view->panel);
1019 5c60c32a 2018-10-18 stsp } else {
1020 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
1021 40236d76 2022-06-23 thomas
1022 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
1023 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
1024 a5d43cac 2022-07-01 thomas }
1025 a5d43cac 2022-07-01 thomas /*
1026 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
1027 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
1028 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
1029 a5d43cac 2022-07-01 thomas */
1030 a5d43cac 2022-07-01 thomas if (hs) {
1031 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
1032 a5d43cac 2022-07-01 thomas if (err)
1033 a5d43cac 2022-07-01 thomas return err;
1034 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
1035 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1036 a5d43cac 2022-07-01 thomas if (err)
1037 a5d43cac 2022-07-01 thomas return err;
1038 a5d43cac 2022-07-01 thomas }
1039 a5d43cac 2022-07-01 thomas view_border(view);
1040 a5d43cac 2022-07-01 thomas update_panels();
1041 a5d43cac 2022-07-01 thomas doupdate();
1042 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
1043 a5d43cac 2022-07-01 thomas nlines = view->nlines;
1044 a5d43cac 2022-07-01 thomas }
1045 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
1046 40236d76 2022-06-23 thomas ncols = COLS;
1047 669b5ffa 2018-10-07 stsp
1048 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
1049 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
1050 ea0bff04 2022-07-19 thomas if (err)
1051 ea0bff04 2022-07-19 thomas return err;
1052 ea0bff04 2022-07-19 thomas }
1053 ea0bff04 2022-07-19 thomas
1054 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
1055 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
1056 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
1057 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
1058 40236d76 2022-06-23 thomas wclear(view->window);
1059 40236d76 2022-06-23 thomas
1060 40236d76 2022-06-23 thomas view->nlines = nlines;
1061 40236d76 2022-06-23 thomas view->ncols = ncols;
1062 40236d76 2022-06-23 thomas view->lines = LINES;
1063 40236d76 2022-06-23 thomas view->cols = COLS;
1064 40236d76 2022-06-23 thomas
1065 5c60c32a 2018-10-18 stsp return NULL;
1066 ea0bff04 2022-07-19 thomas }
1067 ea0bff04 2022-07-19 thomas
1068 ea0bff04 2022-07-19 thomas static const struct got_error *
1069 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
1070 ea0bff04 2022-07-19 thomas {
1071 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
1072 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
1073 3a0139e8 2022-07-22 thomas int n = 0;
1074 ea0bff04 2022-07-19 thomas
1075 3a0139e8 2022-07-22 thomas if (s->selected_entry)
1076 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
1077 3a0139e8 2022-07-22 thomas
1078 ea0bff04 2022-07-19 thomas /*
1079 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
1080 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
1081 ea0bff04 2022-07-19 thomas */
1082 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
1083 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
1084 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
1085 ea0bff04 2022-07-19 thomas }
1086 ea0bff04 2022-07-19 thomas
1087 ea0bff04 2022-07-19 thomas return err;
1088 97cb21cd 2022-07-12 thomas }
1089 97cb21cd 2022-07-12 thomas
1090 97cb21cd 2022-07-12 thomas static void
1091 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
1092 97cb21cd 2022-07-12 thomas {
1093 97cb21cd 2022-07-12 thomas if (n == 0)
1094 97cb21cd 2022-07-12 thomas return;
1095 97cb21cd 2022-07-12 thomas
1096 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
1097 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
1098 97cb21cd 2022-07-12 thomas view->parent->offset += n;
1099 97cb21cd 2022-07-12 thomas else
1100 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
1101 97cb21cd 2022-07-12 thomas } else if (view->offset) {
1102 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
1103 97cb21cd 2022-07-12 thomas view->offset -= n;
1104 97cb21cd 2022-07-12 thomas else
1105 97cb21cd 2022-07-12 thomas view->offset = 0;
1106 97cb21cd 2022-07-12 thomas }
1107 53d2bdd3 2022-07-10 thomas }
1108 53d2bdd3 2022-07-10 thomas
1109 53d2bdd3 2022-07-10 thomas static const struct got_error *
1110 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
1111 53d2bdd3 2022-07-10 thomas {
1112 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1113 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
1114 53d2bdd3 2022-07-10 thomas
1115 53d2bdd3 2022-07-10 thomas if (view->parent)
1116 53d2bdd3 2022-07-10 thomas v = view->parent;
1117 53d2bdd3 2022-07-10 thomas else
1118 53d2bdd3 2022-07-10 thomas v = view;
1119 53d2bdd3 2022-07-10 thomas
1120 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
1121 53d2bdd3 2022-07-10 thomas return NULL;
1122 53d2bdd3 2022-07-10 thomas
1123 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
1124 53d2bdd3 2022-07-10 thomas
1125 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1126 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
1127 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1128 53d2bdd3 2022-07-10 thomas if (view->parent)
1129 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1130 53d2bdd3 2022-07-10 thomas else
1131 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1132 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1133 53d2bdd3 2022-07-10 thomas view->count = 0;
1134 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1135 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1136 53d2bdd3 2022-07-10 thomas view->count = 0;
1137 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1138 53d2bdd3 2022-07-10 thomas }
1139 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1140 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1141 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1142 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1143 53d2bdd3 2022-07-10 thomas if (err)
1144 53d2bdd3 2022-07-10 thomas return err;
1145 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1146 53d2bdd3 2022-07-10 thomas } else {
1147 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1148 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1149 53d2bdd3 2022-07-10 thomas if (view->parent)
1150 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1151 53d2bdd3 2022-07-10 thomas else
1152 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1153 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1154 53d2bdd3 2022-07-10 thomas view->count = 0;
1155 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1156 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1157 53d2bdd3 2022-07-10 thomas view->count = 0;
1158 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1159 53d2bdd3 2022-07-10 thomas }
1160 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1161 53d2bdd3 2022-07-10 thomas }
1162 53d2bdd3 2022-07-10 thomas
1163 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1164 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1165 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1166 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1167 53d2bdd3 2022-07-10 thomas
1168 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1169 53d2bdd3 2022-07-10 thomas if (err)
1170 53d2bdd3 2022-07-10 thomas return err;
1171 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1172 53d2bdd3 2022-07-10 thomas if (err)
1173 53d2bdd3 2022-07-10 thomas return err;
1174 53d2bdd3 2022-07-10 thomas
1175 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1176 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1177 53d2bdd3 2022-07-10 thomas if (err)
1178 53d2bdd3 2022-07-10 thomas return err;
1179 53d2bdd3 2022-07-10 thomas }
1180 53d2bdd3 2022-07-10 thomas
1181 3a0139e8 2022-07-22 thomas if (v->resize)
1182 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1183 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1184 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1185 53d2bdd3 2022-07-10 thomas
1186 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1187 53d2bdd3 2022-07-10 thomas
1188 53d2bdd3 2022-07-10 thomas return err;
1189 53d2bdd3 2022-07-10 thomas }
1190 53d2bdd3 2022-07-10 thomas
1191 53d2bdd3 2022-07-10 thomas static void
1192 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1193 53d2bdd3 2022-07-10 thomas {
1194 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1195 53d2bdd3 2022-07-10 thomas
1196 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1197 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1198 669b5ffa 2018-10-07 stsp }
1199 669b5ffa 2018-10-07 stsp
1200 669b5ffa 2018-10-07 stsp static const struct got_error *
1201 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1202 669b5ffa 2018-10-07 stsp {
1203 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1204 669b5ffa 2018-10-07 stsp
1205 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1206 669b5ffa 2018-10-07 stsp return NULL;
1207 669b5ffa 2018-10-07 stsp
1208 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1209 669b5ffa 2018-10-07 stsp view->child = NULL;
1210 669b5ffa 2018-10-07 stsp return err;
1211 669b5ffa 2018-10-07 stsp }
1212 669b5ffa 2018-10-07 stsp
1213 40236d76 2022-06-23 thomas static const struct got_error *
1214 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1215 669b5ffa 2018-10-07 stsp {
1216 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1217 53d2bdd3 2022-07-10 thomas
1218 669b5ffa 2018-10-07 stsp view->child = child;
1219 669b5ffa 2018-10-07 stsp child->parent = view;
1220 40236d76 2022-06-23 thomas
1221 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1222 53d2bdd3 2022-07-10 thomas if (err)
1223 53d2bdd3 2022-07-10 thomas return err;
1224 53d2bdd3 2022-07-10 thomas
1225 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1226 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1227 53d2bdd3 2022-07-10 thomas
1228 53d2bdd3 2022-07-10 thomas return err;
1229 bfddd0d9 2018-09-29 stsp }
1230 2a31b33b 2022-07-23 thomas
1231 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1232 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1233 2a31b33b 2022-07-23 thomas
1234 2a31b33b 2022-07-23 thomas static const struct got_error *
1235 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1236 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1237 2a31b33b 2022-07-23 thomas {
1238 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1239 2a31b33b 2022-07-23 thomas const struct got_error *err;
1240 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1241 2a31b33b 2022-07-23 thomas
1242 2a31b33b 2022-07-23 thomas *requested = NULL;
1243 2a31b33b 2022-07-23 thomas
1244 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1245 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1246 bfddd0d9 2018-09-29 stsp
1247 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1248 2a31b33b 2022-07-23 thomas if (err)
1249 2a31b33b 2022-07-23 thomas return err;
1250 2a31b33b 2022-07-23 thomas
1251 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1252 a7dd23ad 2022-09-19 thomas request != TOG_VIEW_HELP) {
1253 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1254 2a31b33b 2022-07-23 thomas if (err)
1255 2a31b33b 2022-07-23 thomas return err;
1256 2a31b33b 2022-07-23 thomas }
1257 2a31b33b 2022-07-23 thomas
1258 2a31b33b 2022-07-23 thomas view->focussed = 0;
1259 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1260 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1261 a7dd23ad 2022-09-19 thomas new_view->nlines = request == TOG_VIEW_HELP ?
1262 a7dd23ad 2022-09-19 thomas view->lines : view->lines - y;
1263 2a31b33b 2022-07-23 thomas
1264 a7dd23ad 2022-09-19 thomas if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1265 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1266 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1267 2a31b33b 2022-07-23 thomas if (err)
1268 2a31b33b 2022-07-23 thomas return err;
1269 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1270 2a31b33b 2022-07-23 thomas if (err)
1271 2a31b33b 2022-07-23 thomas return err;
1272 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1273 2a31b33b 2022-07-23 thomas } else
1274 2a31b33b 2022-07-23 thomas *requested = new_view;
1275 2a31b33b 2022-07-23 thomas
1276 2a31b33b 2022-07-23 thomas return NULL;
1277 2a31b33b 2022-07-23 thomas }
1278 2a31b33b 2022-07-23 thomas
1279 34bc9ec9 2019-02-22 stsp static void
1280 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1281 25791caa 2018-10-24 stsp {
1282 25791caa 2018-10-24 stsp int cols, lines;
1283 25791caa 2018-10-24 stsp struct winsize size;
1284 25791caa 2018-10-24 stsp
1285 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1286 25791caa 2018-10-24 stsp cols = 80; /* Default */
1287 25791caa 2018-10-24 stsp lines = 24;
1288 25791caa 2018-10-24 stsp } else {
1289 25791caa 2018-10-24 stsp cols = size.ws_col;
1290 25791caa 2018-10-24 stsp lines = size.ws_row;
1291 25791caa 2018-10-24 stsp }
1292 25791caa 2018-10-24 stsp resize_term(lines, cols);
1293 2b49a8ae 2019-06-22 stsp }
1294 2b49a8ae 2019-06-22 stsp
1295 2b49a8ae 2019-06-22 stsp static const struct got_error *
1296 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1297 2b49a8ae 2019-06-22 stsp {
1298 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1299 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1300 2b49a8ae 2019-06-22 stsp char pattern[1024];
1301 2b49a8ae 2019-06-22 stsp int ret;
1302 c0c4acc8 2021-01-24 stsp
1303 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1304 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1305 c0c4acc8 2021-01-24 stsp view->searching = 0;
1306 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1307 c0c4acc8 2021-01-24 stsp }
1308 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1309 2b49a8ae 2019-06-22 stsp
1310 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1311 2b49a8ae 2019-06-22 stsp return NULL;
1312 2b49a8ae 2019-06-22 stsp
1313 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1314 a5d43cac 2022-07-01 thomas v = view->child;
1315 d07291c6 2022-12-30 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1316 d07291c6 2022-12-30 thomas v = view->parent;
1317 2b49a8ae 2019-06-22 stsp
1318 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1319 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1320 a5d43cac 2022-07-01 thomas
1321 85fbc360 2022-12-30 thomas nodelay(v->window, FALSE); /* block for search term input */
1322 2b49a8ae 2019-06-22 stsp nocbreak();
1323 2b49a8ae 2019-06-22 stsp echo();
1324 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1325 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1326 2b49a8ae 2019-06-22 stsp cbreak();
1327 2b49a8ae 2019-06-22 stsp noecho();
1328 85fbc360 2022-12-30 thomas nodelay(v->window, TRUE);
1329 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1330 2b49a8ae 2019-06-22 stsp return NULL;
1331 2b49a8ae 2019-06-22 stsp
1332 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1333 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1334 7c32bd05 2019-06-22 stsp if (err) {
1335 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1336 7c32bd05 2019-06-22 stsp return err;
1337 7c32bd05 2019-06-22 stsp }
1338 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1339 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1340 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1341 2b49a8ae 2019-06-22 stsp view->search_next(view);
1342 2b49a8ae 2019-06-22 stsp }
1343 2b49a8ae 2019-06-22 stsp
1344 2b49a8ae 2019-06-22 stsp return NULL;
1345 64486692 2022-07-07 thomas }
1346 64486692 2022-07-07 thomas
1347 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1348 64486692 2022-07-07 thomas static const struct got_error *
1349 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1350 64486692 2022-07-07 thomas {
1351 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1352 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1353 64486692 2022-07-07 thomas
1354 64486692 2022-07-07 thomas if (view->parent)
1355 64486692 2022-07-07 thomas v = view->parent;
1356 64486692 2022-07-07 thomas else
1357 64486692 2022-07-07 thomas v = view;
1358 64486692 2022-07-07 thomas
1359 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1360 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1361 ddbc4d37 2022-07-12 thomas else
1362 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1363 64486692 2022-07-07 thomas
1364 ddbc4d37 2022-07-12 thomas if (!v->child)
1365 ddbc4d37 2022-07-12 thomas return NULL;
1366 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1367 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1368 ddbc4d37 2022-07-12 thomas
1369 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1370 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1371 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1372 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1373 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1374 64486692 2022-07-07 thomas
1375 ddbc4d37 2022-07-12 thomas
1376 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1377 64486692 2022-07-07 thomas v->ncols = COLS;
1378 64486692 2022-07-07 thomas v->child->ncols = COLS;
1379 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1380 64486692 2022-07-07 thomas
1381 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1382 64486692 2022-07-07 thomas if (err)
1383 64486692 2022-07-07 thomas return err;
1384 64486692 2022-07-07 thomas }
1385 64486692 2022-07-07 thomas v->child->mode = v->mode;
1386 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1387 64486692 2022-07-07 thomas v->focus_child = 1;
1388 64486692 2022-07-07 thomas
1389 64486692 2022-07-07 thomas err = view_fullscreen(v);
1390 64486692 2022-07-07 thomas if (err)
1391 64486692 2022-07-07 thomas return err;
1392 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1393 64486692 2022-07-07 thomas if (err)
1394 64486692 2022-07-07 thomas return err;
1395 64486692 2022-07-07 thomas
1396 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1397 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1398 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1399 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1400 cf1fe301 2022-07-29 thomas if (err)
1401 cf1fe301 2022-07-29 thomas return err;
1402 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1403 cf1fe301 2022-07-29 thomas if (err)
1404 cf1fe301 2022-07-29 thomas return err;
1405 ddbc4d37 2022-07-12 thomas } else {
1406 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1407 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1408 64486692 2022-07-07 thomas }
1409 f4e6231a 2022-07-22 thomas if (v->resize)
1410 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1411 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1412 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1413 64486692 2022-07-07 thomas
1414 64486692 2022-07-07 thomas return err;
1415 0cf4efb1 2018-09-29 stsp }
1416 6d0fee91 2018-08-01 stsp
1417 07b0611c 2022-06-23 thomas /*
1418 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1419 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1420 07b0611c 2022-06-23 thomas */
1421 07b0611c 2022-06-23 thomas static int
1422 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1423 07b0611c 2022-06-23 thomas {
1424 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1425 a5d43cac 2022-07-01 thomas int x, n = 0;
1426 07b0611c 2022-06-23 thomas
1427 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1428 a5d43cac 2022-07-01 thomas v = view->child;
1429 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1430 a5d43cac 2022-07-01 thomas v = view->parent;
1431 a5d43cac 2022-07-01 thomas
1432 07b0611c 2022-06-23 thomas view->count = 0;
1433 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1434 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1435 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1436 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1437 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1438 07b0611c 2022-06-23 thomas
1439 07b0611c 2022-06-23 thomas do {
1440 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1441 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1442 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1443 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1444 a5d43cac 2022-07-01 thomas }
1445 a5d43cac 2022-07-01 thomas
1446 07b0611c 2022-06-23 thomas /*
1447 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1448 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1449 07b0611c 2022-06-23 thomas */
1450 07b0611c 2022-06-23 thomas if (n >= 9999999)
1451 07b0611c 2022-06-23 thomas n = 9999999;
1452 07b0611c 2022-06-23 thomas else
1453 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1454 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1455 07b0611c 2022-06-23 thomas
1456 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1457 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1458 07dd3ed3 2022-08-06 thomas n = 0;
1459 07dd3ed3 2022-08-06 thomas c = 0;
1460 07dd3ed3 2022-08-06 thomas }
1461 07dd3ed3 2022-08-06 thomas
1462 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1463 07b0611c 2022-06-23 thomas view->count = n;
1464 07b0611c 2022-06-23 thomas
1465 07b0611c 2022-06-23 thomas return c;
1466 f2d06bef 2023-01-23 thomas }
1467 f2d06bef 2023-01-23 thomas
1468 f2d06bef 2023-01-23 thomas static void
1469 f2d06bef 2023-01-23 thomas action_report(struct tog_view *view)
1470 f2d06bef 2023-01-23 thomas {
1471 f2d06bef 2023-01-23 thomas struct tog_view *v = view;
1472 f2d06bef 2023-01-23 thomas
1473 f2d06bef 2023-01-23 thomas if (view_is_hsplit_top(view))
1474 f2d06bef 2023-01-23 thomas v = view->child;
1475 f2d06bef 2023-01-23 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1476 f2d06bef 2023-01-23 thomas v = view->parent;
1477 f2d06bef 2023-01-23 thomas
1478 f2d06bef 2023-01-23 thomas wmove(v->window, v->nlines - 1, 0);
1479 f2d06bef 2023-01-23 thomas wclrtoeol(v->window);
1480 f2d06bef 2023-01-23 thomas wprintw(v->window, ":%s", view->action);
1481 f2d06bef 2023-01-23 thomas wrefresh(v->window);
1482 f2d06bef 2023-01-23 thomas
1483 f2d06bef 2023-01-23 thomas /*
1484 f2d06bef 2023-01-23 thomas * Clear action status report. Only clear in blame view
1485 f2d06bef 2023-01-23 thomas * once annotating is complete, otherwise it's too fast.
1486 f2d06bef 2023-01-23 thomas */
1487 f2d06bef 2023-01-23 thomas if (view->type == TOG_VIEW_BLAME) {
1488 f2d06bef 2023-01-23 thomas if (view->state.blame.blame_complete)
1489 f2d06bef 2023-01-23 thomas view->action = NULL;
1490 f2d06bef 2023-01-23 thomas } else
1491 f2d06bef 2023-01-23 thomas view->action = NULL;
1492 07b0611c 2022-06-23 thomas }
1493 07b0611c 2022-06-23 thomas
1494 0cf4efb1 2018-09-29 stsp static const struct got_error *
1495 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1496 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1497 e5a0f69f 2018-08-18 stsp {
1498 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1499 669b5ffa 2018-10-07 stsp struct tog_view *v;
1500 1a76625f 2018-10-22 stsp int ch, errcode;
1501 e5a0f69f 2018-08-18 stsp
1502 e5a0f69f 2018-08-18 stsp *new = NULL;
1503 f2d06bef 2023-01-23 thomas
1504 f2d06bef 2023-01-23 thomas if (view->action)
1505 f2d06bef 2023-01-23 thomas action_report(view);
1506 8f4ed634 2020-03-26 stsp
1507 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1508 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1509 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1510 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1511 07b0611c 2022-06-23 thomas view->count = 0;
1512 07b0611c 2022-06-23 thomas }
1513 e5a0f69f 2018-08-18 stsp
1514 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1515 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1516 82954512 2020-02-03 stsp if (errcode)
1517 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1518 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1519 3da8ef85 2021-09-21 stsp sched_yield();
1520 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1521 82954512 2020-02-03 stsp if (errcode)
1522 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1523 82954512 2020-02-03 stsp "pthread_mutex_lock");
1524 60493ae3 2019-06-20 stsp view->search_next(view);
1525 60493ae3 2019-06-20 stsp return NULL;
1526 60493ae3 2019-06-20 stsp }
1527 60493ae3 2019-06-20 stsp
1528 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1529 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1530 1a76625f 2018-10-22 stsp if (errcode)
1531 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1532 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1533 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1534 634cb454 2022-07-03 thomas cbreak();
1535 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1536 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1537 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1538 634cb454 2022-07-03 thomas view->count = 0;
1539 634cb454 2022-07-03 thomas else
1540 634cb454 2022-07-03 thomas ch = view->ch;
1541 634cb454 2022-07-03 thomas } else {
1542 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1543 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1544 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1545 07b0611c 2022-06-23 thomas }
1546 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1547 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1548 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1549 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1550 1a76625f 2018-10-22 stsp if (errcode)
1551 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1552 25791caa 2018-10-24 stsp
1553 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1554 25791caa 2018-10-24 stsp tog_resizeterm();
1555 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1556 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1557 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1558 25791caa 2018-10-24 stsp err = view_resize(v);
1559 25791caa 2018-10-24 stsp if (err)
1560 25791caa 2018-10-24 stsp return err;
1561 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1562 25791caa 2018-10-24 stsp if (err)
1563 25791caa 2018-10-24 stsp return err;
1564 cdfcfb03 2020-12-06 stsp if (v->child) {
1565 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1566 cdfcfb03 2020-12-06 stsp if (err)
1567 cdfcfb03 2020-12-06 stsp return err;
1568 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1569 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1570 cdfcfb03 2020-12-06 stsp if (err)
1571 cdfcfb03 2020-12-06 stsp return err;
1572 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1573 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1574 53d2bdd3 2022-07-10 thomas if (err)
1575 53d2bdd3 2022-07-10 thomas return err;
1576 53d2bdd3 2022-07-10 thomas }
1577 cdfcfb03 2020-12-06 stsp }
1578 25791caa 2018-10-24 stsp }
1579 25791caa 2018-10-24 stsp }
1580 25791caa 2018-10-24 stsp
1581 e5a0f69f 2018-08-18 stsp switch (ch) {
1582 fc2737d5 2022-09-15 thomas case '?':
1583 fc2737d5 2022-09-15 thomas case 'H':
1584 fc2737d5 2022-09-15 thomas case KEY_F(1):
1585 fc2737d5 2022-09-15 thomas if (view->type == TOG_VIEW_HELP)
1586 fc2737d5 2022-09-15 thomas err = view->reset(view);
1587 fc2737d5 2022-09-15 thomas else
1588 fc2737d5 2022-09-15 thomas err = view_request_new(new, view, TOG_VIEW_HELP);
1589 fc2737d5 2022-09-15 thomas break;
1590 1e37a5c2 2019-05-12 jcs case '\t':
1591 07b0611c 2022-06-23 thomas view->count = 0;
1592 1e37a5c2 2019-05-12 jcs if (view->child) {
1593 e78dc838 2020-12-04 stsp view->focussed = 0;
1594 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1595 e78dc838 2020-12-04 stsp view->focus_child = 1;
1596 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1597 e78dc838 2020-12-04 stsp view->focussed = 0;
1598 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1599 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1600 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1601 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1602 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1603 3a0139e8 2022-07-22 thomas 0);
1604 a5d43cac 2022-07-01 thomas if (err)
1605 a5d43cac 2022-07-01 thomas return err;
1606 a5d43cac 2022-07-01 thomas }
1607 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1608 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1609 a5d43cac 2022-07-01 thomas if (err)
1610 a5d43cac 2022-07-01 thomas return err;
1611 a5d43cac 2022-07-01 thomas }
1612 1e37a5c2 2019-05-12 jcs }
1613 1e37a5c2 2019-05-12 jcs break;
1614 1e37a5c2 2019-05-12 jcs case 'q':
1615 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1616 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1617 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1618 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1619 a5d43cac 2022-07-01 thomas if (err)
1620 a5d43cac 2022-07-01 thomas break;
1621 a5d43cac 2022-07-01 thomas }
1622 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1623 a5d43cac 2022-07-01 thomas }
1624 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1625 9970f7fc 2020-12-03 stsp view->dying = 1;
1626 1e37a5c2 2019-05-12 jcs break;
1627 1e37a5c2 2019-05-12 jcs case 'Q':
1628 1e37a5c2 2019-05-12 jcs *done = 1;
1629 1e37a5c2 2019-05-12 jcs break;
1630 1c5e5faa 2022-06-23 thomas case 'F':
1631 07b0611c 2022-06-23 thomas view->count = 0;
1632 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1633 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1634 1e37a5c2 2019-05-12 jcs break;
1635 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1636 e78dc838 2020-12-04 stsp view->focussed = 0;
1637 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1638 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1639 53d2bdd3 2022-07-10 thomas } else {
1640 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1641 53d2bdd3 2022-07-10 thomas if (!err)
1642 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1643 53d2bdd3 2022-07-10 thomas }
1644 1e37a5c2 2019-05-12 jcs if (err)
1645 1e37a5c2 2019-05-12 jcs break;
1646 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1647 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1648 1e37a5c2 2019-05-12 jcs } else {
1649 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1650 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1651 e78dc838 2020-12-04 stsp view->focussed = 1;
1652 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1653 1e37a5c2 2019-05-12 jcs } else {
1654 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1655 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1656 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1657 53d2bdd3 2022-07-10 thomas if (!err)
1658 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1659 669b5ffa 2018-10-07 stsp }
1660 1e37a5c2 2019-05-12 jcs if (err)
1661 1e37a5c2 2019-05-12 jcs break;
1662 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1663 a5d43cac 2022-07-01 thomas }
1664 a5d43cac 2022-07-01 thomas if (err)
1665 a5d43cac 2022-07-01 thomas break;
1666 3a0139e8 2022-07-22 thomas if (view->resize) {
1667 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1668 a5d43cac 2022-07-01 thomas if (err)
1669 a5d43cac 2022-07-01 thomas break;
1670 1e37a5c2 2019-05-12 jcs }
1671 a5d43cac 2022-07-01 thomas if (view->parent)
1672 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1673 a5d43cac 2022-07-01 thomas if (!err)
1674 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1675 1e37a5c2 2019-05-12 jcs break;
1676 64486692 2022-07-07 thomas case 'S':
1677 53d2bdd3 2022-07-10 thomas view->count = 0;
1678 64486692 2022-07-07 thomas err = switch_split(view);
1679 53d2bdd3 2022-07-10 thomas break;
1680 53d2bdd3 2022-07-10 thomas case '-':
1681 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1682 64486692 2022-07-07 thomas break;
1683 53d2bdd3 2022-07-10 thomas case '+':
1684 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1685 53d2bdd3 2022-07-10 thomas break;
1686 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1687 60493ae3 2019-06-20 stsp break;
1688 60493ae3 2019-06-20 stsp case '/':
1689 07b0611c 2022-06-23 thomas view->count = 0;
1690 60493ae3 2019-06-20 stsp if (view->search_start)
1691 2b49a8ae 2019-06-22 stsp view_search_start(view);
1692 60493ae3 2019-06-20 stsp else
1693 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1694 1e37a5c2 2019-05-12 jcs break;
1695 b1bf1435 2019-06-21 stsp case 'N':
1696 60493ae3 2019-06-20 stsp case 'n':
1697 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1698 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1699 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1700 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1701 60493ae3 2019-06-20 stsp view->search_next(view);
1702 60493ae3 2019-06-20 stsp } else
1703 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1704 adf4c9e0 2022-07-03 thomas break;
1705 adf4c9e0 2022-07-03 thomas case 'A':
1706 f2d06bef 2023-01-23 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS) {
1707 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1708 f2d06bef 2023-01-23 thomas view->action = "Patience diff algorithm";
1709 f2d06bef 2023-01-23 thomas } else {
1710 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1711 f2d06bef 2023-01-23 thomas view->action = "Myers diff algorithm";
1712 f2d06bef 2023-01-23 thomas }
1713 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1714 adf4c9e0 2022-07-03 thomas if (v->reset) {
1715 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1716 adf4c9e0 2022-07-03 thomas if (err)
1717 adf4c9e0 2022-07-03 thomas return err;
1718 adf4c9e0 2022-07-03 thomas }
1719 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1720 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1721 adf4c9e0 2022-07-03 thomas if (err)
1722 adf4c9e0 2022-07-03 thomas return err;
1723 adf4c9e0 2022-07-03 thomas }
1724 adf4c9e0 2022-07-03 thomas }
1725 60493ae3 2019-06-20 stsp break;
1726 1e37a5c2 2019-05-12 jcs default:
1727 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1728 1e37a5c2 2019-05-12 jcs break;
1729 e5a0f69f 2018-08-18 stsp }
1730 e5a0f69f 2018-08-18 stsp
1731 e5a0f69f 2018-08-18 stsp return err;
1732 bcbd79e2 2018-08-19 stsp }
1733 bcbd79e2 2018-08-19 stsp
1734 ef20f542 2022-06-26 thomas static int
1735 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1736 a3404814 2018-09-02 stsp {
1737 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1738 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1739 669b5ffa 2018-10-07 stsp return 0;
1740 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1741 669b5ffa 2018-10-07 stsp return 0;
1742 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1743 a3404814 2018-09-02 stsp return 0;
1744 a3404814 2018-09-02 stsp
1745 669b5ffa 2018-10-07 stsp return view->focussed;
1746 a3404814 2018-09-02 stsp }
1747 a3404814 2018-09-02 stsp
1748 bcbd79e2 2018-08-19 stsp static const struct got_error *
1749 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1750 e5a0f69f 2018-08-18 stsp {
1751 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1752 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1753 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1754 64486692 2022-07-07 thomas char *mode;
1755 fd823528 2018-10-22 stsp int fast_refresh = 10;
1756 1a76625f 2018-10-22 stsp int done = 0, errcode;
1757 e5a0f69f 2018-08-18 stsp
1758 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1759 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1760 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1761 64486692 2022-07-07 thomas else
1762 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1763 64486692 2022-07-07 thomas
1764 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1765 1a76625f 2018-10-22 stsp if (errcode)
1766 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1767 1a76625f 2018-10-22 stsp
1768 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1769 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1770 e5a0f69f 2018-08-18 stsp
1771 1004088d 2018-09-29 stsp view->focussed = 1;
1772 878940b7 2018-09-29 stsp err = view->show(view);
1773 0cf4efb1 2018-09-29 stsp if (err)
1774 0cf4efb1 2018-09-29 stsp return err;
1775 0cf4efb1 2018-09-29 stsp update_panels();
1776 0cf4efb1 2018-09-29 stsp doupdate();
1777 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1778 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1779 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1780 b185e033 2023-01-23 thomas if (fast_refresh && --fast_refresh == 0)
1781 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1782 fd823528 2018-10-22 stsp
1783 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1784 e5a0f69f 2018-08-18 stsp if (err)
1785 e5a0f69f 2018-08-18 stsp break;
1786 6ec3d39c 2023-01-23 thomas
1787 6ec3d39c 2023-01-23 thomas if (view->dying && view == TAILQ_FIRST(&views) &&
1788 6ec3d39c 2023-01-23 thomas TAILQ_NEXT(view, entry) == NULL)
1789 6ec3d39c 2023-01-23 thomas done = 1;
1790 6ec3d39c 2023-01-23 thomas if (done) {
1791 6ec3d39c 2023-01-23 thomas struct tog_view *v;
1792 6ec3d39c 2023-01-23 thomas
1793 6ec3d39c 2023-01-23 thomas /*
1794 6ec3d39c 2023-01-23 thomas * When we quit, scroll the screen up a single line
1795 6ec3d39c 2023-01-23 thomas * so we don't lose any information.
1796 6ec3d39c 2023-01-23 thomas */
1797 6ec3d39c 2023-01-23 thomas TAILQ_FOREACH(v, &views, entry) {
1798 6ec3d39c 2023-01-23 thomas wmove(v->window, 0, 0);
1799 6ec3d39c 2023-01-23 thomas wdeleteln(v->window);
1800 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->window);
1801 6ec3d39c 2023-01-23 thomas if (v->child && !view_is_fullscreen(v)) {
1802 6ec3d39c 2023-01-23 thomas wmove(v->child->window, 0, 0);
1803 6ec3d39c 2023-01-23 thomas wdeleteln(v->child->window);
1804 6ec3d39c 2023-01-23 thomas wnoutrefresh(v->child->window);
1805 6ec3d39c 2023-01-23 thomas }
1806 6ec3d39c 2023-01-23 thomas }
1807 6ec3d39c 2023-01-23 thomas doupdate();
1808 6ec3d39c 2023-01-23 thomas }
1809 6ec3d39c 2023-01-23 thomas
1810 9970f7fc 2020-12-03 stsp if (view->dying) {
1811 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1812 669b5ffa 2018-10-07 stsp
1813 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1814 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1815 9970f7fc 2020-12-03 stsp entry);
1816 e78dc838 2020-12-04 stsp else if (view->parent)
1817 669b5ffa 2018-10-07 stsp prev = view->parent;
1818 669b5ffa 2018-10-07 stsp
1819 e78dc838 2020-12-04 stsp if (view->parent) {
1820 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1821 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1822 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1823 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1824 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1825 40236d76 2022-06-23 thomas if (err)
1826 40236d76 2022-06-23 thomas break;
1827 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1828 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1829 e78dc838 2020-12-04 stsp } else
1830 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1831 669b5ffa 2018-10-07 stsp
1832 9970f7fc 2020-12-03 stsp err = view_close(view);
1833 fb59748f 2020-12-05 stsp if (err)
1834 e5a0f69f 2018-08-18 stsp goto done;
1835 669b5ffa 2018-10-07 stsp
1836 e78dc838 2020-12-04 stsp view = NULL;
1837 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1838 e78dc838 2020-12-04 stsp if (v->focussed)
1839 e78dc838 2020-12-04 stsp break;
1840 0cf4efb1 2018-09-29 stsp }
1841 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1842 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1843 e78dc838 2020-12-04 stsp if (prev)
1844 e78dc838 2020-12-04 stsp view = prev;
1845 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1846 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1847 e78dc838 2020-12-04 stsp tog_view_list_head);
1848 e78dc838 2020-12-04 stsp }
1849 e78dc838 2020-12-04 stsp if (view) {
1850 e78dc838 2020-12-04 stsp if (view->focus_child) {
1851 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1852 e78dc838 2020-12-04 stsp view = view->child;
1853 e78dc838 2020-12-04 stsp } else
1854 e78dc838 2020-12-04 stsp view->focussed = 1;
1855 e78dc838 2020-12-04 stsp }
1856 e78dc838 2020-12-04 stsp }
1857 e5a0f69f 2018-08-18 stsp }
1858 bcbd79e2 2018-08-19 stsp if (new_view) {
1859 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1860 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1861 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1862 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1863 86c66b02 2018-10-18 stsp continue;
1864 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1865 86c66b02 2018-10-18 stsp err = view_close(v);
1866 86c66b02 2018-10-18 stsp if (err)
1867 86c66b02 2018-10-18 stsp goto done;
1868 86c66b02 2018-10-18 stsp break;
1869 86c66b02 2018-10-18 stsp }
1870 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1871 fed7eaa8 2018-10-24 stsp view = new_view;
1872 7cd52833 2022-06-23 thomas }
1873 6ec3d39c 2023-01-23 thomas if (view && !done) {
1874 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1875 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1876 e78dc838 2020-12-04 stsp view = view->child;
1877 e78dc838 2020-12-04 stsp } else {
1878 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1879 e78dc838 2020-12-04 stsp view = view->parent;
1880 1a76625f 2018-10-22 stsp }
1881 e78dc838 2020-12-04 stsp show_panel(view->panel);
1882 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1883 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1884 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1885 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1886 669b5ffa 2018-10-07 stsp if (err)
1887 1a76625f 2018-10-22 stsp goto done;
1888 669b5ffa 2018-10-07 stsp }
1889 669b5ffa 2018-10-07 stsp err = view->show(view);
1890 0cf4efb1 2018-09-29 stsp if (err)
1891 1a76625f 2018-10-22 stsp goto done;
1892 669b5ffa 2018-10-07 stsp if (view->child) {
1893 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1894 669b5ffa 2018-10-07 stsp if (err)
1895 1a76625f 2018-10-22 stsp goto done;
1896 669b5ffa 2018-10-07 stsp }
1897 1a76625f 2018-10-22 stsp update_panels();
1898 1a76625f 2018-10-22 stsp doupdate();
1899 0cf4efb1 2018-09-29 stsp }
1900 e5a0f69f 2018-08-18 stsp }
1901 e5a0f69f 2018-08-18 stsp done:
1902 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1903 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1904 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1905 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1906 f2d749db 2022-07-12 thomas close_err = view_close(view);
1907 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1908 f2d749db 2022-07-12 thomas err = close_err;
1909 e5a0f69f 2018-08-18 stsp }
1910 1a76625f 2018-10-22 stsp
1911 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1912 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1913 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1914 1a76625f 2018-10-22 stsp
1915 e5a0f69f 2018-08-18 stsp return err;
1916 ea5e7bb5 2018-08-01 stsp }
1917 ea5e7bb5 2018-08-01 stsp
1918 4ed7e80c 2018-05-20 stsp __dead static void
1919 9f7d7167 2018-04-29 stsp usage_log(void)
1920 9f7d7167 2018-04-29 stsp {
1921 80ddbec8 2018-04-29 stsp endwin();
1922 c70c5802 2018-08-01 stsp fprintf(stderr,
1923 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1924 9f7d7167 2018-04-29 stsp getprogname());
1925 9f7d7167 2018-04-29 stsp exit(1);
1926 80ddbec8 2018-04-29 stsp }
1927 80ddbec8 2018-04-29 stsp
1928 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1929 80ddbec8 2018-04-29 stsp static const struct got_error *
1930 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1931 963b370f 2018-05-20 stsp {
1932 00dfcb92 2018-06-11 stsp char *vis = NULL;
1933 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1934 963b370f 2018-05-20 stsp
1935 963b370f 2018-05-20 stsp *ws = NULL;
1936 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1937 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1938 00dfcb92 2018-06-11 stsp int vislen;
1939 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1940 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1941 00dfcb92 2018-06-11 stsp
1942 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1943 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1944 00dfcb92 2018-06-11 stsp if (err)
1945 00dfcb92 2018-06-11 stsp return err;
1946 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1947 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1948 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1949 a7f50699 2018-06-11 stsp goto done;
1950 a7f50699 2018-06-11 stsp }
1951 00dfcb92 2018-06-11 stsp }
1952 963b370f 2018-05-20 stsp
1953 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1954 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1955 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1956 a7f50699 2018-06-11 stsp goto done;
1957 a7f50699 2018-06-11 stsp }
1958 963b370f 2018-05-20 stsp
1959 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1960 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1961 a7f50699 2018-06-11 stsp done:
1962 00dfcb92 2018-06-11 stsp free(vis);
1963 963b370f 2018-05-20 stsp if (err) {
1964 963b370f 2018-05-20 stsp free(*ws);
1965 963b370f 2018-05-20 stsp *ws = NULL;
1966 963b370f 2018-05-20 stsp *wlen = 0;
1967 963b370f 2018-05-20 stsp }
1968 963b370f 2018-05-20 stsp return err;
1969 05171be4 2022-06-23 thomas }
1970 05171be4 2022-06-23 thomas
1971 05171be4 2022-06-23 thomas static const struct got_error *
1972 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1973 05171be4 2022-06-23 thomas {
1974 05171be4 2022-06-23 thomas char *dst;
1975 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1976 05171be4 2022-06-23 thomas
1977 05171be4 2022-06-23 thomas *ptr = NULL;
1978 05171be4 2022-06-23 thomas n = len = strlen(src);
1979 83316834 2022-06-23 thomas dst = malloc(n + 1);
1980 05171be4 2022-06-23 thomas if (dst == NULL)
1981 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1982 05171be4 2022-06-23 thomas
1983 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1984 05171be4 2022-06-23 thomas const char c = src[idx];
1985 05171be4 2022-06-23 thomas
1986 05171be4 2022-06-23 thomas if (c == '\t') {
1987 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1988 b235ad5d 2022-06-23 thomas char *p;
1989 b235ad5d 2022-06-23 thomas
1990 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1991 83316834 2022-06-23 thomas if (p == NULL) {
1992 83316834 2022-06-23 thomas free(dst);
1993 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1994 83316834 2022-06-23 thomas
1995 83316834 2022-06-23 thomas }
1996 83316834 2022-06-23 thomas dst = p;
1997 05171be4 2022-06-23 thomas n += nb;
1998 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1999 05171be4 2022-06-23 thomas sz += nb;
2000 05171be4 2022-06-23 thomas } else
2001 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
2002 05171be4 2022-06-23 thomas ++idx;
2003 05171be4 2022-06-23 thomas }
2004 05171be4 2022-06-23 thomas
2005 05171be4 2022-06-23 thomas dst[sz] = '\0';
2006 05171be4 2022-06-23 thomas *ptr = dst;
2007 05171be4 2022-06-23 thomas return NULL;
2008 963b370f 2018-05-20 stsp }
2009 963b370f 2018-05-20 stsp
2010 8d208d34 2022-06-23 thomas /*
2011 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
2012 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
2013 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
2014 8d208d34 2022-06-23 thomas * *rcol.
2015 f91a2b48 2022-06-23 thomas */
2016 8d208d34 2022-06-23 thomas static int
2017 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
2018 8d208d34 2022-06-23 thomas {
2019 8d208d34 2022-06-23 thomas int width, i, cols = 0;
2020 f91a2b48 2022-06-23 thomas
2021 8d208d34 2022-06-23 thomas if (n == 0) {
2022 8d208d34 2022-06-23 thomas *rcol = cols;
2023 8d208d34 2022-06-23 thomas return off;
2024 8d208d34 2022-06-23 thomas }
2025 f91a2b48 2022-06-23 thomas
2026 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
2027 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
2028 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
2029 8d208d34 2022-06-23 thomas else
2030 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
2031 f91a2b48 2022-06-23 thomas
2032 8d208d34 2022-06-23 thomas if (width == -1) {
2033 8d208d34 2022-06-23 thomas width = 1;
2034 8d208d34 2022-06-23 thomas wline[i] = L'.';
2035 f91a2b48 2022-06-23 thomas }
2036 f91a2b48 2022-06-23 thomas
2037 8d208d34 2022-06-23 thomas if (cols + width > n)
2038 8d208d34 2022-06-23 thomas break;
2039 8d208d34 2022-06-23 thomas cols += width;
2040 f91a2b48 2022-06-23 thomas }
2041 f91a2b48 2022-06-23 thomas
2042 8d208d34 2022-06-23 thomas *rcol = cols;
2043 8d208d34 2022-06-23 thomas return i;
2044 f91a2b48 2022-06-23 thomas }
2045 f91a2b48 2022-06-23 thomas
2046 f91a2b48 2022-06-23 thomas /*
2047 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
2048 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
2049 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
2050 f91a2b48 2022-06-23 thomas */
2051 f91a2b48 2022-06-23 thomas static const struct got_error *
2052 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
2053 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
2054 f91a2b48 2022-06-23 thomas {
2055 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
2056 8d208d34 2022-06-23 thomas int cols;
2057 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
2058 05171be4 2022-06-23 thomas char *exstr = NULL;
2059 963b370f 2018-05-20 stsp size_t wlen;
2060 8d208d34 2022-06-23 thomas int i, scrollx;
2061 963b370f 2018-05-20 stsp
2062 963b370f 2018-05-20 stsp *wlinep = NULL;
2063 b700b5d6 2018-07-10 stsp *widthp = 0;
2064 963b370f 2018-05-20 stsp
2065 05171be4 2022-06-23 thomas if (expand) {
2066 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
2067 05171be4 2022-06-23 thomas if (err)
2068 05171be4 2022-06-23 thomas return err;
2069 05171be4 2022-06-23 thomas }
2070 05171be4 2022-06-23 thomas
2071 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2072 05171be4 2022-06-23 thomas free(exstr);
2073 963b370f 2018-05-20 stsp if (err)
2074 963b370f 2018-05-20 stsp return err;
2075 963b370f 2018-05-20 stsp
2076 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2077 f91a2b48 2022-06-23 thomas
2078 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
2079 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2080 3f670bfb 2020-12-10 stsp wlen--;
2081 3f670bfb 2020-12-10 stsp }
2082 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
2083 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
2084 3f670bfb 2020-12-10 stsp wlen--;
2085 3f670bfb 2020-12-10 stsp }
2086 3f670bfb 2020-12-10 stsp
2087 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2088 8d208d34 2022-06-23 thomas wline[i] = L'\0';
2089 27a741e5 2019-09-11 stsp
2090 b700b5d6 2018-07-10 stsp if (widthp)
2091 b700b5d6 2018-07-10 stsp *widthp = cols;
2092 f91a2b48 2022-06-23 thomas if (scrollxp)
2093 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
2094 963b370f 2018-05-20 stsp if (err)
2095 963b370f 2018-05-20 stsp free(wline);
2096 963b370f 2018-05-20 stsp else
2097 963b370f 2018-05-20 stsp *wlinep = wline;
2098 963b370f 2018-05-20 stsp return err;
2099 963b370f 2018-05-20 stsp }
2100 963b370f 2018-05-20 stsp
2101 8b473291 2019-02-21 stsp static const struct got_error*
2102 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
2103 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
2104 8b473291 2019-02-21 stsp {
2105 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
2106 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
2107 8b473291 2019-02-21 stsp char *s;
2108 8b473291 2019-02-21 stsp const char *name;
2109 8b473291 2019-02-21 stsp
2110 8b473291 2019-02-21 stsp *refs_str = NULL;
2111 8b473291 2019-02-21 stsp
2112 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
2113 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
2114 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
2115 52b5abe1 2019-08-13 stsp int cmp;
2116 52b5abe1 2019-08-13 stsp
2117 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
2118 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2119 8b473291 2019-02-21 stsp continue;
2120 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
2121 8b473291 2019-02-21 stsp name += 5;
2122 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
2123 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
2124 7143d404 2019-03-12 stsp continue;
2125 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
2126 8b473291 2019-02-21 stsp name += 6;
2127 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
2128 8b473291 2019-02-21 stsp name += 8;
2129 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
2130 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
2131 79cc719f 2020-04-24 stsp continue;
2132 79cc719f 2020-04-24 stsp }
2133 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
2134 48cae60d 2020-09-22 stsp if (err)
2135 48cae60d 2020-09-22 stsp break;
2136 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2137 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
2138 5d844a1e 2019-08-13 stsp if (err) {
2139 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
2140 48cae60d 2020-09-22 stsp free(ref_id);
2141 5d844a1e 2019-08-13 stsp break;
2142 48cae60d 2020-09-22 stsp }
2143 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2144 5d844a1e 2019-08-13 stsp err = NULL;
2145 5d844a1e 2019-08-13 stsp tag = NULL;
2146 5d844a1e 2019-08-13 stsp }
2147 52b5abe1 2019-08-13 stsp }
2148 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2149 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
2150 48cae60d 2020-09-22 stsp free(ref_id);
2151 52b5abe1 2019-08-13 stsp if (tag)
2152 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
2153 52b5abe1 2019-08-13 stsp if (cmp != 0)
2154 52b5abe1 2019-08-13 stsp continue;
2155 8b473291 2019-02-21 stsp s = *refs_str;
2156 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
2157 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
2158 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2159 8b473291 2019-02-21 stsp free(s);
2160 8b473291 2019-02-21 stsp *refs_str = NULL;
2161 8b473291 2019-02-21 stsp break;
2162 8b473291 2019-02-21 stsp }
2163 8b473291 2019-02-21 stsp free(s);
2164 8b473291 2019-02-21 stsp }
2165 8b473291 2019-02-21 stsp
2166 8b473291 2019-02-21 stsp return err;
2167 8b473291 2019-02-21 stsp }
2168 8b473291 2019-02-21 stsp
2169 963b370f 2018-05-20 stsp static const struct got_error *
2170 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2171 27a741e5 2019-09-11 stsp int col_tab_align)
2172 5813d178 2019-03-09 stsp {
2173 e6b8b890 2020-12-29 naddy char *smallerthan;
2174 5813d178 2019-03-09 stsp
2175 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
2176 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
2177 5813d178 2019-03-09 stsp author = smallerthan + 1;
2178 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
2179 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
2180 f91a2b48 2022-06-23 thomas col_tab_align, 0);
2181 5813d178 2019-03-09 stsp }
2182 5813d178 2019-03-09 stsp
2183 5813d178 2019-03-09 stsp static const struct got_error *
2184 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
2185 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
2186 8fdc79fe 2020-12-01 naddy int author_display_cols)
2187 80ddbec8 2018-04-29 stsp {
2188 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2189 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2190 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2191 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
2192 5813d178 2019-03-09 stsp char *author = NULL;
2193 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
2194 bb737323 2018-05-20 stsp int author_width, logmsg_width;
2195 5813d178 2019-03-09 stsp char *newline, *line = NULL;
2196 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
2197 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
2198 ccb26ccd 2018-11-05 stsp struct tm tm;
2199 45d799e2 2018-12-23 stsp time_t committer_time;
2200 11b20872 2019-11-08 stsp struct tog_color *tc;
2201 80ddbec8 2018-04-29 stsp
2202 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2203 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2204 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2205 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2206 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2207 b39d25c7 2018-07-10 stsp
2208 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2209 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2210 b39d25c7 2018-07-10 stsp else
2211 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2212 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2213 11b20872 2019-11-08 stsp if (tc)
2214 11b20872 2019-11-08 stsp wattr_on(view->window,
2215 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2216 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2217 11b20872 2019-11-08 stsp if (tc)
2218 11b20872 2019-11-08 stsp wattr_off(view->window,
2219 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2220 27a741e5 2019-09-11 stsp col = limit;
2221 b39d25c7 2018-07-10 stsp if (col > avail)
2222 b39d25c7 2018-07-10 stsp goto done;
2223 6570a66d 2019-11-08 stsp
2224 6570a66d 2019-11-08 stsp if (avail >= 120) {
2225 6570a66d 2019-11-08 stsp char *id_str;
2226 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2227 6570a66d 2019-11-08 stsp if (err)
2228 6570a66d 2019-11-08 stsp goto done;
2229 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2230 11b20872 2019-11-08 stsp if (tc)
2231 11b20872 2019-11-08 stsp wattr_on(view->window,
2232 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2233 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2234 11b20872 2019-11-08 stsp if (tc)
2235 11b20872 2019-11-08 stsp wattr_off(view->window,
2236 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2237 6570a66d 2019-11-08 stsp free(id_str);
2238 6570a66d 2019-11-08 stsp col += 9;
2239 6570a66d 2019-11-08 stsp if (col > avail)
2240 6570a66d 2019-11-08 stsp goto done;
2241 6570a66d 2019-11-08 stsp }
2242 b39d25c7 2018-07-10 stsp
2243 f69c5a46 2022-07-19 thomas if (s->use_committer)
2244 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2245 f69c5a46 2022-07-19 thomas else
2246 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2247 5813d178 2019-03-09 stsp if (author == NULL) {
2248 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2249 80ddbec8 2018-04-29 stsp goto done;
2250 80ddbec8 2018-04-29 stsp }
2251 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2252 bb737323 2018-05-20 stsp if (err)
2253 bb737323 2018-05-20 stsp goto done;
2254 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2255 11b20872 2019-11-08 stsp if (tc)
2256 11b20872 2019-11-08 stsp wattr_on(view->window,
2257 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2258 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2259 bb737323 2018-05-20 stsp col += author_width;
2260 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2261 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2262 bb737323 2018-05-20 stsp col++;
2263 bb737323 2018-05-20 stsp author_width++;
2264 bb737323 2018-05-20 stsp }
2265 f31d6c3b 2022-09-09 thomas if (tc)
2266 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2267 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2268 9c2eaf34 2018-05-20 stsp if (col > avail)
2269 9c2eaf34 2018-05-20 stsp goto done;
2270 80ddbec8 2018-04-29 stsp
2271 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2272 5943eee2 2019-08-13 stsp if (err)
2273 6d9fbc00 2018-04-29 stsp goto done;
2274 bb737323 2018-05-20 stsp logmsg = logmsg0;
2275 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2276 bb737323 2018-05-20 stsp logmsg++;
2277 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2278 bb737323 2018-05-20 stsp if (newline)
2279 bb737323 2018-05-20 stsp *newline = '\0';
2280 f91a2b48 2022-06-23 thomas limit = avail - col;
2281 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2282 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2283 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2284 f91a2b48 2022-06-23 thomas limit, col, 1);
2285 331b1a16 2022-06-23 thomas if (err)
2286 331b1a16 2022-06-23 thomas goto done;
2287 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2288 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2289 27a741e5 2019-09-11 stsp while (col < avail) {
2290 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2291 bb737323 2018-05-20 stsp col++;
2292 881b2d3e 2018-04-30 stsp }
2293 80ddbec8 2018-04-29 stsp done:
2294 80ddbec8 2018-04-29 stsp free(logmsg0);
2295 bb737323 2018-05-20 stsp free(wlogmsg);
2296 5813d178 2019-03-09 stsp free(author);
2297 bb737323 2018-05-20 stsp free(wauthor);
2298 80ddbec8 2018-04-29 stsp free(line);
2299 80ddbec8 2018-04-29 stsp return err;
2300 80ddbec8 2018-04-29 stsp }
2301 26ed57b2 2018-05-19 stsp
2302 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2303 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2304 899d86c2 2018-05-10 stsp struct got_object_id *id)
2305 80ddbec8 2018-04-29 stsp {
2306 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2307 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2308 80ddbec8 2018-04-29 stsp
2309 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2310 80ddbec8 2018-04-29 stsp if (entry == NULL)
2311 899d86c2 2018-05-10 stsp return NULL;
2312 99db9666 2018-05-07 stsp
2313 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2314 d68b9737 2022-09-05 thomas if (dup == NULL) {
2315 d68b9737 2022-09-05 thomas free(entry);
2316 d68b9737 2022-09-05 thomas return NULL;
2317 d68b9737 2022-09-05 thomas }
2318 d68b9737 2022-09-05 thomas
2319 d68b9737 2022-09-05 thomas entry->id = dup;
2320 99db9666 2018-05-07 stsp entry->commit = commit;
2321 899d86c2 2018-05-10 stsp return entry;
2322 99db9666 2018-05-07 stsp }
2323 80ddbec8 2018-04-29 stsp
2324 99db9666 2018-05-07 stsp static void
2325 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2326 99db9666 2018-05-07 stsp {
2327 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2328 99db9666 2018-05-07 stsp
2329 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2330 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2331 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2332 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2333 d68b9737 2022-09-05 thomas free(entry->id);
2334 99db9666 2018-05-07 stsp free(entry);
2335 99db9666 2018-05-07 stsp }
2336 99db9666 2018-05-07 stsp
2337 99db9666 2018-05-07 stsp static void
2338 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2339 99db9666 2018-05-07 stsp {
2340 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2341 99db9666 2018-05-07 stsp pop_commit(commits);
2342 c4972b91 2018-05-07 stsp }
2343 c4972b91 2018-05-07 stsp
2344 c4972b91 2018-05-07 stsp static const struct got_error *
2345 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2346 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2347 13add988 2019-10-15 stsp {
2348 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2349 13add988 2019-10-15 stsp regmatch_t regmatch;
2350 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2351 13add988 2019-10-15 stsp
2352 13add988 2019-10-15 stsp *have_match = 0;
2353 13add988 2019-10-15 stsp
2354 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2355 13add988 2019-10-15 stsp if (err)
2356 13add988 2019-10-15 stsp return err;
2357 13add988 2019-10-15 stsp
2358 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2359 13add988 2019-10-15 stsp if (err)
2360 13add988 2019-10-15 stsp goto done;
2361 13add988 2019-10-15 stsp
2362 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2363 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2364 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2365 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2366 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2367 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2368 13add988 2019-10-15 stsp *have_match = 1;
2369 13add988 2019-10-15 stsp done:
2370 13add988 2019-10-15 stsp free(id_str);
2371 13add988 2019-10-15 stsp free(logmsg);
2372 13add988 2019-10-15 stsp return err;
2373 13add988 2019-10-15 stsp }
2374 13add988 2019-10-15 stsp
2375 13add988 2019-10-15 stsp static const struct got_error *
2376 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2377 c4972b91 2018-05-07 stsp {
2378 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2379 9ba79e04 2018-06-11 stsp
2380 1a76625f 2018-10-22 stsp /*
2381 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2382 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2383 1a76625f 2018-10-22 stsp * while updating the display.
2384 1a76625f 2018-10-22 stsp */
2385 4e0d2870 2020-12-07 naddy do {
2386 7210b715 2022-09-11 thomas struct got_object_id id;
2387 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2388 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2389 7e8004ba 2022-09-11 thomas int limit_match = 0;
2390 1a76625f 2018-10-22 stsp int errcode;
2391 899d86c2 2018-05-10 stsp
2392 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2393 4e0d2870 2020-12-07 naddy NULL, NULL);
2394 7210b715 2022-09-11 thomas if (err)
2395 ecb28ae0 2018-07-16 stsp break;
2396 899d86c2 2018-05-10 stsp
2397 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2398 9ba79e04 2018-06-11 stsp if (err)
2399 9ba79e04 2018-06-11 stsp break;
2400 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2401 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2402 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2403 9ba79e04 2018-06-11 stsp break;
2404 9ba79e04 2018-06-11 stsp }
2405 93e45b7c 2018-09-24 stsp
2406 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2407 1a76625f 2018-10-22 stsp if (errcode) {
2408 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2409 13add988 2019-10-15 stsp "pthread_mutex_lock");
2410 1a76625f 2018-10-22 stsp break;
2411 1a76625f 2018-10-22 stsp }
2412 1a76625f 2018-10-22 stsp
2413 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2414 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2415 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2416 1a76625f 2018-10-22 stsp
2417 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2418 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2419 7e8004ba 2022-09-11 thomas a->limit_regex);
2420 7e8004ba 2022-09-11 thomas if (err)
2421 7e8004ba 2022-09-11 thomas break;
2422 7e8004ba 2022-09-11 thomas
2423 7e8004ba 2022-09-11 thomas if (limit_match) {
2424 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2425 7e8004ba 2022-09-11 thomas
2426 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2427 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2428 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2429 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2430 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2431 7e8004ba 2022-09-11 thomas break;
2432 7e8004ba 2022-09-11 thomas }
2433 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
2434 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
2435 7e8004ba 2022-09-11 thomas
2436 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2437 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2438 7e8004ba 2022-09-11 thomas matched, entry);
2439 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2440 7e8004ba 2022-09-11 thomas }
2441 7e8004ba 2022-09-11 thomas
2442 7e8004ba 2022-09-11 thomas /*
2443 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2444 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2445 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2446 7e8004ba 2022-09-11 thomas */
2447 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2448 7e8004ba 2022-09-11 thomas }
2449 7e8004ba 2022-09-11 thomas
2450 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2451 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2452 7c1452c1 2020-03-26 stsp int have_match;
2453 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2454 7c1452c1 2020-03-26 stsp if (err)
2455 7c1452c1 2020-03-26 stsp break;
2456 7e8004ba 2022-09-11 thomas
2457 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2458 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2459 7e8004ba 2022-09-11 thomas *a->search_next_done =
2460 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2461 7e8004ba 2022-09-11 thomas } else if (have_match)
2462 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2463 13add988 2019-10-15 stsp }
2464 13add988 2019-10-15 stsp
2465 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2466 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2467 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2468 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2469 7c1452c1 2020-03-26 stsp if (err)
2470 13add988 2019-10-15 stsp break;
2471 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2472 899d86c2 2018-05-10 stsp
2473 9ba79e04 2018-06-11 stsp return err;
2474 0553a4e3 2018-04-30 stsp }
2475 0553a4e3 2018-04-30 stsp
2476 2b779855 2020-12-05 naddy static void
2477 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2478 2b779855 2020-12-05 naddy {
2479 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2480 2b779855 2020-12-05 naddy int ncommits = 0;
2481 2b779855 2020-12-05 naddy
2482 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2483 2b779855 2020-12-05 naddy while (entry) {
2484 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2485 2b779855 2020-12-05 naddy s->selected_entry = entry;
2486 2b779855 2020-12-05 naddy break;
2487 2b779855 2020-12-05 naddy }
2488 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2489 2b779855 2020-12-05 naddy ncommits++;
2490 2b779855 2020-12-05 naddy }
2491 2b779855 2020-12-05 naddy }
2492 2b779855 2020-12-05 naddy
2493 0553a4e3 2018-04-30 stsp static const struct got_error *
2494 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2495 0553a4e3 2018-04-30 stsp {
2496 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2497 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2498 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2499 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2500 60493ae3 2019-06-20 stsp int width;
2501 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2502 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2503 8b473291 2019-02-21 stsp char *refs_str = NULL;
2504 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2505 11b20872 2019-11-08 stsp struct tog_color *tc;
2506 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2507 bd3f8225 2022-08-12 thomas
2508 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2509 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2510 0553a4e3 2018-04-30 stsp
2511 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2512 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2513 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2514 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2515 1a76625f 2018-10-22 stsp if (err)
2516 ecb28ae0 2018-07-16 stsp return err;
2517 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2518 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2519 d2075bf3 2020-12-25 stsp if (refs) {
2520 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2521 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2522 d2075bf3 2020-12-25 stsp if (err)
2523 d2075bf3 2020-12-25 stsp goto done;
2524 d2075bf3 2020-12-25 stsp }
2525 867c6645 2018-07-10 stsp }
2526 359bfafd 2019-02-22 stsp
2527 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2528 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2529 1a76625f 2018-10-22 stsp
2530 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2531 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2532 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2533 ba5cc5fa 2022-09-23 thomas (view->searching && !view->search_next_done) ?
2534 ba5cc5fa 2022-09-23 thomas "searching..." : "loading...") == -1) {
2535 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2536 8f4ed634 2020-03-26 stsp goto done;
2537 8f4ed634 2020-03-26 stsp }
2538 8f4ed634 2020-03-26 stsp } else {
2539 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2540 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2541 f9686aa5 2020-03-27 stsp
2542 f9686aa5 2020-03-27 stsp if (view->searching) {
2543 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2544 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2545 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2546 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2547 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2548 f9686aa5 2020-03-27 stsp search_str = "searching...";
2549 f9686aa5 2020-03-27 stsp }
2550 f9686aa5 2020-03-27 stsp
2551 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2552 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2553 7e8004ba 2022-09-11 thomas
2554 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2555 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2556 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2557 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2558 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2559 8f4ed634 2020-03-26 stsp goto done;
2560 8f4ed634 2020-03-26 stsp }
2561 8b473291 2019-02-21 stsp }
2562 1a76625f 2018-10-22 stsp
2563 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2564 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2565 91198554 2022-06-23 thomas "........................................",
2566 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2567 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2568 1a76625f 2018-10-22 stsp header = NULL;
2569 1a76625f 2018-10-22 stsp goto done;
2570 1a76625f 2018-10-22 stsp }
2571 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2572 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2573 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2574 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2575 1a76625f 2018-10-22 stsp header = NULL;
2576 1a76625f 2018-10-22 stsp goto done;
2577 ecb28ae0 2018-07-16 stsp }
2578 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2579 1a76625f 2018-10-22 stsp if (err)
2580 1a76625f 2018-10-22 stsp goto done;
2581 867c6645 2018-07-10 stsp
2582 2814baeb 2018-08-01 stsp werase(view->window);
2583 867c6645 2018-07-10 stsp
2584 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2585 a3404814 2018-09-02 stsp wstandout(view->window);
2586 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2587 11b20872 2019-11-08 stsp if (tc)
2588 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2589 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2590 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2591 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2592 1a76625f 2018-10-22 stsp width++;
2593 1a76625f 2018-10-22 stsp }
2594 86f4aab9 2022-09-09 thomas if (tc)
2595 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2596 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2597 a3404814 2018-09-02 stsp wstandend(view->window);
2598 ecb28ae0 2018-07-16 stsp free(wline);
2599 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2600 1a76625f 2018-10-22 stsp goto done;
2601 0553a4e3 2018-04-30 stsp
2602 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2603 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2604 5813d178 2019-03-09 stsp ncommits = 0;
2605 05171be4 2022-06-23 thomas view->maxx = 0;
2606 5813d178 2019-03-09 stsp while (entry) {
2607 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2608 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2609 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2610 5813d178 2019-03-09 stsp int width;
2611 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2612 5813d178 2019-03-09 stsp break;
2613 f69c5a46 2022-07-19 thomas if (s->use_committer)
2614 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2615 f69c5a46 2022-07-19 thomas else
2616 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2617 5813d178 2019-03-09 stsp if (author == NULL) {
2618 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2619 5813d178 2019-03-09 stsp goto done;
2620 5813d178 2019-03-09 stsp }
2621 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2622 27a741e5 2019-09-11 stsp date_display_cols);
2623 5813d178 2019-03-09 stsp if (author_cols < width)
2624 5813d178 2019-03-09 stsp author_cols = width;
2625 5813d178 2019-03-09 stsp free(wauthor);
2626 5813d178 2019-03-09 stsp free(author);
2627 ef944b8b 2022-07-21 thomas if (err)
2628 ef944b8b 2022-07-21 thomas goto done;
2629 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2630 05171be4 2022-06-23 thomas if (err)
2631 05171be4 2022-06-23 thomas goto done;
2632 05171be4 2022-06-23 thomas msg = msg0;
2633 05171be4 2022-06-23 thomas while (*msg == '\n')
2634 05171be4 2022-06-23 thomas ++msg;
2635 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2636 331b1a16 2022-06-23 thomas *eol = '\0';
2637 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2638 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2639 331b1a16 2022-06-23 thomas if (err)
2640 331b1a16 2022-06-23 thomas goto done;
2641 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2642 05171be4 2022-06-23 thomas free(msg0);
2643 331b1a16 2022-06-23 thomas free(wmsg);
2644 7ca04879 2019-10-19 stsp ncommits++;
2645 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2646 5813d178 2019-03-09 stsp }
2647 5813d178 2019-03-09 stsp
2648 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2649 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2650 867c6645 2018-07-10 stsp ncommits = 0;
2651 899d86c2 2018-05-10 stsp while (entry) {
2652 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2653 899d86c2 2018-05-10 stsp break;
2654 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2655 2814baeb 2018-08-01 stsp wstandout(view->window);
2656 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2657 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2658 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2659 2814baeb 2018-08-01 stsp wstandend(view->window);
2660 0553a4e3 2018-04-30 stsp if (err)
2661 60493ae3 2019-06-20 stsp goto done;
2662 0553a4e3 2018-04-30 stsp ncommits++;
2663 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2664 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2665 80ddbec8 2018-04-29 stsp }
2666 80ddbec8 2018-04-29 stsp
2667 a5d43cac 2022-07-01 thomas view_border(view);
2668 1a76625f 2018-10-22 stsp done:
2669 1a76625f 2018-10-22 stsp free(id_str);
2670 8b473291 2019-02-21 stsp free(refs_str);
2671 1a76625f 2018-10-22 stsp free(ncommits_str);
2672 1a76625f 2018-10-22 stsp free(header);
2673 80ddbec8 2018-04-29 stsp return err;
2674 9f7d7167 2018-04-29 stsp }
2675 07b55e75 2018-05-10 stsp
2676 07b55e75 2018-05-10 stsp static void
2677 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2678 07b55e75 2018-05-10 stsp {
2679 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2680 07b55e75 2018-05-10 stsp int nscrolled = 0;
2681 07b55e75 2018-05-10 stsp
2682 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2683 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2684 07b55e75 2018-05-10 stsp return;
2685 9f7d7167 2018-04-29 stsp
2686 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2687 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2688 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2689 07b55e75 2018-05-10 stsp if (entry) {
2690 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2691 07b55e75 2018-05-10 stsp nscrolled++;
2692 07b55e75 2018-05-10 stsp }
2693 07b55e75 2018-05-10 stsp }
2694 aa075928 2018-05-10 stsp }
2695 aa075928 2018-05-10 stsp
2696 aa075928 2018-05-10 stsp static const struct got_error *
2697 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2698 aa075928 2018-05-10 stsp {
2699 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2700 5e224a3e 2019-02-22 stsp int errcode;
2701 8a42fca8 2019-02-22 stsp
2702 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2703 aa075928 2018-05-10 stsp
2704 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2705 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2706 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2707 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2708 7aafa0d1 2019-02-22 stsp if (errcode)
2709 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2710 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2711 7c1452c1 2020-03-26 stsp
2712 7c1452c1 2020-03-26 stsp /*
2713 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2714 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2715 7c1452c1 2020-03-26 stsp */
2716 7c1452c1 2020-03-26 stsp if (!wait)
2717 7c1452c1 2020-03-26 stsp break;
2718 7c1452c1 2020-03-26 stsp
2719 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2720 ffe38506 2020-12-01 naddy show_log_view(view);
2721 7c1452c1 2020-03-26 stsp update_panels();
2722 7c1452c1 2020-03-26 stsp doupdate();
2723 7c1452c1 2020-03-26 stsp
2724 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2725 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2726 82954512 2020-02-03 stsp if (errcode)
2727 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2728 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2729 82954512 2020-02-03 stsp
2730 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2731 ffe38506 2020-12-01 naddy show_log_view(view);
2732 7c1452c1 2020-03-26 stsp update_panels();
2733 7c1452c1 2020-03-26 stsp doupdate();
2734 5e224a3e 2019-02-22 stsp }
2735 5e224a3e 2019-02-22 stsp
2736 5e224a3e 2019-02-22 stsp return NULL;
2737 a5d43cac 2022-07-01 thomas }
2738 a5d43cac 2022-07-01 thomas
2739 a5d43cac 2022-07-01 thomas static const struct got_error *
2740 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2741 a5d43cac 2022-07-01 thomas {
2742 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2743 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2744 fe731b51 2022-07-14 thomas
2745 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2746 fe731b51 2022-07-14 thomas return NULL;
2747 a5d43cac 2022-07-01 thomas
2748 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2749 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2750 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2751 a5d43cac 2022-07-01 thomas
2752 a5d43cac 2022-07-01 thomas return err;
2753 5e224a3e 2019-02-22 stsp }
2754 5e224a3e 2019-02-22 stsp
2755 5e224a3e 2019-02-22 stsp static const struct got_error *
2756 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2757 5e224a3e 2019-02-22 stsp {
2758 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2759 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2760 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2761 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2762 5e224a3e 2019-02-22 stsp
2763 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2764 5e224a3e 2019-02-22 stsp return NULL;
2765 5e224a3e 2019-02-22 stsp
2766 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2767 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2768 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2769 08ebd0a9 2019-02-22 stsp /*
2770 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2771 08ebd0a9 2019-02-22 stsp */
2772 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2773 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2774 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2775 5e224a3e 2019-02-22 stsp if (err)
2776 5e224a3e 2019-02-22 stsp return err;
2777 7aafa0d1 2019-02-22 stsp }
2778 b295e71b 2019-02-22 stsp
2779 7aafa0d1 2019-02-22 stsp do {
2780 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2781 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2782 88048b54 2019-02-21 stsp break;
2783 88048b54 2019-02-21 stsp
2784 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2785 1a080567 2023-01-14 thomas pentry : s->last_displayed_entry;
2786 aa075928 2018-05-10 stsp
2787 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2788 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2789 dd0a52c1 2018-05-20 stsp break;
2790 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2791 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2792 aa075928 2018-05-10 stsp
2793 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2794 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2795 a5d43cac 2022-07-01 thomas else
2796 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2797 a5d43cac 2022-07-01 thomas
2798 dd0a52c1 2018-05-20 stsp return err;
2799 07b55e75 2018-05-10 stsp }
2800 4a7f7875 2018-05-10 stsp
2801 cd0acaa7 2018-05-20 stsp static const struct got_error *
2802 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2803 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2804 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2805 cd0acaa7 2018-05-20 stsp {
2806 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2807 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2808 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2809 cd0acaa7 2018-05-20 stsp
2810 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2811 15a94983 2018-12-23 stsp if (diff_view == NULL)
2812 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2813 ea5e7bb5 2018-08-01 stsp
2814 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2815 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2816 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2817 e5a0f69f 2018-08-18 stsp if (err == NULL)
2818 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2819 cd0acaa7 2018-05-20 stsp return err;
2820 4a7f7875 2018-05-10 stsp }
2821 4a7f7875 2018-05-10 stsp
2822 80ddbec8 2018-04-29 stsp static const struct got_error *
2823 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2824 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2825 9343a5fb 2018-06-23 stsp {
2826 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2827 941e9f74 2019-05-21 stsp
2828 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2829 941e9f74 2019-05-21 stsp if (parent == NULL)
2830 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2831 941e9f74 2019-05-21 stsp
2832 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2833 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2834 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2835 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2836 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2837 941e9f74 2019-05-21 stsp s->tree = subtree;
2838 941e9f74 2019-05-21 stsp s->selected = 0;
2839 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2840 941e9f74 2019-05-21 stsp return NULL;
2841 941e9f74 2019-05-21 stsp }
2842 941e9f74 2019-05-21 stsp
2843 941e9f74 2019-05-21 stsp static const struct got_error *
2844 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2845 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2846 941e9f74 2019-05-21 stsp {
2847 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2848 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2849 941e9f74 2019-05-21 stsp const char *p;
2850 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2851 9343a5fb 2018-06-23 stsp
2852 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2853 941e9f74 2019-05-21 stsp p = path;
2854 941e9f74 2019-05-21 stsp while (*p) {
2855 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2856 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2857 56e0773d 2019-11-28 stsp char *te_name;
2858 33cbf02b 2020-01-12 stsp
2859 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2860 33cbf02b 2020-01-12 stsp p++;
2861 941e9f74 2019-05-21 stsp
2862 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2863 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2864 941e9f74 2019-05-21 stsp if (slash == NULL)
2865 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2866 33cbf02b 2020-01-12 stsp else
2867 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2868 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2869 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2870 56e0773d 2019-11-28 stsp break;
2871 941e9f74 2019-05-21 stsp }
2872 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2873 56e0773d 2019-11-28 stsp if (te == NULL) {
2874 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2875 56e0773d 2019-11-28 stsp free(te_name);
2876 941e9f74 2019-05-21 stsp break;
2877 941e9f74 2019-05-21 stsp }
2878 56e0773d 2019-11-28 stsp free(te_name);
2879 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2880 941e9f74 2019-05-21 stsp
2881 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2882 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2883 b03c880f 2019-05-21 stsp
2884 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2885 941e9f74 2019-05-21 stsp if (slash)
2886 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2887 941e9f74 2019-05-21 stsp else
2888 941e9f74 2019-05-21 stsp subpath = strdup(path);
2889 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2890 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2891 941e9f74 2019-05-21 stsp break;
2892 941e9f74 2019-05-21 stsp }
2893 941e9f74 2019-05-21 stsp
2894 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2895 941e9f74 2019-05-21 stsp subpath);
2896 941e9f74 2019-05-21 stsp if (err)
2897 941e9f74 2019-05-21 stsp break;
2898 941e9f74 2019-05-21 stsp
2899 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2900 941e9f74 2019-05-21 stsp free(tree_id);
2901 941e9f74 2019-05-21 stsp if (err)
2902 941e9f74 2019-05-21 stsp break;
2903 941e9f74 2019-05-21 stsp
2904 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2905 941e9f74 2019-05-21 stsp if (err) {
2906 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2907 941e9f74 2019-05-21 stsp break;
2908 941e9f74 2019-05-21 stsp }
2909 941e9f74 2019-05-21 stsp if (slash == NULL)
2910 941e9f74 2019-05-21 stsp break;
2911 941e9f74 2019-05-21 stsp free(subpath);
2912 941e9f74 2019-05-21 stsp subpath = NULL;
2913 941e9f74 2019-05-21 stsp p = slash;
2914 941e9f74 2019-05-21 stsp }
2915 941e9f74 2019-05-21 stsp
2916 941e9f74 2019-05-21 stsp free(subpath);
2917 1a76625f 2018-10-22 stsp return err;
2918 61266923 2020-01-14 stsp }
2919 61266923 2020-01-14 stsp
2920 61266923 2020-01-14 stsp static const struct got_error *
2921 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2922 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2923 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2924 55cccc34 2020-02-20 stsp {
2925 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2926 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2927 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2928 55cccc34 2020-02-20 stsp
2929 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2930 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2931 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2932 55cccc34 2020-02-20 stsp
2933 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2934 bc573f3b 2021-07-10 stsp if (err)
2935 55cccc34 2020-02-20 stsp return err;
2936 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2937 55cccc34 2020-02-20 stsp
2938 55cccc34 2020-02-20 stsp *new_view = tree_view;
2939 55cccc34 2020-02-20 stsp
2940 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2941 55cccc34 2020-02-20 stsp return NULL;
2942 55cccc34 2020-02-20 stsp
2943 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2944 55cccc34 2020-02-20 stsp }
2945 55cccc34 2020-02-20 stsp
2946 55cccc34 2020-02-20 stsp static const struct got_error *
2947 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2948 61266923 2020-01-14 stsp {
2949 61266923 2020-01-14 stsp sigset_t sigset;
2950 61266923 2020-01-14 stsp int errcode;
2951 61266923 2020-01-14 stsp
2952 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2953 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2954 61266923 2020-01-14 stsp
2955 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2956 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2957 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2958 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2959 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2960 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2961 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2962 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2963 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2964 61266923 2020-01-14 stsp
2965 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2966 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2967 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2968 61266923 2020-01-14 stsp
2969 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2970 61266923 2020-01-14 stsp if (errcode)
2971 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2972 61266923 2020-01-14 stsp
2973 61266923 2020-01-14 stsp return NULL;
2974 1a76625f 2018-10-22 stsp }
2975 1a76625f 2018-10-22 stsp
2976 1a76625f 2018-10-22 stsp static void *
2977 1a76625f 2018-10-22 stsp log_thread(void *arg)
2978 1a76625f 2018-10-22 stsp {
2979 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2980 1a76625f 2018-10-22 stsp int errcode = 0;
2981 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2982 1a76625f 2018-10-22 stsp int done = 0;
2983 61266923 2020-01-14 stsp
2984 f2d749db 2022-07-12 thomas /*
2985 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2986 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2987 f2d749db 2022-07-12 thomas */
2988 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2989 f2d749db 2022-07-12 thomas if (errcode) {
2990 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2991 61266923 2020-01-14 stsp return (void *)err;
2992 f2d749db 2022-07-12 thomas }
2993 1a76625f 2018-10-22 stsp
2994 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2995 f2d749db 2022-07-12 thomas if (err) {
2996 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2997 f2d749db 2022-07-12 thomas goto done;
2998 f2d749db 2022-07-12 thomas }
2999 f2d749db 2022-07-12 thomas
3000 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
3001 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3002 f2d749db 2022-07-12 thomas if (errcode) {
3003 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
3004 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
3005 f2d749db 2022-07-12 thomas goto done;
3006 f2d749db 2022-07-12 thomas }
3007 4e0d2870 2020-12-07 naddy err = queue_commits(a);
3008 1a76625f 2018-10-22 stsp if (err) {
3009 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
3010 f2d749db 2022-07-12 thomas goto done;
3011 1a76625f 2018-10-22 stsp err = NULL;
3012 1a76625f 2018-10-22 stsp done = 1;
3013 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
3014 7e8004ba 2022-09-11 thomas if (*a->limiting) {
3015 7e8004ba 2022-09-11 thomas if (a->limit_match)
3016 7e8004ba 2022-09-11 thomas a->commits_needed--;
3017 7e8004ba 2022-09-11 thomas } else
3018 7e8004ba 2022-09-11 thomas a->commits_needed--;
3019 7e8004ba 2022-09-11 thomas }
3020 1a76625f 2018-10-22 stsp
3021 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3022 3abe8080 2019-04-10 stsp if (errcode) {
3023 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
3024 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3025 f2d749db 2022-07-12 thomas goto done;
3026 3abe8080 2019-04-10 stsp } else if (*a->quit)
3027 1a76625f 2018-10-22 stsp done = 1;
3028 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
3029 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
3030 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
3031 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
3032 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
3033 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
3034 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
3035 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
3036 1a76625f 2018-10-22 stsp }
3037 1a76625f 2018-10-22 stsp
3038 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
3039 7c1452c1 2020-03-26 stsp if (errcode) {
3040 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3041 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
3042 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
3043 f2d749db 2022-07-12 thomas goto done;
3044 7c1452c1 2020-03-26 stsp }
3045 7c1452c1 2020-03-26 stsp
3046 1a76625f 2018-10-22 stsp if (done)
3047 1a76625f 2018-10-22 stsp a->commits_needed = 0;
3048 7c1452c1 2020-03-26 stsp else {
3049 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
3050 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
3051 7c1452c1 2020-03-26 stsp &tog_mutex);
3052 f2d749db 2022-07-12 thomas if (errcode) {
3053 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
3054 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
3055 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
3056 f2d749db 2022-07-12 thomas goto done;
3057 f2d749db 2022-07-12 thomas }
3058 21355643 2020-12-06 stsp if (*a->quit)
3059 21355643 2020-12-06 stsp done = 1;
3060 7c1452c1 2020-03-26 stsp }
3061 1a76625f 2018-10-22 stsp }
3062 1a76625f 2018-10-22 stsp }
3063 3abe8080 2019-04-10 stsp a->log_complete = 1;
3064 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
3065 f2d749db 2022-07-12 thomas if (errcode)
3066 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3067 f2d749db 2022-07-12 thomas done:
3068 f2d749db 2022-07-12 thomas if (err) {
3069 f2d749db 2022-07-12 thomas tog_thread_error = 1;
3070 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
3071 f2d749db 2022-07-12 thomas }
3072 1a76625f 2018-10-22 stsp return (void *)err;
3073 1a76625f 2018-10-22 stsp }
3074 1a76625f 2018-10-22 stsp
3075 1a76625f 2018-10-22 stsp static const struct got_error *
3076 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
3077 1a76625f 2018-10-22 stsp {
3078 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
3079 1a76625f 2018-10-22 stsp int errcode;
3080 1a76625f 2018-10-22 stsp
3081 1a76625f 2018-10-22 stsp if (s->thread) {
3082 1a76625f 2018-10-22 stsp s->quit = 1;
3083 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
3084 1a76625f 2018-10-22 stsp if (errcode)
3085 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3086 2af4a041 2019-05-11 jcs "pthread_cond_signal");
3087 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3088 1a76625f 2018-10-22 stsp if (errcode)
3089 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3090 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3091 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
3092 1a76625f 2018-10-22 stsp if (errcode)
3093 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3094 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3095 1a76625f 2018-10-22 stsp if (errcode)
3096 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3097 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3098 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
3099 1a76625f 2018-10-22 stsp }
3100 1a76625f 2018-10-22 stsp
3101 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
3102 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
3103 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
3104 1af5eddf 2022-06-23 thomas }
3105 1af5eddf 2022-06-23 thomas
3106 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
3107 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
3108 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
3109 1af5eddf 2022-06-23 thomas if (err == NULL)
3110 1af5eddf 2022-06-23 thomas err = pack_err;
3111 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
3112 1a76625f 2018-10-22 stsp }
3113 1a76625f 2018-10-22 stsp
3114 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
3115 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
3116 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
3117 1a76625f 2018-10-22 stsp }
3118 1a76625f 2018-10-22 stsp
3119 f2d749db 2022-07-12 thomas return err ? err : thread_err;
3120 9343a5fb 2018-06-23 stsp }
3121 9343a5fb 2018-06-23 stsp
3122 9343a5fb 2018-06-23 stsp static const struct got_error *
3123 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
3124 1a76625f 2018-10-22 stsp {
3125 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
3126 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
3127 276b94a1 2020-11-13 naddy int errcode;
3128 1a76625f 2018-10-22 stsp
3129 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
3130 276b94a1 2020-11-13 naddy
3131 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3132 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3133 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3134 276b94a1 2020-11-13 naddy
3135 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3136 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
3137 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
3138 276b94a1 2020-11-13 naddy
3139 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3140 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3141 1a76625f 2018-10-22 stsp free(s->in_repo_path);
3142 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
3143 1a76625f 2018-10-22 stsp free(s->start_id);
3144 797bc7b9 2018-10-22 stsp s->start_id = NULL;
3145 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
3146 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
3147 1a76625f 2018-10-22 stsp return err;
3148 1a76625f 2018-10-22 stsp }
3149 1a76625f 2018-10-22 stsp
3150 7e8004ba 2022-09-11 thomas /*
3151 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
3152 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
3153 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
3154 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
3155 7e8004ba 2022-09-11 thomas * works with very slight change.
3156 7e8004ba 2022-09-11 thomas */
3157 1a76625f 2018-10-22 stsp static const struct got_error *
3158 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
3159 7e8004ba 2022-09-11 thomas {
3160 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
3161 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
3162 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
3163 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
3164 7e8004ba 2022-09-11 thomas char pattern[1024];
3165 7e8004ba 2022-09-11 thomas int ret;
3166 7e8004ba 2022-09-11 thomas
3167 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
3168 7e8004ba 2022-09-11 thomas v = view->child;
3169 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3170 7e8004ba 2022-09-11 thomas v = view->parent;
3171 7e8004ba 2022-09-11 thomas
3172 7e8004ba 2022-09-11 thomas /* Get the pattern */
3173 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
3174 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
3175 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3176 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
3177 7e8004ba 2022-09-11 thomas nocbreak();
3178 7e8004ba 2022-09-11 thomas echo();
3179 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
3180 7e8004ba 2022-09-11 thomas cbreak();
3181 7e8004ba 2022-09-11 thomas noecho();
3182 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
3183 7e8004ba 2022-09-11 thomas if (ret == ERR)
3184 7e8004ba 2022-09-11 thomas return NULL;
3185 7e8004ba 2022-09-11 thomas
3186 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
3187 7e8004ba 2022-09-11 thomas /*
3188 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
3189 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
3190 7e8004ba 2022-09-11 thomas */
3191 7e8004ba 2022-09-11 thomas if (!s->limit_view)
3192 7e8004ba 2022-09-11 thomas return NULL;
3193 7e8004ba 2022-09-11 thomas
3194 7e8004ba 2022-09-11 thomas /*
3195 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3196 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3197 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3198 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3199 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3200 7e8004ba 2022-09-11 thomas * the queue.
3201 7e8004ba 2022-09-11 thomas */
3202 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3203 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3204 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3205 7e8004ba 2022-09-11 thomas s->selected = 0;
3206 7e8004ba 2022-09-11 thomas s->limit_view = 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 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3212 7e8004ba 2022-09-11 thomas return NULL;
3213 7e8004ba 2022-09-11 thomas
3214 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3215 7e8004ba 2022-09-11 thomas
3216 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3217 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3218 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3219 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3220 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3221 7e8004ba 2022-09-11 thomas
3222 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3223 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3224 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3225 7e8004ba 2022-09-11 thomas
3226 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3227 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3228 7e8004ba 2022-09-11 thomas int have_match = 0;
3229 7e8004ba 2022-09-11 thomas
3230 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3231 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3232 7e8004ba 2022-09-11 thomas if (err)
3233 7e8004ba 2022-09-11 thomas return err;
3234 7e8004ba 2022-09-11 thomas
3235 7e8004ba 2022-09-11 thomas if (have_match) {
3236 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3237 7e8004ba 2022-09-11 thomas
3238 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3239 7e8004ba 2022-09-11 thomas entry->id);
3240 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3241 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3242 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3243 7e8004ba 2022-09-11 thomas break;
3244 7e8004ba 2022-09-11 thomas }
3245 6f6c25d6 2022-09-18 thomas matched->commit = entry->commit;
3246 6f6c25d6 2022-09-18 thomas got_object_commit_retain(entry->commit);
3247 7e8004ba 2022-09-11 thomas
3248 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3249 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3250 7e8004ba 2022-09-11 thomas matched, entry);
3251 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3252 7e8004ba 2022-09-11 thomas }
3253 7e8004ba 2022-09-11 thomas }
3254 7e8004ba 2022-09-11 thomas
3255 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3256 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3257 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3258 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3259 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3260 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3261 7e8004ba 2022-09-11 thomas if (err)
3262 7e8004ba 2022-09-11 thomas return err;
3263 7e8004ba 2022-09-11 thomas }
3264 7e8004ba 2022-09-11 thomas
3265 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3266 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3267 7e8004ba 2022-09-11 thomas s->selected = 0;
3268 7e8004ba 2022-09-11 thomas
3269 7e8004ba 2022-09-11 thomas return NULL;
3270 7e8004ba 2022-09-11 thomas }
3271 7e8004ba 2022-09-11 thomas
3272 7e8004ba 2022-09-11 thomas static const struct got_error *
3273 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3274 60493ae3 2019-06-20 stsp {
3275 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3276 60493ae3 2019-06-20 stsp
3277 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3278 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3279 60493ae3 2019-06-20 stsp return NULL;
3280 60493ae3 2019-06-20 stsp }
3281 60493ae3 2019-06-20 stsp
3282 60493ae3 2019-06-20 stsp static const struct got_error *
3283 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3284 60493ae3 2019-06-20 stsp {
3285 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3286 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3287 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3288 60493ae3 2019-06-20 stsp
3289 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3290 f9686aa5 2020-03-27 stsp show_log_view(view);
3291 f9686aa5 2020-03-27 stsp update_panels();
3292 f9686aa5 2020-03-27 stsp doupdate();
3293 f9686aa5 2020-03-27 stsp
3294 96e2b566 2019-07-08 stsp if (s->search_entry) {
3295 13add988 2019-10-15 stsp int errcode, ch;
3296 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3297 13add988 2019-10-15 stsp if (errcode)
3298 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3299 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3300 13add988 2019-10-15 stsp ch = wgetch(view->window);
3301 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3302 13add988 2019-10-15 stsp if (errcode)
3303 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3304 13add988 2019-10-15 stsp "pthread_mutex_lock");
3305 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3306 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3307 678cbce5 2019-07-28 stsp return NULL;
3308 678cbce5 2019-07-28 stsp }
3309 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3310 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3311 96e2b566 2019-07-08 stsp else
3312 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3313 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3314 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3315 df5e841d 2022-06-23 thomas /*
3316 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3317 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3318 1f4d5162 2022-06-23 thomas * might have changed.
3319 df5e841d 2022-06-23 thomas */
3320 e7baaec8 2022-09-13 thomas if (view->searching == TOG_SEARCH_FORWARD)
3321 e7baaec8 2022-09-13 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3322 e7baaec8 2022-09-13 thomas else
3323 e7baaec8 2022-09-13 thomas entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3324 e7baaec8 2022-09-13 thomas entry);
3325 20be8d96 2019-06-21 stsp } else {
3326 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3327 20be8d96 2019-06-21 stsp }
3328 60493ae3 2019-06-20 stsp
3329 60493ae3 2019-06-20 stsp while (1) {
3330 13add988 2019-10-15 stsp int have_match = 0;
3331 13add988 2019-10-15 stsp
3332 60493ae3 2019-06-20 stsp if (entry == NULL) {
3333 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3334 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3335 f9967bca 2020-03-27 stsp view->search_next_done =
3336 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3337 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3338 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3339 f801134a 2019-06-25 stsp return NULL;
3340 60493ae3 2019-06-20 stsp }
3341 96e2b566 2019-07-08 stsp /*
3342 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3343 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3344 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3345 96e2b566 2019-07-08 stsp */
3346 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3347 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3348 60493ae3 2019-06-20 stsp }
3349 60493ae3 2019-06-20 stsp
3350 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3351 13add988 2019-10-15 stsp &view->regex);
3352 5943eee2 2019-08-13 stsp if (err)
3353 13add988 2019-10-15 stsp break;
3354 13add988 2019-10-15 stsp if (have_match) {
3355 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3356 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3357 60493ae3 2019-06-20 stsp break;
3358 60493ae3 2019-06-20 stsp }
3359 13add988 2019-10-15 stsp
3360 96e2b566 2019-07-08 stsp s->search_entry = entry;
3361 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3362 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3363 b1bf1435 2019-06-21 stsp else
3364 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3365 60493ae3 2019-06-20 stsp }
3366 60493ae3 2019-06-20 stsp
3367 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3368 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3369 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3370 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3371 60493ae3 2019-06-20 stsp if (err)
3372 60493ae3 2019-06-20 stsp return err;
3373 ead14cbe 2019-06-21 stsp cur++;
3374 ead14cbe 2019-06-21 stsp }
3375 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3376 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3377 60493ae3 2019-06-20 stsp if (err)
3378 60493ae3 2019-06-20 stsp return err;
3379 ead14cbe 2019-06-21 stsp cur--;
3380 60493ae3 2019-06-20 stsp }
3381 60493ae3 2019-06-20 stsp }
3382 60493ae3 2019-06-20 stsp
3383 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3384 96e2b566 2019-07-08 stsp
3385 60493ae3 2019-06-20 stsp return NULL;
3386 60493ae3 2019-06-20 stsp }
3387 60493ae3 2019-06-20 stsp
3388 60493ae3 2019-06-20 stsp static const struct got_error *
3389 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3390 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3391 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3392 80ddbec8 2018-04-29 stsp {
3393 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3394 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3395 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3396 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3397 1a76625f 2018-10-22 stsp int errcode;
3398 80ddbec8 2018-04-29 stsp
3399 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3400 f135c941 2020-02-20 stsp free(s->in_repo_path);
3401 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3402 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3403 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3404 f135c941 2020-02-20 stsp }
3405 ecb28ae0 2018-07-16 stsp
3406 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3407 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3408 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3409 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3410 78756c87 2020-11-24 stsp
3411 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3412 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3413 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3414 7e8004ba 2022-09-11 thomas
3415 fb2756b9 2018-08-04 stsp s->repo = repo;
3416 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3417 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3418 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3419 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3420 9cd7cbd1 2020-12-07 stsp goto done;
3421 9cd7cbd1 2020-12-07 stsp }
3422 9cd7cbd1 2020-12-07 stsp }
3423 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3424 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3425 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3426 5036bf37 2018-09-24 stsp goto done;
3427 5036bf37 2018-09-24 stsp }
3428 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3429 8eca0bdb 2023-01-02 thomas s->use_committer = 1;
3430 e5a0f69f 2018-08-18 stsp
3431 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3432 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3433 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3434 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3435 11b20872 2019-11-08 stsp if (err)
3436 11b20872 2019-11-08 stsp goto done;
3437 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3438 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3439 11b20872 2019-11-08 stsp if (err) {
3440 11b20872 2019-11-08 stsp free_colors(&s->colors);
3441 11b20872 2019-11-08 stsp goto done;
3442 11b20872 2019-11-08 stsp }
3443 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3444 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3445 11b20872 2019-11-08 stsp if (err) {
3446 11b20872 2019-11-08 stsp free_colors(&s->colors);
3447 11b20872 2019-11-08 stsp goto done;
3448 11b20872 2019-11-08 stsp }
3449 11b20872 2019-11-08 stsp }
3450 11b20872 2019-11-08 stsp
3451 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3452 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3453 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3454 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3455 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3456 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3457 1a76625f 2018-10-22 stsp
3458 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3459 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3460 1af5eddf 2022-06-23 thomas if (err)
3461 1af5eddf 2022-06-23 thomas goto done;
3462 1af5eddf 2022-06-23 thomas }
3463 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3464 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3465 7cd52833 2022-06-23 thomas if (err)
3466 7cd52833 2022-06-23 thomas goto done;
3467 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3468 b672a97a 2020-01-27 stsp !s->log_branches);
3469 1a76625f 2018-10-22 stsp if (err)
3470 1a76625f 2018-10-22 stsp goto done;
3471 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3472 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3473 c5b78334 2020-01-12 stsp if (err)
3474 c5b78334 2020-01-12 stsp goto done;
3475 1a76625f 2018-10-22 stsp
3476 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3477 1a76625f 2018-10-22 stsp if (errcode) {
3478 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3479 1a76625f 2018-10-22 stsp goto done;
3480 1a76625f 2018-10-22 stsp }
3481 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3482 7c1452c1 2020-03-26 stsp if (errcode) {
3483 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3484 7c1452c1 2020-03-26 stsp goto done;
3485 7c1452c1 2020-03-26 stsp }
3486 1a76625f 2018-10-22 stsp
3487 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3488 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3489 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3490 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3491 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3492 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3493 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3494 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3495 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3496 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3497 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3498 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3499 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3500 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3501 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3502 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3503 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3504 ba4f502b 2018-08-04 stsp done:
3505 1a76625f 2018-10-22 stsp if (err)
3506 1a76625f 2018-10-22 stsp close_log_view(view);
3507 ba4f502b 2018-08-04 stsp return err;
3508 ba4f502b 2018-08-04 stsp }
3509 ba4f502b 2018-08-04 stsp
3510 e5a0f69f 2018-08-18 stsp static const struct got_error *
3511 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3512 ba4f502b 2018-08-04 stsp {
3513 f2f6d207 2020-11-24 stsp const struct got_error *err;
3514 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3515 ba4f502b 2018-08-04 stsp
3516 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3517 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3518 2b380cc8 2018-10-24 stsp &s->thread_args);
3519 2b380cc8 2018-10-24 stsp if (errcode)
3520 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3521 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3522 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3523 f2f6d207 2020-11-24 stsp if (err)
3524 f2f6d207 2020-11-24 stsp return err;
3525 f2f6d207 2020-11-24 stsp }
3526 2b380cc8 2018-10-24 stsp }
3527 2b380cc8 2018-10-24 stsp
3528 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3529 b31f89ff 2022-07-01 thomas }
3530 b31f89ff 2022-07-01 thomas
3531 b31f89ff 2022-07-01 thomas static void
3532 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3533 b31f89ff 2022-07-01 thomas {
3534 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3535 b31f89ff 2022-07-01 thomas
3536 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3537 b31f89ff 2022-07-01 thomas return;
3538 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3539 7e8004ba 2022-09-11 thomas view->count = 0;
3540 b31f89ff 2022-07-01 thomas
3541 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3542 b31f89ff 2022-07-01 thomas || home)
3543 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3544 b31f89ff 2022-07-01 thomas
3545 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3546 b31f89ff 2022-07-01 thomas --s->selected;
3547 b31f89ff 2022-07-01 thomas else
3548 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3549 b31f89ff 2022-07-01 thomas
3550 b31f89ff 2022-07-01 thomas select_commit(s);
3551 b31f89ff 2022-07-01 thomas return;
3552 b31f89ff 2022-07-01 thomas }
3553 b31f89ff 2022-07-01 thomas
3554 b31f89ff 2022-07-01 thomas static const struct got_error *
3555 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3556 b31f89ff 2022-07-01 thomas {
3557 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3558 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3559 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3560 b31f89ff 2022-07-01 thomas
3561 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3562 7e8004ba 2022-09-11 thomas return NULL;
3563 7e8004ba 2022-09-11 thomas
3564 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3565 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3566 b31f89ff 2022-07-01 thomas return NULL;
3567 b31f89ff 2022-07-01 thomas
3568 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3569 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3570 b31f89ff 2022-07-01 thomas
3571 7ed048bd 2022-08-12 thomas if (!page) {
3572 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3573 b31f89ff 2022-07-01 thomas ++s->selected;
3574 b31f89ff 2022-07-01 thomas else
3575 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3576 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3577 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3578 7ed048bd 2022-08-12 thomas int n;
3579 7ed048bd 2022-08-12 thomas
3580 7ed048bd 2022-08-12 thomas s->selected = 0;
3581 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3582 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3583 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3584 7ed048bd 2022-08-12 thomas if (entry == NULL)
3585 7ed048bd 2022-08-12 thomas break;
3586 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3587 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3588 7ed048bd 2022-08-12 thomas }
3589 7ed048bd 2022-08-12 thomas if (n > 0)
3590 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3591 b31f89ff 2022-07-01 thomas } else {
3592 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3593 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3594 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3595 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3596 bd3f8225 2022-08-12 thomas else
3597 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3598 b31f89ff 2022-07-01 thomas }
3599 b31f89ff 2022-07-01 thomas if (err)
3600 b31f89ff 2022-07-01 thomas return err;
3601 b31f89ff 2022-07-01 thomas
3602 a5d43cac 2022-07-01 thomas /*
3603 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3604 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3605 a5d43cac 2022-07-01 thomas */
3606 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3607 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3608 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3609 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3610 25100026 2022-08-13 thomas }
3611 a5d43cac 2022-07-01 thomas
3612 b31f89ff 2022-07-01 thomas select_commit(s);
3613 b31f89ff 2022-07-01 thomas
3614 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3615 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3616 b31f89ff 2022-07-01 thomas view->count = 0;
3617 b31f89ff 2022-07-01 thomas
3618 b31f89ff 2022-07-01 thomas return NULL;
3619 e5a0f69f 2018-08-18 stsp }
3620 04cc582a 2018-08-01 stsp
3621 a5d43cac 2022-07-01 thomas static void
3622 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3623 a5d43cac 2022-07-01 thomas {
3624 24415785 2022-07-03 thomas *x = 0;
3625 24415785 2022-07-03 thomas *y = 0;
3626 24415785 2022-07-03 thomas
3627 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3628 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3629 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3630 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3631 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3632 53d2bdd3 2022-07-10 thomas else
3633 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3634 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3635 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3636 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3637 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3638 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3639 53d2bdd3 2022-07-10 thomas else
3640 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3641 53d2bdd3 2022-07-10 thomas }
3642 a5d43cac 2022-07-01 thomas }
3643 a5d43cac 2022-07-01 thomas
3644 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3645 e5a0f69f 2018-08-18 stsp static const struct got_error *
3646 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3647 a5d43cac 2022-07-01 thomas {
3648 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3649 a5d43cac 2022-07-01 thomas
3650 a5d43cac 2022-07-01 thomas view->nlines = y;
3651 64486692 2022-07-07 thomas view->ncols = COLS;
3652 a5d43cac 2022-07-01 thomas err = view_resize(view);
3653 a5d43cac 2022-07-01 thomas if (err)
3654 a5d43cac 2022-07-01 thomas return err;
3655 a5d43cac 2022-07-01 thomas
3656 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3657 a5d43cac 2022-07-01 thomas
3658 a5d43cac 2022-07-01 thomas return err;
3659 07dd3ed3 2022-08-06 thomas }
3660 07dd3ed3 2022-08-06 thomas
3661 07dd3ed3 2022-08-06 thomas static const struct got_error *
3662 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3663 07dd3ed3 2022-08-06 thomas {
3664 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3665 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3666 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3667 25100026 2022-08-13 thomas
3668 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3669 25100026 2022-08-13 thomas return NULL;
3670 07dd3ed3 2022-08-06 thomas
3671 07dd3ed3 2022-08-06 thomas g = view->gline;
3672 07dd3ed3 2022-08-06 thomas view->gline = 0;
3673 07dd3ed3 2022-08-06 thomas
3674 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3675 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3676 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3677 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3678 07dd3ed3 2022-08-06 thomas select_commit(s);
3679 07dd3ed3 2022-08-06 thomas return NULL;
3680 07dd3ed3 2022-08-06 thomas }
3681 07dd3ed3 2022-08-06 thomas
3682 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3683 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3684 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3685 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3686 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3687 07dd3ed3 2022-08-06 thomas if (err)
3688 07dd3ed3 2022-08-06 thomas return err;
3689 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3690 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3691 07dd3ed3 2022-08-06 thomas
3692 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3693 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3694 07dd3ed3 2022-08-06 thomas
3695 07dd3ed3 2022-08-06 thomas select_commit(s);
3696 07dd3ed3 2022-08-06 thomas return NULL;
3697 c72de8ab 2023-02-03 thomas
3698 c72de8ab 2023-02-03 thomas }
3699 c72de8ab 2023-02-03 thomas
3700 c72de8ab 2023-02-03 thomas static void
3701 c72de8ab 2023-02-03 thomas horizontal_scroll_input(struct tog_view *view, int ch)
3702 c72de8ab 2023-02-03 thomas {
3703 07dd3ed3 2022-08-06 thomas
3704 c72de8ab 2023-02-03 thomas switch (ch) {
3705 c72de8ab 2023-02-03 thomas case KEY_LEFT:
3706 c72de8ab 2023-02-03 thomas case 'h':
3707 c72de8ab 2023-02-03 thomas view->x -= MIN(view->x, 2);
3708 c72de8ab 2023-02-03 thomas if (view->x <= 0)
3709 c72de8ab 2023-02-03 thomas view->count = 0;
3710 c72de8ab 2023-02-03 thomas break;
3711 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
3712 c72de8ab 2023-02-03 thomas case 'l':
3713 c72de8ab 2023-02-03 thomas if (view->x + view->ncols / 2 < view->maxx)
3714 c72de8ab 2023-02-03 thomas view->x += 2;
3715 c72de8ab 2023-02-03 thomas else
3716 c72de8ab 2023-02-03 thomas view->count = 0;
3717 c72de8ab 2023-02-03 thomas break;
3718 c72de8ab 2023-02-03 thomas case '0':
3719 c72de8ab 2023-02-03 thomas view->x = 0;
3720 c72de8ab 2023-02-03 thomas break;
3721 c72de8ab 2023-02-03 thomas case '$':
3722 c72de8ab 2023-02-03 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3723 c72de8ab 2023-02-03 thomas view->count = 0;
3724 c72de8ab 2023-02-03 thomas break;
3725 c72de8ab 2023-02-03 thomas default:
3726 c72de8ab 2023-02-03 thomas break;
3727 c72de8ab 2023-02-03 thomas }
3728 a5d43cac 2022-07-01 thomas }
3729 a5d43cac 2022-07-01 thomas
3730 a5d43cac 2022-07-01 thomas static const struct got_error *
3731 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3732 e5a0f69f 2018-08-18 stsp {
3733 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3734 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3735 7ed048bd 2022-08-12 thomas int eos, nscroll;
3736 80ddbec8 2018-04-29 stsp
3737 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3738 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3739 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3740 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3741 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3742 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3743 fb280deb 2021-08-30 stsp }
3744 c0be8933 2022-08-12 thomas if (err)
3745 c0be8933 2022-08-12 thomas return err;
3746 528dedf3 2021-08-30 stsp }
3747 068ab281 2022-07-01 thomas
3748 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3749 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3750 068ab281 2022-07-01 thomas --eos; /* border */
3751 07dd3ed3 2022-08-06 thomas
3752 07dd3ed3 2022-08-06 thomas if (view->gline)
3753 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3754 068ab281 2022-07-01 thomas
3755 528dedf3 2021-08-30 stsp switch (ch) {
3756 7e8004ba 2022-09-11 thomas case '&':
3757 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3758 7e8004ba 2022-09-11 thomas break;
3759 1e37a5c2 2019-05-12 jcs case 'q':
3760 1e37a5c2 2019-05-12 jcs s->quit = 1;
3761 05171be4 2022-06-23 thomas break;
3762 05171be4 2022-06-23 thomas case '0':
3763 05171be4 2022-06-23 thomas case '$':
3764 05171be4 2022-06-23 thomas case KEY_RIGHT:
3765 05171be4 2022-06-23 thomas case 'l':
3766 05171be4 2022-06-23 thomas case KEY_LEFT:
3767 05171be4 2022-06-23 thomas case 'h':
3768 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
3769 05171be4 2022-06-23 thomas break;
3770 1e37a5c2 2019-05-12 jcs case 'k':
3771 1e37a5c2 2019-05-12 jcs case KEY_UP:
3772 1e37a5c2 2019-05-12 jcs case '<':
3773 1e37a5c2 2019-05-12 jcs case ',':
3774 f7140bf5 2021-10-17 thomas case CTRL('p'):
3775 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3776 912a3f79 2021-08-30 j break;
3777 912a3f79 2021-08-30 j case 'g':
3778 aa7a1117 2023-01-09 thomas case '=':
3779 912a3f79 2021-08-30 j case KEY_HOME:
3780 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3781 07b0611c 2022-06-23 thomas view->count = 0;
3782 1e37a5c2 2019-05-12 jcs break;
3783 70f17a53 2022-06-13 thomas case CTRL('u'):
3784 23427b14 2022-06-23 thomas case 'u':
3785 70f17a53 2022-06-13 thomas nscroll /= 2;
3786 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3787 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3788 a4292ac5 2019-05-12 jcs case CTRL('b'):
3789 1c5e5faa 2022-06-23 thomas case 'b':
3790 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3791 1e37a5c2 2019-05-12 jcs break;
3792 1e37a5c2 2019-05-12 jcs case 'j':
3793 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3794 1e37a5c2 2019-05-12 jcs case '>':
3795 1e37a5c2 2019-05-12 jcs case '.':
3796 f7140bf5 2021-10-17 thomas case CTRL('n'):
3797 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3798 912a3f79 2021-08-30 j break;
3799 f69c5a46 2022-07-19 thomas case '@':
3800 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3801 f2d06bef 2023-01-23 thomas view->action = s->use_committer ?
3802 f2d06bef 2023-01-23 thomas "show committer" : "show commit author";
3803 f69c5a46 2022-07-19 thomas break;
3804 912a3f79 2021-08-30 j case 'G':
3805 aa7a1117 2023-01-09 thomas case '*':
3806 912a3f79 2021-08-30 j case KEY_END: {
3807 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3808 912a3f79 2021-08-30 j * traverse them all. */
3809 07b0611c 2022-06-23 thomas view->count = 0;
3810 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3811 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3812 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3813 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3814 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3815 1e37a5c2 2019-05-12 jcs break;
3816 912a3f79 2021-08-30 j }
3817 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3818 23427b14 2022-06-23 thomas case 'd':
3819 70f17a53 2022-06-13 thomas nscroll /= 2;
3820 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3821 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3822 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3823 4c2d69cb 2022-06-23 thomas case 'f':
3824 b31f89ff 2022-07-01 thomas case ' ':
3825 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3826 1e37a5c2 2019-05-12 jcs break;
3827 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3828 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3829 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3830 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3831 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3832 2b779855 2020-12-05 naddy select_commit(s);
3833 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3834 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3835 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3836 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3837 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3838 0bf7f153 2020-12-02 naddy }
3839 1e37a5c2 2019-05-12 jcs break;
3840 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3841 444d5325 2022-07-03 thomas case '\r':
3842 07b0611c 2022-06-23 thomas view->count = 0;
3843 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3844 e5a0f69f 2018-08-18 stsp break;
3845 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3846 1e37a5c2 2019-05-12 jcs break;
3847 1be4947a 2022-07-22 thomas case 'T':
3848 07b0611c 2022-06-23 thomas view->count = 0;
3849 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3850 5036bf37 2018-09-24 stsp break;
3851 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3852 1e37a5c2 2019-05-12 jcs break;
3853 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3854 21355643 2020-12-06 stsp case CTRL('l'):
3855 21355643 2020-12-06 stsp case 'B':
3856 07b0611c 2022-06-23 thomas view->count = 0;
3857 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3858 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3859 1e37a5c2 2019-05-12 jcs break;
3860 21355643 2020-12-06 stsp err = stop_log_thread(s);
3861 74cfe85e 2020-10-20 stsp if (err)
3862 74cfe85e 2020-10-20 stsp return err;
3863 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3864 21355643 2020-12-06 stsp char *parent_path;
3865 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3866 21355643 2020-12-06 stsp if (err)
3867 21355643 2020-12-06 stsp return err;
3868 21355643 2020-12-06 stsp free(s->in_repo_path);
3869 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3870 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3871 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3872 21355643 2020-12-06 stsp struct got_object_id *start_id;
3873 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3874 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3875 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3876 466429a1 2022-11-17 thomas if (err) {
3877 466429a1 2022-11-17 thomas if (s->head_ref_name == NULL ||
3878 466429a1 2022-11-17 thomas err->code != GOT_ERR_NOT_REF)
3879 466429a1 2022-11-17 thomas return err;
3880 466429a1 2022-11-17 thomas /* Try to cope with deleted references. */
3881 466429a1 2022-11-17 thomas free(s->head_ref_name);
3882 466429a1 2022-11-17 thomas s->head_ref_name = NULL;
3883 466429a1 2022-11-17 thomas err = got_repo_match_object_id(&start_id,
3884 466429a1 2022-11-17 thomas NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3885 466429a1 2022-11-17 thomas &tog_refs, s->repo);
3886 466429a1 2022-11-17 thomas if (err)
3887 466429a1 2022-11-17 thomas return err;
3888 466429a1 2022-11-17 thomas }
3889 21355643 2020-12-06 stsp free(s->start_id);
3890 21355643 2020-12-06 stsp s->start_id = start_id;
3891 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3892 21355643 2020-12-06 stsp } else /* 'B' */
3893 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3894 21355643 2020-12-06 stsp
3895 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3896 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3897 bf7e79b3 2022-06-23 thomas if (err)
3898 bf7e79b3 2022-06-23 thomas return err;
3899 bf7e79b3 2022-06-23 thomas }
3900 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3901 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3902 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3903 74cfe85e 2020-10-20 stsp if (err)
3904 21355643 2020-12-06 stsp return err;
3905 51a10b52 2020-12-26 stsp tog_free_refs();
3906 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3907 ca51c541 2020-12-07 stsp if (err)
3908 ca51c541 2020-12-07 stsp return err;
3909 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3910 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3911 d01904d4 2019-06-25 stsp if (err)
3912 d01904d4 2019-06-25 stsp return err;
3913 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3914 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3915 b672a97a 2020-01-27 stsp if (err)
3916 b672a97a 2020-01-27 stsp return err;
3917 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3918 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3919 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3920 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3921 21355643 2020-12-06 stsp s->selected_entry = NULL;
3922 21355643 2020-12-06 stsp s->selected = 0;
3923 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3924 21355643 2020-12-06 stsp s->quit = 0;
3925 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3926 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3927 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3928 94ecf40d 2022-07-22 thomas view->offset = 0;
3929 d01904d4 2019-06-25 stsp break;
3930 1be4947a 2022-07-22 thomas case 'R':
3931 07b0611c 2022-06-23 thomas view->count = 0;
3932 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3933 6458efa5 2020-11-24 stsp break;
3934 1e37a5c2 2019-05-12 jcs default:
3935 07b0611c 2022-06-23 thomas view->count = 0;
3936 1e37a5c2 2019-05-12 jcs break;
3937 899d86c2 2018-05-10 stsp }
3938 e5a0f69f 2018-08-18 stsp
3939 80ddbec8 2018-04-29 stsp return err;
3940 80ddbec8 2018-04-29 stsp }
3941 80ddbec8 2018-04-29 stsp
3942 4ed7e80c 2018-05-20 stsp static const struct got_error *
3943 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3944 c2db6724 2019-01-04 stsp {
3945 c2db6724 2019-01-04 stsp const struct got_error *error;
3946 c2db6724 2019-01-04 stsp
3947 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3948 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3949 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3950 37c06ea4 2019-07-15 stsp #endif
3951 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3952 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3953 c2db6724 2019-01-04 stsp
3954 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3955 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3956 c2db6724 2019-01-04 stsp
3957 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3958 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3959 c2db6724 2019-01-04 stsp
3960 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3961 c2db6724 2019-01-04 stsp if (error != NULL)
3962 c2db6724 2019-01-04 stsp return error;
3963 c2db6724 2019-01-04 stsp
3964 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3965 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3966 c2db6724 2019-01-04 stsp
3967 c2db6724 2019-01-04 stsp return NULL;
3968 c2db6724 2019-01-04 stsp }
3969 c2db6724 2019-01-04 stsp
3970 a915003a 2019-02-05 stsp static void
3971 a915003a 2019-02-05 stsp init_curses(void)
3972 a915003a 2019-02-05 stsp {
3973 296152d1 2022-05-31 thomas /*
3974 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3975 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3976 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3977 296152d1 2022-05-31 thomas */
3978 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3979 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3980 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3981 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3982 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3983 296152d1 2022-05-31 thomas
3984 a915003a 2019-02-05 stsp initscr();
3985 a915003a 2019-02-05 stsp cbreak();
3986 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3987 a915003a 2019-02-05 stsp noecho();
3988 a915003a 2019-02-05 stsp nonl();
3989 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3990 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3991 a915003a 2019-02-05 stsp curs_set(0);
3992 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3993 6d17833f 2019-11-08 stsp start_color();
3994 6d17833f 2019-11-08 stsp use_default_colors();
3995 6d17833f 2019-11-08 stsp }
3996 a915003a 2019-02-05 stsp }
3997 a915003a 2019-02-05 stsp
3998 c2db6724 2019-01-04 stsp static const struct got_error *
3999 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
4000 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
4001 f135c941 2020-02-20 stsp {
4002 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
4003 f135c941 2020-02-20 stsp
4004 f135c941 2020-02-20 stsp if (argc == 0) {
4005 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
4006 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
4007 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
4008 f135c941 2020-02-20 stsp return NULL;
4009 f135c941 2020-02-20 stsp }
4010 f135c941 2020-02-20 stsp
4011 f135c941 2020-02-20 stsp if (worktree) {
4012 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4013 bfd61697 2020-11-14 stsp char *p;
4014 f135c941 2020-02-20 stsp
4015 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
4016 f135c941 2020-02-20 stsp if (err)
4017 f135c941 2020-02-20 stsp return err;
4018 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
4019 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4020 bfd61697 2020-11-14 stsp p) == -1) {
4021 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
4022 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
4023 f135c941 2020-02-20 stsp }
4024 f135c941 2020-02-20 stsp free(p);
4025 f135c941 2020-02-20 stsp } else
4026 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
4027 f135c941 2020-02-20 stsp
4028 f135c941 2020-02-20 stsp return err;
4029 f135c941 2020-02-20 stsp }
4030 f135c941 2020-02-20 stsp
4031 f135c941 2020-02-20 stsp static const struct got_error *
4032 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
4033 9f7d7167 2018-04-29 stsp {
4034 80ddbec8 2018-04-29 stsp const struct got_error *error;
4035 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
4036 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
4037 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
4038 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
4039 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
4040 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
4041 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
4042 f135c941 2020-02-20 stsp int ch, log_branches = 0;
4043 04cc582a 2018-08-01 stsp struct tog_view *view;
4044 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4045 80ddbec8 2018-04-29 stsp
4046 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
4047 80ddbec8 2018-04-29 stsp switch (ch) {
4048 b672a97a 2020-01-27 stsp case 'b':
4049 b672a97a 2020-01-27 stsp log_branches = 1;
4050 b672a97a 2020-01-27 stsp break;
4051 80ddbec8 2018-04-29 stsp case 'c':
4052 80ddbec8 2018-04-29 stsp start_commit = optarg;
4053 80ddbec8 2018-04-29 stsp break;
4054 ecb28ae0 2018-07-16 stsp case 'r':
4055 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4056 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
4057 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4058 9ba1d308 2019-10-21 stsp optarg);
4059 ecb28ae0 2018-07-16 stsp break;
4060 80ddbec8 2018-04-29 stsp default:
4061 17020d27 2019-03-07 stsp usage_log();
4062 80ddbec8 2018-04-29 stsp /* NOTREACHED */
4063 80ddbec8 2018-04-29 stsp }
4064 80ddbec8 2018-04-29 stsp }
4065 80ddbec8 2018-04-29 stsp
4066 80ddbec8 2018-04-29 stsp argc -= optind;
4067 80ddbec8 2018-04-29 stsp argv += optind;
4068 80ddbec8 2018-04-29 stsp
4069 f135c941 2020-02-20 stsp if (argc > 1)
4070 f135c941 2020-02-20 stsp usage_log();
4071 963f97a1 2019-03-18 stsp
4072 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4073 7cd52833 2022-06-23 thomas if (error != NULL)
4074 7cd52833 2022-06-23 thomas goto done;
4075 7cd52833 2022-06-23 thomas
4076 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
4077 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4078 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4079 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4080 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4081 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4082 c156c7a4 2020-12-18 stsp goto done;
4083 a1fbf39a 2019-08-11 stsp if (worktree)
4084 6962eb72 2020-02-20 stsp repo_path =
4085 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
4086 a1fbf39a 2019-08-11 stsp else
4087 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
4088 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4089 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4090 c156c7a4 2020-12-18 stsp goto done;
4091 c156c7a4 2020-12-18 stsp }
4092 ecb28ae0 2018-07-16 stsp }
4093 ecb28ae0 2018-07-16 stsp
4094 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4095 80ddbec8 2018-04-29 stsp if (error != NULL)
4096 ecb28ae0 2018-07-16 stsp goto done;
4097 80ddbec8 2018-04-29 stsp
4098 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4099 f135c941 2020-02-20 stsp repo, worktree);
4100 f135c941 2020-02-20 stsp if (error)
4101 f135c941 2020-02-20 stsp goto done;
4102 f135c941 2020-02-20 stsp
4103 f135c941 2020-02-20 stsp init_curses();
4104 f135c941 2020-02-20 stsp
4105 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
4106 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4107 c02c541e 2019-03-29 stsp if (error)
4108 c02c541e 2019-03-29 stsp goto done;
4109 c02c541e 2019-03-29 stsp
4110 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
4111 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
4112 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4113 87670572 2020-12-26 naddy if (error)
4114 87670572 2020-12-26 naddy goto done;
4115 87670572 2020-12-26 naddy }
4116 51a10b52 2020-12-26 stsp
4117 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
4118 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
4119 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
4120 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4121 d8f38dc4 2020-12-05 stsp if (error)
4122 d8f38dc4 2020-12-05 stsp goto done;
4123 d8f38dc4 2020-12-05 stsp head_ref_name = label;
4124 d8f38dc4 2020-12-05 stsp } else {
4125 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
4126 d8f38dc4 2020-12-05 stsp if (error == NULL)
4127 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
4128 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
4129 d8f38dc4 2020-12-05 stsp goto done;
4130 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
4131 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4132 d8f38dc4 2020-12-05 stsp if (error)
4133 d8f38dc4 2020-12-05 stsp goto done;
4134 d8f38dc4 2020-12-05 stsp }
4135 8b473291 2019-02-21 stsp
4136 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4137 04cc582a 2018-08-01 stsp if (view == NULL) {
4138 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4139 04cc582a 2018-08-01 stsp goto done;
4140 04cc582a 2018-08-01 stsp }
4141 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
4142 f135c941 2020-02-20 stsp in_repo_path, log_branches);
4143 ba4f502b 2018-08-04 stsp if (error)
4144 ba4f502b 2018-08-04 stsp goto done;
4145 2fc00ff4 2019-08-31 stsp if (worktree) {
4146 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
4147 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
4148 2fc00ff4 2019-08-31 stsp worktree = NULL;
4149 2fc00ff4 2019-08-31 stsp }
4150 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4151 ecb28ae0 2018-07-16 stsp done:
4152 f135c941 2020-02-20 stsp free(in_repo_path);
4153 ecb28ae0 2018-07-16 stsp free(repo_path);
4154 ecb28ae0 2018-07-16 stsp free(cwd);
4155 899d86c2 2018-05-10 stsp free(start_id);
4156 d8f38dc4 2020-12-05 stsp free(label);
4157 d8f38dc4 2020-12-05 stsp if (ref)
4158 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
4159 1d0f4054 2021-06-17 stsp if (repo) {
4160 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4161 1d0f4054 2021-06-17 stsp if (error == NULL)
4162 1d0f4054 2021-06-17 stsp error = close_err;
4163 1d0f4054 2021-06-17 stsp }
4164 ec142235 2019-03-07 stsp if (worktree)
4165 ec142235 2019-03-07 stsp got_worktree_close(worktree);
4166 7cd52833 2022-06-23 thomas if (pack_fds) {
4167 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4168 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4169 7cd52833 2022-06-23 thomas if (error == NULL)
4170 7cd52833 2022-06-23 thomas error = pack_err;
4171 7cd52833 2022-06-23 thomas }
4172 51a10b52 2020-12-26 stsp tog_free_refs();
4173 80ddbec8 2018-04-29 stsp return error;
4174 9f7d7167 2018-04-29 stsp }
4175 9f7d7167 2018-04-29 stsp
4176 4ed7e80c 2018-05-20 stsp __dead static void
4177 9f7d7167 2018-04-29 stsp usage_diff(void)
4178 9f7d7167 2018-04-29 stsp {
4179 80ddbec8 2018-04-29 stsp endwin();
4180 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4181 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
4182 9f7d7167 2018-04-29 stsp exit(1);
4183 b304db33 2018-05-20 stsp }
4184 b304db33 2018-05-20 stsp
4185 6d17833f 2019-11-08 stsp static int
4186 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
4187 41605754 2020-11-12 stsp regmatch_t *regmatch)
4188 6d17833f 2019-11-08 stsp {
4189 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
4190 6d17833f 2019-11-08 stsp }
4191 6d17833f 2019-11-08 stsp
4192 ef20f542 2022-06-26 thomas static struct tog_color *
4193 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
4194 6d17833f 2019-11-08 stsp {
4195 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
4196 6d17833f 2019-11-08 stsp
4197 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
4198 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
4199 6d17833f 2019-11-08 stsp return tc;
4200 6d17833f 2019-11-08 stsp }
4201 6d17833f 2019-11-08 stsp
4202 6d17833f 2019-11-08 stsp return NULL;
4203 6d17833f 2019-11-08 stsp }
4204 6d17833f 2019-11-08 stsp
4205 4ed7e80c 2018-05-20 stsp static const struct got_error *
4206 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4207 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
4208 41605754 2020-11-12 stsp {
4209 41605754 2020-11-12 stsp const struct got_error *err = NULL;
4210 666f7b10 2022-06-23 thomas char *exstr = NULL;
4211 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
4212 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
4213 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4214 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4215 41605754 2020-11-12 stsp
4216 41605754 2020-11-12 stsp *wtotal = 0;
4217 cbea3800 2022-06-23 thomas
4218 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4219 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4220 41605754 2020-11-12 stsp
4221 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4222 666f7b10 2022-06-23 thomas if (err)
4223 666f7b10 2022-06-23 thomas return err;
4224 666f7b10 2022-06-23 thomas
4225 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4226 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4227 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4228 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4229 666f7b10 2022-06-23 thomas goto done;
4230 666f7b10 2022-06-23 thomas }
4231 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4232 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4233 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4234 cbea3800 2022-06-23 thomas goto done;
4235 cbea3800 2022-06-23 thomas }
4236 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4237 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4238 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4239 cbea3800 2022-06-23 thomas goto done;
4240 cbea3800 2022-06-23 thomas }
4241 05171be4 2022-06-23 thomas
4242 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4243 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4244 cbea3800 2022-06-23 thomas col_tab_align, 1);
4245 cbea3800 2022-06-23 thomas if (err)
4246 cbea3800 2022-06-23 thomas goto done;
4247 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4248 05171be4 2022-06-23 thomas if (n) {
4249 cbea3800 2022-06-23 thomas free(wline);
4250 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4251 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4252 cbea3800 2022-06-23 thomas if (err)
4253 cbea3800 2022-06-23 thomas goto done;
4254 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4255 cbea3800 2022-06-23 thomas wlimit -= width;
4256 cbea3800 2022-06-23 thomas *wtotal += width;
4257 41605754 2020-11-12 stsp }
4258 41605754 2020-11-12 stsp
4259 41605754 2020-11-12 stsp if (wlimit > 0) {
4260 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4261 cbea3800 2022-06-23 thomas size_t wlen;
4262 cbea3800 2022-06-23 thomas
4263 cbea3800 2022-06-23 thomas free(wline);
4264 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4265 cbea3800 2022-06-23 thomas col_tab_align, 1);
4266 cbea3800 2022-06-23 thomas if (err)
4267 cbea3800 2022-06-23 thomas goto done;
4268 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4269 cbea3800 2022-06-23 thomas while (i < wlen) {
4270 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4271 cbea3800 2022-06-23 thomas if (width == -1) {
4272 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4273 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4274 cbea3800 2022-06-23 thomas goto done;
4275 cbea3800 2022-06-23 thomas }
4276 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4277 cbea3800 2022-06-23 thomas break;
4278 1c5e5faa 2022-06-23 thomas w += width;
4279 cbea3800 2022-06-23 thomas i++;
4280 41605754 2020-11-12 stsp }
4281 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4282 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4283 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4284 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4285 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4286 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4287 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4288 41605754 2020-11-12 stsp }
4289 41605754 2020-11-12 stsp }
4290 41605754 2020-11-12 stsp
4291 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4292 cbea3800 2022-06-23 thomas free(wline);
4293 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4294 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4295 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4296 cbea3800 2022-06-23 thomas col_tab_align, 1);
4297 cbea3800 2022-06-23 thomas if (err)
4298 cbea3800 2022-06-23 thomas goto done;
4299 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4300 cbea3800 2022-06-23 thomas } else {
4301 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4302 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4303 cbea3800 2022-06-23 thomas if (err)
4304 cbea3800 2022-06-23 thomas goto done;
4305 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4306 cbea3800 2022-06-23 thomas }
4307 cbea3800 2022-06-23 thomas *wtotal += width2;
4308 41605754 2020-11-12 stsp }
4309 cbea3800 2022-06-23 thomas done:
4310 05171be4 2022-06-23 thomas free(wline);
4311 666f7b10 2022-06-23 thomas free(exstr);
4312 cbea3800 2022-06-23 thomas free(seg0);
4313 cbea3800 2022-06-23 thomas free(seg1);
4314 cbea3800 2022-06-23 thomas free(seg2);
4315 cbea3800 2022-06-23 thomas return err;
4316 41605754 2020-11-12 stsp }
4317 07dd3ed3 2022-08-06 thomas
4318 07dd3ed3 2022-08-06 thomas static int
4319 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4320 07dd3ed3 2022-08-06 thomas {
4321 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4322 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4323 07dd3ed3 2022-08-06 thomas
4324 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4325 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4326 fc2737d5 2022-09-15 thomas
4327 fc2737d5 2022-09-15 thomas first = &s->first_displayed_line;
4328 fc2737d5 2022-09-15 thomas selected = first;
4329 fc2737d5 2022-09-15 thomas eof = &s->eof;
4330 fc2737d5 2022-09-15 thomas f = s->f;
4331 fc2737d5 2022-09-15 thomas } else if (view->type == TOG_VIEW_HELP) {
4332 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
4333 41605754 2020-11-12 stsp
4334 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4335 07dd3ed3 2022-08-06 thomas selected = first;
4336 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4337 07dd3ed3 2022-08-06 thomas f = s->f;
4338 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4339 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4340 07dd3ed3 2022-08-06 thomas
4341 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4342 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4343 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4344 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4345 07dd3ed3 2022-08-06 thomas } else
4346 07dd3ed3 2022-08-06 thomas return 0;
4347 07dd3ed3 2022-08-06 thomas
4348 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4349 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4350 07dd3ed3 2022-08-06 thomas return 0;
4351 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4352 07dd3ed3 2022-08-06 thomas rewind(f);
4353 07dd3ed3 2022-08-06 thomas *eof = 0;
4354 07dd3ed3 2022-08-06 thomas *first = 1;
4355 07dd3ed3 2022-08-06 thomas *lineno = 0;
4356 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4357 07dd3ed3 2022-08-06 thomas return 0;
4358 07dd3ed3 2022-08-06 thomas }
4359 07dd3ed3 2022-08-06 thomas
4360 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4361 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4362 07dd3ed3 2022-08-06 thomas view->gline = 0;
4363 07dd3ed3 2022-08-06 thomas
4364 07dd3ed3 2022-08-06 thomas return 1;
4365 07dd3ed3 2022-08-06 thomas }
4366 07dd3ed3 2022-08-06 thomas
4367 41605754 2020-11-12 stsp static const struct got_error *
4368 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4369 26ed57b2 2018-05-19 stsp {
4370 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4371 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4372 61e69b96 2018-05-20 stsp const struct got_error *err;
4373 82c78e96 2022-08-06 thomas int nprinted = 0;
4374 b304db33 2018-05-20 stsp char *line;
4375 826082fe 2020-12-10 stsp size_t linesize = 0;
4376 826082fe 2020-12-10 stsp ssize_t linelen;
4377 61e69b96 2018-05-20 stsp wchar_t *wline;
4378 e0b650dd 2018-05-20 stsp int width;
4379 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4380 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4381 fe621944 2020-11-10 stsp off_t line_offset;
4382 26ed57b2 2018-05-19 stsp
4383 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4384 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4385 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4386 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4387 fe621944 2020-11-10 stsp
4388 f7d12f7e 2018-08-01 stsp werase(view->window);
4389 a3404814 2018-09-02 stsp
4390 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4391 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4392 07dd3ed3 2022-08-06 thomas
4393 a3404814 2018-09-02 stsp if (header) {
4394 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4395 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4396 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4397 07dd3ed3 2022-08-06 thomas
4398 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4399 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4400 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4401 f91a2b48 2022-06-23 thomas 0, 0);
4402 135a2da0 2020-11-11 stsp free(line);
4403 135a2da0 2020-11-11 stsp if (err)
4404 a3404814 2018-09-02 stsp return err;
4405 a3404814 2018-09-02 stsp
4406 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4407 a3404814 2018-09-02 stsp wstandout(view->window);
4408 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4409 e54cc94a 2020-11-11 stsp free(wline);
4410 e54cc94a 2020-11-11 stsp wline = NULL;
4411 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4412 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4413 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4414 a3404814 2018-09-02 stsp wstandend(view->window);
4415 26ed57b2 2018-05-19 stsp
4416 a3404814 2018-09-02 stsp if (max_lines <= 1)
4417 a3404814 2018-09-02 stsp return NULL;
4418 a3404814 2018-09-02 stsp max_lines--;
4419 a3404814 2018-09-02 stsp }
4420 a3404814 2018-09-02 stsp
4421 89f1a395 2020-12-01 naddy s->eof = 0;
4422 05171be4 2022-06-23 thomas view->maxx = 0;
4423 826082fe 2020-12-10 stsp line = NULL;
4424 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4425 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4426 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4427 6568f0aa 2022-08-06 thomas
4428 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4429 826082fe 2020-12-10 stsp if (linelen == -1) {
4430 826082fe 2020-12-10 stsp if (feof(s->f)) {
4431 826082fe 2020-12-10 stsp s->eof = 1;
4432 826082fe 2020-12-10 stsp break;
4433 826082fe 2020-12-10 stsp }
4434 826082fe 2020-12-10 stsp free(line);
4435 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4436 61e69b96 2018-05-20 stsp }
4437 05171be4 2022-06-23 thomas
4438 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4439 07dd3ed3 2022-08-06 thomas continue;
4440 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4441 07dd3ed3 2022-08-06 thomas continue;
4442 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4443 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4444 07dd3ed3 2022-08-06 thomas
4445 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4446 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4447 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4448 cbea3800 2022-06-23 thomas if (err) {
4449 cbea3800 2022-06-23 thomas free(line);
4450 cbea3800 2022-06-23 thomas return err;
4451 cbea3800 2022-06-23 thomas }
4452 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4453 cbea3800 2022-06-23 thomas free(wline);
4454 cbea3800 2022-06-23 thomas wline = NULL;
4455 cbea3800 2022-06-23 thomas
4456 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4457 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4458 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4459 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4460 07dd3ed3 2022-08-06 thomas if (attr)
4461 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4462 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4463 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4464 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4465 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4466 41605754 2020-11-12 stsp if (err) {
4467 41605754 2020-11-12 stsp free(line);
4468 41605754 2020-11-12 stsp return err;
4469 41605754 2020-11-12 stsp }
4470 41605754 2020-11-12 stsp } else {
4471 f1c0ec19 2022-06-23 thomas int skip;
4472 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4473 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4474 f1c0ec19 2022-06-23 thomas if (err) {
4475 f1c0ec19 2022-06-23 thomas free(line);
4476 f1c0ec19 2022-06-23 thomas return err;
4477 f1c0ec19 2022-06-23 thomas }
4478 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4479 f1c0ec19 2022-06-23 thomas free(wline);
4480 41605754 2020-11-12 stsp wline = NULL;
4481 41605754 2020-11-12 stsp }
4482 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4483 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4484 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4485 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4486 07dd3ed3 2022-08-06 thomas } else {
4487 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4488 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4489 07dd3ed3 2022-08-06 thomas }
4490 07dd3ed3 2022-08-06 thomas if (attr)
4491 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4492 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4493 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4494 826082fe 2020-12-10 stsp }
4495 826082fe 2020-12-10 stsp free(line);
4496 fe621944 2020-11-10 stsp if (nprinted >= 1)
4497 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4498 89f1a395 2020-12-01 naddy (nprinted - 1);
4499 fe621944 2020-11-10 stsp else
4500 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4501 26ed57b2 2018-05-19 stsp
4502 a5d43cac 2022-07-01 thomas view_border(view);
4503 c3e9aa98 2019-05-13 jcs
4504 89f1a395 2020-12-01 naddy if (s->eof) {
4505 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4506 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4507 c3e9aa98 2019-05-13 jcs nprinted++;
4508 c3e9aa98 2019-05-13 jcs }
4509 c3e9aa98 2019-05-13 jcs
4510 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4511 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4512 c3e9aa98 2019-05-13 jcs if (err) {
4513 c3e9aa98 2019-05-13 jcs return err;
4514 c3e9aa98 2019-05-13 jcs }
4515 26ed57b2 2018-05-19 stsp
4516 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4517 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4518 e54cc94a 2020-11-11 stsp free(wline);
4519 e54cc94a 2020-11-11 stsp wline = NULL;
4520 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4521 c3e9aa98 2019-05-13 jcs }
4522 c3e9aa98 2019-05-13 jcs
4523 26ed57b2 2018-05-19 stsp return NULL;
4524 abd2672a 2018-12-23 stsp }
4525 abd2672a 2018-12-23 stsp
4526 abd2672a 2018-12-23 stsp static char *
4527 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4528 abd2672a 2018-12-23 stsp {
4529 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4530 09867e48 2019-08-13 stsp char *p, *s;
4531 09867e48 2019-08-13 stsp
4532 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4533 09867e48 2019-08-13 stsp if (tm == NULL)
4534 09867e48 2019-08-13 stsp return NULL;
4535 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4536 09867e48 2019-08-13 stsp if (s == NULL)
4537 09867e48 2019-08-13 stsp return NULL;
4538 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4539 abd2672a 2018-12-23 stsp if (p)
4540 abd2672a 2018-12-23 stsp *p = '\0';
4541 abd2672a 2018-12-23 stsp return s;
4542 9f7d7167 2018-04-29 stsp }
4543 9f7d7167 2018-04-29 stsp
4544 4ed7e80c 2018-05-20 stsp static const struct got_error *
4545 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4546 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4547 abd2672a 2018-12-23 stsp {
4548 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4549 fe621944 2020-11-10 stsp
4550 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4551 fe621944 2020-11-10 stsp if (p == NULL)
4552 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4553 82c78e96 2022-08-06 thomas *lines = p;
4554 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4555 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4556 fe621944 2020-11-10 stsp (*nlines)++;
4557 82c78e96 2022-08-06 thomas
4558 fe621944 2020-11-10 stsp return NULL;
4559 fe621944 2020-11-10 stsp }
4560 fe621944 2020-11-10 stsp
4561 fe621944 2020-11-10 stsp static const struct got_error *
4562 be97ab03 2023-01-19 thomas cat_diff(FILE *dst, FILE *src, struct got_diff_line **d_lines, size_t *d_nlines,
4563 be97ab03 2023-01-19 thomas struct got_diff_line *s_lines, size_t s_nlines)
4564 be97ab03 2023-01-19 thomas {
4565 be97ab03 2023-01-19 thomas struct got_diff_line *p;
4566 be97ab03 2023-01-19 thomas char buf[BUFSIZ];
4567 be97ab03 2023-01-19 thomas size_t i, r;
4568 be97ab03 2023-01-19 thomas
4569 be97ab03 2023-01-19 thomas if (fseeko(src, 0L, SEEK_SET) == -1)
4570 be97ab03 2023-01-19 thomas return got_error_from_errno("fseeko");
4571 be97ab03 2023-01-19 thomas
4572 be97ab03 2023-01-19 thomas for (;;) {
4573 be97ab03 2023-01-19 thomas r = fread(buf, 1, sizeof(buf), src);
4574 be97ab03 2023-01-19 thomas if (r == 0) {
4575 be97ab03 2023-01-19 thomas if (ferror(src))
4576 be97ab03 2023-01-19 thomas return got_error_from_errno("fread");
4577 be97ab03 2023-01-19 thomas if (feof(src))
4578 be97ab03 2023-01-19 thomas break;
4579 be97ab03 2023-01-19 thomas }
4580 be97ab03 2023-01-19 thomas if (fwrite(buf, 1, r, dst) != r)
4581 be97ab03 2023-01-19 thomas return got_ferror(dst, GOT_ERR_IO);
4582 be97ab03 2023-01-19 thomas }
4583 be97ab03 2023-01-19 thomas
4584 be97ab03 2023-01-19 thomas /*
4585 be97ab03 2023-01-19 thomas * The diff driver initialises the first line at offset zero when the
4586 be97ab03 2023-01-19 thomas * array isn't prepopulated, skip it; we already have it in *d_lines.
4587 be97ab03 2023-01-19 thomas */
4588 be97ab03 2023-01-19 thomas for (i = 1; i < s_nlines; ++i)
4589 be97ab03 2023-01-19 thomas s_lines[i].offset += (*d_lines)[*d_nlines - 1].offset;
4590 be97ab03 2023-01-19 thomas
4591 be97ab03 2023-01-19 thomas --s_nlines;
4592 be97ab03 2023-01-19 thomas
4593 be97ab03 2023-01-19 thomas p = reallocarray(*d_lines, *d_nlines + s_nlines, sizeof(*p));
4594 be97ab03 2023-01-19 thomas if (p == NULL) {
4595 be97ab03 2023-01-19 thomas /* d_lines is freed in close_diff_view() */
4596 be97ab03 2023-01-19 thomas return got_error_from_errno("reallocarray");
4597 be97ab03 2023-01-19 thomas }
4598 be97ab03 2023-01-19 thomas
4599 be97ab03 2023-01-19 thomas *d_lines = p;
4600 be97ab03 2023-01-19 thomas
4601 be97ab03 2023-01-19 thomas memcpy(*d_lines + *d_nlines, s_lines + 1, s_nlines * sizeof(*s_lines));
4602 be97ab03 2023-01-19 thomas *d_nlines += s_nlines;
4603 be97ab03 2023-01-19 thomas
4604 be97ab03 2023-01-19 thomas return NULL;
4605 be97ab03 2023-01-19 thomas }
4606 be97ab03 2023-01-19 thomas
4607 be97ab03 2023-01-19 thomas static const struct got_error *
4608 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4609 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4610 772fcad5 2023-01-07 thomas struct got_repository *repo, int ignore_ws, int force_text_diff,
4611 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg *dsa, FILE *outfile)
4612 fe621944 2020-11-10 stsp {
4613 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4614 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4615 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4616 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4617 45d799e2 2018-12-23 stsp time_t committer_time;
4618 45d799e2 2018-12-23 stsp const char *author, *committer;
4619 8b473291 2019-02-21 stsp char *refs_str = NULL;
4620 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4621 fe621944 2020-11-10 stsp off_t outoff = 0;
4622 fe621944 2020-11-10 stsp int n;
4623 abd2672a 2018-12-23 stsp
4624 8b473291 2019-02-21 stsp if (refs) {
4625 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4626 8b473291 2019-02-21 stsp if (err)
4627 8b473291 2019-02-21 stsp return err;
4628 8b473291 2019-02-21 stsp }
4629 8b473291 2019-02-21 stsp
4630 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4631 abd2672a 2018-12-23 stsp if (err)
4632 abd2672a 2018-12-23 stsp return err;
4633 abd2672a 2018-12-23 stsp
4634 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4635 15a94983 2018-12-23 stsp if (err) {
4636 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4637 15a94983 2018-12-23 stsp goto done;
4638 15a94983 2018-12-23 stsp }
4639 abd2672a 2018-12-23 stsp
4640 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4641 fe621944 2020-11-10 stsp if (err)
4642 fe621944 2020-11-10 stsp goto done;
4643 fe621944 2020-11-10 stsp
4644 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4645 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4646 fe621944 2020-11-10 stsp if (n < 0) {
4647 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4648 abd2672a 2018-12-23 stsp goto done;
4649 abd2672a 2018-12-23 stsp }
4650 fe621944 2020-11-10 stsp outoff += n;
4651 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4652 fe621944 2020-11-10 stsp if (err)
4653 fe621944 2020-11-10 stsp goto done;
4654 fe621944 2020-11-10 stsp
4655 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4656 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4657 fe621944 2020-11-10 stsp if (n < 0) {
4658 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4659 abd2672a 2018-12-23 stsp goto done;
4660 abd2672a 2018-12-23 stsp }
4661 fe621944 2020-11-10 stsp outoff += n;
4662 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4663 fe621944 2020-11-10 stsp if (err)
4664 fe621944 2020-11-10 stsp goto done;
4665 fe621944 2020-11-10 stsp
4666 48830929 2023-01-07 thomas author = got_object_commit_get_author(commit);
4667 48830929 2023-01-07 thomas committer = got_object_commit_get_committer(commit);
4668 48830929 2023-01-07 thomas if (strcmp(author, committer) != 0) {
4669 48830929 2023-01-07 thomas n = fprintf(outfile, "via: %s\n", committer);
4670 fe621944 2020-11-10 stsp if (n < 0) {
4671 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4672 fe621944 2020-11-10 stsp goto done;
4673 fe621944 2020-11-10 stsp }
4674 fe621944 2020-11-10 stsp outoff += n;
4675 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4676 48830929 2023-01-07 thomas GOT_DIFF_LINE_AUTHOR);
4677 fe621944 2020-11-10 stsp if (err)
4678 fe621944 2020-11-10 stsp goto done;
4679 abd2672a 2018-12-23 stsp }
4680 48830929 2023-01-07 thomas committer_time = got_object_commit_get_committer_time(commit);
4681 48830929 2023-01-07 thomas datestr = get_datestr(&committer_time, datebuf);
4682 48830929 2023-01-07 thomas if (datestr) {
4683 48830929 2023-01-07 thomas n = fprintf(outfile, "date: %s UTC\n", datestr);
4684 fe621944 2020-11-10 stsp if (n < 0) {
4685 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4686 fe621944 2020-11-10 stsp goto done;
4687 fe621944 2020-11-10 stsp }
4688 fe621944 2020-11-10 stsp outoff += n;
4689 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4690 48830929 2023-01-07 thomas GOT_DIFF_LINE_DATE);
4691 fe621944 2020-11-10 stsp if (err)
4692 fe621944 2020-11-10 stsp goto done;
4693 abd2672a 2018-12-23 stsp }
4694 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4695 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4696 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4697 ac4dc263 2021-09-24 thomas int pn = 1;
4698 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4699 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4700 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4701 ac4dc263 2021-09-24 thomas if (err)
4702 ac4dc263 2021-09-24 thomas goto done;
4703 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4704 ac4dc263 2021-09-24 thomas if (n < 0) {
4705 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4706 ac4dc263 2021-09-24 thomas goto done;
4707 ac4dc263 2021-09-24 thomas }
4708 ac4dc263 2021-09-24 thomas outoff += n;
4709 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4710 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4711 ac4dc263 2021-09-24 thomas if (err)
4712 ac4dc263 2021-09-24 thomas goto done;
4713 ac4dc263 2021-09-24 thomas free(id_str);
4714 ac4dc263 2021-09-24 thomas id_str = NULL;
4715 ac4dc263 2021-09-24 thomas }
4716 ac4dc263 2021-09-24 thomas }
4717 ac4dc263 2021-09-24 thomas
4718 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4719 5943eee2 2019-08-13 stsp if (err)
4720 5943eee2 2019-08-13 stsp goto done;
4721 fe621944 2020-11-10 stsp s = logmsg;
4722 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4723 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4724 fe621944 2020-11-10 stsp if (n < 0) {
4725 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4726 fe621944 2020-11-10 stsp goto done;
4727 fe621944 2020-11-10 stsp }
4728 fe621944 2020-11-10 stsp outoff += n;
4729 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4730 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4731 fe621944 2020-11-10 stsp if (err)
4732 fe621944 2020-11-10 stsp goto done;
4733 abd2672a 2018-12-23 stsp }
4734 fe621944 2020-11-10 stsp
4735 be97ab03 2023-01-19 thomas TAILQ_FOREACH(pe, dsa->paths, entry) {
4736 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4737 be97ab03 2023-01-19 thomas int pad = dsa->max_path_len - pe->path_len + 1;
4738 772fcad5 2023-01-07 thomas
4739 772fcad5 2023-01-07 thomas n = fprintf(outfile, "%c %s%*c | %*d+ %*d-\n", cp->status,
4740 be97ab03 2023-01-19 thomas pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
4741 be97ab03 2023-01-19 thomas dsa->rm_cols + 1, cp->rm);
4742 fe621944 2020-11-10 stsp if (n < 0) {
4743 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4744 fe621944 2020-11-10 stsp goto done;
4745 fe621944 2020-11-10 stsp }
4746 fe621944 2020-11-10 stsp outoff += n;
4747 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4748 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4749 fe621944 2020-11-10 stsp if (err)
4750 fe621944 2020-11-10 stsp goto done;
4751 0208f208 2020-05-05 stsp }
4752 fe621944 2020-11-10 stsp
4753 0208f208 2020-05-05 stsp fputc('\n', outfile);
4754 fe621944 2020-11-10 stsp outoff++;
4755 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4756 772fcad5 2023-01-07 thomas if (err)
4757 772fcad5 2023-01-07 thomas goto done;
4758 772fcad5 2023-01-07 thomas
4759 772fcad5 2023-01-07 thomas n = fprintf(outfile,
4760 5c24b9c1 2023-01-15 thomas "%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n",
4761 be97ab03 2023-01-19 thomas dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4762 be97ab03 2023-01-19 thomas dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4763 772fcad5 2023-01-07 thomas if (n < 0) {
4764 772fcad5 2023-01-07 thomas err = got_error_from_errno("fprintf");
4765 772fcad5 2023-01-07 thomas goto done;
4766 772fcad5 2023-01-07 thomas }
4767 772fcad5 2023-01-07 thomas outoff += n;
4768 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4769 772fcad5 2023-01-07 thomas if (err)
4770 772fcad5 2023-01-07 thomas goto done;
4771 772fcad5 2023-01-07 thomas
4772 772fcad5 2023-01-07 thomas fputc('\n', outfile);
4773 772fcad5 2023-01-07 thomas outoff++;
4774 772fcad5 2023-01-07 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4775 abd2672a 2018-12-23 stsp done:
4776 abd2672a 2018-12-23 stsp free(id_str);
4777 5943eee2 2019-08-13 stsp free(logmsg);
4778 8b473291 2019-02-21 stsp free(refs_str);
4779 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4780 7510f233 2020-08-09 stsp if (err) {
4781 82c78e96 2022-08-06 thomas free(*lines);
4782 82c78e96 2022-08-06 thomas *lines = NULL;
4783 ae6a6978 2020-08-09 stsp *nlines = 0;
4784 7510f233 2020-08-09 stsp }
4785 fe621944 2020-11-10 stsp return err;
4786 abd2672a 2018-12-23 stsp }
4787 abd2672a 2018-12-23 stsp
4788 abd2672a 2018-12-23 stsp static const struct got_error *
4789 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4790 26ed57b2 2018-05-19 stsp {
4791 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4792 be97ab03 2023-01-19 thomas FILE *f = NULL, *tmp_diff_file = NULL;
4793 15a94983 2018-12-23 stsp int obj_type;
4794 be97ab03 2023-01-19 thomas struct got_diff_line *lines = NULL;
4795 be97ab03 2023-01-19 thomas struct got_pathlist_head changed_paths;
4796 fe621944 2020-11-10 stsp
4797 be97ab03 2023-01-19 thomas TAILQ_INIT(&changed_paths);
4798 be97ab03 2023-01-19 thomas
4799 82c78e96 2022-08-06 thomas free(s->lines);
4800 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4801 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4802 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4803 fe621944 2020-11-10 stsp s->nlines = 0;
4804 26ed57b2 2018-05-19 stsp
4805 511a516b 2018-05-19 stsp f = got_opentemp();
4806 48ae06ee 2018-10-18 stsp if (f == NULL) {
4807 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4808 48ae06ee 2018-10-18 stsp goto done;
4809 48ae06ee 2018-10-18 stsp }
4810 be97ab03 2023-01-19 thomas tmp_diff_file = got_opentemp();
4811 be97ab03 2023-01-19 thomas if (tmp_diff_file == NULL) {
4812 be97ab03 2023-01-19 thomas err = got_error_from_errno("got_opentemp");
4813 be97ab03 2023-01-19 thomas goto done;
4814 be97ab03 2023-01-19 thomas }
4815 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4816 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4817 fb43ecf1 2019-02-11 stsp goto done;
4818 fb43ecf1 2019-02-11 stsp }
4819 48ae06ee 2018-10-18 stsp s->f = f;
4820 26ed57b2 2018-05-19 stsp
4821 15a94983 2018-12-23 stsp if (s->id1)
4822 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4823 15a94983 2018-12-23 stsp else
4824 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4825 15a94983 2018-12-23 stsp if (err)
4826 15a94983 2018-12-23 stsp goto done;
4827 15a94983 2018-12-23 stsp
4828 15a94983 2018-12-23 stsp switch (obj_type) {
4829 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4830 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4831 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4832 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4833 be97ab03 2023-01-19 thomas s->ignore_whitespace, s->force_text_diff, NULL, s->repo,
4834 53d03f97 2023-01-10 thomas s->f);
4835 26ed57b2 2018-05-19 stsp break;
4836 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4837 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4838 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4839 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4840 be97ab03 2023-01-19 thomas s->force_text_diff, NULL, s->repo, s->f);
4841 26ed57b2 2018-05-19 stsp break;
4842 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4843 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4844 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4845 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4846 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4847 be97ab03 2023-01-19 thomas size_t nlines = 0;
4848 be97ab03 2023-01-19 thomas struct got_diffstat_cb_arg dsa = {
4849 be97ab03 2023-01-19 thomas 0, 0, 0, 0, 0, 0,
4850 be97ab03 2023-01-19 thomas &changed_paths,
4851 be97ab03 2023-01-19 thomas s->ignore_whitespace,
4852 be97ab03 2023-01-19 thomas s->force_text_diff,
4853 be97ab03 2023-01-19 thomas tog_diff_algo
4854 be97ab03 2023-01-19 thomas };
4855 abd2672a 2018-12-23 stsp
4856 be97ab03 2023-01-19 thomas lines = malloc(sizeof(*lines));
4857 be97ab03 2023-01-19 thomas if (lines == NULL) {
4858 be97ab03 2023-01-19 thomas err = got_error_from_errno("malloc");
4859 be97ab03 2023-01-19 thomas goto done;
4860 be97ab03 2023-01-19 thomas }
4861 be97ab03 2023-01-19 thomas
4862 be97ab03 2023-01-19 thomas /* build diff first in tmp file then append to commit info */
4863 be97ab03 2023-01-19 thomas err = got_diff_objects_as_commits(&lines, &nlines,
4864 be97ab03 2023-01-19 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4865 be97ab03 2023-01-19 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4866 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->repo, tmp_diff_file);
4867 be97ab03 2023-01-19 thomas if (err)
4868 be97ab03 2023-01-19 thomas break;
4869 be97ab03 2023-01-19 thomas
4870 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4871 abd2672a 2018-12-23 stsp if (err)
4872 3ffacbe1 2020-02-02 tracey goto done;
4873 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4874 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4875 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4876 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4877 772fcad5 2023-01-07 thomas refs, s->repo, s->ignore_whitespace,
4878 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
4879 f44b1f58 2020-02-02 tracey if (err)
4880 f44b1f58 2020-02-02 tracey goto done;
4881 f44b1f58 2020-02-02 tracey } else {
4882 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4883 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4884 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4885 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4886 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4887 772fcad5 2023-01-07 thomas s->ignore_whitespace,
4888 be97ab03 2023-01-19 thomas s->force_text_diff, &dsa, s->f);
4889 f44b1f58 2020-02-02 tracey if (err)
4890 f44b1f58 2020-02-02 tracey goto done;
4891 f5404e4e 2020-02-02 tracey break;
4892 15a087fe 2019-02-21 stsp }
4893 abd2672a 2018-12-23 stsp }
4894 abd2672a 2018-12-23 stsp }
4895 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4896 abd2672a 2018-12-23 stsp
4897 be97ab03 2023-01-19 thomas err = cat_diff(s->f, tmp_diff_file, &s->lines, &s->nlines,
4898 be97ab03 2023-01-19 thomas lines, nlines);
4899 26ed57b2 2018-05-19 stsp break;
4900 abd2672a 2018-12-23 stsp }
4901 26ed57b2 2018-05-19 stsp default:
4902 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4903 48ae06ee 2018-10-18 stsp break;
4904 26ed57b2 2018-05-19 stsp }
4905 48ae06ee 2018-10-18 stsp done:
4906 be97ab03 2023-01-19 thomas free(lines);
4907 be97ab03 2023-01-19 thomas got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4908 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4909 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4910 be97ab03 2023-01-19 thomas if (tmp_diff_file && fclose(tmp_diff_file) == EOF && err == NULL)
4911 be97ab03 2023-01-19 thomas err = got_error_from_errno("fclose");
4912 48ae06ee 2018-10-18 stsp return err;
4913 48ae06ee 2018-10-18 stsp }
4914 26ed57b2 2018-05-19 stsp
4915 f5215bb9 2019-02-22 stsp static void
4916 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4917 f5215bb9 2019-02-22 stsp {
4918 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4919 f5215bb9 2019-02-22 stsp update_panels();
4920 f5215bb9 2019-02-22 stsp doupdate();
4921 f44b1f58 2020-02-02 tracey }
4922 f44b1f58 2020-02-02 tracey
4923 f44b1f58 2020-02-02 tracey static const struct got_error *
4924 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4925 f44b1f58 2020-02-02 tracey {
4926 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4927 f44b1f58 2020-02-02 tracey
4928 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4929 f44b1f58 2020-02-02 tracey return NULL;
4930 f44b1f58 2020-02-02 tracey }
4931 f44b1f58 2020-02-02 tracey
4932 2a7d3cd7 2022-09-17 thomas static void
4933 2a7d3cd7 2022-09-17 thomas search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4934 fc2737d5 2022-09-15 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
4935 f44b1f58 2020-02-02 tracey {
4936 2a7d3cd7 2022-09-17 thomas struct tog_diff_view_state *s = &view->state.diff;
4937 fc2737d5 2022-09-15 thomas
4938 2a7d3cd7 2022-09-17 thomas *f = s->f;
4939 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
4940 2a7d3cd7 2022-09-17 thomas *line_offsets = NULL;
4941 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
4942 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
4943 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
4944 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
4945 fc2737d5 2022-09-15 thomas }
4946 fc2737d5 2022-09-15 thomas
4947 fc2737d5 2022-09-15 thomas static const struct got_error *
4948 fc2737d5 2022-09-15 thomas search_next_view_match(struct tog_view *view)
4949 fc2737d5 2022-09-15 thomas {
4950 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4951 fc2737d5 2022-09-15 thomas FILE *f;
4952 f44b1f58 2020-02-02 tracey int lineno;
4953 78eecffc 2022-06-23 thomas char *line = NULL;
4954 826082fe 2020-12-10 stsp size_t linesize = 0;
4955 826082fe 2020-12-10 stsp ssize_t linelen;
4956 fc2737d5 2022-09-15 thomas off_t *line_offsets;
4957 fc2737d5 2022-09-15 thomas size_t nlines = 0;
4958 fc2737d5 2022-09-15 thomas int *first, *last, *match, *selected;
4959 f44b1f58 2020-02-02 tracey
4960 2a7d3cd7 2022-09-17 thomas if (!view->search_setup)
4961 2a7d3cd7 2022-09-17 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
4962 2a7d3cd7 2022-09-17 thomas "view search not supported");
4963 2a7d3cd7 2022-09-17 thomas view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4964 fc2737d5 2022-09-15 thomas &match, &selected);
4965 fc2737d5 2022-09-15 thomas
4966 f44b1f58 2020-02-02 tracey if (!view->searching) {
4967 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4968 f44b1f58 2020-02-02 tracey return NULL;
4969 f44b1f58 2020-02-02 tracey }
4970 f44b1f58 2020-02-02 tracey
4971 fc2737d5 2022-09-15 thomas if (*match) {
4972 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4973 fc2737d5 2022-09-15 thomas lineno = *match + 1;
4974 f44b1f58 2020-02-02 tracey else
4975 fc2737d5 2022-09-15 thomas lineno = *match - 1;
4976 4b3f9dac 2021-12-17 thomas } else
4977 fc2737d5 2022-09-15 thomas lineno = *first - 1 + *selected;
4978 f44b1f58 2020-02-02 tracey
4979 f44b1f58 2020-02-02 tracey while (1) {
4980 f44b1f58 2020-02-02 tracey off_t offset;
4981 f44b1f58 2020-02-02 tracey
4982 fc2737d5 2022-09-15 thomas if (lineno <= 0 || lineno > nlines) {
4983 fc2737d5 2022-09-15 thomas if (*match == 0) {
4984 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4985 f44b1f58 2020-02-02 tracey break;
4986 f44b1f58 2020-02-02 tracey }
4987 f44b1f58 2020-02-02 tracey
4988 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4989 f44b1f58 2020-02-02 tracey lineno = 1;
4990 f44b1f58 2020-02-02 tracey else
4991 fc2737d5 2022-09-15 thomas lineno = nlines;
4992 f44b1f58 2020-02-02 tracey }
4993 f44b1f58 2020-02-02 tracey
4994 fc2737d5 2022-09-15 thomas offset = view->type == TOG_VIEW_DIFF ?
4995 fc2737d5 2022-09-15 thomas view->state.diff.lines[lineno - 1].offset :
4996 fc2737d5 2022-09-15 thomas line_offsets[lineno - 1];
4997 fc2737d5 2022-09-15 thomas if (fseeko(f, offset, SEEK_SET) != 0) {
4998 f44b1f58 2020-02-02 tracey free(line);
4999 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
5000 f44b1f58 2020-02-02 tracey }
5001 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesize, f);
5002 78eecffc 2022-06-23 thomas if (linelen != -1) {
5003 78eecffc 2022-06-23 thomas char *exstr;
5004 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5005 78eecffc 2022-06-23 thomas if (err)
5006 78eecffc 2022-06-23 thomas break;
5007 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5008 78eecffc 2022-06-23 thomas &view->regmatch)) {
5009 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5010 fc2737d5 2022-09-15 thomas *match = lineno;
5011 78eecffc 2022-06-23 thomas free(exstr);
5012 78eecffc 2022-06-23 thomas break;
5013 78eecffc 2022-06-23 thomas }
5014 78eecffc 2022-06-23 thomas free(exstr);
5015 f44b1f58 2020-02-02 tracey }
5016 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
5017 f44b1f58 2020-02-02 tracey lineno++;
5018 f44b1f58 2020-02-02 tracey else
5019 f44b1f58 2020-02-02 tracey lineno--;
5020 f44b1f58 2020-02-02 tracey }
5021 826082fe 2020-12-10 stsp free(line);
5022 f44b1f58 2020-02-02 tracey
5023 fc2737d5 2022-09-15 thomas if (*match) {
5024 fc2737d5 2022-09-15 thomas *first = *match;
5025 fc2737d5 2022-09-15 thomas *selected = 1;
5026 f44b1f58 2020-02-02 tracey }
5027 f44b1f58 2020-02-02 tracey
5028 05171be4 2022-06-23 thomas return err;
5029 f5215bb9 2019-02-22 stsp }
5030 f5215bb9 2019-02-22 stsp
5031 48ae06ee 2018-10-18 stsp static const struct got_error *
5032 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
5033 a0f32f33 2022-06-13 thomas {
5034 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
5035 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
5036 a0f32f33 2022-06-13 thomas
5037 a0f32f33 2022-06-13 thomas free(s->id1);
5038 a0f32f33 2022-06-13 thomas s->id1 = NULL;
5039 a0f32f33 2022-06-13 thomas free(s->id2);
5040 a0f32f33 2022-06-13 thomas s->id2 = NULL;
5041 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
5042 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5043 a0f32f33 2022-06-13 thomas s->f = NULL;
5044 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
5045 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5046 a0f32f33 2022-06-13 thomas s->f1 = NULL;
5047 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
5048 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
5049 a0f32f33 2022-06-13 thomas s->f2 = NULL;
5050 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
5051 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5052 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5053 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
5054 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
5055 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5056 82c78e96 2022-08-06 thomas free(s->lines);
5057 82c78e96 2022-08-06 thomas s->lines = NULL;
5058 a0f32f33 2022-06-13 thomas s->nlines = 0;
5059 a0f32f33 2022-06-13 thomas return err;
5060 a0f32f33 2022-06-13 thomas }
5061 a0f32f33 2022-06-13 thomas
5062 a0f32f33 2022-06-13 thomas static const struct got_error *
5063 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
5064 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
5065 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
5066 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
5067 48ae06ee 2018-10-18 stsp {
5068 48ae06ee 2018-10-18 stsp const struct got_error *err;
5069 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
5070 5dc9f4bc 2018-08-04 stsp
5071 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
5072 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
5073 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
5074 a0f32f33 2022-06-13 thomas
5075 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
5076 15a94983 2018-12-23 stsp int type1, type2;
5077 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
5078 15a94983 2018-12-23 stsp if (err)
5079 15a94983 2018-12-23 stsp return err;
5080 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
5081 15a94983 2018-12-23 stsp if (err)
5082 15a94983 2018-12-23 stsp return err;
5083 15a94983 2018-12-23 stsp
5084 15a94983 2018-12-23 stsp if (type1 != type2)
5085 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
5086 15a94983 2018-12-23 stsp }
5087 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
5088 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
5089 f44b1f58 2020-02-02 tracey s->selected_line = 1;
5090 f44b1f58 2020-02-02 tracey s->repo = repo;
5091 f44b1f58 2020-02-02 tracey s->id1 = id1;
5092 f44b1f58 2020-02-02 tracey s->id2 = id2;
5093 3dbaef42 2020-11-24 stsp s->label1 = label1;
5094 3dbaef42 2020-11-24 stsp s->label2 = label2;
5095 48ae06ee 2018-10-18 stsp
5096 15a94983 2018-12-23 stsp if (id1) {
5097 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
5098 5465d566 2020-02-01 tracey if (s->id1 == NULL)
5099 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5100 48ae06ee 2018-10-18 stsp } else
5101 5465d566 2020-02-01 tracey s->id1 = NULL;
5102 48ae06ee 2018-10-18 stsp
5103 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
5104 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
5105 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
5106 a0f32f33 2022-06-13 thomas goto done;
5107 48ae06ee 2018-10-18 stsp }
5108 a0f32f33 2022-06-13 thomas
5109 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
5110 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
5111 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
5112 9a1d7435 2022-06-23 thomas goto done;
5113 9a1d7435 2022-06-23 thomas }
5114 9a1d7435 2022-06-23 thomas
5115 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
5116 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
5117 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
5118 a0f32f33 2022-06-13 thomas goto done;
5119 a0f32f33 2022-06-13 thomas }
5120 a0f32f33 2022-06-13 thomas
5121 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
5122 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
5123 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5124 19a6a6b5 2022-07-01 thomas goto done;
5125 19a6a6b5 2022-07-01 thomas }
5126 19a6a6b5 2022-07-01 thomas
5127 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
5128 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
5129 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5130 19a6a6b5 2022-07-01 thomas goto done;
5131 19a6a6b5 2022-07-01 thomas }
5132 19a6a6b5 2022-07-01 thomas
5133 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
5134 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
5135 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
5136 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
5137 5465d566 2020-02-01 tracey s->repo = repo;
5138 6d17833f 2019-11-08 stsp
5139 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5140 6568f0aa 2022-08-06 thomas int rc;
5141 6d17833f 2019-11-08 stsp
5142 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
5143 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5144 6568f0aa 2022-08-06 thomas if (rc != ERR)
5145 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
5146 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5147 6568f0aa 2022-08-06 thomas if (rc != ERR)
5148 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
5149 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5150 6568f0aa 2022-08-06 thomas if (rc != ERR)
5151 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
5152 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5153 6568f0aa 2022-08-06 thomas if (rc != ERR)
5154 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
5155 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5156 6568f0aa 2022-08-06 thomas if (rc != ERR)
5157 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5158 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5159 6568f0aa 2022-08-06 thomas if (rc != ERR)
5160 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5161 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
5162 6568f0aa 2022-08-06 thomas if (rc != ERR)
5163 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5164 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
5165 6568f0aa 2022-08-06 thomas if (rc != ERR)
5166 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
5167 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
5168 6568f0aa 2022-08-06 thomas if (rc == ERR) {
5169 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
5170 a0f32f33 2022-06-13 thomas goto done;
5171 6568f0aa 2022-08-06 thomas }
5172 6d17833f 2019-11-08 stsp }
5173 5dc9f4bc 2018-08-04 stsp
5174 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5175 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
5176 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
5177 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
5178 f5215bb9 2019-02-22 stsp
5179 5465d566 2020-02-01 tracey err = create_diff(s);
5180 48ae06ee 2018-10-18 stsp
5181 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
5182 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
5183 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
5184 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
5185 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
5186 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_diff_view;
5187 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
5188 a0f32f33 2022-06-13 thomas done:
5189 a0f32f33 2022-06-13 thomas if (err)
5190 a0f32f33 2022-06-13 thomas close_diff_view(view);
5191 e5a0f69f 2018-08-18 stsp return err;
5192 5dc9f4bc 2018-08-04 stsp }
5193 5dc9f4bc 2018-08-04 stsp
5194 5dc9f4bc 2018-08-04 stsp static const struct got_error *
5195 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
5196 5dc9f4bc 2018-08-04 stsp {
5197 a3404814 2018-09-02 stsp const struct got_error *err;
5198 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
5199 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
5200 3dbaef42 2020-11-24 stsp const char *label1, *label2;
5201 a3404814 2018-09-02 stsp
5202 a3404814 2018-09-02 stsp if (s->id1) {
5203 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
5204 a3404814 2018-09-02 stsp if (err)
5205 a3404814 2018-09-02 stsp return err;
5206 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
5207 3dbaef42 2020-11-24 stsp } else
5208 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
5209 3dbaef42 2020-11-24 stsp
5210 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
5211 a3404814 2018-09-02 stsp if (err)
5212 a3404814 2018-09-02 stsp return err;
5213 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
5214 26ed57b2 2018-05-19 stsp
5215 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5216 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5217 a3404814 2018-09-02 stsp free(id_str1);
5218 a3404814 2018-09-02 stsp free(id_str2);
5219 a3404814 2018-09-02 stsp return err;
5220 a3404814 2018-09-02 stsp }
5221 a3404814 2018-09-02 stsp free(id_str1);
5222 a3404814 2018-09-02 stsp free(id_str2);
5223 a3404814 2018-09-02 stsp
5224 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
5225 267bb3b8 2021-08-01 stsp free(header);
5226 267bb3b8 2021-08-01 stsp return err;
5227 15a087fe 2019-02-21 stsp }
5228 15a087fe 2019-02-21 stsp
5229 15a087fe 2019-02-21 stsp static const struct got_error *
5230 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
5231 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
5232 15a087fe 2019-02-21 stsp {
5233 d7a04538 2019-02-21 stsp const struct got_error *err;
5234 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
5235 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
5236 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
5237 15a087fe 2019-02-21 stsp
5238 15a087fe 2019-02-21 stsp free(s->id2);
5239 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
5240 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
5241 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
5242 15a087fe 2019-02-21 stsp
5243 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5244 d7a04538 2019-02-21 stsp if (err)
5245 d7a04538 2019-02-21 stsp return err;
5246 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
5247 15a087fe 2019-02-21 stsp free(s->id1);
5248 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
5249 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5250 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
5251 15a087fe 2019-02-21 stsp return NULL;
5252 0cf4efb1 2018-09-29 stsp }
5253 0cf4efb1 2018-09-29 stsp
5254 0cf4efb1 2018-09-29 stsp static const struct got_error *
5255 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
5256 adf4c9e0 2022-07-03 thomas {
5257 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
5258 adf4c9e0 2022-07-03 thomas
5259 adf4c9e0 2022-07-03 thomas view->count = 0;
5260 adf4c9e0 2022-07-03 thomas wclear(view->window);
5261 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
5262 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
5263 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
5264 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
5265 adf4c9e0 2022-07-03 thomas return create_diff(s);
5266 82c78e96 2022-08-06 thomas }
5267 82c78e96 2022-08-06 thomas
5268 82c78e96 2022-08-06 thomas static void
5269 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5270 82c78e96 2022-08-06 thomas {
5271 82c78e96 2022-08-06 thomas int start, i;
5272 82c78e96 2022-08-06 thomas
5273 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
5274 82c78e96 2022-08-06 thomas
5275 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5276 82c78e96 2022-08-06 thomas if (i == 0)
5277 82c78e96 2022-08-06 thomas i = s->nlines - 1;
5278 82c78e96 2022-08-06 thomas if (--i == start)
5279 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5280 82c78e96 2022-08-06 thomas }
5281 82c78e96 2022-08-06 thomas
5282 82c78e96 2022-08-06 thomas s->selected_line = 1;
5283 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5284 adf4c9e0 2022-07-03 thomas }
5285 adf4c9e0 2022-07-03 thomas
5286 82c78e96 2022-08-06 thomas static void
5287 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5288 82c78e96 2022-08-06 thomas {
5289 82c78e96 2022-08-06 thomas int start, i;
5290 82c78e96 2022-08-06 thomas
5291 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
5292 82c78e96 2022-08-06 thomas
5293 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5294 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5295 82c78e96 2022-08-06 thomas i = 0;
5296 82c78e96 2022-08-06 thomas if (++i == start)
5297 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5298 82c78e96 2022-08-06 thomas }
5299 82c78e96 2022-08-06 thomas
5300 82c78e96 2022-08-06 thomas s->selected_line = 1;
5301 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5302 82c78e96 2022-08-06 thomas }
5303 82c78e96 2022-08-06 thomas
5304 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5305 4fc71f3b 2022-07-12 thomas int, int, int);
5306 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5307 4fc71f3b 2022-07-12 thomas int, int);
5308 4fc71f3b 2022-07-12 thomas
5309 adf4c9e0 2022-07-03 thomas static const struct got_error *
5310 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5311 e5a0f69f 2018-08-18 stsp {
5312 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5313 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5314 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5315 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5316 826082fe 2020-12-10 stsp char *line = NULL;
5317 826082fe 2020-12-10 stsp size_t linesize = 0;
5318 826082fe 2020-12-10 stsp ssize_t linelen;
5319 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5320 e5a0f69f 2018-08-18 stsp
5321 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5322 82c78e96 2022-08-06 thomas
5323 e5a0f69f 2018-08-18 stsp switch (ch) {
5324 05171be4 2022-06-23 thomas case '0':
5325 05171be4 2022-06-23 thomas case '$':
5326 05171be4 2022-06-23 thomas case KEY_RIGHT:
5327 05171be4 2022-06-23 thomas case 'l':
5328 05171be4 2022-06-23 thomas case KEY_LEFT:
5329 05171be4 2022-06-23 thomas case 'h':
5330 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
5331 05171be4 2022-06-23 thomas break;
5332 64453f7e 2020-11-21 stsp case 'a':
5333 3dbaef42 2020-11-24 stsp case 'w':
5334 f2d06bef 2023-01-23 thomas if (ch == 'a') {
5335 f2d06bef 2023-01-23 thomas s->force_text_diff = !s->force_text_diff;
5336 f2d06bef 2023-01-23 thomas view->action = s->force_text_diff ?
5337 f2d06bef 2023-01-23 thomas "force ASCII text enabled" :
5338 f2d06bef 2023-01-23 thomas "force ASCII text disabled";
5339 f2d06bef 2023-01-23 thomas }
5340 f2d06bef 2023-01-23 thomas else if (ch == 'w') {
5341 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5342 f2d06bef 2023-01-23 thomas view->action = s->ignore_whitespace ?
5343 f2d06bef 2023-01-23 thomas "ignore whitespace enabled" :
5344 f2d06bef 2023-01-23 thomas "ignore whitespace disabled";
5345 f2d06bef 2023-01-23 thomas }
5346 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5347 912a3f79 2021-08-30 j break;
5348 912a3f79 2021-08-30 j case 'g':
5349 912a3f79 2021-08-30 j case KEY_HOME:
5350 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5351 07b0611c 2022-06-23 thomas view->count = 0;
5352 64453f7e 2020-11-21 stsp break;
5353 912a3f79 2021-08-30 j case 'G':
5354 912a3f79 2021-08-30 j case KEY_END:
5355 07b0611c 2022-06-23 thomas view->count = 0;
5356 912a3f79 2021-08-30 j if (s->eof)
5357 912a3f79 2021-08-30 j break;
5358 912a3f79 2021-08-30 j
5359 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5360 912a3f79 2021-08-30 j s->eof = 1;
5361 912a3f79 2021-08-30 j break;
5362 1e37a5c2 2019-05-12 jcs case 'k':
5363 1e37a5c2 2019-05-12 jcs case KEY_UP:
5364 f7140bf5 2021-10-17 thomas case CTRL('p'):
5365 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5366 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5367 07b0611c 2022-06-23 thomas else
5368 07b0611c 2022-06-23 thomas view->count = 0;
5369 1e37a5c2 2019-05-12 jcs break;
5370 70f17a53 2022-06-13 thomas case CTRL('u'):
5371 23427b14 2022-06-23 thomas case 'u':
5372 70f17a53 2022-06-13 thomas nscroll /= 2;
5373 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5374 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5375 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5376 1c5e5faa 2022-06-23 thomas case 'b':
5377 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5378 07b0611c 2022-06-23 thomas view->count = 0;
5379 26ed57b2 2018-05-19 stsp break;
5380 07b0611c 2022-06-23 thomas }
5381 1e37a5c2 2019-05-12 jcs i = 0;
5382 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5383 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5384 1e37a5c2 2019-05-12 jcs break;
5385 1e37a5c2 2019-05-12 jcs case 'j':
5386 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5387 f7140bf5 2021-10-17 thomas case CTRL('n'):
5388 1e37a5c2 2019-05-12 jcs if (!s->eof)
5389 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5390 07b0611c 2022-06-23 thomas else
5391 07b0611c 2022-06-23 thomas view->count = 0;
5392 1e37a5c2 2019-05-12 jcs break;
5393 70f17a53 2022-06-13 thomas case CTRL('d'):
5394 23427b14 2022-06-23 thomas case 'd':
5395 70f17a53 2022-06-13 thomas nscroll /= 2;
5396 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5397 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5398 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5399 1c5e5faa 2022-06-23 thomas case 'f':
5400 1e37a5c2 2019-05-12 jcs case ' ':
5401 07b0611c 2022-06-23 thomas if (s->eof) {
5402 07b0611c 2022-06-23 thomas view->count = 0;
5403 1e37a5c2 2019-05-12 jcs break;
5404 07b0611c 2022-06-23 thomas }
5405 1e37a5c2 2019-05-12 jcs i = 0;
5406 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5407 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5408 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5409 826082fe 2020-12-10 stsp if (linelen == -1) {
5410 826082fe 2020-12-10 stsp if (feof(s->f)) {
5411 826082fe 2020-12-10 stsp s->eof = 1;
5412 826082fe 2020-12-10 stsp } else
5413 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5414 34bc9ec9 2019-02-22 stsp break;
5415 826082fe 2020-12-10 stsp }
5416 1e37a5c2 2019-05-12 jcs }
5417 826082fe 2020-12-10 stsp free(line);
5418 1e37a5c2 2019-05-12 jcs break;
5419 82c78e96 2022-08-06 thomas case '(':
5420 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5421 82c78e96 2022-08-06 thomas break;
5422 82c78e96 2022-08-06 thomas case ')':
5423 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5424 82c78e96 2022-08-06 thomas break;
5425 82c78e96 2022-08-06 thomas case '{':
5426 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5427 82c78e96 2022-08-06 thomas break;
5428 82c78e96 2022-08-06 thomas case '}':
5429 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5430 82c78e96 2022-08-06 thomas break;
5431 1e37a5c2 2019-05-12 jcs case '[':
5432 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5433 1e37a5c2 2019-05-12 jcs s->diff_context--;
5434 aa61903a 2021-12-31 thomas s->matched_line = 0;
5435 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5436 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5437 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5438 27829c9e 2020-11-21 stsp s->nlines) {
5439 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5440 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5441 27829c9e 2020-11-21 stsp }
5442 07b0611c 2022-06-23 thomas } else
5443 07b0611c 2022-06-23 thomas view->count = 0;
5444 1e37a5c2 2019-05-12 jcs break;
5445 1e37a5c2 2019-05-12 jcs case ']':
5446 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5447 1e37a5c2 2019-05-12 jcs s->diff_context++;
5448 aa61903a 2021-12-31 thomas s->matched_line = 0;
5449 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5450 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5451 07b0611c 2022-06-23 thomas } else
5452 07b0611c 2022-06-23 thomas view->count = 0;
5453 1e37a5c2 2019-05-12 jcs break;
5454 1e37a5c2 2019-05-12 jcs case '<':
5455 1e37a5c2 2019-05-12 jcs case ',':
5456 777aae21 2022-07-20 thomas case 'K':
5457 4fc71f3b 2022-07-12 thomas up = 1;
5458 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5459 4fc71f3b 2022-07-12 thomas case '>':
5460 4fc71f3b 2022-07-12 thomas case '.':
5461 777aae21 2022-07-20 thomas case 'J':
5462 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5463 07b0611c 2022-06-23 thomas view->count = 0;
5464 48ae06ee 2018-10-18 stsp break;
5465 07b0611c 2022-06-23 thomas }
5466 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5467 6524637e 2019-02-21 stsp
5468 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5469 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5470 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5471 15a087fe 2019-02-21 stsp
5472 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5473 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5474 4fc71f3b 2022-07-12 thomas if (err)
5475 4fc71f3b 2022-07-12 thomas break;
5476 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5477 15a087fe 2019-02-21 stsp
5478 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5479 4fc71f3b 2022-07-12 thomas break;
5480 15a087fe 2019-02-21 stsp
5481 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5482 4fc71f3b 2022-07-12 thomas if (err)
5483 4fc71f3b 2022-07-12 thomas break;
5484 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5485 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5486 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5487 5e224a3e 2019-02-22 stsp
5488 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5489 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5490 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5491 4fc71f3b 2022-07-12 thomas
5492 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5493 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5494 4fc71f3b 2022-07-12 thomas if (err)
5495 4fc71f3b 2022-07-12 thomas break;
5496 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5497 5a8b5076 2020-12-05 stsp
5498 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5499 4fc71f3b 2022-07-12 thomas break;
5500 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5501 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5502 4fc71f3b 2022-07-12 thomas bs->selected_line);
5503 4fc71f3b 2022-07-12 thomas if (id == NULL)
5504 4fc71f3b 2022-07-12 thomas break;
5505 15a087fe 2019-02-21 stsp
5506 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5507 4fc71f3b 2022-07-12 thomas break;
5508 15a087fe 2019-02-21 stsp
5509 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5510 4fc71f3b 2022-07-12 thomas if (err)
5511 4fc71f3b 2022-07-12 thomas break;
5512 4fc71f3b 2022-07-12 thomas }
5513 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5514 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5515 aa61903a 2021-12-31 thomas s->matched_line = 0;
5516 05171be4 2022-06-23 thomas view->x = 0;
5517 1e37a5c2 2019-05-12 jcs
5518 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5519 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5520 1e37a5c2 2019-05-12 jcs break;
5521 1e37a5c2 2019-05-12 jcs default:
5522 07b0611c 2022-06-23 thomas view->count = 0;
5523 1e37a5c2 2019-05-12 jcs break;
5524 26ed57b2 2018-05-19 stsp }
5525 e5a0f69f 2018-08-18 stsp
5526 bcbd79e2 2018-08-19 stsp return err;
5527 26ed57b2 2018-05-19 stsp }
5528 26ed57b2 2018-05-19 stsp
5529 4ed7e80c 2018-05-20 stsp static const struct got_error *
5530 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5531 9f7d7167 2018-04-29 stsp {
5532 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5533 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5534 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5535 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5536 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5537 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5538 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5539 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5540 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5541 3dbaef42 2020-11-24 stsp const char *errstr;
5542 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5543 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5544 70ac5f84 2019-03-28 stsp
5545 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5546 26ed57b2 2018-05-19 stsp switch (ch) {
5547 64453f7e 2020-11-21 stsp case 'a':
5548 64453f7e 2020-11-21 stsp force_text_diff = 1;
5549 3dbaef42 2020-11-24 stsp break;
5550 3dbaef42 2020-11-24 stsp case 'C':
5551 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5552 3dbaef42 2020-11-24 stsp &errstr);
5553 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5554 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5555 8f666e67 2022-02-12 thomas errstr, errstr);
5556 64453f7e 2020-11-21 stsp break;
5557 09b5bff8 2020-02-23 naddy case 'r':
5558 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5559 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5560 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5561 09b5bff8 2020-02-23 naddy optarg);
5562 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5563 09b5bff8 2020-02-23 naddy break;
5564 3dbaef42 2020-11-24 stsp case 'w':
5565 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5566 3dbaef42 2020-11-24 stsp break;
5567 26ed57b2 2018-05-19 stsp default:
5568 17020d27 2019-03-07 stsp usage_diff();
5569 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5570 26ed57b2 2018-05-19 stsp }
5571 26ed57b2 2018-05-19 stsp }
5572 26ed57b2 2018-05-19 stsp
5573 26ed57b2 2018-05-19 stsp argc -= optind;
5574 26ed57b2 2018-05-19 stsp argv += optind;
5575 26ed57b2 2018-05-19 stsp
5576 26ed57b2 2018-05-19 stsp if (argc == 0) {
5577 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5578 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5579 15a94983 2018-12-23 stsp id_str1 = argv[0];
5580 15a94983 2018-12-23 stsp id_str2 = argv[1];
5581 26ed57b2 2018-05-19 stsp } else
5582 26ed57b2 2018-05-19 stsp usage_diff();
5583 eb6600df 2019-01-04 stsp
5584 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5585 7cd52833 2022-06-23 thomas if (error)
5586 7cd52833 2022-06-23 thomas goto done;
5587 7cd52833 2022-06-23 thomas
5588 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5589 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5590 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5591 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5592 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5593 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5594 c156c7a4 2020-12-18 stsp goto done;
5595 a273ac94 2020-02-23 naddy if (worktree)
5596 a273ac94 2020-02-23 naddy repo_path =
5597 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5598 a273ac94 2020-02-23 naddy else
5599 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5600 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5601 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5602 c156c7a4 2020-12-18 stsp goto done;
5603 c156c7a4 2020-12-18 stsp }
5604 a273ac94 2020-02-23 naddy }
5605 a273ac94 2020-02-23 naddy
5606 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5607 eb6600df 2019-01-04 stsp if (error)
5608 eb6600df 2019-01-04 stsp goto done;
5609 26ed57b2 2018-05-19 stsp
5610 a273ac94 2020-02-23 naddy init_curses();
5611 a273ac94 2020-02-23 naddy
5612 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5613 51a10b52 2020-12-26 stsp if (error)
5614 51a10b52 2020-12-26 stsp goto done;
5615 51a10b52 2020-12-26 stsp
5616 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5617 26ed57b2 2018-05-19 stsp if (error)
5618 26ed57b2 2018-05-19 stsp goto done;
5619 26ed57b2 2018-05-19 stsp
5620 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5621 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5622 26ed57b2 2018-05-19 stsp if (error)
5623 26ed57b2 2018-05-19 stsp goto done;
5624 26ed57b2 2018-05-19 stsp
5625 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5626 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5627 26ed57b2 2018-05-19 stsp if (error)
5628 26ed57b2 2018-05-19 stsp goto done;
5629 26ed57b2 2018-05-19 stsp
5630 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5631 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5632 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5633 ea5e7bb5 2018-08-01 stsp goto done;
5634 ea5e7bb5 2018-08-01 stsp }
5635 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5636 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5637 5dc9f4bc 2018-08-04 stsp if (error)
5638 5dc9f4bc 2018-08-04 stsp goto done;
5639 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5640 26ed57b2 2018-05-19 stsp done:
5641 3dbaef42 2020-11-24 stsp free(label1);
5642 3dbaef42 2020-11-24 stsp free(label2);
5643 c02c541e 2019-03-29 stsp free(repo_path);
5644 a273ac94 2020-02-23 naddy free(cwd);
5645 1d0f4054 2021-06-17 stsp if (repo) {
5646 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5647 1d0f4054 2021-06-17 stsp if (error == NULL)
5648 1d0f4054 2021-06-17 stsp error = close_err;
5649 1d0f4054 2021-06-17 stsp }
5650 a273ac94 2020-02-23 naddy if (worktree)
5651 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5652 7cd52833 2022-06-23 thomas if (pack_fds) {
5653 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5654 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5655 7cd52833 2022-06-23 thomas if (error == NULL)
5656 7cd52833 2022-06-23 thomas error = pack_err;
5657 7cd52833 2022-06-23 thomas }
5658 51a10b52 2020-12-26 stsp tog_free_refs();
5659 26ed57b2 2018-05-19 stsp return error;
5660 9f7d7167 2018-04-29 stsp }
5661 9f7d7167 2018-04-29 stsp
5662 4ed7e80c 2018-05-20 stsp __dead static void
5663 9f7d7167 2018-04-29 stsp usage_blame(void)
5664 9f7d7167 2018-04-29 stsp {
5665 80ddbec8 2018-04-29 stsp endwin();
5666 91198554 2022-06-23 thomas fprintf(stderr,
5667 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5668 9f7d7167 2018-04-29 stsp getprogname());
5669 9f7d7167 2018-04-29 stsp exit(1);
5670 9f7d7167 2018-04-29 stsp }
5671 84451b3e 2018-07-10 stsp
5672 84451b3e 2018-07-10 stsp struct tog_blame_line {
5673 84451b3e 2018-07-10 stsp int annotated;
5674 84451b3e 2018-07-10 stsp struct got_object_id *id;
5675 84451b3e 2018-07-10 stsp };
5676 9f7d7167 2018-04-29 stsp
5677 4ed7e80c 2018-05-20 stsp static const struct got_error *
5678 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5679 84451b3e 2018-07-10 stsp {
5680 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5681 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5682 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5683 84451b3e 2018-07-10 stsp const struct got_error *err;
5684 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5685 826082fe 2020-12-10 stsp char *line = NULL;
5686 826082fe 2020-12-10 stsp size_t linesize = 0;
5687 826082fe 2020-12-10 stsp ssize_t linelen;
5688 84451b3e 2018-07-10 stsp wchar_t *wline;
5689 27a741e5 2019-09-11 stsp int width;
5690 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5691 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5692 ab089a2a 2018-07-12 stsp char *id_str;
5693 11b20872 2019-11-08 stsp struct tog_color *tc;
5694 ab089a2a 2018-07-12 stsp
5695 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5696 ab089a2a 2018-07-12 stsp if (err)
5697 ab089a2a 2018-07-12 stsp return err;
5698 84451b3e 2018-07-10 stsp
5699 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5700 f7d12f7e 2018-08-01 stsp werase(view->window);
5701 84451b3e 2018-07-10 stsp
5702 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5703 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5704 ab089a2a 2018-07-12 stsp free(id_str);
5705 ab089a2a 2018-07-12 stsp return err;
5706 ab089a2a 2018-07-12 stsp }
5707 ab089a2a 2018-07-12 stsp
5708 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5709 ab089a2a 2018-07-12 stsp free(line);
5710 2550e4c3 2018-07-13 stsp line = NULL;
5711 1cae65b4 2019-09-22 stsp if (err)
5712 1cae65b4 2019-09-22 stsp return err;
5713 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5714 a3404814 2018-09-02 stsp wstandout(view->window);
5715 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5716 11b20872 2019-11-08 stsp if (tc)
5717 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5718 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5719 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5720 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5721 11b20872 2019-11-08 stsp if (tc)
5722 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5723 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5724 a3404814 2018-09-02 stsp wstandend(view->window);
5725 2550e4c3 2018-07-13 stsp free(wline);
5726 2550e4c3 2018-07-13 stsp wline = NULL;
5727 ab089a2a 2018-07-12 stsp
5728 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5729 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5730 07dd3ed3 2022-08-06 thomas
5731 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5732 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5733 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5734 ab089a2a 2018-07-12 stsp free(id_str);
5735 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5736 ab089a2a 2018-07-12 stsp }
5737 ab089a2a 2018-07-12 stsp free(id_str);
5738 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5739 3f60a8ef 2018-07-10 stsp free(line);
5740 2550e4c3 2018-07-13 stsp line = NULL;
5741 3f60a8ef 2018-07-10 stsp if (err)
5742 3f60a8ef 2018-07-10 stsp return err;
5743 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5744 2550e4c3 2018-07-13 stsp free(wline);
5745 2550e4c3 2018-07-13 stsp wline = NULL;
5746 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5747 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5748 3f60a8ef 2018-07-10 stsp
5749 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5750 05171be4 2022-06-23 thomas view->maxx = 0;
5751 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5752 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5753 826082fe 2020-12-10 stsp if (linelen == -1) {
5754 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5755 826082fe 2020-12-10 stsp s->eof = 1;
5756 826082fe 2020-12-10 stsp break;
5757 826082fe 2020-12-10 stsp }
5758 84451b3e 2018-07-10 stsp free(line);
5759 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5760 84451b3e 2018-07-10 stsp }
5761 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5762 07dd3ed3 2022-08-06 thomas continue;
5763 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5764 826082fe 2020-12-10 stsp continue;
5765 cbea3800 2022-06-23 thomas
5766 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5767 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5768 cbea3800 2022-06-23 thomas if (err) {
5769 cbea3800 2022-06-23 thomas free(line);
5770 cbea3800 2022-06-23 thomas return err;
5771 cbea3800 2022-06-23 thomas }
5772 cbea3800 2022-06-23 thomas free(wline);
5773 cbea3800 2022-06-23 thomas wline = NULL;
5774 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5775 84451b3e 2018-07-10 stsp
5776 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5777 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5778 b700b5d6 2018-07-10 stsp
5779 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5780 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5781 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5782 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5783 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5784 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5785 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5786 8d0fe45a 2019-08-12 stsp char *id_str;
5787 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5788 91198554 2022-06-23 thomas blame_line->id);
5789 8d0fe45a 2019-08-12 stsp if (err) {
5790 8d0fe45a 2019-08-12 stsp free(line);
5791 8d0fe45a 2019-08-12 stsp return err;
5792 8d0fe45a 2019-08-12 stsp }
5793 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5794 11b20872 2019-11-08 stsp if (tc)
5795 11b20872 2019-11-08 stsp wattr_on(view->window,
5796 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5797 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5798 11b20872 2019-11-08 stsp if (tc)
5799 11b20872 2019-11-08 stsp wattr_off(view->window,
5800 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5801 8d0fe45a 2019-08-12 stsp free(id_str);
5802 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5803 8d0fe45a 2019-08-12 stsp } else {
5804 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5805 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5806 84451b3e 2018-07-10 stsp }
5807 ee41ec32 2018-07-10 stsp } else {
5808 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5809 ee41ec32 2018-07-10 stsp prev_id = NULL;
5810 ee41ec32 2018-07-10 stsp }
5811 84451b3e 2018-07-10 stsp
5812 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5813 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5814 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5815 27a741e5 2019-09-11 stsp
5816 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5817 41605754 2020-11-12 stsp width = 9;
5818 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5819 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5820 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5821 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5822 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5823 41605754 2020-11-12 stsp if (err) {
5824 41605754 2020-11-12 stsp free(line);
5825 41605754 2020-11-12 stsp return err;
5826 41605754 2020-11-12 stsp }
5827 41605754 2020-11-12 stsp width += 9;
5828 41605754 2020-11-12 stsp } else {
5829 923086fd 2022-06-23 thomas int skip;
5830 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5831 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5832 923086fd 2022-06-23 thomas if (err) {
5833 923086fd 2022-06-23 thomas free(line);
5834 923086fd 2022-06-23 thomas return err;
5835 05171be4 2022-06-23 thomas }
5836 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5837 02bce7e2 2022-06-23 thomas width += 9;
5838 41605754 2020-11-12 stsp free(wline);
5839 41605754 2020-11-12 stsp wline = NULL;
5840 41605754 2020-11-12 stsp }
5841 41605754 2020-11-12 stsp
5842 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5843 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5844 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5845 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5846 84451b3e 2018-07-10 stsp }
5847 826082fe 2020-12-10 stsp free(line);
5848 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5849 84451b3e 2018-07-10 stsp
5850 a5d43cac 2022-07-01 thomas view_border(view);
5851 84451b3e 2018-07-10 stsp
5852 84451b3e 2018-07-10 stsp return NULL;
5853 84451b3e 2018-07-10 stsp }
5854 84451b3e 2018-07-10 stsp
5855 84451b3e 2018-07-10 stsp static const struct got_error *
5856 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5857 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5858 84451b3e 2018-07-10 stsp {
5859 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5860 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5861 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5862 1a76625f 2018-10-22 stsp int errcode;
5863 84451b3e 2018-07-10 stsp
5864 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5865 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5866 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5867 84451b3e 2018-07-10 stsp
5868 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5869 1a76625f 2018-10-22 stsp if (errcode)
5870 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5871 84451b3e 2018-07-10 stsp
5872 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5873 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5874 d68a0a7d 2018-07-10 stsp goto done;
5875 d68a0a7d 2018-07-10 stsp }
5876 d68a0a7d 2018-07-10 stsp
5877 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5878 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5879 d68a0a7d 2018-07-10 stsp
5880 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5881 d68a0a7d 2018-07-10 stsp if (line->annotated)
5882 d68a0a7d 2018-07-10 stsp goto done;
5883 d68a0a7d 2018-07-10 stsp
5884 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5885 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5886 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5887 84451b3e 2018-07-10 stsp goto done;
5888 84451b3e 2018-07-10 stsp }
5889 84451b3e 2018-07-10 stsp line->annotated = 1;
5890 84451b3e 2018-07-10 stsp done:
5891 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5892 1a76625f 2018-10-22 stsp if (errcode)
5893 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5894 84451b3e 2018-07-10 stsp return err;
5895 84451b3e 2018-07-10 stsp }
5896 84451b3e 2018-07-10 stsp
5897 84451b3e 2018-07-10 stsp static void *
5898 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5899 84451b3e 2018-07-10 stsp {
5900 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5901 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5902 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5903 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5904 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5905 00a8878e 2022-07-01 thomas
5906 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5907 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5908 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5909 9117a7b7 2022-07-01 thomas
5910 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5911 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5912 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5913 9117a7b7 2022-07-01 thomas goto done;
5914 9117a7b7 2022-07-01 thomas }
5915 18430de3 2018-07-10 stsp
5916 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5917 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5918 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5919 9117a7b7 2022-07-01 thomas goto done;
5920 9117a7b7 2022-07-01 thomas }
5921 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5922 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5923 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5924 9117a7b7 2022-07-01 thomas goto done;
5925 9117a7b7 2022-07-01 thomas }
5926 9117a7b7 2022-07-01 thomas
5927 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5928 61266923 2020-01-14 stsp if (err)
5929 9117a7b7 2022-07-01 thomas goto done;
5930 61266923 2020-01-14 stsp
5931 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5932 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5933 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5934 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5935 fc06ba56 2019-08-22 stsp err = NULL;
5936 18430de3 2018-07-10 stsp
5937 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5938 9117a7b7 2022-07-01 thomas if (errcode) {
5939 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5940 9117a7b7 2022-07-01 thomas goto done;
5941 9117a7b7 2022-07-01 thomas }
5942 18430de3 2018-07-10 stsp
5943 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5944 1d0f4054 2021-06-17 stsp if (err == NULL)
5945 1d0f4054 2021-06-17 stsp err = close_err;
5946 c9beca56 2018-07-22 stsp ta->repo = NULL;
5947 c9beca56 2018-07-22 stsp *ta->complete = 1;
5948 18430de3 2018-07-10 stsp
5949 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5950 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5951 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5952 18430de3 2018-07-10 stsp
5953 9117a7b7 2022-07-01 thomas done:
5954 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5955 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5956 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5957 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5958 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5959 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5960 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5961 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5962 00a8878e 2022-07-01 thomas
5963 18430de3 2018-07-10 stsp return (void *)err;
5964 84451b3e 2018-07-10 stsp }
5965 84451b3e 2018-07-10 stsp
5966 245d91c1 2018-07-12 stsp static struct got_object_id *
5967 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5968 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5969 245d91c1 2018-07-12 stsp {
5970 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5971 8d0fe45a 2019-08-12 stsp
5972 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5973 8d0fe45a 2019-08-12 stsp return NULL;
5974 b880a918 2018-07-10 stsp
5975 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5976 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5977 4fc71f3b 2022-07-12 thomas return NULL;
5978 4fc71f3b 2022-07-12 thomas
5979 4fc71f3b 2022-07-12 thomas return line->id;
5980 4fc71f3b 2022-07-12 thomas }
5981 4fc71f3b 2022-07-12 thomas
5982 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5983 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5984 4fc71f3b 2022-07-12 thomas int lineno)
5985 4fc71f3b 2022-07-12 thomas {
5986 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5987 4fc71f3b 2022-07-12 thomas
5988 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5989 4fc71f3b 2022-07-12 thomas return NULL;
5990 4fc71f3b 2022-07-12 thomas
5991 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5992 245d91c1 2018-07-12 stsp if (!line->annotated)
5993 245d91c1 2018-07-12 stsp return NULL;
5994 245d91c1 2018-07-12 stsp
5995 245d91c1 2018-07-12 stsp return line->id;
5996 b880a918 2018-07-10 stsp }
5997 245d91c1 2018-07-12 stsp
5998 b880a918 2018-07-10 stsp static const struct got_error *
5999 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
6000 a70480e0 2018-06-23 stsp {
6001 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
6002 245d91c1 2018-07-12 stsp int i;
6003 245d91c1 2018-07-12 stsp
6004 245d91c1 2018-07-12 stsp if (blame->thread) {
6005 1a76625f 2018-10-22 stsp int errcode;
6006 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6007 1a76625f 2018-10-22 stsp if (errcode)
6008 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6009 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
6010 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
6011 1a76625f 2018-10-22 stsp if (errcode)
6012 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
6013 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6014 1a76625f 2018-10-22 stsp if (errcode)
6015 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
6016 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
6017 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
6018 245d91c1 2018-07-12 stsp err = NULL;
6019 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
6020 245d91c1 2018-07-12 stsp }
6021 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
6022 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
6023 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
6024 1d0f4054 2021-06-17 stsp if (err == NULL)
6025 1d0f4054 2021-06-17 stsp err = close_err;
6026 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
6027 245d91c1 2018-07-12 stsp }
6028 245d91c1 2018-07-12 stsp if (blame->f) {
6029 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
6030 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
6031 245d91c1 2018-07-12 stsp blame->f = NULL;
6032 245d91c1 2018-07-12 stsp }
6033 57670559 2018-12-23 stsp if (blame->lines) {
6034 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
6035 57670559 2018-12-23 stsp free(blame->lines[i].id);
6036 57670559 2018-12-23 stsp free(blame->lines);
6037 57670559 2018-12-23 stsp blame->lines = NULL;
6038 57670559 2018-12-23 stsp }
6039 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
6040 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
6041 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
6042 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6043 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
6044 7cd52833 2022-06-23 thomas if (err == NULL)
6045 7cd52833 2022-06-23 thomas err = pack_err;
6046 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
6047 7cd52833 2022-06-23 thomas }
6048 245d91c1 2018-07-12 stsp return err;
6049 245d91c1 2018-07-12 stsp }
6050 245d91c1 2018-07-12 stsp
6051 245d91c1 2018-07-12 stsp static const struct got_error *
6052 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
6053 fc06ba56 2019-08-22 stsp {
6054 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
6055 fc06ba56 2019-08-22 stsp int *done = arg;
6056 fc06ba56 2019-08-22 stsp int errcode;
6057 fc06ba56 2019-08-22 stsp
6058 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
6059 fc06ba56 2019-08-22 stsp if (errcode)
6060 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6061 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
6062 fc06ba56 2019-08-22 stsp
6063 fc06ba56 2019-08-22 stsp if (*done)
6064 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
6065 fc06ba56 2019-08-22 stsp
6066 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
6067 fc06ba56 2019-08-22 stsp if (errcode)
6068 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
6069 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
6070 fc06ba56 2019-08-22 stsp
6071 fc06ba56 2019-08-22 stsp return err;
6072 fc06ba56 2019-08-22 stsp }
6073 fc06ba56 2019-08-22 stsp
6074 fc06ba56 2019-08-22 stsp static const struct got_error *
6075 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
6076 245d91c1 2018-07-12 stsp {
6077 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
6078 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
6079 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
6080 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6081 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
6082 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
6083 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
6084 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
6085 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6086 a70480e0 2018-06-23 stsp
6087 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
6088 ec242592 2022-04-22 thomas &s->blamed_commit->id);
6089 27d434c2 2018-09-15 stsp if (err)
6090 15a94983 2018-12-23 stsp return err;
6091 f4ae6ddb 2022-07-01 thomas
6092 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
6093 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
6094 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
6095 f4ae6ddb 2022-07-01 thomas goto done;
6096 f4ae6ddb 2022-07-01 thomas }
6097 945f9229 2022-04-16 thomas
6098 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
6099 945f9229 2022-04-16 thomas if (err)
6100 945f9229 2022-04-16 thomas goto done;
6101 27d434c2 2018-09-15 stsp
6102 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
6103 84451b3e 2018-07-10 stsp if (err)
6104 84451b3e 2018-07-10 stsp goto done;
6105 27d434c2 2018-09-15 stsp
6106 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
6107 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6108 84451b3e 2018-07-10 stsp goto done;
6109 84451b3e 2018-07-10 stsp }
6110 a70480e0 2018-06-23 stsp
6111 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
6112 a70480e0 2018-06-23 stsp if (err)
6113 a70480e0 2018-06-23 stsp goto done;
6114 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
6115 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
6116 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
6117 84451b3e 2018-07-10 stsp goto done;
6118 84451b3e 2018-07-10 stsp }
6119 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
6120 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
6121 1fddf795 2021-01-20 stsp if (err)
6122 1fddf795 2021-01-20 stsp goto done;
6123 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
6124 1fddf795 2021-01-20 stsp s->blame_complete = 1;
6125 84451b3e 2018-07-10 stsp goto done;
6126 1fddf795 2021-01-20 stsp }
6127 b02560ec 2019-08-19 stsp
6128 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
6129 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6130 b02560ec 2019-08-19 stsp blame->nlines--;
6131 a70480e0 2018-06-23 stsp
6132 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6133 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
6134 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
6135 84451b3e 2018-07-10 stsp goto done;
6136 84451b3e 2018-07-10 stsp }
6137 a70480e0 2018-06-23 stsp
6138 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
6139 bd24772e 2018-07-11 stsp if (err)
6140 bd24772e 2018-07-11 stsp goto done;
6141 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6142 7cd52833 2022-06-23 thomas pack_fds);
6143 7cd52833 2022-06-23 thomas if (err)
6144 7cd52833 2022-06-23 thomas goto done;
6145 bd24772e 2018-07-11 stsp
6146 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
6147 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
6148 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
6149 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
6150 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6151 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
6152 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
6153 245d91c1 2018-07-12 stsp goto done;
6154 245d91c1 2018-07-12 stsp }
6155 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
6156 245d91c1 2018-07-12 stsp
6157 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
6158 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
6159 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
6160 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
6161 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
6162 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
6163 a5388363 2020-12-01 naddy s->blame_complete = 0;
6164 f5a09613 2020-12-13 naddy
6165 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6166 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
6167 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
6168 f5a09613 2020-12-13 naddy s->selected_line = 1;
6169 f5a09613 2020-12-13 naddy }
6170 aa61903a 2021-12-31 thomas s->matched_line = 0;
6171 245d91c1 2018-07-12 stsp
6172 245d91c1 2018-07-12 stsp done:
6173 945f9229 2022-04-16 thomas if (commit)
6174 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6175 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
6176 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
6177 245d91c1 2018-07-12 stsp if (blob)
6178 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
6179 27d434c2 2018-09-15 stsp free(obj_id);
6180 245d91c1 2018-07-12 stsp if (err)
6181 245d91c1 2018-07-12 stsp stop_blame(blame);
6182 245d91c1 2018-07-12 stsp return err;
6183 245d91c1 2018-07-12 stsp }
6184 245d91c1 2018-07-12 stsp
6185 245d91c1 2018-07-12 stsp static const struct got_error *
6186 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
6187 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6188 245d91c1 2018-07-12 stsp {
6189 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
6190 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6191 dbc6a6b6 2018-07-12 stsp
6192 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
6193 245d91c1 2018-07-12 stsp
6194 c4843652 2019-08-12 stsp s->path = strdup(path);
6195 c4843652 2019-08-12 stsp if (s->path == NULL)
6196 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
6197 c4843652 2019-08-12 stsp
6198 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6199 c4843652 2019-08-12 stsp if (err) {
6200 c4843652 2019-08-12 stsp free(s->path);
6201 7cbe629d 2018-08-04 stsp return err;
6202 c4843652 2019-08-12 stsp }
6203 245d91c1 2018-07-12 stsp
6204 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6205 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
6206 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
6207 fb2756b9 2018-08-04 stsp s->selected_line = 1;
6208 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
6209 fb2756b9 2018-08-04 stsp s->repo = repo;
6210 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
6211 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
6212 7cbe629d 2018-08-04 stsp
6213 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6214 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6215 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6216 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6217 11b20872 2019-11-08 stsp if (err)
6218 11b20872 2019-11-08 stsp return err;
6219 11b20872 2019-11-08 stsp }
6220 11b20872 2019-11-08 stsp
6221 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
6222 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
6223 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
6224 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
6225 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
6226 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_blame_view;
6227 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
6228 e5a0f69f 2018-08-18 stsp
6229 a5388363 2020-12-01 naddy return run_blame(view);
6230 7cbe629d 2018-08-04 stsp }
6231 7cbe629d 2018-08-04 stsp
6232 e5a0f69f 2018-08-18 stsp static const struct got_error *
6233 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
6234 7cbe629d 2018-08-04 stsp {
6235 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6236 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6237 7cbe629d 2018-08-04 stsp
6238 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
6239 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
6240 e5a0f69f 2018-08-18 stsp
6241 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
6242 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
6243 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6244 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6245 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
6246 7cbe629d 2018-08-04 stsp }
6247 e5a0f69f 2018-08-18 stsp
6248 e5a0f69f 2018-08-18 stsp free(s->path);
6249 11b20872 2019-11-08 stsp free_colors(&s->colors);
6250 e5a0f69f 2018-08-18 stsp return err;
6251 7cbe629d 2018-08-04 stsp }
6252 7cbe629d 2018-08-04 stsp
6253 7cbe629d 2018-08-04 stsp static const struct got_error *
6254 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
6255 6c4c42e0 2019-06-24 stsp {
6256 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
6257 6c4c42e0 2019-06-24 stsp
6258 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
6259 6c4c42e0 2019-06-24 stsp return NULL;
6260 2a7d3cd7 2022-09-17 thomas }
6261 2a7d3cd7 2022-09-17 thomas
6262 2a7d3cd7 2022-09-17 thomas static void
6263 2a7d3cd7 2022-09-17 thomas search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6264 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
6265 2a7d3cd7 2022-09-17 thomas {
6266 2a7d3cd7 2022-09-17 thomas struct tog_blame_view_state *s = &view->state.blame;
6267 2a7d3cd7 2022-09-17 thomas
6268 2a7d3cd7 2022-09-17 thomas *f = s->blame.f;
6269 2a7d3cd7 2022-09-17 thomas *nlines = s->blame.nlines;
6270 2a7d3cd7 2022-09-17 thomas *line_offsets = s->blame.line_offsets;
6271 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
6272 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
6273 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
6274 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
6275 6c4c42e0 2019-06-24 stsp }
6276 6c4c42e0 2019-06-24 stsp
6277 6c4c42e0 2019-06-24 stsp static const struct got_error *
6278 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6279 7cbe629d 2018-08-04 stsp {
6280 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6281 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6282 2b380cc8 2018-10-24 stsp int errcode;
6283 2b380cc8 2018-10-24 stsp
6284 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6285 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6286 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6287 2b380cc8 2018-10-24 stsp if (errcode)
6288 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6289 51fe7530 2019-08-19 stsp
6290 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6291 2b380cc8 2018-10-24 stsp }
6292 e5a0f69f 2018-08-18 stsp
6293 51fe7530 2019-08-19 stsp if (s->blame_complete)
6294 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6295 51fe7530 2019-08-19 stsp
6296 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6297 e5a0f69f 2018-08-18 stsp
6298 a5d43cac 2022-07-01 thomas view_border(view);
6299 e5a0f69f 2018-08-18 stsp return err;
6300 e5a0f69f 2018-08-18 stsp }
6301 e5a0f69f 2018-08-18 stsp
6302 e5a0f69f 2018-08-18 stsp static const struct got_error *
6303 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6304 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6305 eaeaa612 2022-07-20 thomas {
6306 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6307 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6308 eaeaa612 2022-07-20 thomas
6309 eaeaa612 2022-07-20 thomas *new_view = NULL;
6310 eaeaa612 2022-07-20 thomas
6311 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6312 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6313 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6314 eaeaa612 2022-07-20 thomas
6315 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6316 eaeaa612 2022-07-20 thomas if (err)
6317 eaeaa612 2022-07-20 thomas view_close(log_view);
6318 eaeaa612 2022-07-20 thomas else
6319 eaeaa612 2022-07-20 thomas *new_view = log_view;
6320 eaeaa612 2022-07-20 thomas
6321 eaeaa612 2022-07-20 thomas return err;
6322 eaeaa612 2022-07-20 thomas }
6323 eaeaa612 2022-07-20 thomas
6324 eaeaa612 2022-07-20 thomas static const struct got_error *
6325 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6326 e5a0f69f 2018-08-18 stsp {
6327 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6328 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6329 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6330 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6331 a5d43cac 2022-07-01 thomas
6332 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6333 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6334 a5d43cac 2022-07-01 thomas --eos; /* border */
6335 7cbe629d 2018-08-04 stsp
6336 e5a0f69f 2018-08-18 stsp switch (ch) {
6337 05171be4 2022-06-23 thomas case '0':
6338 05171be4 2022-06-23 thomas case '$':
6339 05171be4 2022-06-23 thomas case KEY_RIGHT:
6340 05171be4 2022-06-23 thomas case 'l':
6341 05171be4 2022-06-23 thomas case KEY_LEFT:
6342 05171be4 2022-06-23 thomas case 'h':
6343 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
6344 05171be4 2022-06-23 thomas break;
6345 1e37a5c2 2019-05-12 jcs case 'q':
6346 1e37a5c2 2019-05-12 jcs s->done = 1;
6347 4deef56f 2021-09-02 naddy break;
6348 4deef56f 2021-09-02 naddy case 'g':
6349 4deef56f 2021-09-02 naddy case KEY_HOME:
6350 4deef56f 2021-09-02 naddy s->selected_line = 1;
6351 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6352 07b0611c 2022-06-23 thomas view->count = 0;
6353 4deef56f 2021-09-02 naddy break;
6354 4deef56f 2021-09-02 naddy case 'G':
6355 4deef56f 2021-09-02 naddy case KEY_END:
6356 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6357 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6358 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6359 4deef56f 2021-09-02 naddy } else {
6360 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6361 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6362 4deef56f 2021-09-02 naddy }
6363 07b0611c 2022-06-23 thomas view->count = 0;
6364 1e37a5c2 2019-05-12 jcs break;
6365 1e37a5c2 2019-05-12 jcs case 'k':
6366 1e37a5c2 2019-05-12 jcs case KEY_UP:
6367 f7140bf5 2021-10-17 thomas case CTRL('p'):
6368 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6369 1e37a5c2 2019-05-12 jcs s->selected_line--;
6370 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6371 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6372 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6373 07b0611c 2022-06-23 thomas else
6374 07b0611c 2022-06-23 thomas view->count = 0;
6375 1e37a5c2 2019-05-12 jcs break;
6376 70f17a53 2022-06-13 thomas case CTRL('u'):
6377 23427b14 2022-06-23 thomas case 'u':
6378 70f17a53 2022-06-13 thomas nscroll /= 2;
6379 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6380 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6381 ea025d1d 2020-02-22 naddy case CTRL('b'):
6382 1c5e5faa 2022-06-23 thomas case 'b':
6383 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6384 07b0611c 2022-06-23 thomas if (view->count > 1)
6385 07b0611c 2022-06-23 thomas nscroll += nscroll;
6386 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6387 07b0611c 2022-06-23 thomas view->count = 0;
6388 e5a0f69f 2018-08-18 stsp break;
6389 1e37a5c2 2019-05-12 jcs }
6390 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6391 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6392 1e37a5c2 2019-05-12 jcs else
6393 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6394 1e37a5c2 2019-05-12 jcs break;
6395 1e37a5c2 2019-05-12 jcs case 'j':
6396 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6397 f7140bf5 2021-10-17 thomas case CTRL('n'):
6398 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6399 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6400 1e37a5c2 2019-05-12 jcs s->selected_line++;
6401 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6402 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6403 07b0611c 2022-06-23 thomas else
6404 07b0611c 2022-06-23 thomas view->count = 0;
6405 1e37a5c2 2019-05-12 jcs break;
6406 1c5e5faa 2022-06-23 thomas case 'c':
6407 1e37a5c2 2019-05-12 jcs case 'p': {
6408 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6409 07b0611c 2022-06-23 thomas
6410 07b0611c 2022-06-23 thomas view->count = 0;
6411 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6412 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6413 1e37a5c2 2019-05-12 jcs if (id == NULL)
6414 e5a0f69f 2018-08-18 stsp break;
6415 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6416 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6417 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6418 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6419 1e37a5c2 2019-05-12 jcs int obj_type;
6420 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6421 1e37a5c2 2019-05-12 jcs s->repo, id);
6422 e5a0f69f 2018-08-18 stsp if (err)
6423 e5a0f69f 2018-08-18 stsp break;
6424 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6425 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6426 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6427 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6428 e5a0f69f 2018-08-18 stsp break;
6429 e5a0f69f 2018-08-18 stsp }
6430 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6431 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6432 ec242592 2022-04-22 thomas s->repo, &pid->id);
6433 945f9229 2022-04-16 thomas if (err)
6434 945f9229 2022-04-16 thomas break;
6435 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6436 945f9229 2022-04-16 thomas pcommit, s->path);
6437 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6438 e5a0f69f 2018-08-18 stsp if (err) {
6439 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6440 1e37a5c2 2019-05-12 jcs err = NULL;
6441 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6442 e5a0f69f 2018-08-18 stsp break;
6443 e5a0f69f 2018-08-18 stsp }
6444 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6445 1e37a5c2 2019-05-12 jcs blob_id);
6446 1e37a5c2 2019-05-12 jcs free(blob_id);
6447 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6448 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6449 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6450 e5a0f69f 2018-08-18 stsp break;
6451 1e37a5c2 2019-05-12 jcs }
6452 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6453 ec242592 2022-04-22 thomas &pid->id);
6454 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6455 1e37a5c2 2019-05-12 jcs } else {
6456 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6457 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6458 1e37a5c2 2019-05-12 jcs break;
6459 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6460 1e37a5c2 2019-05-12 jcs id);
6461 1e37a5c2 2019-05-12 jcs }
6462 1e37a5c2 2019-05-12 jcs if (err)
6463 e5a0f69f 2018-08-18 stsp break;
6464 1e37a5c2 2019-05-12 jcs s->done = 1;
6465 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6466 1e37a5c2 2019-05-12 jcs s->done = 0;
6467 1e37a5c2 2019-05-12 jcs if (thread_err)
6468 1e37a5c2 2019-05-12 jcs break;
6469 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6470 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6471 a5388363 2020-12-01 naddy err = run_blame(view);
6472 1e37a5c2 2019-05-12 jcs if (err)
6473 1e37a5c2 2019-05-12 jcs break;
6474 1e37a5c2 2019-05-12 jcs break;
6475 1e37a5c2 2019-05-12 jcs }
6476 1c5e5faa 2022-06-23 thomas case 'C': {
6477 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6478 07b0611c 2022-06-23 thomas
6479 07b0611c 2022-06-23 thomas view->count = 0;
6480 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6481 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6482 1e37a5c2 2019-05-12 jcs break;
6483 1e37a5c2 2019-05-12 jcs s->done = 1;
6484 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6485 1e37a5c2 2019-05-12 jcs s->done = 0;
6486 1e37a5c2 2019-05-12 jcs if (thread_err)
6487 1e37a5c2 2019-05-12 jcs break;
6488 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6489 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6490 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6491 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6492 a5388363 2020-12-01 naddy err = run_blame(view);
6493 1e37a5c2 2019-05-12 jcs if (err)
6494 1e37a5c2 2019-05-12 jcs break;
6495 1e37a5c2 2019-05-12 jcs break;
6496 1e37a5c2 2019-05-12 jcs }
6497 2a31b33b 2022-07-23 thomas case 'L':
6498 eaeaa612 2022-07-20 thomas view->count = 0;
6499 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6500 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6501 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6502 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6503 eaeaa612 2022-07-20 thomas break;
6504 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6505 1e37a5c2 2019-05-12 jcs case '\r': {
6506 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6507 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6508 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6509 07b0611c 2022-06-23 thomas
6510 07b0611c 2022-06-23 thomas view->count = 0;
6511 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6512 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6513 1e37a5c2 2019-05-12 jcs if (id == NULL)
6514 1e37a5c2 2019-05-12 jcs break;
6515 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6516 1e37a5c2 2019-05-12 jcs if (err)
6517 1e37a5c2 2019-05-12 jcs break;
6518 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6519 4fc71f3b 2022-07-12 thomas if (*new_view) {
6520 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6521 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6522 4fc71f3b 2022-07-12 thomas if (err)
6523 4fc71f3b 2022-07-12 thomas break;
6524 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6525 4fc71f3b 2022-07-12 thomas } else {
6526 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6527 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6528 a5d43cac 2022-07-01 thomas
6529 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6530 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6531 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6532 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6533 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6534 4fc71f3b 2022-07-12 thomas break;
6535 4fc71f3b 2022-07-12 thomas }
6536 15a94983 2018-12-23 stsp }
6537 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6538 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6539 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6540 1e37a5c2 2019-05-12 jcs if (err) {
6541 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6542 1e37a5c2 2019-05-12 jcs break;
6543 1e37a5c2 2019-05-12 jcs }
6544 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6545 4fc71f3b 2022-07-12 thomas s->selected_line;
6546 4fc71f3b 2022-07-12 thomas if (*new_view)
6547 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6548 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6549 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6550 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6551 a5d43cac 2022-07-01 thomas if (err)
6552 a5d43cac 2022-07-01 thomas break;
6553 a5d43cac 2022-07-01 thomas }
6554 a5d43cac 2022-07-01 thomas
6555 e78dc838 2020-12-04 stsp view->focussed = 0;
6556 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6557 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6558 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6559 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6560 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6561 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6562 1e37a5c2 2019-05-12 jcs if (err)
6563 34bc9ec9 2019-02-22 stsp break;
6564 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6565 40236d76 2022-06-23 thomas if (err)
6566 40236d76 2022-06-23 thomas break;
6567 e78dc838 2020-12-04 stsp view->focus_child = 1;
6568 1e37a5c2 2019-05-12 jcs } else
6569 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6570 1e37a5c2 2019-05-12 jcs if (err)
6571 e5a0f69f 2018-08-18 stsp break;
6572 1e37a5c2 2019-05-12 jcs break;
6573 1e37a5c2 2019-05-12 jcs }
6574 70f17a53 2022-06-13 thomas case CTRL('d'):
6575 23427b14 2022-06-23 thomas case 'd':
6576 70f17a53 2022-06-13 thomas nscroll /= 2;
6577 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6578 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6579 ea025d1d 2020-02-22 naddy case CTRL('f'):
6580 1c5e5faa 2022-06-23 thomas case 'f':
6581 1e37a5c2 2019-05-12 jcs case ' ':
6582 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6583 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6584 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6585 07b0611c 2022-06-23 thomas view->count = 0;
6586 e5a0f69f 2018-08-18 stsp break;
6587 1e37a5c2 2019-05-12 jcs }
6588 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6589 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6590 70f17a53 2022-06-13 thomas s->selected_line +=
6591 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6592 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6593 1e37a5c2 2019-05-12 jcs }
6594 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6595 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6596 1e37a5c2 2019-05-12 jcs else
6597 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6598 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6599 1e37a5c2 2019-05-12 jcs break;
6600 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6601 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6602 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6603 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6604 1e37a5c2 2019-05-12 jcs }
6605 1e37a5c2 2019-05-12 jcs break;
6606 1e37a5c2 2019-05-12 jcs default:
6607 07b0611c 2022-06-23 thomas view->count = 0;
6608 1e37a5c2 2019-05-12 jcs break;
6609 a70480e0 2018-06-23 stsp }
6610 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6611 adf4c9e0 2022-07-03 thomas }
6612 adf4c9e0 2022-07-03 thomas
6613 adf4c9e0 2022-07-03 thomas static const struct got_error *
6614 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6615 adf4c9e0 2022-07-03 thomas {
6616 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6617 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6618 adf4c9e0 2022-07-03 thomas
6619 adf4c9e0 2022-07-03 thomas view->count = 0;
6620 adf4c9e0 2022-07-03 thomas s->done = 1;
6621 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6622 adf4c9e0 2022-07-03 thomas s->done = 0;
6623 adf4c9e0 2022-07-03 thomas if (err)
6624 adf4c9e0 2022-07-03 thomas return err;
6625 adf4c9e0 2022-07-03 thomas return run_blame(view);
6626 a70480e0 2018-06-23 stsp }
6627 a70480e0 2018-06-23 stsp
6628 a70480e0 2018-06-23 stsp static const struct got_error *
6629 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6630 9f7d7167 2018-04-29 stsp {
6631 a70480e0 2018-06-23 stsp const struct got_error *error;
6632 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6633 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6634 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6635 0587e10c 2020-07-23 stsp char *link_target = NULL;
6636 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6637 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6638 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6639 a70480e0 2018-06-23 stsp int ch;
6640 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6641 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6642 a70480e0 2018-06-23 stsp
6643 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6644 a70480e0 2018-06-23 stsp switch (ch) {
6645 a70480e0 2018-06-23 stsp case 'c':
6646 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6647 a70480e0 2018-06-23 stsp break;
6648 69069811 2018-08-02 stsp case 'r':
6649 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6650 69069811 2018-08-02 stsp if (repo_path == NULL)
6651 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6652 9ba1d308 2019-10-21 stsp optarg);
6653 69069811 2018-08-02 stsp break;
6654 a70480e0 2018-06-23 stsp default:
6655 17020d27 2019-03-07 stsp usage_blame();
6656 a70480e0 2018-06-23 stsp /* NOTREACHED */
6657 a70480e0 2018-06-23 stsp }
6658 a70480e0 2018-06-23 stsp }
6659 a70480e0 2018-06-23 stsp
6660 a70480e0 2018-06-23 stsp argc -= optind;
6661 a70480e0 2018-06-23 stsp argv += optind;
6662 a70480e0 2018-06-23 stsp
6663 f135c941 2020-02-20 stsp if (argc != 1)
6664 a70480e0 2018-06-23 stsp usage_blame();
6665 6962eb72 2020-02-20 stsp
6666 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6667 7cd52833 2022-06-23 thomas if (error != NULL)
6668 7cd52833 2022-06-23 thomas goto done;
6669 7cd52833 2022-06-23 thomas
6670 69069811 2018-08-02 stsp if (repo_path == NULL) {
6671 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6672 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6673 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6674 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6675 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6676 c156c7a4 2020-12-18 stsp goto done;
6677 f135c941 2020-02-20 stsp if (worktree)
6678 eb41ed75 2019-02-05 stsp repo_path =
6679 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6680 f135c941 2020-02-20 stsp else
6681 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6682 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6683 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6684 c156c7a4 2020-12-18 stsp goto done;
6685 c156c7a4 2020-12-18 stsp }
6686 f135c941 2020-02-20 stsp }
6687 a915003a 2019-02-05 stsp
6688 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6689 c02c541e 2019-03-29 stsp if (error != NULL)
6690 8e94dd5b 2019-01-04 stsp goto done;
6691 69069811 2018-08-02 stsp
6692 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6693 f135c941 2020-02-20 stsp worktree);
6694 c02c541e 2019-03-29 stsp if (error)
6695 92205607 2019-01-04 stsp goto done;
6696 69069811 2018-08-02 stsp
6697 f135c941 2020-02-20 stsp init_curses();
6698 f135c941 2020-02-20 stsp
6699 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6700 51a10b52 2020-12-26 stsp if (error)
6701 51a10b52 2020-12-26 stsp goto done;
6702 51a10b52 2020-12-26 stsp
6703 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6704 eb41ed75 2019-02-05 stsp if (error)
6705 69069811 2018-08-02 stsp goto done;
6706 a70480e0 2018-06-23 stsp
6707 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6708 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6709 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6710 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6711 a70480e0 2018-06-23 stsp if (error != NULL)
6712 66b4983c 2018-06-23 stsp goto done;
6713 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6714 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6715 a70480e0 2018-06-23 stsp } else {
6716 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6717 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6718 a70480e0 2018-06-23 stsp }
6719 a19e88aa 2018-06-23 stsp if (error != NULL)
6720 8b473291 2019-02-21 stsp goto done;
6721 8b473291 2019-02-21 stsp
6722 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6723 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6724 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6725 e1cd8fed 2018-08-01 stsp goto done;
6726 e1cd8fed 2018-08-01 stsp }
6727 0587e10c 2020-07-23 stsp
6728 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6729 945f9229 2022-04-16 thomas if (error)
6730 945f9229 2022-04-16 thomas goto done;
6731 945f9229 2022-04-16 thomas
6732 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6733 945f9229 2022-04-16 thomas commit, repo);
6734 7cbe629d 2018-08-04 stsp if (error)
6735 7cbe629d 2018-08-04 stsp goto done;
6736 0587e10c 2020-07-23 stsp
6737 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6738 78756c87 2020-11-24 stsp commit_id, repo);
6739 0587e10c 2020-07-23 stsp if (error)
6740 0587e10c 2020-07-23 stsp goto done;
6741 12314ad4 2019-08-31 stsp if (worktree) {
6742 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6743 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6744 12314ad4 2019-08-31 stsp worktree = NULL;
6745 12314ad4 2019-08-31 stsp }
6746 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6747 a70480e0 2018-06-23 stsp done:
6748 69069811 2018-08-02 stsp free(repo_path);
6749 f135c941 2020-02-20 stsp free(in_repo_path);
6750 0587e10c 2020-07-23 stsp free(link_target);
6751 69069811 2018-08-02 stsp free(cwd);
6752 a70480e0 2018-06-23 stsp free(commit_id);
6753 945f9229 2022-04-16 thomas if (commit)
6754 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6755 eb41ed75 2019-02-05 stsp if (worktree)
6756 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6757 1d0f4054 2021-06-17 stsp if (repo) {
6758 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6759 1d0f4054 2021-06-17 stsp if (error == NULL)
6760 1d0f4054 2021-06-17 stsp error = close_err;
6761 1d0f4054 2021-06-17 stsp }
6762 7cd52833 2022-06-23 thomas if (pack_fds) {
6763 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6764 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6765 7cd52833 2022-06-23 thomas if (error == NULL)
6766 7cd52833 2022-06-23 thomas error = pack_err;
6767 7cd52833 2022-06-23 thomas }
6768 51a10b52 2020-12-26 stsp tog_free_refs();
6769 a70480e0 2018-06-23 stsp return error;
6770 ffd1d5e5 2018-06-23 stsp }
6771 ffd1d5e5 2018-06-23 stsp
6772 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6773 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6774 ffd1d5e5 2018-06-23 stsp {
6775 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6776 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6777 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6778 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6779 86f4aab9 2022-09-09 thomas char *index = NULL;
6780 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6781 c72de8ab 2023-02-03 thomas int width, n, nentries, scrollx, i = 1;
6782 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6783 ffd1d5e5 2018-06-23 stsp
6784 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6785 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6786 a5d43cac 2022-07-01 thomas --limit; /* border */
6787 ffd1d5e5 2018-06-23 stsp
6788 f7d12f7e 2018-08-01 stsp werase(view->window);
6789 ffd1d5e5 2018-06-23 stsp
6790 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6791 ffd1d5e5 2018-06-23 stsp return NULL;
6792 ffd1d5e5 2018-06-23 stsp
6793 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6794 f91a2b48 2022-06-23 thomas 0, 0);
6795 ffd1d5e5 2018-06-23 stsp if (err)
6796 ffd1d5e5 2018-06-23 stsp return err;
6797 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6798 a3404814 2018-09-02 stsp wstandout(view->window);
6799 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6800 11b20872 2019-11-08 stsp if (tc)
6801 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6802 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6803 86f4aab9 2022-09-09 thomas free(wline);
6804 86f4aab9 2022-09-09 thomas wline = NULL;
6805 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6806 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6807 11b20872 2019-11-08 stsp if (tc)
6808 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6809 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6810 a3404814 2018-09-02 stsp wstandend(view->window);
6811 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6812 86f4aab9 2022-09-09 thomas return NULL;
6813 07dd3ed3 2022-08-06 thomas
6814 6f5f393a 2022-08-12 thomas i += s->selected;
6815 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6816 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6817 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6818 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6819 07dd3ed3 2022-08-06 thomas }
6820 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6821 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6822 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6823 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6824 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6825 86f4aab9 2022-09-09 thomas free(index);
6826 ce52c690 2018-06-23 stsp if (err)
6827 ce52c690 2018-06-23 stsp return err;
6828 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6829 2550e4c3 2018-07-13 stsp free(wline);
6830 2550e4c3 2018-07-13 stsp wline = NULL;
6831 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6832 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6833 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6834 ffd1d5e5 2018-06-23 stsp return NULL;
6835 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6836 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6837 a1eca9bb 2018-06-23 stsp return NULL;
6838 ffd1d5e5 2018-06-23 stsp
6839 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6840 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6841 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6842 0cf4efb1 2018-09-29 stsp if (view->focussed)
6843 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6844 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6845 ffd1d5e5 2018-06-23 stsp }
6846 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6847 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6848 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6849 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6850 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6851 ffd1d5e5 2018-06-23 stsp return NULL;
6852 ffd1d5e5 2018-06-23 stsp n = 1;
6853 ffd1d5e5 2018-06-23 stsp } else {
6854 ffd1d5e5 2018-06-23 stsp n = 0;
6855 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6856 ffd1d5e5 2018-06-23 stsp }
6857 ffd1d5e5 2018-06-23 stsp
6858 c72de8ab 2023-02-03 thomas view->maxx = 0;
6859 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6860 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6861 848d6979 2019-08-12 stsp const char *modestr = "";
6862 56e0773d 2019-11-28 stsp mode_t mode;
6863 1d13200f 2018-07-12 stsp
6864 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6865 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6866 56e0773d 2019-11-28 stsp
6867 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6868 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6869 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6870 1d13200f 2018-07-12 stsp if (err)
6871 638f9024 2019-05-13 stsp return got_error_from_errno(
6872 230a42bd 2019-05-11 jcs "got_object_id_str");
6873 1d13200f 2018-07-12 stsp }
6874 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6875 63c5ca5d 2019-08-24 stsp modestr = "$";
6876 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6877 0d6c6ee3 2020-05-20 stsp int i;
6878 0d6c6ee3 2020-05-20 stsp
6879 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6880 d86d3b18 2020-12-01 naddy te, s->repo);
6881 0d6c6ee3 2020-05-20 stsp if (err) {
6882 0d6c6ee3 2020-05-20 stsp free(id_str);
6883 0d6c6ee3 2020-05-20 stsp return err;
6884 0d6c6ee3 2020-05-20 stsp }
6885 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6886 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6887 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6888 0d6c6ee3 2020-05-20 stsp }
6889 848d6979 2019-08-12 stsp modestr = "@";
6890 0d6c6ee3 2020-05-20 stsp }
6891 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6892 848d6979 2019-08-12 stsp modestr = "/";
6893 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6894 848d6979 2019-08-12 stsp modestr = "*";
6895 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6896 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6897 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6898 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6899 1d13200f 2018-07-12 stsp free(id_str);
6900 0d6c6ee3 2020-05-20 stsp free(link_target);
6901 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6902 1d13200f 2018-07-12 stsp }
6903 1d13200f 2018-07-12 stsp free(id_str);
6904 0d6c6ee3 2020-05-20 stsp free(link_target);
6905 c72de8ab 2023-02-03 thomas
6906 c72de8ab 2023-02-03 thomas /* use full line width to determine view->maxx */
6907 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
6908 ffd1d5e5 2018-06-23 stsp if (err) {
6909 ffd1d5e5 2018-06-23 stsp free(line);
6910 ffd1d5e5 2018-06-23 stsp break;
6911 ffd1d5e5 2018-06-23 stsp }
6912 c72de8ab 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
6913 c72de8ab 2023-02-03 thomas free(wline);
6914 c72de8ab 2023-02-03 thomas wline = NULL;
6915 c72de8ab 2023-02-03 thomas
6916 c72de8ab 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
6917 c72de8ab 2023-02-03 thomas view->ncols, 0, 0);
6918 c72de8ab 2023-02-03 thomas if (err) {
6919 c72de8ab 2023-02-03 thomas free(line);
6920 c72de8ab 2023-02-03 thomas break;
6921 c72de8ab 2023-02-03 thomas }
6922 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6923 0cf4efb1 2018-09-29 stsp if (view->focussed)
6924 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6925 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6926 ffd1d5e5 2018-06-23 stsp }
6927 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6928 f26dddb7 2019-11-08 stsp if (tc)
6929 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6930 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6931 c72de8ab 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
6932 f26dddb7 2019-11-08 stsp if (tc)
6933 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6934 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6935 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
6936 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6937 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6938 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6939 ffd1d5e5 2018-06-23 stsp free(line);
6940 2550e4c3 2018-07-13 stsp free(wline);
6941 2550e4c3 2018-07-13 stsp wline = NULL;
6942 ffd1d5e5 2018-06-23 stsp n++;
6943 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6944 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6945 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6946 ffd1d5e5 2018-06-23 stsp break;
6947 ffd1d5e5 2018-06-23 stsp }
6948 ffd1d5e5 2018-06-23 stsp
6949 ffd1d5e5 2018-06-23 stsp return err;
6950 ffd1d5e5 2018-06-23 stsp }
6951 ffd1d5e5 2018-06-23 stsp
6952 ffd1d5e5 2018-06-23 stsp static void
6953 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6954 ffd1d5e5 2018-06-23 stsp {
6955 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6956 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6957 fa86c4bf 2020-11-29 stsp int i = 0;
6958 ffd1d5e5 2018-06-23 stsp
6959 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6960 ffd1d5e5 2018-06-23 stsp return;
6961 ffd1d5e5 2018-06-23 stsp
6962 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6963 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6964 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6965 fa86c4bf 2020-11-29 stsp if (!isroot)
6966 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6967 fa86c4bf 2020-11-29 stsp break;
6968 fa86c4bf 2020-11-29 stsp }
6969 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6970 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6971 ffd1d5e5 2018-06-23 stsp }
6972 ffd1d5e5 2018-06-23 stsp }
6973 ffd1d5e5 2018-06-23 stsp
6974 a5d43cac 2022-07-01 thomas static const struct got_error *
6975 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6976 ffd1d5e5 2018-06-23 stsp {
6977 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6978 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6979 ffd1d5e5 2018-06-23 stsp int n = 0;
6980 ffd1d5e5 2018-06-23 stsp
6981 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6982 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6983 694d3271 2020-12-01 naddy s->first_displayed_entry);
6984 694d3271 2020-12-01 naddy else
6985 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6986 56e0773d 2019-11-28 stsp
6987 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6988 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6989 07dd3ed3 2022-08-06 thomas if (last) {
6990 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6991 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6992 07dd3ed3 2022-08-06 thomas }
6993 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6994 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6995 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6996 768394f3 2019-01-24 stsp }
6997 ffd1d5e5 2018-06-23 stsp }
6998 a5d43cac 2022-07-01 thomas
6999 a5d43cac 2022-07-01 thomas return NULL;
7000 ffd1d5e5 2018-06-23 stsp }
7001 ffd1d5e5 2018-06-23 stsp
7002 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7003 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
7004 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
7005 ffd1d5e5 2018-06-23 stsp {
7006 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
7007 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
7008 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
7009 ffd1d5e5 2018-06-23 stsp
7010 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
7011 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
7012 56e0773d 2019-11-28 stsp + 1 /* slash */;
7013 ce52c690 2018-06-23 stsp if (te)
7014 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
7015 ce52c690 2018-06-23 stsp
7016 ce52c690 2018-06-23 stsp *path = calloc(1, len);
7017 ffd1d5e5 2018-06-23 stsp if (path == NULL)
7018 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
7019 ffd1d5e5 2018-06-23 stsp
7020 ce52c690 2018-06-23 stsp (*path)[0] = '/';
7021 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
7022 d9765a41 2018-06-23 stsp while (pt) {
7023 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
7024 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
7025 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7026 cb2ebc8a 2018-06-23 stsp goto done;
7027 cb2ebc8a 2018-06-23 stsp }
7028 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
7029 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7030 cb2ebc8a 2018-06-23 stsp goto done;
7031 cb2ebc8a 2018-06-23 stsp }
7032 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
7033 ffd1d5e5 2018-06-23 stsp }
7034 ce52c690 2018-06-23 stsp if (te) {
7035 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
7036 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7037 ce52c690 2018-06-23 stsp goto done;
7038 ce52c690 2018-06-23 stsp }
7039 cb2ebc8a 2018-06-23 stsp }
7040 ce52c690 2018-06-23 stsp done:
7041 ce52c690 2018-06-23 stsp if (err) {
7042 ce52c690 2018-06-23 stsp free(*path);
7043 ce52c690 2018-06-23 stsp *path = NULL;
7044 ce52c690 2018-06-23 stsp }
7045 ce52c690 2018-06-23 stsp return err;
7046 ce52c690 2018-06-23 stsp }
7047 ce52c690 2018-06-23 stsp
7048 ce52c690 2018-06-23 stsp static const struct got_error *
7049 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7050 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
7051 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7052 ce52c690 2018-06-23 stsp {
7053 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
7054 ce52c690 2018-06-23 stsp char *path;
7055 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
7056 a0de39f3 2019-08-09 stsp
7057 a0de39f3 2019-08-09 stsp *new_view = NULL;
7058 69efd4c4 2018-07-18 stsp
7059 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
7060 ce52c690 2018-06-23 stsp if (err)
7061 ce52c690 2018-06-23 stsp return err;
7062 ffd1d5e5 2018-06-23 stsp
7063 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
7064 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
7065 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
7066 83ce39e3 2019-08-12 stsp goto done;
7067 83ce39e3 2019-08-12 stsp }
7068 cdf1ee82 2018-08-01 stsp
7069 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
7070 e5a0f69f 2018-08-18 stsp if (err) {
7071 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
7072 fc06ba56 2019-08-22 stsp err = NULL;
7073 e5a0f69f 2018-08-18 stsp view_close(blame_view);
7074 e5a0f69f 2018-08-18 stsp } else
7075 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
7076 83ce39e3 2019-08-12 stsp done:
7077 83ce39e3 2019-08-12 stsp free(path);
7078 69efd4c4 2018-07-18 stsp return err;
7079 69efd4c4 2018-07-18 stsp }
7080 69efd4c4 2018-07-18 stsp
7081 69efd4c4 2018-07-18 stsp static const struct got_error *
7082 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
7083 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
7084 69efd4c4 2018-07-18 stsp {
7085 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
7086 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
7087 69efd4c4 2018-07-18 stsp char *path;
7088 a0de39f3 2019-08-09 stsp
7089 a0de39f3 2019-08-09 stsp *new_view = NULL;
7090 69efd4c4 2018-07-18 stsp
7091 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7092 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
7093 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
7094 e5a0f69f 2018-08-18 stsp
7095 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
7096 69efd4c4 2018-07-18 stsp if (err)
7097 69efd4c4 2018-07-18 stsp return err;
7098 69efd4c4 2018-07-18 stsp
7099 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
7100 4e97c21c 2020-12-06 stsp path, 0);
7101 ba4f502b 2018-08-04 stsp if (err)
7102 e5a0f69f 2018-08-18 stsp view_close(log_view);
7103 e5a0f69f 2018-08-18 stsp else
7104 e5a0f69f 2018-08-18 stsp *new_view = log_view;
7105 cb2ebc8a 2018-06-23 stsp free(path);
7106 cb2ebc8a 2018-06-23 stsp return err;
7107 ffd1d5e5 2018-06-23 stsp }
7108 ffd1d5e5 2018-06-23 stsp
7109 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7110 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
7111 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
7112 ffd1d5e5 2018-06-23 stsp {
7113 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
7114 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
7115 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7116 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
7117 ffd1d5e5 2018-06-23 stsp
7118 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
7119 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
7120 bc573f3b 2021-07-10 stsp
7121 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
7122 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
7123 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
7124 ffd1d5e5 2018-06-23 stsp
7125 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7126 bc573f3b 2021-07-10 stsp if (err)
7127 bc573f3b 2021-07-10 stsp goto done;
7128 bc573f3b 2021-07-10 stsp
7129 bc573f3b 2021-07-10 stsp /*
7130 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
7131 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
7132 bc573f3b 2021-07-10 stsp * closed on demand.
7133 bc573f3b 2021-07-10 stsp */
7134 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
7135 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
7136 bc573f3b 2021-07-10 stsp if (err)
7137 bc573f3b 2021-07-10 stsp goto done;
7138 bc573f3b 2021-07-10 stsp s->tree = s->root;
7139 bc573f3b 2021-07-10 stsp
7140 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
7141 ffd1d5e5 2018-06-23 stsp if (err != NULL)
7142 ffd1d5e5 2018-06-23 stsp goto done;
7143 ffd1d5e5 2018-06-23 stsp
7144 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7145 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
7146 ffd1d5e5 2018-06-23 stsp goto done;
7147 ffd1d5e5 2018-06-23 stsp }
7148 ffd1d5e5 2018-06-23 stsp
7149 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7150 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7151 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
7152 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
7153 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
7154 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
7155 9cd7cbd1 2020-12-07 stsp goto done;
7156 9cd7cbd1 2020-12-07 stsp }
7157 9cd7cbd1 2020-12-07 stsp }
7158 fb2756b9 2018-08-04 stsp s->repo = repo;
7159 c0b01bdb 2019-11-08 stsp
7160 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7161 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
7162 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
7163 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7164 c0b01bdb 2019-11-08 stsp if (err)
7165 c0b01bdb 2019-11-08 stsp goto done;
7166 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7167 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
7168 bc573f3b 2021-07-10 stsp if (err)
7169 c0b01bdb 2019-11-08 stsp goto done;
7170 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
7171 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
7172 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7173 bc573f3b 2021-07-10 stsp if (err)
7174 c0b01bdb 2019-11-08 stsp goto done;
7175 e5a0f69f 2018-08-18 stsp
7176 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
7177 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
7178 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7179 bc573f3b 2021-07-10 stsp if (err)
7180 11b20872 2019-11-08 stsp goto done;
7181 11b20872 2019-11-08 stsp
7182 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7183 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
7184 bc573f3b 2021-07-10 stsp if (err)
7185 c0b01bdb 2019-11-08 stsp goto done;
7186 c0b01bdb 2019-11-08 stsp }
7187 c0b01bdb 2019-11-08 stsp
7188 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
7189 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
7190 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
7191 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
7192 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
7193 ad80ab7b 2018-08-04 stsp done:
7194 ad80ab7b 2018-08-04 stsp free(commit_id_str);
7195 bc573f3b 2021-07-10 stsp if (commit)
7196 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
7197 bc573f3b 2021-07-10 stsp if (err)
7198 bc573f3b 2021-07-10 stsp close_tree_view(view);
7199 ad80ab7b 2018-08-04 stsp return err;
7200 ad80ab7b 2018-08-04 stsp }
7201 ad80ab7b 2018-08-04 stsp
7202 e5a0f69f 2018-08-18 stsp static const struct got_error *
7203 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
7204 ad80ab7b 2018-08-04 stsp {
7205 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7206 ad80ab7b 2018-08-04 stsp
7207 bddb1296 2019-11-08 stsp free_colors(&s->colors);
7208 fb2756b9 2018-08-04 stsp free(s->tree_label);
7209 6484ec90 2018-09-29 stsp s->tree_label = NULL;
7210 6484ec90 2018-09-29 stsp free(s->commit_id);
7211 6484ec90 2018-09-29 stsp s->commit_id = NULL;
7212 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
7213 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
7214 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
7215 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
7216 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
7217 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
7218 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
7219 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
7220 ad80ab7b 2018-08-04 stsp free(parent);
7221 ad80ab7b 2018-08-04 stsp
7222 ad80ab7b 2018-08-04 stsp }
7223 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
7224 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
7225 bc573f3b 2021-07-10 stsp if (s->root)
7226 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
7227 7c32bd05 2019-06-22 stsp return NULL;
7228 7c32bd05 2019-06-22 stsp }
7229 7c32bd05 2019-06-22 stsp
7230 7c32bd05 2019-06-22 stsp static const struct got_error *
7231 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7232 7c32bd05 2019-06-22 stsp {
7233 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7234 7c32bd05 2019-06-22 stsp
7235 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7236 7c32bd05 2019-06-22 stsp return NULL;
7237 7c32bd05 2019-06-22 stsp }
7238 7c32bd05 2019-06-22 stsp
7239 7c32bd05 2019-06-22 stsp static int
7240 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7241 7c32bd05 2019-06-22 stsp {
7242 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7243 7c32bd05 2019-06-22 stsp
7244 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7245 56e0773d 2019-11-28 stsp 0) == 0;
7246 7c32bd05 2019-06-22 stsp }
7247 7c32bd05 2019-06-22 stsp
7248 7c32bd05 2019-06-22 stsp static const struct got_error *
7249 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7250 7c32bd05 2019-06-22 stsp {
7251 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7252 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7253 7c32bd05 2019-06-22 stsp
7254 7c32bd05 2019-06-22 stsp if (!view->searching) {
7255 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7256 7c32bd05 2019-06-22 stsp return NULL;
7257 7c32bd05 2019-06-22 stsp }
7258 7c32bd05 2019-06-22 stsp
7259 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7260 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7261 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7262 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7263 56e0773d 2019-11-28 stsp s->selected_entry);
7264 7c32bd05 2019-06-22 stsp else
7265 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7266 56e0773d 2019-11-28 stsp } else {
7267 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7268 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7269 56e0773d 2019-11-28 stsp else
7270 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7271 56e0773d 2019-11-28 stsp s->selected_entry);
7272 7c32bd05 2019-06-22 stsp }
7273 7c32bd05 2019-06-22 stsp } else {
7274 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7275 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7276 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7277 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7278 56e0773d 2019-11-28 stsp else
7279 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7280 7c32bd05 2019-06-22 stsp }
7281 7c32bd05 2019-06-22 stsp
7282 7c32bd05 2019-06-22 stsp while (1) {
7283 56e0773d 2019-11-28 stsp if (te == NULL) {
7284 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7285 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7286 ac66afb8 2019-06-24 stsp return NULL;
7287 ac66afb8 2019-06-24 stsp }
7288 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7289 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7290 56e0773d 2019-11-28 stsp else
7291 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7292 7c32bd05 2019-06-22 stsp }
7293 7c32bd05 2019-06-22 stsp
7294 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7295 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7296 56e0773d 2019-11-28 stsp s->matched_entry = te;
7297 7c32bd05 2019-06-22 stsp break;
7298 7c32bd05 2019-06-22 stsp }
7299 7c32bd05 2019-06-22 stsp
7300 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7301 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7302 56e0773d 2019-11-28 stsp else
7303 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7304 7c32bd05 2019-06-22 stsp }
7305 e5a0f69f 2018-08-18 stsp
7306 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7307 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7308 7c32bd05 2019-06-22 stsp s->selected = 0;
7309 7c32bd05 2019-06-22 stsp }
7310 7c32bd05 2019-06-22 stsp
7311 e5a0f69f 2018-08-18 stsp return NULL;
7312 ad80ab7b 2018-08-04 stsp }
7313 ad80ab7b 2018-08-04 stsp
7314 ad80ab7b 2018-08-04 stsp static const struct got_error *
7315 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7316 ad80ab7b 2018-08-04 stsp {
7317 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7318 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7319 e5a0f69f 2018-08-18 stsp char *parent_path;
7320 ad80ab7b 2018-08-04 stsp
7321 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7322 e5a0f69f 2018-08-18 stsp if (err)
7323 e5a0f69f 2018-08-18 stsp return err;
7324 ffd1d5e5 2018-06-23 stsp
7325 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7326 e5a0f69f 2018-08-18 stsp free(parent_path);
7327 669b5ffa 2018-10-07 stsp
7328 a5d43cac 2022-07-01 thomas view_border(view);
7329 e5a0f69f 2018-08-18 stsp return err;
7330 07dd3ed3 2022-08-06 thomas }
7331 07dd3ed3 2022-08-06 thomas
7332 07dd3ed3 2022-08-06 thomas static const struct got_error *
7333 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7334 07dd3ed3 2022-08-06 thomas {
7335 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7336 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7337 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7338 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7339 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7340 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7341 07dd3ed3 2022-08-06 thomas
7342 07dd3ed3 2022-08-06 thomas g = view->gline;
7343 07dd3ed3 2022-08-06 thomas view->gline = 0;
7344 07dd3ed3 2022-08-06 thomas
7345 07dd3ed3 2022-08-06 thomas if (g == 0)
7346 07dd3ed3 2022-08-06 thomas g = 1;
7347 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7348 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7349 07dd3ed3 2022-08-06 thomas
7350 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7351 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7352 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7353 07dd3ed3 2022-08-06 thomas
7354 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7355 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7356 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7357 07dd3ed3 2022-08-06 thomas }
7358 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7359 07dd3ed3 2022-08-06 thomas last += off;
7360 07dd3ed3 2022-08-06 thomas
7361 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7362 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7363 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7364 07dd3ed3 2022-08-06 thomas }
7365 07dd3ed3 2022-08-06 thomas
7366 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7367 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7368 07dd3ed3 2022-08-06 thomas i += off;
7369 07dd3ed3 2022-08-06 thomas }
7370 07dd3ed3 2022-08-06 thomas
7371 07dd3ed3 2022-08-06 thomas if (i < g) {
7372 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7373 07dd3ed3 2022-08-06 thomas if (err)
7374 07dd3ed3 2022-08-06 thomas return err;
7375 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7376 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7377 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7378 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7379 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7380 07dd3ed3 2022-08-06 thomas first += off;
7381 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7382 07dd3ed3 2022-08-06 thomas }
7383 07dd3ed3 2022-08-06 thomas } else if (i > g)
7384 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7385 07dd3ed3 2022-08-06 thomas
7386 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7387 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7388 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7389 07dd3ed3 2022-08-06 thomas
7390 07dd3ed3 2022-08-06 thomas return NULL;
7391 e5a0f69f 2018-08-18 stsp }
7392 ce52c690 2018-06-23 stsp
7393 e5a0f69f 2018-08-18 stsp static const struct got_error *
7394 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7395 e5a0f69f 2018-08-18 stsp {
7396 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7397 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7398 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7399 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7400 ffd1d5e5 2018-06-23 stsp
7401 07dd3ed3 2022-08-06 thomas if (view->gline)
7402 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7403 07dd3ed3 2022-08-06 thomas
7404 e5a0f69f 2018-08-18 stsp switch (ch) {
7405 c72de8ab 2023-02-03 thomas case '0':
7406 c72de8ab 2023-02-03 thomas case '$':
7407 c72de8ab 2023-02-03 thomas case KEY_RIGHT:
7408 c72de8ab 2023-02-03 thomas case 'l':
7409 c72de8ab 2023-02-03 thomas case KEY_LEFT:
7410 c72de8ab 2023-02-03 thomas case 'h':
7411 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
7412 c72de8ab 2023-02-03 thomas break;
7413 1e37a5c2 2019-05-12 jcs case 'i':
7414 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7415 07b0611c 2022-06-23 thomas view->count = 0;
7416 1e37a5c2 2019-05-12 jcs break;
7417 1be4947a 2022-07-22 thomas case 'L':
7418 07b0611c 2022-06-23 thomas view->count = 0;
7419 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7420 ffd1d5e5 2018-06-23 stsp break;
7421 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7422 152c1c93 2020-11-29 stsp break;
7423 1be4947a 2022-07-22 thomas case 'R':
7424 07b0611c 2022-06-23 thomas view->count = 0;
7425 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7426 e4526bf5 2021-09-03 naddy break;
7427 e4526bf5 2021-09-03 naddy case 'g':
7428 aa7a1117 2023-01-09 thomas case '=':
7429 e4526bf5 2021-09-03 naddy case KEY_HOME:
7430 e4526bf5 2021-09-03 naddy s->selected = 0;
7431 07b0611c 2022-06-23 thomas view->count = 0;
7432 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7433 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7434 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7435 e4526bf5 2021-09-03 naddy else
7436 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7437 1e37a5c2 2019-05-12 jcs break;
7438 e4526bf5 2021-09-03 naddy case 'G':
7439 aa7a1117 2023-01-09 thomas case '*':
7440 a5d43cac 2022-07-01 thomas case KEY_END: {
7441 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7442 a5d43cac 2022-07-01 thomas
7443 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7444 a5d43cac 2022-07-01 thomas --eos; /* border */
7445 e4526bf5 2021-09-03 naddy s->selected = 0;
7446 07b0611c 2022-06-23 thomas view->count = 0;
7447 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7448 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7449 e4526bf5 2021-09-03 naddy if (te == NULL) {
7450 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7451 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7452 e4526bf5 2021-09-03 naddy n++;
7453 e4526bf5 2021-09-03 naddy }
7454 e4526bf5 2021-09-03 naddy break;
7455 e4526bf5 2021-09-03 naddy }
7456 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7457 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7458 e4526bf5 2021-09-03 naddy }
7459 e4526bf5 2021-09-03 naddy if (n > 0)
7460 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7461 e4526bf5 2021-09-03 naddy break;
7462 a5d43cac 2022-07-01 thomas }
7463 1e37a5c2 2019-05-12 jcs case 'k':
7464 1e37a5c2 2019-05-12 jcs case KEY_UP:
7465 f7140bf5 2021-10-17 thomas case CTRL('p'):
7466 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7467 1e37a5c2 2019-05-12 jcs s->selected--;
7468 fa86c4bf 2020-11-29 stsp break;
7469 1e37a5c2 2019-05-12 jcs }
7470 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7471 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7472 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7473 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7474 07b0611c 2022-06-23 thomas view->count = 0;
7475 1e37a5c2 2019-05-12 jcs break;
7476 70f17a53 2022-06-13 thomas case CTRL('u'):
7477 23427b14 2022-06-23 thomas case 'u':
7478 70f17a53 2022-06-13 thomas nscroll /= 2;
7479 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7480 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7481 ea025d1d 2020-02-22 naddy case CTRL('b'):
7482 1c5e5faa 2022-06-23 thomas case 'b':
7483 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7484 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7485 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7486 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7487 fa86c4bf 2020-11-29 stsp } else {
7488 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7489 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7490 fa86c4bf 2020-11-29 stsp }
7491 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7492 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7493 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7494 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7495 07b0611c 2022-06-23 thomas view->count = 0;
7496 1e37a5c2 2019-05-12 jcs break;
7497 1e37a5c2 2019-05-12 jcs case 'j':
7498 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7499 f7140bf5 2021-10-17 thomas case CTRL('n'):
7500 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7501 1e37a5c2 2019-05-12 jcs s->selected++;
7502 1e37a5c2 2019-05-12 jcs break;
7503 1e37a5c2 2019-05-12 jcs }
7504 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7505 07b0611c 2022-06-23 thomas == NULL) {
7506 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7507 07b0611c 2022-06-23 thomas view->count = 0;
7508 1e37a5c2 2019-05-12 jcs break;
7509 07b0611c 2022-06-23 thomas }
7510 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7511 1e37a5c2 2019-05-12 jcs break;
7512 70f17a53 2022-06-13 thomas case CTRL('d'):
7513 23427b14 2022-06-23 thomas case 'd':
7514 70f17a53 2022-06-13 thomas nscroll /= 2;
7515 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7516 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7517 ea025d1d 2020-02-22 naddy case CTRL('f'):
7518 1c5e5faa 2022-06-23 thomas case 'f':
7519 4c2d69cb 2022-06-23 thomas case ' ':
7520 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7521 1e37a5c2 2019-05-12 jcs == NULL) {
7522 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7523 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7524 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7525 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7526 07b0611c 2022-06-23 thomas else
7527 07b0611c 2022-06-23 thomas view->count = 0;
7528 1e37a5c2 2019-05-12 jcs break;
7529 1e37a5c2 2019-05-12 jcs }
7530 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7531 1e37a5c2 2019-05-12 jcs break;
7532 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7533 1e37a5c2 2019-05-12 jcs case '\r':
7534 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7535 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7536 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7537 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7538 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7539 07b0611c 2022-06-23 thomas view->count = 0;
7540 1e37a5c2 2019-05-12 jcs break;
7541 07b0611c 2022-06-23 thomas }
7542 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7543 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7544 1e37a5c2 2019-05-12 jcs entry);
7545 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7546 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7547 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7548 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7549 1e37a5c2 2019-05-12 jcs s->selected_entry =
7550 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7551 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7552 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7553 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7554 a5d43cac 2022-07-01 thomas if (err)
7555 a5d43cac 2022-07-01 thomas break;
7556 a5d43cac 2022-07-01 thomas }
7557 1e37a5c2 2019-05-12 jcs free(parent);
7558 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7559 56e0773d 2019-11-28 stsp s->selected_entry))) {
7560 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7561 07b0611c 2022-06-23 thomas view->count = 0;
7562 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7563 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7564 1e37a5c2 2019-05-12 jcs if (err)
7565 1e37a5c2 2019-05-12 jcs break;
7566 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7567 941e9f74 2019-05-21 stsp if (err) {
7568 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7569 1e37a5c2 2019-05-12 jcs break;
7570 1e37a5c2 2019-05-12 jcs }
7571 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7572 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7573 1e37a5c2 2019-05-12 jcs break;
7574 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7575 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7576 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7577 07b0611c 2022-06-23 thomas view->count = 0;
7578 1e37a5c2 2019-05-12 jcs break;
7579 1e37a5c2 2019-05-12 jcs default:
7580 07b0611c 2022-06-23 thomas view->count = 0;
7581 1e37a5c2 2019-05-12 jcs break;
7582 ffd1d5e5 2018-06-23 stsp }
7583 e5a0f69f 2018-08-18 stsp
7584 ffd1d5e5 2018-06-23 stsp return err;
7585 9f7d7167 2018-04-29 stsp }
7586 9f7d7167 2018-04-29 stsp
7587 ffd1d5e5 2018-06-23 stsp __dead static void
7588 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7589 ffd1d5e5 2018-06-23 stsp {
7590 ffd1d5e5 2018-06-23 stsp endwin();
7591 91198554 2022-06-23 thomas fprintf(stderr,
7592 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7593 ffd1d5e5 2018-06-23 stsp getprogname());
7594 ffd1d5e5 2018-06-23 stsp exit(1);
7595 ffd1d5e5 2018-06-23 stsp }
7596 ffd1d5e5 2018-06-23 stsp
7597 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7598 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7599 ffd1d5e5 2018-06-23 stsp {
7600 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7601 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7602 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7603 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7604 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7605 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7606 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7607 4e97c21c 2020-12-06 stsp char *label = NULL;
7608 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7609 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7610 ffd1d5e5 2018-06-23 stsp int ch;
7611 5221c383 2018-08-01 stsp struct tog_view *view;
7612 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7613 70ac5f84 2019-03-28 stsp
7614 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7615 ffd1d5e5 2018-06-23 stsp switch (ch) {
7616 ffd1d5e5 2018-06-23 stsp case 'c':
7617 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7618 74283ab8 2020-02-07 stsp break;
7619 74283ab8 2020-02-07 stsp case 'r':
7620 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7621 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7622 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7623 74283ab8 2020-02-07 stsp optarg);
7624 ffd1d5e5 2018-06-23 stsp break;
7625 ffd1d5e5 2018-06-23 stsp default:
7626 e99e2d15 2020-11-24 naddy usage_tree();
7627 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7628 ffd1d5e5 2018-06-23 stsp }
7629 ffd1d5e5 2018-06-23 stsp }
7630 ffd1d5e5 2018-06-23 stsp
7631 ffd1d5e5 2018-06-23 stsp argc -= optind;
7632 ffd1d5e5 2018-06-23 stsp argv += optind;
7633 ffd1d5e5 2018-06-23 stsp
7634 55cccc34 2020-02-20 stsp if (argc > 1)
7635 e99e2d15 2020-11-24 naddy usage_tree();
7636 7cd52833 2022-06-23 thomas
7637 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7638 7cd52833 2022-06-23 thomas if (error != NULL)
7639 7cd52833 2022-06-23 thomas goto done;
7640 74283ab8 2020-02-07 stsp
7641 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7642 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7643 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7644 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7645 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7646 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7647 c156c7a4 2020-12-18 stsp goto done;
7648 55cccc34 2020-02-20 stsp if (worktree)
7649 52185f70 2019-02-05 stsp repo_path =
7650 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7651 55cccc34 2020-02-20 stsp else
7652 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7653 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7654 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7655 c156c7a4 2020-12-18 stsp goto done;
7656 c156c7a4 2020-12-18 stsp }
7657 55cccc34 2020-02-20 stsp }
7658 a915003a 2019-02-05 stsp
7659 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7660 c02c541e 2019-03-29 stsp if (error != NULL)
7661 52185f70 2019-02-05 stsp goto done;
7662 d188b9a6 2019-01-04 stsp
7663 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7664 55cccc34 2020-02-20 stsp repo, worktree);
7665 55cccc34 2020-02-20 stsp if (error)
7666 55cccc34 2020-02-20 stsp goto done;
7667 55cccc34 2020-02-20 stsp
7668 55cccc34 2020-02-20 stsp init_curses();
7669 55cccc34 2020-02-20 stsp
7670 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7671 c02c541e 2019-03-29 stsp if (error)
7672 52185f70 2019-02-05 stsp goto done;
7673 ffd1d5e5 2018-06-23 stsp
7674 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7675 51a10b52 2020-12-26 stsp if (error)
7676 51a10b52 2020-12-26 stsp goto done;
7677 51a10b52 2020-12-26 stsp
7678 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7679 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7680 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7681 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7682 4e97c21c 2020-12-06 stsp if (error)
7683 4e97c21c 2020-12-06 stsp goto done;
7684 4e97c21c 2020-12-06 stsp head_ref_name = label;
7685 4e97c21c 2020-12-06 stsp } else {
7686 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7687 4e97c21c 2020-12-06 stsp if (error == NULL)
7688 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7689 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7690 4e97c21c 2020-12-06 stsp goto done;
7691 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7692 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7693 4e97c21c 2020-12-06 stsp if (error)
7694 4e97c21c 2020-12-06 stsp goto done;
7695 4e97c21c 2020-12-06 stsp }
7696 ffd1d5e5 2018-06-23 stsp
7697 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7698 945f9229 2022-04-16 thomas if (error)
7699 945f9229 2022-04-16 thomas goto done;
7700 945f9229 2022-04-16 thomas
7701 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7702 5221c383 2018-08-01 stsp if (view == NULL) {
7703 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7704 5221c383 2018-08-01 stsp goto done;
7705 5221c383 2018-08-01 stsp }
7706 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7707 ad80ab7b 2018-08-04 stsp if (error)
7708 ad80ab7b 2018-08-04 stsp goto done;
7709 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7710 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7711 d91faf3b 2020-12-01 naddy in_repo_path);
7712 55cccc34 2020-02-20 stsp if (error)
7713 55cccc34 2020-02-20 stsp goto done;
7714 55cccc34 2020-02-20 stsp }
7715 55cccc34 2020-02-20 stsp
7716 55cccc34 2020-02-20 stsp if (worktree) {
7717 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7718 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7719 55cccc34 2020-02-20 stsp worktree = NULL;
7720 55cccc34 2020-02-20 stsp }
7721 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7722 ffd1d5e5 2018-06-23 stsp done:
7723 52185f70 2019-02-05 stsp free(repo_path);
7724 e4a0e26d 2020-02-20 stsp free(cwd);
7725 ffd1d5e5 2018-06-23 stsp free(commit_id);
7726 4e97c21c 2020-12-06 stsp free(label);
7727 486cd271 2020-12-06 stsp if (ref)
7728 486cd271 2020-12-06 stsp got_ref_close(ref);
7729 1d0f4054 2021-06-17 stsp if (repo) {
7730 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7731 1d0f4054 2021-06-17 stsp if (error == NULL)
7732 1d0f4054 2021-06-17 stsp error = close_err;
7733 1d0f4054 2021-06-17 stsp }
7734 7cd52833 2022-06-23 thomas if (pack_fds) {
7735 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7736 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7737 7cd52833 2022-06-23 thomas if (error == NULL)
7738 7cd52833 2022-06-23 thomas error = pack_err;
7739 7cd52833 2022-06-23 thomas }
7740 51a10b52 2020-12-26 stsp tog_free_refs();
7741 ffd1d5e5 2018-06-23 stsp return error;
7742 6458efa5 2020-11-24 stsp }
7743 6458efa5 2020-11-24 stsp
7744 6458efa5 2020-11-24 stsp static const struct got_error *
7745 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7746 6458efa5 2020-11-24 stsp {
7747 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7748 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7749 6458efa5 2020-11-24 stsp
7750 6458efa5 2020-11-24 stsp s->nrefs = 0;
7751 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7752 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7753 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7754 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7755 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7756 6458efa5 2020-11-24 stsp continue;
7757 6458efa5 2020-11-24 stsp
7758 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7759 6458efa5 2020-11-24 stsp if (re == NULL)
7760 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7761 6458efa5 2020-11-24 stsp
7762 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7763 8924d611 2020-12-26 stsp if (re->ref == NULL)
7764 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7765 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7766 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7767 6458efa5 2020-11-24 stsp }
7768 6458efa5 2020-11-24 stsp
7769 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7770 6458efa5 2020-11-24 stsp return NULL;
7771 6458efa5 2020-11-24 stsp }
7772 6458efa5 2020-11-24 stsp
7773 ef20f542 2022-06-26 thomas static void
7774 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7775 6458efa5 2020-11-24 stsp {
7776 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7777 6458efa5 2020-11-24 stsp
7778 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7779 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7780 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7781 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7782 6458efa5 2020-11-24 stsp free(re);
7783 6458efa5 2020-11-24 stsp }
7784 6458efa5 2020-11-24 stsp }
7785 6458efa5 2020-11-24 stsp
7786 6458efa5 2020-11-24 stsp static const struct got_error *
7787 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7788 6458efa5 2020-11-24 stsp {
7789 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7790 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7791 6458efa5 2020-11-24 stsp
7792 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7793 6458efa5 2020-11-24 stsp s->repo = repo;
7794 6458efa5 2020-11-24 stsp
7795 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7796 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7797 6458efa5 2020-11-24 stsp
7798 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7799 6458efa5 2020-11-24 stsp if (err)
7800 6458efa5 2020-11-24 stsp return err;
7801 34ba6917 2020-11-30 stsp
7802 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7803 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7804 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7805 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7806 6458efa5 2020-11-24 stsp if (err)
7807 6458efa5 2020-11-24 stsp goto done;
7808 6458efa5 2020-11-24 stsp
7809 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7810 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7811 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7812 6458efa5 2020-11-24 stsp if (err)
7813 6458efa5 2020-11-24 stsp goto done;
7814 6458efa5 2020-11-24 stsp
7815 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7816 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7817 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7818 2183bbf6 2022-01-23 thomas if (err)
7819 2183bbf6 2022-01-23 thomas goto done;
7820 2183bbf6 2022-01-23 thomas
7821 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7822 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7823 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7824 6458efa5 2020-11-24 stsp if (err)
7825 6458efa5 2020-11-24 stsp goto done;
7826 6458efa5 2020-11-24 stsp }
7827 6458efa5 2020-11-24 stsp
7828 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7829 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7830 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7831 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7832 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7833 6458efa5 2020-11-24 stsp done:
7834 6458efa5 2020-11-24 stsp if (err)
7835 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7836 6458efa5 2020-11-24 stsp return err;
7837 6458efa5 2020-11-24 stsp }
7838 6458efa5 2020-11-24 stsp
7839 6458efa5 2020-11-24 stsp static const struct got_error *
7840 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7841 6458efa5 2020-11-24 stsp {
7842 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7843 6458efa5 2020-11-24 stsp
7844 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7845 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7846 6458efa5 2020-11-24 stsp
7847 6458efa5 2020-11-24 stsp return NULL;
7848 9f7d7167 2018-04-29 stsp }
7849 ce5b7c56 2019-07-09 stsp
7850 6458efa5 2020-11-24 stsp static const struct got_error *
7851 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7852 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7853 6458efa5 2020-11-24 stsp {
7854 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7855 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7856 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7857 6458efa5 2020-11-24 stsp int obj_type;
7858 6458efa5 2020-11-24 stsp
7859 c42c9805 2020-11-24 stsp *commit_id = NULL;
7860 6458efa5 2020-11-24 stsp
7861 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7862 6458efa5 2020-11-24 stsp if (err)
7863 6458efa5 2020-11-24 stsp return err;
7864 6458efa5 2020-11-24 stsp
7865 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7866 6458efa5 2020-11-24 stsp if (err)
7867 6458efa5 2020-11-24 stsp goto done;
7868 6458efa5 2020-11-24 stsp
7869 6458efa5 2020-11-24 stsp switch (obj_type) {
7870 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7871 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7872 6458efa5 2020-11-24 stsp break;
7873 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7874 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7875 6458efa5 2020-11-24 stsp if (err)
7876 6458efa5 2020-11-24 stsp goto done;
7877 c42c9805 2020-11-24 stsp free(obj_id);
7878 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7879 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7880 c42c9805 2020-11-24 stsp if (err)
7881 6458efa5 2020-11-24 stsp goto done;
7882 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7883 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7884 c42c9805 2020-11-24 stsp goto done;
7885 c42c9805 2020-11-24 stsp }
7886 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7887 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7888 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7889 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7890 c42c9805 2020-11-24 stsp goto done;
7891 c42c9805 2020-11-24 stsp }
7892 6458efa5 2020-11-24 stsp break;
7893 6458efa5 2020-11-24 stsp default:
7894 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7895 c42c9805 2020-11-24 stsp break;
7896 6458efa5 2020-11-24 stsp }
7897 6458efa5 2020-11-24 stsp
7898 c42c9805 2020-11-24 stsp done:
7899 c42c9805 2020-11-24 stsp if (tag)
7900 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7901 c42c9805 2020-11-24 stsp if (err) {
7902 c42c9805 2020-11-24 stsp free(*commit_id);
7903 c42c9805 2020-11-24 stsp *commit_id = NULL;
7904 c42c9805 2020-11-24 stsp }
7905 c42c9805 2020-11-24 stsp return err;
7906 c42c9805 2020-11-24 stsp }
7907 c42c9805 2020-11-24 stsp
7908 c42c9805 2020-11-24 stsp static const struct got_error *
7909 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7910 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7911 c42c9805 2020-11-24 stsp {
7912 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7913 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7914 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7915 c42c9805 2020-11-24 stsp
7916 c42c9805 2020-11-24 stsp *new_view = NULL;
7917 c42c9805 2020-11-24 stsp
7918 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7919 c42c9805 2020-11-24 stsp if (err) {
7920 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7921 c42c9805 2020-11-24 stsp return err;
7922 c42c9805 2020-11-24 stsp else
7923 c42c9805 2020-11-24 stsp return NULL;
7924 c42c9805 2020-11-24 stsp }
7925 c42c9805 2020-11-24 stsp
7926 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7927 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7928 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7929 6458efa5 2020-11-24 stsp goto done;
7930 6458efa5 2020-11-24 stsp }
7931 6458efa5 2020-11-24 stsp
7932 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7933 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7934 6458efa5 2020-11-24 stsp done:
7935 6458efa5 2020-11-24 stsp if (err)
7936 6458efa5 2020-11-24 stsp view_close(log_view);
7937 6458efa5 2020-11-24 stsp else
7938 6458efa5 2020-11-24 stsp *new_view = log_view;
7939 c42c9805 2020-11-24 stsp free(commit_id);
7940 6458efa5 2020-11-24 stsp return err;
7941 6458efa5 2020-11-24 stsp }
7942 6458efa5 2020-11-24 stsp
7943 ce5b7c56 2019-07-09 stsp static void
7944 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7945 6458efa5 2020-11-24 stsp {
7946 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7947 34ba6917 2020-11-30 stsp int i = 0;
7948 6458efa5 2020-11-24 stsp
7949 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7950 6458efa5 2020-11-24 stsp return;
7951 6458efa5 2020-11-24 stsp
7952 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7953 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7954 34ba6917 2020-11-30 stsp if (re == NULL)
7955 34ba6917 2020-11-30 stsp break;
7956 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7957 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7958 6458efa5 2020-11-24 stsp }
7959 6458efa5 2020-11-24 stsp }
7960 6458efa5 2020-11-24 stsp
7961 a5d43cac 2022-07-01 thomas static const struct got_error *
7962 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7963 6458efa5 2020-11-24 stsp {
7964 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7965 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7966 6458efa5 2020-11-24 stsp int n = 0;
7967 6458efa5 2020-11-24 stsp
7968 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7969 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7970 6458efa5 2020-11-24 stsp else
7971 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7972 6458efa5 2020-11-24 stsp
7973 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7974 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7975 07dd3ed3 2022-08-06 thomas if (last) {
7976 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7977 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7978 07dd3ed3 2022-08-06 thomas }
7979 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7980 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7981 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7982 6458efa5 2020-11-24 stsp }
7983 6458efa5 2020-11-24 stsp }
7984 a5d43cac 2022-07-01 thomas
7985 a5d43cac 2022-07-01 thomas return NULL;
7986 6458efa5 2020-11-24 stsp }
7987 6458efa5 2020-11-24 stsp
7988 6458efa5 2020-11-24 stsp static const struct got_error *
7989 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7990 6458efa5 2020-11-24 stsp {
7991 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7992 6458efa5 2020-11-24 stsp
7993 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7994 6458efa5 2020-11-24 stsp return NULL;
7995 6458efa5 2020-11-24 stsp }
7996 6458efa5 2020-11-24 stsp
7997 6458efa5 2020-11-24 stsp static int
7998 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7999 6458efa5 2020-11-24 stsp {
8000 6458efa5 2020-11-24 stsp regmatch_t regmatch;
8001 6458efa5 2020-11-24 stsp
8002 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
8003 6458efa5 2020-11-24 stsp 0) == 0;
8004 6458efa5 2020-11-24 stsp }
8005 6458efa5 2020-11-24 stsp
8006 6458efa5 2020-11-24 stsp static const struct got_error *
8007 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
8008 6458efa5 2020-11-24 stsp {
8009 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8010 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
8011 6458efa5 2020-11-24 stsp
8012 6458efa5 2020-11-24 stsp if (!view->searching) {
8013 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8014 6458efa5 2020-11-24 stsp return NULL;
8015 6458efa5 2020-11-24 stsp }
8016 6458efa5 2020-11-24 stsp
8017 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8018 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
8019 6458efa5 2020-11-24 stsp if (s->selected_entry)
8020 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
8021 6458efa5 2020-11-24 stsp else
8022 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8023 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8024 6458efa5 2020-11-24 stsp } else {
8025 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
8026 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8027 6458efa5 2020-11-24 stsp else
8028 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
8029 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
8030 6458efa5 2020-11-24 stsp }
8031 6458efa5 2020-11-24 stsp } else {
8032 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
8033 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
8034 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
8035 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8036 6458efa5 2020-11-24 stsp else
8037 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8038 6458efa5 2020-11-24 stsp }
8039 6458efa5 2020-11-24 stsp
8040 6458efa5 2020-11-24 stsp while (1) {
8041 6458efa5 2020-11-24 stsp if (re == NULL) {
8042 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
8043 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8044 6458efa5 2020-11-24 stsp return NULL;
8045 6458efa5 2020-11-24 stsp }
8046 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8047 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
8048 6458efa5 2020-11-24 stsp else
8049 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
8050 6458efa5 2020-11-24 stsp }
8051 6458efa5 2020-11-24 stsp
8052 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
8053 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
8054 6458efa5 2020-11-24 stsp s->matched_entry = re;
8055 6458efa5 2020-11-24 stsp break;
8056 6458efa5 2020-11-24 stsp }
8057 6458efa5 2020-11-24 stsp
8058 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
8059 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8060 6458efa5 2020-11-24 stsp else
8061 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
8062 6458efa5 2020-11-24 stsp }
8063 6458efa5 2020-11-24 stsp
8064 6458efa5 2020-11-24 stsp if (s->matched_entry) {
8065 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
8066 6458efa5 2020-11-24 stsp s->selected = 0;
8067 6458efa5 2020-11-24 stsp }
8068 6458efa5 2020-11-24 stsp
8069 6458efa5 2020-11-24 stsp return NULL;
8070 6458efa5 2020-11-24 stsp }
8071 6458efa5 2020-11-24 stsp
8072 6458efa5 2020-11-24 stsp static const struct got_error *
8073 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
8074 6458efa5 2020-11-24 stsp {
8075 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8076 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8077 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
8078 6458efa5 2020-11-24 stsp char *line = NULL;
8079 6458efa5 2020-11-24 stsp wchar_t *wline;
8080 6458efa5 2020-11-24 stsp struct tog_color *tc;
8081 66a70b97 2023-02-03 thomas int width, n, scrollx;
8082 6458efa5 2020-11-24 stsp int limit = view->nlines;
8083 6458efa5 2020-11-24 stsp
8084 6458efa5 2020-11-24 stsp werase(view->window);
8085 6458efa5 2020-11-24 stsp
8086 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
8087 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
8088 a5d43cac 2022-07-01 thomas --limit; /* border */
8089 6458efa5 2020-11-24 stsp
8090 6458efa5 2020-11-24 stsp if (limit == 0)
8091 6458efa5 2020-11-24 stsp return NULL;
8092 6458efa5 2020-11-24 stsp
8093 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
8094 6458efa5 2020-11-24 stsp
8095 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
8096 6458efa5 2020-11-24 stsp s->nrefs) == -1)
8097 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8098 6458efa5 2020-11-24 stsp
8099 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
8100 6458efa5 2020-11-24 stsp if (err) {
8101 6458efa5 2020-11-24 stsp free(line);
8102 6458efa5 2020-11-24 stsp return err;
8103 6458efa5 2020-11-24 stsp }
8104 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8105 6458efa5 2020-11-24 stsp wstandout(view->window);
8106 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
8107 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
8108 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
8109 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
8110 6458efa5 2020-11-24 stsp wstandend(view->window);
8111 6458efa5 2020-11-24 stsp free(wline);
8112 6458efa5 2020-11-24 stsp wline = NULL;
8113 6458efa5 2020-11-24 stsp free(line);
8114 6458efa5 2020-11-24 stsp line = NULL;
8115 6458efa5 2020-11-24 stsp if (--limit <= 0)
8116 6458efa5 2020-11-24 stsp return NULL;
8117 6458efa5 2020-11-24 stsp
8118 6458efa5 2020-11-24 stsp n = 0;
8119 66a70b97 2023-02-03 thomas view->maxx = 0;
8120 6458efa5 2020-11-24 stsp while (re && limit > 0) {
8121 6458efa5 2020-11-24 stsp char *line = NULL;
8122 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
8123 6458efa5 2020-11-24 stsp
8124 84227eb1 2022-06-23 thomas if (s->show_date) {
8125 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
8126 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
8127 84227eb1 2022-06-23 thomas struct got_object_id *id;
8128 84227eb1 2022-06-23 thomas struct tm tm;
8129 84227eb1 2022-06-23 thomas time_t t;
8130 84227eb1 2022-06-23 thomas
8131 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
8132 84227eb1 2022-06-23 thomas if (err)
8133 84227eb1 2022-06-23 thomas return err;
8134 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
8135 84227eb1 2022-06-23 thomas if (err) {
8136 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
8137 84227eb1 2022-06-23 thomas free(id);
8138 84227eb1 2022-06-23 thomas return err;
8139 84227eb1 2022-06-23 thomas }
8140 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
8141 84227eb1 2022-06-23 thomas id);
8142 84227eb1 2022-06-23 thomas if (err) {
8143 84227eb1 2022-06-23 thomas free(id);
8144 84227eb1 2022-06-23 thomas return err;
8145 84227eb1 2022-06-23 thomas }
8146 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
8147 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
8148 84227eb1 2022-06-23 thomas } else {
8149 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
8150 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
8151 84227eb1 2022-06-23 thomas }
8152 84227eb1 2022-06-23 thomas free(id);
8153 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
8154 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
8155 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8156 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
8157 84227eb1 2022-06-23 thomas }
8158 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
8159 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
8160 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
8161 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
8162 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
8163 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
8164 6458efa5 2020-11-24 stsp struct got_object_id *id;
8165 6458efa5 2020-11-24 stsp char *id_str;
8166 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
8167 6458efa5 2020-11-24 stsp if (err)
8168 6458efa5 2020-11-24 stsp return err;
8169 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
8170 6458efa5 2020-11-24 stsp if (err) {
8171 6458efa5 2020-11-24 stsp free(id);
8172 6458efa5 2020-11-24 stsp return err;
8173 6458efa5 2020-11-24 stsp }
8174 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8175 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
8176 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
8177 6458efa5 2020-11-24 stsp free(id);
8178 6458efa5 2020-11-24 stsp free(id_str);
8179 6458efa5 2020-11-24 stsp return err;
8180 6458efa5 2020-11-24 stsp }
8181 6458efa5 2020-11-24 stsp free(id);
8182 6458efa5 2020-11-24 stsp free(id_str);
8183 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8184 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
8185 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
8186 6458efa5 2020-11-24 stsp
8187 66a70b97 2023-02-03 thomas /* use full line width to determine view->maxx */
8188 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
8189 6458efa5 2020-11-24 stsp if (err) {
8190 6458efa5 2020-11-24 stsp free(line);
8191 6458efa5 2020-11-24 stsp return err;
8192 6458efa5 2020-11-24 stsp }
8193 66a70b97 2023-02-03 thomas view->maxx = MAX(view->maxx, width);
8194 66a70b97 2023-02-03 thomas free(wline);
8195 66a70b97 2023-02-03 thomas wline = NULL;
8196 66a70b97 2023-02-03 thomas
8197 66a70b97 2023-02-03 thomas err = format_line(&wline, &width, &scrollx, line, view->x,
8198 66a70b97 2023-02-03 thomas view->ncols, 0, 0);
8199 66a70b97 2023-02-03 thomas if (err) {
8200 66a70b97 2023-02-03 thomas free(line);
8201 66a70b97 2023-02-03 thomas return err;
8202 66a70b97 2023-02-03 thomas }
8203 6458efa5 2020-11-24 stsp if (n == s->selected) {
8204 6458efa5 2020-11-24 stsp if (view->focussed)
8205 6458efa5 2020-11-24 stsp wstandout(view->window);
8206 6458efa5 2020-11-24 stsp s->selected_entry = re;
8207 6458efa5 2020-11-24 stsp }
8208 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
8209 6458efa5 2020-11-24 stsp if (tc)
8210 6458efa5 2020-11-24 stsp wattr_on(view->window,
8211 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8212 66a70b97 2023-02-03 thomas waddwstr(view->window, &wline[scrollx]);
8213 6458efa5 2020-11-24 stsp if (tc)
8214 6458efa5 2020-11-24 stsp wattr_off(view->window,
8215 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
8216 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8217 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
8218 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
8219 6458efa5 2020-11-24 stsp wstandend(view->window);
8220 6458efa5 2020-11-24 stsp free(line);
8221 6458efa5 2020-11-24 stsp free(wline);
8222 6458efa5 2020-11-24 stsp wline = NULL;
8223 6458efa5 2020-11-24 stsp n++;
8224 6458efa5 2020-11-24 stsp s->ndisplayed++;
8225 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
8226 6458efa5 2020-11-24 stsp
8227 6458efa5 2020-11-24 stsp limit--;
8228 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
8229 6458efa5 2020-11-24 stsp }
8230 6458efa5 2020-11-24 stsp
8231 a5d43cac 2022-07-01 thomas view_border(view);
8232 6458efa5 2020-11-24 stsp return err;
8233 6458efa5 2020-11-24 stsp }
8234 6458efa5 2020-11-24 stsp
8235 6458efa5 2020-11-24 stsp static const struct got_error *
8236 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8237 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
8238 c42c9805 2020-11-24 stsp {
8239 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
8240 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
8241 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
8242 c42c9805 2020-11-24 stsp
8243 c42c9805 2020-11-24 stsp *new_view = NULL;
8244 c42c9805 2020-11-24 stsp
8245 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
8246 c42c9805 2020-11-24 stsp if (err) {
8247 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
8248 c42c9805 2020-11-24 stsp return err;
8249 c42c9805 2020-11-24 stsp else
8250 c42c9805 2020-11-24 stsp return NULL;
8251 c42c9805 2020-11-24 stsp }
8252 c42c9805 2020-11-24 stsp
8253 c42c9805 2020-11-24 stsp
8254 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8255 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8256 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8257 c42c9805 2020-11-24 stsp goto done;
8258 c42c9805 2020-11-24 stsp }
8259 c42c9805 2020-11-24 stsp
8260 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8261 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8262 c42c9805 2020-11-24 stsp if (err)
8263 c42c9805 2020-11-24 stsp goto done;
8264 c42c9805 2020-11-24 stsp
8265 c42c9805 2020-11-24 stsp *new_view = tree_view;
8266 c42c9805 2020-11-24 stsp done:
8267 c42c9805 2020-11-24 stsp free(commit_id);
8268 c42c9805 2020-11-24 stsp return err;
8269 07dd3ed3 2022-08-06 thomas }
8270 07dd3ed3 2022-08-06 thomas
8271 07dd3ed3 2022-08-06 thomas static const struct got_error *
8272 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8273 07dd3ed3 2022-08-06 thomas {
8274 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8275 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8276 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8277 07dd3ed3 2022-08-06 thomas
8278 07dd3ed3 2022-08-06 thomas g = view->gline;
8279 07dd3ed3 2022-08-06 thomas view->gline = 0;
8280 07dd3ed3 2022-08-06 thomas
8281 07dd3ed3 2022-08-06 thomas if (g == 0)
8282 07dd3ed3 2022-08-06 thomas g = 1;
8283 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8284 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8285 07dd3ed3 2022-08-06 thomas
8286 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8287 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8288 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8289 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8290 07dd3ed3 2022-08-06 thomas return NULL;
8291 07dd3ed3 2022-08-06 thomas }
8292 07dd3ed3 2022-08-06 thomas
8293 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8294 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8295 07dd3ed3 2022-08-06 thomas if (err)
8296 07dd3ed3 2022-08-06 thomas return err;
8297 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8298 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8299 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8300 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8301 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8302 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8303 07dd3ed3 2022-08-06 thomas
8304 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8305 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8306 07dd3ed3 2022-08-06 thomas
8307 07dd3ed3 2022-08-06 thomas return NULL;
8308 07dd3ed3 2022-08-06 thomas
8309 c42c9805 2020-11-24 stsp }
8310 07dd3ed3 2022-08-06 thomas
8311 c42c9805 2020-11-24 stsp static const struct got_error *
8312 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8313 6458efa5 2020-11-24 stsp {
8314 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8315 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8316 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8317 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8318 6458efa5 2020-11-24 stsp
8319 07dd3ed3 2022-08-06 thomas if (view->gline)
8320 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8321 07dd3ed3 2022-08-06 thomas
8322 6458efa5 2020-11-24 stsp switch (ch) {
8323 66a70b97 2023-02-03 thomas case '0':
8324 66a70b97 2023-02-03 thomas case '$':
8325 66a70b97 2023-02-03 thomas case KEY_RIGHT:
8326 66a70b97 2023-02-03 thomas case 'l':
8327 66a70b97 2023-02-03 thomas case KEY_LEFT:
8328 66a70b97 2023-02-03 thomas case 'h':
8329 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8330 66a70b97 2023-02-03 thomas break;
8331 6458efa5 2020-11-24 stsp case 'i':
8332 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8333 07b0611c 2022-06-23 thomas view->count = 0;
8334 3bfadbd4 2021-11-20 thomas break;
8335 84227eb1 2022-06-23 thomas case 'm':
8336 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8337 07b0611c 2022-06-23 thomas view->count = 0;
8338 84227eb1 2022-06-23 thomas break;
8339 98182bd0 2021-11-20 thomas case 'o':
8340 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8341 f2d06bef 2023-01-23 thomas view->action = s->sort_by_date ? "sort by date" : "sort by name";
8342 07b0611c 2022-06-23 thomas view->count = 0;
8343 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8344 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8345 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8346 c591440f 2021-11-20 thomas if (err)
8347 c591440f 2021-11-20 thomas break;
8348 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8349 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8350 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8351 3bfadbd4 2021-11-20 thomas if (err)
8352 3bfadbd4 2021-11-20 thomas break;
8353 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8354 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8355 6458efa5 2020-11-24 stsp break;
8356 6458efa5 2020-11-24 stsp case KEY_ENTER:
8357 6458efa5 2020-11-24 stsp case '\r':
8358 07b0611c 2022-06-23 thomas view->count = 0;
8359 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8360 a5d43cac 2022-07-01 thomas break;
8361 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8362 6458efa5 2020-11-24 stsp break;
8363 1be4947a 2022-07-22 thomas case 'T':
8364 07b0611c 2022-06-23 thomas view->count = 0;
8365 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8366 c42c9805 2020-11-24 stsp break;
8367 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8368 c42c9805 2020-11-24 stsp break;
8369 e4526bf5 2021-09-03 naddy case 'g':
8370 aa7a1117 2023-01-09 thomas case '=':
8371 e4526bf5 2021-09-03 naddy case KEY_HOME:
8372 e4526bf5 2021-09-03 naddy s->selected = 0;
8373 07b0611c 2022-06-23 thomas view->count = 0;
8374 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8375 e4526bf5 2021-09-03 naddy break;
8376 e4526bf5 2021-09-03 naddy case 'G':
8377 aa7a1117 2023-01-09 thomas case '*':
8378 a5d43cac 2022-07-01 thomas case KEY_END: {
8379 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8380 a5d43cac 2022-07-01 thomas
8381 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8382 a5d43cac 2022-07-01 thomas --eos; /* border */
8383 e4526bf5 2021-09-03 naddy s->selected = 0;
8384 07b0611c 2022-06-23 thomas view->count = 0;
8385 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8386 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8387 e4526bf5 2021-09-03 naddy if (re == NULL)
8388 e4526bf5 2021-09-03 naddy break;
8389 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8390 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8391 e4526bf5 2021-09-03 naddy }
8392 e4526bf5 2021-09-03 naddy if (n > 0)
8393 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8394 e4526bf5 2021-09-03 naddy break;
8395 a5d43cac 2022-07-01 thomas }
8396 6458efa5 2020-11-24 stsp case 'k':
8397 6458efa5 2020-11-24 stsp case KEY_UP:
8398 f7140bf5 2021-10-17 thomas case CTRL('p'):
8399 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8400 6458efa5 2020-11-24 stsp s->selected--;
8401 6458efa5 2020-11-24 stsp break;
8402 34ba6917 2020-11-30 stsp }
8403 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8404 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8405 07b0611c 2022-06-23 thomas view->count = 0;
8406 6458efa5 2020-11-24 stsp break;
8407 70f17a53 2022-06-13 thomas case CTRL('u'):
8408 23427b14 2022-06-23 thomas case 'u':
8409 70f17a53 2022-06-13 thomas nscroll /= 2;
8410 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8411 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8412 6458efa5 2020-11-24 stsp case CTRL('b'):
8413 1c5e5faa 2022-06-23 thomas case 'b':
8414 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8415 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8416 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8417 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8418 07b0611c 2022-06-23 thomas view->count = 0;
8419 6458efa5 2020-11-24 stsp break;
8420 6458efa5 2020-11-24 stsp case 'j':
8421 6458efa5 2020-11-24 stsp case KEY_DOWN:
8422 f7140bf5 2021-10-17 thomas case CTRL('n'):
8423 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8424 6458efa5 2020-11-24 stsp s->selected++;
8425 6458efa5 2020-11-24 stsp break;
8426 6458efa5 2020-11-24 stsp }
8427 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8428 6458efa5 2020-11-24 stsp /* can't scroll any further */
8429 07b0611c 2022-06-23 thomas view->count = 0;
8430 6458efa5 2020-11-24 stsp break;
8431 07b0611c 2022-06-23 thomas }
8432 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8433 6458efa5 2020-11-24 stsp break;
8434 70f17a53 2022-06-13 thomas case CTRL('d'):
8435 23427b14 2022-06-23 thomas case 'd':
8436 70f17a53 2022-06-13 thomas nscroll /= 2;
8437 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8438 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8439 6458efa5 2020-11-24 stsp case CTRL('f'):
8440 1c5e5faa 2022-06-23 thomas case 'f':
8441 4c2d69cb 2022-06-23 thomas case ' ':
8442 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8443 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8444 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8445 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8446 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8447 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8448 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8449 07b0611c 2022-06-23 thomas view->count = 0;
8450 6458efa5 2020-11-24 stsp break;
8451 6458efa5 2020-11-24 stsp }
8452 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8453 6458efa5 2020-11-24 stsp break;
8454 6458efa5 2020-11-24 stsp case CTRL('l'):
8455 07b0611c 2022-06-23 thomas view->count = 0;
8456 8924d611 2020-12-26 stsp tog_free_refs();
8457 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8458 8924d611 2020-12-26 stsp if (err)
8459 8924d611 2020-12-26 stsp break;
8460 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8461 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8462 6458efa5 2020-11-24 stsp break;
8463 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8464 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8465 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8466 6458efa5 2020-11-24 stsp break;
8467 6458efa5 2020-11-24 stsp default:
8468 07b0611c 2022-06-23 thomas view->count = 0;
8469 6458efa5 2020-11-24 stsp break;
8470 6458efa5 2020-11-24 stsp }
8471 6458efa5 2020-11-24 stsp
8472 6458efa5 2020-11-24 stsp return err;
8473 6458efa5 2020-11-24 stsp }
8474 6458efa5 2020-11-24 stsp
8475 6458efa5 2020-11-24 stsp __dead static void
8476 6458efa5 2020-11-24 stsp usage_ref(void)
8477 6458efa5 2020-11-24 stsp {
8478 6458efa5 2020-11-24 stsp endwin();
8479 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8480 6458efa5 2020-11-24 stsp getprogname());
8481 6458efa5 2020-11-24 stsp exit(1);
8482 6458efa5 2020-11-24 stsp }
8483 6458efa5 2020-11-24 stsp
8484 6458efa5 2020-11-24 stsp static const struct got_error *
8485 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8486 6458efa5 2020-11-24 stsp {
8487 6458efa5 2020-11-24 stsp const struct got_error *error;
8488 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8489 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8490 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8491 6458efa5 2020-11-24 stsp int ch;
8492 6458efa5 2020-11-24 stsp struct tog_view *view;
8493 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8494 6458efa5 2020-11-24 stsp
8495 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8496 6458efa5 2020-11-24 stsp switch (ch) {
8497 6458efa5 2020-11-24 stsp case 'r':
8498 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8499 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8500 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8501 6458efa5 2020-11-24 stsp optarg);
8502 6458efa5 2020-11-24 stsp break;
8503 6458efa5 2020-11-24 stsp default:
8504 e99e2d15 2020-11-24 naddy usage_ref();
8505 6458efa5 2020-11-24 stsp /* NOTREACHED */
8506 6458efa5 2020-11-24 stsp }
8507 6458efa5 2020-11-24 stsp }
8508 6458efa5 2020-11-24 stsp
8509 6458efa5 2020-11-24 stsp argc -= optind;
8510 6458efa5 2020-11-24 stsp argv += optind;
8511 6458efa5 2020-11-24 stsp
8512 6458efa5 2020-11-24 stsp if (argc > 1)
8513 e99e2d15 2020-11-24 naddy usage_ref();
8514 7cd52833 2022-06-23 thomas
8515 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8516 7cd52833 2022-06-23 thomas if (error != NULL)
8517 7cd52833 2022-06-23 thomas goto done;
8518 6458efa5 2020-11-24 stsp
8519 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8520 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8521 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8522 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8523 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8524 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8525 c156c7a4 2020-12-18 stsp goto done;
8526 6458efa5 2020-11-24 stsp if (worktree)
8527 6458efa5 2020-11-24 stsp repo_path =
8528 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8529 6458efa5 2020-11-24 stsp else
8530 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8531 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8532 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8533 c156c7a4 2020-12-18 stsp goto done;
8534 c156c7a4 2020-12-18 stsp }
8535 6458efa5 2020-11-24 stsp }
8536 6458efa5 2020-11-24 stsp
8537 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8538 6458efa5 2020-11-24 stsp if (error != NULL)
8539 6458efa5 2020-11-24 stsp goto done;
8540 6458efa5 2020-11-24 stsp
8541 6458efa5 2020-11-24 stsp init_curses();
8542 6458efa5 2020-11-24 stsp
8543 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8544 51a10b52 2020-12-26 stsp if (error)
8545 51a10b52 2020-12-26 stsp goto done;
8546 51a10b52 2020-12-26 stsp
8547 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8548 6458efa5 2020-11-24 stsp if (error)
8549 6458efa5 2020-11-24 stsp goto done;
8550 6458efa5 2020-11-24 stsp
8551 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8552 6458efa5 2020-11-24 stsp if (view == NULL) {
8553 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8554 6458efa5 2020-11-24 stsp goto done;
8555 6458efa5 2020-11-24 stsp }
8556 6458efa5 2020-11-24 stsp
8557 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8558 6458efa5 2020-11-24 stsp if (error)
8559 6458efa5 2020-11-24 stsp goto done;
8560 6458efa5 2020-11-24 stsp
8561 6458efa5 2020-11-24 stsp if (worktree) {
8562 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8563 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8564 6458efa5 2020-11-24 stsp worktree = NULL;
8565 6458efa5 2020-11-24 stsp }
8566 6458efa5 2020-11-24 stsp error = view_loop(view);
8567 6458efa5 2020-11-24 stsp done:
8568 6458efa5 2020-11-24 stsp free(repo_path);
8569 6458efa5 2020-11-24 stsp free(cwd);
8570 1d0f4054 2021-06-17 stsp if (repo) {
8571 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8572 1d0f4054 2021-06-17 stsp if (close_err)
8573 1d0f4054 2021-06-17 stsp error = close_err;
8574 1d0f4054 2021-06-17 stsp }
8575 7cd52833 2022-06-23 thomas if (pack_fds) {
8576 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8577 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8578 7cd52833 2022-06-23 thomas if (error == NULL)
8579 7cd52833 2022-06-23 thomas error = pack_err;
8580 7cd52833 2022-06-23 thomas }
8581 51a10b52 2020-12-26 stsp tog_free_refs();
8582 6458efa5 2020-11-24 stsp return error;
8583 6458efa5 2020-11-24 stsp }
8584 6458efa5 2020-11-24 stsp
8585 fc2737d5 2022-09-15 thomas static const struct got_error*
8586 fc2737d5 2022-09-15 thomas win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8587 fc2737d5 2022-09-15 thomas const char *str)
8588 fc2737d5 2022-09-15 thomas {
8589 fc2737d5 2022-09-15 thomas size_t len;
8590 fc2737d5 2022-09-15 thomas
8591 fc2737d5 2022-09-15 thomas if (win == NULL)
8592 fc2737d5 2022-09-15 thomas win = stdscr;
8593 fc2737d5 2022-09-15 thomas
8594 fc2737d5 2022-09-15 thomas len = strlen(str);
8595 fc2737d5 2022-09-15 thomas x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8596 fc2737d5 2022-09-15 thomas
8597 fc2737d5 2022-09-15 thomas if (focus)
8598 fc2737d5 2022-09-15 thomas wstandout(win);
8599 fc2737d5 2022-09-15 thomas if (mvwprintw(win, y, x, "%s", str) == ERR)
8600 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8601 fc2737d5 2022-09-15 thomas if (focus)
8602 fc2737d5 2022-09-15 thomas wstandend(win);
8603 fc2737d5 2022-09-15 thomas
8604 fc2737d5 2022-09-15 thomas return NULL;
8605 fc2737d5 2022-09-15 thomas }
8606 fc2737d5 2022-09-15 thomas
8607 2a31b33b 2022-07-23 thomas static const struct got_error *
8608 fc2737d5 2022-09-15 thomas add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8609 fc2737d5 2022-09-15 thomas {
8610 fc2737d5 2022-09-15 thomas off_t *p;
8611 fc2737d5 2022-09-15 thomas
8612 fc2737d5 2022-09-15 thomas p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8613 fc2737d5 2022-09-15 thomas if (p == NULL) {
8614 fc2737d5 2022-09-15 thomas free(*line_offsets);
8615 fc2737d5 2022-09-15 thomas *line_offsets = NULL;
8616 fc2737d5 2022-09-15 thomas return got_error_from_errno("reallocarray");
8617 fc2737d5 2022-09-15 thomas }
8618 fc2737d5 2022-09-15 thomas
8619 fc2737d5 2022-09-15 thomas *line_offsets = p;
8620 fc2737d5 2022-09-15 thomas (*line_offsets)[*nlines] = off;
8621 fc2737d5 2022-09-15 thomas ++(*nlines);
8622 fc2737d5 2022-09-15 thomas return NULL;
8623 fc2737d5 2022-09-15 thomas }
8624 fc2737d5 2022-09-15 thomas
8625 fc2737d5 2022-09-15 thomas static const struct got_error *
8626 fc2737d5 2022-09-15 thomas max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8627 fc2737d5 2022-09-15 thomas {
8628 fc2737d5 2022-09-15 thomas *ret = 0;
8629 fc2737d5 2022-09-15 thomas
8630 fc2737d5 2022-09-15 thomas for (;n > 0; --n, ++km) {
8631 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8632 fc2737d5 2022-09-15 thomas size_t len = 1;
8633 fc2737d5 2022-09-15 thomas
8634 fc2737d5 2022-09-15 thomas if (km->keys == NULL)
8635 fc2737d5 2022-09-15 thomas continue;
8636 fc2737d5 2022-09-15 thomas
8637 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8638 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8639 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8640 fc2737d5 2022-09-15 thomas
8641 fc2737d5 2022-09-15 thomas len += strlen(t);
8642 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL)
8643 fc2737d5 2022-09-15 thomas len += strlen(k) > 1 ? 2 : 0;
8644 fc2737d5 2022-09-15 thomas free(t0);
8645 fc2737d5 2022-09-15 thomas *ret = MAX(*ret, len);
8646 fc2737d5 2022-09-15 thomas }
8647 fc2737d5 2022-09-15 thomas
8648 fc2737d5 2022-09-15 thomas return NULL;
8649 fc2737d5 2022-09-15 thomas }
8650 fc2737d5 2022-09-15 thomas
8651 fc2737d5 2022-09-15 thomas /*
8652 fc2737d5 2022-09-15 thomas * Write keymap section headers, keys, and key info in km to f.
8653 fc2737d5 2022-09-15 thomas * Save line offset to *off. If terminal has UTF8 encoding enabled,
8654 fc2737d5 2022-09-15 thomas * wrap control and symbolic keys in guillemets, else use <>.
8655 fc2737d5 2022-09-15 thomas */
8656 fc2737d5 2022-09-15 thomas static const struct got_error *
8657 fc2737d5 2022-09-15 thomas format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8658 fc2737d5 2022-09-15 thomas {
8659 fc2737d5 2022-09-15 thomas int n, len = width;
8660 fc2737d5 2022-09-15 thomas
8661 fc2737d5 2022-09-15 thomas if (km->keys) {
8662 30e77f6a 2022-09-18 thomas static const char *u8_glyph[] = {
8663 30e77f6a 2022-09-18 thomas "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8664 30e77f6a 2022-09-18 thomas "\xe2\x80\xba" /* U+203A (utf8 >) */
8665 30e77f6a 2022-09-18 thomas };
8666 fc2737d5 2022-09-15 thomas char *t0, *t, *k;
8667 fc2737d5 2022-09-15 thomas int cs, s, first = 1;
8668 fc2737d5 2022-09-15 thomas
8669 fc2737d5 2022-09-15 thomas cs = got_locale_is_utf8();
8670 fc2737d5 2022-09-15 thomas
8671 fc2737d5 2022-09-15 thomas t = t0 = strdup(km->keys);
8672 fc2737d5 2022-09-15 thomas if (t0 == NULL)
8673 fc2737d5 2022-09-15 thomas return got_error_from_errno("strdup");
8674 fc2737d5 2022-09-15 thomas
8675 fc2737d5 2022-09-15 thomas len = strlen(km->keys);
8676 fc2737d5 2022-09-15 thomas while ((k = strsep(&t, " ")) != NULL) {
8677 fc2737d5 2022-09-15 thomas s = strlen(k) > 1; /* control or symbolic key */
8678 fc2737d5 2022-09-15 thomas n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8679 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[0] : s ? "<" : "", k,
8680 30e77f6a 2022-09-18 thomas cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8681 fc2737d5 2022-09-15 thomas if (n < 0) {
8682 fc2737d5 2022-09-15 thomas free(t0);
8683 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8684 fc2737d5 2022-09-15 thomas }
8685 fc2737d5 2022-09-15 thomas first = 0;
8686 fc2737d5 2022-09-15 thomas len += s ? 2 : 0;
8687 fc2737d5 2022-09-15 thomas *off += n;
8688 fc2737d5 2022-09-15 thomas }
8689 fc2737d5 2022-09-15 thomas free(t0);
8690 fc2737d5 2022-09-15 thomas }
8691 fc2737d5 2022-09-15 thomas n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8692 fc2737d5 2022-09-15 thomas if (n < 0)
8693 fc2737d5 2022-09-15 thomas return got_error_from_errno("fprintf");
8694 fc2737d5 2022-09-15 thomas *off += n;
8695 fc2737d5 2022-09-15 thomas
8696 fc2737d5 2022-09-15 thomas return NULL;
8697 fc2737d5 2022-09-15 thomas }
8698 fc2737d5 2022-09-15 thomas
8699 fc2737d5 2022-09-15 thomas static const struct got_error *
8700 fc2737d5 2022-09-15 thomas format_help(struct tog_help_view_state *s)
8701 fc2737d5 2022-09-15 thomas {
8702 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8703 fc2737d5 2022-09-15 thomas off_t off = 0;
8704 fc2737d5 2022-09-15 thomas int i, max, n, show = s->all;
8705 fc2737d5 2022-09-15 thomas static const struct tog_key_map km[] = {
8706 fc2737d5 2022-09-15 thomas #define KEYMAP_(info, type) { NULL, (info), type }
8707 fc2737d5 2022-09-15 thomas #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8708 fc2737d5 2022-09-15 thomas GENERATE_HELP
8709 fc2737d5 2022-09-15 thomas #undef KEYMAP_
8710 fc2737d5 2022-09-15 thomas #undef KEY_
8711 fc2737d5 2022-09-15 thomas };
8712 fc2737d5 2022-09-15 thomas
8713 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8714 fc2737d5 2022-09-15 thomas if (err)
8715 fc2737d5 2022-09-15 thomas return err;
8716 fc2737d5 2022-09-15 thomas
8717 fc2737d5 2022-09-15 thomas n = nitems(km);
8718 fc2737d5 2022-09-15 thomas err = max_key_str(&max, km, n);
8719 fc2737d5 2022-09-15 thomas if (err)
8720 fc2737d5 2022-09-15 thomas return err;
8721 fc2737d5 2022-09-15 thomas
8722 fc2737d5 2022-09-15 thomas for (i = 0; i < n; ++i) {
8723 fc2737d5 2022-09-15 thomas if (km[i].keys == NULL) {
8724 fc2737d5 2022-09-15 thomas show = s->all;
8725 fc2737d5 2022-09-15 thomas if (km[i].type == TOG_KEYMAP_GLOBAL ||
8726 fc2737d5 2022-09-15 thomas km[i].type == s->type || s->all)
8727 fc2737d5 2022-09-15 thomas show = 1;
8728 fc2737d5 2022-09-15 thomas }
8729 fc2737d5 2022-09-15 thomas if (show) {
8730 fc2737d5 2022-09-15 thomas err = format_help_line(&off, s->f, &km[i], max);
8731 fc2737d5 2022-09-15 thomas if (err)
8732 fc2737d5 2022-09-15 thomas return err;
8733 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8734 fc2737d5 2022-09-15 thomas if (err)
8735 fc2737d5 2022-09-15 thomas return err;
8736 fc2737d5 2022-09-15 thomas }
8737 fc2737d5 2022-09-15 thomas }
8738 fc2737d5 2022-09-15 thomas fputc('\n', s->f);
8739 fc2737d5 2022-09-15 thomas ++off;
8740 fc2737d5 2022-09-15 thomas err = add_line_offset(&s->line_offsets, &s->nlines, off);
8741 fc2737d5 2022-09-15 thomas return err;
8742 fc2737d5 2022-09-15 thomas }
8743 fc2737d5 2022-09-15 thomas
8744 fc2737d5 2022-09-15 thomas static const struct got_error *
8745 fc2737d5 2022-09-15 thomas create_help(struct tog_help_view_state *s)
8746 fc2737d5 2022-09-15 thomas {
8747 fc2737d5 2022-09-15 thomas FILE *f;
8748 fc2737d5 2022-09-15 thomas const struct got_error *err;
8749 fc2737d5 2022-09-15 thomas
8750 fc2737d5 2022-09-15 thomas free(s->line_offsets);
8751 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
8752 fc2737d5 2022-09-15 thomas s->nlines = 0;
8753 fc2737d5 2022-09-15 thomas
8754 fc2737d5 2022-09-15 thomas f = got_opentemp();
8755 fc2737d5 2022-09-15 thomas if (f == NULL)
8756 fc2737d5 2022-09-15 thomas return got_error_from_errno("got_opentemp");
8757 fc2737d5 2022-09-15 thomas s->f = f;
8758 fc2737d5 2022-09-15 thomas
8759 fc2737d5 2022-09-15 thomas err = format_help(s);
8760 fc2737d5 2022-09-15 thomas if (err)
8761 fc2737d5 2022-09-15 thomas return err;
8762 fc2737d5 2022-09-15 thomas
8763 fc2737d5 2022-09-15 thomas if (s->f && fflush(s->f) != 0)
8764 fc2737d5 2022-09-15 thomas return got_error_from_errno("fflush");
8765 fc2737d5 2022-09-15 thomas
8766 fc2737d5 2022-09-15 thomas return NULL;
8767 fc2737d5 2022-09-15 thomas }
8768 fc2737d5 2022-09-15 thomas
8769 fc2737d5 2022-09-15 thomas static const struct got_error *
8770 fc2737d5 2022-09-15 thomas search_start_help_view(struct tog_view *view)
8771 fc2737d5 2022-09-15 thomas {
8772 fc2737d5 2022-09-15 thomas view->state.help.matched_line = 0;
8773 fc2737d5 2022-09-15 thomas return NULL;
8774 fc2737d5 2022-09-15 thomas }
8775 fc2737d5 2022-09-15 thomas
8776 2a7d3cd7 2022-09-17 thomas static void
8777 2a7d3cd7 2022-09-17 thomas search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8778 2a7d3cd7 2022-09-17 thomas size_t *nlines, int **first, int **last, int **match, int **selected)
8779 2a7d3cd7 2022-09-17 thomas {
8780 2a7d3cd7 2022-09-17 thomas struct tog_help_view_state *s = &view->state.help;
8781 2a7d3cd7 2022-09-17 thomas
8782 2a7d3cd7 2022-09-17 thomas *f = s->f;
8783 2a7d3cd7 2022-09-17 thomas *nlines = s->nlines;
8784 2a7d3cd7 2022-09-17 thomas *line_offsets = s->line_offsets;
8785 2a7d3cd7 2022-09-17 thomas *match = &s->matched_line;
8786 2a7d3cd7 2022-09-17 thomas *first = &s->first_displayed_line;
8787 2a7d3cd7 2022-09-17 thomas *last = &s->last_displayed_line;
8788 2a7d3cd7 2022-09-17 thomas *selected = &s->selected_line;
8789 2a7d3cd7 2022-09-17 thomas }
8790 2a7d3cd7 2022-09-17 thomas
8791 fc2737d5 2022-09-15 thomas static const struct got_error *
8792 fc2737d5 2022-09-15 thomas show_help_view(struct tog_view *view)
8793 fc2737d5 2022-09-15 thomas {
8794 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8795 fc2737d5 2022-09-15 thomas const struct got_error *err;
8796 fc2737d5 2022-09-15 thomas regmatch_t *regmatch = &view->regmatch;
8797 fc2737d5 2022-09-15 thomas wchar_t *wline;
8798 fc2737d5 2022-09-15 thomas char *line;
8799 fc2737d5 2022-09-15 thomas ssize_t linelen;
8800 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8801 fc2737d5 2022-09-15 thomas int width, nprinted = 0, rc = 0;
8802 fc2737d5 2022-09-15 thomas int eos = view->nlines;
8803 fc2737d5 2022-09-15 thomas
8804 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8805 fc2737d5 2022-09-15 thomas --eos; /* account for border */
8806 fc2737d5 2022-09-15 thomas
8807 fc2737d5 2022-09-15 thomas s->lineno = 0;
8808 fc2737d5 2022-09-15 thomas rewind(s->f);
8809 fc2737d5 2022-09-15 thomas werase(view->window);
8810 fc2737d5 2022-09-15 thomas
8811 fc2737d5 2022-09-15 thomas if (view->gline > s->nlines - 1)
8812 fc2737d5 2022-09-15 thomas view->gline = s->nlines - 1;
8813 fc2737d5 2022-09-15 thomas
8814 fc2737d5 2022-09-15 thomas err = win_draw_center(view->window, 0, 0, view->ncols,
8815 06ff88bb 2022-09-19 thomas view_needs_focus_indication(view),
8816 850265be 2022-09-19 thomas "tog help (press q to return to tog)");
8817 fc2737d5 2022-09-15 thomas if (err)
8818 fc2737d5 2022-09-15 thomas return err;
8819 fc2737d5 2022-09-15 thomas if (eos <= 1)
8820 fc2737d5 2022-09-15 thomas return NULL;
8821 fc2737d5 2022-09-15 thomas waddstr(view->window, "\n\n");
8822 fc2737d5 2022-09-15 thomas eos -= 2;
8823 fc2737d5 2022-09-15 thomas
8824 fc2737d5 2022-09-15 thomas s->eof = 0;
8825 fc2737d5 2022-09-15 thomas view->maxx = 0;
8826 fc2737d5 2022-09-15 thomas line = NULL;
8827 fc2737d5 2022-09-15 thomas while (eos > 0 && nprinted < eos) {
8828 fc2737d5 2022-09-15 thomas attr_t attr = 0;
8829 fc2737d5 2022-09-15 thomas
8830 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
8831 fc2737d5 2022-09-15 thomas if (linelen == -1) {
8832 fc2737d5 2022-09-15 thomas if (!feof(s->f)) {
8833 fc2737d5 2022-09-15 thomas free(line);
8834 fc2737d5 2022-09-15 thomas return got_ferror(s->f, GOT_ERR_IO);
8835 fc2737d5 2022-09-15 thomas }
8836 fc2737d5 2022-09-15 thomas s->eof = 1;
8837 fc2737d5 2022-09-15 thomas break;
8838 fc2737d5 2022-09-15 thomas }
8839 fc2737d5 2022-09-15 thomas if (++s->lineno < s->first_displayed_line)
8840 fc2737d5 2022-09-15 thomas continue;
8841 fc2737d5 2022-09-15 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8842 fc2737d5 2022-09-15 thomas continue;
8843 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline)
8844 fc2737d5 2022-09-15 thomas attr = A_STANDOUT;
8845 fc2737d5 2022-09-15 thomas
8846 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8847 fc2737d5 2022-09-15 thomas view->x ? 1 : 0);
8848 fc2737d5 2022-09-15 thomas if (err) {
8849 fc2737d5 2022-09-15 thomas free(line);
8850 fc2737d5 2022-09-15 thomas return err;
8851 fc2737d5 2022-09-15 thomas }
8852 fc2737d5 2022-09-15 thomas view->maxx = MAX(view->maxx, width);
8853 fc2737d5 2022-09-15 thomas free(wline);
8854 fc2737d5 2022-09-15 thomas wline = NULL;
8855 fc2737d5 2022-09-15 thomas
8856 fc2737d5 2022-09-15 thomas if (attr)
8857 fc2737d5 2022-09-15 thomas wattron(view->window, attr);
8858 fc2737d5 2022-09-15 thomas if (s->first_displayed_line + nprinted == s->matched_line &&
8859 fc2737d5 2022-09-15 thomas regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8860 fc2737d5 2022-09-15 thomas err = add_matched_line(&width, line, view->ncols - 1, 0,
8861 fc2737d5 2022-09-15 thomas view->window, view->x, regmatch);
8862 fc2737d5 2022-09-15 thomas if (err) {
8863 fc2737d5 2022-09-15 thomas free(line);
8864 fc2737d5 2022-09-15 thomas return err;
8865 fc2737d5 2022-09-15 thomas }
8866 fc2737d5 2022-09-15 thomas } else {
8867 fc2737d5 2022-09-15 thomas int skip;
8868 fc2737d5 2022-09-15 thomas
8869 fc2737d5 2022-09-15 thomas err = format_line(&wline, &width, &skip, line,
8870 5c3a0a1a 2023-02-03 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
8871 fc2737d5 2022-09-15 thomas if (err) {
8872 fc2737d5 2022-09-15 thomas free(line);
8873 fc2737d5 2022-09-15 thomas return err;
8874 fc2737d5 2022-09-15 thomas }
8875 5c3a0a1a 2023-02-03 thomas waddwstr(view->window, &wline[skip]);
8876 fc2737d5 2022-09-15 thomas free(wline);
8877 fc2737d5 2022-09-15 thomas wline = NULL;
8878 fc2737d5 2022-09-15 thomas }
8879 fc2737d5 2022-09-15 thomas if (s->lineno == view->hiline) {
8880 fc2737d5 2022-09-15 thomas while (width++ < view->ncols)
8881 fc2737d5 2022-09-15 thomas waddch(view->window, ' ');
8882 fc2737d5 2022-09-15 thomas } else {
8883 5c3a0a1a 2023-02-03 thomas if (width < view->ncols)
8884 fc2737d5 2022-09-15 thomas waddch(view->window, '\n');
8885 fc2737d5 2022-09-15 thomas }
8886 fc2737d5 2022-09-15 thomas if (attr)
8887 fc2737d5 2022-09-15 thomas wattroff(view->window, attr);
8888 fc2737d5 2022-09-15 thomas if (++nprinted == 1)
8889 fc2737d5 2022-09-15 thomas s->first_displayed_line = s->lineno;
8890 fc2737d5 2022-09-15 thomas }
8891 fc2737d5 2022-09-15 thomas free(line);
8892 fc2737d5 2022-09-15 thomas if (nprinted > 0)
8893 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8894 fc2737d5 2022-09-15 thomas else
8895 fc2737d5 2022-09-15 thomas s->last_displayed_line = s->first_displayed_line;
8896 fc2737d5 2022-09-15 thomas
8897 fc2737d5 2022-09-15 thomas view_border(view);
8898 fc2737d5 2022-09-15 thomas
8899 fc2737d5 2022-09-15 thomas if (s->eof) {
8900 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window,
8901 fc2737d5 2022-09-15 thomas "See the tog(1) manual page for full documentation",
8902 fc2737d5 2022-09-15 thomas view->ncols - 1);
8903 fc2737d5 2022-09-15 thomas if (rc == ERR)
8904 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8905 fc2737d5 2022-09-15 thomas } else {
8906 fc2737d5 2022-09-15 thomas wmove(view->window, view->nlines - 1, 0);
8907 fc2737d5 2022-09-15 thomas wclrtoeol(view->window);
8908 fc2737d5 2022-09-15 thomas wstandout(view->window);
8909 fc2737d5 2022-09-15 thomas rc = waddnstr(view->window, "scroll down for more...",
8910 fc2737d5 2022-09-15 thomas view->ncols - 1);
8911 fc2737d5 2022-09-15 thomas if (rc == ERR)
8912 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8913 fc2737d5 2022-09-15 thomas if (getcurx(view->window) < view->ncols - 6) {
8914 fc2737d5 2022-09-15 thomas rc = wprintw(view->window, "[%.0f%%]",
8915 fc2737d5 2022-09-15 thomas 100.00 * s->last_displayed_line / s->nlines);
8916 fc2737d5 2022-09-15 thomas if (rc == ERR)
8917 fc2737d5 2022-09-15 thomas return got_error_msg(GOT_ERR_IO, "wprintw");
8918 fc2737d5 2022-09-15 thomas }
8919 fc2737d5 2022-09-15 thomas wstandend(view->window);
8920 fc2737d5 2022-09-15 thomas }
8921 fc2737d5 2022-09-15 thomas
8922 fc2737d5 2022-09-15 thomas return NULL;
8923 fc2737d5 2022-09-15 thomas }
8924 fc2737d5 2022-09-15 thomas
8925 fc2737d5 2022-09-15 thomas static const struct got_error *
8926 fc2737d5 2022-09-15 thomas input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8927 fc2737d5 2022-09-15 thomas {
8928 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
8929 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
8930 fc2737d5 2022-09-15 thomas char *line = NULL;
8931 fc2737d5 2022-09-15 thomas ssize_t linelen;
8932 fc2737d5 2022-09-15 thomas size_t linesz = 0;
8933 fc2737d5 2022-09-15 thomas int eos, nscroll;
8934 fc2737d5 2022-09-15 thomas
8935 fc2737d5 2022-09-15 thomas eos = nscroll = view->nlines;
8936 fc2737d5 2022-09-15 thomas if (view_is_hsplit_top(view))
8937 fc2737d5 2022-09-15 thomas --eos; /* border */
8938 fc2737d5 2022-09-15 thomas
8939 fc2737d5 2022-09-15 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
8940 fc2737d5 2022-09-15 thomas
8941 fc2737d5 2022-09-15 thomas switch (ch) {
8942 fc2737d5 2022-09-15 thomas case '0':
8943 fc2737d5 2022-09-15 thomas case '$':
8944 fc2737d5 2022-09-15 thomas case KEY_RIGHT:
8945 fc2737d5 2022-09-15 thomas case 'l':
8946 fc2737d5 2022-09-15 thomas case KEY_LEFT:
8947 fc2737d5 2022-09-15 thomas case 'h':
8948 c72de8ab 2023-02-03 thomas horizontal_scroll_input(view, ch);
8949 fc2737d5 2022-09-15 thomas break;
8950 fc2737d5 2022-09-15 thomas case 'g':
8951 fc2737d5 2022-09-15 thomas case KEY_HOME:
8952 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
8953 fc2737d5 2022-09-15 thomas view->count = 0;
8954 fc2737d5 2022-09-15 thomas break;
8955 fc2737d5 2022-09-15 thomas case 'G':
8956 fc2737d5 2022-09-15 thomas case KEY_END:
8957 fc2737d5 2022-09-15 thomas view->count = 0;
8958 fc2737d5 2022-09-15 thomas if (s->eof)
8959 fc2737d5 2022-09-15 thomas break;
8960 fc2737d5 2022-09-15 thomas s->first_displayed_line = (s->nlines - eos) + 3;
8961 fc2737d5 2022-09-15 thomas s->eof = 1;
8962 fc2737d5 2022-09-15 thomas break;
8963 fc2737d5 2022-09-15 thomas case 'k':
8964 fc2737d5 2022-09-15 thomas case KEY_UP:
8965 fc2737d5 2022-09-15 thomas if (s->first_displayed_line > 1)
8966 fc2737d5 2022-09-15 thomas --s->first_displayed_line;
8967 fc2737d5 2022-09-15 thomas else
8968 fc2737d5 2022-09-15 thomas view->count = 0;
8969 fc2737d5 2022-09-15 thomas break;
8970 fc2737d5 2022-09-15 thomas case CTRL('u'):
8971 fc2737d5 2022-09-15 thomas case 'u':
8972 fc2737d5 2022-09-15 thomas nscroll /= 2;
8973 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8974 fc2737d5 2022-09-15 thomas case KEY_PPAGE:
8975 fc2737d5 2022-09-15 thomas case CTRL('b'):
8976 fc2737d5 2022-09-15 thomas case 'b':
8977 fc2737d5 2022-09-15 thomas if (s->first_displayed_line == 1) {
8978 fc2737d5 2022-09-15 thomas view->count = 0;
8979 fc2737d5 2022-09-15 thomas break;
8980 fc2737d5 2022-09-15 thomas }
8981 fc2737d5 2022-09-15 thomas while (--nscroll > 0 && s->first_displayed_line > 1)
8982 fc2737d5 2022-09-15 thomas s->first_displayed_line--;
8983 fc2737d5 2022-09-15 thomas break;
8984 fc2737d5 2022-09-15 thomas case 'j':
8985 fc2737d5 2022-09-15 thomas case KEY_DOWN:
8986 fc2737d5 2022-09-15 thomas case CTRL('n'):
8987 fc2737d5 2022-09-15 thomas if (!s->eof)
8988 fc2737d5 2022-09-15 thomas ++s->first_displayed_line;
8989 fc2737d5 2022-09-15 thomas else
8990 fc2737d5 2022-09-15 thomas view->count = 0;
8991 fc2737d5 2022-09-15 thomas break;
8992 fc2737d5 2022-09-15 thomas case CTRL('d'):
8993 fc2737d5 2022-09-15 thomas case 'd':
8994 fc2737d5 2022-09-15 thomas nscroll /= 2;
8995 fc2737d5 2022-09-15 thomas /* FALL THROUGH */
8996 fc2737d5 2022-09-15 thomas case KEY_NPAGE:
8997 fc2737d5 2022-09-15 thomas case CTRL('f'):
8998 fc2737d5 2022-09-15 thomas case 'f':
8999 fc2737d5 2022-09-15 thomas case ' ':
9000 fc2737d5 2022-09-15 thomas if (s->eof) {
9001 fc2737d5 2022-09-15 thomas view->count = 0;
9002 fc2737d5 2022-09-15 thomas break;
9003 fc2737d5 2022-09-15 thomas }
9004 fc2737d5 2022-09-15 thomas while (!s->eof && --nscroll > 0) {
9005 fc2737d5 2022-09-15 thomas linelen = getline(&line, &linesz, s->f);
9006 fc2737d5 2022-09-15 thomas s->first_displayed_line++;
9007 fc2737d5 2022-09-15 thomas if (linelen == -1) {
9008 fc2737d5 2022-09-15 thomas if (feof(s->f))
9009 fc2737d5 2022-09-15 thomas s->eof = 1;
9010 fc2737d5 2022-09-15 thomas else
9011 fc2737d5 2022-09-15 thomas err = got_ferror(s->f, GOT_ERR_IO);
9012 fc2737d5 2022-09-15 thomas break;
9013 fc2737d5 2022-09-15 thomas }
9014 fc2737d5 2022-09-15 thomas }
9015 fc2737d5 2022-09-15 thomas free(line);
9016 fc2737d5 2022-09-15 thomas break;
9017 fc2737d5 2022-09-15 thomas default:
9018 fc2737d5 2022-09-15 thomas view->count = 0;
9019 fc2737d5 2022-09-15 thomas break;
9020 fc2737d5 2022-09-15 thomas }
9021 fc2737d5 2022-09-15 thomas
9022 fc2737d5 2022-09-15 thomas return err;
9023 fc2737d5 2022-09-15 thomas }
9024 fc2737d5 2022-09-15 thomas
9025 fc2737d5 2022-09-15 thomas static const struct got_error *
9026 fc2737d5 2022-09-15 thomas close_help_view(struct tog_view *view)
9027 fc2737d5 2022-09-15 thomas {
9028 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9029 fc2737d5 2022-09-15 thomas
9030 fc2737d5 2022-09-15 thomas free(s->line_offsets);
9031 fc2737d5 2022-09-15 thomas s->line_offsets = NULL;
9032 fc2737d5 2022-09-15 thomas if (fclose(s->f) == EOF)
9033 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9034 fc2737d5 2022-09-15 thomas
9035 fc2737d5 2022-09-15 thomas return NULL;
9036 fc2737d5 2022-09-15 thomas }
9037 fc2737d5 2022-09-15 thomas
9038 fc2737d5 2022-09-15 thomas static const struct got_error *
9039 fc2737d5 2022-09-15 thomas reset_help_view(struct tog_view *view)
9040 fc2737d5 2022-09-15 thomas {
9041 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9042 fc2737d5 2022-09-15 thomas
9043 fc2737d5 2022-09-15 thomas
9044 fc2737d5 2022-09-15 thomas if (s->f && fclose(s->f) == EOF)
9045 fc2737d5 2022-09-15 thomas return got_error_from_errno("fclose");
9046 fc2737d5 2022-09-15 thomas
9047 fc2737d5 2022-09-15 thomas wclear(view->window);
9048 fc2737d5 2022-09-15 thomas view->count = 0;
9049 fc2737d5 2022-09-15 thomas view->x = 0;
9050 fc2737d5 2022-09-15 thomas s->all = !s->all;
9051 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9052 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9053 fc2737d5 2022-09-15 thomas s->matched_line = 0;
9054 fc2737d5 2022-09-15 thomas
9055 fc2737d5 2022-09-15 thomas return create_help(s);
9056 fc2737d5 2022-09-15 thomas }
9057 fc2737d5 2022-09-15 thomas
9058 fc2737d5 2022-09-15 thomas static const struct got_error *
9059 fc2737d5 2022-09-15 thomas open_help_view(struct tog_view *view, struct tog_view *parent)
9060 fc2737d5 2022-09-15 thomas {
9061 fc2737d5 2022-09-15 thomas const struct got_error *err = NULL;
9062 fc2737d5 2022-09-15 thomas struct tog_help_view_state *s = &view->state.help;
9063 fc2737d5 2022-09-15 thomas
9064 fc2737d5 2022-09-15 thomas s->type = (enum tog_keymap_type)parent->type;
9065 fc2737d5 2022-09-15 thomas s->first_displayed_line = 1;
9066 fc2737d5 2022-09-15 thomas s->last_displayed_line = view->nlines;
9067 fc2737d5 2022-09-15 thomas s->selected_line = 1;
9068 fc2737d5 2022-09-15 thomas
9069 fc2737d5 2022-09-15 thomas view->show = show_help_view;
9070 fc2737d5 2022-09-15 thomas view->input = input_help_view;
9071 fc2737d5 2022-09-15 thomas view->reset = reset_help_view;
9072 fc2737d5 2022-09-15 thomas view->close = close_help_view;
9073 fc2737d5 2022-09-15 thomas view->search_start = search_start_help_view;
9074 2a7d3cd7 2022-09-17 thomas view->search_setup = search_setup_help_view;
9075 fc2737d5 2022-09-15 thomas view->search_next = search_next_view_match;
9076 fc2737d5 2022-09-15 thomas
9077 fc2737d5 2022-09-15 thomas err = create_help(s);
9078 fc2737d5 2022-09-15 thomas return err;
9079 fc2737d5 2022-09-15 thomas }
9080 fc2737d5 2022-09-15 thomas
9081 fc2737d5 2022-09-15 thomas static const struct got_error *
9082 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
9083 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
9084 2a31b33b 2022-07-23 thomas {
9085 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
9086 2a31b33b 2022-07-23 thomas
9087 2a31b33b 2022-07-23 thomas *new_view = NULL;
9088 2a31b33b 2022-07-23 thomas
9089 2a31b33b 2022-07-23 thomas switch (request) {
9090 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
9091 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
9092 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
9093 2a31b33b 2022-07-23 thomas
9094 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
9095 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
9096 2a31b33b 2022-07-23 thomas view, s->repo);
9097 2a31b33b 2022-07-23 thomas } else
9098 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9099 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9100 2a31b33b 2022-07-23 thomas break;
9101 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
9102 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
9103 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
9104 2a31b33b 2022-07-23 thomas
9105 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
9106 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
9107 2a31b33b 2022-07-23 thomas s->repo);
9108 2a31b33b 2022-07-23 thomas } else
9109 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9110 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9111 2a31b33b 2022-07-23 thomas break;
9112 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
9113 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
9114 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
9115 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
9116 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9117 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
9118 2a31b33b 2022-07-23 thomas &view->state.tree);
9119 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9120 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
9121 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9122 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9123 2a31b33b 2022-07-23 thomas else
9124 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9125 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9126 2a31b33b 2022-07-23 thomas break;
9127 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
9128 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9129 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
9130 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
9131 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
9132 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
9133 2a31b33b 2022-07-23 thomas view->state.log.repo);
9134 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
9135 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
9136 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
9137 2a31b33b 2022-07-23 thomas view->state.ref.repo);
9138 2a31b33b 2022-07-23 thomas else
9139 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
9140 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9141 2a31b33b 2022-07-23 thomas break;
9142 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
9143 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9144 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
9145 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
9146 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
9147 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
9148 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
9149 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
9150 2a31b33b 2022-07-23 thomas else
9151 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
9152 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
9153 2a31b33b 2022-07-23 thomas if (err)
9154 2a31b33b 2022-07-23 thomas view_close(*new_view);
9155 2a31b33b 2022-07-23 thomas break;
9156 fc2737d5 2022-09-15 thomas case TOG_VIEW_HELP:
9157 a7dd23ad 2022-09-19 thomas *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9158 fc2737d5 2022-09-15 thomas if (*new_view == NULL)
9159 fc2737d5 2022-09-15 thomas return got_error_from_errno("view_open");
9160 fc2737d5 2022-09-15 thomas err = open_help_view(*new_view, view);
9161 fc2737d5 2022-09-15 thomas if (err)
9162 fc2737d5 2022-09-15 thomas view_close(*new_view);
9163 fc2737d5 2022-09-15 thomas break;
9164 2a31b33b 2022-07-23 thomas default:
9165 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9166 2a31b33b 2022-07-23 thomas }
9167 2a31b33b 2022-07-23 thomas
9168 2a31b33b 2022-07-23 thomas return err;
9169 2a31b33b 2022-07-23 thomas }
9170 2a31b33b 2022-07-23 thomas
9171 a5d43cac 2022-07-01 thomas /*
9172 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
9173 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
9174 a5d43cac 2022-07-01 thomas */
9175 6458efa5 2020-11-24 stsp static void
9176 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
9177 a5d43cac 2022-07-01 thomas {
9178 a5d43cac 2022-07-01 thomas switch (view->type) {
9179 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9180 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9181 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
9182 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
9183 a5d43cac 2022-07-01 thomas 1);
9184 a5d43cac 2022-07-01 thomas break;
9185 a5d43cac 2022-07-01 thomas }
9186 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
9187 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
9188 a5d43cac 2022-07-01 thomas else
9189 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
9190 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
9191 a5d43cac 2022-07-01 thomas break;
9192 a5d43cac 2022-07-01 thomas }
9193 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
9194 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
9195 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
9196 a5d43cac 2022-07-01 thomas break;
9197 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
9198 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
9199 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
9200 a5d43cac 2022-07-01 thomas break;
9201 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
9202 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
9203 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
9204 a5d43cac 2022-07-01 thomas break;
9205 a5d43cac 2022-07-01 thomas default:
9206 a5d43cac 2022-07-01 thomas break;
9207 a5d43cac 2022-07-01 thomas }
9208 a5d43cac 2022-07-01 thomas
9209 a5d43cac 2022-07-01 thomas view->offset = 0;
9210 a5d43cac 2022-07-01 thomas }
9211 a5d43cac 2022-07-01 thomas
9212 a5d43cac 2022-07-01 thomas /*
9213 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
9214 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
9215 a5d43cac 2022-07-01 thomas */
9216 a5d43cac 2022-07-01 thomas static const struct got_error *
9217 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
9218 a5d43cac 2022-07-01 thomas {
9219 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
9220 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
9221 a5d43cac 2022-07-01 thomas int *selected = NULL;
9222 a5d43cac 2022-07-01 thomas int header, offset;
9223 a5d43cac 2022-07-01 thomas
9224 a5d43cac 2022-07-01 thomas switch (view->type) {
9225 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
9226 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
9227 a5d43cac 2022-07-01 thomas header = 3;
9228 a5d43cac 2022-07-01 thomas scrolld = NULL;
9229 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
9230 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
9231 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
9232 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
9233 a5d43cac 2022-07-01 thomas view->offset = offset;
9234 a5d43cac 2022-07-01 thomas }
9235 a5d43cac 2022-07-01 thomas break;
9236 a5d43cac 2022-07-01 thomas }
9237 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
9238 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
9239 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
9240 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
9241 a5d43cac 2022-07-01 thomas selected = &s->selected;
9242 a5d43cac 2022-07-01 thomas break;
9243 a5d43cac 2022-07-01 thomas }
9244 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
9245 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
9246 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
9247 a5d43cac 2022-07-01 thomas header = 3;
9248 a5d43cac 2022-07-01 thomas selected = &s->selected;
9249 a5d43cac 2022-07-01 thomas break;
9250 a5d43cac 2022-07-01 thomas }
9251 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
9252 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
9253 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
9254 a5d43cac 2022-07-01 thomas header = 5;
9255 a5d43cac 2022-07-01 thomas selected = &s->selected;
9256 a5d43cac 2022-07-01 thomas break;
9257 a5d43cac 2022-07-01 thomas }
9258 a5d43cac 2022-07-01 thomas default:
9259 a5d43cac 2022-07-01 thomas selected = NULL;
9260 a5d43cac 2022-07-01 thomas scrolld = NULL;
9261 a5d43cac 2022-07-01 thomas header = 0;
9262 a5d43cac 2022-07-01 thomas break;
9263 a5d43cac 2022-07-01 thomas }
9264 a5d43cac 2022-07-01 thomas
9265 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
9266 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
9267 a5d43cac 2022-07-01 thomas view->offset = offset;
9268 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
9269 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
9270 a5d43cac 2022-07-01 thomas *selected -= offset;
9271 a5d43cac 2022-07-01 thomas }
9272 a5d43cac 2022-07-01 thomas }
9273 a5d43cac 2022-07-01 thomas
9274 a5d43cac 2022-07-01 thomas return err;
9275 a5d43cac 2022-07-01 thomas }
9276 a5d43cac 2022-07-01 thomas
9277 a5d43cac 2022-07-01 thomas static void
9278 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
9279 ce5b7c56 2019-07-09 stsp {
9280 6059809a 2020-12-17 stsp size_t i;
9281 9f7d7167 2018-04-29 stsp
9282 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
9283 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
9284 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
9285 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
9286 ce5b7c56 2019-07-09 stsp }
9287 6879ba42 2020-10-01 naddy fputc('\n', fp);
9288 ce5b7c56 2019-07-09 stsp }
9289 ce5b7c56 2019-07-09 stsp
9290 4ed7e80c 2018-05-20 stsp __dead static void
9291 6879ba42 2020-10-01 naddy usage(int hflag, int status)
9292 9f7d7167 2018-04-29 stsp {
9293 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
9294 6879ba42 2020-10-01 naddy
9295 0a58e722 2022-10-04 thomas fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9296 6879ba42 2020-10-01 naddy getprogname());
9297 6879ba42 2020-10-01 naddy if (hflag) {
9298 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
9299 6879ba42 2020-10-01 naddy list_commands(fp);
9300 ee85c5e8 2020-02-29 stsp }
9301 6879ba42 2020-10-01 naddy exit(status);
9302 9f7d7167 2018-04-29 stsp }
9303 9f7d7167 2018-04-29 stsp
9304 c2301be8 2018-04-30 stsp static char **
9305 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
9306 c2301be8 2018-04-30 stsp {
9307 ee85c5e8 2020-02-29 stsp va_list ap;
9308 c2301be8 2018-04-30 stsp char **argv;
9309 ee85c5e8 2020-02-29 stsp int i;
9310 c2301be8 2018-04-30 stsp
9311 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
9312 ee85c5e8 2020-02-29 stsp
9313 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
9314 c2301be8 2018-04-30 stsp if (argv == NULL)
9315 c2301be8 2018-04-30 stsp err(1, "calloc");
9316 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
9317 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
9318 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
9319 e10c916e 2019-09-15 hiltjo err(1, "strdup");
9320 c2301be8 2018-04-30 stsp }
9321 c2301be8 2018-04-30 stsp
9322 ee85c5e8 2020-02-29 stsp va_end(ap);
9323 c2301be8 2018-04-30 stsp return argv;
9324 ee85c5e8 2020-02-29 stsp }
9325 ee85c5e8 2020-02-29 stsp
9326 ee85c5e8 2020-02-29 stsp /*
9327 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
9328 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
9329 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
9330 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
9331 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
9332 ee85c5e8 2020-02-29 stsp */
9333 ee85c5e8 2020-02-29 stsp static const struct got_error *
9334 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
9335 ee85c5e8 2020-02-29 stsp {
9336 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
9337 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9338 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
9339 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
9340 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
9341 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
9342 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9343 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
9344 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
9345 84de9106 2020-12-26 stsp
9346 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
9347 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
9348 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
9349 ee85c5e8 2020-02-29 stsp
9350 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
9351 7cd52833 2022-06-23 thomas if (error != NULL)
9352 7cd52833 2022-06-23 thomas goto done;
9353 7cd52833 2022-06-23 thomas
9354 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
9355 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9356 ee85c5e8 2020-02-29 stsp goto done;
9357 ee85c5e8 2020-02-29 stsp
9358 ee85c5e8 2020-02-29 stsp if (worktree)
9359 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
9360 ee85c5e8 2020-02-29 stsp else
9361 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
9362 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
9363 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
9364 ee85c5e8 2020-02-29 stsp goto done;
9365 ee85c5e8 2020-02-29 stsp }
9366 ee85c5e8 2020-02-29 stsp
9367 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9368 ee85c5e8 2020-02-29 stsp if (error != NULL)
9369 ee85c5e8 2020-02-29 stsp goto done;
9370 ee85c5e8 2020-02-29 stsp
9371 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9372 ee85c5e8 2020-02-29 stsp repo, worktree);
9373 ee85c5e8 2020-02-29 stsp if (error)
9374 ee85c5e8 2020-02-29 stsp goto done;
9375 ee85c5e8 2020-02-29 stsp
9376 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
9377 84de9106 2020-12-26 stsp if (error)
9378 84de9106 2020-12-26 stsp goto done;
9379 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9380 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9381 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9382 ee85c5e8 2020-02-29 stsp if (error)
9383 ee85c5e8 2020-02-29 stsp goto done;
9384 ee85c5e8 2020-02-29 stsp
9385 ee85c5e8 2020-02-29 stsp if (worktree) {
9386 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9387 ee85c5e8 2020-02-29 stsp worktree = NULL;
9388 ee85c5e8 2020-02-29 stsp }
9389 ee85c5e8 2020-02-29 stsp
9390 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
9391 945f9229 2022-04-16 thomas if (error)
9392 945f9229 2022-04-16 thomas goto done;
9393 945f9229 2022-04-16 thomas
9394 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9395 ee85c5e8 2020-02-29 stsp if (error) {
9396 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
9397 ee85c5e8 2020-02-29 stsp goto done;
9398 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
9399 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
9400 6879ba42 2020-10-01 naddy usage(1, 1);
9401 ee85c5e8 2020-02-29 stsp /* not reached */
9402 ee85c5e8 2020-02-29 stsp }
9403 ee85c5e8 2020-02-29 stsp
9404 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
9405 ee85c5e8 2020-02-29 stsp if (error)
9406 ee85c5e8 2020-02-29 stsp goto done;
9407 ee85c5e8 2020-02-29 stsp
9408 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
9409 ee85c5e8 2020-02-29 stsp argc = 4;
9410 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9411 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
9412 ee85c5e8 2020-02-29 stsp done:
9413 1d0f4054 2021-06-17 stsp if (repo) {
9414 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
9415 1d0f4054 2021-06-17 stsp if (error == NULL)
9416 1d0f4054 2021-06-17 stsp error = close_err;
9417 1d0f4054 2021-06-17 stsp }
9418 945f9229 2022-04-16 thomas if (commit)
9419 945f9229 2022-04-16 thomas got_object_commit_close(commit);
9420 ee85c5e8 2020-02-29 stsp if (worktree)
9421 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
9422 7cd52833 2022-06-23 thomas if (pack_fds) {
9423 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
9424 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
9425 7cd52833 2022-06-23 thomas if (error == NULL)
9426 7cd52833 2022-06-23 thomas error = pack_err;
9427 7cd52833 2022-06-23 thomas }
9428 ee85c5e8 2020-02-29 stsp free(id);
9429 ee85c5e8 2020-02-29 stsp free(commit_id_str);
9430 ee85c5e8 2020-02-29 stsp free(commit_id);
9431 ee85c5e8 2020-02-29 stsp free(cwd);
9432 ee85c5e8 2020-02-29 stsp free(repo_path);
9433 ee85c5e8 2020-02-29 stsp free(in_repo_path);
9434 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
9435 ee85c5e8 2020-02-29 stsp int i;
9436 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
9437 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
9438 ee85c5e8 2020-02-29 stsp free(cmd_argv);
9439 ee85c5e8 2020-02-29 stsp }
9440 87670572 2020-12-26 naddy tog_free_refs();
9441 ee85c5e8 2020-02-29 stsp return error;
9442 c2301be8 2018-04-30 stsp }
9443 c2301be8 2018-04-30 stsp
9444 9f7d7167 2018-04-29 stsp int
9445 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
9446 9f7d7167 2018-04-29 stsp {
9447 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
9448 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
9449 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
9450 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
9451 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
9452 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
9453 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
9454 83cd27f8 2020-01-13 stsp };
9455 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
9456 9f7d7167 2018-04-29 stsp
9457 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
9458 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
9459 cacf1690 2022-09-06 thomas
9460 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
9461 9f7d7167 2018-04-29 stsp
9462 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9463 9f7d7167 2018-04-29 stsp switch (ch) {
9464 9f7d7167 2018-04-29 stsp case 'h':
9465 9f7d7167 2018-04-29 stsp hflag = 1;
9466 9f7d7167 2018-04-29 stsp break;
9467 53ccebc2 2019-07-30 stsp case 'V':
9468 53ccebc2 2019-07-30 stsp Vflag = 1;
9469 53ccebc2 2019-07-30 stsp break;
9470 9f7d7167 2018-04-29 stsp default:
9471 6879ba42 2020-10-01 naddy usage(hflag, 1);
9472 9f7d7167 2018-04-29 stsp /* NOTREACHED */
9473 9f7d7167 2018-04-29 stsp }
9474 9f7d7167 2018-04-29 stsp }
9475 9f7d7167 2018-04-29 stsp
9476 9f7d7167 2018-04-29 stsp argc -= optind;
9477 9f7d7167 2018-04-29 stsp argv += optind;
9478 9814e6a3 2020-09-27 naddy optind = 1;
9479 c2301be8 2018-04-30 stsp optreset = 1;
9480 9f7d7167 2018-04-29 stsp
9481 53ccebc2 2019-07-30 stsp if (Vflag) {
9482 53ccebc2 2019-07-30 stsp got_version_print_str();
9483 6879ba42 2020-10-01 naddy return 0;
9484 53ccebc2 2019-07-30 stsp }
9485 4010e238 2020-12-04 stsp
9486 4010e238 2020-12-04 stsp #ifndef PROFILE
9487 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9488 4010e238 2020-12-04 stsp NULL) == -1)
9489 4010e238 2020-12-04 stsp err(1, "pledge");
9490 4010e238 2020-12-04 stsp #endif
9491 53ccebc2 2019-07-30 stsp
9492 c2301be8 2018-04-30 stsp if (argc == 0) {
9493 f29d3e89 2018-06-23 stsp if (hflag)
9494 6879ba42 2020-10-01 naddy usage(hflag, 0);
9495 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
9496 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
9497 c2301be8 2018-04-30 stsp argc = 1;
9498 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
9499 c2301be8 2018-04-30 stsp } else {
9500 6059809a 2020-12-17 stsp size_t i;
9501 9f7d7167 2018-04-29 stsp
9502 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
9503 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
9504 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
9505 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
9506 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
9507 9f7d7167 2018-04-29 stsp break;
9508 9f7d7167 2018-04-29 stsp }
9509 9f7d7167 2018-04-29 stsp }
9510 ee85c5e8 2020-02-29 stsp }
9511 3642c4c6 2019-07-09 stsp
9512 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9513 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
9514 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
9515 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9516 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
9517 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9518 adf4c9e0 2022-07-03 thomas }
9519 adf4c9e0 2022-07-03 thomas
9520 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
9521 ee85c5e8 2020-02-29 stsp if (argc != 1)
9522 6879ba42 2020-10-01 naddy usage(0, 1);
9523 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
9524 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
9525 ee85c5e8 2020-02-29 stsp } else {
9526 ee85c5e8 2020-02-29 stsp if (hflag)
9527 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
9528 ee85c5e8 2020-02-29 stsp else
9529 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9530 9f7d7167 2018-04-29 stsp }
9531 9f7d7167 2018-04-29 stsp
9532 9f7d7167 2018-04-29 stsp endwin();
9533 a2f4a359 2020-02-28 stsp if (cmd_argv) {
9534 a2f4a359 2020-02-28 stsp int i;
9535 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
9536 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
9537 a2f4a359 2020-02-28 stsp free(cmd_argv);
9538 a2f4a359 2020-02-28 stsp }
9539 a2f4a359 2020-02-28 stsp
9540 27a7eb23 2022-10-24 thomas if (error && error->code != GOT_ERR_CANCELLED &&
9541 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_EOF &&
9542 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_EXIT &&
9543 27a7eb23 2022-10-24 thomas error->code != GOT_ERR_PRIVSEP_PIPE &&
9544 27a7eb23 2022-10-24 thomas !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9545 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9546 9f7d7167 2018-04-29 stsp return 0;
9547 9f7d7167 2018-04-29 stsp }