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 d6b05b5b 2018-08-04 stsp };
112 c3e9aa98 2019-05-13 jcs
113 a5d43cac 2022-07-01 thomas enum tog_view_mode {
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
117 a5d43cac 2022-07-01 thomas };
118 a5d43cac 2022-07-01 thomas
119 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 a5d43cac 2022-07-01 thomas
121 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
122 d6b05b5b 2018-08-04 stsp
123 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
124 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
125 ba4f502b 2018-08-04 stsp struct got_object_id *id;
126 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
127 1a76625f 2018-10-22 stsp int idx;
128 ba4f502b 2018-08-04 stsp };
129 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 ba4f502b 2018-08-04 stsp struct commit_queue {
131 ba4f502b 2018-08-04 stsp int ncommits;
132 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
133 6d17833f 2019-11-08 stsp };
134 6d17833f 2019-11-08 stsp
135 f26dddb7 2019-11-08 stsp struct tog_color {
136 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
137 6d17833f 2019-11-08 stsp regex_t regex;
138 6d17833f 2019-11-08 stsp short colorpair;
139 15a087fe 2019-02-21 stsp };
140 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
141 11b20872 2019-11-08 stsp
142 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
144 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 2183bbf6 2022-01-23 thomas
146 2183bbf6 2022-01-23 thomas static const struct got_error *
147 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 2183bbf6 2022-01-23 thomas struct got_reference* re2)
149 2183bbf6 2022-01-23 thomas {
150 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
151 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
152 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
153 2183bbf6 2022-01-23 thomas
154 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
155 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
158 2183bbf6 2022-01-23 thomas *cmp = -1;
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
161 2183bbf6 2022-01-23 thomas *cmp = 1;
162 2183bbf6 2022-01-23 thomas return NULL;
163 2183bbf6 2022-01-23 thomas }
164 2183bbf6 2022-01-23 thomas
165 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 2183bbf6 2022-01-23 thomas return NULL;
167 2183bbf6 2022-01-23 thomas }
168 51a10b52 2020-12-26 stsp
169 11b20872 2019-11-08 stsp static const struct got_error *
170 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
171 51a10b52 2020-12-26 stsp {
172 51a10b52 2020-12-26 stsp const struct got_error *err;
173 51a10b52 2020-12-26 stsp
174 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 3bfadbd4 2021-11-20 thomas repo);
177 51a10b52 2020-12-26 stsp if (err)
178 51a10b52 2020-12-26 stsp return err;
179 51a10b52 2020-12-26 stsp
180 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 51a10b52 2020-12-26 stsp repo);
182 51a10b52 2020-12-26 stsp }
183 51a10b52 2020-12-26 stsp
184 51a10b52 2020-12-26 stsp static void
185 51a10b52 2020-12-26 stsp tog_free_refs(void)
186 51a10b52 2020-12-26 stsp {
187 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
188 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
189 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
190 51a10b52 2020-12-26 stsp }
191 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
192 51a10b52 2020-12-26 stsp }
193 51a10b52 2020-12-26 stsp
194 51a10b52 2020-12-26 stsp static const struct got_error *
195 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
196 11b20872 2019-11-08 stsp int idx, short color)
197 11b20872 2019-11-08 stsp {
198 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
199 11b20872 2019-11-08 stsp struct tog_color *tc;
200 11b20872 2019-11-08 stsp int regerr = 0;
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
203 11b20872 2019-11-08 stsp return NULL;
204 11b20872 2019-11-08 stsp
205 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
206 11b20872 2019-11-08 stsp
207 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
208 11b20872 2019-11-08 stsp if (tc == NULL)
209 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
210 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
211 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 11b20872 2019-11-08 stsp if (regerr) {
213 11b20872 2019-11-08 stsp static char regerr_msg[512];
214 11b20872 2019-11-08 stsp static char err_msg[512];
215 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
216 11b20872 2019-11-08 stsp sizeof(regerr_msg));
217 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 11b20872 2019-11-08 stsp regerr_msg);
219 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 11b20872 2019-11-08 stsp free(tc);
221 11b20872 2019-11-08 stsp return err;
222 11b20872 2019-11-08 stsp }
223 11b20872 2019-11-08 stsp tc->colorpair = idx;
224 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
225 11b20872 2019-11-08 stsp return NULL;
226 11b20872 2019-11-08 stsp }
227 11b20872 2019-11-08 stsp
228 11b20872 2019-11-08 stsp static void
229 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
230 11b20872 2019-11-08 stsp {
231 11b20872 2019-11-08 stsp struct tog_color *tc;
232 11b20872 2019-11-08 stsp
233 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
234 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
235 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
236 11b20872 2019-11-08 stsp regfree(&tc->regex);
237 11b20872 2019-11-08 stsp free(tc);
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 ef20f542 2022-06-26 thomas static struct tog_color *
242 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
247 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
248 11b20872 2019-11-08 stsp return tc;
249 11b20872 2019-11-08 stsp }
250 11b20872 2019-11-08 stsp
251 11b20872 2019-11-08 stsp return NULL;
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 11b20872 2019-11-08 stsp static int
255 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_CYAN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 11b20872 2019-11-08 stsp return COLOR_YELLOW;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 11b20872 2019-11-08 stsp return COLOR_CYAN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 11b20872 2019-11-08 stsp return COLOR_YELLOW;
279 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 6458efa5 2020-11-24 stsp return COLOR_GREEN;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
285 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
287 11b20872 2019-11-08 stsp
288 11b20872 2019-11-08 stsp return -1;
289 11b20872 2019-11-08 stsp }
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp static int
292 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
293 11b20872 2019-11-08 stsp {
294 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
295 11b20872 2019-11-08 stsp
296 11b20872 2019-11-08 stsp if (val == NULL)
297 11b20872 2019-11-08 stsp return default_color_value(envvar);
298 15a087fe 2019-02-21 stsp
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
300 11b20872 2019-11-08 stsp return COLOR_BLACK;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
302 11b20872 2019-11-08 stsp return COLOR_RED;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
304 11b20872 2019-11-08 stsp return COLOR_GREEN;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
306 11b20872 2019-11-08 stsp return COLOR_YELLOW;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLUE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
310 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
312 11b20872 2019-11-08 stsp return COLOR_CYAN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
314 11b20872 2019-11-08 stsp return COLOR_WHITE;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
316 11b20872 2019-11-08 stsp return -1;
317 11b20872 2019-11-08 stsp
318 11b20872 2019-11-08 stsp return default_color_value(envvar);
319 11b20872 2019-11-08 stsp }
320 11b20872 2019-11-08 stsp
321 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
322 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
323 3dbaef42 2020-11-24 stsp const char *label1, *label2;
324 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
325 19a6a6b5 2022-07-01 thomas int fd1, fd2;
326 82c78e96 2022-08-06 thomas int lineno;
327 15a087fe 2019-02-21 stsp int first_displayed_line;
328 15a087fe 2019-02-21 stsp int last_displayed_line;
329 15a087fe 2019-02-21 stsp int eof;
330 15a087fe 2019-02-21 stsp int diff_context;
331 3dbaef42 2020-11-24 stsp int ignore_whitespace;
332 64453f7e 2020-11-21 stsp int force_text_diff;
333 15a087fe 2019-02-21 stsp struct got_repository *repo;
334 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey int matched_line;
337 f44b1f58 2020-02-02 tracey int selected_line;
338 15a087fe 2019-02-21 stsp
339 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
340 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
341 b01e7d3b 2018-08-04 stsp };
342 b01e7d3b 2018-08-04 stsp
343 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
345 1a76625f 2018-10-22 stsp
346 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
347 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
348 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
349 1a76625f 2018-10-22 stsp int commits_needed;
350 fb280deb 2021-08-30 stsp int load_all;
351 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
352 7e8004ba 2022-09-11 thomas struct commit_queue *real_commits;
353 1a76625f 2018-10-22 stsp const char *in_repo_path;
354 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
355 1a76625f 2018-10-22 stsp struct got_repository *repo;
356 1af5eddf 2022-06-23 thomas int *pack_fds;
357 1a76625f 2018-10-22 stsp int log_complete;
358 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
359 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
361 13add988 2019-10-15 stsp int *searching;
362 13add988 2019-10-15 stsp int *search_next_done;
363 13add988 2019-10-15 stsp regex_t *regex;
364 7e8004ba 2022-09-11 thomas int *limiting;
365 7e8004ba 2022-09-11 thomas int limit_match;
366 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
367 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
368 1a76625f 2018-10-22 stsp };
369 1a76625f 2018-10-22 stsp
370 1a76625f 2018-10-22 stsp struct tog_log_view_state {
371 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
372 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
373 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
374 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
375 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
376 b01e7d3b 2018-08-04 stsp int selected;
377 b01e7d3b 2018-08-04 stsp char *in_repo_path;
378 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
379 b672a97a 2020-01-27 stsp int log_branches;
380 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
381 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
382 1a76625f 2018-10-22 stsp sig_atomic_t quit;
383 1a76625f 2018-10-22 stsp pthread_t thread;
384 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
385 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
386 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
387 11b20872 2019-11-08 stsp struct tog_colors colors;
388 f69c5a46 2022-07-19 thomas int use_committer;
389 7e8004ba 2022-09-11 thomas int limit_view;
390 7e8004ba 2022-09-11 thomas regex_t limit_regex;
391 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
392 ba4f502b 2018-08-04 stsp };
393 11b20872 2019-11-08 stsp
394 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
395 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
396 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
397 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
398 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
399 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
400 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
401 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
402 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
403 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
404 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
405 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
406 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
407 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
408 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
409 ba4f502b 2018-08-04 stsp
410 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
411 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
412 e9424729 2018-08-04 stsp int nlines;
413 e9424729 2018-08-04 stsp
414 e9424729 2018-08-04 stsp struct tog_view *view;
415 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
416 e9424729 2018-08-04 stsp int *quit;
417 e9424729 2018-08-04 stsp };
418 e9424729 2018-08-04 stsp
419 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
420 e9424729 2018-08-04 stsp const char *path;
421 e9424729 2018-08-04 stsp struct got_repository *repo;
422 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
423 e9424729 2018-08-04 stsp int *complete;
424 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
425 fc06ba56 2019-08-22 stsp void *cancel_arg;
426 e9424729 2018-08-04 stsp };
427 e9424729 2018-08-04 stsp
428 e9424729 2018-08-04 stsp struct tog_blame {
429 e9424729 2018-08-04 stsp FILE *f;
430 be659d10 2020-11-18 stsp off_t filesize;
431 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
432 6fcac457 2018-11-19 stsp int nlines;
433 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
434 e9424729 2018-08-04 stsp pthread_t thread;
435 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
436 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
437 e9424729 2018-08-04 stsp const char *path;
438 7cd52833 2022-06-23 thomas int *pack_fds;
439 e9424729 2018-08-04 stsp };
440 e9424729 2018-08-04 stsp
441 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
442 7cbe629d 2018-08-04 stsp int first_displayed_line;
443 7cbe629d 2018-08-04 stsp int last_displayed_line;
444 7cbe629d 2018-08-04 stsp int selected_line;
445 4fc71f3b 2022-07-12 thomas int last_diffed_line;
446 7cbe629d 2018-08-04 stsp int blame_complete;
447 e5a0f69f 2018-08-18 stsp int eof;
448 e5a0f69f 2018-08-18 stsp int done;
449 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
450 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
451 e5a0f69f 2018-08-18 stsp char *path;
452 7cbe629d 2018-08-04 stsp struct got_repository *repo;
453 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
454 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
455 e9424729 2018-08-04 stsp struct tog_blame blame;
456 6c4c42e0 2019-06-24 stsp int matched_line;
457 11b20872 2019-11-08 stsp struct tog_colors colors;
458 ad80ab7b 2018-08-04 stsp };
459 ad80ab7b 2018-08-04 stsp
460 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
461 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
462 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
465 ad80ab7b 2018-08-04 stsp int selected;
466 ad80ab7b 2018-08-04 stsp };
467 ad80ab7b 2018-08-04 stsp
468 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
469 ad80ab7b 2018-08-04 stsp
470 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
471 ad80ab7b 2018-08-04 stsp char *tree_label;
472 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
473 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
474 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
475 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
476 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
478 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
479 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
480 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
481 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
482 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
483 6458efa5 2020-11-24 stsp struct tog_colors colors;
484 6458efa5 2020-11-24 stsp };
485 6458efa5 2020-11-24 stsp
486 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
487 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
488 6458efa5 2020-11-24 stsp struct got_reference *ref;
489 6458efa5 2020-11-24 stsp int idx;
490 6458efa5 2020-11-24 stsp };
491 6458efa5 2020-11-24 stsp
492 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
493 6458efa5 2020-11-24 stsp
494 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
495 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
496 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
497 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
498 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
499 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
500 6458efa5 2020-11-24 stsp struct got_repository *repo;
501 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
502 bddb1296 2019-11-08 stsp struct tog_colors colors;
503 7cbe629d 2018-08-04 stsp };
504 7cbe629d 2018-08-04 stsp
505 669b5ffa 2018-10-07 stsp /*
506 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
507 669b5ffa 2018-10-07 stsp *
508 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
509 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
510 669b5ffa 2018-10-07 stsp * there is enough screen estate.
511 669b5ffa 2018-10-07 stsp *
512 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
513 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
514 669b5ffa 2018-10-07 stsp *
515 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
516 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
517 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
518 669b5ffa 2018-10-07 stsp *
519 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
520 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
521 669b5ffa 2018-10-07 stsp */
522 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
523 669b5ffa 2018-10-07 stsp
524 cc3c9aac 2018-08-01 stsp struct tog_view {
525 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
526 26ed57b2 2018-05-19 stsp WINDOW *window;
527 26ed57b2 2018-05-19 stsp PANEL *panel;
528 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
529 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
530 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
531 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
532 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
533 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
534 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
535 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
536 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
537 9970f7fc 2020-12-03 stsp int dying;
538 669b5ffa 2018-10-07 stsp struct tog_view *parent;
539 669b5ffa 2018-10-07 stsp struct tog_view *child;
540 5dc9f4bc 2018-08-04 stsp
541 e78dc838 2020-12-04 stsp /*
542 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
543 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
544 e78dc838 2020-12-04 stsp * between parent and child.
545 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
546 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
547 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
548 e78dc838 2020-12-04 stsp * situations.
549 e78dc838 2020-12-04 stsp */
550 e78dc838 2020-12-04 stsp int focus_child;
551 e78dc838 2020-12-04 stsp
552 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
553 5dc9f4bc 2018-08-04 stsp /* type-specific state */
554 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
555 5dc9f4bc 2018-08-04 stsp union {
556 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
557 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
558 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
559 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
560 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
561 5dc9f4bc 2018-08-04 stsp } state;
562 e5a0f69f 2018-08-18 stsp
563 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
564 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
565 e78dc838 2020-12-04 stsp struct tog_view *, int);
566 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
567 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
568 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
569 60493ae3 2019-06-20 stsp
570 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
571 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
572 c0c4acc8 2021-01-24 stsp int search_started;
573 60493ae3 2019-06-20 stsp int searching;
574 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
575 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
576 60493ae3 2019-06-20 stsp int search_next_done;
577 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
578 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
579 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
580 1803e47f 2019-06-22 stsp regex_t regex;
581 41605754 2020-11-12 stsp regmatch_t regmatch;
582 cc3c9aac 2018-08-01 stsp };
583 cd0acaa7 2018-05-20 stsp
584 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
585 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
586 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
587 78756c87 2020-11-24 stsp struct got_repository *);
588 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
593 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
594 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
595 e5a0f69f 2018-08-18 stsp
596 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
597 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
598 78756c87 2020-11-24 stsp const char *, const char *, int);
599 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
600 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
601 e78dc838 2020-12-04 stsp struct tog_view *, int);
602 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
603 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
604 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
605 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
606 e5a0f69f 2018-08-18 stsp
607 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
608 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
609 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
610 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
611 e78dc838 2020-12-04 stsp struct tog_view *, int);
612 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
613 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
614 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
615 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
616 e5a0f69f 2018-08-18 stsp
617 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
618 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
619 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
620 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
621 e78dc838 2020-12-04 stsp struct tog_view *, int);
622 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
623 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
624 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
625 6458efa5 2020-11-24 stsp
626 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
627 6458efa5 2020-11-24 stsp struct got_repository *);
628 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
629 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
630 e78dc838 2020-12-04 stsp struct tog_view *, int);
631 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
632 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
633 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
634 25791caa 2018-10-24 stsp
635 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
636 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
637 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
638 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
639 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
640 25791caa 2018-10-24 stsp
641 25791caa 2018-10-24 stsp static void
642 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
643 25791caa 2018-10-24 stsp {
644 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
645 83baff54 2019-08-12 stsp }
646 83baff54 2019-08-12 stsp
647 83baff54 2019-08-12 stsp static void
648 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
649 83baff54 2019-08-12 stsp {
650 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
651 25791caa 2018-10-24 stsp }
652 26ed57b2 2018-05-19 stsp
653 61266923 2020-01-14 stsp static void
654 61266923 2020-01-14 stsp tog_sigcont(int signo)
655 61266923 2020-01-14 stsp {
656 61266923 2020-01-14 stsp tog_sigcont_received = 1;
657 61266923 2020-01-14 stsp }
658 61266923 2020-01-14 stsp
659 296152d1 2022-05-31 thomas static void
660 296152d1 2022-05-31 thomas tog_sigint(int signo)
661 296152d1 2022-05-31 thomas {
662 296152d1 2022-05-31 thomas tog_sigint_received = 1;
663 296152d1 2022-05-31 thomas }
664 296152d1 2022-05-31 thomas
665 296152d1 2022-05-31 thomas static void
666 296152d1 2022-05-31 thomas tog_sigterm(int signo)
667 296152d1 2022-05-31 thomas {
668 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
669 296152d1 2022-05-31 thomas }
670 296152d1 2022-05-31 thomas
671 296152d1 2022-05-31 thomas static int
672 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
673 296152d1 2022-05-31 thomas {
674 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
675 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
676 296152d1 2022-05-31 thomas }
677 296152d1 2022-05-31 thomas
678 e5a0f69f 2018-08-18 stsp static const struct got_error *
679 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
680 ea5e7bb5 2018-08-01 stsp {
681 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
682 e5a0f69f 2018-08-18 stsp
683 669b5ffa 2018-10-07 stsp if (view->child) {
684 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
685 669b5ffa 2018-10-07 stsp view->child = NULL;
686 669b5ffa 2018-10-07 stsp }
687 e5a0f69f 2018-08-18 stsp if (view->close)
688 e5a0f69f 2018-08-18 stsp err = view->close(view);
689 ea5e7bb5 2018-08-01 stsp if (view->panel)
690 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
691 ea5e7bb5 2018-08-01 stsp if (view->window)
692 ea5e7bb5 2018-08-01 stsp delwin(view->window);
693 ea5e7bb5 2018-08-01 stsp free(view);
694 f2d749db 2022-07-12 thomas return err ? err : child_err;
695 ea5e7bb5 2018-08-01 stsp }
696 ea5e7bb5 2018-08-01 stsp
697 ea5e7bb5 2018-08-01 stsp static struct tog_view *
698 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
699 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
700 ea5e7bb5 2018-08-01 stsp {
701 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
702 ea5e7bb5 2018-08-01 stsp
703 ea5e7bb5 2018-08-01 stsp if (view == NULL)
704 ea5e7bb5 2018-08-01 stsp return NULL;
705 ea5e7bb5 2018-08-01 stsp
706 d6b05b5b 2018-08-04 stsp view->type = type;
707 f7d12f7e 2018-08-01 stsp view->lines = LINES;
708 f7d12f7e 2018-08-01 stsp view->cols = COLS;
709 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
710 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
711 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
712 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
713 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
714 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
715 96a765a8 2018-08-04 stsp view_close(view);
716 ea5e7bb5 2018-08-01 stsp return NULL;
717 ea5e7bb5 2018-08-01 stsp }
718 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
719 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
720 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
721 96a765a8 2018-08-04 stsp view_close(view);
722 ea5e7bb5 2018-08-01 stsp return NULL;
723 ea5e7bb5 2018-08-01 stsp }
724 ea5e7bb5 2018-08-01 stsp
725 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
726 ea5e7bb5 2018-08-01 stsp return view;
727 cdf1ee82 2018-08-01 stsp }
728 cdf1ee82 2018-08-01 stsp
729 0cf4efb1 2018-09-29 stsp static int
730 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
731 0cf4efb1 2018-09-29 stsp {
732 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
733 0cf4efb1 2018-09-29 stsp return 0;
734 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
735 5c60c32a 2018-10-18 stsp }
736 5c60c32a 2018-10-18 stsp
737 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
738 a5d43cac 2022-07-01 thomas static int
739 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
740 a5d43cac 2022-07-01 thomas {
741 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
742 a5d43cac 2022-07-01 thomas }
743 a5d43cac 2022-07-01 thomas
744 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
745 5c60c32a 2018-10-18 stsp
746 5c60c32a 2018-10-18 stsp static const struct got_error *
747 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
748 5c60c32a 2018-10-18 stsp {
749 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
750 5c60c32a 2018-10-18 stsp
751 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
752 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
753 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
754 53d2bdd3 2022-07-10 thomas else
755 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
756 a5d43cac 2022-07-01 thomas view->begin_x = 0;
757 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
758 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
759 53d2bdd3 2022-07-10 thomas view->cols > 119)
760 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
761 53d2bdd3 2022-07-10 thomas else
762 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
763 a5d43cac 2022-07-01 thomas view->begin_y = 0;
764 a5d43cac 2022-07-01 thomas }
765 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
766 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
767 5c60c32a 2018-10-18 stsp view->lines = LINES;
768 5c60c32a 2018-10-18 stsp view->cols = COLS;
769 5c60c32a 2018-10-18 stsp err = view_resize(view);
770 5c60c32a 2018-10-18 stsp if (err)
771 5c60c32a 2018-10-18 stsp return err;
772 5c60c32a 2018-10-18 stsp
773 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
774 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
775 a5d43cac 2022-07-01 thomas
776 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
777 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
778 5c60c32a 2018-10-18 stsp
779 5c60c32a 2018-10-18 stsp return NULL;
780 5c60c32a 2018-10-18 stsp }
781 5c60c32a 2018-10-18 stsp
782 5c60c32a 2018-10-18 stsp static const struct got_error *
783 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
784 5c60c32a 2018-10-18 stsp {
785 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
786 5c60c32a 2018-10-18 stsp
787 5c60c32a 2018-10-18 stsp view->begin_x = 0;
788 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
789 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
790 5c60c32a 2018-10-18 stsp view->ncols = COLS;
791 5c60c32a 2018-10-18 stsp view->lines = LINES;
792 5c60c32a 2018-10-18 stsp view->cols = COLS;
793 5c60c32a 2018-10-18 stsp err = view_resize(view);
794 5c60c32a 2018-10-18 stsp if (err)
795 5c60c32a 2018-10-18 stsp return err;
796 5c60c32a 2018-10-18 stsp
797 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
798 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
799 5c60c32a 2018-10-18 stsp
800 5c60c32a 2018-10-18 stsp return NULL;
801 0cf4efb1 2018-09-29 stsp }
802 0cf4efb1 2018-09-29 stsp
803 5c60c32a 2018-10-18 stsp static int
804 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
805 5c60c32a 2018-10-18 stsp {
806 5c60c32a 2018-10-18 stsp return view->parent == NULL;
807 5c60c32a 2018-10-18 stsp }
808 5c60c32a 2018-10-18 stsp
809 b65b3ea0 2022-06-23 thomas static int
810 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
811 b65b3ea0 2022-06-23 thomas {
812 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
813 e44940c3 2022-07-01 thomas }
814 e44940c3 2022-07-01 thomas
815 e44940c3 2022-07-01 thomas static int
816 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
817 e44940c3 2022-07-01 thomas {
818 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
819 b65b3ea0 2022-06-23 thomas }
820 b65b3ea0 2022-06-23 thomas
821 444d5325 2022-07-03 thomas static int
822 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
823 444d5325 2022-07-03 thomas {
824 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
825 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
826 444d5325 2022-07-03 thomas }
827 444d5325 2022-07-03 thomas
828 a5d43cac 2022-07-01 thomas static void
829 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
830 a5d43cac 2022-07-01 thomas {
831 a5d43cac 2022-07-01 thomas PANEL *panel;
832 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
833 b65b3ea0 2022-06-23 thomas
834 a5d43cac 2022-07-01 thomas if (view->parent)
835 a5d43cac 2022-07-01 thomas return view_border(view->parent);
836 a5d43cac 2022-07-01 thomas
837 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
838 a5d43cac 2022-07-01 thomas if (panel == NULL)
839 a5d43cac 2022-07-01 thomas return;
840 a5d43cac 2022-07-01 thomas
841 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
842 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
843 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
844 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
845 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
846 a5d43cac 2022-07-01 thomas else
847 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
848 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
849 a5d43cac 2022-07-01 thomas }
850 a5d43cac 2022-07-01 thomas
851 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
852 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
853 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
854 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
855 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
856 a5d43cac 2022-07-01 thomas
857 4d8c2215 2018-08-19 stsp static const struct got_error *
858 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
859 f7d12f7e 2018-08-01 stsp {
860 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
861 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
862 f7d12f7e 2018-08-01 stsp
863 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
864 a5d43cac 2022-07-01 thomas
865 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
866 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
867 0cf4efb1 2018-09-29 stsp else
868 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
869 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
870 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
871 0cf4efb1 2018-09-29 stsp else
872 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
873 6d0fee91 2018-08-01 stsp
874 4918811f 2022-07-01 thomas if (view->child) {
875 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
876 a5d43cac 2022-07-01 thomas
877 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
878 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
879 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
880 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
881 40236d76 2022-06-23 thomas ncols = COLS;
882 40236d76 2022-06-23 thomas
883 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
884 5c60c32a 2018-10-18 stsp if (view->child->focussed)
885 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
886 5c60c32a 2018-10-18 stsp else
887 5c60c32a 2018-10-18 stsp show_panel(view->panel);
888 5c60c32a 2018-10-18 stsp } else {
889 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
890 40236d76 2022-06-23 thomas
891 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
892 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
893 a5d43cac 2022-07-01 thomas }
894 a5d43cac 2022-07-01 thomas /*
895 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
896 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
897 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
898 a5d43cac 2022-07-01 thomas */
899 a5d43cac 2022-07-01 thomas if (hs) {
900 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
901 a5d43cac 2022-07-01 thomas if (err)
902 a5d43cac 2022-07-01 thomas return err;
903 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
904 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
905 a5d43cac 2022-07-01 thomas if (err)
906 a5d43cac 2022-07-01 thomas return err;
907 a5d43cac 2022-07-01 thomas }
908 a5d43cac 2022-07-01 thomas view_border(view);
909 a5d43cac 2022-07-01 thomas update_panels();
910 a5d43cac 2022-07-01 thomas doupdate();
911 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
912 a5d43cac 2022-07-01 thomas nlines = view->nlines;
913 a5d43cac 2022-07-01 thomas }
914 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
915 40236d76 2022-06-23 thomas ncols = COLS;
916 669b5ffa 2018-10-07 stsp
917 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
918 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
919 ea0bff04 2022-07-19 thomas if (err)
920 ea0bff04 2022-07-19 thomas return err;
921 ea0bff04 2022-07-19 thomas }
922 ea0bff04 2022-07-19 thomas
923 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
924 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
925 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
926 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
927 40236d76 2022-06-23 thomas wclear(view->window);
928 40236d76 2022-06-23 thomas
929 40236d76 2022-06-23 thomas view->nlines = nlines;
930 40236d76 2022-06-23 thomas view->ncols = ncols;
931 40236d76 2022-06-23 thomas view->lines = LINES;
932 40236d76 2022-06-23 thomas view->cols = COLS;
933 40236d76 2022-06-23 thomas
934 5c60c32a 2018-10-18 stsp return NULL;
935 ea0bff04 2022-07-19 thomas }
936 ea0bff04 2022-07-19 thomas
937 ea0bff04 2022-07-19 thomas static const struct got_error *
938 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
939 ea0bff04 2022-07-19 thomas {
940 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
941 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
942 3a0139e8 2022-07-22 thomas int n = 0;
943 ea0bff04 2022-07-19 thomas
944 3a0139e8 2022-07-22 thomas if (s->selected_entry)
945 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
946 3a0139e8 2022-07-22 thomas
947 ea0bff04 2022-07-19 thomas /*
948 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
949 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
950 ea0bff04 2022-07-19 thomas */
951 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
952 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
953 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
954 ea0bff04 2022-07-19 thomas }
955 ea0bff04 2022-07-19 thomas
956 ea0bff04 2022-07-19 thomas return err;
957 97cb21cd 2022-07-12 thomas }
958 97cb21cd 2022-07-12 thomas
959 97cb21cd 2022-07-12 thomas static void
960 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
961 97cb21cd 2022-07-12 thomas {
962 97cb21cd 2022-07-12 thomas if (n == 0)
963 97cb21cd 2022-07-12 thomas return;
964 97cb21cd 2022-07-12 thomas
965 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
966 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
967 97cb21cd 2022-07-12 thomas view->parent->offset += n;
968 97cb21cd 2022-07-12 thomas else
969 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
970 97cb21cd 2022-07-12 thomas } else if (view->offset) {
971 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
972 97cb21cd 2022-07-12 thomas view->offset -= n;
973 97cb21cd 2022-07-12 thomas else
974 97cb21cd 2022-07-12 thomas view->offset = 0;
975 97cb21cd 2022-07-12 thomas }
976 53d2bdd3 2022-07-10 thomas }
977 53d2bdd3 2022-07-10 thomas
978 53d2bdd3 2022-07-10 thomas static const struct got_error *
979 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
980 53d2bdd3 2022-07-10 thomas {
981 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
982 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
983 53d2bdd3 2022-07-10 thomas
984 53d2bdd3 2022-07-10 thomas if (view->parent)
985 53d2bdd3 2022-07-10 thomas v = view->parent;
986 53d2bdd3 2022-07-10 thomas else
987 53d2bdd3 2022-07-10 thomas v = view;
988 53d2bdd3 2022-07-10 thomas
989 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
990 53d2bdd3 2022-07-10 thomas return NULL;
991 53d2bdd3 2022-07-10 thomas
992 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
993 53d2bdd3 2022-07-10 thomas
994 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
995 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
996 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
997 53d2bdd3 2022-07-10 thomas if (view->parent)
998 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
999 53d2bdd3 2022-07-10 thomas else
1000 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1001 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1002 53d2bdd3 2022-07-10 thomas view->count = 0;
1003 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1004 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1005 53d2bdd3 2022-07-10 thomas view->count = 0;
1006 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1007 53d2bdd3 2022-07-10 thomas }
1008 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1009 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1010 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1011 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1012 53d2bdd3 2022-07-10 thomas if (err)
1013 53d2bdd3 2022-07-10 thomas return err;
1014 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1015 53d2bdd3 2022-07-10 thomas } else {
1016 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1017 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1018 53d2bdd3 2022-07-10 thomas if (view->parent)
1019 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1020 53d2bdd3 2022-07-10 thomas else
1021 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1022 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1023 53d2bdd3 2022-07-10 thomas view->count = 0;
1024 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1025 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1026 53d2bdd3 2022-07-10 thomas view->count = 0;
1027 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1028 53d2bdd3 2022-07-10 thomas }
1029 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1030 53d2bdd3 2022-07-10 thomas }
1031 53d2bdd3 2022-07-10 thomas
1032 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1033 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1034 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1035 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1036 53d2bdd3 2022-07-10 thomas
1037 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1038 53d2bdd3 2022-07-10 thomas if (err)
1039 53d2bdd3 2022-07-10 thomas return err;
1040 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1041 53d2bdd3 2022-07-10 thomas if (err)
1042 53d2bdd3 2022-07-10 thomas return err;
1043 53d2bdd3 2022-07-10 thomas
1044 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1045 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1046 53d2bdd3 2022-07-10 thomas if (err)
1047 53d2bdd3 2022-07-10 thomas return err;
1048 53d2bdd3 2022-07-10 thomas }
1049 53d2bdd3 2022-07-10 thomas
1050 3a0139e8 2022-07-22 thomas if (v->resize)
1051 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1052 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1053 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1054 53d2bdd3 2022-07-10 thomas
1055 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1056 53d2bdd3 2022-07-10 thomas
1057 53d2bdd3 2022-07-10 thomas return err;
1058 53d2bdd3 2022-07-10 thomas }
1059 53d2bdd3 2022-07-10 thomas
1060 53d2bdd3 2022-07-10 thomas static void
1061 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1062 53d2bdd3 2022-07-10 thomas {
1063 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1064 53d2bdd3 2022-07-10 thomas
1065 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1066 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1067 669b5ffa 2018-10-07 stsp }
1068 669b5ffa 2018-10-07 stsp
1069 669b5ffa 2018-10-07 stsp static const struct got_error *
1070 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1071 669b5ffa 2018-10-07 stsp {
1072 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1073 669b5ffa 2018-10-07 stsp
1074 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1075 669b5ffa 2018-10-07 stsp return NULL;
1076 669b5ffa 2018-10-07 stsp
1077 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1078 669b5ffa 2018-10-07 stsp view->child = NULL;
1079 669b5ffa 2018-10-07 stsp return err;
1080 669b5ffa 2018-10-07 stsp }
1081 669b5ffa 2018-10-07 stsp
1082 40236d76 2022-06-23 thomas static const struct got_error *
1083 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1084 669b5ffa 2018-10-07 stsp {
1085 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1086 53d2bdd3 2022-07-10 thomas
1087 669b5ffa 2018-10-07 stsp view->child = child;
1088 669b5ffa 2018-10-07 stsp child->parent = view;
1089 40236d76 2022-06-23 thomas
1090 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1091 53d2bdd3 2022-07-10 thomas if (err)
1092 53d2bdd3 2022-07-10 thomas return err;
1093 53d2bdd3 2022-07-10 thomas
1094 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1095 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1096 53d2bdd3 2022-07-10 thomas
1097 53d2bdd3 2022-07-10 thomas return err;
1098 bfddd0d9 2018-09-29 stsp }
1099 2a31b33b 2022-07-23 thomas
1100 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1101 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1102 2a31b33b 2022-07-23 thomas
1103 2a31b33b 2022-07-23 thomas static const struct got_error *
1104 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1105 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1106 2a31b33b 2022-07-23 thomas {
1107 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1108 2a31b33b 2022-07-23 thomas const struct got_error *err;
1109 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1110 2a31b33b 2022-07-23 thomas
1111 2a31b33b 2022-07-23 thomas *requested = NULL;
1112 2a31b33b 2022-07-23 thomas
1113 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view))
1114 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1115 bfddd0d9 2018-09-29 stsp
1116 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1117 2a31b33b 2022-07-23 thomas if (err)
1118 2a31b33b 2022-07-23 thomas return err;
1119 2a31b33b 2022-07-23 thomas
1120 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1121 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1122 2a31b33b 2022-07-23 thomas if (err)
1123 2a31b33b 2022-07-23 thomas return err;
1124 2a31b33b 2022-07-23 thomas }
1125 2a31b33b 2022-07-23 thomas
1126 2a31b33b 2022-07-23 thomas view->focussed = 0;
1127 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1128 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1129 2a31b33b 2022-07-23 thomas new_view->nlines = view->lines - y;
1130 2a31b33b 2022-07-23 thomas
1131 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view)) {
1132 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1133 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1134 2a31b33b 2022-07-23 thomas if (err)
1135 2a31b33b 2022-07-23 thomas return err;
1136 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1137 2a31b33b 2022-07-23 thomas if (err)
1138 2a31b33b 2022-07-23 thomas return err;
1139 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1140 2a31b33b 2022-07-23 thomas } else
1141 2a31b33b 2022-07-23 thomas *requested = new_view;
1142 2a31b33b 2022-07-23 thomas
1143 2a31b33b 2022-07-23 thomas return NULL;
1144 2a31b33b 2022-07-23 thomas }
1145 2a31b33b 2022-07-23 thomas
1146 34bc9ec9 2019-02-22 stsp static void
1147 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1148 25791caa 2018-10-24 stsp {
1149 25791caa 2018-10-24 stsp int cols, lines;
1150 25791caa 2018-10-24 stsp struct winsize size;
1151 25791caa 2018-10-24 stsp
1152 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1153 25791caa 2018-10-24 stsp cols = 80; /* Default */
1154 25791caa 2018-10-24 stsp lines = 24;
1155 25791caa 2018-10-24 stsp } else {
1156 25791caa 2018-10-24 stsp cols = size.ws_col;
1157 25791caa 2018-10-24 stsp lines = size.ws_row;
1158 25791caa 2018-10-24 stsp }
1159 25791caa 2018-10-24 stsp resize_term(lines, cols);
1160 2b49a8ae 2019-06-22 stsp }
1161 2b49a8ae 2019-06-22 stsp
1162 2b49a8ae 2019-06-22 stsp static const struct got_error *
1163 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1164 2b49a8ae 2019-06-22 stsp {
1165 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1166 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1167 2b49a8ae 2019-06-22 stsp char pattern[1024];
1168 2b49a8ae 2019-06-22 stsp int ret;
1169 c0c4acc8 2021-01-24 stsp
1170 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1171 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1172 c0c4acc8 2021-01-24 stsp view->searching = 0;
1173 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1174 c0c4acc8 2021-01-24 stsp }
1175 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1176 2b49a8ae 2019-06-22 stsp
1177 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1178 2b49a8ae 2019-06-22 stsp return NULL;
1179 2b49a8ae 2019-06-22 stsp
1180 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1181 a5d43cac 2022-07-01 thomas v = view->child;
1182 2b49a8ae 2019-06-22 stsp
1183 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1184 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1185 a5d43cac 2022-07-01 thomas
1186 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1187 2b49a8ae 2019-06-22 stsp nocbreak();
1188 2b49a8ae 2019-06-22 stsp echo();
1189 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1190 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1191 2b49a8ae 2019-06-22 stsp cbreak();
1192 2b49a8ae 2019-06-22 stsp noecho();
1193 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1194 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1195 2b49a8ae 2019-06-22 stsp return NULL;
1196 2b49a8ae 2019-06-22 stsp
1197 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1198 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1199 7c32bd05 2019-06-22 stsp if (err) {
1200 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1201 7c32bd05 2019-06-22 stsp return err;
1202 7c32bd05 2019-06-22 stsp }
1203 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1204 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1205 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1206 2b49a8ae 2019-06-22 stsp view->search_next(view);
1207 2b49a8ae 2019-06-22 stsp }
1208 2b49a8ae 2019-06-22 stsp
1209 2b49a8ae 2019-06-22 stsp return NULL;
1210 64486692 2022-07-07 thomas }
1211 64486692 2022-07-07 thomas
1212 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1213 64486692 2022-07-07 thomas static const struct got_error *
1214 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1215 64486692 2022-07-07 thomas {
1216 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1217 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1218 64486692 2022-07-07 thomas
1219 64486692 2022-07-07 thomas if (view->parent)
1220 64486692 2022-07-07 thomas v = view->parent;
1221 64486692 2022-07-07 thomas else
1222 64486692 2022-07-07 thomas v = view;
1223 64486692 2022-07-07 thomas
1224 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1225 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1226 ddbc4d37 2022-07-12 thomas else
1227 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1228 64486692 2022-07-07 thomas
1229 ddbc4d37 2022-07-12 thomas if (!v->child)
1230 ddbc4d37 2022-07-12 thomas return NULL;
1231 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1232 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1233 ddbc4d37 2022-07-12 thomas
1234 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1235 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1236 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1237 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1238 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1239 64486692 2022-07-07 thomas
1240 ddbc4d37 2022-07-12 thomas
1241 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1242 64486692 2022-07-07 thomas v->ncols = COLS;
1243 64486692 2022-07-07 thomas v->child->ncols = COLS;
1244 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1245 64486692 2022-07-07 thomas
1246 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1247 64486692 2022-07-07 thomas if (err)
1248 64486692 2022-07-07 thomas return err;
1249 64486692 2022-07-07 thomas }
1250 64486692 2022-07-07 thomas v->child->mode = v->mode;
1251 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1252 64486692 2022-07-07 thomas v->focus_child = 1;
1253 64486692 2022-07-07 thomas
1254 64486692 2022-07-07 thomas err = view_fullscreen(v);
1255 64486692 2022-07-07 thomas if (err)
1256 64486692 2022-07-07 thomas return err;
1257 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1258 64486692 2022-07-07 thomas if (err)
1259 64486692 2022-07-07 thomas return err;
1260 64486692 2022-07-07 thomas
1261 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1262 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1263 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1264 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1265 cf1fe301 2022-07-29 thomas if (err)
1266 cf1fe301 2022-07-29 thomas return err;
1267 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1268 cf1fe301 2022-07-29 thomas if (err)
1269 cf1fe301 2022-07-29 thomas return err;
1270 ddbc4d37 2022-07-12 thomas } else {
1271 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1272 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1273 64486692 2022-07-07 thomas }
1274 f4e6231a 2022-07-22 thomas if (v->resize)
1275 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1276 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1277 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1278 64486692 2022-07-07 thomas
1279 64486692 2022-07-07 thomas return err;
1280 0cf4efb1 2018-09-29 stsp }
1281 6d0fee91 2018-08-01 stsp
1282 07b0611c 2022-06-23 thomas /*
1283 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1284 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1285 07b0611c 2022-06-23 thomas */
1286 07b0611c 2022-06-23 thomas static int
1287 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1288 07b0611c 2022-06-23 thomas {
1289 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1290 a5d43cac 2022-07-01 thomas int x, n = 0;
1291 07b0611c 2022-06-23 thomas
1292 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1293 a5d43cac 2022-07-01 thomas v = view->child;
1294 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1295 a5d43cac 2022-07-01 thomas v = view->parent;
1296 a5d43cac 2022-07-01 thomas
1297 07b0611c 2022-06-23 thomas view->count = 0;
1298 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1299 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1300 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1301 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1302 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1303 07b0611c 2022-06-23 thomas
1304 07b0611c 2022-06-23 thomas do {
1305 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1306 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1307 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1308 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1309 a5d43cac 2022-07-01 thomas }
1310 a5d43cac 2022-07-01 thomas
1311 07b0611c 2022-06-23 thomas /*
1312 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1313 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1314 07b0611c 2022-06-23 thomas */
1315 07b0611c 2022-06-23 thomas if (n >= 9999999)
1316 07b0611c 2022-06-23 thomas n = 9999999;
1317 07b0611c 2022-06-23 thomas else
1318 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1319 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1320 07b0611c 2022-06-23 thomas
1321 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1322 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1323 07dd3ed3 2022-08-06 thomas n = 0;
1324 07dd3ed3 2022-08-06 thomas c = 0;
1325 07dd3ed3 2022-08-06 thomas }
1326 07dd3ed3 2022-08-06 thomas
1327 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1328 07b0611c 2022-06-23 thomas view->count = n;
1329 07b0611c 2022-06-23 thomas
1330 07b0611c 2022-06-23 thomas return c;
1331 07b0611c 2022-06-23 thomas }
1332 07b0611c 2022-06-23 thomas
1333 0cf4efb1 2018-09-29 stsp static const struct got_error *
1334 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1335 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1336 e5a0f69f 2018-08-18 stsp {
1337 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1338 669b5ffa 2018-10-07 stsp struct tog_view *v;
1339 1a76625f 2018-10-22 stsp int ch, errcode;
1340 e5a0f69f 2018-08-18 stsp
1341 e5a0f69f 2018-08-18 stsp *new = NULL;
1342 8f4ed634 2020-03-26 stsp
1343 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1344 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1345 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1346 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1347 07b0611c 2022-06-23 thomas view->count = 0;
1348 07b0611c 2022-06-23 thomas }
1349 e5a0f69f 2018-08-18 stsp
1350 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1351 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1352 82954512 2020-02-03 stsp if (errcode)
1353 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1354 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1355 3da8ef85 2021-09-21 stsp sched_yield();
1356 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1357 82954512 2020-02-03 stsp if (errcode)
1358 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1359 82954512 2020-02-03 stsp "pthread_mutex_lock");
1360 60493ae3 2019-06-20 stsp view->search_next(view);
1361 60493ae3 2019-06-20 stsp return NULL;
1362 60493ae3 2019-06-20 stsp }
1363 60493ae3 2019-06-20 stsp
1364 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1365 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1366 1a76625f 2018-10-22 stsp if (errcode)
1367 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1368 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1369 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1370 634cb454 2022-07-03 thomas cbreak();
1371 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1372 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1373 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1374 634cb454 2022-07-03 thomas view->count = 0;
1375 634cb454 2022-07-03 thomas else
1376 634cb454 2022-07-03 thomas ch = view->ch;
1377 634cb454 2022-07-03 thomas } else {
1378 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1379 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1380 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1381 07b0611c 2022-06-23 thomas }
1382 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1383 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1384 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1385 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1386 1a76625f 2018-10-22 stsp if (errcode)
1387 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1388 25791caa 2018-10-24 stsp
1389 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1390 25791caa 2018-10-24 stsp tog_resizeterm();
1391 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1392 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1393 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1394 25791caa 2018-10-24 stsp err = view_resize(v);
1395 25791caa 2018-10-24 stsp if (err)
1396 25791caa 2018-10-24 stsp return err;
1397 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1398 25791caa 2018-10-24 stsp if (err)
1399 25791caa 2018-10-24 stsp return err;
1400 cdfcfb03 2020-12-06 stsp if (v->child) {
1401 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1402 cdfcfb03 2020-12-06 stsp if (err)
1403 cdfcfb03 2020-12-06 stsp return err;
1404 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1405 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1406 cdfcfb03 2020-12-06 stsp if (err)
1407 cdfcfb03 2020-12-06 stsp return err;
1408 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1409 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1410 53d2bdd3 2022-07-10 thomas if (err)
1411 53d2bdd3 2022-07-10 thomas return err;
1412 53d2bdd3 2022-07-10 thomas }
1413 cdfcfb03 2020-12-06 stsp }
1414 25791caa 2018-10-24 stsp }
1415 25791caa 2018-10-24 stsp }
1416 25791caa 2018-10-24 stsp
1417 e5a0f69f 2018-08-18 stsp switch (ch) {
1418 1e37a5c2 2019-05-12 jcs case '\t':
1419 07b0611c 2022-06-23 thomas view->count = 0;
1420 1e37a5c2 2019-05-12 jcs if (view->child) {
1421 e78dc838 2020-12-04 stsp view->focussed = 0;
1422 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1423 e78dc838 2020-12-04 stsp view->focus_child = 1;
1424 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1425 e78dc838 2020-12-04 stsp view->focussed = 0;
1426 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1427 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1428 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1429 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1430 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1431 3a0139e8 2022-07-22 thomas 0);
1432 a5d43cac 2022-07-01 thomas if (err)
1433 a5d43cac 2022-07-01 thomas return err;
1434 a5d43cac 2022-07-01 thomas }
1435 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1436 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1437 a5d43cac 2022-07-01 thomas if (err)
1438 a5d43cac 2022-07-01 thomas return err;
1439 a5d43cac 2022-07-01 thomas }
1440 1e37a5c2 2019-05-12 jcs }
1441 1e37a5c2 2019-05-12 jcs break;
1442 1e37a5c2 2019-05-12 jcs case 'q':
1443 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1444 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1445 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1446 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1447 a5d43cac 2022-07-01 thomas if (err)
1448 a5d43cac 2022-07-01 thomas break;
1449 a5d43cac 2022-07-01 thomas }
1450 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1451 a5d43cac 2022-07-01 thomas }
1452 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1453 9970f7fc 2020-12-03 stsp view->dying = 1;
1454 1e37a5c2 2019-05-12 jcs break;
1455 1e37a5c2 2019-05-12 jcs case 'Q':
1456 1e37a5c2 2019-05-12 jcs *done = 1;
1457 1e37a5c2 2019-05-12 jcs break;
1458 1c5e5faa 2022-06-23 thomas case 'F':
1459 07b0611c 2022-06-23 thomas view->count = 0;
1460 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1461 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1462 1e37a5c2 2019-05-12 jcs break;
1463 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1464 e78dc838 2020-12-04 stsp view->focussed = 0;
1465 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1466 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1467 53d2bdd3 2022-07-10 thomas } else {
1468 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1469 53d2bdd3 2022-07-10 thomas if (!err)
1470 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1471 53d2bdd3 2022-07-10 thomas }
1472 1e37a5c2 2019-05-12 jcs if (err)
1473 1e37a5c2 2019-05-12 jcs break;
1474 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1475 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1476 1e37a5c2 2019-05-12 jcs } else {
1477 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1478 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1479 e78dc838 2020-12-04 stsp view->focussed = 1;
1480 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1481 1e37a5c2 2019-05-12 jcs } else {
1482 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1483 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1484 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1485 53d2bdd3 2022-07-10 thomas if (!err)
1486 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1487 669b5ffa 2018-10-07 stsp }
1488 1e37a5c2 2019-05-12 jcs if (err)
1489 1e37a5c2 2019-05-12 jcs break;
1490 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1491 a5d43cac 2022-07-01 thomas }
1492 a5d43cac 2022-07-01 thomas if (err)
1493 a5d43cac 2022-07-01 thomas break;
1494 3a0139e8 2022-07-22 thomas if (view->resize) {
1495 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1496 a5d43cac 2022-07-01 thomas if (err)
1497 a5d43cac 2022-07-01 thomas break;
1498 1e37a5c2 2019-05-12 jcs }
1499 a5d43cac 2022-07-01 thomas if (view->parent)
1500 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1501 a5d43cac 2022-07-01 thomas if (!err)
1502 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1503 1e37a5c2 2019-05-12 jcs break;
1504 64486692 2022-07-07 thomas case 'S':
1505 53d2bdd3 2022-07-10 thomas view->count = 0;
1506 64486692 2022-07-07 thomas err = switch_split(view);
1507 53d2bdd3 2022-07-10 thomas break;
1508 53d2bdd3 2022-07-10 thomas case '-':
1509 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1510 64486692 2022-07-07 thomas break;
1511 53d2bdd3 2022-07-10 thomas case '+':
1512 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1513 53d2bdd3 2022-07-10 thomas break;
1514 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1515 60493ae3 2019-06-20 stsp break;
1516 60493ae3 2019-06-20 stsp case '/':
1517 07b0611c 2022-06-23 thomas view->count = 0;
1518 60493ae3 2019-06-20 stsp if (view->search_start)
1519 2b49a8ae 2019-06-22 stsp view_search_start(view);
1520 60493ae3 2019-06-20 stsp else
1521 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1522 1e37a5c2 2019-05-12 jcs break;
1523 b1bf1435 2019-06-21 stsp case 'N':
1524 60493ae3 2019-06-20 stsp case 'n':
1525 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1526 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1527 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1528 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1529 60493ae3 2019-06-20 stsp view->search_next(view);
1530 60493ae3 2019-06-20 stsp } else
1531 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1532 adf4c9e0 2022-07-03 thomas break;
1533 adf4c9e0 2022-07-03 thomas case 'A':
1534 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1535 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1536 adf4c9e0 2022-07-03 thomas else
1537 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1538 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1539 adf4c9e0 2022-07-03 thomas if (v->reset) {
1540 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1541 adf4c9e0 2022-07-03 thomas if (err)
1542 adf4c9e0 2022-07-03 thomas return err;
1543 adf4c9e0 2022-07-03 thomas }
1544 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1545 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1546 adf4c9e0 2022-07-03 thomas if (err)
1547 adf4c9e0 2022-07-03 thomas return err;
1548 adf4c9e0 2022-07-03 thomas }
1549 adf4c9e0 2022-07-03 thomas }
1550 60493ae3 2019-06-20 stsp break;
1551 1e37a5c2 2019-05-12 jcs default:
1552 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1553 1e37a5c2 2019-05-12 jcs break;
1554 e5a0f69f 2018-08-18 stsp }
1555 e5a0f69f 2018-08-18 stsp
1556 e5a0f69f 2018-08-18 stsp return err;
1557 bcbd79e2 2018-08-19 stsp }
1558 bcbd79e2 2018-08-19 stsp
1559 ef20f542 2022-06-26 thomas static int
1560 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1561 a3404814 2018-09-02 stsp {
1562 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1563 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1564 669b5ffa 2018-10-07 stsp return 0;
1565 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1566 669b5ffa 2018-10-07 stsp return 0;
1567 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1568 a3404814 2018-09-02 stsp return 0;
1569 a3404814 2018-09-02 stsp
1570 669b5ffa 2018-10-07 stsp return view->focussed;
1571 a3404814 2018-09-02 stsp }
1572 a3404814 2018-09-02 stsp
1573 bcbd79e2 2018-08-19 stsp static const struct got_error *
1574 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1575 e5a0f69f 2018-08-18 stsp {
1576 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1577 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1578 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1579 64486692 2022-07-07 thomas char *mode;
1580 fd823528 2018-10-22 stsp int fast_refresh = 10;
1581 1a76625f 2018-10-22 stsp int done = 0, errcode;
1582 e5a0f69f 2018-08-18 stsp
1583 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1584 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1585 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1586 64486692 2022-07-07 thomas else
1587 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1588 64486692 2022-07-07 thomas
1589 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1590 1a76625f 2018-10-22 stsp if (errcode)
1591 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1592 1a76625f 2018-10-22 stsp
1593 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1594 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1595 e5a0f69f 2018-08-18 stsp
1596 1004088d 2018-09-29 stsp view->focussed = 1;
1597 878940b7 2018-09-29 stsp err = view->show(view);
1598 0cf4efb1 2018-09-29 stsp if (err)
1599 0cf4efb1 2018-09-29 stsp return err;
1600 0cf4efb1 2018-09-29 stsp update_panels();
1601 0cf4efb1 2018-09-29 stsp doupdate();
1602 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1603 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1604 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1605 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1606 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1607 fd823528 2018-10-22 stsp
1608 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1609 e5a0f69f 2018-08-18 stsp if (err)
1610 e5a0f69f 2018-08-18 stsp break;
1611 9970f7fc 2020-12-03 stsp if (view->dying) {
1612 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1613 669b5ffa 2018-10-07 stsp
1614 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1615 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1616 9970f7fc 2020-12-03 stsp entry);
1617 e78dc838 2020-12-04 stsp else if (view->parent)
1618 669b5ffa 2018-10-07 stsp prev = view->parent;
1619 669b5ffa 2018-10-07 stsp
1620 e78dc838 2020-12-04 stsp if (view->parent) {
1621 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1622 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1623 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1624 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1625 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1626 40236d76 2022-06-23 thomas if (err)
1627 40236d76 2022-06-23 thomas break;
1628 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1629 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1630 e78dc838 2020-12-04 stsp } else
1631 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1632 669b5ffa 2018-10-07 stsp
1633 9970f7fc 2020-12-03 stsp err = view_close(view);
1634 fb59748f 2020-12-05 stsp if (err)
1635 e5a0f69f 2018-08-18 stsp goto done;
1636 669b5ffa 2018-10-07 stsp
1637 e78dc838 2020-12-04 stsp view = NULL;
1638 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1639 e78dc838 2020-12-04 stsp if (v->focussed)
1640 e78dc838 2020-12-04 stsp break;
1641 0cf4efb1 2018-09-29 stsp }
1642 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1643 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1644 e78dc838 2020-12-04 stsp if (prev)
1645 e78dc838 2020-12-04 stsp view = prev;
1646 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1647 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1648 e78dc838 2020-12-04 stsp tog_view_list_head);
1649 e78dc838 2020-12-04 stsp }
1650 e78dc838 2020-12-04 stsp if (view) {
1651 e78dc838 2020-12-04 stsp if (view->focus_child) {
1652 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1653 e78dc838 2020-12-04 stsp view = view->child;
1654 e78dc838 2020-12-04 stsp } else
1655 e78dc838 2020-12-04 stsp view->focussed = 1;
1656 e78dc838 2020-12-04 stsp }
1657 e78dc838 2020-12-04 stsp }
1658 e5a0f69f 2018-08-18 stsp }
1659 bcbd79e2 2018-08-19 stsp if (new_view) {
1660 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1661 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1662 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1663 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1664 86c66b02 2018-10-18 stsp continue;
1665 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1666 86c66b02 2018-10-18 stsp err = view_close(v);
1667 86c66b02 2018-10-18 stsp if (err)
1668 86c66b02 2018-10-18 stsp goto done;
1669 86c66b02 2018-10-18 stsp break;
1670 86c66b02 2018-10-18 stsp }
1671 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1672 fed7eaa8 2018-10-24 stsp view = new_view;
1673 7cd52833 2022-06-23 thomas }
1674 669b5ffa 2018-10-07 stsp if (view) {
1675 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1676 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1677 e78dc838 2020-12-04 stsp view = view->child;
1678 e78dc838 2020-12-04 stsp } else {
1679 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1680 e78dc838 2020-12-04 stsp view = view->parent;
1681 1a76625f 2018-10-22 stsp }
1682 e78dc838 2020-12-04 stsp show_panel(view->panel);
1683 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1684 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1685 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1686 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1687 669b5ffa 2018-10-07 stsp if (err)
1688 1a76625f 2018-10-22 stsp goto done;
1689 669b5ffa 2018-10-07 stsp }
1690 669b5ffa 2018-10-07 stsp err = view->show(view);
1691 0cf4efb1 2018-09-29 stsp if (err)
1692 1a76625f 2018-10-22 stsp goto done;
1693 669b5ffa 2018-10-07 stsp if (view->child) {
1694 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1695 669b5ffa 2018-10-07 stsp if (err)
1696 1a76625f 2018-10-22 stsp goto done;
1697 669b5ffa 2018-10-07 stsp }
1698 1a76625f 2018-10-22 stsp update_panels();
1699 1a76625f 2018-10-22 stsp doupdate();
1700 0cf4efb1 2018-09-29 stsp }
1701 e5a0f69f 2018-08-18 stsp }
1702 e5a0f69f 2018-08-18 stsp done:
1703 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1704 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1705 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1706 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1707 f2d749db 2022-07-12 thomas close_err = view_close(view);
1708 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1709 f2d749db 2022-07-12 thomas err = close_err;
1710 e5a0f69f 2018-08-18 stsp }
1711 1a76625f 2018-10-22 stsp
1712 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1713 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1714 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1715 1a76625f 2018-10-22 stsp
1716 e5a0f69f 2018-08-18 stsp return err;
1717 ea5e7bb5 2018-08-01 stsp }
1718 ea5e7bb5 2018-08-01 stsp
1719 4ed7e80c 2018-05-20 stsp __dead static void
1720 9f7d7167 2018-04-29 stsp usage_log(void)
1721 9f7d7167 2018-04-29 stsp {
1722 80ddbec8 2018-04-29 stsp endwin();
1723 c70c5802 2018-08-01 stsp fprintf(stderr,
1724 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1725 9f7d7167 2018-04-29 stsp getprogname());
1726 9f7d7167 2018-04-29 stsp exit(1);
1727 80ddbec8 2018-04-29 stsp }
1728 80ddbec8 2018-04-29 stsp
1729 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1730 80ddbec8 2018-04-29 stsp static const struct got_error *
1731 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1732 963b370f 2018-05-20 stsp {
1733 00dfcb92 2018-06-11 stsp char *vis = NULL;
1734 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1735 963b370f 2018-05-20 stsp
1736 963b370f 2018-05-20 stsp *ws = NULL;
1737 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1738 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1739 00dfcb92 2018-06-11 stsp int vislen;
1740 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1741 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1742 00dfcb92 2018-06-11 stsp
1743 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1744 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1745 00dfcb92 2018-06-11 stsp if (err)
1746 00dfcb92 2018-06-11 stsp return err;
1747 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1748 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1749 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1750 a7f50699 2018-06-11 stsp goto done;
1751 a7f50699 2018-06-11 stsp }
1752 00dfcb92 2018-06-11 stsp }
1753 963b370f 2018-05-20 stsp
1754 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1755 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1756 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1757 a7f50699 2018-06-11 stsp goto done;
1758 a7f50699 2018-06-11 stsp }
1759 963b370f 2018-05-20 stsp
1760 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1761 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1762 a7f50699 2018-06-11 stsp done:
1763 00dfcb92 2018-06-11 stsp free(vis);
1764 963b370f 2018-05-20 stsp if (err) {
1765 963b370f 2018-05-20 stsp free(*ws);
1766 963b370f 2018-05-20 stsp *ws = NULL;
1767 963b370f 2018-05-20 stsp *wlen = 0;
1768 963b370f 2018-05-20 stsp }
1769 963b370f 2018-05-20 stsp return err;
1770 05171be4 2022-06-23 thomas }
1771 05171be4 2022-06-23 thomas
1772 05171be4 2022-06-23 thomas static const struct got_error *
1773 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1774 05171be4 2022-06-23 thomas {
1775 05171be4 2022-06-23 thomas char *dst;
1776 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1777 05171be4 2022-06-23 thomas
1778 05171be4 2022-06-23 thomas *ptr = NULL;
1779 05171be4 2022-06-23 thomas n = len = strlen(src);
1780 83316834 2022-06-23 thomas dst = malloc(n + 1);
1781 05171be4 2022-06-23 thomas if (dst == NULL)
1782 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1783 05171be4 2022-06-23 thomas
1784 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1785 05171be4 2022-06-23 thomas const char c = src[idx];
1786 05171be4 2022-06-23 thomas
1787 05171be4 2022-06-23 thomas if (c == '\t') {
1788 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1789 b235ad5d 2022-06-23 thomas char *p;
1790 b235ad5d 2022-06-23 thomas
1791 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1792 83316834 2022-06-23 thomas if (p == NULL) {
1793 83316834 2022-06-23 thomas free(dst);
1794 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1795 83316834 2022-06-23 thomas
1796 83316834 2022-06-23 thomas }
1797 83316834 2022-06-23 thomas dst = p;
1798 05171be4 2022-06-23 thomas n += nb;
1799 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1800 05171be4 2022-06-23 thomas sz += nb;
1801 05171be4 2022-06-23 thomas } else
1802 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1803 05171be4 2022-06-23 thomas ++idx;
1804 05171be4 2022-06-23 thomas }
1805 05171be4 2022-06-23 thomas
1806 05171be4 2022-06-23 thomas dst[sz] = '\0';
1807 05171be4 2022-06-23 thomas *ptr = dst;
1808 05171be4 2022-06-23 thomas return NULL;
1809 963b370f 2018-05-20 stsp }
1810 963b370f 2018-05-20 stsp
1811 8d208d34 2022-06-23 thomas /*
1812 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1813 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1814 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1815 8d208d34 2022-06-23 thomas * *rcol.
1816 f91a2b48 2022-06-23 thomas */
1817 8d208d34 2022-06-23 thomas static int
1818 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1819 8d208d34 2022-06-23 thomas {
1820 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1821 f91a2b48 2022-06-23 thomas
1822 8d208d34 2022-06-23 thomas if (n == 0) {
1823 8d208d34 2022-06-23 thomas *rcol = cols;
1824 8d208d34 2022-06-23 thomas return off;
1825 8d208d34 2022-06-23 thomas }
1826 f91a2b48 2022-06-23 thomas
1827 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1828 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1829 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1830 8d208d34 2022-06-23 thomas else
1831 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1832 f91a2b48 2022-06-23 thomas
1833 8d208d34 2022-06-23 thomas if (width == -1) {
1834 8d208d34 2022-06-23 thomas width = 1;
1835 8d208d34 2022-06-23 thomas wline[i] = L'.';
1836 f91a2b48 2022-06-23 thomas }
1837 f91a2b48 2022-06-23 thomas
1838 8d208d34 2022-06-23 thomas if (cols + width > n)
1839 8d208d34 2022-06-23 thomas break;
1840 8d208d34 2022-06-23 thomas cols += width;
1841 f91a2b48 2022-06-23 thomas }
1842 f91a2b48 2022-06-23 thomas
1843 8d208d34 2022-06-23 thomas *rcol = cols;
1844 8d208d34 2022-06-23 thomas return i;
1845 f91a2b48 2022-06-23 thomas }
1846 f91a2b48 2022-06-23 thomas
1847 f91a2b48 2022-06-23 thomas /*
1848 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1849 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1850 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1851 f91a2b48 2022-06-23 thomas */
1852 f91a2b48 2022-06-23 thomas static const struct got_error *
1853 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1854 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1855 f91a2b48 2022-06-23 thomas {
1856 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1857 8d208d34 2022-06-23 thomas int cols;
1858 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1859 05171be4 2022-06-23 thomas char *exstr = NULL;
1860 963b370f 2018-05-20 stsp size_t wlen;
1861 8d208d34 2022-06-23 thomas int i, scrollx;
1862 963b370f 2018-05-20 stsp
1863 963b370f 2018-05-20 stsp *wlinep = NULL;
1864 b700b5d6 2018-07-10 stsp *widthp = 0;
1865 963b370f 2018-05-20 stsp
1866 05171be4 2022-06-23 thomas if (expand) {
1867 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1868 05171be4 2022-06-23 thomas if (err)
1869 05171be4 2022-06-23 thomas return err;
1870 05171be4 2022-06-23 thomas }
1871 05171be4 2022-06-23 thomas
1872 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1873 05171be4 2022-06-23 thomas free(exstr);
1874 963b370f 2018-05-20 stsp if (err)
1875 963b370f 2018-05-20 stsp return err;
1876 963b370f 2018-05-20 stsp
1877 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1878 f91a2b48 2022-06-23 thomas
1879 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1880 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1881 3f670bfb 2020-12-10 stsp wlen--;
1882 3f670bfb 2020-12-10 stsp }
1883 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1884 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1885 3f670bfb 2020-12-10 stsp wlen--;
1886 3f670bfb 2020-12-10 stsp }
1887 3f670bfb 2020-12-10 stsp
1888 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1889 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1890 27a741e5 2019-09-11 stsp
1891 b700b5d6 2018-07-10 stsp if (widthp)
1892 b700b5d6 2018-07-10 stsp *widthp = cols;
1893 f91a2b48 2022-06-23 thomas if (scrollxp)
1894 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1895 963b370f 2018-05-20 stsp if (err)
1896 963b370f 2018-05-20 stsp free(wline);
1897 963b370f 2018-05-20 stsp else
1898 963b370f 2018-05-20 stsp *wlinep = wline;
1899 963b370f 2018-05-20 stsp return err;
1900 963b370f 2018-05-20 stsp }
1901 963b370f 2018-05-20 stsp
1902 8b473291 2019-02-21 stsp static const struct got_error*
1903 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1904 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1905 8b473291 2019-02-21 stsp {
1906 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1907 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1908 8b473291 2019-02-21 stsp char *s;
1909 8b473291 2019-02-21 stsp const char *name;
1910 8b473291 2019-02-21 stsp
1911 8b473291 2019-02-21 stsp *refs_str = NULL;
1912 8b473291 2019-02-21 stsp
1913 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1914 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1915 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1916 52b5abe1 2019-08-13 stsp int cmp;
1917 52b5abe1 2019-08-13 stsp
1918 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1919 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1920 8b473291 2019-02-21 stsp continue;
1921 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1922 8b473291 2019-02-21 stsp name += 5;
1923 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1924 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1925 7143d404 2019-03-12 stsp continue;
1926 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1927 8b473291 2019-02-21 stsp name += 6;
1928 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1929 8b473291 2019-02-21 stsp name += 8;
1930 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1931 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1932 79cc719f 2020-04-24 stsp continue;
1933 79cc719f 2020-04-24 stsp }
1934 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1935 48cae60d 2020-09-22 stsp if (err)
1936 48cae60d 2020-09-22 stsp break;
1937 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1938 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1939 5d844a1e 2019-08-13 stsp if (err) {
1940 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1941 48cae60d 2020-09-22 stsp free(ref_id);
1942 5d844a1e 2019-08-13 stsp break;
1943 48cae60d 2020-09-22 stsp }
1944 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1945 5d844a1e 2019-08-13 stsp err = NULL;
1946 5d844a1e 2019-08-13 stsp tag = NULL;
1947 5d844a1e 2019-08-13 stsp }
1948 52b5abe1 2019-08-13 stsp }
1949 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1950 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1951 48cae60d 2020-09-22 stsp free(ref_id);
1952 52b5abe1 2019-08-13 stsp if (tag)
1953 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1954 52b5abe1 2019-08-13 stsp if (cmp != 0)
1955 52b5abe1 2019-08-13 stsp continue;
1956 8b473291 2019-02-21 stsp s = *refs_str;
1957 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1958 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1959 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1960 8b473291 2019-02-21 stsp free(s);
1961 8b473291 2019-02-21 stsp *refs_str = NULL;
1962 8b473291 2019-02-21 stsp break;
1963 8b473291 2019-02-21 stsp }
1964 8b473291 2019-02-21 stsp free(s);
1965 8b473291 2019-02-21 stsp }
1966 8b473291 2019-02-21 stsp
1967 8b473291 2019-02-21 stsp return err;
1968 8b473291 2019-02-21 stsp }
1969 8b473291 2019-02-21 stsp
1970 963b370f 2018-05-20 stsp static const struct got_error *
1971 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1972 27a741e5 2019-09-11 stsp int col_tab_align)
1973 5813d178 2019-03-09 stsp {
1974 e6b8b890 2020-12-29 naddy char *smallerthan;
1975 5813d178 2019-03-09 stsp
1976 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1977 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1978 5813d178 2019-03-09 stsp author = smallerthan + 1;
1979 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1980 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1981 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1982 5813d178 2019-03-09 stsp }
1983 5813d178 2019-03-09 stsp
1984 5813d178 2019-03-09 stsp static const struct got_error *
1985 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1986 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1987 8fdc79fe 2020-12-01 naddy int author_display_cols)
1988 80ddbec8 2018-04-29 stsp {
1989 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1990 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1991 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1992 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1993 5813d178 2019-03-09 stsp char *author = NULL;
1994 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1995 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1996 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1997 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1998 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1999 ccb26ccd 2018-11-05 stsp struct tm tm;
2000 45d799e2 2018-12-23 stsp time_t committer_time;
2001 11b20872 2019-11-08 stsp struct tog_color *tc;
2002 80ddbec8 2018-04-29 stsp
2003 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2004 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2005 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2006 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2007 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2008 b39d25c7 2018-07-10 stsp
2009 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2010 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2011 b39d25c7 2018-07-10 stsp else
2012 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2013 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2014 11b20872 2019-11-08 stsp if (tc)
2015 11b20872 2019-11-08 stsp wattr_on(view->window,
2016 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2017 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2018 11b20872 2019-11-08 stsp if (tc)
2019 11b20872 2019-11-08 stsp wattr_off(view->window,
2020 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2021 27a741e5 2019-09-11 stsp col = limit;
2022 b39d25c7 2018-07-10 stsp if (col > avail)
2023 b39d25c7 2018-07-10 stsp goto done;
2024 6570a66d 2019-11-08 stsp
2025 6570a66d 2019-11-08 stsp if (avail >= 120) {
2026 6570a66d 2019-11-08 stsp char *id_str;
2027 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2028 6570a66d 2019-11-08 stsp if (err)
2029 6570a66d 2019-11-08 stsp goto done;
2030 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2031 11b20872 2019-11-08 stsp if (tc)
2032 11b20872 2019-11-08 stsp wattr_on(view->window,
2033 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2034 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2035 11b20872 2019-11-08 stsp if (tc)
2036 11b20872 2019-11-08 stsp wattr_off(view->window,
2037 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2038 6570a66d 2019-11-08 stsp free(id_str);
2039 6570a66d 2019-11-08 stsp col += 9;
2040 6570a66d 2019-11-08 stsp if (col > avail)
2041 6570a66d 2019-11-08 stsp goto done;
2042 6570a66d 2019-11-08 stsp }
2043 b39d25c7 2018-07-10 stsp
2044 f69c5a46 2022-07-19 thomas if (s->use_committer)
2045 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2046 f69c5a46 2022-07-19 thomas else
2047 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2048 5813d178 2019-03-09 stsp if (author == NULL) {
2049 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2050 80ddbec8 2018-04-29 stsp goto done;
2051 80ddbec8 2018-04-29 stsp }
2052 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2053 bb737323 2018-05-20 stsp if (err)
2054 bb737323 2018-05-20 stsp goto done;
2055 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2056 11b20872 2019-11-08 stsp if (tc)
2057 11b20872 2019-11-08 stsp wattr_on(view->window,
2058 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2059 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2060 bb737323 2018-05-20 stsp col += author_width;
2061 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2062 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2063 bb737323 2018-05-20 stsp col++;
2064 bb737323 2018-05-20 stsp author_width++;
2065 bb737323 2018-05-20 stsp }
2066 f31d6c3b 2022-09-09 thomas if (tc)
2067 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2068 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2069 9c2eaf34 2018-05-20 stsp if (col > avail)
2070 9c2eaf34 2018-05-20 stsp goto done;
2071 80ddbec8 2018-04-29 stsp
2072 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2073 5943eee2 2019-08-13 stsp if (err)
2074 6d9fbc00 2018-04-29 stsp goto done;
2075 bb737323 2018-05-20 stsp logmsg = logmsg0;
2076 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2077 bb737323 2018-05-20 stsp logmsg++;
2078 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2079 bb737323 2018-05-20 stsp if (newline)
2080 bb737323 2018-05-20 stsp *newline = '\0';
2081 f91a2b48 2022-06-23 thomas limit = avail - col;
2082 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2083 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2084 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2085 f91a2b48 2022-06-23 thomas limit, col, 1);
2086 331b1a16 2022-06-23 thomas if (err)
2087 331b1a16 2022-06-23 thomas goto done;
2088 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2089 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2090 27a741e5 2019-09-11 stsp while (col < avail) {
2091 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2092 bb737323 2018-05-20 stsp col++;
2093 881b2d3e 2018-04-30 stsp }
2094 80ddbec8 2018-04-29 stsp done:
2095 80ddbec8 2018-04-29 stsp free(logmsg0);
2096 bb737323 2018-05-20 stsp free(wlogmsg);
2097 5813d178 2019-03-09 stsp free(author);
2098 bb737323 2018-05-20 stsp free(wauthor);
2099 80ddbec8 2018-04-29 stsp free(line);
2100 80ddbec8 2018-04-29 stsp return err;
2101 80ddbec8 2018-04-29 stsp }
2102 26ed57b2 2018-05-19 stsp
2103 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2104 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2105 899d86c2 2018-05-10 stsp struct got_object_id *id)
2106 80ddbec8 2018-04-29 stsp {
2107 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2108 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2109 80ddbec8 2018-04-29 stsp
2110 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2111 80ddbec8 2018-04-29 stsp if (entry == NULL)
2112 899d86c2 2018-05-10 stsp return NULL;
2113 99db9666 2018-05-07 stsp
2114 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2115 d68b9737 2022-09-05 thomas if (dup == NULL) {
2116 d68b9737 2022-09-05 thomas free(entry);
2117 d68b9737 2022-09-05 thomas return NULL;
2118 d68b9737 2022-09-05 thomas }
2119 d68b9737 2022-09-05 thomas
2120 d68b9737 2022-09-05 thomas entry->id = dup;
2121 99db9666 2018-05-07 stsp entry->commit = commit;
2122 899d86c2 2018-05-10 stsp return entry;
2123 99db9666 2018-05-07 stsp }
2124 80ddbec8 2018-04-29 stsp
2125 99db9666 2018-05-07 stsp static void
2126 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2127 99db9666 2018-05-07 stsp {
2128 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2129 99db9666 2018-05-07 stsp
2130 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2131 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2132 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2133 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2134 d68b9737 2022-09-05 thomas free(entry->id);
2135 99db9666 2018-05-07 stsp free(entry);
2136 99db9666 2018-05-07 stsp }
2137 99db9666 2018-05-07 stsp
2138 99db9666 2018-05-07 stsp static void
2139 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2140 99db9666 2018-05-07 stsp {
2141 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2142 99db9666 2018-05-07 stsp pop_commit(commits);
2143 c4972b91 2018-05-07 stsp }
2144 c4972b91 2018-05-07 stsp
2145 c4972b91 2018-05-07 stsp static const struct got_error *
2146 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2147 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2148 13add988 2019-10-15 stsp {
2149 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2150 13add988 2019-10-15 stsp regmatch_t regmatch;
2151 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2152 13add988 2019-10-15 stsp
2153 13add988 2019-10-15 stsp *have_match = 0;
2154 13add988 2019-10-15 stsp
2155 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2156 13add988 2019-10-15 stsp if (err)
2157 13add988 2019-10-15 stsp return err;
2158 13add988 2019-10-15 stsp
2159 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2160 13add988 2019-10-15 stsp if (err)
2161 13add988 2019-10-15 stsp goto done;
2162 13add988 2019-10-15 stsp
2163 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2164 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2165 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2166 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2167 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2168 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2169 13add988 2019-10-15 stsp *have_match = 1;
2170 13add988 2019-10-15 stsp done:
2171 13add988 2019-10-15 stsp free(id_str);
2172 13add988 2019-10-15 stsp free(logmsg);
2173 13add988 2019-10-15 stsp return err;
2174 13add988 2019-10-15 stsp }
2175 13add988 2019-10-15 stsp
2176 13add988 2019-10-15 stsp static const struct got_error *
2177 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2178 c4972b91 2018-05-07 stsp {
2179 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2180 9ba79e04 2018-06-11 stsp
2181 1a76625f 2018-10-22 stsp /*
2182 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2183 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2184 1a76625f 2018-10-22 stsp * while updating the display.
2185 1a76625f 2018-10-22 stsp */
2186 4e0d2870 2020-12-07 naddy do {
2187 7210b715 2022-09-11 thomas struct got_object_id id;
2188 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2189 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2190 7e8004ba 2022-09-11 thomas int limit_match = 0;
2191 1a76625f 2018-10-22 stsp int errcode;
2192 899d86c2 2018-05-10 stsp
2193 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2194 4e0d2870 2020-12-07 naddy NULL, NULL);
2195 7210b715 2022-09-11 thomas if (err)
2196 ecb28ae0 2018-07-16 stsp break;
2197 899d86c2 2018-05-10 stsp
2198 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2199 9ba79e04 2018-06-11 stsp if (err)
2200 9ba79e04 2018-06-11 stsp break;
2201 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2202 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2203 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2204 9ba79e04 2018-06-11 stsp break;
2205 9ba79e04 2018-06-11 stsp }
2206 93e45b7c 2018-09-24 stsp
2207 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2208 1a76625f 2018-10-22 stsp if (errcode) {
2209 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2210 13add988 2019-10-15 stsp "pthread_mutex_lock");
2211 1a76625f 2018-10-22 stsp break;
2212 1a76625f 2018-10-22 stsp }
2213 1a76625f 2018-10-22 stsp
2214 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2215 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2216 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2217 1a76625f 2018-10-22 stsp
2218 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2219 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2220 7e8004ba 2022-09-11 thomas a->limit_regex);
2221 7e8004ba 2022-09-11 thomas if (err)
2222 7e8004ba 2022-09-11 thomas break;
2223 7e8004ba 2022-09-11 thomas
2224 7e8004ba 2022-09-11 thomas if (limit_match) {
2225 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2226 7e8004ba 2022-09-11 thomas
2227 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2228 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2229 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2230 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2231 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2232 7e8004ba 2022-09-11 thomas break;
2233 7e8004ba 2022-09-11 thomas }
2234 7e8004ba 2022-09-11 thomas
2235 7e8004ba 2022-09-11 thomas err = got_object_commit_dup(&matched->commit,
2236 7e8004ba 2022-09-11 thomas entry->commit);
2237 7e8004ba 2022-09-11 thomas if (err)
2238 7e8004ba 2022-09-11 thomas break;
2239 7e8004ba 2022-09-11 thomas
2240 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2241 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2242 7e8004ba 2022-09-11 thomas matched, entry);
2243 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2244 7e8004ba 2022-09-11 thomas }
2245 7e8004ba 2022-09-11 thomas
2246 7e8004ba 2022-09-11 thomas /*
2247 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2248 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2249 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2250 7e8004ba 2022-09-11 thomas */
2251 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2252 7e8004ba 2022-09-11 thomas }
2253 7e8004ba 2022-09-11 thomas
2254 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2255 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2256 7c1452c1 2020-03-26 stsp int have_match;
2257 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2258 7c1452c1 2020-03-26 stsp if (err)
2259 7c1452c1 2020-03-26 stsp break;
2260 7e8004ba 2022-09-11 thomas
2261 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2262 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2263 7e8004ba 2022-09-11 thomas *a->search_next_done =
2264 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2265 7e8004ba 2022-09-11 thomas } else if (have_match)
2266 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2267 13add988 2019-10-15 stsp }
2268 13add988 2019-10-15 stsp
2269 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2270 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2271 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2272 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2273 7c1452c1 2020-03-26 stsp if (err)
2274 13add988 2019-10-15 stsp break;
2275 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2276 899d86c2 2018-05-10 stsp
2277 9ba79e04 2018-06-11 stsp return err;
2278 0553a4e3 2018-04-30 stsp }
2279 0553a4e3 2018-04-30 stsp
2280 2b779855 2020-12-05 naddy static void
2281 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2282 2b779855 2020-12-05 naddy {
2283 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2284 2b779855 2020-12-05 naddy int ncommits = 0;
2285 2b779855 2020-12-05 naddy
2286 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2287 2b779855 2020-12-05 naddy while (entry) {
2288 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2289 2b779855 2020-12-05 naddy s->selected_entry = entry;
2290 2b779855 2020-12-05 naddy break;
2291 2b779855 2020-12-05 naddy }
2292 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2293 2b779855 2020-12-05 naddy ncommits++;
2294 2b779855 2020-12-05 naddy }
2295 2b779855 2020-12-05 naddy }
2296 2b779855 2020-12-05 naddy
2297 0553a4e3 2018-04-30 stsp static const struct got_error *
2298 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2299 0553a4e3 2018-04-30 stsp {
2300 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2301 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2302 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2303 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2304 60493ae3 2019-06-20 stsp int width;
2305 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2306 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2307 8b473291 2019-02-21 stsp char *refs_str = NULL;
2308 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2309 11b20872 2019-11-08 stsp struct tog_color *tc;
2310 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2311 bd3f8225 2022-08-12 thomas
2312 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2313 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2314 0553a4e3 2018-04-30 stsp
2315 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2316 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2317 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2318 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2319 1a76625f 2018-10-22 stsp if (err)
2320 ecb28ae0 2018-07-16 stsp return err;
2321 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2322 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2323 d2075bf3 2020-12-25 stsp if (refs) {
2324 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2325 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2326 d2075bf3 2020-12-25 stsp if (err)
2327 d2075bf3 2020-12-25 stsp goto done;
2328 d2075bf3 2020-12-25 stsp }
2329 867c6645 2018-07-10 stsp }
2330 359bfafd 2019-02-22 stsp
2331 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2332 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2333 1a76625f 2018-10-22 stsp
2334 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2335 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2336 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2337 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2338 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2339 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2340 8f4ed634 2020-03-26 stsp goto done;
2341 8f4ed634 2020-03-26 stsp }
2342 8f4ed634 2020-03-26 stsp } else {
2343 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2344 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2345 f9686aa5 2020-03-27 stsp
2346 f9686aa5 2020-03-27 stsp if (view->searching) {
2347 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2348 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2349 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2350 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2351 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2352 f9686aa5 2020-03-27 stsp search_str = "searching...";
2353 f9686aa5 2020-03-27 stsp }
2354 f9686aa5 2020-03-27 stsp
2355 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2356 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2357 7e8004ba 2022-09-11 thomas
2358 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2359 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2360 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2361 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2362 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2363 8f4ed634 2020-03-26 stsp goto done;
2364 8f4ed634 2020-03-26 stsp }
2365 8b473291 2019-02-21 stsp }
2366 1a76625f 2018-10-22 stsp
2367 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2368 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2369 91198554 2022-06-23 thomas "........................................",
2370 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2371 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2372 1a76625f 2018-10-22 stsp header = NULL;
2373 1a76625f 2018-10-22 stsp goto done;
2374 1a76625f 2018-10-22 stsp }
2375 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2376 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2377 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2378 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2379 1a76625f 2018-10-22 stsp header = NULL;
2380 1a76625f 2018-10-22 stsp goto done;
2381 ecb28ae0 2018-07-16 stsp }
2382 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2383 1a76625f 2018-10-22 stsp if (err)
2384 1a76625f 2018-10-22 stsp goto done;
2385 867c6645 2018-07-10 stsp
2386 2814baeb 2018-08-01 stsp werase(view->window);
2387 867c6645 2018-07-10 stsp
2388 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2389 a3404814 2018-09-02 stsp wstandout(view->window);
2390 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2391 11b20872 2019-11-08 stsp if (tc)
2392 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2393 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2394 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2395 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2396 1a76625f 2018-10-22 stsp width++;
2397 1a76625f 2018-10-22 stsp }
2398 86f4aab9 2022-09-09 thomas if (tc)
2399 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2400 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2401 a3404814 2018-09-02 stsp wstandend(view->window);
2402 ecb28ae0 2018-07-16 stsp free(wline);
2403 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2404 1a76625f 2018-10-22 stsp goto done;
2405 0553a4e3 2018-04-30 stsp
2406 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2407 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2408 5813d178 2019-03-09 stsp ncommits = 0;
2409 05171be4 2022-06-23 thomas view->maxx = 0;
2410 5813d178 2019-03-09 stsp while (entry) {
2411 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2412 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2413 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2414 5813d178 2019-03-09 stsp int width;
2415 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2416 5813d178 2019-03-09 stsp break;
2417 f69c5a46 2022-07-19 thomas if (s->use_committer)
2418 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2419 f69c5a46 2022-07-19 thomas else
2420 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2421 5813d178 2019-03-09 stsp if (author == NULL) {
2422 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2423 5813d178 2019-03-09 stsp goto done;
2424 5813d178 2019-03-09 stsp }
2425 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2426 27a741e5 2019-09-11 stsp date_display_cols);
2427 5813d178 2019-03-09 stsp if (author_cols < width)
2428 5813d178 2019-03-09 stsp author_cols = width;
2429 5813d178 2019-03-09 stsp free(wauthor);
2430 5813d178 2019-03-09 stsp free(author);
2431 ef944b8b 2022-07-21 thomas if (err)
2432 ef944b8b 2022-07-21 thomas goto done;
2433 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2434 05171be4 2022-06-23 thomas if (err)
2435 05171be4 2022-06-23 thomas goto done;
2436 05171be4 2022-06-23 thomas msg = msg0;
2437 05171be4 2022-06-23 thomas while (*msg == '\n')
2438 05171be4 2022-06-23 thomas ++msg;
2439 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2440 331b1a16 2022-06-23 thomas *eol = '\0';
2441 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2442 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2443 331b1a16 2022-06-23 thomas if (err)
2444 331b1a16 2022-06-23 thomas goto done;
2445 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2446 05171be4 2022-06-23 thomas free(msg0);
2447 331b1a16 2022-06-23 thomas free(wmsg);
2448 7ca04879 2019-10-19 stsp ncommits++;
2449 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2450 5813d178 2019-03-09 stsp }
2451 5813d178 2019-03-09 stsp
2452 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2453 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2454 867c6645 2018-07-10 stsp ncommits = 0;
2455 899d86c2 2018-05-10 stsp while (entry) {
2456 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2457 899d86c2 2018-05-10 stsp break;
2458 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2459 2814baeb 2018-08-01 stsp wstandout(view->window);
2460 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2461 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2462 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2463 2814baeb 2018-08-01 stsp wstandend(view->window);
2464 0553a4e3 2018-04-30 stsp if (err)
2465 60493ae3 2019-06-20 stsp goto done;
2466 0553a4e3 2018-04-30 stsp ncommits++;
2467 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2468 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2469 80ddbec8 2018-04-29 stsp }
2470 80ddbec8 2018-04-29 stsp
2471 a5d43cac 2022-07-01 thomas view_border(view);
2472 1a76625f 2018-10-22 stsp done:
2473 1a76625f 2018-10-22 stsp free(id_str);
2474 8b473291 2019-02-21 stsp free(refs_str);
2475 1a76625f 2018-10-22 stsp free(ncommits_str);
2476 1a76625f 2018-10-22 stsp free(header);
2477 80ddbec8 2018-04-29 stsp return err;
2478 9f7d7167 2018-04-29 stsp }
2479 07b55e75 2018-05-10 stsp
2480 07b55e75 2018-05-10 stsp static void
2481 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2482 07b55e75 2018-05-10 stsp {
2483 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2484 07b55e75 2018-05-10 stsp int nscrolled = 0;
2485 07b55e75 2018-05-10 stsp
2486 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2487 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2488 07b55e75 2018-05-10 stsp return;
2489 9f7d7167 2018-04-29 stsp
2490 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2491 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2492 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2493 07b55e75 2018-05-10 stsp if (entry) {
2494 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2495 07b55e75 2018-05-10 stsp nscrolled++;
2496 07b55e75 2018-05-10 stsp }
2497 07b55e75 2018-05-10 stsp }
2498 aa075928 2018-05-10 stsp }
2499 aa075928 2018-05-10 stsp
2500 aa075928 2018-05-10 stsp static const struct got_error *
2501 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2502 aa075928 2018-05-10 stsp {
2503 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2504 5e224a3e 2019-02-22 stsp int errcode;
2505 8a42fca8 2019-02-22 stsp
2506 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2507 aa075928 2018-05-10 stsp
2508 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2509 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2510 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2511 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2512 7aafa0d1 2019-02-22 stsp if (errcode)
2513 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2514 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2515 7c1452c1 2020-03-26 stsp
2516 7c1452c1 2020-03-26 stsp /*
2517 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2518 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2519 7c1452c1 2020-03-26 stsp */
2520 7c1452c1 2020-03-26 stsp if (!wait)
2521 7c1452c1 2020-03-26 stsp break;
2522 7c1452c1 2020-03-26 stsp
2523 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2524 ffe38506 2020-12-01 naddy show_log_view(view);
2525 7c1452c1 2020-03-26 stsp update_panels();
2526 7c1452c1 2020-03-26 stsp doupdate();
2527 7c1452c1 2020-03-26 stsp
2528 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2529 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2530 82954512 2020-02-03 stsp if (errcode)
2531 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2532 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2533 82954512 2020-02-03 stsp
2534 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2535 ffe38506 2020-12-01 naddy show_log_view(view);
2536 7c1452c1 2020-03-26 stsp update_panels();
2537 7c1452c1 2020-03-26 stsp doupdate();
2538 5e224a3e 2019-02-22 stsp }
2539 5e224a3e 2019-02-22 stsp
2540 5e224a3e 2019-02-22 stsp return NULL;
2541 a5d43cac 2022-07-01 thomas }
2542 a5d43cac 2022-07-01 thomas
2543 a5d43cac 2022-07-01 thomas static const struct got_error *
2544 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2545 a5d43cac 2022-07-01 thomas {
2546 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2547 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2548 fe731b51 2022-07-14 thomas
2549 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2550 fe731b51 2022-07-14 thomas return NULL;
2551 a5d43cac 2022-07-01 thomas
2552 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2553 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2554 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2555 a5d43cac 2022-07-01 thomas
2556 a5d43cac 2022-07-01 thomas return err;
2557 5e224a3e 2019-02-22 stsp }
2558 5e224a3e 2019-02-22 stsp
2559 5e224a3e 2019-02-22 stsp static const struct got_error *
2560 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2561 5e224a3e 2019-02-22 stsp {
2562 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2563 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2564 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2565 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2566 5e224a3e 2019-02-22 stsp
2567 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2568 5e224a3e 2019-02-22 stsp return NULL;
2569 5e224a3e 2019-02-22 stsp
2570 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2571 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2572 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2573 08ebd0a9 2019-02-22 stsp /*
2574 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2575 08ebd0a9 2019-02-22 stsp */
2576 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2577 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2578 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2579 5e224a3e 2019-02-22 stsp if (err)
2580 5e224a3e 2019-02-22 stsp return err;
2581 7aafa0d1 2019-02-22 stsp }
2582 b295e71b 2019-02-22 stsp
2583 7aafa0d1 2019-02-22 stsp do {
2584 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2585 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2586 88048b54 2019-02-21 stsp break;
2587 88048b54 2019-02-21 stsp
2588 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2589 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2590 aa075928 2018-05-10 stsp
2591 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2592 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2593 dd0a52c1 2018-05-20 stsp break;
2594 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2595 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2596 aa075928 2018-05-10 stsp
2597 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2598 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2599 a5d43cac 2022-07-01 thomas else
2600 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2601 a5d43cac 2022-07-01 thomas
2602 dd0a52c1 2018-05-20 stsp return err;
2603 07b55e75 2018-05-10 stsp }
2604 4a7f7875 2018-05-10 stsp
2605 cd0acaa7 2018-05-20 stsp static const struct got_error *
2606 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2607 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2608 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2609 cd0acaa7 2018-05-20 stsp {
2610 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2611 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2612 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2613 cd0acaa7 2018-05-20 stsp
2614 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2615 15a94983 2018-12-23 stsp if (diff_view == NULL)
2616 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2617 ea5e7bb5 2018-08-01 stsp
2618 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2619 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2620 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2621 e5a0f69f 2018-08-18 stsp if (err == NULL)
2622 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2623 cd0acaa7 2018-05-20 stsp return err;
2624 4a7f7875 2018-05-10 stsp }
2625 4a7f7875 2018-05-10 stsp
2626 80ddbec8 2018-04-29 stsp static const struct got_error *
2627 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2628 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2629 9343a5fb 2018-06-23 stsp {
2630 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2631 941e9f74 2019-05-21 stsp
2632 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2633 941e9f74 2019-05-21 stsp if (parent == NULL)
2634 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2635 941e9f74 2019-05-21 stsp
2636 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2637 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2638 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2639 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2640 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2641 941e9f74 2019-05-21 stsp s->tree = subtree;
2642 941e9f74 2019-05-21 stsp s->selected = 0;
2643 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2644 941e9f74 2019-05-21 stsp return NULL;
2645 941e9f74 2019-05-21 stsp }
2646 941e9f74 2019-05-21 stsp
2647 941e9f74 2019-05-21 stsp static const struct got_error *
2648 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2649 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2650 941e9f74 2019-05-21 stsp {
2651 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2652 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2653 941e9f74 2019-05-21 stsp const char *p;
2654 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2655 9343a5fb 2018-06-23 stsp
2656 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2657 941e9f74 2019-05-21 stsp p = path;
2658 941e9f74 2019-05-21 stsp while (*p) {
2659 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2660 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2661 56e0773d 2019-11-28 stsp char *te_name;
2662 33cbf02b 2020-01-12 stsp
2663 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2664 33cbf02b 2020-01-12 stsp p++;
2665 941e9f74 2019-05-21 stsp
2666 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2667 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2668 941e9f74 2019-05-21 stsp if (slash == NULL)
2669 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2670 33cbf02b 2020-01-12 stsp else
2671 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2672 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2673 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2674 56e0773d 2019-11-28 stsp break;
2675 941e9f74 2019-05-21 stsp }
2676 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2677 56e0773d 2019-11-28 stsp if (te == NULL) {
2678 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2679 56e0773d 2019-11-28 stsp free(te_name);
2680 941e9f74 2019-05-21 stsp break;
2681 941e9f74 2019-05-21 stsp }
2682 56e0773d 2019-11-28 stsp free(te_name);
2683 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2684 941e9f74 2019-05-21 stsp
2685 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2686 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2687 b03c880f 2019-05-21 stsp
2688 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2689 941e9f74 2019-05-21 stsp if (slash)
2690 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2691 941e9f74 2019-05-21 stsp else
2692 941e9f74 2019-05-21 stsp subpath = strdup(path);
2693 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2694 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2695 941e9f74 2019-05-21 stsp break;
2696 941e9f74 2019-05-21 stsp }
2697 941e9f74 2019-05-21 stsp
2698 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2699 941e9f74 2019-05-21 stsp subpath);
2700 941e9f74 2019-05-21 stsp if (err)
2701 941e9f74 2019-05-21 stsp break;
2702 941e9f74 2019-05-21 stsp
2703 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2704 941e9f74 2019-05-21 stsp free(tree_id);
2705 941e9f74 2019-05-21 stsp if (err)
2706 941e9f74 2019-05-21 stsp break;
2707 941e9f74 2019-05-21 stsp
2708 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2709 941e9f74 2019-05-21 stsp if (err) {
2710 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2711 941e9f74 2019-05-21 stsp break;
2712 941e9f74 2019-05-21 stsp }
2713 941e9f74 2019-05-21 stsp if (slash == NULL)
2714 941e9f74 2019-05-21 stsp break;
2715 941e9f74 2019-05-21 stsp free(subpath);
2716 941e9f74 2019-05-21 stsp subpath = NULL;
2717 941e9f74 2019-05-21 stsp p = slash;
2718 941e9f74 2019-05-21 stsp }
2719 941e9f74 2019-05-21 stsp
2720 941e9f74 2019-05-21 stsp free(subpath);
2721 1a76625f 2018-10-22 stsp return err;
2722 61266923 2020-01-14 stsp }
2723 61266923 2020-01-14 stsp
2724 61266923 2020-01-14 stsp static const struct got_error *
2725 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2726 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2727 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2728 55cccc34 2020-02-20 stsp {
2729 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2730 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2731 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2732 55cccc34 2020-02-20 stsp
2733 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2734 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2735 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2736 55cccc34 2020-02-20 stsp
2737 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2738 bc573f3b 2021-07-10 stsp if (err)
2739 55cccc34 2020-02-20 stsp return err;
2740 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2741 55cccc34 2020-02-20 stsp
2742 55cccc34 2020-02-20 stsp *new_view = tree_view;
2743 55cccc34 2020-02-20 stsp
2744 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2745 55cccc34 2020-02-20 stsp return NULL;
2746 55cccc34 2020-02-20 stsp
2747 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2748 55cccc34 2020-02-20 stsp }
2749 55cccc34 2020-02-20 stsp
2750 55cccc34 2020-02-20 stsp static const struct got_error *
2751 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2752 61266923 2020-01-14 stsp {
2753 61266923 2020-01-14 stsp sigset_t sigset;
2754 61266923 2020-01-14 stsp int errcode;
2755 61266923 2020-01-14 stsp
2756 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2757 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2758 61266923 2020-01-14 stsp
2759 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2760 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2761 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2762 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2763 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2764 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2765 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2766 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2767 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2768 61266923 2020-01-14 stsp
2769 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2770 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2771 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2772 61266923 2020-01-14 stsp
2773 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2774 61266923 2020-01-14 stsp if (errcode)
2775 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2776 61266923 2020-01-14 stsp
2777 61266923 2020-01-14 stsp return NULL;
2778 1a76625f 2018-10-22 stsp }
2779 1a76625f 2018-10-22 stsp
2780 1a76625f 2018-10-22 stsp static void *
2781 1a76625f 2018-10-22 stsp log_thread(void *arg)
2782 1a76625f 2018-10-22 stsp {
2783 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2784 1a76625f 2018-10-22 stsp int errcode = 0;
2785 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2786 1a76625f 2018-10-22 stsp int done = 0;
2787 61266923 2020-01-14 stsp
2788 f2d749db 2022-07-12 thomas /*
2789 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2790 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2791 f2d749db 2022-07-12 thomas */
2792 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2793 f2d749db 2022-07-12 thomas if (errcode) {
2794 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2795 61266923 2020-01-14 stsp return (void *)err;
2796 f2d749db 2022-07-12 thomas }
2797 1a76625f 2018-10-22 stsp
2798 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2799 f2d749db 2022-07-12 thomas if (err) {
2800 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2801 f2d749db 2022-07-12 thomas goto done;
2802 f2d749db 2022-07-12 thomas }
2803 f2d749db 2022-07-12 thomas
2804 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2805 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2806 f2d749db 2022-07-12 thomas if (errcode) {
2807 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2808 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2809 f2d749db 2022-07-12 thomas goto done;
2810 f2d749db 2022-07-12 thomas }
2811 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2812 1a76625f 2018-10-22 stsp if (err) {
2813 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2814 f2d749db 2022-07-12 thomas goto done;
2815 1a76625f 2018-10-22 stsp err = NULL;
2816 1a76625f 2018-10-22 stsp done = 1;
2817 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
2818 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2819 7e8004ba 2022-09-11 thomas if (a->limit_match)
2820 7e8004ba 2022-09-11 thomas a->commits_needed--;
2821 7e8004ba 2022-09-11 thomas } else
2822 7e8004ba 2022-09-11 thomas a->commits_needed--;
2823 7e8004ba 2022-09-11 thomas }
2824 1a76625f 2018-10-22 stsp
2825 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2826 3abe8080 2019-04-10 stsp if (errcode) {
2827 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2828 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2829 f2d749db 2022-07-12 thomas goto done;
2830 3abe8080 2019-04-10 stsp } else if (*a->quit)
2831 1a76625f 2018-10-22 stsp done = 1;
2832 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
2833 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2834 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
2835 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2836 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
2837 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
2838 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
2839 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
2840 1a76625f 2018-10-22 stsp }
2841 1a76625f 2018-10-22 stsp
2842 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2843 7c1452c1 2020-03-26 stsp if (errcode) {
2844 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2845 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2846 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2847 f2d749db 2022-07-12 thomas goto done;
2848 7c1452c1 2020-03-26 stsp }
2849 7c1452c1 2020-03-26 stsp
2850 1a76625f 2018-10-22 stsp if (done)
2851 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2852 7c1452c1 2020-03-26 stsp else {
2853 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2854 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2855 7c1452c1 2020-03-26 stsp &tog_mutex);
2856 f2d749db 2022-07-12 thomas if (errcode) {
2857 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2858 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2859 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2860 f2d749db 2022-07-12 thomas goto done;
2861 f2d749db 2022-07-12 thomas }
2862 21355643 2020-12-06 stsp if (*a->quit)
2863 21355643 2020-12-06 stsp done = 1;
2864 7c1452c1 2020-03-26 stsp }
2865 1a76625f 2018-10-22 stsp }
2866 1a76625f 2018-10-22 stsp }
2867 3abe8080 2019-04-10 stsp a->log_complete = 1;
2868 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2869 f2d749db 2022-07-12 thomas if (errcode)
2870 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2871 f2d749db 2022-07-12 thomas done:
2872 f2d749db 2022-07-12 thomas if (err) {
2873 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2874 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2875 f2d749db 2022-07-12 thomas }
2876 1a76625f 2018-10-22 stsp return (void *)err;
2877 1a76625f 2018-10-22 stsp }
2878 1a76625f 2018-10-22 stsp
2879 1a76625f 2018-10-22 stsp static const struct got_error *
2880 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2881 1a76625f 2018-10-22 stsp {
2882 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2883 1a76625f 2018-10-22 stsp int errcode;
2884 1a76625f 2018-10-22 stsp
2885 1a76625f 2018-10-22 stsp if (s->thread) {
2886 1a76625f 2018-10-22 stsp s->quit = 1;
2887 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2888 1a76625f 2018-10-22 stsp if (errcode)
2889 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2890 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2891 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2892 1a76625f 2018-10-22 stsp if (errcode)
2893 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2894 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2895 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2896 1a76625f 2018-10-22 stsp if (errcode)
2897 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2898 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2899 1a76625f 2018-10-22 stsp if (errcode)
2900 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2901 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2902 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2903 1a76625f 2018-10-22 stsp }
2904 1a76625f 2018-10-22 stsp
2905 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2906 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2907 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2908 1af5eddf 2022-06-23 thomas }
2909 1af5eddf 2022-06-23 thomas
2910 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2911 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2912 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2913 1af5eddf 2022-06-23 thomas if (err == NULL)
2914 1af5eddf 2022-06-23 thomas err = pack_err;
2915 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2916 1a76625f 2018-10-22 stsp }
2917 1a76625f 2018-10-22 stsp
2918 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2919 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2920 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2921 1a76625f 2018-10-22 stsp }
2922 1a76625f 2018-10-22 stsp
2923 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2924 9343a5fb 2018-06-23 stsp }
2925 9343a5fb 2018-06-23 stsp
2926 9343a5fb 2018-06-23 stsp static const struct got_error *
2927 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2928 1a76625f 2018-10-22 stsp {
2929 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2930 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2931 276b94a1 2020-11-13 naddy int errcode;
2932 1a76625f 2018-10-22 stsp
2933 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2934 276b94a1 2020-11-13 naddy
2935 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2936 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2937 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2938 276b94a1 2020-11-13 naddy
2939 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2940 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2941 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2942 276b94a1 2020-11-13 naddy
2943 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
2944 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
2945 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2946 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2947 1a76625f 2018-10-22 stsp free(s->start_id);
2948 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2949 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2950 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2951 1a76625f 2018-10-22 stsp return err;
2952 1a76625f 2018-10-22 stsp }
2953 1a76625f 2018-10-22 stsp
2954 7e8004ba 2022-09-11 thomas /*
2955 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
2956 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
2957 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
2958 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
2959 7e8004ba 2022-09-11 thomas * works with very slight change.
2960 7e8004ba 2022-09-11 thomas */
2961 1a76625f 2018-10-22 stsp static const struct got_error *
2962 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
2963 7e8004ba 2022-09-11 thomas {
2964 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
2965 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
2966 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
2967 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
2968 7e8004ba 2022-09-11 thomas char pattern[1024];
2969 7e8004ba 2022-09-11 thomas int ret;
2970 7e8004ba 2022-09-11 thomas
2971 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
2972 7e8004ba 2022-09-11 thomas v = view->child;
2973 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
2974 7e8004ba 2022-09-11 thomas v = view->parent;
2975 7e8004ba 2022-09-11 thomas
2976 7e8004ba 2022-09-11 thomas /* Get the pattern */
2977 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
2978 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
2979 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
2980 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
2981 7e8004ba 2022-09-11 thomas nocbreak();
2982 7e8004ba 2022-09-11 thomas echo();
2983 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
2984 7e8004ba 2022-09-11 thomas cbreak();
2985 7e8004ba 2022-09-11 thomas noecho();
2986 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
2987 7e8004ba 2022-09-11 thomas if (ret == ERR)
2988 7e8004ba 2022-09-11 thomas return NULL;
2989 7e8004ba 2022-09-11 thomas
2990 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
2991 7e8004ba 2022-09-11 thomas /*
2992 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
2993 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
2994 7e8004ba 2022-09-11 thomas */
2995 7e8004ba 2022-09-11 thomas if (!s->limit_view)
2996 7e8004ba 2022-09-11 thomas return NULL;
2997 7e8004ba 2022-09-11 thomas
2998 7e8004ba 2022-09-11 thomas /*
2999 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3000 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3001 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3002 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3003 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3004 7e8004ba 2022-09-11 thomas * the queue.
3005 7e8004ba 2022-09-11 thomas */
3006 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3007 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3008 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3009 7e8004ba 2022-09-11 thomas s->selected = 0;
3010 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3011 7e8004ba 2022-09-11 thomas
3012 7e8004ba 2022-09-11 thomas return NULL;
3013 7e8004ba 2022-09-11 thomas }
3014 7e8004ba 2022-09-11 thomas
3015 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3016 7e8004ba 2022-09-11 thomas return NULL;
3017 7e8004ba 2022-09-11 thomas
3018 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3019 7e8004ba 2022-09-11 thomas
3020 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3021 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3022 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3023 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3024 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3025 7e8004ba 2022-09-11 thomas
3026 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3027 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3028 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3029 7e8004ba 2022-09-11 thomas
3030 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3031 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3032 7e8004ba 2022-09-11 thomas int have_match = 0;
3033 7e8004ba 2022-09-11 thomas
3034 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3035 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3036 7e8004ba 2022-09-11 thomas if (err)
3037 7e8004ba 2022-09-11 thomas return err;
3038 7e8004ba 2022-09-11 thomas
3039 7e8004ba 2022-09-11 thomas if (have_match) {
3040 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3041 7e8004ba 2022-09-11 thomas
3042 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3043 7e8004ba 2022-09-11 thomas entry->id);
3044 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3045 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3046 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3047 7e8004ba 2022-09-11 thomas break;
3048 7e8004ba 2022-09-11 thomas }
3049 7e8004ba 2022-09-11 thomas
3050 7e8004ba 2022-09-11 thomas err = got_object_commit_dup(&matched->commit,
3051 7e8004ba 2022-09-11 thomas entry->commit);
3052 7e8004ba 2022-09-11 thomas if (err) {
3053 7e8004ba 2022-09-11 thomas free(matched);
3054 7e8004ba 2022-09-11 thomas return err;
3055 7e8004ba 2022-09-11 thomas }
3056 7e8004ba 2022-09-11 thomas
3057 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3058 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3059 7e8004ba 2022-09-11 thomas matched, entry);
3060 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3061 7e8004ba 2022-09-11 thomas }
3062 7e8004ba 2022-09-11 thomas }
3063 7e8004ba 2022-09-11 thomas
3064 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3065 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3066 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3067 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3068 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3069 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3070 7e8004ba 2022-09-11 thomas if (err)
3071 7e8004ba 2022-09-11 thomas return err;
3072 7e8004ba 2022-09-11 thomas }
3073 7e8004ba 2022-09-11 thomas
3074 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3075 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3076 7e8004ba 2022-09-11 thomas s->selected = 0;
3077 7e8004ba 2022-09-11 thomas
3078 7e8004ba 2022-09-11 thomas return NULL;
3079 7e8004ba 2022-09-11 thomas }
3080 7e8004ba 2022-09-11 thomas
3081 7e8004ba 2022-09-11 thomas static const struct got_error *
3082 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3083 60493ae3 2019-06-20 stsp {
3084 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3085 60493ae3 2019-06-20 stsp
3086 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3087 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3088 60493ae3 2019-06-20 stsp return NULL;
3089 60493ae3 2019-06-20 stsp }
3090 60493ae3 2019-06-20 stsp
3091 60493ae3 2019-06-20 stsp static const struct got_error *
3092 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3093 60493ae3 2019-06-20 stsp {
3094 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3095 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3096 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3097 60493ae3 2019-06-20 stsp
3098 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3099 f9686aa5 2020-03-27 stsp show_log_view(view);
3100 f9686aa5 2020-03-27 stsp update_panels();
3101 f9686aa5 2020-03-27 stsp doupdate();
3102 f9686aa5 2020-03-27 stsp
3103 96e2b566 2019-07-08 stsp if (s->search_entry) {
3104 13add988 2019-10-15 stsp int errcode, ch;
3105 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3106 13add988 2019-10-15 stsp if (errcode)
3107 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3108 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3109 13add988 2019-10-15 stsp ch = wgetch(view->window);
3110 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3111 13add988 2019-10-15 stsp if (errcode)
3112 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3113 13add988 2019-10-15 stsp "pthread_mutex_lock");
3114 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3115 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3116 678cbce5 2019-07-28 stsp return NULL;
3117 678cbce5 2019-07-28 stsp }
3118 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3119 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3120 96e2b566 2019-07-08 stsp else
3121 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3122 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3123 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3124 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
3125 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
3126 df5e841d 2022-06-23 thomas
3127 df5e841d 2022-06-23 thomas /*
3128 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3129 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3130 1f4d5162 2022-06-23 thomas * might have changed.
3131 df5e841d 2022-06-23 thomas */
3132 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
3133 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
3134 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3135 df5e841d 2022-06-23 thomas else
3136 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
3137 b74feda6 2022-06-23 thomas } else {
3138 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
3139 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
3140 df5e841d 2022-06-23 thomas commit_queue_head, entry);
3141 df5e841d 2022-06-23 thomas else
3142 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
3143 df5e841d 2022-06-23 thomas commit_queue_head, entry);
3144 b74feda6 2022-06-23 thomas }
3145 20be8d96 2019-06-21 stsp } else {
3146 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3147 20be8d96 2019-06-21 stsp }
3148 60493ae3 2019-06-20 stsp
3149 60493ae3 2019-06-20 stsp while (1) {
3150 13add988 2019-10-15 stsp int have_match = 0;
3151 13add988 2019-10-15 stsp
3152 60493ae3 2019-06-20 stsp if (entry == NULL) {
3153 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3154 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3155 f9967bca 2020-03-27 stsp view->search_next_done =
3156 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3157 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3158 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3159 f801134a 2019-06-25 stsp return NULL;
3160 60493ae3 2019-06-20 stsp }
3161 96e2b566 2019-07-08 stsp /*
3162 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3163 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3164 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3165 96e2b566 2019-07-08 stsp */
3166 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3167 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3168 60493ae3 2019-06-20 stsp }
3169 60493ae3 2019-06-20 stsp
3170 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3171 13add988 2019-10-15 stsp &view->regex);
3172 5943eee2 2019-08-13 stsp if (err)
3173 13add988 2019-10-15 stsp break;
3174 13add988 2019-10-15 stsp if (have_match) {
3175 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3176 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3177 60493ae3 2019-06-20 stsp break;
3178 60493ae3 2019-06-20 stsp }
3179 13add988 2019-10-15 stsp
3180 96e2b566 2019-07-08 stsp s->search_entry = entry;
3181 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3182 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3183 b1bf1435 2019-06-21 stsp else
3184 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3185 60493ae3 2019-06-20 stsp }
3186 60493ae3 2019-06-20 stsp
3187 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3188 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3189 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3190 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3191 60493ae3 2019-06-20 stsp if (err)
3192 60493ae3 2019-06-20 stsp return err;
3193 ead14cbe 2019-06-21 stsp cur++;
3194 ead14cbe 2019-06-21 stsp }
3195 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3196 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3197 60493ae3 2019-06-20 stsp if (err)
3198 60493ae3 2019-06-20 stsp return err;
3199 ead14cbe 2019-06-21 stsp cur--;
3200 60493ae3 2019-06-20 stsp }
3201 60493ae3 2019-06-20 stsp }
3202 60493ae3 2019-06-20 stsp
3203 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3204 96e2b566 2019-07-08 stsp
3205 60493ae3 2019-06-20 stsp return NULL;
3206 60493ae3 2019-06-20 stsp }
3207 60493ae3 2019-06-20 stsp
3208 60493ae3 2019-06-20 stsp static const struct got_error *
3209 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3210 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3211 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3212 80ddbec8 2018-04-29 stsp {
3213 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3214 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3215 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3216 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3217 1a76625f 2018-10-22 stsp int errcode;
3218 80ddbec8 2018-04-29 stsp
3219 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3220 f135c941 2020-02-20 stsp free(s->in_repo_path);
3221 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3222 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3223 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3224 f135c941 2020-02-20 stsp }
3225 ecb28ae0 2018-07-16 stsp
3226 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3227 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3228 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3229 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3230 78756c87 2020-11-24 stsp
3231 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3232 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3233 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3234 7e8004ba 2022-09-11 thomas
3235 fb2756b9 2018-08-04 stsp s->repo = repo;
3236 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3237 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3238 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3239 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3240 9cd7cbd1 2020-12-07 stsp goto done;
3241 9cd7cbd1 2020-12-07 stsp }
3242 9cd7cbd1 2020-12-07 stsp }
3243 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3244 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3245 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3246 5036bf37 2018-09-24 stsp goto done;
3247 5036bf37 2018-09-24 stsp }
3248 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3249 e5a0f69f 2018-08-18 stsp
3250 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3251 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3252 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3253 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3254 11b20872 2019-11-08 stsp if (err)
3255 11b20872 2019-11-08 stsp goto done;
3256 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3257 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3258 11b20872 2019-11-08 stsp if (err) {
3259 11b20872 2019-11-08 stsp free_colors(&s->colors);
3260 11b20872 2019-11-08 stsp goto done;
3261 11b20872 2019-11-08 stsp }
3262 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3263 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3264 11b20872 2019-11-08 stsp if (err) {
3265 11b20872 2019-11-08 stsp free_colors(&s->colors);
3266 11b20872 2019-11-08 stsp goto done;
3267 11b20872 2019-11-08 stsp }
3268 11b20872 2019-11-08 stsp }
3269 11b20872 2019-11-08 stsp
3270 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3271 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3272 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3273 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3274 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3275 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3276 1a76625f 2018-10-22 stsp
3277 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3278 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3279 1af5eddf 2022-06-23 thomas if (err)
3280 1af5eddf 2022-06-23 thomas goto done;
3281 1af5eddf 2022-06-23 thomas }
3282 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3283 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3284 7cd52833 2022-06-23 thomas if (err)
3285 7cd52833 2022-06-23 thomas goto done;
3286 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3287 b672a97a 2020-01-27 stsp !s->log_branches);
3288 1a76625f 2018-10-22 stsp if (err)
3289 1a76625f 2018-10-22 stsp goto done;
3290 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3291 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3292 c5b78334 2020-01-12 stsp if (err)
3293 c5b78334 2020-01-12 stsp goto done;
3294 1a76625f 2018-10-22 stsp
3295 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3296 1a76625f 2018-10-22 stsp if (errcode) {
3297 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3298 1a76625f 2018-10-22 stsp goto done;
3299 1a76625f 2018-10-22 stsp }
3300 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3301 7c1452c1 2020-03-26 stsp if (errcode) {
3302 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3303 7c1452c1 2020-03-26 stsp goto done;
3304 7c1452c1 2020-03-26 stsp }
3305 1a76625f 2018-10-22 stsp
3306 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3307 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3308 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3309 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3310 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3311 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3312 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3313 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3314 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3315 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3316 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3317 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3318 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3319 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3320 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3321 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3322 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3323 ba4f502b 2018-08-04 stsp done:
3324 1a76625f 2018-10-22 stsp if (err)
3325 1a76625f 2018-10-22 stsp close_log_view(view);
3326 ba4f502b 2018-08-04 stsp return err;
3327 ba4f502b 2018-08-04 stsp }
3328 ba4f502b 2018-08-04 stsp
3329 e5a0f69f 2018-08-18 stsp static const struct got_error *
3330 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3331 ba4f502b 2018-08-04 stsp {
3332 f2f6d207 2020-11-24 stsp const struct got_error *err;
3333 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3334 ba4f502b 2018-08-04 stsp
3335 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3336 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3337 2b380cc8 2018-10-24 stsp &s->thread_args);
3338 2b380cc8 2018-10-24 stsp if (errcode)
3339 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3340 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3341 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3342 f2f6d207 2020-11-24 stsp if (err)
3343 f2f6d207 2020-11-24 stsp return err;
3344 f2f6d207 2020-11-24 stsp }
3345 2b380cc8 2018-10-24 stsp }
3346 2b380cc8 2018-10-24 stsp
3347 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3348 b31f89ff 2022-07-01 thomas }
3349 b31f89ff 2022-07-01 thomas
3350 b31f89ff 2022-07-01 thomas static void
3351 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3352 b31f89ff 2022-07-01 thomas {
3353 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3354 b31f89ff 2022-07-01 thomas
3355 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3356 b31f89ff 2022-07-01 thomas return;
3357 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3358 7e8004ba 2022-09-11 thomas view->count = 0;
3359 b31f89ff 2022-07-01 thomas
3360 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3361 b31f89ff 2022-07-01 thomas || home)
3362 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3363 b31f89ff 2022-07-01 thomas
3364 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3365 b31f89ff 2022-07-01 thomas --s->selected;
3366 b31f89ff 2022-07-01 thomas else
3367 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3368 b31f89ff 2022-07-01 thomas
3369 b31f89ff 2022-07-01 thomas select_commit(s);
3370 b31f89ff 2022-07-01 thomas return;
3371 b31f89ff 2022-07-01 thomas }
3372 b31f89ff 2022-07-01 thomas
3373 b31f89ff 2022-07-01 thomas static const struct got_error *
3374 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3375 b31f89ff 2022-07-01 thomas {
3376 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3377 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3378 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3379 b31f89ff 2022-07-01 thomas
3380 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3381 7e8004ba 2022-09-11 thomas return NULL;
3382 7e8004ba 2022-09-11 thomas
3383 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3384 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3385 b31f89ff 2022-07-01 thomas return NULL;
3386 b31f89ff 2022-07-01 thomas
3387 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3388 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3389 b31f89ff 2022-07-01 thomas
3390 7ed048bd 2022-08-12 thomas if (!page) {
3391 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3392 b31f89ff 2022-07-01 thomas ++s->selected;
3393 b31f89ff 2022-07-01 thomas else
3394 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3395 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3396 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3397 7ed048bd 2022-08-12 thomas int n;
3398 7ed048bd 2022-08-12 thomas
3399 7ed048bd 2022-08-12 thomas s->selected = 0;
3400 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3401 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3402 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3403 7ed048bd 2022-08-12 thomas if (entry == NULL)
3404 7ed048bd 2022-08-12 thomas break;
3405 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3406 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3407 7ed048bd 2022-08-12 thomas }
3408 7ed048bd 2022-08-12 thomas if (n > 0)
3409 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3410 b31f89ff 2022-07-01 thomas } else {
3411 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3412 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3413 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3414 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3415 bd3f8225 2022-08-12 thomas else
3416 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3417 b31f89ff 2022-07-01 thomas }
3418 b31f89ff 2022-07-01 thomas if (err)
3419 b31f89ff 2022-07-01 thomas return err;
3420 b31f89ff 2022-07-01 thomas
3421 a5d43cac 2022-07-01 thomas /*
3422 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3423 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3424 a5d43cac 2022-07-01 thomas */
3425 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3426 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3427 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3428 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3429 25100026 2022-08-13 thomas }
3430 a5d43cac 2022-07-01 thomas
3431 b31f89ff 2022-07-01 thomas select_commit(s);
3432 b31f89ff 2022-07-01 thomas
3433 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3434 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3435 b31f89ff 2022-07-01 thomas view->count = 0;
3436 b31f89ff 2022-07-01 thomas
3437 b31f89ff 2022-07-01 thomas return NULL;
3438 e5a0f69f 2018-08-18 stsp }
3439 04cc582a 2018-08-01 stsp
3440 a5d43cac 2022-07-01 thomas static void
3441 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3442 a5d43cac 2022-07-01 thomas {
3443 24415785 2022-07-03 thomas *x = 0;
3444 24415785 2022-07-03 thomas *y = 0;
3445 24415785 2022-07-03 thomas
3446 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3447 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3448 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3449 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3450 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3451 53d2bdd3 2022-07-10 thomas else
3452 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3453 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3454 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3455 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3456 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3457 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3458 53d2bdd3 2022-07-10 thomas else
3459 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3460 53d2bdd3 2022-07-10 thomas }
3461 a5d43cac 2022-07-01 thomas }
3462 a5d43cac 2022-07-01 thomas
3463 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3464 e5a0f69f 2018-08-18 stsp static const struct got_error *
3465 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3466 a5d43cac 2022-07-01 thomas {
3467 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3468 a5d43cac 2022-07-01 thomas
3469 a5d43cac 2022-07-01 thomas view->nlines = y;
3470 64486692 2022-07-07 thomas view->ncols = COLS;
3471 a5d43cac 2022-07-01 thomas err = view_resize(view);
3472 a5d43cac 2022-07-01 thomas if (err)
3473 a5d43cac 2022-07-01 thomas return err;
3474 a5d43cac 2022-07-01 thomas
3475 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3476 a5d43cac 2022-07-01 thomas
3477 a5d43cac 2022-07-01 thomas return err;
3478 07dd3ed3 2022-08-06 thomas }
3479 07dd3ed3 2022-08-06 thomas
3480 07dd3ed3 2022-08-06 thomas static const struct got_error *
3481 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3482 07dd3ed3 2022-08-06 thomas {
3483 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3484 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3485 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3486 25100026 2022-08-13 thomas
3487 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3488 25100026 2022-08-13 thomas return NULL;
3489 07dd3ed3 2022-08-06 thomas
3490 07dd3ed3 2022-08-06 thomas g = view->gline;
3491 07dd3ed3 2022-08-06 thomas view->gline = 0;
3492 07dd3ed3 2022-08-06 thomas
3493 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3494 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3495 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3496 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3497 07dd3ed3 2022-08-06 thomas select_commit(s);
3498 07dd3ed3 2022-08-06 thomas return NULL;
3499 07dd3ed3 2022-08-06 thomas }
3500 07dd3ed3 2022-08-06 thomas
3501 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3502 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3503 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3504 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3505 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3506 07dd3ed3 2022-08-06 thomas if (err)
3507 07dd3ed3 2022-08-06 thomas return err;
3508 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3509 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3510 07dd3ed3 2022-08-06 thomas
3511 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3512 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3513 07dd3ed3 2022-08-06 thomas
3514 07dd3ed3 2022-08-06 thomas select_commit(s);
3515 07dd3ed3 2022-08-06 thomas return NULL;
3516 07dd3ed3 2022-08-06 thomas
3517 a5d43cac 2022-07-01 thomas }
3518 a5d43cac 2022-07-01 thomas
3519 a5d43cac 2022-07-01 thomas static const struct got_error *
3520 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3521 e5a0f69f 2018-08-18 stsp {
3522 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3523 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3524 7ed048bd 2022-08-12 thomas int eos, nscroll;
3525 80ddbec8 2018-04-29 stsp
3526 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3527 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3528 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3529 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3530 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3531 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3532 fb280deb 2021-08-30 stsp }
3533 c0be8933 2022-08-12 thomas if (err)
3534 c0be8933 2022-08-12 thomas return err;
3535 528dedf3 2021-08-30 stsp }
3536 068ab281 2022-07-01 thomas
3537 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3538 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3539 068ab281 2022-07-01 thomas --eos; /* border */
3540 07dd3ed3 2022-08-06 thomas
3541 07dd3ed3 2022-08-06 thomas if (view->gline)
3542 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3543 068ab281 2022-07-01 thomas
3544 528dedf3 2021-08-30 stsp switch (ch) {
3545 7e8004ba 2022-09-11 thomas case '&':
3546 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3547 7e8004ba 2022-09-11 thomas break;
3548 1e37a5c2 2019-05-12 jcs case 'q':
3549 1e37a5c2 2019-05-12 jcs s->quit = 1;
3550 05171be4 2022-06-23 thomas break;
3551 05171be4 2022-06-23 thomas case '0':
3552 05171be4 2022-06-23 thomas view->x = 0;
3553 05171be4 2022-06-23 thomas break;
3554 05171be4 2022-06-23 thomas case '$':
3555 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3556 07b0611c 2022-06-23 thomas view->count = 0;
3557 05171be4 2022-06-23 thomas break;
3558 05171be4 2022-06-23 thomas case KEY_RIGHT:
3559 05171be4 2022-06-23 thomas case 'l':
3560 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3561 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3562 07b0611c 2022-06-23 thomas else
3563 07b0611c 2022-06-23 thomas view->count = 0;
3564 1e37a5c2 2019-05-12 jcs break;
3565 05171be4 2022-06-23 thomas case KEY_LEFT:
3566 05171be4 2022-06-23 thomas case 'h':
3567 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3568 07b0611c 2022-06-23 thomas if (view->x <= 0)
3569 07b0611c 2022-06-23 thomas view->count = 0;
3570 05171be4 2022-06-23 thomas break;
3571 1e37a5c2 2019-05-12 jcs case 'k':
3572 1e37a5c2 2019-05-12 jcs case KEY_UP:
3573 1e37a5c2 2019-05-12 jcs case '<':
3574 1e37a5c2 2019-05-12 jcs case ',':
3575 f7140bf5 2021-10-17 thomas case CTRL('p'):
3576 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3577 912a3f79 2021-08-30 j break;
3578 912a3f79 2021-08-30 j case 'g':
3579 912a3f79 2021-08-30 j case KEY_HOME:
3580 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3581 07b0611c 2022-06-23 thomas view->count = 0;
3582 1e37a5c2 2019-05-12 jcs break;
3583 70f17a53 2022-06-13 thomas case CTRL('u'):
3584 23427b14 2022-06-23 thomas case 'u':
3585 70f17a53 2022-06-13 thomas nscroll /= 2;
3586 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3587 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3588 a4292ac5 2019-05-12 jcs case CTRL('b'):
3589 1c5e5faa 2022-06-23 thomas case 'b':
3590 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3591 1e37a5c2 2019-05-12 jcs break;
3592 1e37a5c2 2019-05-12 jcs case 'j':
3593 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3594 1e37a5c2 2019-05-12 jcs case '>':
3595 1e37a5c2 2019-05-12 jcs case '.':
3596 f7140bf5 2021-10-17 thomas case CTRL('n'):
3597 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3598 912a3f79 2021-08-30 j break;
3599 f69c5a46 2022-07-19 thomas case '@':
3600 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3601 f69c5a46 2022-07-19 thomas break;
3602 912a3f79 2021-08-30 j case 'G':
3603 912a3f79 2021-08-30 j case KEY_END: {
3604 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3605 912a3f79 2021-08-30 j * traverse them all. */
3606 07b0611c 2022-06-23 thomas view->count = 0;
3607 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3608 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3609 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3610 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3611 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3612 1e37a5c2 2019-05-12 jcs break;
3613 912a3f79 2021-08-30 j }
3614 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3615 23427b14 2022-06-23 thomas case 'd':
3616 70f17a53 2022-06-13 thomas nscroll /= 2;
3617 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3618 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3619 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3620 4c2d69cb 2022-06-23 thomas case 'f':
3621 b31f89ff 2022-07-01 thomas case ' ':
3622 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3623 1e37a5c2 2019-05-12 jcs break;
3624 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3625 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3626 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3627 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3628 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3629 2b779855 2020-12-05 naddy select_commit(s);
3630 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3631 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3632 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3633 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3634 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3635 0bf7f153 2020-12-02 naddy }
3636 1e37a5c2 2019-05-12 jcs break;
3637 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3638 444d5325 2022-07-03 thomas case '\r':
3639 07b0611c 2022-06-23 thomas view->count = 0;
3640 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3641 e5a0f69f 2018-08-18 stsp break;
3642 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3643 1e37a5c2 2019-05-12 jcs break;
3644 1be4947a 2022-07-22 thomas case 'T':
3645 07b0611c 2022-06-23 thomas view->count = 0;
3646 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3647 5036bf37 2018-09-24 stsp break;
3648 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3649 1e37a5c2 2019-05-12 jcs break;
3650 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3651 21355643 2020-12-06 stsp case CTRL('l'):
3652 21355643 2020-12-06 stsp case 'B':
3653 07b0611c 2022-06-23 thomas view->count = 0;
3654 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3655 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3656 1e37a5c2 2019-05-12 jcs break;
3657 21355643 2020-12-06 stsp err = stop_log_thread(s);
3658 74cfe85e 2020-10-20 stsp if (err)
3659 74cfe85e 2020-10-20 stsp return err;
3660 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3661 21355643 2020-12-06 stsp char *parent_path;
3662 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3663 21355643 2020-12-06 stsp if (err)
3664 21355643 2020-12-06 stsp return err;
3665 21355643 2020-12-06 stsp free(s->in_repo_path);
3666 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3667 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3668 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3669 21355643 2020-12-06 stsp struct got_object_id *start_id;
3670 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3671 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3672 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3673 21355643 2020-12-06 stsp if (err)
3674 21355643 2020-12-06 stsp return err;
3675 21355643 2020-12-06 stsp free(s->start_id);
3676 21355643 2020-12-06 stsp s->start_id = start_id;
3677 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3678 21355643 2020-12-06 stsp } else /* 'B' */
3679 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3680 21355643 2020-12-06 stsp
3681 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3682 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3683 bf7e79b3 2022-06-23 thomas if (err)
3684 bf7e79b3 2022-06-23 thomas return err;
3685 bf7e79b3 2022-06-23 thomas }
3686 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3687 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3688 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3689 74cfe85e 2020-10-20 stsp if (err)
3690 21355643 2020-12-06 stsp return err;
3691 51a10b52 2020-12-26 stsp tog_free_refs();
3692 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3693 ca51c541 2020-12-07 stsp if (err)
3694 ca51c541 2020-12-07 stsp return err;
3695 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3696 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3697 d01904d4 2019-06-25 stsp if (err)
3698 d01904d4 2019-06-25 stsp return err;
3699 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3700 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3701 b672a97a 2020-01-27 stsp if (err)
3702 b672a97a 2020-01-27 stsp return err;
3703 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3704 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3705 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3706 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3707 21355643 2020-12-06 stsp s->selected_entry = NULL;
3708 21355643 2020-12-06 stsp s->selected = 0;
3709 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3710 21355643 2020-12-06 stsp s->quit = 0;
3711 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3712 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3713 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3714 94ecf40d 2022-07-22 thomas view->offset = 0;
3715 d01904d4 2019-06-25 stsp break;
3716 1be4947a 2022-07-22 thomas case 'R':
3717 07b0611c 2022-06-23 thomas view->count = 0;
3718 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3719 6458efa5 2020-11-24 stsp break;
3720 1e37a5c2 2019-05-12 jcs default:
3721 07b0611c 2022-06-23 thomas view->count = 0;
3722 1e37a5c2 2019-05-12 jcs break;
3723 899d86c2 2018-05-10 stsp }
3724 e5a0f69f 2018-08-18 stsp
3725 80ddbec8 2018-04-29 stsp return err;
3726 80ddbec8 2018-04-29 stsp }
3727 80ddbec8 2018-04-29 stsp
3728 4ed7e80c 2018-05-20 stsp static const struct got_error *
3729 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3730 c2db6724 2019-01-04 stsp {
3731 c2db6724 2019-01-04 stsp const struct got_error *error;
3732 c2db6724 2019-01-04 stsp
3733 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3734 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3735 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3736 37c06ea4 2019-07-15 stsp #endif
3737 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3738 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3739 c2db6724 2019-01-04 stsp
3740 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3741 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3742 c2db6724 2019-01-04 stsp
3743 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3744 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3745 c2db6724 2019-01-04 stsp
3746 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3747 c2db6724 2019-01-04 stsp if (error != NULL)
3748 c2db6724 2019-01-04 stsp return error;
3749 c2db6724 2019-01-04 stsp
3750 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3751 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3752 c2db6724 2019-01-04 stsp
3753 c2db6724 2019-01-04 stsp return NULL;
3754 c2db6724 2019-01-04 stsp }
3755 c2db6724 2019-01-04 stsp
3756 a915003a 2019-02-05 stsp static void
3757 a915003a 2019-02-05 stsp init_curses(void)
3758 a915003a 2019-02-05 stsp {
3759 296152d1 2022-05-31 thomas /*
3760 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3761 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3762 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3763 296152d1 2022-05-31 thomas */
3764 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3765 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3766 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3767 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3768 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3769 296152d1 2022-05-31 thomas
3770 a915003a 2019-02-05 stsp initscr();
3771 a915003a 2019-02-05 stsp cbreak();
3772 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3773 a915003a 2019-02-05 stsp noecho();
3774 a915003a 2019-02-05 stsp nonl();
3775 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3776 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3777 a915003a 2019-02-05 stsp curs_set(0);
3778 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3779 6d17833f 2019-11-08 stsp start_color();
3780 6d17833f 2019-11-08 stsp use_default_colors();
3781 6d17833f 2019-11-08 stsp }
3782 a915003a 2019-02-05 stsp }
3783 a915003a 2019-02-05 stsp
3784 c2db6724 2019-01-04 stsp static const struct got_error *
3785 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3786 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3787 f135c941 2020-02-20 stsp {
3788 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3789 f135c941 2020-02-20 stsp
3790 f135c941 2020-02-20 stsp if (argc == 0) {
3791 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3792 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3793 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3794 f135c941 2020-02-20 stsp return NULL;
3795 f135c941 2020-02-20 stsp }
3796 f135c941 2020-02-20 stsp
3797 f135c941 2020-02-20 stsp if (worktree) {
3798 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3799 bfd61697 2020-11-14 stsp char *p;
3800 f135c941 2020-02-20 stsp
3801 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3802 f135c941 2020-02-20 stsp if (err)
3803 f135c941 2020-02-20 stsp return err;
3804 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3805 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3806 bfd61697 2020-11-14 stsp p) == -1) {
3807 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3808 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3809 f135c941 2020-02-20 stsp }
3810 f135c941 2020-02-20 stsp free(p);
3811 f135c941 2020-02-20 stsp } else
3812 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3813 f135c941 2020-02-20 stsp
3814 f135c941 2020-02-20 stsp return err;
3815 f135c941 2020-02-20 stsp }
3816 f135c941 2020-02-20 stsp
3817 f135c941 2020-02-20 stsp static const struct got_error *
3818 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3819 9f7d7167 2018-04-29 stsp {
3820 80ddbec8 2018-04-29 stsp const struct got_error *error;
3821 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3822 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3823 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3824 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3825 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3826 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3827 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3828 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3829 04cc582a 2018-08-01 stsp struct tog_view *view;
3830 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3831 80ddbec8 2018-04-29 stsp
3832 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3833 80ddbec8 2018-04-29 stsp switch (ch) {
3834 b672a97a 2020-01-27 stsp case 'b':
3835 b672a97a 2020-01-27 stsp log_branches = 1;
3836 b672a97a 2020-01-27 stsp break;
3837 80ddbec8 2018-04-29 stsp case 'c':
3838 80ddbec8 2018-04-29 stsp start_commit = optarg;
3839 80ddbec8 2018-04-29 stsp break;
3840 ecb28ae0 2018-07-16 stsp case 'r':
3841 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3842 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3843 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3844 9ba1d308 2019-10-21 stsp optarg);
3845 ecb28ae0 2018-07-16 stsp break;
3846 80ddbec8 2018-04-29 stsp default:
3847 17020d27 2019-03-07 stsp usage_log();
3848 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3849 80ddbec8 2018-04-29 stsp }
3850 80ddbec8 2018-04-29 stsp }
3851 80ddbec8 2018-04-29 stsp
3852 80ddbec8 2018-04-29 stsp argc -= optind;
3853 80ddbec8 2018-04-29 stsp argv += optind;
3854 80ddbec8 2018-04-29 stsp
3855 f135c941 2020-02-20 stsp if (argc > 1)
3856 f135c941 2020-02-20 stsp usage_log();
3857 963f97a1 2019-03-18 stsp
3858 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3859 7cd52833 2022-06-23 thomas if (error != NULL)
3860 7cd52833 2022-06-23 thomas goto done;
3861 7cd52833 2022-06-23 thomas
3862 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3863 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3864 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3865 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3866 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3867 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3868 c156c7a4 2020-12-18 stsp goto done;
3869 a1fbf39a 2019-08-11 stsp if (worktree)
3870 6962eb72 2020-02-20 stsp repo_path =
3871 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3872 a1fbf39a 2019-08-11 stsp else
3873 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3874 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3875 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3876 c156c7a4 2020-12-18 stsp goto done;
3877 c156c7a4 2020-12-18 stsp }
3878 ecb28ae0 2018-07-16 stsp }
3879 ecb28ae0 2018-07-16 stsp
3880 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3881 80ddbec8 2018-04-29 stsp if (error != NULL)
3882 ecb28ae0 2018-07-16 stsp goto done;
3883 80ddbec8 2018-04-29 stsp
3884 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3885 f135c941 2020-02-20 stsp repo, worktree);
3886 f135c941 2020-02-20 stsp if (error)
3887 f135c941 2020-02-20 stsp goto done;
3888 f135c941 2020-02-20 stsp
3889 f135c941 2020-02-20 stsp init_curses();
3890 f135c941 2020-02-20 stsp
3891 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3892 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3893 c02c541e 2019-03-29 stsp if (error)
3894 c02c541e 2019-03-29 stsp goto done;
3895 c02c541e 2019-03-29 stsp
3896 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3897 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3898 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3899 87670572 2020-12-26 naddy if (error)
3900 87670572 2020-12-26 naddy goto done;
3901 87670572 2020-12-26 naddy }
3902 51a10b52 2020-12-26 stsp
3903 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3904 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3905 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3906 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3907 d8f38dc4 2020-12-05 stsp if (error)
3908 d8f38dc4 2020-12-05 stsp goto done;
3909 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3910 d8f38dc4 2020-12-05 stsp } else {
3911 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3912 d8f38dc4 2020-12-05 stsp if (error == NULL)
3913 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3914 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3915 d8f38dc4 2020-12-05 stsp goto done;
3916 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3917 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3918 d8f38dc4 2020-12-05 stsp if (error)
3919 d8f38dc4 2020-12-05 stsp goto done;
3920 d8f38dc4 2020-12-05 stsp }
3921 8b473291 2019-02-21 stsp
3922 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3923 04cc582a 2018-08-01 stsp if (view == NULL) {
3924 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3925 04cc582a 2018-08-01 stsp goto done;
3926 04cc582a 2018-08-01 stsp }
3927 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3928 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3929 ba4f502b 2018-08-04 stsp if (error)
3930 ba4f502b 2018-08-04 stsp goto done;
3931 2fc00ff4 2019-08-31 stsp if (worktree) {
3932 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3933 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3934 2fc00ff4 2019-08-31 stsp worktree = NULL;
3935 2fc00ff4 2019-08-31 stsp }
3936 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3937 ecb28ae0 2018-07-16 stsp done:
3938 f135c941 2020-02-20 stsp free(in_repo_path);
3939 ecb28ae0 2018-07-16 stsp free(repo_path);
3940 ecb28ae0 2018-07-16 stsp free(cwd);
3941 899d86c2 2018-05-10 stsp free(start_id);
3942 d8f38dc4 2020-12-05 stsp free(label);
3943 d8f38dc4 2020-12-05 stsp if (ref)
3944 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3945 1d0f4054 2021-06-17 stsp if (repo) {
3946 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3947 1d0f4054 2021-06-17 stsp if (error == NULL)
3948 1d0f4054 2021-06-17 stsp error = close_err;
3949 1d0f4054 2021-06-17 stsp }
3950 ec142235 2019-03-07 stsp if (worktree)
3951 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3952 7cd52833 2022-06-23 thomas if (pack_fds) {
3953 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3954 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3955 7cd52833 2022-06-23 thomas if (error == NULL)
3956 7cd52833 2022-06-23 thomas error = pack_err;
3957 7cd52833 2022-06-23 thomas }
3958 51a10b52 2020-12-26 stsp tog_free_refs();
3959 80ddbec8 2018-04-29 stsp return error;
3960 9f7d7167 2018-04-29 stsp }
3961 9f7d7167 2018-04-29 stsp
3962 4ed7e80c 2018-05-20 stsp __dead static void
3963 9f7d7167 2018-04-29 stsp usage_diff(void)
3964 9f7d7167 2018-04-29 stsp {
3965 80ddbec8 2018-04-29 stsp endwin();
3966 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3967 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
3968 9f7d7167 2018-04-29 stsp exit(1);
3969 b304db33 2018-05-20 stsp }
3970 b304db33 2018-05-20 stsp
3971 6d17833f 2019-11-08 stsp static int
3972 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3973 41605754 2020-11-12 stsp regmatch_t *regmatch)
3974 6d17833f 2019-11-08 stsp {
3975 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3976 6d17833f 2019-11-08 stsp }
3977 6d17833f 2019-11-08 stsp
3978 ef20f542 2022-06-26 thomas static struct tog_color *
3979 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3980 6d17833f 2019-11-08 stsp {
3981 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3982 6d17833f 2019-11-08 stsp
3983 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3984 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3985 6d17833f 2019-11-08 stsp return tc;
3986 6d17833f 2019-11-08 stsp }
3987 6d17833f 2019-11-08 stsp
3988 6d17833f 2019-11-08 stsp return NULL;
3989 6d17833f 2019-11-08 stsp }
3990 6d17833f 2019-11-08 stsp
3991 4ed7e80c 2018-05-20 stsp static const struct got_error *
3992 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3993 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3994 41605754 2020-11-12 stsp {
3995 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3996 666f7b10 2022-06-23 thomas char *exstr = NULL;
3997 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3998 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3999 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4000 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4001 41605754 2020-11-12 stsp
4002 41605754 2020-11-12 stsp *wtotal = 0;
4003 cbea3800 2022-06-23 thomas
4004 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4005 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4006 41605754 2020-11-12 stsp
4007 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4008 666f7b10 2022-06-23 thomas if (err)
4009 666f7b10 2022-06-23 thomas return err;
4010 666f7b10 2022-06-23 thomas
4011 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4012 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4013 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4014 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4015 666f7b10 2022-06-23 thomas goto done;
4016 666f7b10 2022-06-23 thomas }
4017 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4018 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4019 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4020 cbea3800 2022-06-23 thomas goto done;
4021 cbea3800 2022-06-23 thomas }
4022 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4023 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4024 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4025 cbea3800 2022-06-23 thomas goto done;
4026 cbea3800 2022-06-23 thomas }
4027 05171be4 2022-06-23 thomas
4028 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4029 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4030 cbea3800 2022-06-23 thomas col_tab_align, 1);
4031 cbea3800 2022-06-23 thomas if (err)
4032 cbea3800 2022-06-23 thomas goto done;
4033 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4034 05171be4 2022-06-23 thomas if (n) {
4035 cbea3800 2022-06-23 thomas free(wline);
4036 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4037 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4038 cbea3800 2022-06-23 thomas if (err)
4039 cbea3800 2022-06-23 thomas goto done;
4040 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4041 cbea3800 2022-06-23 thomas wlimit -= width;
4042 cbea3800 2022-06-23 thomas *wtotal += width;
4043 41605754 2020-11-12 stsp }
4044 41605754 2020-11-12 stsp
4045 41605754 2020-11-12 stsp if (wlimit > 0) {
4046 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4047 cbea3800 2022-06-23 thomas size_t wlen;
4048 cbea3800 2022-06-23 thomas
4049 cbea3800 2022-06-23 thomas free(wline);
4050 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4051 cbea3800 2022-06-23 thomas col_tab_align, 1);
4052 cbea3800 2022-06-23 thomas if (err)
4053 cbea3800 2022-06-23 thomas goto done;
4054 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4055 cbea3800 2022-06-23 thomas while (i < wlen) {
4056 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4057 cbea3800 2022-06-23 thomas if (width == -1) {
4058 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4059 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4060 cbea3800 2022-06-23 thomas goto done;
4061 cbea3800 2022-06-23 thomas }
4062 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4063 cbea3800 2022-06-23 thomas break;
4064 1c5e5faa 2022-06-23 thomas w += width;
4065 cbea3800 2022-06-23 thomas i++;
4066 41605754 2020-11-12 stsp }
4067 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4068 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4069 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4070 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4071 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4072 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4073 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4074 41605754 2020-11-12 stsp }
4075 41605754 2020-11-12 stsp }
4076 41605754 2020-11-12 stsp
4077 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4078 cbea3800 2022-06-23 thomas free(wline);
4079 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4080 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4081 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4082 cbea3800 2022-06-23 thomas col_tab_align, 1);
4083 cbea3800 2022-06-23 thomas if (err)
4084 cbea3800 2022-06-23 thomas goto done;
4085 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4086 cbea3800 2022-06-23 thomas } else {
4087 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4088 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4089 cbea3800 2022-06-23 thomas if (err)
4090 cbea3800 2022-06-23 thomas goto done;
4091 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4092 cbea3800 2022-06-23 thomas }
4093 cbea3800 2022-06-23 thomas *wtotal += width2;
4094 41605754 2020-11-12 stsp }
4095 cbea3800 2022-06-23 thomas done:
4096 05171be4 2022-06-23 thomas free(wline);
4097 666f7b10 2022-06-23 thomas free(exstr);
4098 cbea3800 2022-06-23 thomas free(seg0);
4099 cbea3800 2022-06-23 thomas free(seg1);
4100 cbea3800 2022-06-23 thomas free(seg2);
4101 cbea3800 2022-06-23 thomas return err;
4102 41605754 2020-11-12 stsp }
4103 07dd3ed3 2022-08-06 thomas
4104 07dd3ed3 2022-08-06 thomas static int
4105 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4106 07dd3ed3 2022-08-06 thomas {
4107 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4108 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4109 07dd3ed3 2022-08-06 thomas
4110 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4111 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4112 41605754 2020-11-12 stsp
4113 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4114 07dd3ed3 2022-08-06 thomas selected = first;
4115 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4116 07dd3ed3 2022-08-06 thomas f = s->f;
4117 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4118 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4119 07dd3ed3 2022-08-06 thomas
4120 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4121 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4122 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4123 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4124 07dd3ed3 2022-08-06 thomas } else
4125 07dd3ed3 2022-08-06 thomas return 0;
4126 07dd3ed3 2022-08-06 thomas
4127 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4128 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4129 07dd3ed3 2022-08-06 thomas return 0;
4130 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4131 07dd3ed3 2022-08-06 thomas rewind(f);
4132 07dd3ed3 2022-08-06 thomas *eof = 0;
4133 07dd3ed3 2022-08-06 thomas *first = 1;
4134 07dd3ed3 2022-08-06 thomas *lineno = 0;
4135 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4136 07dd3ed3 2022-08-06 thomas return 0;
4137 07dd3ed3 2022-08-06 thomas }
4138 07dd3ed3 2022-08-06 thomas
4139 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4140 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4141 07dd3ed3 2022-08-06 thomas view->gline = 0;
4142 07dd3ed3 2022-08-06 thomas
4143 07dd3ed3 2022-08-06 thomas return 1;
4144 07dd3ed3 2022-08-06 thomas }
4145 07dd3ed3 2022-08-06 thomas
4146 41605754 2020-11-12 stsp static const struct got_error *
4147 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4148 26ed57b2 2018-05-19 stsp {
4149 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4150 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4151 61e69b96 2018-05-20 stsp const struct got_error *err;
4152 82c78e96 2022-08-06 thomas int nprinted = 0;
4153 b304db33 2018-05-20 stsp char *line;
4154 826082fe 2020-12-10 stsp size_t linesize = 0;
4155 826082fe 2020-12-10 stsp ssize_t linelen;
4156 61e69b96 2018-05-20 stsp wchar_t *wline;
4157 e0b650dd 2018-05-20 stsp int width;
4158 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4159 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4160 fe621944 2020-11-10 stsp off_t line_offset;
4161 26ed57b2 2018-05-19 stsp
4162 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4163 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4164 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4165 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4166 fe621944 2020-11-10 stsp
4167 f7d12f7e 2018-08-01 stsp werase(view->window);
4168 a3404814 2018-09-02 stsp
4169 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4170 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4171 07dd3ed3 2022-08-06 thomas
4172 a3404814 2018-09-02 stsp if (header) {
4173 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4174 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4175 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4176 07dd3ed3 2022-08-06 thomas
4177 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4178 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4179 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4180 f91a2b48 2022-06-23 thomas 0, 0);
4181 135a2da0 2020-11-11 stsp free(line);
4182 135a2da0 2020-11-11 stsp if (err)
4183 a3404814 2018-09-02 stsp return err;
4184 a3404814 2018-09-02 stsp
4185 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4186 a3404814 2018-09-02 stsp wstandout(view->window);
4187 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4188 e54cc94a 2020-11-11 stsp free(wline);
4189 e54cc94a 2020-11-11 stsp wline = NULL;
4190 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4191 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4192 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4193 a3404814 2018-09-02 stsp wstandend(view->window);
4194 26ed57b2 2018-05-19 stsp
4195 a3404814 2018-09-02 stsp if (max_lines <= 1)
4196 a3404814 2018-09-02 stsp return NULL;
4197 a3404814 2018-09-02 stsp max_lines--;
4198 a3404814 2018-09-02 stsp }
4199 a3404814 2018-09-02 stsp
4200 89f1a395 2020-12-01 naddy s->eof = 0;
4201 05171be4 2022-06-23 thomas view->maxx = 0;
4202 826082fe 2020-12-10 stsp line = NULL;
4203 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4204 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4205 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4206 6568f0aa 2022-08-06 thomas
4207 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4208 826082fe 2020-12-10 stsp if (linelen == -1) {
4209 826082fe 2020-12-10 stsp if (feof(s->f)) {
4210 826082fe 2020-12-10 stsp s->eof = 1;
4211 826082fe 2020-12-10 stsp break;
4212 826082fe 2020-12-10 stsp }
4213 826082fe 2020-12-10 stsp free(line);
4214 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4215 61e69b96 2018-05-20 stsp }
4216 05171be4 2022-06-23 thomas
4217 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4218 07dd3ed3 2022-08-06 thomas continue;
4219 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4220 07dd3ed3 2022-08-06 thomas continue;
4221 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4222 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4223 07dd3ed3 2022-08-06 thomas
4224 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4225 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4226 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4227 cbea3800 2022-06-23 thomas if (err) {
4228 cbea3800 2022-06-23 thomas free(line);
4229 cbea3800 2022-06-23 thomas return err;
4230 cbea3800 2022-06-23 thomas }
4231 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4232 cbea3800 2022-06-23 thomas free(wline);
4233 cbea3800 2022-06-23 thomas wline = NULL;
4234 cbea3800 2022-06-23 thomas
4235 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4236 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4237 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4238 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4239 07dd3ed3 2022-08-06 thomas if (attr)
4240 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4241 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4242 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4243 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4244 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4245 41605754 2020-11-12 stsp if (err) {
4246 41605754 2020-11-12 stsp free(line);
4247 41605754 2020-11-12 stsp return err;
4248 41605754 2020-11-12 stsp }
4249 41605754 2020-11-12 stsp } else {
4250 f1c0ec19 2022-06-23 thomas int skip;
4251 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4252 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4253 f1c0ec19 2022-06-23 thomas if (err) {
4254 f1c0ec19 2022-06-23 thomas free(line);
4255 f1c0ec19 2022-06-23 thomas return err;
4256 f1c0ec19 2022-06-23 thomas }
4257 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4258 f1c0ec19 2022-06-23 thomas free(wline);
4259 41605754 2020-11-12 stsp wline = NULL;
4260 41605754 2020-11-12 stsp }
4261 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4262 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4263 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4264 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4265 07dd3ed3 2022-08-06 thomas } else {
4266 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4267 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4268 07dd3ed3 2022-08-06 thomas }
4269 07dd3ed3 2022-08-06 thomas if (attr)
4270 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4271 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4272 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4273 826082fe 2020-12-10 stsp }
4274 826082fe 2020-12-10 stsp free(line);
4275 fe621944 2020-11-10 stsp if (nprinted >= 1)
4276 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4277 89f1a395 2020-12-01 naddy (nprinted - 1);
4278 fe621944 2020-11-10 stsp else
4279 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4280 26ed57b2 2018-05-19 stsp
4281 a5d43cac 2022-07-01 thomas view_border(view);
4282 c3e9aa98 2019-05-13 jcs
4283 89f1a395 2020-12-01 naddy if (s->eof) {
4284 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4285 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4286 c3e9aa98 2019-05-13 jcs nprinted++;
4287 c3e9aa98 2019-05-13 jcs }
4288 c3e9aa98 2019-05-13 jcs
4289 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4290 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4291 c3e9aa98 2019-05-13 jcs if (err) {
4292 c3e9aa98 2019-05-13 jcs return err;
4293 c3e9aa98 2019-05-13 jcs }
4294 26ed57b2 2018-05-19 stsp
4295 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4296 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4297 e54cc94a 2020-11-11 stsp free(wline);
4298 e54cc94a 2020-11-11 stsp wline = NULL;
4299 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4300 c3e9aa98 2019-05-13 jcs }
4301 c3e9aa98 2019-05-13 jcs
4302 26ed57b2 2018-05-19 stsp return NULL;
4303 abd2672a 2018-12-23 stsp }
4304 abd2672a 2018-12-23 stsp
4305 abd2672a 2018-12-23 stsp static char *
4306 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4307 abd2672a 2018-12-23 stsp {
4308 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4309 09867e48 2019-08-13 stsp char *p, *s;
4310 09867e48 2019-08-13 stsp
4311 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4312 09867e48 2019-08-13 stsp if (tm == NULL)
4313 09867e48 2019-08-13 stsp return NULL;
4314 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4315 09867e48 2019-08-13 stsp if (s == NULL)
4316 09867e48 2019-08-13 stsp return NULL;
4317 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4318 abd2672a 2018-12-23 stsp if (p)
4319 abd2672a 2018-12-23 stsp *p = '\0';
4320 abd2672a 2018-12-23 stsp return s;
4321 9f7d7167 2018-04-29 stsp }
4322 9f7d7167 2018-04-29 stsp
4323 4ed7e80c 2018-05-20 stsp static const struct got_error *
4324 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4325 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4326 0208f208 2020-05-05 stsp {
4327 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4328 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4329 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4330 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4331 0208f208 2020-05-05 stsp
4332 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4333 0208f208 2020-05-05 stsp if (qid != NULL) {
4334 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4335 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4336 ec242592 2022-04-22 thomas &qid->id);
4337 0208f208 2020-05-05 stsp if (err)
4338 0208f208 2020-05-05 stsp return err;
4339 0208f208 2020-05-05 stsp
4340 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4341 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4342 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4343 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4344 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4345 aa8b5dd0 2021-08-01 stsp }
4346 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4347 0208f208 2020-05-05 stsp
4348 0208f208 2020-05-05 stsp }
4349 0208f208 2020-05-05 stsp
4350 0208f208 2020-05-05 stsp if (tree_id1) {
4351 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4352 0208f208 2020-05-05 stsp if (err)
4353 0208f208 2020-05-05 stsp goto done;
4354 0208f208 2020-05-05 stsp }
4355 0208f208 2020-05-05 stsp
4356 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4357 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4358 0208f208 2020-05-05 stsp if (err)
4359 0208f208 2020-05-05 stsp goto done;
4360 0208f208 2020-05-05 stsp
4361 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4362 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4363 0208f208 2020-05-05 stsp done:
4364 0208f208 2020-05-05 stsp if (tree1)
4365 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4366 0208f208 2020-05-05 stsp if (tree2)
4367 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4368 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4369 0208f208 2020-05-05 stsp return err;
4370 0208f208 2020-05-05 stsp }
4371 0208f208 2020-05-05 stsp
4372 0208f208 2020-05-05 stsp static const struct got_error *
4373 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4374 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4375 abd2672a 2018-12-23 stsp {
4376 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4377 fe621944 2020-11-10 stsp
4378 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4379 fe621944 2020-11-10 stsp if (p == NULL)
4380 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4381 82c78e96 2022-08-06 thomas *lines = p;
4382 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4383 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4384 fe621944 2020-11-10 stsp (*nlines)++;
4385 82c78e96 2022-08-06 thomas
4386 fe621944 2020-11-10 stsp return NULL;
4387 fe621944 2020-11-10 stsp }
4388 fe621944 2020-11-10 stsp
4389 fe621944 2020-11-10 stsp static const struct got_error *
4390 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4391 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4392 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4393 fe621944 2020-11-10 stsp {
4394 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4395 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4396 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4397 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4398 45d799e2 2018-12-23 stsp time_t committer_time;
4399 45d799e2 2018-12-23 stsp const char *author, *committer;
4400 8b473291 2019-02-21 stsp char *refs_str = NULL;
4401 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4402 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4403 fe621944 2020-11-10 stsp off_t outoff = 0;
4404 fe621944 2020-11-10 stsp int n;
4405 abd2672a 2018-12-23 stsp
4406 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4407 0208f208 2020-05-05 stsp
4408 8b473291 2019-02-21 stsp if (refs) {
4409 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4410 8b473291 2019-02-21 stsp if (err)
4411 8b473291 2019-02-21 stsp return err;
4412 8b473291 2019-02-21 stsp }
4413 8b473291 2019-02-21 stsp
4414 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4415 abd2672a 2018-12-23 stsp if (err)
4416 abd2672a 2018-12-23 stsp return err;
4417 abd2672a 2018-12-23 stsp
4418 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4419 15a94983 2018-12-23 stsp if (err) {
4420 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4421 15a94983 2018-12-23 stsp goto done;
4422 15a94983 2018-12-23 stsp }
4423 abd2672a 2018-12-23 stsp
4424 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4425 fe621944 2020-11-10 stsp if (err)
4426 fe621944 2020-11-10 stsp goto done;
4427 fe621944 2020-11-10 stsp
4428 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4429 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4430 fe621944 2020-11-10 stsp if (n < 0) {
4431 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4432 abd2672a 2018-12-23 stsp goto done;
4433 abd2672a 2018-12-23 stsp }
4434 fe621944 2020-11-10 stsp outoff += n;
4435 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4436 fe621944 2020-11-10 stsp if (err)
4437 fe621944 2020-11-10 stsp goto done;
4438 fe621944 2020-11-10 stsp
4439 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4440 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4441 fe621944 2020-11-10 stsp if (n < 0) {
4442 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4443 abd2672a 2018-12-23 stsp goto done;
4444 abd2672a 2018-12-23 stsp }
4445 fe621944 2020-11-10 stsp outoff += n;
4446 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4447 fe621944 2020-11-10 stsp if (err)
4448 fe621944 2020-11-10 stsp goto done;
4449 fe621944 2020-11-10 stsp
4450 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4451 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4452 fe621944 2020-11-10 stsp if (datestr) {
4453 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4454 fe621944 2020-11-10 stsp if (n < 0) {
4455 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4456 fe621944 2020-11-10 stsp goto done;
4457 fe621944 2020-11-10 stsp }
4458 fe621944 2020-11-10 stsp outoff += n;
4459 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4460 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4461 fe621944 2020-11-10 stsp if (err)
4462 fe621944 2020-11-10 stsp goto done;
4463 abd2672a 2018-12-23 stsp }
4464 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4465 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4466 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4467 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4468 fe621944 2020-11-10 stsp if (n < 0) {
4469 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4470 fe621944 2020-11-10 stsp goto done;
4471 fe621944 2020-11-10 stsp }
4472 fe621944 2020-11-10 stsp outoff += n;
4473 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4474 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_AUTHOR);
4475 fe621944 2020-11-10 stsp if (err)
4476 fe621944 2020-11-10 stsp goto done;
4477 abd2672a 2018-12-23 stsp }
4478 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4479 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4480 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4481 ac4dc263 2021-09-24 thomas int pn = 1;
4482 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4483 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4484 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4485 ac4dc263 2021-09-24 thomas if (err)
4486 ac4dc263 2021-09-24 thomas goto done;
4487 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4488 ac4dc263 2021-09-24 thomas if (n < 0) {
4489 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4490 ac4dc263 2021-09-24 thomas goto done;
4491 ac4dc263 2021-09-24 thomas }
4492 ac4dc263 2021-09-24 thomas outoff += n;
4493 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4494 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4495 ac4dc263 2021-09-24 thomas if (err)
4496 ac4dc263 2021-09-24 thomas goto done;
4497 ac4dc263 2021-09-24 thomas free(id_str);
4498 ac4dc263 2021-09-24 thomas id_str = NULL;
4499 ac4dc263 2021-09-24 thomas }
4500 ac4dc263 2021-09-24 thomas }
4501 ac4dc263 2021-09-24 thomas
4502 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4503 5943eee2 2019-08-13 stsp if (err)
4504 5943eee2 2019-08-13 stsp goto done;
4505 fe621944 2020-11-10 stsp s = logmsg;
4506 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4507 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4508 fe621944 2020-11-10 stsp if (n < 0) {
4509 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4510 fe621944 2020-11-10 stsp goto done;
4511 fe621944 2020-11-10 stsp }
4512 fe621944 2020-11-10 stsp outoff += n;
4513 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4514 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4515 fe621944 2020-11-10 stsp if (err)
4516 fe621944 2020-11-10 stsp goto done;
4517 abd2672a 2018-12-23 stsp }
4518 fe621944 2020-11-10 stsp
4519 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4520 0208f208 2020-05-05 stsp if (err)
4521 0208f208 2020-05-05 stsp goto done;
4522 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4523 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4524 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4525 fe621944 2020-11-10 stsp if (n < 0) {
4526 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4527 fe621944 2020-11-10 stsp goto done;
4528 fe621944 2020-11-10 stsp }
4529 fe621944 2020-11-10 stsp outoff += n;
4530 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4531 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4532 fe621944 2020-11-10 stsp if (err)
4533 fe621944 2020-11-10 stsp goto done;
4534 0208f208 2020-05-05 stsp free((char *)pe->path);
4535 0208f208 2020-05-05 stsp free(pe->data);
4536 0208f208 2020-05-05 stsp }
4537 fe621944 2020-11-10 stsp
4538 0208f208 2020-05-05 stsp fputc('\n', outfile);
4539 fe621944 2020-11-10 stsp outoff++;
4540 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4541 abd2672a 2018-12-23 stsp done:
4542 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4543 abd2672a 2018-12-23 stsp free(id_str);
4544 5943eee2 2019-08-13 stsp free(logmsg);
4545 8b473291 2019-02-21 stsp free(refs_str);
4546 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4547 7510f233 2020-08-09 stsp if (err) {
4548 82c78e96 2022-08-06 thomas free(*lines);
4549 82c78e96 2022-08-06 thomas *lines = NULL;
4550 ae6a6978 2020-08-09 stsp *nlines = 0;
4551 7510f233 2020-08-09 stsp }
4552 fe621944 2020-11-10 stsp return err;
4553 abd2672a 2018-12-23 stsp }
4554 abd2672a 2018-12-23 stsp
4555 abd2672a 2018-12-23 stsp static const struct got_error *
4556 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4557 26ed57b2 2018-05-19 stsp {
4558 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4559 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4560 15a94983 2018-12-23 stsp int obj_type;
4561 fe621944 2020-11-10 stsp
4562 82c78e96 2022-08-06 thomas free(s->lines);
4563 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4564 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4565 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4566 fe621944 2020-11-10 stsp s->nlines = 0;
4567 26ed57b2 2018-05-19 stsp
4568 511a516b 2018-05-19 stsp f = got_opentemp();
4569 48ae06ee 2018-10-18 stsp if (f == NULL) {
4570 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4571 48ae06ee 2018-10-18 stsp goto done;
4572 48ae06ee 2018-10-18 stsp }
4573 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4574 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4575 fb43ecf1 2019-02-11 stsp goto done;
4576 fb43ecf1 2019-02-11 stsp }
4577 48ae06ee 2018-10-18 stsp s->f = f;
4578 26ed57b2 2018-05-19 stsp
4579 15a94983 2018-12-23 stsp if (s->id1)
4580 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4581 15a94983 2018-12-23 stsp else
4582 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4583 15a94983 2018-12-23 stsp if (err)
4584 15a94983 2018-12-23 stsp goto done;
4585 15a94983 2018-12-23 stsp
4586 15a94983 2018-12-23 stsp switch (obj_type) {
4587 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4588 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4589 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4590 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4591 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4592 26ed57b2 2018-05-19 stsp break;
4593 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4594 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4595 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4596 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4597 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4598 26ed57b2 2018-05-19 stsp break;
4599 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4600 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4601 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4602 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4603 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4604 abd2672a 2018-12-23 stsp
4605 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4606 abd2672a 2018-12-23 stsp if (err)
4607 3ffacbe1 2020-02-02 tracey goto done;
4608 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4609 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4610 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4611 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4612 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4613 f44b1f58 2020-02-02 tracey if (err)
4614 f44b1f58 2020-02-02 tracey goto done;
4615 f44b1f58 2020-02-02 tracey } else {
4616 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4617 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4618 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4619 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4620 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4621 82c78e96 2022-08-06 thomas s->f);
4622 f44b1f58 2020-02-02 tracey if (err)
4623 f44b1f58 2020-02-02 tracey goto done;
4624 f5404e4e 2020-02-02 tracey break;
4625 15a087fe 2019-02-21 stsp }
4626 abd2672a 2018-12-23 stsp }
4627 abd2672a 2018-12-23 stsp }
4628 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4629 abd2672a 2018-12-23 stsp
4630 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4631 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4632 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4633 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4634 26ed57b2 2018-05-19 stsp break;
4635 abd2672a 2018-12-23 stsp }
4636 26ed57b2 2018-05-19 stsp default:
4637 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4638 48ae06ee 2018-10-18 stsp break;
4639 26ed57b2 2018-05-19 stsp }
4640 48ae06ee 2018-10-18 stsp done:
4641 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4642 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4643 48ae06ee 2018-10-18 stsp return err;
4644 48ae06ee 2018-10-18 stsp }
4645 26ed57b2 2018-05-19 stsp
4646 f5215bb9 2019-02-22 stsp static void
4647 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4648 f5215bb9 2019-02-22 stsp {
4649 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4650 f5215bb9 2019-02-22 stsp update_panels();
4651 f5215bb9 2019-02-22 stsp doupdate();
4652 f44b1f58 2020-02-02 tracey }
4653 f44b1f58 2020-02-02 tracey
4654 f44b1f58 2020-02-02 tracey static const struct got_error *
4655 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4656 f44b1f58 2020-02-02 tracey {
4657 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4658 f44b1f58 2020-02-02 tracey
4659 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4660 f44b1f58 2020-02-02 tracey return NULL;
4661 f44b1f58 2020-02-02 tracey }
4662 f44b1f58 2020-02-02 tracey
4663 f44b1f58 2020-02-02 tracey static const struct got_error *
4664 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4665 f44b1f58 2020-02-02 tracey {
4666 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4667 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4668 f44b1f58 2020-02-02 tracey int lineno;
4669 78eecffc 2022-06-23 thomas char *line = NULL;
4670 826082fe 2020-12-10 stsp size_t linesize = 0;
4671 826082fe 2020-12-10 stsp ssize_t linelen;
4672 f44b1f58 2020-02-02 tracey
4673 f44b1f58 2020-02-02 tracey if (!view->searching) {
4674 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4675 f44b1f58 2020-02-02 tracey return NULL;
4676 f44b1f58 2020-02-02 tracey }
4677 f44b1f58 2020-02-02 tracey
4678 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4679 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4680 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4681 f44b1f58 2020-02-02 tracey else
4682 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4683 4b3f9dac 2021-12-17 thomas } else
4684 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4685 f44b1f58 2020-02-02 tracey
4686 f44b1f58 2020-02-02 tracey while (1) {
4687 f44b1f58 2020-02-02 tracey off_t offset;
4688 f44b1f58 2020-02-02 tracey
4689 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4690 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4691 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4692 f44b1f58 2020-02-02 tracey break;
4693 f44b1f58 2020-02-02 tracey }
4694 f44b1f58 2020-02-02 tracey
4695 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4696 f44b1f58 2020-02-02 tracey lineno = 1;
4697 f44b1f58 2020-02-02 tracey else
4698 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4699 f44b1f58 2020-02-02 tracey }
4700 f44b1f58 2020-02-02 tracey
4701 82c78e96 2022-08-06 thomas offset = s->lines[lineno - 1].offset;
4702 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4703 f44b1f58 2020-02-02 tracey free(line);
4704 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4705 f44b1f58 2020-02-02 tracey }
4706 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4707 78eecffc 2022-06-23 thomas if (linelen != -1) {
4708 78eecffc 2022-06-23 thomas char *exstr;
4709 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4710 78eecffc 2022-06-23 thomas if (err)
4711 78eecffc 2022-06-23 thomas break;
4712 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4713 78eecffc 2022-06-23 thomas &view->regmatch)) {
4714 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4715 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4716 78eecffc 2022-06-23 thomas free(exstr);
4717 78eecffc 2022-06-23 thomas break;
4718 78eecffc 2022-06-23 thomas }
4719 78eecffc 2022-06-23 thomas free(exstr);
4720 f44b1f58 2020-02-02 tracey }
4721 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4722 f44b1f58 2020-02-02 tracey lineno++;
4723 f44b1f58 2020-02-02 tracey else
4724 f44b1f58 2020-02-02 tracey lineno--;
4725 f44b1f58 2020-02-02 tracey }
4726 826082fe 2020-12-10 stsp free(line);
4727 f44b1f58 2020-02-02 tracey
4728 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4729 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4730 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4731 f44b1f58 2020-02-02 tracey }
4732 f44b1f58 2020-02-02 tracey
4733 05171be4 2022-06-23 thomas return err;
4734 f5215bb9 2019-02-22 stsp }
4735 f5215bb9 2019-02-22 stsp
4736 48ae06ee 2018-10-18 stsp static const struct got_error *
4737 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4738 a0f32f33 2022-06-13 thomas {
4739 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4740 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4741 a0f32f33 2022-06-13 thomas
4742 a0f32f33 2022-06-13 thomas free(s->id1);
4743 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4744 a0f32f33 2022-06-13 thomas free(s->id2);
4745 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4746 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4747 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4748 a0f32f33 2022-06-13 thomas s->f = NULL;
4749 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4750 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4751 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4752 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4753 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4754 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4755 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4756 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4757 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4758 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4759 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4760 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4761 82c78e96 2022-08-06 thomas free(s->lines);
4762 82c78e96 2022-08-06 thomas s->lines = NULL;
4763 a0f32f33 2022-06-13 thomas s->nlines = 0;
4764 a0f32f33 2022-06-13 thomas return err;
4765 a0f32f33 2022-06-13 thomas }
4766 a0f32f33 2022-06-13 thomas
4767 a0f32f33 2022-06-13 thomas static const struct got_error *
4768 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4769 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4770 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4771 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4772 48ae06ee 2018-10-18 stsp {
4773 48ae06ee 2018-10-18 stsp const struct got_error *err;
4774 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4775 5dc9f4bc 2018-08-04 stsp
4776 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4777 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4778 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4779 a0f32f33 2022-06-13 thomas
4780 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4781 15a94983 2018-12-23 stsp int type1, type2;
4782 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4783 15a94983 2018-12-23 stsp if (err)
4784 15a94983 2018-12-23 stsp return err;
4785 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4786 15a94983 2018-12-23 stsp if (err)
4787 15a94983 2018-12-23 stsp return err;
4788 15a94983 2018-12-23 stsp
4789 15a94983 2018-12-23 stsp if (type1 != type2)
4790 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4791 15a94983 2018-12-23 stsp }
4792 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4793 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4794 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4795 f44b1f58 2020-02-02 tracey s->repo = repo;
4796 f44b1f58 2020-02-02 tracey s->id1 = id1;
4797 f44b1f58 2020-02-02 tracey s->id2 = id2;
4798 3dbaef42 2020-11-24 stsp s->label1 = label1;
4799 3dbaef42 2020-11-24 stsp s->label2 = label2;
4800 48ae06ee 2018-10-18 stsp
4801 15a94983 2018-12-23 stsp if (id1) {
4802 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4803 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4804 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4805 48ae06ee 2018-10-18 stsp } else
4806 5465d566 2020-02-01 tracey s->id1 = NULL;
4807 48ae06ee 2018-10-18 stsp
4808 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4809 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4810 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4811 a0f32f33 2022-06-13 thomas goto done;
4812 48ae06ee 2018-10-18 stsp }
4813 a0f32f33 2022-06-13 thomas
4814 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4815 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4816 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4817 9a1d7435 2022-06-23 thomas goto done;
4818 9a1d7435 2022-06-23 thomas }
4819 9a1d7435 2022-06-23 thomas
4820 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4821 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4822 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4823 a0f32f33 2022-06-13 thomas goto done;
4824 a0f32f33 2022-06-13 thomas }
4825 a0f32f33 2022-06-13 thomas
4826 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4827 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4828 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4829 19a6a6b5 2022-07-01 thomas goto done;
4830 19a6a6b5 2022-07-01 thomas }
4831 19a6a6b5 2022-07-01 thomas
4832 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4833 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4834 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4835 19a6a6b5 2022-07-01 thomas goto done;
4836 19a6a6b5 2022-07-01 thomas }
4837 19a6a6b5 2022-07-01 thomas
4838 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4839 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4840 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4841 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4842 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4843 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4844 5465d566 2020-02-01 tracey s->repo = repo;
4845 6d17833f 2019-11-08 stsp
4846 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4847 6568f0aa 2022-08-06 thomas int rc;
4848 6d17833f 2019-11-08 stsp
4849 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
4850 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4851 6568f0aa 2022-08-06 thomas if (rc != ERR)
4852 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
4853 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4854 6568f0aa 2022-08-06 thomas if (rc != ERR)
4855 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
4856 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4857 6568f0aa 2022-08-06 thomas if (rc != ERR)
4858 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
4859 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4860 6568f0aa 2022-08-06 thomas if (rc != ERR)
4861 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
4862 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4863 6568f0aa 2022-08-06 thomas if (rc != ERR)
4864 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4865 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4866 6568f0aa 2022-08-06 thomas if (rc != ERR)
4867 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4868 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4869 6568f0aa 2022-08-06 thomas if (rc != ERR)
4870 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4871 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
4872 6568f0aa 2022-08-06 thomas if (rc != ERR)
4873 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
4874 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
4875 6568f0aa 2022-08-06 thomas if (rc == ERR) {
4876 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
4877 a0f32f33 2022-06-13 thomas goto done;
4878 6568f0aa 2022-08-06 thomas }
4879 6d17833f 2019-11-08 stsp }
4880 5dc9f4bc 2018-08-04 stsp
4881 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4882 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4883 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4884 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4885 f5215bb9 2019-02-22 stsp
4886 5465d566 2020-02-01 tracey err = create_diff(s);
4887 48ae06ee 2018-10-18 stsp
4888 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4889 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4890 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4891 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4892 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4893 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4894 a0f32f33 2022-06-13 thomas done:
4895 a0f32f33 2022-06-13 thomas if (err)
4896 a0f32f33 2022-06-13 thomas close_diff_view(view);
4897 e5a0f69f 2018-08-18 stsp return err;
4898 5dc9f4bc 2018-08-04 stsp }
4899 5dc9f4bc 2018-08-04 stsp
4900 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4901 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4902 5dc9f4bc 2018-08-04 stsp {
4903 a3404814 2018-09-02 stsp const struct got_error *err;
4904 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4905 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4906 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4907 a3404814 2018-09-02 stsp
4908 a3404814 2018-09-02 stsp if (s->id1) {
4909 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4910 a3404814 2018-09-02 stsp if (err)
4911 a3404814 2018-09-02 stsp return err;
4912 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
4913 3dbaef42 2020-11-24 stsp } else
4914 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4915 3dbaef42 2020-11-24 stsp
4916 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4917 a3404814 2018-09-02 stsp if (err)
4918 a3404814 2018-09-02 stsp return err;
4919 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
4920 26ed57b2 2018-05-19 stsp
4921 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4922 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4923 a3404814 2018-09-02 stsp free(id_str1);
4924 a3404814 2018-09-02 stsp free(id_str2);
4925 a3404814 2018-09-02 stsp return err;
4926 a3404814 2018-09-02 stsp }
4927 a3404814 2018-09-02 stsp free(id_str1);
4928 a3404814 2018-09-02 stsp free(id_str2);
4929 a3404814 2018-09-02 stsp
4930 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4931 267bb3b8 2021-08-01 stsp free(header);
4932 267bb3b8 2021-08-01 stsp return err;
4933 15a087fe 2019-02-21 stsp }
4934 15a087fe 2019-02-21 stsp
4935 15a087fe 2019-02-21 stsp static const struct got_error *
4936 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4937 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4938 15a087fe 2019-02-21 stsp {
4939 d7a04538 2019-02-21 stsp const struct got_error *err;
4940 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4941 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4942 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4943 15a087fe 2019-02-21 stsp
4944 15a087fe 2019-02-21 stsp free(s->id2);
4945 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4946 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4947 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4948 15a087fe 2019-02-21 stsp
4949 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4950 d7a04538 2019-02-21 stsp if (err)
4951 d7a04538 2019-02-21 stsp return err;
4952 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4953 15a087fe 2019-02-21 stsp free(s->id1);
4954 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4955 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4956 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4957 15a087fe 2019-02-21 stsp return NULL;
4958 0cf4efb1 2018-09-29 stsp }
4959 0cf4efb1 2018-09-29 stsp
4960 0cf4efb1 2018-09-29 stsp static const struct got_error *
4961 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4962 adf4c9e0 2022-07-03 thomas {
4963 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4964 adf4c9e0 2022-07-03 thomas
4965 adf4c9e0 2022-07-03 thomas view->count = 0;
4966 adf4c9e0 2022-07-03 thomas wclear(view->window);
4967 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4968 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4969 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4970 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4971 adf4c9e0 2022-07-03 thomas return create_diff(s);
4972 82c78e96 2022-08-06 thomas }
4973 82c78e96 2022-08-06 thomas
4974 82c78e96 2022-08-06 thomas static void
4975 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4976 82c78e96 2022-08-06 thomas {
4977 82c78e96 2022-08-06 thomas int start, i;
4978 82c78e96 2022-08-06 thomas
4979 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
4980 82c78e96 2022-08-06 thomas
4981 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4982 82c78e96 2022-08-06 thomas if (i == 0)
4983 82c78e96 2022-08-06 thomas i = s->nlines - 1;
4984 82c78e96 2022-08-06 thomas if (--i == start)
4985 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4986 82c78e96 2022-08-06 thomas }
4987 82c78e96 2022-08-06 thomas
4988 82c78e96 2022-08-06 thomas s->selected_line = 1;
4989 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4990 adf4c9e0 2022-07-03 thomas }
4991 adf4c9e0 2022-07-03 thomas
4992 82c78e96 2022-08-06 thomas static void
4993 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4994 82c78e96 2022-08-06 thomas {
4995 82c78e96 2022-08-06 thomas int start, i;
4996 82c78e96 2022-08-06 thomas
4997 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
4998 82c78e96 2022-08-06 thomas
4999 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5000 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5001 82c78e96 2022-08-06 thomas i = 0;
5002 82c78e96 2022-08-06 thomas if (++i == start)
5003 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5004 82c78e96 2022-08-06 thomas }
5005 82c78e96 2022-08-06 thomas
5006 82c78e96 2022-08-06 thomas s->selected_line = 1;
5007 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5008 82c78e96 2022-08-06 thomas }
5009 82c78e96 2022-08-06 thomas
5010 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5011 4fc71f3b 2022-07-12 thomas int, int, int);
5012 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5013 4fc71f3b 2022-07-12 thomas int, int);
5014 4fc71f3b 2022-07-12 thomas
5015 adf4c9e0 2022-07-03 thomas static const struct got_error *
5016 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5017 e5a0f69f 2018-08-18 stsp {
5018 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5019 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5020 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5021 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5022 826082fe 2020-12-10 stsp char *line = NULL;
5023 826082fe 2020-12-10 stsp size_t linesize = 0;
5024 826082fe 2020-12-10 stsp ssize_t linelen;
5025 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5026 e5a0f69f 2018-08-18 stsp
5027 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5028 82c78e96 2022-08-06 thomas
5029 e5a0f69f 2018-08-18 stsp switch (ch) {
5030 05171be4 2022-06-23 thomas case '0':
5031 05171be4 2022-06-23 thomas view->x = 0;
5032 05171be4 2022-06-23 thomas break;
5033 05171be4 2022-06-23 thomas case '$':
5034 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5035 07b0611c 2022-06-23 thomas view->count = 0;
5036 05171be4 2022-06-23 thomas break;
5037 05171be4 2022-06-23 thomas case KEY_RIGHT:
5038 05171be4 2022-06-23 thomas case 'l':
5039 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5040 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5041 07b0611c 2022-06-23 thomas else
5042 07b0611c 2022-06-23 thomas view->count = 0;
5043 05171be4 2022-06-23 thomas break;
5044 05171be4 2022-06-23 thomas case KEY_LEFT:
5045 05171be4 2022-06-23 thomas case 'h':
5046 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5047 07b0611c 2022-06-23 thomas if (view->x <= 0)
5048 07b0611c 2022-06-23 thomas view->count = 0;
5049 05171be4 2022-06-23 thomas break;
5050 64453f7e 2020-11-21 stsp case 'a':
5051 3dbaef42 2020-11-24 stsp case 'w':
5052 3dbaef42 2020-11-24 stsp if (ch == 'a')
5053 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5054 3dbaef42 2020-11-24 stsp if (ch == 'w')
5055 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5056 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5057 912a3f79 2021-08-30 j break;
5058 912a3f79 2021-08-30 j case 'g':
5059 912a3f79 2021-08-30 j case KEY_HOME:
5060 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5061 07b0611c 2022-06-23 thomas view->count = 0;
5062 64453f7e 2020-11-21 stsp break;
5063 912a3f79 2021-08-30 j case 'G':
5064 912a3f79 2021-08-30 j case KEY_END:
5065 07b0611c 2022-06-23 thomas view->count = 0;
5066 912a3f79 2021-08-30 j if (s->eof)
5067 912a3f79 2021-08-30 j break;
5068 912a3f79 2021-08-30 j
5069 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5070 912a3f79 2021-08-30 j s->eof = 1;
5071 912a3f79 2021-08-30 j break;
5072 1e37a5c2 2019-05-12 jcs case 'k':
5073 1e37a5c2 2019-05-12 jcs case KEY_UP:
5074 f7140bf5 2021-10-17 thomas case CTRL('p'):
5075 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5076 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5077 07b0611c 2022-06-23 thomas else
5078 07b0611c 2022-06-23 thomas view->count = 0;
5079 1e37a5c2 2019-05-12 jcs break;
5080 70f17a53 2022-06-13 thomas case CTRL('u'):
5081 23427b14 2022-06-23 thomas case 'u':
5082 70f17a53 2022-06-13 thomas nscroll /= 2;
5083 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5084 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5085 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5086 1c5e5faa 2022-06-23 thomas case 'b':
5087 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5088 07b0611c 2022-06-23 thomas view->count = 0;
5089 26ed57b2 2018-05-19 stsp break;
5090 07b0611c 2022-06-23 thomas }
5091 1e37a5c2 2019-05-12 jcs i = 0;
5092 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5093 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5094 1e37a5c2 2019-05-12 jcs break;
5095 1e37a5c2 2019-05-12 jcs case 'j':
5096 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5097 f7140bf5 2021-10-17 thomas case CTRL('n'):
5098 1e37a5c2 2019-05-12 jcs if (!s->eof)
5099 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5100 07b0611c 2022-06-23 thomas else
5101 07b0611c 2022-06-23 thomas view->count = 0;
5102 1e37a5c2 2019-05-12 jcs break;
5103 70f17a53 2022-06-13 thomas case CTRL('d'):
5104 23427b14 2022-06-23 thomas case 'd':
5105 70f17a53 2022-06-13 thomas nscroll /= 2;
5106 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5107 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5108 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5109 1c5e5faa 2022-06-23 thomas case 'f':
5110 1e37a5c2 2019-05-12 jcs case ' ':
5111 07b0611c 2022-06-23 thomas if (s->eof) {
5112 07b0611c 2022-06-23 thomas view->count = 0;
5113 1e37a5c2 2019-05-12 jcs break;
5114 07b0611c 2022-06-23 thomas }
5115 1e37a5c2 2019-05-12 jcs i = 0;
5116 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5117 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5118 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5119 826082fe 2020-12-10 stsp if (linelen == -1) {
5120 826082fe 2020-12-10 stsp if (feof(s->f)) {
5121 826082fe 2020-12-10 stsp s->eof = 1;
5122 826082fe 2020-12-10 stsp } else
5123 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5124 34bc9ec9 2019-02-22 stsp break;
5125 826082fe 2020-12-10 stsp }
5126 1e37a5c2 2019-05-12 jcs }
5127 826082fe 2020-12-10 stsp free(line);
5128 1e37a5c2 2019-05-12 jcs break;
5129 82c78e96 2022-08-06 thomas case '(':
5130 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5131 82c78e96 2022-08-06 thomas break;
5132 82c78e96 2022-08-06 thomas case ')':
5133 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5134 82c78e96 2022-08-06 thomas break;
5135 82c78e96 2022-08-06 thomas case '{':
5136 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5137 82c78e96 2022-08-06 thomas break;
5138 82c78e96 2022-08-06 thomas case '}':
5139 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5140 82c78e96 2022-08-06 thomas break;
5141 1e37a5c2 2019-05-12 jcs case '[':
5142 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5143 1e37a5c2 2019-05-12 jcs s->diff_context--;
5144 aa61903a 2021-12-31 thomas s->matched_line = 0;
5145 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5146 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5147 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5148 27829c9e 2020-11-21 stsp s->nlines) {
5149 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5150 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5151 27829c9e 2020-11-21 stsp }
5152 07b0611c 2022-06-23 thomas } else
5153 07b0611c 2022-06-23 thomas view->count = 0;
5154 1e37a5c2 2019-05-12 jcs break;
5155 1e37a5c2 2019-05-12 jcs case ']':
5156 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5157 1e37a5c2 2019-05-12 jcs s->diff_context++;
5158 aa61903a 2021-12-31 thomas s->matched_line = 0;
5159 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5160 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5161 07b0611c 2022-06-23 thomas } else
5162 07b0611c 2022-06-23 thomas view->count = 0;
5163 1e37a5c2 2019-05-12 jcs break;
5164 1e37a5c2 2019-05-12 jcs case '<':
5165 1e37a5c2 2019-05-12 jcs case ',':
5166 777aae21 2022-07-20 thomas case 'K':
5167 4fc71f3b 2022-07-12 thomas up = 1;
5168 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5169 4fc71f3b 2022-07-12 thomas case '>':
5170 4fc71f3b 2022-07-12 thomas case '.':
5171 777aae21 2022-07-20 thomas case 'J':
5172 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5173 07b0611c 2022-06-23 thomas view->count = 0;
5174 48ae06ee 2018-10-18 stsp break;
5175 07b0611c 2022-06-23 thomas }
5176 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5177 6524637e 2019-02-21 stsp
5178 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5179 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5180 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5181 15a087fe 2019-02-21 stsp
5182 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5183 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5184 4fc71f3b 2022-07-12 thomas if (err)
5185 4fc71f3b 2022-07-12 thomas break;
5186 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5187 15a087fe 2019-02-21 stsp
5188 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5189 4fc71f3b 2022-07-12 thomas break;
5190 15a087fe 2019-02-21 stsp
5191 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5192 4fc71f3b 2022-07-12 thomas if (err)
5193 4fc71f3b 2022-07-12 thomas break;
5194 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5195 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5196 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5197 5e224a3e 2019-02-22 stsp
5198 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5199 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5200 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5201 4fc71f3b 2022-07-12 thomas
5202 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5203 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5204 4fc71f3b 2022-07-12 thomas if (err)
5205 4fc71f3b 2022-07-12 thomas break;
5206 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5207 5a8b5076 2020-12-05 stsp
5208 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5209 4fc71f3b 2022-07-12 thomas break;
5210 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5211 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5212 4fc71f3b 2022-07-12 thomas bs->selected_line);
5213 4fc71f3b 2022-07-12 thomas if (id == NULL)
5214 4fc71f3b 2022-07-12 thomas break;
5215 15a087fe 2019-02-21 stsp
5216 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5217 4fc71f3b 2022-07-12 thomas break;
5218 15a087fe 2019-02-21 stsp
5219 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5220 4fc71f3b 2022-07-12 thomas if (err)
5221 4fc71f3b 2022-07-12 thomas break;
5222 4fc71f3b 2022-07-12 thomas }
5223 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5224 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5225 aa61903a 2021-12-31 thomas s->matched_line = 0;
5226 05171be4 2022-06-23 thomas view->x = 0;
5227 1e37a5c2 2019-05-12 jcs
5228 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5229 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5230 1e37a5c2 2019-05-12 jcs break;
5231 1e37a5c2 2019-05-12 jcs default:
5232 07b0611c 2022-06-23 thomas view->count = 0;
5233 1e37a5c2 2019-05-12 jcs break;
5234 26ed57b2 2018-05-19 stsp }
5235 e5a0f69f 2018-08-18 stsp
5236 bcbd79e2 2018-08-19 stsp return err;
5237 26ed57b2 2018-05-19 stsp }
5238 26ed57b2 2018-05-19 stsp
5239 4ed7e80c 2018-05-20 stsp static const struct got_error *
5240 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5241 9f7d7167 2018-04-29 stsp {
5242 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5243 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5244 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5245 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5246 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5247 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5248 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5249 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5250 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5251 3dbaef42 2020-11-24 stsp const char *errstr;
5252 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5253 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5254 70ac5f84 2019-03-28 stsp
5255 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5256 26ed57b2 2018-05-19 stsp switch (ch) {
5257 64453f7e 2020-11-21 stsp case 'a':
5258 64453f7e 2020-11-21 stsp force_text_diff = 1;
5259 3dbaef42 2020-11-24 stsp break;
5260 3dbaef42 2020-11-24 stsp case 'C':
5261 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5262 3dbaef42 2020-11-24 stsp &errstr);
5263 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5264 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5265 8f666e67 2022-02-12 thomas errstr, errstr);
5266 64453f7e 2020-11-21 stsp break;
5267 09b5bff8 2020-02-23 naddy case 'r':
5268 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5269 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5270 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5271 09b5bff8 2020-02-23 naddy optarg);
5272 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5273 09b5bff8 2020-02-23 naddy break;
5274 3dbaef42 2020-11-24 stsp case 'w':
5275 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5276 3dbaef42 2020-11-24 stsp break;
5277 26ed57b2 2018-05-19 stsp default:
5278 17020d27 2019-03-07 stsp usage_diff();
5279 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5280 26ed57b2 2018-05-19 stsp }
5281 26ed57b2 2018-05-19 stsp }
5282 26ed57b2 2018-05-19 stsp
5283 26ed57b2 2018-05-19 stsp argc -= optind;
5284 26ed57b2 2018-05-19 stsp argv += optind;
5285 26ed57b2 2018-05-19 stsp
5286 26ed57b2 2018-05-19 stsp if (argc == 0) {
5287 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5288 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5289 15a94983 2018-12-23 stsp id_str1 = argv[0];
5290 15a94983 2018-12-23 stsp id_str2 = argv[1];
5291 26ed57b2 2018-05-19 stsp } else
5292 26ed57b2 2018-05-19 stsp usage_diff();
5293 eb6600df 2019-01-04 stsp
5294 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5295 7cd52833 2022-06-23 thomas if (error)
5296 7cd52833 2022-06-23 thomas goto done;
5297 7cd52833 2022-06-23 thomas
5298 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5299 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5300 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5301 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5302 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5303 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5304 c156c7a4 2020-12-18 stsp goto done;
5305 a273ac94 2020-02-23 naddy if (worktree)
5306 a273ac94 2020-02-23 naddy repo_path =
5307 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5308 a273ac94 2020-02-23 naddy else
5309 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5310 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5311 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5312 c156c7a4 2020-12-18 stsp goto done;
5313 c156c7a4 2020-12-18 stsp }
5314 a273ac94 2020-02-23 naddy }
5315 a273ac94 2020-02-23 naddy
5316 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5317 eb6600df 2019-01-04 stsp if (error)
5318 eb6600df 2019-01-04 stsp goto done;
5319 26ed57b2 2018-05-19 stsp
5320 a273ac94 2020-02-23 naddy init_curses();
5321 a273ac94 2020-02-23 naddy
5322 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5323 51a10b52 2020-12-26 stsp if (error)
5324 51a10b52 2020-12-26 stsp goto done;
5325 51a10b52 2020-12-26 stsp
5326 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5327 26ed57b2 2018-05-19 stsp if (error)
5328 26ed57b2 2018-05-19 stsp goto done;
5329 26ed57b2 2018-05-19 stsp
5330 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5331 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5332 26ed57b2 2018-05-19 stsp if (error)
5333 26ed57b2 2018-05-19 stsp goto done;
5334 26ed57b2 2018-05-19 stsp
5335 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5336 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5337 26ed57b2 2018-05-19 stsp if (error)
5338 26ed57b2 2018-05-19 stsp goto done;
5339 26ed57b2 2018-05-19 stsp
5340 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5341 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5342 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5343 ea5e7bb5 2018-08-01 stsp goto done;
5344 ea5e7bb5 2018-08-01 stsp }
5345 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5346 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5347 5dc9f4bc 2018-08-04 stsp if (error)
5348 5dc9f4bc 2018-08-04 stsp goto done;
5349 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5350 26ed57b2 2018-05-19 stsp done:
5351 3dbaef42 2020-11-24 stsp free(label1);
5352 3dbaef42 2020-11-24 stsp free(label2);
5353 c02c541e 2019-03-29 stsp free(repo_path);
5354 a273ac94 2020-02-23 naddy free(cwd);
5355 1d0f4054 2021-06-17 stsp if (repo) {
5356 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5357 1d0f4054 2021-06-17 stsp if (error == NULL)
5358 1d0f4054 2021-06-17 stsp error = close_err;
5359 1d0f4054 2021-06-17 stsp }
5360 a273ac94 2020-02-23 naddy if (worktree)
5361 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5362 7cd52833 2022-06-23 thomas if (pack_fds) {
5363 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5364 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5365 7cd52833 2022-06-23 thomas if (error == NULL)
5366 7cd52833 2022-06-23 thomas error = pack_err;
5367 7cd52833 2022-06-23 thomas }
5368 51a10b52 2020-12-26 stsp tog_free_refs();
5369 26ed57b2 2018-05-19 stsp return error;
5370 9f7d7167 2018-04-29 stsp }
5371 9f7d7167 2018-04-29 stsp
5372 4ed7e80c 2018-05-20 stsp __dead static void
5373 9f7d7167 2018-04-29 stsp usage_blame(void)
5374 9f7d7167 2018-04-29 stsp {
5375 80ddbec8 2018-04-29 stsp endwin();
5376 91198554 2022-06-23 thomas fprintf(stderr,
5377 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5378 9f7d7167 2018-04-29 stsp getprogname());
5379 9f7d7167 2018-04-29 stsp exit(1);
5380 9f7d7167 2018-04-29 stsp }
5381 84451b3e 2018-07-10 stsp
5382 84451b3e 2018-07-10 stsp struct tog_blame_line {
5383 84451b3e 2018-07-10 stsp int annotated;
5384 84451b3e 2018-07-10 stsp struct got_object_id *id;
5385 84451b3e 2018-07-10 stsp };
5386 9f7d7167 2018-04-29 stsp
5387 4ed7e80c 2018-05-20 stsp static const struct got_error *
5388 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5389 84451b3e 2018-07-10 stsp {
5390 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5391 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5392 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5393 84451b3e 2018-07-10 stsp const struct got_error *err;
5394 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5395 826082fe 2020-12-10 stsp char *line = NULL;
5396 826082fe 2020-12-10 stsp size_t linesize = 0;
5397 826082fe 2020-12-10 stsp ssize_t linelen;
5398 84451b3e 2018-07-10 stsp wchar_t *wline;
5399 27a741e5 2019-09-11 stsp int width;
5400 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5401 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5402 ab089a2a 2018-07-12 stsp char *id_str;
5403 11b20872 2019-11-08 stsp struct tog_color *tc;
5404 ab089a2a 2018-07-12 stsp
5405 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5406 ab089a2a 2018-07-12 stsp if (err)
5407 ab089a2a 2018-07-12 stsp return err;
5408 84451b3e 2018-07-10 stsp
5409 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5410 f7d12f7e 2018-08-01 stsp werase(view->window);
5411 84451b3e 2018-07-10 stsp
5412 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5413 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5414 ab089a2a 2018-07-12 stsp free(id_str);
5415 ab089a2a 2018-07-12 stsp return err;
5416 ab089a2a 2018-07-12 stsp }
5417 ab089a2a 2018-07-12 stsp
5418 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5419 ab089a2a 2018-07-12 stsp free(line);
5420 2550e4c3 2018-07-13 stsp line = NULL;
5421 1cae65b4 2019-09-22 stsp if (err)
5422 1cae65b4 2019-09-22 stsp return err;
5423 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5424 a3404814 2018-09-02 stsp wstandout(view->window);
5425 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5426 11b20872 2019-11-08 stsp if (tc)
5427 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5428 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5429 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5430 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5431 11b20872 2019-11-08 stsp if (tc)
5432 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5433 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5434 a3404814 2018-09-02 stsp wstandend(view->window);
5435 2550e4c3 2018-07-13 stsp free(wline);
5436 2550e4c3 2018-07-13 stsp wline = NULL;
5437 ab089a2a 2018-07-12 stsp
5438 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5439 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5440 07dd3ed3 2022-08-06 thomas
5441 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5442 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5443 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5444 ab089a2a 2018-07-12 stsp free(id_str);
5445 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5446 ab089a2a 2018-07-12 stsp }
5447 ab089a2a 2018-07-12 stsp free(id_str);
5448 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5449 3f60a8ef 2018-07-10 stsp free(line);
5450 2550e4c3 2018-07-13 stsp line = NULL;
5451 3f60a8ef 2018-07-10 stsp if (err)
5452 3f60a8ef 2018-07-10 stsp return err;
5453 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5454 2550e4c3 2018-07-13 stsp free(wline);
5455 2550e4c3 2018-07-13 stsp wline = NULL;
5456 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5457 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5458 3f60a8ef 2018-07-10 stsp
5459 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5460 05171be4 2022-06-23 thomas view->maxx = 0;
5461 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5462 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5463 826082fe 2020-12-10 stsp if (linelen == -1) {
5464 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5465 826082fe 2020-12-10 stsp s->eof = 1;
5466 826082fe 2020-12-10 stsp break;
5467 826082fe 2020-12-10 stsp }
5468 84451b3e 2018-07-10 stsp free(line);
5469 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5470 84451b3e 2018-07-10 stsp }
5471 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5472 07dd3ed3 2022-08-06 thomas continue;
5473 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5474 826082fe 2020-12-10 stsp continue;
5475 cbea3800 2022-06-23 thomas
5476 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5477 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5478 cbea3800 2022-06-23 thomas if (err) {
5479 cbea3800 2022-06-23 thomas free(line);
5480 cbea3800 2022-06-23 thomas return err;
5481 cbea3800 2022-06-23 thomas }
5482 cbea3800 2022-06-23 thomas free(wline);
5483 cbea3800 2022-06-23 thomas wline = NULL;
5484 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5485 84451b3e 2018-07-10 stsp
5486 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5487 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5488 b700b5d6 2018-07-10 stsp
5489 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5490 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5491 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5492 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5493 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5494 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5495 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5496 8d0fe45a 2019-08-12 stsp char *id_str;
5497 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5498 91198554 2022-06-23 thomas blame_line->id);
5499 8d0fe45a 2019-08-12 stsp if (err) {
5500 8d0fe45a 2019-08-12 stsp free(line);
5501 8d0fe45a 2019-08-12 stsp return err;
5502 8d0fe45a 2019-08-12 stsp }
5503 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5504 11b20872 2019-11-08 stsp if (tc)
5505 11b20872 2019-11-08 stsp wattr_on(view->window,
5506 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5507 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5508 11b20872 2019-11-08 stsp if (tc)
5509 11b20872 2019-11-08 stsp wattr_off(view->window,
5510 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5511 8d0fe45a 2019-08-12 stsp free(id_str);
5512 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5513 8d0fe45a 2019-08-12 stsp } else {
5514 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5515 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5516 84451b3e 2018-07-10 stsp }
5517 ee41ec32 2018-07-10 stsp } else {
5518 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5519 ee41ec32 2018-07-10 stsp prev_id = NULL;
5520 ee41ec32 2018-07-10 stsp }
5521 84451b3e 2018-07-10 stsp
5522 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5523 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5524 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5525 27a741e5 2019-09-11 stsp
5526 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5527 41605754 2020-11-12 stsp width = 9;
5528 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5529 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5530 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5531 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5532 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5533 41605754 2020-11-12 stsp if (err) {
5534 41605754 2020-11-12 stsp free(line);
5535 41605754 2020-11-12 stsp return err;
5536 41605754 2020-11-12 stsp }
5537 41605754 2020-11-12 stsp width += 9;
5538 41605754 2020-11-12 stsp } else {
5539 923086fd 2022-06-23 thomas int skip;
5540 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5541 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5542 923086fd 2022-06-23 thomas if (err) {
5543 923086fd 2022-06-23 thomas free(line);
5544 923086fd 2022-06-23 thomas return err;
5545 05171be4 2022-06-23 thomas }
5546 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5547 02bce7e2 2022-06-23 thomas width += 9;
5548 41605754 2020-11-12 stsp free(wline);
5549 41605754 2020-11-12 stsp wline = NULL;
5550 41605754 2020-11-12 stsp }
5551 41605754 2020-11-12 stsp
5552 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5553 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5554 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5555 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5556 84451b3e 2018-07-10 stsp }
5557 826082fe 2020-12-10 stsp free(line);
5558 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5559 84451b3e 2018-07-10 stsp
5560 a5d43cac 2022-07-01 thomas view_border(view);
5561 84451b3e 2018-07-10 stsp
5562 84451b3e 2018-07-10 stsp return NULL;
5563 84451b3e 2018-07-10 stsp }
5564 84451b3e 2018-07-10 stsp
5565 84451b3e 2018-07-10 stsp static const struct got_error *
5566 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5567 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5568 84451b3e 2018-07-10 stsp {
5569 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5570 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5571 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5572 1a76625f 2018-10-22 stsp int errcode;
5573 84451b3e 2018-07-10 stsp
5574 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5575 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5576 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5577 84451b3e 2018-07-10 stsp
5578 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5579 1a76625f 2018-10-22 stsp if (errcode)
5580 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5581 84451b3e 2018-07-10 stsp
5582 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5583 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5584 d68a0a7d 2018-07-10 stsp goto done;
5585 d68a0a7d 2018-07-10 stsp }
5586 d68a0a7d 2018-07-10 stsp
5587 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5588 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5589 d68a0a7d 2018-07-10 stsp
5590 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5591 d68a0a7d 2018-07-10 stsp if (line->annotated)
5592 d68a0a7d 2018-07-10 stsp goto done;
5593 d68a0a7d 2018-07-10 stsp
5594 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5595 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5596 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5597 84451b3e 2018-07-10 stsp goto done;
5598 84451b3e 2018-07-10 stsp }
5599 84451b3e 2018-07-10 stsp line->annotated = 1;
5600 84451b3e 2018-07-10 stsp done:
5601 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5602 1a76625f 2018-10-22 stsp if (errcode)
5603 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5604 84451b3e 2018-07-10 stsp return err;
5605 84451b3e 2018-07-10 stsp }
5606 84451b3e 2018-07-10 stsp
5607 84451b3e 2018-07-10 stsp static void *
5608 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5609 84451b3e 2018-07-10 stsp {
5610 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5611 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5612 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5613 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5614 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5615 00a8878e 2022-07-01 thomas
5616 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5617 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5618 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5619 9117a7b7 2022-07-01 thomas
5620 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5621 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5622 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5623 9117a7b7 2022-07-01 thomas goto done;
5624 9117a7b7 2022-07-01 thomas }
5625 18430de3 2018-07-10 stsp
5626 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5627 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5628 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5629 9117a7b7 2022-07-01 thomas goto done;
5630 9117a7b7 2022-07-01 thomas }
5631 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5632 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5633 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5634 9117a7b7 2022-07-01 thomas goto done;
5635 9117a7b7 2022-07-01 thomas }
5636 9117a7b7 2022-07-01 thomas
5637 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5638 61266923 2020-01-14 stsp if (err)
5639 9117a7b7 2022-07-01 thomas goto done;
5640 61266923 2020-01-14 stsp
5641 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5642 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5643 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5644 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5645 fc06ba56 2019-08-22 stsp err = NULL;
5646 18430de3 2018-07-10 stsp
5647 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5648 9117a7b7 2022-07-01 thomas if (errcode) {
5649 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5650 9117a7b7 2022-07-01 thomas goto done;
5651 9117a7b7 2022-07-01 thomas }
5652 18430de3 2018-07-10 stsp
5653 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5654 1d0f4054 2021-06-17 stsp if (err == NULL)
5655 1d0f4054 2021-06-17 stsp err = close_err;
5656 c9beca56 2018-07-22 stsp ta->repo = NULL;
5657 c9beca56 2018-07-22 stsp *ta->complete = 1;
5658 18430de3 2018-07-10 stsp
5659 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5660 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5661 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5662 18430de3 2018-07-10 stsp
5663 9117a7b7 2022-07-01 thomas done:
5664 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5665 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5666 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5667 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5668 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5669 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5670 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5671 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5672 00a8878e 2022-07-01 thomas
5673 18430de3 2018-07-10 stsp return (void *)err;
5674 84451b3e 2018-07-10 stsp }
5675 84451b3e 2018-07-10 stsp
5676 245d91c1 2018-07-12 stsp static struct got_object_id *
5677 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5678 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5679 245d91c1 2018-07-12 stsp {
5680 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5681 8d0fe45a 2019-08-12 stsp
5682 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5683 8d0fe45a 2019-08-12 stsp return NULL;
5684 b880a918 2018-07-10 stsp
5685 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5686 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5687 4fc71f3b 2022-07-12 thomas return NULL;
5688 4fc71f3b 2022-07-12 thomas
5689 4fc71f3b 2022-07-12 thomas return line->id;
5690 4fc71f3b 2022-07-12 thomas }
5691 4fc71f3b 2022-07-12 thomas
5692 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5693 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5694 4fc71f3b 2022-07-12 thomas int lineno)
5695 4fc71f3b 2022-07-12 thomas {
5696 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5697 4fc71f3b 2022-07-12 thomas
5698 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5699 4fc71f3b 2022-07-12 thomas return NULL;
5700 4fc71f3b 2022-07-12 thomas
5701 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5702 245d91c1 2018-07-12 stsp if (!line->annotated)
5703 245d91c1 2018-07-12 stsp return NULL;
5704 245d91c1 2018-07-12 stsp
5705 245d91c1 2018-07-12 stsp return line->id;
5706 b880a918 2018-07-10 stsp }
5707 245d91c1 2018-07-12 stsp
5708 b880a918 2018-07-10 stsp static const struct got_error *
5709 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5710 a70480e0 2018-06-23 stsp {
5711 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5712 245d91c1 2018-07-12 stsp int i;
5713 245d91c1 2018-07-12 stsp
5714 245d91c1 2018-07-12 stsp if (blame->thread) {
5715 1a76625f 2018-10-22 stsp int errcode;
5716 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5717 1a76625f 2018-10-22 stsp if (errcode)
5718 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5719 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5720 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5721 1a76625f 2018-10-22 stsp if (errcode)
5722 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5723 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5724 1a76625f 2018-10-22 stsp if (errcode)
5725 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5726 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5727 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5728 245d91c1 2018-07-12 stsp err = NULL;
5729 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5730 245d91c1 2018-07-12 stsp }
5731 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5732 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5733 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5734 1d0f4054 2021-06-17 stsp if (err == NULL)
5735 1d0f4054 2021-06-17 stsp err = close_err;
5736 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5737 245d91c1 2018-07-12 stsp }
5738 245d91c1 2018-07-12 stsp if (blame->f) {
5739 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5740 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5741 245d91c1 2018-07-12 stsp blame->f = NULL;
5742 245d91c1 2018-07-12 stsp }
5743 57670559 2018-12-23 stsp if (blame->lines) {
5744 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5745 57670559 2018-12-23 stsp free(blame->lines[i].id);
5746 57670559 2018-12-23 stsp free(blame->lines);
5747 57670559 2018-12-23 stsp blame->lines = NULL;
5748 57670559 2018-12-23 stsp }
5749 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5750 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5751 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5752 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5753 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5754 7cd52833 2022-06-23 thomas if (err == NULL)
5755 7cd52833 2022-06-23 thomas err = pack_err;
5756 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5757 7cd52833 2022-06-23 thomas }
5758 245d91c1 2018-07-12 stsp return err;
5759 245d91c1 2018-07-12 stsp }
5760 245d91c1 2018-07-12 stsp
5761 245d91c1 2018-07-12 stsp static const struct got_error *
5762 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5763 fc06ba56 2019-08-22 stsp {
5764 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5765 fc06ba56 2019-08-22 stsp int *done = arg;
5766 fc06ba56 2019-08-22 stsp int errcode;
5767 fc06ba56 2019-08-22 stsp
5768 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5769 fc06ba56 2019-08-22 stsp if (errcode)
5770 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5771 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5772 fc06ba56 2019-08-22 stsp
5773 fc06ba56 2019-08-22 stsp if (*done)
5774 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5775 fc06ba56 2019-08-22 stsp
5776 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5777 fc06ba56 2019-08-22 stsp if (errcode)
5778 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5779 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5780 fc06ba56 2019-08-22 stsp
5781 fc06ba56 2019-08-22 stsp return err;
5782 fc06ba56 2019-08-22 stsp }
5783 fc06ba56 2019-08-22 stsp
5784 fc06ba56 2019-08-22 stsp static const struct got_error *
5785 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5786 245d91c1 2018-07-12 stsp {
5787 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5788 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5789 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5790 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5791 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5792 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5793 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5794 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5795 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5796 a70480e0 2018-06-23 stsp
5797 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5798 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5799 27d434c2 2018-09-15 stsp if (err)
5800 15a94983 2018-12-23 stsp return err;
5801 f4ae6ddb 2022-07-01 thomas
5802 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5803 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5804 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5805 f4ae6ddb 2022-07-01 thomas goto done;
5806 f4ae6ddb 2022-07-01 thomas }
5807 945f9229 2022-04-16 thomas
5808 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5809 945f9229 2022-04-16 thomas if (err)
5810 945f9229 2022-04-16 thomas goto done;
5811 27d434c2 2018-09-15 stsp
5812 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5813 84451b3e 2018-07-10 stsp if (err)
5814 84451b3e 2018-07-10 stsp goto done;
5815 27d434c2 2018-09-15 stsp
5816 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5817 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5818 84451b3e 2018-07-10 stsp goto done;
5819 84451b3e 2018-07-10 stsp }
5820 a70480e0 2018-06-23 stsp
5821 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5822 a70480e0 2018-06-23 stsp if (err)
5823 a70480e0 2018-06-23 stsp goto done;
5824 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5825 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5826 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5827 84451b3e 2018-07-10 stsp goto done;
5828 84451b3e 2018-07-10 stsp }
5829 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5830 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5831 1fddf795 2021-01-20 stsp if (err)
5832 1fddf795 2021-01-20 stsp goto done;
5833 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5834 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5835 84451b3e 2018-07-10 stsp goto done;
5836 1fddf795 2021-01-20 stsp }
5837 b02560ec 2019-08-19 stsp
5838 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5839 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5840 b02560ec 2019-08-19 stsp blame->nlines--;
5841 a70480e0 2018-06-23 stsp
5842 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5843 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5844 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5845 84451b3e 2018-07-10 stsp goto done;
5846 84451b3e 2018-07-10 stsp }
5847 a70480e0 2018-06-23 stsp
5848 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5849 bd24772e 2018-07-11 stsp if (err)
5850 bd24772e 2018-07-11 stsp goto done;
5851 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5852 7cd52833 2022-06-23 thomas pack_fds);
5853 7cd52833 2022-06-23 thomas if (err)
5854 7cd52833 2022-06-23 thomas goto done;
5855 bd24772e 2018-07-11 stsp
5856 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5857 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5858 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5859 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5860 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5861 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5862 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5863 245d91c1 2018-07-12 stsp goto done;
5864 245d91c1 2018-07-12 stsp }
5865 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5866 245d91c1 2018-07-12 stsp
5867 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5868 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5869 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5870 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5871 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5872 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5873 a5388363 2020-12-01 naddy s->blame_complete = 0;
5874 f5a09613 2020-12-13 naddy
5875 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5876 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5877 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5878 f5a09613 2020-12-13 naddy s->selected_line = 1;
5879 f5a09613 2020-12-13 naddy }
5880 aa61903a 2021-12-31 thomas s->matched_line = 0;
5881 245d91c1 2018-07-12 stsp
5882 245d91c1 2018-07-12 stsp done:
5883 945f9229 2022-04-16 thomas if (commit)
5884 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5885 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5886 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5887 245d91c1 2018-07-12 stsp if (blob)
5888 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5889 27d434c2 2018-09-15 stsp free(obj_id);
5890 245d91c1 2018-07-12 stsp if (err)
5891 245d91c1 2018-07-12 stsp stop_blame(blame);
5892 245d91c1 2018-07-12 stsp return err;
5893 245d91c1 2018-07-12 stsp }
5894 245d91c1 2018-07-12 stsp
5895 245d91c1 2018-07-12 stsp static const struct got_error *
5896 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5897 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5898 245d91c1 2018-07-12 stsp {
5899 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5900 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5901 dbc6a6b6 2018-07-12 stsp
5902 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5903 245d91c1 2018-07-12 stsp
5904 c4843652 2019-08-12 stsp s->path = strdup(path);
5905 c4843652 2019-08-12 stsp if (s->path == NULL)
5906 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5907 c4843652 2019-08-12 stsp
5908 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5909 c4843652 2019-08-12 stsp if (err) {
5910 c4843652 2019-08-12 stsp free(s->path);
5911 7cbe629d 2018-08-04 stsp return err;
5912 c4843652 2019-08-12 stsp }
5913 245d91c1 2018-07-12 stsp
5914 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5915 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5916 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5917 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5918 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5919 fb2756b9 2018-08-04 stsp s->repo = repo;
5920 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5921 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5922 7cbe629d 2018-08-04 stsp
5923 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5924 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5925 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5926 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5927 11b20872 2019-11-08 stsp if (err)
5928 11b20872 2019-11-08 stsp return err;
5929 11b20872 2019-11-08 stsp }
5930 11b20872 2019-11-08 stsp
5931 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5932 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5933 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5934 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5935 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5936 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5937 e5a0f69f 2018-08-18 stsp
5938 a5388363 2020-12-01 naddy return run_blame(view);
5939 7cbe629d 2018-08-04 stsp }
5940 7cbe629d 2018-08-04 stsp
5941 e5a0f69f 2018-08-18 stsp static const struct got_error *
5942 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5943 7cbe629d 2018-08-04 stsp {
5944 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5945 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5946 7cbe629d 2018-08-04 stsp
5947 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5948 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5949 e5a0f69f 2018-08-18 stsp
5950 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5951 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5952 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5953 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5954 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5955 7cbe629d 2018-08-04 stsp }
5956 e5a0f69f 2018-08-18 stsp
5957 e5a0f69f 2018-08-18 stsp free(s->path);
5958 11b20872 2019-11-08 stsp free_colors(&s->colors);
5959 e5a0f69f 2018-08-18 stsp return err;
5960 7cbe629d 2018-08-04 stsp }
5961 7cbe629d 2018-08-04 stsp
5962 7cbe629d 2018-08-04 stsp static const struct got_error *
5963 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5964 6c4c42e0 2019-06-24 stsp {
5965 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5966 6c4c42e0 2019-06-24 stsp
5967 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5968 6c4c42e0 2019-06-24 stsp return NULL;
5969 6c4c42e0 2019-06-24 stsp }
5970 6c4c42e0 2019-06-24 stsp
5971 6c4c42e0 2019-06-24 stsp static const struct got_error *
5972 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5973 6c4c42e0 2019-06-24 stsp {
5974 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5975 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5976 6c4c42e0 2019-06-24 stsp int lineno;
5977 78eecffc 2022-06-23 thomas char *line = NULL;
5978 826082fe 2020-12-10 stsp size_t linesize = 0;
5979 826082fe 2020-12-10 stsp ssize_t linelen;
5980 6c4c42e0 2019-06-24 stsp
5981 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5982 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5983 6c4c42e0 2019-06-24 stsp return NULL;
5984 6c4c42e0 2019-06-24 stsp }
5985 6c4c42e0 2019-06-24 stsp
5986 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5987 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5988 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5989 6c4c42e0 2019-06-24 stsp else
5990 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5991 4b3f9dac 2021-12-17 thomas } else
5992 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5993 6c4c42e0 2019-06-24 stsp
5994 6c4c42e0 2019-06-24 stsp while (1) {
5995 6c4c42e0 2019-06-24 stsp off_t offset;
5996 6c4c42e0 2019-06-24 stsp
5997 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5998 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5999 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6000 6c4c42e0 2019-06-24 stsp break;
6001 6c4c42e0 2019-06-24 stsp }
6002 2246482e 2019-06-25 stsp
6003 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6004 6c4c42e0 2019-06-24 stsp lineno = 1;
6005 6c4c42e0 2019-06-24 stsp else
6006 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
6007 6c4c42e0 2019-06-24 stsp }
6008 6c4c42e0 2019-06-24 stsp
6009 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
6010 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
6011 6c4c42e0 2019-06-24 stsp free(line);
6012 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
6013 6c4c42e0 2019-06-24 stsp }
6014 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
6015 78eecffc 2022-06-23 thomas if (linelen != -1) {
6016 78eecffc 2022-06-23 thomas char *exstr;
6017 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
6018 78eecffc 2022-06-23 thomas if (err)
6019 78eecffc 2022-06-23 thomas break;
6020 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
6021 78eecffc 2022-06-23 thomas &view->regmatch)) {
6022 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
6023 78eecffc 2022-06-23 thomas s->matched_line = lineno;
6024 78eecffc 2022-06-23 thomas free(exstr);
6025 78eecffc 2022-06-23 thomas break;
6026 78eecffc 2022-06-23 thomas }
6027 78eecffc 2022-06-23 thomas free(exstr);
6028 6c4c42e0 2019-06-24 stsp }
6029 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6030 6c4c42e0 2019-06-24 stsp lineno++;
6031 6c4c42e0 2019-06-24 stsp else
6032 6c4c42e0 2019-06-24 stsp lineno--;
6033 6c4c42e0 2019-06-24 stsp }
6034 826082fe 2020-12-10 stsp free(line);
6035 6c4c42e0 2019-06-24 stsp
6036 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
6037 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
6038 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
6039 6c4c42e0 2019-06-24 stsp }
6040 6c4c42e0 2019-06-24 stsp
6041 05171be4 2022-06-23 thomas return err;
6042 6c4c42e0 2019-06-24 stsp }
6043 6c4c42e0 2019-06-24 stsp
6044 6c4c42e0 2019-06-24 stsp static const struct got_error *
6045 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6046 7cbe629d 2018-08-04 stsp {
6047 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6048 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6049 2b380cc8 2018-10-24 stsp int errcode;
6050 2b380cc8 2018-10-24 stsp
6051 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6052 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6053 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6054 2b380cc8 2018-10-24 stsp if (errcode)
6055 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6056 51fe7530 2019-08-19 stsp
6057 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6058 2b380cc8 2018-10-24 stsp }
6059 e5a0f69f 2018-08-18 stsp
6060 51fe7530 2019-08-19 stsp if (s->blame_complete)
6061 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6062 51fe7530 2019-08-19 stsp
6063 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6064 e5a0f69f 2018-08-18 stsp
6065 a5d43cac 2022-07-01 thomas view_border(view);
6066 e5a0f69f 2018-08-18 stsp return err;
6067 e5a0f69f 2018-08-18 stsp }
6068 e5a0f69f 2018-08-18 stsp
6069 e5a0f69f 2018-08-18 stsp static const struct got_error *
6070 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6071 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6072 eaeaa612 2022-07-20 thomas {
6073 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6074 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6075 eaeaa612 2022-07-20 thomas
6076 eaeaa612 2022-07-20 thomas *new_view = NULL;
6077 eaeaa612 2022-07-20 thomas
6078 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6079 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6080 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6081 eaeaa612 2022-07-20 thomas
6082 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6083 eaeaa612 2022-07-20 thomas if (err)
6084 eaeaa612 2022-07-20 thomas view_close(log_view);
6085 eaeaa612 2022-07-20 thomas else
6086 eaeaa612 2022-07-20 thomas *new_view = log_view;
6087 eaeaa612 2022-07-20 thomas
6088 eaeaa612 2022-07-20 thomas return err;
6089 eaeaa612 2022-07-20 thomas }
6090 eaeaa612 2022-07-20 thomas
6091 eaeaa612 2022-07-20 thomas static const struct got_error *
6092 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6093 e5a0f69f 2018-08-18 stsp {
6094 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6095 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6096 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6097 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6098 a5d43cac 2022-07-01 thomas
6099 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6100 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6101 a5d43cac 2022-07-01 thomas --eos; /* border */
6102 7cbe629d 2018-08-04 stsp
6103 e5a0f69f 2018-08-18 stsp switch (ch) {
6104 05171be4 2022-06-23 thomas case '0':
6105 05171be4 2022-06-23 thomas view->x = 0;
6106 05171be4 2022-06-23 thomas break;
6107 05171be4 2022-06-23 thomas case '$':
6108 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
6109 07b0611c 2022-06-23 thomas view->count = 0;
6110 05171be4 2022-06-23 thomas break;
6111 05171be4 2022-06-23 thomas case KEY_RIGHT:
6112 05171be4 2022-06-23 thomas case 'l':
6113 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
6114 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
6115 07b0611c 2022-06-23 thomas else
6116 07b0611c 2022-06-23 thomas view->count = 0;
6117 05171be4 2022-06-23 thomas break;
6118 05171be4 2022-06-23 thomas case KEY_LEFT:
6119 05171be4 2022-06-23 thomas case 'h':
6120 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
6121 07b0611c 2022-06-23 thomas if (view->x <= 0)
6122 07b0611c 2022-06-23 thomas view->count = 0;
6123 05171be4 2022-06-23 thomas break;
6124 1e37a5c2 2019-05-12 jcs case 'q':
6125 1e37a5c2 2019-05-12 jcs s->done = 1;
6126 4deef56f 2021-09-02 naddy break;
6127 4deef56f 2021-09-02 naddy case 'g':
6128 4deef56f 2021-09-02 naddy case KEY_HOME:
6129 4deef56f 2021-09-02 naddy s->selected_line = 1;
6130 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6131 07b0611c 2022-06-23 thomas view->count = 0;
6132 4deef56f 2021-09-02 naddy break;
6133 4deef56f 2021-09-02 naddy case 'G':
6134 4deef56f 2021-09-02 naddy case KEY_END:
6135 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6136 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6137 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6138 4deef56f 2021-09-02 naddy } else {
6139 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6140 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6141 4deef56f 2021-09-02 naddy }
6142 07b0611c 2022-06-23 thomas view->count = 0;
6143 1e37a5c2 2019-05-12 jcs break;
6144 1e37a5c2 2019-05-12 jcs case 'k':
6145 1e37a5c2 2019-05-12 jcs case KEY_UP:
6146 f7140bf5 2021-10-17 thomas case CTRL('p'):
6147 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6148 1e37a5c2 2019-05-12 jcs s->selected_line--;
6149 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6150 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6151 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6152 07b0611c 2022-06-23 thomas else
6153 07b0611c 2022-06-23 thomas view->count = 0;
6154 1e37a5c2 2019-05-12 jcs break;
6155 70f17a53 2022-06-13 thomas case CTRL('u'):
6156 23427b14 2022-06-23 thomas case 'u':
6157 70f17a53 2022-06-13 thomas nscroll /= 2;
6158 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6159 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6160 ea025d1d 2020-02-22 naddy case CTRL('b'):
6161 1c5e5faa 2022-06-23 thomas case 'b':
6162 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6163 07b0611c 2022-06-23 thomas if (view->count > 1)
6164 07b0611c 2022-06-23 thomas nscroll += nscroll;
6165 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6166 07b0611c 2022-06-23 thomas view->count = 0;
6167 e5a0f69f 2018-08-18 stsp break;
6168 1e37a5c2 2019-05-12 jcs }
6169 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6170 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6171 1e37a5c2 2019-05-12 jcs else
6172 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6173 1e37a5c2 2019-05-12 jcs break;
6174 1e37a5c2 2019-05-12 jcs case 'j':
6175 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6176 f7140bf5 2021-10-17 thomas case CTRL('n'):
6177 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6178 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6179 1e37a5c2 2019-05-12 jcs s->selected_line++;
6180 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6181 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6182 07b0611c 2022-06-23 thomas else
6183 07b0611c 2022-06-23 thomas view->count = 0;
6184 1e37a5c2 2019-05-12 jcs break;
6185 1c5e5faa 2022-06-23 thomas case 'c':
6186 1e37a5c2 2019-05-12 jcs case 'p': {
6187 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6188 07b0611c 2022-06-23 thomas
6189 07b0611c 2022-06-23 thomas view->count = 0;
6190 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6191 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6192 1e37a5c2 2019-05-12 jcs if (id == NULL)
6193 e5a0f69f 2018-08-18 stsp break;
6194 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6195 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6196 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6197 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6198 1e37a5c2 2019-05-12 jcs int obj_type;
6199 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6200 1e37a5c2 2019-05-12 jcs s->repo, id);
6201 e5a0f69f 2018-08-18 stsp if (err)
6202 e5a0f69f 2018-08-18 stsp break;
6203 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6204 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6205 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6206 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6207 e5a0f69f 2018-08-18 stsp break;
6208 e5a0f69f 2018-08-18 stsp }
6209 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6210 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6211 ec242592 2022-04-22 thomas s->repo, &pid->id);
6212 945f9229 2022-04-16 thomas if (err)
6213 945f9229 2022-04-16 thomas break;
6214 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6215 945f9229 2022-04-16 thomas pcommit, s->path);
6216 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6217 e5a0f69f 2018-08-18 stsp if (err) {
6218 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6219 1e37a5c2 2019-05-12 jcs err = NULL;
6220 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6221 e5a0f69f 2018-08-18 stsp break;
6222 e5a0f69f 2018-08-18 stsp }
6223 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6224 1e37a5c2 2019-05-12 jcs blob_id);
6225 1e37a5c2 2019-05-12 jcs free(blob_id);
6226 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6227 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6228 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6229 e5a0f69f 2018-08-18 stsp break;
6230 1e37a5c2 2019-05-12 jcs }
6231 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6232 ec242592 2022-04-22 thomas &pid->id);
6233 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6234 1e37a5c2 2019-05-12 jcs } else {
6235 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6236 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6237 1e37a5c2 2019-05-12 jcs break;
6238 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6239 1e37a5c2 2019-05-12 jcs id);
6240 1e37a5c2 2019-05-12 jcs }
6241 1e37a5c2 2019-05-12 jcs if (err)
6242 e5a0f69f 2018-08-18 stsp break;
6243 1e37a5c2 2019-05-12 jcs s->done = 1;
6244 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6245 1e37a5c2 2019-05-12 jcs s->done = 0;
6246 1e37a5c2 2019-05-12 jcs if (thread_err)
6247 1e37a5c2 2019-05-12 jcs break;
6248 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6249 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6250 a5388363 2020-12-01 naddy err = run_blame(view);
6251 1e37a5c2 2019-05-12 jcs if (err)
6252 1e37a5c2 2019-05-12 jcs break;
6253 1e37a5c2 2019-05-12 jcs break;
6254 1e37a5c2 2019-05-12 jcs }
6255 1c5e5faa 2022-06-23 thomas case 'C': {
6256 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6257 07b0611c 2022-06-23 thomas
6258 07b0611c 2022-06-23 thomas view->count = 0;
6259 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6260 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6261 1e37a5c2 2019-05-12 jcs break;
6262 1e37a5c2 2019-05-12 jcs s->done = 1;
6263 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6264 1e37a5c2 2019-05-12 jcs s->done = 0;
6265 1e37a5c2 2019-05-12 jcs if (thread_err)
6266 1e37a5c2 2019-05-12 jcs break;
6267 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6268 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6269 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6270 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6271 a5388363 2020-12-01 naddy err = run_blame(view);
6272 1e37a5c2 2019-05-12 jcs if (err)
6273 1e37a5c2 2019-05-12 jcs break;
6274 1e37a5c2 2019-05-12 jcs break;
6275 1e37a5c2 2019-05-12 jcs }
6276 2a31b33b 2022-07-23 thomas case 'L':
6277 eaeaa612 2022-07-20 thomas view->count = 0;
6278 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6279 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6280 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6281 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6282 eaeaa612 2022-07-20 thomas break;
6283 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6284 1e37a5c2 2019-05-12 jcs case '\r': {
6285 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6286 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6287 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6288 07b0611c 2022-06-23 thomas
6289 07b0611c 2022-06-23 thomas view->count = 0;
6290 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6291 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6292 1e37a5c2 2019-05-12 jcs if (id == NULL)
6293 1e37a5c2 2019-05-12 jcs break;
6294 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6295 1e37a5c2 2019-05-12 jcs if (err)
6296 1e37a5c2 2019-05-12 jcs break;
6297 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6298 4fc71f3b 2022-07-12 thomas if (*new_view) {
6299 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6300 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6301 4fc71f3b 2022-07-12 thomas if (err)
6302 4fc71f3b 2022-07-12 thomas break;
6303 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6304 4fc71f3b 2022-07-12 thomas } else {
6305 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6306 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6307 a5d43cac 2022-07-01 thomas
6308 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6309 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6310 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6311 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6312 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6313 4fc71f3b 2022-07-12 thomas break;
6314 4fc71f3b 2022-07-12 thomas }
6315 15a94983 2018-12-23 stsp }
6316 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6317 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6318 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6319 1e37a5c2 2019-05-12 jcs if (err) {
6320 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6321 1e37a5c2 2019-05-12 jcs break;
6322 1e37a5c2 2019-05-12 jcs }
6323 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6324 4fc71f3b 2022-07-12 thomas s->selected_line;
6325 4fc71f3b 2022-07-12 thomas if (*new_view)
6326 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6327 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6328 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6329 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6330 a5d43cac 2022-07-01 thomas if (err)
6331 a5d43cac 2022-07-01 thomas break;
6332 a5d43cac 2022-07-01 thomas }
6333 a5d43cac 2022-07-01 thomas
6334 e78dc838 2020-12-04 stsp view->focussed = 0;
6335 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6336 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6337 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6338 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6339 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6340 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6341 1e37a5c2 2019-05-12 jcs if (err)
6342 34bc9ec9 2019-02-22 stsp break;
6343 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6344 40236d76 2022-06-23 thomas if (err)
6345 40236d76 2022-06-23 thomas break;
6346 e78dc838 2020-12-04 stsp view->focus_child = 1;
6347 1e37a5c2 2019-05-12 jcs } else
6348 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6349 1e37a5c2 2019-05-12 jcs if (err)
6350 e5a0f69f 2018-08-18 stsp break;
6351 1e37a5c2 2019-05-12 jcs break;
6352 1e37a5c2 2019-05-12 jcs }
6353 70f17a53 2022-06-13 thomas case CTRL('d'):
6354 23427b14 2022-06-23 thomas case 'd':
6355 70f17a53 2022-06-13 thomas nscroll /= 2;
6356 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6357 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6358 ea025d1d 2020-02-22 naddy case CTRL('f'):
6359 1c5e5faa 2022-06-23 thomas case 'f':
6360 1e37a5c2 2019-05-12 jcs case ' ':
6361 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6362 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6363 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6364 07b0611c 2022-06-23 thomas view->count = 0;
6365 e5a0f69f 2018-08-18 stsp break;
6366 1e37a5c2 2019-05-12 jcs }
6367 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6368 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6369 70f17a53 2022-06-13 thomas s->selected_line +=
6370 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6371 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6372 1e37a5c2 2019-05-12 jcs }
6373 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6374 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6375 1e37a5c2 2019-05-12 jcs else
6376 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6377 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6378 1e37a5c2 2019-05-12 jcs break;
6379 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6380 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6381 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6382 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6383 1e37a5c2 2019-05-12 jcs }
6384 1e37a5c2 2019-05-12 jcs break;
6385 1e37a5c2 2019-05-12 jcs default:
6386 07b0611c 2022-06-23 thomas view->count = 0;
6387 1e37a5c2 2019-05-12 jcs break;
6388 a70480e0 2018-06-23 stsp }
6389 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6390 adf4c9e0 2022-07-03 thomas }
6391 adf4c9e0 2022-07-03 thomas
6392 adf4c9e0 2022-07-03 thomas static const struct got_error *
6393 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6394 adf4c9e0 2022-07-03 thomas {
6395 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6396 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6397 adf4c9e0 2022-07-03 thomas
6398 adf4c9e0 2022-07-03 thomas view->count = 0;
6399 adf4c9e0 2022-07-03 thomas s->done = 1;
6400 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6401 adf4c9e0 2022-07-03 thomas s->done = 0;
6402 adf4c9e0 2022-07-03 thomas if (err)
6403 adf4c9e0 2022-07-03 thomas return err;
6404 adf4c9e0 2022-07-03 thomas return run_blame(view);
6405 a70480e0 2018-06-23 stsp }
6406 a70480e0 2018-06-23 stsp
6407 a70480e0 2018-06-23 stsp static const struct got_error *
6408 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6409 9f7d7167 2018-04-29 stsp {
6410 a70480e0 2018-06-23 stsp const struct got_error *error;
6411 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6412 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6413 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6414 0587e10c 2020-07-23 stsp char *link_target = NULL;
6415 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6416 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6417 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6418 a70480e0 2018-06-23 stsp int ch;
6419 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6420 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6421 a70480e0 2018-06-23 stsp
6422 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6423 a70480e0 2018-06-23 stsp switch (ch) {
6424 a70480e0 2018-06-23 stsp case 'c':
6425 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6426 a70480e0 2018-06-23 stsp break;
6427 69069811 2018-08-02 stsp case 'r':
6428 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6429 69069811 2018-08-02 stsp if (repo_path == NULL)
6430 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6431 9ba1d308 2019-10-21 stsp optarg);
6432 69069811 2018-08-02 stsp break;
6433 a70480e0 2018-06-23 stsp default:
6434 17020d27 2019-03-07 stsp usage_blame();
6435 a70480e0 2018-06-23 stsp /* NOTREACHED */
6436 a70480e0 2018-06-23 stsp }
6437 a70480e0 2018-06-23 stsp }
6438 a70480e0 2018-06-23 stsp
6439 a70480e0 2018-06-23 stsp argc -= optind;
6440 a70480e0 2018-06-23 stsp argv += optind;
6441 a70480e0 2018-06-23 stsp
6442 f135c941 2020-02-20 stsp if (argc != 1)
6443 a70480e0 2018-06-23 stsp usage_blame();
6444 6962eb72 2020-02-20 stsp
6445 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6446 7cd52833 2022-06-23 thomas if (error != NULL)
6447 7cd52833 2022-06-23 thomas goto done;
6448 7cd52833 2022-06-23 thomas
6449 69069811 2018-08-02 stsp if (repo_path == NULL) {
6450 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6451 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6452 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6453 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6454 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6455 c156c7a4 2020-12-18 stsp goto done;
6456 f135c941 2020-02-20 stsp if (worktree)
6457 eb41ed75 2019-02-05 stsp repo_path =
6458 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6459 f135c941 2020-02-20 stsp else
6460 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6461 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6462 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6463 c156c7a4 2020-12-18 stsp goto done;
6464 c156c7a4 2020-12-18 stsp }
6465 f135c941 2020-02-20 stsp }
6466 a915003a 2019-02-05 stsp
6467 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6468 c02c541e 2019-03-29 stsp if (error != NULL)
6469 8e94dd5b 2019-01-04 stsp goto done;
6470 69069811 2018-08-02 stsp
6471 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6472 f135c941 2020-02-20 stsp worktree);
6473 c02c541e 2019-03-29 stsp if (error)
6474 92205607 2019-01-04 stsp goto done;
6475 69069811 2018-08-02 stsp
6476 f135c941 2020-02-20 stsp init_curses();
6477 f135c941 2020-02-20 stsp
6478 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6479 51a10b52 2020-12-26 stsp if (error)
6480 51a10b52 2020-12-26 stsp goto done;
6481 51a10b52 2020-12-26 stsp
6482 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6483 eb41ed75 2019-02-05 stsp if (error)
6484 69069811 2018-08-02 stsp goto done;
6485 a70480e0 2018-06-23 stsp
6486 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6487 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6488 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6489 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6490 a70480e0 2018-06-23 stsp if (error != NULL)
6491 66b4983c 2018-06-23 stsp goto done;
6492 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6493 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6494 a70480e0 2018-06-23 stsp } else {
6495 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6496 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6497 a70480e0 2018-06-23 stsp }
6498 a19e88aa 2018-06-23 stsp if (error != NULL)
6499 8b473291 2019-02-21 stsp goto done;
6500 8b473291 2019-02-21 stsp
6501 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6502 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6503 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6504 e1cd8fed 2018-08-01 stsp goto done;
6505 e1cd8fed 2018-08-01 stsp }
6506 0587e10c 2020-07-23 stsp
6507 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6508 945f9229 2022-04-16 thomas if (error)
6509 945f9229 2022-04-16 thomas goto done;
6510 945f9229 2022-04-16 thomas
6511 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6512 945f9229 2022-04-16 thomas commit, repo);
6513 7cbe629d 2018-08-04 stsp if (error)
6514 7cbe629d 2018-08-04 stsp goto done;
6515 0587e10c 2020-07-23 stsp
6516 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6517 78756c87 2020-11-24 stsp commit_id, repo);
6518 0587e10c 2020-07-23 stsp if (error)
6519 0587e10c 2020-07-23 stsp goto done;
6520 12314ad4 2019-08-31 stsp if (worktree) {
6521 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6522 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6523 12314ad4 2019-08-31 stsp worktree = NULL;
6524 12314ad4 2019-08-31 stsp }
6525 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6526 a70480e0 2018-06-23 stsp done:
6527 69069811 2018-08-02 stsp free(repo_path);
6528 f135c941 2020-02-20 stsp free(in_repo_path);
6529 0587e10c 2020-07-23 stsp free(link_target);
6530 69069811 2018-08-02 stsp free(cwd);
6531 a70480e0 2018-06-23 stsp free(commit_id);
6532 945f9229 2022-04-16 thomas if (commit)
6533 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6534 eb41ed75 2019-02-05 stsp if (worktree)
6535 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6536 1d0f4054 2021-06-17 stsp if (repo) {
6537 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6538 1d0f4054 2021-06-17 stsp if (error == NULL)
6539 1d0f4054 2021-06-17 stsp error = close_err;
6540 1d0f4054 2021-06-17 stsp }
6541 7cd52833 2022-06-23 thomas if (pack_fds) {
6542 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6543 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6544 7cd52833 2022-06-23 thomas if (error == NULL)
6545 7cd52833 2022-06-23 thomas error = pack_err;
6546 7cd52833 2022-06-23 thomas }
6547 51a10b52 2020-12-26 stsp tog_free_refs();
6548 a70480e0 2018-06-23 stsp return error;
6549 ffd1d5e5 2018-06-23 stsp }
6550 ffd1d5e5 2018-06-23 stsp
6551 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6552 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6553 ffd1d5e5 2018-06-23 stsp {
6554 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6555 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6556 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6557 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6558 86f4aab9 2022-09-09 thomas char *index = NULL;
6559 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6560 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6561 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6562 ffd1d5e5 2018-06-23 stsp
6563 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6564 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6565 a5d43cac 2022-07-01 thomas --limit; /* border */
6566 ffd1d5e5 2018-06-23 stsp
6567 f7d12f7e 2018-08-01 stsp werase(view->window);
6568 ffd1d5e5 2018-06-23 stsp
6569 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6570 ffd1d5e5 2018-06-23 stsp return NULL;
6571 ffd1d5e5 2018-06-23 stsp
6572 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6573 f91a2b48 2022-06-23 thomas 0, 0);
6574 ffd1d5e5 2018-06-23 stsp if (err)
6575 ffd1d5e5 2018-06-23 stsp return err;
6576 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6577 a3404814 2018-09-02 stsp wstandout(view->window);
6578 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6579 11b20872 2019-11-08 stsp if (tc)
6580 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6581 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6582 86f4aab9 2022-09-09 thomas free(wline);
6583 86f4aab9 2022-09-09 thomas wline = NULL;
6584 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6585 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6586 11b20872 2019-11-08 stsp if (tc)
6587 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6588 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6589 a3404814 2018-09-02 stsp wstandend(view->window);
6590 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6591 86f4aab9 2022-09-09 thomas return NULL;
6592 07dd3ed3 2022-08-06 thomas
6593 6f5f393a 2022-08-12 thomas i += s->selected;
6594 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6595 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6596 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6597 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6598 07dd3ed3 2022-08-06 thomas }
6599 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6600 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6601 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6602 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6603 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6604 86f4aab9 2022-09-09 thomas free(index);
6605 ce52c690 2018-06-23 stsp if (err)
6606 ce52c690 2018-06-23 stsp return err;
6607 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6608 2550e4c3 2018-07-13 stsp free(wline);
6609 2550e4c3 2018-07-13 stsp wline = NULL;
6610 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6611 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6612 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6613 ffd1d5e5 2018-06-23 stsp return NULL;
6614 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6615 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6616 a1eca9bb 2018-06-23 stsp return NULL;
6617 ffd1d5e5 2018-06-23 stsp
6618 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6619 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6620 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6621 0cf4efb1 2018-09-29 stsp if (view->focussed)
6622 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6623 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6624 ffd1d5e5 2018-06-23 stsp }
6625 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6626 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6627 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6628 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6629 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6630 ffd1d5e5 2018-06-23 stsp return NULL;
6631 ffd1d5e5 2018-06-23 stsp n = 1;
6632 ffd1d5e5 2018-06-23 stsp } else {
6633 ffd1d5e5 2018-06-23 stsp n = 0;
6634 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6635 ffd1d5e5 2018-06-23 stsp }
6636 ffd1d5e5 2018-06-23 stsp
6637 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6638 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6639 848d6979 2019-08-12 stsp const char *modestr = "";
6640 56e0773d 2019-11-28 stsp mode_t mode;
6641 1d13200f 2018-07-12 stsp
6642 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6643 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6644 56e0773d 2019-11-28 stsp
6645 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6646 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6647 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6648 1d13200f 2018-07-12 stsp if (err)
6649 638f9024 2019-05-13 stsp return got_error_from_errno(
6650 230a42bd 2019-05-11 jcs "got_object_id_str");
6651 1d13200f 2018-07-12 stsp }
6652 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6653 63c5ca5d 2019-08-24 stsp modestr = "$";
6654 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6655 0d6c6ee3 2020-05-20 stsp int i;
6656 0d6c6ee3 2020-05-20 stsp
6657 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6658 d86d3b18 2020-12-01 naddy te, s->repo);
6659 0d6c6ee3 2020-05-20 stsp if (err) {
6660 0d6c6ee3 2020-05-20 stsp free(id_str);
6661 0d6c6ee3 2020-05-20 stsp return err;
6662 0d6c6ee3 2020-05-20 stsp }
6663 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6664 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6665 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6666 0d6c6ee3 2020-05-20 stsp }
6667 848d6979 2019-08-12 stsp modestr = "@";
6668 0d6c6ee3 2020-05-20 stsp }
6669 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6670 848d6979 2019-08-12 stsp modestr = "/";
6671 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6672 848d6979 2019-08-12 stsp modestr = "*";
6673 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6674 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6675 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6676 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6677 1d13200f 2018-07-12 stsp free(id_str);
6678 0d6c6ee3 2020-05-20 stsp free(link_target);
6679 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6680 1d13200f 2018-07-12 stsp }
6681 1d13200f 2018-07-12 stsp free(id_str);
6682 0d6c6ee3 2020-05-20 stsp free(link_target);
6683 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6684 f91a2b48 2022-06-23 thomas 0, 0);
6685 ffd1d5e5 2018-06-23 stsp if (err) {
6686 ffd1d5e5 2018-06-23 stsp free(line);
6687 ffd1d5e5 2018-06-23 stsp break;
6688 ffd1d5e5 2018-06-23 stsp }
6689 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6690 0cf4efb1 2018-09-29 stsp if (view->focussed)
6691 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6692 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6693 ffd1d5e5 2018-06-23 stsp }
6694 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6695 f26dddb7 2019-11-08 stsp if (tc)
6696 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6697 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6698 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6699 f26dddb7 2019-11-08 stsp if (tc)
6700 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6701 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6702 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6703 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6704 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6705 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6706 ffd1d5e5 2018-06-23 stsp free(line);
6707 2550e4c3 2018-07-13 stsp free(wline);
6708 2550e4c3 2018-07-13 stsp wline = NULL;
6709 ffd1d5e5 2018-06-23 stsp n++;
6710 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6711 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6712 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6713 ffd1d5e5 2018-06-23 stsp break;
6714 ffd1d5e5 2018-06-23 stsp }
6715 ffd1d5e5 2018-06-23 stsp
6716 ffd1d5e5 2018-06-23 stsp return err;
6717 ffd1d5e5 2018-06-23 stsp }
6718 ffd1d5e5 2018-06-23 stsp
6719 ffd1d5e5 2018-06-23 stsp static void
6720 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6721 ffd1d5e5 2018-06-23 stsp {
6722 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6723 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6724 fa86c4bf 2020-11-29 stsp int i = 0;
6725 ffd1d5e5 2018-06-23 stsp
6726 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6727 ffd1d5e5 2018-06-23 stsp return;
6728 ffd1d5e5 2018-06-23 stsp
6729 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6730 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6731 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6732 fa86c4bf 2020-11-29 stsp if (!isroot)
6733 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6734 fa86c4bf 2020-11-29 stsp break;
6735 fa86c4bf 2020-11-29 stsp }
6736 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6737 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6738 ffd1d5e5 2018-06-23 stsp }
6739 ffd1d5e5 2018-06-23 stsp }
6740 ffd1d5e5 2018-06-23 stsp
6741 a5d43cac 2022-07-01 thomas static const struct got_error *
6742 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6743 ffd1d5e5 2018-06-23 stsp {
6744 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6745 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6746 ffd1d5e5 2018-06-23 stsp int n = 0;
6747 ffd1d5e5 2018-06-23 stsp
6748 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6749 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6750 694d3271 2020-12-01 naddy s->first_displayed_entry);
6751 694d3271 2020-12-01 naddy else
6752 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6753 56e0773d 2019-11-28 stsp
6754 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6755 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6756 07dd3ed3 2022-08-06 thomas if (last) {
6757 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6758 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6759 07dd3ed3 2022-08-06 thomas }
6760 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6761 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6762 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6763 768394f3 2019-01-24 stsp }
6764 ffd1d5e5 2018-06-23 stsp }
6765 a5d43cac 2022-07-01 thomas
6766 a5d43cac 2022-07-01 thomas return NULL;
6767 ffd1d5e5 2018-06-23 stsp }
6768 ffd1d5e5 2018-06-23 stsp
6769 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6770 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6771 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6772 ffd1d5e5 2018-06-23 stsp {
6773 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6774 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6775 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6776 ffd1d5e5 2018-06-23 stsp
6777 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6778 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6779 56e0773d 2019-11-28 stsp + 1 /* slash */;
6780 ce52c690 2018-06-23 stsp if (te)
6781 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6782 ce52c690 2018-06-23 stsp
6783 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6784 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6785 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6786 ffd1d5e5 2018-06-23 stsp
6787 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6788 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6789 d9765a41 2018-06-23 stsp while (pt) {
6790 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6791 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6792 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6793 cb2ebc8a 2018-06-23 stsp goto done;
6794 cb2ebc8a 2018-06-23 stsp }
6795 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6796 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6797 cb2ebc8a 2018-06-23 stsp goto done;
6798 cb2ebc8a 2018-06-23 stsp }
6799 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6800 ffd1d5e5 2018-06-23 stsp }
6801 ce52c690 2018-06-23 stsp if (te) {
6802 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6803 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6804 ce52c690 2018-06-23 stsp goto done;
6805 ce52c690 2018-06-23 stsp }
6806 cb2ebc8a 2018-06-23 stsp }
6807 ce52c690 2018-06-23 stsp done:
6808 ce52c690 2018-06-23 stsp if (err) {
6809 ce52c690 2018-06-23 stsp free(*path);
6810 ce52c690 2018-06-23 stsp *path = NULL;
6811 ce52c690 2018-06-23 stsp }
6812 ce52c690 2018-06-23 stsp return err;
6813 ce52c690 2018-06-23 stsp }
6814 ce52c690 2018-06-23 stsp
6815 ce52c690 2018-06-23 stsp static const struct got_error *
6816 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6817 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6818 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6819 ce52c690 2018-06-23 stsp {
6820 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6821 ce52c690 2018-06-23 stsp char *path;
6822 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6823 a0de39f3 2019-08-09 stsp
6824 a0de39f3 2019-08-09 stsp *new_view = NULL;
6825 69efd4c4 2018-07-18 stsp
6826 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6827 ce52c690 2018-06-23 stsp if (err)
6828 ce52c690 2018-06-23 stsp return err;
6829 ffd1d5e5 2018-06-23 stsp
6830 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6831 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6832 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6833 83ce39e3 2019-08-12 stsp goto done;
6834 83ce39e3 2019-08-12 stsp }
6835 cdf1ee82 2018-08-01 stsp
6836 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6837 e5a0f69f 2018-08-18 stsp if (err) {
6838 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6839 fc06ba56 2019-08-22 stsp err = NULL;
6840 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6841 e5a0f69f 2018-08-18 stsp } else
6842 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6843 83ce39e3 2019-08-12 stsp done:
6844 83ce39e3 2019-08-12 stsp free(path);
6845 69efd4c4 2018-07-18 stsp return err;
6846 69efd4c4 2018-07-18 stsp }
6847 69efd4c4 2018-07-18 stsp
6848 69efd4c4 2018-07-18 stsp static const struct got_error *
6849 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6850 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6851 69efd4c4 2018-07-18 stsp {
6852 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6853 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6854 69efd4c4 2018-07-18 stsp char *path;
6855 a0de39f3 2019-08-09 stsp
6856 a0de39f3 2019-08-09 stsp *new_view = NULL;
6857 69efd4c4 2018-07-18 stsp
6858 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6859 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6860 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6861 e5a0f69f 2018-08-18 stsp
6862 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6863 69efd4c4 2018-07-18 stsp if (err)
6864 69efd4c4 2018-07-18 stsp return err;
6865 69efd4c4 2018-07-18 stsp
6866 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6867 4e97c21c 2020-12-06 stsp path, 0);
6868 ba4f502b 2018-08-04 stsp if (err)
6869 e5a0f69f 2018-08-18 stsp view_close(log_view);
6870 e5a0f69f 2018-08-18 stsp else
6871 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6872 cb2ebc8a 2018-06-23 stsp free(path);
6873 cb2ebc8a 2018-06-23 stsp return err;
6874 ffd1d5e5 2018-06-23 stsp }
6875 ffd1d5e5 2018-06-23 stsp
6876 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6877 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6878 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6879 ffd1d5e5 2018-06-23 stsp {
6880 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6881 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6882 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6883 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6884 ffd1d5e5 2018-06-23 stsp
6885 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6886 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6887 bc573f3b 2021-07-10 stsp
6888 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6889 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6890 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6891 ffd1d5e5 2018-06-23 stsp
6892 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6893 bc573f3b 2021-07-10 stsp if (err)
6894 bc573f3b 2021-07-10 stsp goto done;
6895 bc573f3b 2021-07-10 stsp
6896 bc573f3b 2021-07-10 stsp /*
6897 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6898 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6899 bc573f3b 2021-07-10 stsp * closed on demand.
6900 bc573f3b 2021-07-10 stsp */
6901 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6902 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6903 bc573f3b 2021-07-10 stsp if (err)
6904 bc573f3b 2021-07-10 stsp goto done;
6905 bc573f3b 2021-07-10 stsp s->tree = s->root;
6906 bc573f3b 2021-07-10 stsp
6907 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6908 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6909 ffd1d5e5 2018-06-23 stsp goto done;
6910 ffd1d5e5 2018-06-23 stsp
6911 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6912 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6913 ffd1d5e5 2018-06-23 stsp goto done;
6914 ffd1d5e5 2018-06-23 stsp }
6915 ffd1d5e5 2018-06-23 stsp
6916 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6917 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6918 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6919 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6920 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6921 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6922 9cd7cbd1 2020-12-07 stsp goto done;
6923 9cd7cbd1 2020-12-07 stsp }
6924 9cd7cbd1 2020-12-07 stsp }
6925 fb2756b9 2018-08-04 stsp s->repo = repo;
6926 c0b01bdb 2019-11-08 stsp
6927 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6928 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6929 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6930 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6931 c0b01bdb 2019-11-08 stsp if (err)
6932 c0b01bdb 2019-11-08 stsp goto done;
6933 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6934 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6935 bc573f3b 2021-07-10 stsp if (err)
6936 c0b01bdb 2019-11-08 stsp goto done;
6937 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6938 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6939 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6940 bc573f3b 2021-07-10 stsp if (err)
6941 c0b01bdb 2019-11-08 stsp goto done;
6942 e5a0f69f 2018-08-18 stsp
6943 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6944 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6945 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6946 bc573f3b 2021-07-10 stsp if (err)
6947 11b20872 2019-11-08 stsp goto done;
6948 11b20872 2019-11-08 stsp
6949 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6950 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6951 bc573f3b 2021-07-10 stsp if (err)
6952 c0b01bdb 2019-11-08 stsp goto done;
6953 c0b01bdb 2019-11-08 stsp }
6954 c0b01bdb 2019-11-08 stsp
6955 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6956 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6957 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6958 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6959 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6960 ad80ab7b 2018-08-04 stsp done:
6961 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6962 bc573f3b 2021-07-10 stsp if (commit)
6963 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6964 bc573f3b 2021-07-10 stsp if (err)
6965 bc573f3b 2021-07-10 stsp close_tree_view(view);
6966 ad80ab7b 2018-08-04 stsp return err;
6967 ad80ab7b 2018-08-04 stsp }
6968 ad80ab7b 2018-08-04 stsp
6969 e5a0f69f 2018-08-18 stsp static const struct got_error *
6970 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6971 ad80ab7b 2018-08-04 stsp {
6972 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6973 ad80ab7b 2018-08-04 stsp
6974 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6975 fb2756b9 2018-08-04 stsp free(s->tree_label);
6976 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6977 6484ec90 2018-09-29 stsp free(s->commit_id);
6978 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6979 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6980 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6981 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6982 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6983 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6984 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6985 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6986 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6987 ad80ab7b 2018-08-04 stsp free(parent);
6988 ad80ab7b 2018-08-04 stsp
6989 ad80ab7b 2018-08-04 stsp }
6990 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6991 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6992 bc573f3b 2021-07-10 stsp if (s->root)
6993 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6994 7c32bd05 2019-06-22 stsp return NULL;
6995 7c32bd05 2019-06-22 stsp }
6996 7c32bd05 2019-06-22 stsp
6997 7c32bd05 2019-06-22 stsp static const struct got_error *
6998 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6999 7c32bd05 2019-06-22 stsp {
7000 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7001 7c32bd05 2019-06-22 stsp
7002 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7003 7c32bd05 2019-06-22 stsp return NULL;
7004 7c32bd05 2019-06-22 stsp }
7005 7c32bd05 2019-06-22 stsp
7006 7c32bd05 2019-06-22 stsp static int
7007 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7008 7c32bd05 2019-06-22 stsp {
7009 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7010 7c32bd05 2019-06-22 stsp
7011 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7012 56e0773d 2019-11-28 stsp 0) == 0;
7013 7c32bd05 2019-06-22 stsp }
7014 7c32bd05 2019-06-22 stsp
7015 7c32bd05 2019-06-22 stsp static const struct got_error *
7016 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7017 7c32bd05 2019-06-22 stsp {
7018 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7019 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7020 7c32bd05 2019-06-22 stsp
7021 7c32bd05 2019-06-22 stsp if (!view->searching) {
7022 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7023 7c32bd05 2019-06-22 stsp return NULL;
7024 7c32bd05 2019-06-22 stsp }
7025 7c32bd05 2019-06-22 stsp
7026 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7027 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7028 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7029 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7030 56e0773d 2019-11-28 stsp s->selected_entry);
7031 7c32bd05 2019-06-22 stsp else
7032 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7033 56e0773d 2019-11-28 stsp } else {
7034 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7035 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7036 56e0773d 2019-11-28 stsp else
7037 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7038 56e0773d 2019-11-28 stsp s->selected_entry);
7039 7c32bd05 2019-06-22 stsp }
7040 7c32bd05 2019-06-22 stsp } else {
7041 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7042 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7043 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7044 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7045 56e0773d 2019-11-28 stsp else
7046 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7047 7c32bd05 2019-06-22 stsp }
7048 7c32bd05 2019-06-22 stsp
7049 7c32bd05 2019-06-22 stsp while (1) {
7050 56e0773d 2019-11-28 stsp if (te == NULL) {
7051 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7052 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7053 ac66afb8 2019-06-24 stsp return NULL;
7054 ac66afb8 2019-06-24 stsp }
7055 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7056 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7057 56e0773d 2019-11-28 stsp else
7058 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7059 7c32bd05 2019-06-22 stsp }
7060 7c32bd05 2019-06-22 stsp
7061 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7062 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7063 56e0773d 2019-11-28 stsp s->matched_entry = te;
7064 7c32bd05 2019-06-22 stsp break;
7065 7c32bd05 2019-06-22 stsp }
7066 7c32bd05 2019-06-22 stsp
7067 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7068 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7069 56e0773d 2019-11-28 stsp else
7070 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7071 7c32bd05 2019-06-22 stsp }
7072 e5a0f69f 2018-08-18 stsp
7073 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7074 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7075 7c32bd05 2019-06-22 stsp s->selected = 0;
7076 7c32bd05 2019-06-22 stsp }
7077 7c32bd05 2019-06-22 stsp
7078 e5a0f69f 2018-08-18 stsp return NULL;
7079 ad80ab7b 2018-08-04 stsp }
7080 ad80ab7b 2018-08-04 stsp
7081 ad80ab7b 2018-08-04 stsp static const struct got_error *
7082 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7083 ad80ab7b 2018-08-04 stsp {
7084 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7085 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7086 e5a0f69f 2018-08-18 stsp char *parent_path;
7087 ad80ab7b 2018-08-04 stsp
7088 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7089 e5a0f69f 2018-08-18 stsp if (err)
7090 e5a0f69f 2018-08-18 stsp return err;
7091 ffd1d5e5 2018-06-23 stsp
7092 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7093 e5a0f69f 2018-08-18 stsp free(parent_path);
7094 669b5ffa 2018-10-07 stsp
7095 a5d43cac 2022-07-01 thomas view_border(view);
7096 e5a0f69f 2018-08-18 stsp return err;
7097 07dd3ed3 2022-08-06 thomas }
7098 07dd3ed3 2022-08-06 thomas
7099 07dd3ed3 2022-08-06 thomas static const struct got_error *
7100 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7101 07dd3ed3 2022-08-06 thomas {
7102 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7103 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7104 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7105 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7106 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7107 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7108 07dd3ed3 2022-08-06 thomas
7109 07dd3ed3 2022-08-06 thomas g = view->gline;
7110 07dd3ed3 2022-08-06 thomas view->gline = 0;
7111 07dd3ed3 2022-08-06 thomas
7112 07dd3ed3 2022-08-06 thomas if (g == 0)
7113 07dd3ed3 2022-08-06 thomas g = 1;
7114 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7115 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7116 07dd3ed3 2022-08-06 thomas
7117 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7118 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7119 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7120 07dd3ed3 2022-08-06 thomas
7121 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7122 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7123 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7124 07dd3ed3 2022-08-06 thomas }
7125 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7126 07dd3ed3 2022-08-06 thomas last += off;
7127 07dd3ed3 2022-08-06 thomas
7128 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7129 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7130 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7131 07dd3ed3 2022-08-06 thomas }
7132 07dd3ed3 2022-08-06 thomas
7133 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7134 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7135 07dd3ed3 2022-08-06 thomas i += off;
7136 07dd3ed3 2022-08-06 thomas }
7137 07dd3ed3 2022-08-06 thomas
7138 07dd3ed3 2022-08-06 thomas if (i < g) {
7139 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7140 07dd3ed3 2022-08-06 thomas if (err)
7141 07dd3ed3 2022-08-06 thomas return err;
7142 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7143 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7144 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7145 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7146 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7147 07dd3ed3 2022-08-06 thomas first += off;
7148 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7149 07dd3ed3 2022-08-06 thomas }
7150 07dd3ed3 2022-08-06 thomas } else if (i > g)
7151 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7152 07dd3ed3 2022-08-06 thomas
7153 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7154 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7155 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7156 07dd3ed3 2022-08-06 thomas
7157 07dd3ed3 2022-08-06 thomas return NULL;
7158 e5a0f69f 2018-08-18 stsp }
7159 ce52c690 2018-06-23 stsp
7160 e5a0f69f 2018-08-18 stsp static const struct got_error *
7161 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7162 e5a0f69f 2018-08-18 stsp {
7163 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7164 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7165 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7166 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7167 ffd1d5e5 2018-06-23 stsp
7168 07dd3ed3 2022-08-06 thomas if (view->gline)
7169 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7170 07dd3ed3 2022-08-06 thomas
7171 e5a0f69f 2018-08-18 stsp switch (ch) {
7172 1e37a5c2 2019-05-12 jcs case 'i':
7173 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7174 07b0611c 2022-06-23 thomas view->count = 0;
7175 1e37a5c2 2019-05-12 jcs break;
7176 1be4947a 2022-07-22 thomas case 'L':
7177 07b0611c 2022-06-23 thomas view->count = 0;
7178 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7179 ffd1d5e5 2018-06-23 stsp break;
7180 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7181 152c1c93 2020-11-29 stsp break;
7182 1be4947a 2022-07-22 thomas case 'R':
7183 07b0611c 2022-06-23 thomas view->count = 0;
7184 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7185 e4526bf5 2021-09-03 naddy break;
7186 e4526bf5 2021-09-03 naddy case 'g':
7187 e4526bf5 2021-09-03 naddy case KEY_HOME:
7188 e4526bf5 2021-09-03 naddy s->selected = 0;
7189 07b0611c 2022-06-23 thomas view->count = 0;
7190 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7191 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7192 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7193 e4526bf5 2021-09-03 naddy else
7194 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7195 1e37a5c2 2019-05-12 jcs break;
7196 e4526bf5 2021-09-03 naddy case 'G':
7197 a5d43cac 2022-07-01 thomas case KEY_END: {
7198 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7199 a5d43cac 2022-07-01 thomas
7200 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7201 a5d43cac 2022-07-01 thomas --eos; /* border */
7202 e4526bf5 2021-09-03 naddy s->selected = 0;
7203 07b0611c 2022-06-23 thomas view->count = 0;
7204 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7205 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7206 e4526bf5 2021-09-03 naddy if (te == NULL) {
7207 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7208 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7209 e4526bf5 2021-09-03 naddy n++;
7210 e4526bf5 2021-09-03 naddy }
7211 e4526bf5 2021-09-03 naddy break;
7212 e4526bf5 2021-09-03 naddy }
7213 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7214 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7215 e4526bf5 2021-09-03 naddy }
7216 e4526bf5 2021-09-03 naddy if (n > 0)
7217 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7218 e4526bf5 2021-09-03 naddy break;
7219 a5d43cac 2022-07-01 thomas }
7220 1e37a5c2 2019-05-12 jcs case 'k':
7221 1e37a5c2 2019-05-12 jcs case KEY_UP:
7222 f7140bf5 2021-10-17 thomas case CTRL('p'):
7223 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7224 1e37a5c2 2019-05-12 jcs s->selected--;
7225 fa86c4bf 2020-11-29 stsp break;
7226 1e37a5c2 2019-05-12 jcs }
7227 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7228 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7229 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7230 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7231 07b0611c 2022-06-23 thomas view->count = 0;
7232 1e37a5c2 2019-05-12 jcs break;
7233 70f17a53 2022-06-13 thomas case CTRL('u'):
7234 23427b14 2022-06-23 thomas case 'u':
7235 70f17a53 2022-06-13 thomas nscroll /= 2;
7236 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7237 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7238 ea025d1d 2020-02-22 naddy case CTRL('b'):
7239 1c5e5faa 2022-06-23 thomas case 'b':
7240 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7241 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7242 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7243 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7244 fa86c4bf 2020-11-29 stsp } else {
7245 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7246 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7247 fa86c4bf 2020-11-29 stsp }
7248 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7249 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7250 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7251 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7252 07b0611c 2022-06-23 thomas view->count = 0;
7253 1e37a5c2 2019-05-12 jcs break;
7254 1e37a5c2 2019-05-12 jcs case 'j':
7255 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7256 f7140bf5 2021-10-17 thomas case CTRL('n'):
7257 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7258 1e37a5c2 2019-05-12 jcs s->selected++;
7259 1e37a5c2 2019-05-12 jcs break;
7260 1e37a5c2 2019-05-12 jcs }
7261 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7262 07b0611c 2022-06-23 thomas == NULL) {
7263 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7264 07b0611c 2022-06-23 thomas view->count = 0;
7265 1e37a5c2 2019-05-12 jcs break;
7266 07b0611c 2022-06-23 thomas }
7267 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7268 1e37a5c2 2019-05-12 jcs break;
7269 70f17a53 2022-06-13 thomas case CTRL('d'):
7270 23427b14 2022-06-23 thomas case 'd':
7271 70f17a53 2022-06-13 thomas nscroll /= 2;
7272 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7273 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7274 ea025d1d 2020-02-22 naddy case CTRL('f'):
7275 1c5e5faa 2022-06-23 thomas case 'f':
7276 4c2d69cb 2022-06-23 thomas case ' ':
7277 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7278 1e37a5c2 2019-05-12 jcs == NULL) {
7279 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7280 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7281 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7282 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7283 07b0611c 2022-06-23 thomas else
7284 07b0611c 2022-06-23 thomas view->count = 0;
7285 1e37a5c2 2019-05-12 jcs break;
7286 1e37a5c2 2019-05-12 jcs }
7287 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7288 1e37a5c2 2019-05-12 jcs break;
7289 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7290 1e37a5c2 2019-05-12 jcs case '\r':
7291 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7292 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7293 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7294 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7295 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7296 07b0611c 2022-06-23 thomas view->count = 0;
7297 1e37a5c2 2019-05-12 jcs break;
7298 07b0611c 2022-06-23 thomas }
7299 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7300 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7301 1e37a5c2 2019-05-12 jcs entry);
7302 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7303 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7304 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7305 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7306 1e37a5c2 2019-05-12 jcs s->selected_entry =
7307 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7308 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7309 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7310 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7311 a5d43cac 2022-07-01 thomas if (err)
7312 a5d43cac 2022-07-01 thomas break;
7313 a5d43cac 2022-07-01 thomas }
7314 1e37a5c2 2019-05-12 jcs free(parent);
7315 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7316 56e0773d 2019-11-28 stsp s->selected_entry))) {
7317 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7318 07b0611c 2022-06-23 thomas view->count = 0;
7319 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7320 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7321 1e37a5c2 2019-05-12 jcs if (err)
7322 1e37a5c2 2019-05-12 jcs break;
7323 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7324 941e9f74 2019-05-21 stsp if (err) {
7325 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7326 1e37a5c2 2019-05-12 jcs break;
7327 1e37a5c2 2019-05-12 jcs }
7328 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7329 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7330 1e37a5c2 2019-05-12 jcs break;
7331 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7332 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7333 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7334 07b0611c 2022-06-23 thomas view->count = 0;
7335 1e37a5c2 2019-05-12 jcs break;
7336 1e37a5c2 2019-05-12 jcs default:
7337 07b0611c 2022-06-23 thomas view->count = 0;
7338 1e37a5c2 2019-05-12 jcs break;
7339 ffd1d5e5 2018-06-23 stsp }
7340 e5a0f69f 2018-08-18 stsp
7341 ffd1d5e5 2018-06-23 stsp return err;
7342 9f7d7167 2018-04-29 stsp }
7343 9f7d7167 2018-04-29 stsp
7344 ffd1d5e5 2018-06-23 stsp __dead static void
7345 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7346 ffd1d5e5 2018-06-23 stsp {
7347 ffd1d5e5 2018-06-23 stsp endwin();
7348 91198554 2022-06-23 thomas fprintf(stderr,
7349 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7350 ffd1d5e5 2018-06-23 stsp getprogname());
7351 ffd1d5e5 2018-06-23 stsp exit(1);
7352 ffd1d5e5 2018-06-23 stsp }
7353 ffd1d5e5 2018-06-23 stsp
7354 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7355 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7356 ffd1d5e5 2018-06-23 stsp {
7357 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7358 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7359 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7360 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7361 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7362 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7363 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7364 4e97c21c 2020-12-06 stsp char *label = NULL;
7365 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7366 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7367 ffd1d5e5 2018-06-23 stsp int ch;
7368 5221c383 2018-08-01 stsp struct tog_view *view;
7369 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7370 70ac5f84 2019-03-28 stsp
7371 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7372 ffd1d5e5 2018-06-23 stsp switch (ch) {
7373 ffd1d5e5 2018-06-23 stsp case 'c':
7374 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7375 74283ab8 2020-02-07 stsp break;
7376 74283ab8 2020-02-07 stsp case 'r':
7377 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7378 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7379 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7380 74283ab8 2020-02-07 stsp optarg);
7381 ffd1d5e5 2018-06-23 stsp break;
7382 ffd1d5e5 2018-06-23 stsp default:
7383 e99e2d15 2020-11-24 naddy usage_tree();
7384 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7385 ffd1d5e5 2018-06-23 stsp }
7386 ffd1d5e5 2018-06-23 stsp }
7387 ffd1d5e5 2018-06-23 stsp
7388 ffd1d5e5 2018-06-23 stsp argc -= optind;
7389 ffd1d5e5 2018-06-23 stsp argv += optind;
7390 ffd1d5e5 2018-06-23 stsp
7391 55cccc34 2020-02-20 stsp if (argc > 1)
7392 e99e2d15 2020-11-24 naddy usage_tree();
7393 7cd52833 2022-06-23 thomas
7394 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7395 7cd52833 2022-06-23 thomas if (error != NULL)
7396 7cd52833 2022-06-23 thomas goto done;
7397 74283ab8 2020-02-07 stsp
7398 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7399 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7400 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7401 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7402 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7403 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7404 c156c7a4 2020-12-18 stsp goto done;
7405 55cccc34 2020-02-20 stsp if (worktree)
7406 52185f70 2019-02-05 stsp repo_path =
7407 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7408 55cccc34 2020-02-20 stsp else
7409 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7410 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7411 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7412 c156c7a4 2020-12-18 stsp goto done;
7413 c156c7a4 2020-12-18 stsp }
7414 55cccc34 2020-02-20 stsp }
7415 a915003a 2019-02-05 stsp
7416 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7417 c02c541e 2019-03-29 stsp if (error != NULL)
7418 52185f70 2019-02-05 stsp goto done;
7419 d188b9a6 2019-01-04 stsp
7420 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7421 55cccc34 2020-02-20 stsp repo, worktree);
7422 55cccc34 2020-02-20 stsp if (error)
7423 55cccc34 2020-02-20 stsp goto done;
7424 55cccc34 2020-02-20 stsp
7425 55cccc34 2020-02-20 stsp init_curses();
7426 55cccc34 2020-02-20 stsp
7427 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7428 c02c541e 2019-03-29 stsp if (error)
7429 52185f70 2019-02-05 stsp goto done;
7430 ffd1d5e5 2018-06-23 stsp
7431 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7432 51a10b52 2020-12-26 stsp if (error)
7433 51a10b52 2020-12-26 stsp goto done;
7434 51a10b52 2020-12-26 stsp
7435 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7436 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7437 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7438 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7439 4e97c21c 2020-12-06 stsp if (error)
7440 4e97c21c 2020-12-06 stsp goto done;
7441 4e97c21c 2020-12-06 stsp head_ref_name = label;
7442 4e97c21c 2020-12-06 stsp } else {
7443 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7444 4e97c21c 2020-12-06 stsp if (error == NULL)
7445 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7446 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7447 4e97c21c 2020-12-06 stsp goto done;
7448 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7449 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7450 4e97c21c 2020-12-06 stsp if (error)
7451 4e97c21c 2020-12-06 stsp goto done;
7452 4e97c21c 2020-12-06 stsp }
7453 ffd1d5e5 2018-06-23 stsp
7454 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7455 945f9229 2022-04-16 thomas if (error)
7456 945f9229 2022-04-16 thomas goto done;
7457 945f9229 2022-04-16 thomas
7458 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7459 5221c383 2018-08-01 stsp if (view == NULL) {
7460 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7461 5221c383 2018-08-01 stsp goto done;
7462 5221c383 2018-08-01 stsp }
7463 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7464 ad80ab7b 2018-08-04 stsp if (error)
7465 ad80ab7b 2018-08-04 stsp goto done;
7466 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7467 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7468 d91faf3b 2020-12-01 naddy in_repo_path);
7469 55cccc34 2020-02-20 stsp if (error)
7470 55cccc34 2020-02-20 stsp goto done;
7471 55cccc34 2020-02-20 stsp }
7472 55cccc34 2020-02-20 stsp
7473 55cccc34 2020-02-20 stsp if (worktree) {
7474 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7475 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7476 55cccc34 2020-02-20 stsp worktree = NULL;
7477 55cccc34 2020-02-20 stsp }
7478 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7479 ffd1d5e5 2018-06-23 stsp done:
7480 52185f70 2019-02-05 stsp free(repo_path);
7481 e4a0e26d 2020-02-20 stsp free(cwd);
7482 ffd1d5e5 2018-06-23 stsp free(commit_id);
7483 4e97c21c 2020-12-06 stsp free(label);
7484 486cd271 2020-12-06 stsp if (ref)
7485 486cd271 2020-12-06 stsp got_ref_close(ref);
7486 1d0f4054 2021-06-17 stsp if (repo) {
7487 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7488 1d0f4054 2021-06-17 stsp if (error == NULL)
7489 1d0f4054 2021-06-17 stsp error = close_err;
7490 1d0f4054 2021-06-17 stsp }
7491 7cd52833 2022-06-23 thomas if (pack_fds) {
7492 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7493 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7494 7cd52833 2022-06-23 thomas if (error == NULL)
7495 7cd52833 2022-06-23 thomas error = pack_err;
7496 7cd52833 2022-06-23 thomas }
7497 51a10b52 2020-12-26 stsp tog_free_refs();
7498 ffd1d5e5 2018-06-23 stsp return error;
7499 6458efa5 2020-11-24 stsp }
7500 6458efa5 2020-11-24 stsp
7501 6458efa5 2020-11-24 stsp static const struct got_error *
7502 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7503 6458efa5 2020-11-24 stsp {
7504 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7506 6458efa5 2020-11-24 stsp
7507 6458efa5 2020-11-24 stsp s->nrefs = 0;
7508 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7509 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7510 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7511 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7512 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7513 6458efa5 2020-11-24 stsp continue;
7514 6458efa5 2020-11-24 stsp
7515 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7516 6458efa5 2020-11-24 stsp if (re == NULL)
7517 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7518 6458efa5 2020-11-24 stsp
7519 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7520 8924d611 2020-12-26 stsp if (re->ref == NULL)
7521 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7522 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7523 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7524 6458efa5 2020-11-24 stsp }
7525 6458efa5 2020-11-24 stsp
7526 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7527 6458efa5 2020-11-24 stsp return NULL;
7528 6458efa5 2020-11-24 stsp }
7529 6458efa5 2020-11-24 stsp
7530 ef20f542 2022-06-26 thomas static void
7531 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7532 6458efa5 2020-11-24 stsp {
7533 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7534 6458efa5 2020-11-24 stsp
7535 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7536 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7537 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7538 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7539 6458efa5 2020-11-24 stsp free(re);
7540 6458efa5 2020-11-24 stsp }
7541 6458efa5 2020-11-24 stsp }
7542 6458efa5 2020-11-24 stsp
7543 6458efa5 2020-11-24 stsp static const struct got_error *
7544 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7545 6458efa5 2020-11-24 stsp {
7546 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7547 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7548 6458efa5 2020-11-24 stsp
7549 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7550 6458efa5 2020-11-24 stsp s->repo = repo;
7551 6458efa5 2020-11-24 stsp
7552 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7553 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7554 6458efa5 2020-11-24 stsp
7555 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7556 6458efa5 2020-11-24 stsp if (err)
7557 6458efa5 2020-11-24 stsp return err;
7558 34ba6917 2020-11-30 stsp
7559 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7560 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7561 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7562 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7563 6458efa5 2020-11-24 stsp if (err)
7564 6458efa5 2020-11-24 stsp goto done;
7565 6458efa5 2020-11-24 stsp
7566 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7567 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7568 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7569 6458efa5 2020-11-24 stsp if (err)
7570 6458efa5 2020-11-24 stsp goto done;
7571 6458efa5 2020-11-24 stsp
7572 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7573 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7574 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7575 2183bbf6 2022-01-23 thomas if (err)
7576 2183bbf6 2022-01-23 thomas goto done;
7577 2183bbf6 2022-01-23 thomas
7578 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7579 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7580 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7581 6458efa5 2020-11-24 stsp if (err)
7582 6458efa5 2020-11-24 stsp goto done;
7583 6458efa5 2020-11-24 stsp }
7584 6458efa5 2020-11-24 stsp
7585 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7586 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7587 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7588 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7589 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7590 6458efa5 2020-11-24 stsp done:
7591 6458efa5 2020-11-24 stsp if (err)
7592 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7593 6458efa5 2020-11-24 stsp return err;
7594 6458efa5 2020-11-24 stsp }
7595 6458efa5 2020-11-24 stsp
7596 6458efa5 2020-11-24 stsp static const struct got_error *
7597 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7598 6458efa5 2020-11-24 stsp {
7599 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7600 6458efa5 2020-11-24 stsp
7601 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7602 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7603 6458efa5 2020-11-24 stsp
7604 6458efa5 2020-11-24 stsp return NULL;
7605 9f7d7167 2018-04-29 stsp }
7606 ce5b7c56 2019-07-09 stsp
7607 6458efa5 2020-11-24 stsp static const struct got_error *
7608 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7609 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7610 6458efa5 2020-11-24 stsp {
7611 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7612 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7613 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7614 6458efa5 2020-11-24 stsp int obj_type;
7615 6458efa5 2020-11-24 stsp
7616 c42c9805 2020-11-24 stsp *commit_id = NULL;
7617 6458efa5 2020-11-24 stsp
7618 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7619 6458efa5 2020-11-24 stsp if (err)
7620 6458efa5 2020-11-24 stsp return err;
7621 6458efa5 2020-11-24 stsp
7622 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7623 6458efa5 2020-11-24 stsp if (err)
7624 6458efa5 2020-11-24 stsp goto done;
7625 6458efa5 2020-11-24 stsp
7626 6458efa5 2020-11-24 stsp switch (obj_type) {
7627 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7628 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7629 6458efa5 2020-11-24 stsp break;
7630 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7631 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7632 6458efa5 2020-11-24 stsp if (err)
7633 6458efa5 2020-11-24 stsp goto done;
7634 c42c9805 2020-11-24 stsp free(obj_id);
7635 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7636 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7637 c42c9805 2020-11-24 stsp if (err)
7638 6458efa5 2020-11-24 stsp goto done;
7639 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7640 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7641 c42c9805 2020-11-24 stsp goto done;
7642 c42c9805 2020-11-24 stsp }
7643 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7644 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7645 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7646 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7647 c42c9805 2020-11-24 stsp goto done;
7648 c42c9805 2020-11-24 stsp }
7649 6458efa5 2020-11-24 stsp break;
7650 6458efa5 2020-11-24 stsp default:
7651 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7652 c42c9805 2020-11-24 stsp break;
7653 6458efa5 2020-11-24 stsp }
7654 6458efa5 2020-11-24 stsp
7655 c42c9805 2020-11-24 stsp done:
7656 c42c9805 2020-11-24 stsp if (tag)
7657 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7658 c42c9805 2020-11-24 stsp if (err) {
7659 c42c9805 2020-11-24 stsp free(*commit_id);
7660 c42c9805 2020-11-24 stsp *commit_id = NULL;
7661 c42c9805 2020-11-24 stsp }
7662 c42c9805 2020-11-24 stsp return err;
7663 c42c9805 2020-11-24 stsp }
7664 c42c9805 2020-11-24 stsp
7665 c42c9805 2020-11-24 stsp static const struct got_error *
7666 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7667 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7668 c42c9805 2020-11-24 stsp {
7669 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7670 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7671 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7672 c42c9805 2020-11-24 stsp
7673 c42c9805 2020-11-24 stsp *new_view = NULL;
7674 c42c9805 2020-11-24 stsp
7675 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7676 c42c9805 2020-11-24 stsp if (err) {
7677 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7678 c42c9805 2020-11-24 stsp return err;
7679 c42c9805 2020-11-24 stsp else
7680 c42c9805 2020-11-24 stsp return NULL;
7681 c42c9805 2020-11-24 stsp }
7682 c42c9805 2020-11-24 stsp
7683 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7684 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7685 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7686 6458efa5 2020-11-24 stsp goto done;
7687 6458efa5 2020-11-24 stsp }
7688 6458efa5 2020-11-24 stsp
7689 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7690 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7691 6458efa5 2020-11-24 stsp done:
7692 6458efa5 2020-11-24 stsp if (err)
7693 6458efa5 2020-11-24 stsp view_close(log_view);
7694 6458efa5 2020-11-24 stsp else
7695 6458efa5 2020-11-24 stsp *new_view = log_view;
7696 c42c9805 2020-11-24 stsp free(commit_id);
7697 6458efa5 2020-11-24 stsp return err;
7698 6458efa5 2020-11-24 stsp }
7699 6458efa5 2020-11-24 stsp
7700 ce5b7c56 2019-07-09 stsp static void
7701 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7702 6458efa5 2020-11-24 stsp {
7703 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7704 34ba6917 2020-11-30 stsp int i = 0;
7705 6458efa5 2020-11-24 stsp
7706 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7707 6458efa5 2020-11-24 stsp return;
7708 6458efa5 2020-11-24 stsp
7709 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7710 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7711 34ba6917 2020-11-30 stsp if (re == NULL)
7712 34ba6917 2020-11-30 stsp break;
7713 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7714 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7715 6458efa5 2020-11-24 stsp }
7716 6458efa5 2020-11-24 stsp }
7717 6458efa5 2020-11-24 stsp
7718 a5d43cac 2022-07-01 thomas static const struct got_error *
7719 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7720 6458efa5 2020-11-24 stsp {
7721 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7722 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7723 6458efa5 2020-11-24 stsp int n = 0;
7724 6458efa5 2020-11-24 stsp
7725 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7726 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7727 6458efa5 2020-11-24 stsp else
7728 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7729 6458efa5 2020-11-24 stsp
7730 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7731 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7732 07dd3ed3 2022-08-06 thomas if (last) {
7733 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7734 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7735 07dd3ed3 2022-08-06 thomas }
7736 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7737 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7738 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7739 6458efa5 2020-11-24 stsp }
7740 6458efa5 2020-11-24 stsp }
7741 a5d43cac 2022-07-01 thomas
7742 a5d43cac 2022-07-01 thomas return NULL;
7743 6458efa5 2020-11-24 stsp }
7744 6458efa5 2020-11-24 stsp
7745 6458efa5 2020-11-24 stsp static const struct got_error *
7746 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7747 6458efa5 2020-11-24 stsp {
7748 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7749 6458efa5 2020-11-24 stsp
7750 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7751 6458efa5 2020-11-24 stsp return NULL;
7752 6458efa5 2020-11-24 stsp }
7753 6458efa5 2020-11-24 stsp
7754 6458efa5 2020-11-24 stsp static int
7755 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7756 6458efa5 2020-11-24 stsp {
7757 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7758 6458efa5 2020-11-24 stsp
7759 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7760 6458efa5 2020-11-24 stsp 0) == 0;
7761 6458efa5 2020-11-24 stsp }
7762 6458efa5 2020-11-24 stsp
7763 6458efa5 2020-11-24 stsp static const struct got_error *
7764 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7765 6458efa5 2020-11-24 stsp {
7766 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7767 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7768 6458efa5 2020-11-24 stsp
7769 6458efa5 2020-11-24 stsp if (!view->searching) {
7770 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7771 6458efa5 2020-11-24 stsp return NULL;
7772 6458efa5 2020-11-24 stsp }
7773 6458efa5 2020-11-24 stsp
7774 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7775 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7776 6458efa5 2020-11-24 stsp if (s->selected_entry)
7777 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7778 6458efa5 2020-11-24 stsp else
7779 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7780 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7781 6458efa5 2020-11-24 stsp } else {
7782 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7783 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7784 6458efa5 2020-11-24 stsp else
7785 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7786 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7787 6458efa5 2020-11-24 stsp }
7788 6458efa5 2020-11-24 stsp } else {
7789 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7790 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7791 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7792 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7793 6458efa5 2020-11-24 stsp else
7794 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7795 6458efa5 2020-11-24 stsp }
7796 6458efa5 2020-11-24 stsp
7797 6458efa5 2020-11-24 stsp while (1) {
7798 6458efa5 2020-11-24 stsp if (re == NULL) {
7799 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7800 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7801 6458efa5 2020-11-24 stsp return NULL;
7802 6458efa5 2020-11-24 stsp }
7803 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7804 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7805 6458efa5 2020-11-24 stsp else
7806 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7807 6458efa5 2020-11-24 stsp }
7808 6458efa5 2020-11-24 stsp
7809 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7810 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7811 6458efa5 2020-11-24 stsp s->matched_entry = re;
7812 6458efa5 2020-11-24 stsp break;
7813 6458efa5 2020-11-24 stsp }
7814 6458efa5 2020-11-24 stsp
7815 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7816 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7817 6458efa5 2020-11-24 stsp else
7818 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7819 6458efa5 2020-11-24 stsp }
7820 6458efa5 2020-11-24 stsp
7821 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7822 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7823 6458efa5 2020-11-24 stsp s->selected = 0;
7824 6458efa5 2020-11-24 stsp }
7825 6458efa5 2020-11-24 stsp
7826 6458efa5 2020-11-24 stsp return NULL;
7827 6458efa5 2020-11-24 stsp }
7828 6458efa5 2020-11-24 stsp
7829 6458efa5 2020-11-24 stsp static const struct got_error *
7830 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7831 6458efa5 2020-11-24 stsp {
7832 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7833 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7834 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7835 6458efa5 2020-11-24 stsp char *line = NULL;
7836 6458efa5 2020-11-24 stsp wchar_t *wline;
7837 6458efa5 2020-11-24 stsp struct tog_color *tc;
7838 6458efa5 2020-11-24 stsp int width, n;
7839 6458efa5 2020-11-24 stsp int limit = view->nlines;
7840 6458efa5 2020-11-24 stsp
7841 6458efa5 2020-11-24 stsp werase(view->window);
7842 6458efa5 2020-11-24 stsp
7843 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7844 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7845 a5d43cac 2022-07-01 thomas --limit; /* border */
7846 6458efa5 2020-11-24 stsp
7847 6458efa5 2020-11-24 stsp if (limit == 0)
7848 6458efa5 2020-11-24 stsp return NULL;
7849 6458efa5 2020-11-24 stsp
7850 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7851 6458efa5 2020-11-24 stsp
7852 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7853 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7854 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7855 6458efa5 2020-11-24 stsp
7856 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7857 6458efa5 2020-11-24 stsp if (err) {
7858 6458efa5 2020-11-24 stsp free(line);
7859 6458efa5 2020-11-24 stsp return err;
7860 6458efa5 2020-11-24 stsp }
7861 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7862 6458efa5 2020-11-24 stsp wstandout(view->window);
7863 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7864 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7865 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7866 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7867 6458efa5 2020-11-24 stsp wstandend(view->window);
7868 6458efa5 2020-11-24 stsp free(wline);
7869 6458efa5 2020-11-24 stsp wline = NULL;
7870 6458efa5 2020-11-24 stsp free(line);
7871 6458efa5 2020-11-24 stsp line = NULL;
7872 6458efa5 2020-11-24 stsp if (--limit <= 0)
7873 6458efa5 2020-11-24 stsp return NULL;
7874 6458efa5 2020-11-24 stsp
7875 6458efa5 2020-11-24 stsp n = 0;
7876 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7877 6458efa5 2020-11-24 stsp char *line = NULL;
7878 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7879 6458efa5 2020-11-24 stsp
7880 84227eb1 2022-06-23 thomas if (s->show_date) {
7881 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7882 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7883 84227eb1 2022-06-23 thomas struct got_object_id *id;
7884 84227eb1 2022-06-23 thomas struct tm tm;
7885 84227eb1 2022-06-23 thomas time_t t;
7886 84227eb1 2022-06-23 thomas
7887 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7888 84227eb1 2022-06-23 thomas if (err)
7889 84227eb1 2022-06-23 thomas return err;
7890 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7891 84227eb1 2022-06-23 thomas if (err) {
7892 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7893 84227eb1 2022-06-23 thomas free(id);
7894 84227eb1 2022-06-23 thomas return err;
7895 84227eb1 2022-06-23 thomas }
7896 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7897 84227eb1 2022-06-23 thomas id);
7898 84227eb1 2022-06-23 thomas if (err) {
7899 84227eb1 2022-06-23 thomas free(id);
7900 84227eb1 2022-06-23 thomas return err;
7901 84227eb1 2022-06-23 thomas }
7902 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7903 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7904 84227eb1 2022-06-23 thomas } else {
7905 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7906 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7907 84227eb1 2022-06-23 thomas }
7908 84227eb1 2022-06-23 thomas free(id);
7909 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7910 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7911 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7912 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7913 84227eb1 2022-06-23 thomas }
7914 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7915 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7916 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7917 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7918 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7919 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7920 6458efa5 2020-11-24 stsp struct got_object_id *id;
7921 6458efa5 2020-11-24 stsp char *id_str;
7922 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7923 6458efa5 2020-11-24 stsp if (err)
7924 6458efa5 2020-11-24 stsp return err;
7925 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7926 6458efa5 2020-11-24 stsp if (err) {
7927 6458efa5 2020-11-24 stsp free(id);
7928 6458efa5 2020-11-24 stsp return err;
7929 6458efa5 2020-11-24 stsp }
7930 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7931 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7932 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7933 6458efa5 2020-11-24 stsp free(id);
7934 6458efa5 2020-11-24 stsp free(id_str);
7935 6458efa5 2020-11-24 stsp return err;
7936 6458efa5 2020-11-24 stsp }
7937 6458efa5 2020-11-24 stsp free(id);
7938 6458efa5 2020-11-24 stsp free(id_str);
7939 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7940 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7941 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7942 6458efa5 2020-11-24 stsp
7943 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7944 f91a2b48 2022-06-23 thomas 0, 0);
7945 6458efa5 2020-11-24 stsp if (err) {
7946 6458efa5 2020-11-24 stsp free(line);
7947 6458efa5 2020-11-24 stsp return err;
7948 6458efa5 2020-11-24 stsp }
7949 6458efa5 2020-11-24 stsp if (n == s->selected) {
7950 6458efa5 2020-11-24 stsp if (view->focussed)
7951 6458efa5 2020-11-24 stsp wstandout(view->window);
7952 6458efa5 2020-11-24 stsp s->selected_entry = re;
7953 6458efa5 2020-11-24 stsp }
7954 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7955 6458efa5 2020-11-24 stsp if (tc)
7956 6458efa5 2020-11-24 stsp wattr_on(view->window,
7957 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7958 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7959 6458efa5 2020-11-24 stsp if (tc)
7960 6458efa5 2020-11-24 stsp wattr_off(view->window,
7961 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7962 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7963 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7964 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7965 6458efa5 2020-11-24 stsp wstandend(view->window);
7966 6458efa5 2020-11-24 stsp free(line);
7967 6458efa5 2020-11-24 stsp free(wline);
7968 6458efa5 2020-11-24 stsp wline = NULL;
7969 6458efa5 2020-11-24 stsp n++;
7970 6458efa5 2020-11-24 stsp s->ndisplayed++;
7971 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7972 6458efa5 2020-11-24 stsp
7973 6458efa5 2020-11-24 stsp limit--;
7974 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7975 6458efa5 2020-11-24 stsp }
7976 6458efa5 2020-11-24 stsp
7977 a5d43cac 2022-07-01 thomas view_border(view);
7978 6458efa5 2020-11-24 stsp return err;
7979 6458efa5 2020-11-24 stsp }
7980 6458efa5 2020-11-24 stsp
7981 6458efa5 2020-11-24 stsp static const struct got_error *
7982 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7983 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7984 c42c9805 2020-11-24 stsp {
7985 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7986 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7987 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7988 c42c9805 2020-11-24 stsp
7989 c42c9805 2020-11-24 stsp *new_view = NULL;
7990 c42c9805 2020-11-24 stsp
7991 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7992 c42c9805 2020-11-24 stsp if (err) {
7993 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7994 c42c9805 2020-11-24 stsp return err;
7995 c42c9805 2020-11-24 stsp else
7996 c42c9805 2020-11-24 stsp return NULL;
7997 c42c9805 2020-11-24 stsp }
7998 c42c9805 2020-11-24 stsp
7999 c42c9805 2020-11-24 stsp
8000 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8001 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8002 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8003 c42c9805 2020-11-24 stsp goto done;
8004 c42c9805 2020-11-24 stsp }
8005 c42c9805 2020-11-24 stsp
8006 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8007 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8008 c42c9805 2020-11-24 stsp if (err)
8009 c42c9805 2020-11-24 stsp goto done;
8010 c42c9805 2020-11-24 stsp
8011 c42c9805 2020-11-24 stsp *new_view = tree_view;
8012 c42c9805 2020-11-24 stsp done:
8013 c42c9805 2020-11-24 stsp free(commit_id);
8014 c42c9805 2020-11-24 stsp return err;
8015 07dd3ed3 2022-08-06 thomas }
8016 07dd3ed3 2022-08-06 thomas
8017 07dd3ed3 2022-08-06 thomas static const struct got_error *
8018 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8019 07dd3ed3 2022-08-06 thomas {
8020 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8021 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8022 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8023 07dd3ed3 2022-08-06 thomas
8024 07dd3ed3 2022-08-06 thomas g = view->gline;
8025 07dd3ed3 2022-08-06 thomas view->gline = 0;
8026 07dd3ed3 2022-08-06 thomas
8027 07dd3ed3 2022-08-06 thomas if (g == 0)
8028 07dd3ed3 2022-08-06 thomas g = 1;
8029 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8030 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8031 07dd3ed3 2022-08-06 thomas
8032 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8033 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8034 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8035 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8036 07dd3ed3 2022-08-06 thomas return NULL;
8037 07dd3ed3 2022-08-06 thomas }
8038 07dd3ed3 2022-08-06 thomas
8039 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8040 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8041 07dd3ed3 2022-08-06 thomas if (err)
8042 07dd3ed3 2022-08-06 thomas return err;
8043 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8044 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8045 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8046 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8047 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8048 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8049 07dd3ed3 2022-08-06 thomas
8050 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8051 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8052 07dd3ed3 2022-08-06 thomas
8053 07dd3ed3 2022-08-06 thomas return NULL;
8054 07dd3ed3 2022-08-06 thomas
8055 c42c9805 2020-11-24 stsp }
8056 07dd3ed3 2022-08-06 thomas
8057 c42c9805 2020-11-24 stsp static const struct got_error *
8058 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8059 6458efa5 2020-11-24 stsp {
8060 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8061 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8062 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8063 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8064 6458efa5 2020-11-24 stsp
8065 07dd3ed3 2022-08-06 thomas if (view->gline)
8066 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8067 07dd3ed3 2022-08-06 thomas
8068 6458efa5 2020-11-24 stsp switch (ch) {
8069 6458efa5 2020-11-24 stsp case 'i':
8070 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8071 07b0611c 2022-06-23 thomas view->count = 0;
8072 3bfadbd4 2021-11-20 thomas break;
8073 84227eb1 2022-06-23 thomas case 'm':
8074 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8075 07b0611c 2022-06-23 thomas view->count = 0;
8076 84227eb1 2022-06-23 thomas break;
8077 98182bd0 2021-11-20 thomas case 'o':
8078 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8079 07b0611c 2022-06-23 thomas view->count = 0;
8080 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8081 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8082 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8083 c591440f 2021-11-20 thomas if (err)
8084 c591440f 2021-11-20 thomas break;
8085 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8086 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8087 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8088 3bfadbd4 2021-11-20 thomas if (err)
8089 3bfadbd4 2021-11-20 thomas break;
8090 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8091 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8092 6458efa5 2020-11-24 stsp break;
8093 6458efa5 2020-11-24 stsp case KEY_ENTER:
8094 6458efa5 2020-11-24 stsp case '\r':
8095 07b0611c 2022-06-23 thomas view->count = 0;
8096 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8097 a5d43cac 2022-07-01 thomas break;
8098 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8099 6458efa5 2020-11-24 stsp break;
8100 1be4947a 2022-07-22 thomas case 'T':
8101 07b0611c 2022-06-23 thomas view->count = 0;
8102 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8103 c42c9805 2020-11-24 stsp break;
8104 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8105 c42c9805 2020-11-24 stsp break;
8106 e4526bf5 2021-09-03 naddy case 'g':
8107 e4526bf5 2021-09-03 naddy case KEY_HOME:
8108 e4526bf5 2021-09-03 naddy s->selected = 0;
8109 07b0611c 2022-06-23 thomas view->count = 0;
8110 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8111 e4526bf5 2021-09-03 naddy break;
8112 e4526bf5 2021-09-03 naddy case 'G':
8113 a5d43cac 2022-07-01 thomas case KEY_END: {
8114 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8115 a5d43cac 2022-07-01 thomas
8116 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8117 a5d43cac 2022-07-01 thomas --eos; /* border */
8118 e4526bf5 2021-09-03 naddy s->selected = 0;
8119 07b0611c 2022-06-23 thomas view->count = 0;
8120 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8121 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8122 e4526bf5 2021-09-03 naddy if (re == NULL)
8123 e4526bf5 2021-09-03 naddy break;
8124 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8125 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8126 e4526bf5 2021-09-03 naddy }
8127 e4526bf5 2021-09-03 naddy if (n > 0)
8128 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8129 e4526bf5 2021-09-03 naddy break;
8130 a5d43cac 2022-07-01 thomas }
8131 6458efa5 2020-11-24 stsp case 'k':
8132 6458efa5 2020-11-24 stsp case KEY_UP:
8133 f7140bf5 2021-10-17 thomas case CTRL('p'):
8134 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8135 6458efa5 2020-11-24 stsp s->selected--;
8136 6458efa5 2020-11-24 stsp break;
8137 34ba6917 2020-11-30 stsp }
8138 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8139 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8140 07b0611c 2022-06-23 thomas view->count = 0;
8141 6458efa5 2020-11-24 stsp break;
8142 70f17a53 2022-06-13 thomas case CTRL('u'):
8143 23427b14 2022-06-23 thomas case 'u':
8144 70f17a53 2022-06-13 thomas nscroll /= 2;
8145 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8146 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8147 6458efa5 2020-11-24 stsp case CTRL('b'):
8148 1c5e5faa 2022-06-23 thomas case 'b':
8149 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8150 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8151 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8152 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8153 07b0611c 2022-06-23 thomas view->count = 0;
8154 6458efa5 2020-11-24 stsp break;
8155 6458efa5 2020-11-24 stsp case 'j':
8156 6458efa5 2020-11-24 stsp case KEY_DOWN:
8157 f7140bf5 2021-10-17 thomas case CTRL('n'):
8158 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8159 6458efa5 2020-11-24 stsp s->selected++;
8160 6458efa5 2020-11-24 stsp break;
8161 6458efa5 2020-11-24 stsp }
8162 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8163 6458efa5 2020-11-24 stsp /* can't scroll any further */
8164 07b0611c 2022-06-23 thomas view->count = 0;
8165 6458efa5 2020-11-24 stsp break;
8166 07b0611c 2022-06-23 thomas }
8167 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8168 6458efa5 2020-11-24 stsp break;
8169 70f17a53 2022-06-13 thomas case CTRL('d'):
8170 23427b14 2022-06-23 thomas case 'd':
8171 70f17a53 2022-06-13 thomas nscroll /= 2;
8172 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8173 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8174 6458efa5 2020-11-24 stsp case CTRL('f'):
8175 1c5e5faa 2022-06-23 thomas case 'f':
8176 4c2d69cb 2022-06-23 thomas case ' ':
8177 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8178 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8179 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8180 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8181 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8182 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8183 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8184 07b0611c 2022-06-23 thomas view->count = 0;
8185 6458efa5 2020-11-24 stsp break;
8186 6458efa5 2020-11-24 stsp }
8187 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8188 6458efa5 2020-11-24 stsp break;
8189 6458efa5 2020-11-24 stsp case CTRL('l'):
8190 07b0611c 2022-06-23 thomas view->count = 0;
8191 8924d611 2020-12-26 stsp tog_free_refs();
8192 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8193 8924d611 2020-12-26 stsp if (err)
8194 8924d611 2020-12-26 stsp break;
8195 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8196 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8197 6458efa5 2020-11-24 stsp break;
8198 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8199 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8200 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8201 6458efa5 2020-11-24 stsp break;
8202 6458efa5 2020-11-24 stsp default:
8203 07b0611c 2022-06-23 thomas view->count = 0;
8204 6458efa5 2020-11-24 stsp break;
8205 6458efa5 2020-11-24 stsp }
8206 6458efa5 2020-11-24 stsp
8207 6458efa5 2020-11-24 stsp return err;
8208 6458efa5 2020-11-24 stsp }
8209 6458efa5 2020-11-24 stsp
8210 6458efa5 2020-11-24 stsp __dead static void
8211 6458efa5 2020-11-24 stsp usage_ref(void)
8212 6458efa5 2020-11-24 stsp {
8213 6458efa5 2020-11-24 stsp endwin();
8214 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8215 6458efa5 2020-11-24 stsp getprogname());
8216 6458efa5 2020-11-24 stsp exit(1);
8217 6458efa5 2020-11-24 stsp }
8218 6458efa5 2020-11-24 stsp
8219 6458efa5 2020-11-24 stsp static const struct got_error *
8220 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8221 6458efa5 2020-11-24 stsp {
8222 6458efa5 2020-11-24 stsp const struct got_error *error;
8223 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8224 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8225 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8226 6458efa5 2020-11-24 stsp int ch;
8227 6458efa5 2020-11-24 stsp struct tog_view *view;
8228 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8229 6458efa5 2020-11-24 stsp
8230 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8231 6458efa5 2020-11-24 stsp switch (ch) {
8232 6458efa5 2020-11-24 stsp case 'r':
8233 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8234 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8235 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8236 6458efa5 2020-11-24 stsp optarg);
8237 6458efa5 2020-11-24 stsp break;
8238 6458efa5 2020-11-24 stsp default:
8239 e99e2d15 2020-11-24 naddy usage_ref();
8240 6458efa5 2020-11-24 stsp /* NOTREACHED */
8241 6458efa5 2020-11-24 stsp }
8242 6458efa5 2020-11-24 stsp }
8243 6458efa5 2020-11-24 stsp
8244 6458efa5 2020-11-24 stsp argc -= optind;
8245 6458efa5 2020-11-24 stsp argv += optind;
8246 6458efa5 2020-11-24 stsp
8247 6458efa5 2020-11-24 stsp if (argc > 1)
8248 e99e2d15 2020-11-24 naddy usage_ref();
8249 7cd52833 2022-06-23 thomas
8250 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8251 7cd52833 2022-06-23 thomas if (error != NULL)
8252 7cd52833 2022-06-23 thomas goto done;
8253 6458efa5 2020-11-24 stsp
8254 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8255 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8256 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8257 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8258 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8259 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8260 c156c7a4 2020-12-18 stsp goto done;
8261 6458efa5 2020-11-24 stsp if (worktree)
8262 6458efa5 2020-11-24 stsp repo_path =
8263 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8264 6458efa5 2020-11-24 stsp else
8265 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8266 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8267 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8268 c156c7a4 2020-12-18 stsp goto done;
8269 c156c7a4 2020-12-18 stsp }
8270 6458efa5 2020-11-24 stsp }
8271 6458efa5 2020-11-24 stsp
8272 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8273 6458efa5 2020-11-24 stsp if (error != NULL)
8274 6458efa5 2020-11-24 stsp goto done;
8275 6458efa5 2020-11-24 stsp
8276 6458efa5 2020-11-24 stsp init_curses();
8277 6458efa5 2020-11-24 stsp
8278 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8279 51a10b52 2020-12-26 stsp if (error)
8280 51a10b52 2020-12-26 stsp goto done;
8281 51a10b52 2020-12-26 stsp
8282 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8283 6458efa5 2020-11-24 stsp if (error)
8284 6458efa5 2020-11-24 stsp goto done;
8285 6458efa5 2020-11-24 stsp
8286 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8287 6458efa5 2020-11-24 stsp if (view == NULL) {
8288 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8289 6458efa5 2020-11-24 stsp goto done;
8290 6458efa5 2020-11-24 stsp }
8291 6458efa5 2020-11-24 stsp
8292 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8293 6458efa5 2020-11-24 stsp if (error)
8294 6458efa5 2020-11-24 stsp goto done;
8295 6458efa5 2020-11-24 stsp
8296 6458efa5 2020-11-24 stsp if (worktree) {
8297 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8298 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8299 6458efa5 2020-11-24 stsp worktree = NULL;
8300 6458efa5 2020-11-24 stsp }
8301 6458efa5 2020-11-24 stsp error = view_loop(view);
8302 6458efa5 2020-11-24 stsp done:
8303 6458efa5 2020-11-24 stsp free(repo_path);
8304 6458efa5 2020-11-24 stsp free(cwd);
8305 1d0f4054 2021-06-17 stsp if (repo) {
8306 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8307 1d0f4054 2021-06-17 stsp if (close_err)
8308 1d0f4054 2021-06-17 stsp error = close_err;
8309 1d0f4054 2021-06-17 stsp }
8310 7cd52833 2022-06-23 thomas if (pack_fds) {
8311 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8312 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8313 7cd52833 2022-06-23 thomas if (error == NULL)
8314 7cd52833 2022-06-23 thomas error = pack_err;
8315 7cd52833 2022-06-23 thomas }
8316 51a10b52 2020-12-26 stsp tog_free_refs();
8317 6458efa5 2020-11-24 stsp return error;
8318 6458efa5 2020-11-24 stsp }
8319 6458efa5 2020-11-24 stsp
8320 2a31b33b 2022-07-23 thomas static const struct got_error *
8321 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8322 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8323 2a31b33b 2022-07-23 thomas {
8324 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8325 2a31b33b 2022-07-23 thomas
8326 2a31b33b 2022-07-23 thomas *new_view = NULL;
8327 2a31b33b 2022-07-23 thomas
8328 2a31b33b 2022-07-23 thomas switch (request) {
8329 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8330 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8331 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8332 2a31b33b 2022-07-23 thomas
8333 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8334 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8335 2a31b33b 2022-07-23 thomas view, s->repo);
8336 2a31b33b 2022-07-23 thomas } else
8337 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8338 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8339 2a31b33b 2022-07-23 thomas break;
8340 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8341 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8342 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8343 2a31b33b 2022-07-23 thomas
8344 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8345 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8346 2a31b33b 2022-07-23 thomas s->repo);
8347 2a31b33b 2022-07-23 thomas } else
8348 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8349 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8350 2a31b33b 2022-07-23 thomas break;
8351 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8352 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8353 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8354 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8355 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8356 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8357 2a31b33b 2022-07-23 thomas &view->state.tree);
8358 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8359 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8360 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8361 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8362 2a31b33b 2022-07-23 thomas else
8363 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8364 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8365 2a31b33b 2022-07-23 thomas break;
8366 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8367 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8368 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8369 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8370 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8371 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8372 2a31b33b 2022-07-23 thomas view->state.log.repo);
8373 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8374 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8375 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8376 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8377 2a31b33b 2022-07-23 thomas else
8378 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8379 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8380 2a31b33b 2022-07-23 thomas break;
8381 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
8382 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8383 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
8384 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
8385 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8386 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
8387 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8388 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
8389 2a31b33b 2022-07-23 thomas else
8390 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
8391 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8392 2a31b33b 2022-07-23 thomas if (err)
8393 2a31b33b 2022-07-23 thomas view_close(*new_view);
8394 2a31b33b 2022-07-23 thomas break;
8395 2a31b33b 2022-07-23 thomas default:
8396 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8397 2a31b33b 2022-07-23 thomas }
8398 2a31b33b 2022-07-23 thomas
8399 2a31b33b 2022-07-23 thomas return err;
8400 2a31b33b 2022-07-23 thomas }
8401 2a31b33b 2022-07-23 thomas
8402 a5d43cac 2022-07-01 thomas /*
8403 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8404 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8405 a5d43cac 2022-07-01 thomas */
8406 6458efa5 2020-11-24 stsp static void
8407 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8408 a5d43cac 2022-07-01 thomas {
8409 a5d43cac 2022-07-01 thomas switch (view->type) {
8410 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8411 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8412 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8413 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8414 a5d43cac 2022-07-01 thomas 1);
8415 a5d43cac 2022-07-01 thomas break;
8416 a5d43cac 2022-07-01 thomas }
8417 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8418 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8419 a5d43cac 2022-07-01 thomas else
8420 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8421 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8422 a5d43cac 2022-07-01 thomas break;
8423 a5d43cac 2022-07-01 thomas }
8424 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8425 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8426 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8427 a5d43cac 2022-07-01 thomas break;
8428 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8429 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8430 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8431 a5d43cac 2022-07-01 thomas break;
8432 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8433 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8434 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8435 a5d43cac 2022-07-01 thomas break;
8436 a5d43cac 2022-07-01 thomas default:
8437 a5d43cac 2022-07-01 thomas break;
8438 a5d43cac 2022-07-01 thomas }
8439 a5d43cac 2022-07-01 thomas
8440 a5d43cac 2022-07-01 thomas view->offset = 0;
8441 a5d43cac 2022-07-01 thomas }
8442 a5d43cac 2022-07-01 thomas
8443 a5d43cac 2022-07-01 thomas /*
8444 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8445 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8446 a5d43cac 2022-07-01 thomas */
8447 a5d43cac 2022-07-01 thomas static const struct got_error *
8448 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8449 a5d43cac 2022-07-01 thomas {
8450 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8451 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8452 a5d43cac 2022-07-01 thomas int *selected = NULL;
8453 a5d43cac 2022-07-01 thomas int header, offset;
8454 a5d43cac 2022-07-01 thomas
8455 a5d43cac 2022-07-01 thomas switch (view->type) {
8456 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8457 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8458 a5d43cac 2022-07-01 thomas header = 3;
8459 a5d43cac 2022-07-01 thomas scrolld = NULL;
8460 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8461 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8462 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8463 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8464 a5d43cac 2022-07-01 thomas view->offset = offset;
8465 a5d43cac 2022-07-01 thomas }
8466 a5d43cac 2022-07-01 thomas break;
8467 a5d43cac 2022-07-01 thomas }
8468 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8469 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8470 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8471 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8472 a5d43cac 2022-07-01 thomas selected = &s->selected;
8473 a5d43cac 2022-07-01 thomas break;
8474 a5d43cac 2022-07-01 thomas }
8475 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8476 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8477 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8478 a5d43cac 2022-07-01 thomas header = 3;
8479 a5d43cac 2022-07-01 thomas selected = &s->selected;
8480 a5d43cac 2022-07-01 thomas break;
8481 a5d43cac 2022-07-01 thomas }
8482 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8483 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8484 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8485 a5d43cac 2022-07-01 thomas header = 5;
8486 a5d43cac 2022-07-01 thomas selected = &s->selected;
8487 a5d43cac 2022-07-01 thomas break;
8488 a5d43cac 2022-07-01 thomas }
8489 a5d43cac 2022-07-01 thomas default:
8490 a5d43cac 2022-07-01 thomas selected = NULL;
8491 a5d43cac 2022-07-01 thomas scrolld = NULL;
8492 a5d43cac 2022-07-01 thomas header = 0;
8493 a5d43cac 2022-07-01 thomas break;
8494 a5d43cac 2022-07-01 thomas }
8495 a5d43cac 2022-07-01 thomas
8496 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8497 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8498 a5d43cac 2022-07-01 thomas view->offset = offset;
8499 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8500 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8501 a5d43cac 2022-07-01 thomas *selected -= offset;
8502 a5d43cac 2022-07-01 thomas }
8503 a5d43cac 2022-07-01 thomas }
8504 a5d43cac 2022-07-01 thomas
8505 a5d43cac 2022-07-01 thomas return err;
8506 a5d43cac 2022-07-01 thomas }
8507 a5d43cac 2022-07-01 thomas
8508 a5d43cac 2022-07-01 thomas static void
8509 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8510 ce5b7c56 2019-07-09 stsp {
8511 6059809a 2020-12-17 stsp size_t i;
8512 9f7d7167 2018-04-29 stsp
8513 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8514 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8515 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8516 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8517 ce5b7c56 2019-07-09 stsp }
8518 6879ba42 2020-10-01 naddy fputc('\n', fp);
8519 ce5b7c56 2019-07-09 stsp }
8520 ce5b7c56 2019-07-09 stsp
8521 4ed7e80c 2018-05-20 stsp __dead static void
8522 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8523 9f7d7167 2018-04-29 stsp {
8524 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8525 6879ba42 2020-10-01 naddy
8526 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8527 6879ba42 2020-10-01 naddy getprogname());
8528 6879ba42 2020-10-01 naddy if (hflag) {
8529 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8530 6879ba42 2020-10-01 naddy list_commands(fp);
8531 ee85c5e8 2020-02-29 stsp }
8532 6879ba42 2020-10-01 naddy exit(status);
8533 9f7d7167 2018-04-29 stsp }
8534 9f7d7167 2018-04-29 stsp
8535 c2301be8 2018-04-30 stsp static char **
8536 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8537 c2301be8 2018-04-30 stsp {
8538 ee85c5e8 2020-02-29 stsp va_list ap;
8539 c2301be8 2018-04-30 stsp char **argv;
8540 ee85c5e8 2020-02-29 stsp int i;
8541 c2301be8 2018-04-30 stsp
8542 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8543 ee85c5e8 2020-02-29 stsp
8544 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8545 c2301be8 2018-04-30 stsp if (argv == NULL)
8546 c2301be8 2018-04-30 stsp err(1, "calloc");
8547 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8548 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8549 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8550 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8551 c2301be8 2018-04-30 stsp }
8552 c2301be8 2018-04-30 stsp
8553 ee85c5e8 2020-02-29 stsp va_end(ap);
8554 c2301be8 2018-04-30 stsp return argv;
8555 ee85c5e8 2020-02-29 stsp }
8556 ee85c5e8 2020-02-29 stsp
8557 ee85c5e8 2020-02-29 stsp /*
8558 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8559 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8560 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8561 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8562 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8563 ee85c5e8 2020-02-29 stsp */
8564 ee85c5e8 2020-02-29 stsp static const struct got_error *
8565 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8566 ee85c5e8 2020-02-29 stsp {
8567 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8568 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8569 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8570 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8571 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8572 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8573 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8574 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8575 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8576 84de9106 2020-12-26 stsp
8577 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8578 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8579 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8580 ee85c5e8 2020-02-29 stsp
8581 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8582 7cd52833 2022-06-23 thomas if (error != NULL)
8583 7cd52833 2022-06-23 thomas goto done;
8584 7cd52833 2022-06-23 thomas
8585 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8586 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8587 ee85c5e8 2020-02-29 stsp goto done;
8588 ee85c5e8 2020-02-29 stsp
8589 ee85c5e8 2020-02-29 stsp if (worktree)
8590 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8591 ee85c5e8 2020-02-29 stsp else
8592 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8593 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8594 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8595 ee85c5e8 2020-02-29 stsp goto done;
8596 ee85c5e8 2020-02-29 stsp }
8597 ee85c5e8 2020-02-29 stsp
8598 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8599 ee85c5e8 2020-02-29 stsp if (error != NULL)
8600 ee85c5e8 2020-02-29 stsp goto done;
8601 ee85c5e8 2020-02-29 stsp
8602 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8603 ee85c5e8 2020-02-29 stsp repo, worktree);
8604 ee85c5e8 2020-02-29 stsp if (error)
8605 ee85c5e8 2020-02-29 stsp goto done;
8606 ee85c5e8 2020-02-29 stsp
8607 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8608 84de9106 2020-12-26 stsp if (error)
8609 84de9106 2020-12-26 stsp goto done;
8610 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8611 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8612 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8613 ee85c5e8 2020-02-29 stsp if (error)
8614 ee85c5e8 2020-02-29 stsp goto done;
8615 ee85c5e8 2020-02-29 stsp
8616 ee85c5e8 2020-02-29 stsp if (worktree) {
8617 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8618 ee85c5e8 2020-02-29 stsp worktree = NULL;
8619 ee85c5e8 2020-02-29 stsp }
8620 ee85c5e8 2020-02-29 stsp
8621 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8622 945f9229 2022-04-16 thomas if (error)
8623 945f9229 2022-04-16 thomas goto done;
8624 945f9229 2022-04-16 thomas
8625 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8626 ee85c5e8 2020-02-29 stsp if (error) {
8627 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8628 ee85c5e8 2020-02-29 stsp goto done;
8629 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8630 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8631 6879ba42 2020-10-01 naddy usage(1, 1);
8632 ee85c5e8 2020-02-29 stsp /* not reached */
8633 ee85c5e8 2020-02-29 stsp }
8634 ee85c5e8 2020-02-29 stsp
8635 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8636 ee85c5e8 2020-02-29 stsp if (error)
8637 ee85c5e8 2020-02-29 stsp goto done;
8638 ee85c5e8 2020-02-29 stsp
8639 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8640 ee85c5e8 2020-02-29 stsp argc = 4;
8641 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8642 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8643 ee85c5e8 2020-02-29 stsp done:
8644 1d0f4054 2021-06-17 stsp if (repo) {
8645 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8646 1d0f4054 2021-06-17 stsp if (error == NULL)
8647 1d0f4054 2021-06-17 stsp error = close_err;
8648 1d0f4054 2021-06-17 stsp }
8649 945f9229 2022-04-16 thomas if (commit)
8650 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8651 ee85c5e8 2020-02-29 stsp if (worktree)
8652 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8653 7cd52833 2022-06-23 thomas if (pack_fds) {
8654 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8655 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8656 7cd52833 2022-06-23 thomas if (error == NULL)
8657 7cd52833 2022-06-23 thomas error = pack_err;
8658 7cd52833 2022-06-23 thomas }
8659 ee85c5e8 2020-02-29 stsp free(id);
8660 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8661 ee85c5e8 2020-02-29 stsp free(commit_id);
8662 ee85c5e8 2020-02-29 stsp free(cwd);
8663 ee85c5e8 2020-02-29 stsp free(repo_path);
8664 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8665 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8666 ee85c5e8 2020-02-29 stsp int i;
8667 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8668 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8669 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8670 ee85c5e8 2020-02-29 stsp }
8671 87670572 2020-12-26 naddy tog_free_refs();
8672 ee85c5e8 2020-02-29 stsp return error;
8673 c2301be8 2018-04-30 stsp }
8674 c2301be8 2018-04-30 stsp
8675 9f7d7167 2018-04-29 stsp int
8676 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8677 9f7d7167 2018-04-29 stsp {
8678 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8679 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8680 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8681 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8682 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8683 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8684 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8685 83cd27f8 2020-01-13 stsp };
8686 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8687 9f7d7167 2018-04-29 stsp
8688 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
8689 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
8690 cacf1690 2022-09-06 thomas
8691 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8692 9f7d7167 2018-04-29 stsp
8693 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8694 9f7d7167 2018-04-29 stsp switch (ch) {
8695 9f7d7167 2018-04-29 stsp case 'h':
8696 9f7d7167 2018-04-29 stsp hflag = 1;
8697 9f7d7167 2018-04-29 stsp break;
8698 53ccebc2 2019-07-30 stsp case 'V':
8699 53ccebc2 2019-07-30 stsp Vflag = 1;
8700 53ccebc2 2019-07-30 stsp break;
8701 9f7d7167 2018-04-29 stsp default:
8702 6879ba42 2020-10-01 naddy usage(hflag, 1);
8703 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8704 9f7d7167 2018-04-29 stsp }
8705 9f7d7167 2018-04-29 stsp }
8706 9f7d7167 2018-04-29 stsp
8707 9f7d7167 2018-04-29 stsp argc -= optind;
8708 9f7d7167 2018-04-29 stsp argv += optind;
8709 9814e6a3 2020-09-27 naddy optind = 1;
8710 c2301be8 2018-04-30 stsp optreset = 1;
8711 9f7d7167 2018-04-29 stsp
8712 53ccebc2 2019-07-30 stsp if (Vflag) {
8713 53ccebc2 2019-07-30 stsp got_version_print_str();
8714 6879ba42 2020-10-01 naddy return 0;
8715 53ccebc2 2019-07-30 stsp }
8716 4010e238 2020-12-04 stsp
8717 4010e238 2020-12-04 stsp #ifndef PROFILE
8718 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8719 4010e238 2020-12-04 stsp NULL) == -1)
8720 4010e238 2020-12-04 stsp err(1, "pledge");
8721 4010e238 2020-12-04 stsp #endif
8722 53ccebc2 2019-07-30 stsp
8723 c2301be8 2018-04-30 stsp if (argc == 0) {
8724 f29d3e89 2018-06-23 stsp if (hflag)
8725 6879ba42 2020-10-01 naddy usage(hflag, 0);
8726 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8727 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8728 c2301be8 2018-04-30 stsp argc = 1;
8729 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8730 c2301be8 2018-04-30 stsp } else {
8731 6059809a 2020-12-17 stsp size_t i;
8732 9f7d7167 2018-04-29 stsp
8733 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8734 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8735 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8736 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8737 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8738 9f7d7167 2018-04-29 stsp break;
8739 9f7d7167 2018-04-29 stsp }
8740 9f7d7167 2018-04-29 stsp }
8741 ee85c5e8 2020-02-29 stsp }
8742 3642c4c6 2019-07-09 stsp
8743 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8744 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8745 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8746 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8747 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8748 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8749 adf4c9e0 2022-07-03 thomas }
8750 adf4c9e0 2022-07-03 thomas
8751 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8752 ee85c5e8 2020-02-29 stsp if (argc != 1)
8753 6879ba42 2020-10-01 naddy usage(0, 1);
8754 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8755 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8756 ee85c5e8 2020-02-29 stsp } else {
8757 ee85c5e8 2020-02-29 stsp if (hflag)
8758 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8759 ee85c5e8 2020-02-29 stsp else
8760 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8761 9f7d7167 2018-04-29 stsp }
8762 9f7d7167 2018-04-29 stsp
8763 9f7d7167 2018-04-29 stsp endwin();
8764 b46c1e04 2020-09-20 naddy putchar('\n');
8765 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8766 a2f4a359 2020-02-28 stsp int i;
8767 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8768 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8769 a2f4a359 2020-02-28 stsp free(cmd_argv);
8770 a2f4a359 2020-02-28 stsp }
8771 a2f4a359 2020-02-28 stsp
8772 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8773 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8774 9f7d7167 2018-04-29 stsp return 0;
8775 9f7d7167 2018-04-29 stsp }