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 1a76625f 2018-10-22 stsp struct commit_queue *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 1a76625f 2018-10-22 stsp };
365 1a76625f 2018-10-22 stsp
366 1a76625f 2018-10-22 stsp struct tog_log_view_state {
367 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
368 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
371 b01e7d3b 2018-08-04 stsp int selected;
372 b01e7d3b 2018-08-04 stsp char *in_repo_path;
373 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
374 b672a97a 2020-01-27 stsp int log_branches;
375 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
376 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
377 1a76625f 2018-10-22 stsp sig_atomic_t quit;
378 1a76625f 2018-10-22 stsp pthread_t thread;
379 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
380 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
381 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
382 11b20872 2019-11-08 stsp struct tog_colors colors;
383 f69c5a46 2022-07-19 thomas int use_committer;
384 ba4f502b 2018-08-04 stsp };
385 11b20872 2019-11-08 stsp
386 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
390 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
394 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
395 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
396 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
397 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
400 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
401 ba4f502b 2018-08-04 stsp
402 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
403 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
404 e9424729 2018-08-04 stsp int nlines;
405 e9424729 2018-08-04 stsp
406 e9424729 2018-08-04 stsp struct tog_view *view;
407 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
408 e9424729 2018-08-04 stsp int *quit;
409 e9424729 2018-08-04 stsp };
410 e9424729 2018-08-04 stsp
411 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
412 e9424729 2018-08-04 stsp const char *path;
413 e9424729 2018-08-04 stsp struct got_repository *repo;
414 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
415 e9424729 2018-08-04 stsp int *complete;
416 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
417 fc06ba56 2019-08-22 stsp void *cancel_arg;
418 e9424729 2018-08-04 stsp };
419 e9424729 2018-08-04 stsp
420 e9424729 2018-08-04 stsp struct tog_blame {
421 e9424729 2018-08-04 stsp FILE *f;
422 be659d10 2020-11-18 stsp off_t filesize;
423 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
424 6fcac457 2018-11-19 stsp int nlines;
425 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
426 e9424729 2018-08-04 stsp pthread_t thread;
427 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
428 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
429 e9424729 2018-08-04 stsp const char *path;
430 7cd52833 2022-06-23 thomas int *pack_fds;
431 e9424729 2018-08-04 stsp };
432 e9424729 2018-08-04 stsp
433 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
434 7cbe629d 2018-08-04 stsp int first_displayed_line;
435 7cbe629d 2018-08-04 stsp int last_displayed_line;
436 7cbe629d 2018-08-04 stsp int selected_line;
437 4fc71f3b 2022-07-12 thomas int last_diffed_line;
438 7cbe629d 2018-08-04 stsp int blame_complete;
439 e5a0f69f 2018-08-18 stsp int eof;
440 e5a0f69f 2018-08-18 stsp int done;
441 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
442 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
443 e5a0f69f 2018-08-18 stsp char *path;
444 7cbe629d 2018-08-04 stsp struct got_repository *repo;
445 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
446 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
447 e9424729 2018-08-04 stsp struct tog_blame blame;
448 6c4c42e0 2019-06-24 stsp int matched_line;
449 11b20872 2019-11-08 stsp struct tog_colors colors;
450 ad80ab7b 2018-08-04 stsp };
451 ad80ab7b 2018-08-04 stsp
452 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
453 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
454 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 ad80ab7b 2018-08-04 stsp int selected;
458 ad80ab7b 2018-08-04 stsp };
459 ad80ab7b 2018-08-04 stsp
460 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
461 ad80ab7b 2018-08-04 stsp
462 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
463 ad80ab7b 2018-08-04 stsp char *tree_label;
464 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
465 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
466 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
469 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
470 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
471 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
473 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
474 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
475 6458efa5 2020-11-24 stsp struct tog_colors colors;
476 6458efa5 2020-11-24 stsp };
477 6458efa5 2020-11-24 stsp
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
479 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
480 6458efa5 2020-11-24 stsp struct got_reference *ref;
481 6458efa5 2020-11-24 stsp int idx;
482 6458efa5 2020-11-24 stsp };
483 6458efa5 2020-11-24 stsp
484 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
485 6458efa5 2020-11-24 stsp
486 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
487 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
490 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
491 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 6458efa5 2020-11-24 stsp struct got_repository *repo;
493 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
494 bddb1296 2019-11-08 stsp struct tog_colors colors;
495 7cbe629d 2018-08-04 stsp };
496 7cbe629d 2018-08-04 stsp
497 669b5ffa 2018-10-07 stsp /*
498 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
499 669b5ffa 2018-10-07 stsp *
500 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
501 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
502 669b5ffa 2018-10-07 stsp * there is enough screen estate.
503 669b5ffa 2018-10-07 stsp *
504 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
505 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
506 669b5ffa 2018-10-07 stsp *
507 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
508 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
509 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
510 669b5ffa 2018-10-07 stsp *
511 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
512 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
513 669b5ffa 2018-10-07 stsp */
514 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
515 669b5ffa 2018-10-07 stsp
516 cc3c9aac 2018-08-01 stsp struct tog_view {
517 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
518 26ed57b2 2018-05-19 stsp WINDOW *window;
519 26ed57b2 2018-05-19 stsp PANEL *panel;
520 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
522 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
523 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
524 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
526 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
527 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
528 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
529 9970f7fc 2020-12-03 stsp int dying;
530 669b5ffa 2018-10-07 stsp struct tog_view *parent;
531 669b5ffa 2018-10-07 stsp struct tog_view *child;
532 5dc9f4bc 2018-08-04 stsp
533 e78dc838 2020-12-04 stsp /*
534 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
535 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
536 e78dc838 2020-12-04 stsp * between parent and child.
537 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
538 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
539 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
540 e78dc838 2020-12-04 stsp * situations.
541 e78dc838 2020-12-04 stsp */
542 e78dc838 2020-12-04 stsp int focus_child;
543 e78dc838 2020-12-04 stsp
544 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
545 5dc9f4bc 2018-08-04 stsp /* type-specific state */
546 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
547 5dc9f4bc 2018-08-04 stsp union {
548 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
549 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
550 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
551 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
552 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
553 5dc9f4bc 2018-08-04 stsp } state;
554 e5a0f69f 2018-08-18 stsp
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
556 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
557 e78dc838 2020-12-04 stsp struct tog_view *, int);
558 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
559 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
560 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
561 60493ae3 2019-06-20 stsp
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
563 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
564 c0c4acc8 2021-01-24 stsp int search_started;
565 60493ae3 2019-06-20 stsp int searching;
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
567 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
568 60493ae3 2019-06-20 stsp int search_next_done;
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
570 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
571 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
572 1803e47f 2019-06-22 stsp regex_t regex;
573 41605754 2020-11-12 stsp regmatch_t regmatch;
574 cc3c9aac 2018-08-01 stsp };
575 cd0acaa7 2018-05-20 stsp
576 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
577 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
578 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
579 78756c87 2020-11-24 stsp struct got_repository *);
580 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
581 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
582 e78dc838 2020-12-04 stsp struct tog_view *, int);
583 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
584 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
586 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp
588 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
589 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
590 78756c87 2020-11-24 stsp const char *, const char *, int);
591 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
593 e78dc838 2020-12-04 stsp struct tog_view *, int);
594 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
597 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
598 e5a0f69f 2018-08-18 stsp
599 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
600 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
601 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
602 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
603 e78dc838 2020-12-04 stsp struct tog_view *, int);
604 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
605 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
607 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
608 e5a0f69f 2018-08-18 stsp
609 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
610 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
611 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
612 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
613 e78dc838 2020-12-04 stsp struct tog_view *, int);
614 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
616 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
617 6458efa5 2020-11-24 stsp
618 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
619 6458efa5 2020-11-24 stsp struct got_repository *);
620 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
621 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
622 e78dc838 2020-12-04 stsp struct tog_view *, int);
623 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
625 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
626 25791caa 2018-10-24 stsp
627 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
628 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
629 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
631 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
632 25791caa 2018-10-24 stsp
633 25791caa 2018-10-24 stsp static void
634 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
635 25791caa 2018-10-24 stsp {
636 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
637 83baff54 2019-08-12 stsp }
638 83baff54 2019-08-12 stsp
639 83baff54 2019-08-12 stsp static void
640 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
641 83baff54 2019-08-12 stsp {
642 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
643 25791caa 2018-10-24 stsp }
644 26ed57b2 2018-05-19 stsp
645 61266923 2020-01-14 stsp static void
646 61266923 2020-01-14 stsp tog_sigcont(int signo)
647 61266923 2020-01-14 stsp {
648 61266923 2020-01-14 stsp tog_sigcont_received = 1;
649 61266923 2020-01-14 stsp }
650 61266923 2020-01-14 stsp
651 296152d1 2022-05-31 thomas static void
652 296152d1 2022-05-31 thomas tog_sigint(int signo)
653 296152d1 2022-05-31 thomas {
654 296152d1 2022-05-31 thomas tog_sigint_received = 1;
655 296152d1 2022-05-31 thomas }
656 296152d1 2022-05-31 thomas
657 296152d1 2022-05-31 thomas static void
658 296152d1 2022-05-31 thomas tog_sigterm(int signo)
659 296152d1 2022-05-31 thomas {
660 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
661 296152d1 2022-05-31 thomas }
662 296152d1 2022-05-31 thomas
663 296152d1 2022-05-31 thomas static int
664 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
665 296152d1 2022-05-31 thomas {
666 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
667 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
668 296152d1 2022-05-31 thomas }
669 296152d1 2022-05-31 thomas
670 e5a0f69f 2018-08-18 stsp static const struct got_error *
671 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
672 ea5e7bb5 2018-08-01 stsp {
673 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
674 e5a0f69f 2018-08-18 stsp
675 669b5ffa 2018-10-07 stsp if (view->child) {
676 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
677 669b5ffa 2018-10-07 stsp view->child = NULL;
678 669b5ffa 2018-10-07 stsp }
679 e5a0f69f 2018-08-18 stsp if (view->close)
680 e5a0f69f 2018-08-18 stsp err = view->close(view);
681 ea5e7bb5 2018-08-01 stsp if (view->panel)
682 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
683 ea5e7bb5 2018-08-01 stsp if (view->window)
684 ea5e7bb5 2018-08-01 stsp delwin(view->window);
685 ea5e7bb5 2018-08-01 stsp free(view);
686 f2d749db 2022-07-12 thomas return err ? err : child_err;
687 ea5e7bb5 2018-08-01 stsp }
688 ea5e7bb5 2018-08-01 stsp
689 ea5e7bb5 2018-08-01 stsp static struct tog_view *
690 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
691 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
692 ea5e7bb5 2018-08-01 stsp {
693 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
694 ea5e7bb5 2018-08-01 stsp
695 ea5e7bb5 2018-08-01 stsp if (view == NULL)
696 ea5e7bb5 2018-08-01 stsp return NULL;
697 ea5e7bb5 2018-08-01 stsp
698 d6b05b5b 2018-08-04 stsp view->type = type;
699 f7d12f7e 2018-08-01 stsp view->lines = LINES;
700 f7d12f7e 2018-08-01 stsp view->cols = COLS;
701 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
702 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
703 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
704 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
705 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
706 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
709 ea5e7bb5 2018-08-01 stsp }
710 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
711 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
712 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
713 96a765a8 2018-08-04 stsp view_close(view);
714 ea5e7bb5 2018-08-01 stsp return NULL;
715 ea5e7bb5 2018-08-01 stsp }
716 ea5e7bb5 2018-08-01 stsp
717 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
718 ea5e7bb5 2018-08-01 stsp return view;
719 cdf1ee82 2018-08-01 stsp }
720 cdf1ee82 2018-08-01 stsp
721 0cf4efb1 2018-09-29 stsp static int
722 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
723 0cf4efb1 2018-09-29 stsp {
724 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
725 0cf4efb1 2018-09-29 stsp return 0;
726 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
727 5c60c32a 2018-10-18 stsp }
728 5c60c32a 2018-10-18 stsp
729 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
730 a5d43cac 2022-07-01 thomas static int
731 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
732 a5d43cac 2022-07-01 thomas {
733 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
734 a5d43cac 2022-07-01 thomas }
735 a5d43cac 2022-07-01 thomas
736 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
737 5c60c32a 2018-10-18 stsp
738 5c60c32a 2018-10-18 stsp static const struct got_error *
739 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
740 5c60c32a 2018-10-18 stsp {
741 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
742 5c60c32a 2018-10-18 stsp
743 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
744 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
745 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
746 53d2bdd3 2022-07-10 thomas else
747 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
748 a5d43cac 2022-07-01 thomas view->begin_x = 0;
749 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
750 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
751 53d2bdd3 2022-07-10 thomas view->cols > 119)
752 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
753 53d2bdd3 2022-07-10 thomas else
754 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
755 a5d43cac 2022-07-01 thomas view->begin_y = 0;
756 a5d43cac 2022-07-01 thomas }
757 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
758 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
759 5c60c32a 2018-10-18 stsp view->lines = LINES;
760 5c60c32a 2018-10-18 stsp view->cols = COLS;
761 5c60c32a 2018-10-18 stsp err = view_resize(view);
762 5c60c32a 2018-10-18 stsp if (err)
763 5c60c32a 2018-10-18 stsp return err;
764 5c60c32a 2018-10-18 stsp
765 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
766 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
767 a5d43cac 2022-07-01 thomas
768 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
769 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
770 5c60c32a 2018-10-18 stsp
771 5c60c32a 2018-10-18 stsp return NULL;
772 5c60c32a 2018-10-18 stsp }
773 5c60c32a 2018-10-18 stsp
774 5c60c32a 2018-10-18 stsp static const struct got_error *
775 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
776 5c60c32a 2018-10-18 stsp {
777 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
778 5c60c32a 2018-10-18 stsp
779 5c60c32a 2018-10-18 stsp view->begin_x = 0;
780 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
781 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
782 5c60c32a 2018-10-18 stsp view->ncols = COLS;
783 5c60c32a 2018-10-18 stsp view->lines = LINES;
784 5c60c32a 2018-10-18 stsp view->cols = COLS;
785 5c60c32a 2018-10-18 stsp err = view_resize(view);
786 5c60c32a 2018-10-18 stsp if (err)
787 5c60c32a 2018-10-18 stsp return err;
788 5c60c32a 2018-10-18 stsp
789 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
790 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
791 5c60c32a 2018-10-18 stsp
792 5c60c32a 2018-10-18 stsp return NULL;
793 0cf4efb1 2018-09-29 stsp }
794 0cf4efb1 2018-09-29 stsp
795 5c60c32a 2018-10-18 stsp static int
796 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
797 5c60c32a 2018-10-18 stsp {
798 5c60c32a 2018-10-18 stsp return view->parent == NULL;
799 5c60c32a 2018-10-18 stsp }
800 5c60c32a 2018-10-18 stsp
801 b65b3ea0 2022-06-23 thomas static int
802 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
803 b65b3ea0 2022-06-23 thomas {
804 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
805 e44940c3 2022-07-01 thomas }
806 e44940c3 2022-07-01 thomas
807 e44940c3 2022-07-01 thomas static int
808 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
809 e44940c3 2022-07-01 thomas {
810 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
811 b65b3ea0 2022-06-23 thomas }
812 b65b3ea0 2022-06-23 thomas
813 444d5325 2022-07-03 thomas static int
814 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
815 444d5325 2022-07-03 thomas {
816 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
817 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
818 444d5325 2022-07-03 thomas }
819 444d5325 2022-07-03 thomas
820 a5d43cac 2022-07-01 thomas static void
821 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
822 a5d43cac 2022-07-01 thomas {
823 a5d43cac 2022-07-01 thomas PANEL *panel;
824 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
825 b65b3ea0 2022-06-23 thomas
826 a5d43cac 2022-07-01 thomas if (view->parent)
827 a5d43cac 2022-07-01 thomas return view_border(view->parent);
828 a5d43cac 2022-07-01 thomas
829 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
830 a5d43cac 2022-07-01 thomas if (panel == NULL)
831 a5d43cac 2022-07-01 thomas return;
832 a5d43cac 2022-07-01 thomas
833 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
834 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
835 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
836 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
837 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
838 a5d43cac 2022-07-01 thomas else
839 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
840 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
841 a5d43cac 2022-07-01 thomas }
842 a5d43cac 2022-07-01 thomas
843 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
844 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
846 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
847 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
848 a5d43cac 2022-07-01 thomas
849 4d8c2215 2018-08-19 stsp static const struct got_error *
850 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
851 f7d12f7e 2018-08-01 stsp {
852 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
853 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
854 f7d12f7e 2018-08-01 stsp
855 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
856 a5d43cac 2022-07-01 thomas
857 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
858 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
859 0cf4efb1 2018-09-29 stsp else
860 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
861 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
862 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
863 0cf4efb1 2018-09-29 stsp else
864 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
865 6d0fee91 2018-08-01 stsp
866 4918811f 2022-07-01 thomas if (view->child) {
867 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
868 a5d43cac 2022-07-01 thomas
869 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
870 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
871 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
872 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
873 40236d76 2022-06-23 thomas ncols = COLS;
874 40236d76 2022-06-23 thomas
875 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
876 5c60c32a 2018-10-18 stsp if (view->child->focussed)
877 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
878 5c60c32a 2018-10-18 stsp else
879 5c60c32a 2018-10-18 stsp show_panel(view->panel);
880 5c60c32a 2018-10-18 stsp } else {
881 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
882 40236d76 2022-06-23 thomas
883 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
884 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
885 a5d43cac 2022-07-01 thomas }
886 a5d43cac 2022-07-01 thomas /*
887 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
888 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
889 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
890 a5d43cac 2022-07-01 thomas */
891 a5d43cac 2022-07-01 thomas if (hs) {
892 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
893 a5d43cac 2022-07-01 thomas if (err)
894 a5d43cac 2022-07-01 thomas return err;
895 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
896 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
897 a5d43cac 2022-07-01 thomas if (err)
898 a5d43cac 2022-07-01 thomas return err;
899 a5d43cac 2022-07-01 thomas }
900 a5d43cac 2022-07-01 thomas view_border(view);
901 a5d43cac 2022-07-01 thomas update_panels();
902 a5d43cac 2022-07-01 thomas doupdate();
903 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
904 a5d43cac 2022-07-01 thomas nlines = view->nlines;
905 a5d43cac 2022-07-01 thomas }
906 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
907 40236d76 2022-06-23 thomas ncols = COLS;
908 669b5ffa 2018-10-07 stsp
909 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
910 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
911 ea0bff04 2022-07-19 thomas if (err)
912 ea0bff04 2022-07-19 thomas return err;
913 ea0bff04 2022-07-19 thomas }
914 ea0bff04 2022-07-19 thomas
915 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
916 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
917 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
918 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
919 40236d76 2022-06-23 thomas wclear(view->window);
920 40236d76 2022-06-23 thomas
921 40236d76 2022-06-23 thomas view->nlines = nlines;
922 40236d76 2022-06-23 thomas view->ncols = ncols;
923 40236d76 2022-06-23 thomas view->lines = LINES;
924 40236d76 2022-06-23 thomas view->cols = COLS;
925 40236d76 2022-06-23 thomas
926 5c60c32a 2018-10-18 stsp return NULL;
927 ea0bff04 2022-07-19 thomas }
928 ea0bff04 2022-07-19 thomas
929 ea0bff04 2022-07-19 thomas static const struct got_error *
930 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
931 ea0bff04 2022-07-19 thomas {
932 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
933 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
934 3a0139e8 2022-07-22 thomas int n = 0;
935 ea0bff04 2022-07-19 thomas
936 3a0139e8 2022-07-22 thomas if (s->selected_entry)
937 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
938 3a0139e8 2022-07-22 thomas
939 ea0bff04 2022-07-19 thomas /*
940 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
941 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
942 ea0bff04 2022-07-19 thomas */
943 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
944 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
945 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
946 ea0bff04 2022-07-19 thomas }
947 ea0bff04 2022-07-19 thomas
948 ea0bff04 2022-07-19 thomas return err;
949 97cb21cd 2022-07-12 thomas }
950 97cb21cd 2022-07-12 thomas
951 97cb21cd 2022-07-12 thomas static void
952 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
953 97cb21cd 2022-07-12 thomas {
954 97cb21cd 2022-07-12 thomas if (n == 0)
955 97cb21cd 2022-07-12 thomas return;
956 97cb21cd 2022-07-12 thomas
957 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
958 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
959 97cb21cd 2022-07-12 thomas view->parent->offset += n;
960 97cb21cd 2022-07-12 thomas else
961 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
962 97cb21cd 2022-07-12 thomas } else if (view->offset) {
963 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
964 97cb21cd 2022-07-12 thomas view->offset -= n;
965 97cb21cd 2022-07-12 thomas else
966 97cb21cd 2022-07-12 thomas view->offset = 0;
967 97cb21cd 2022-07-12 thomas }
968 53d2bdd3 2022-07-10 thomas }
969 53d2bdd3 2022-07-10 thomas
970 53d2bdd3 2022-07-10 thomas static const struct got_error *
971 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
972 53d2bdd3 2022-07-10 thomas {
973 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
974 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
975 53d2bdd3 2022-07-10 thomas
976 53d2bdd3 2022-07-10 thomas if (view->parent)
977 53d2bdd3 2022-07-10 thomas v = view->parent;
978 53d2bdd3 2022-07-10 thomas else
979 53d2bdd3 2022-07-10 thomas v = view;
980 53d2bdd3 2022-07-10 thomas
981 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
982 53d2bdd3 2022-07-10 thomas return NULL;
983 53d2bdd3 2022-07-10 thomas
984 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
985 53d2bdd3 2022-07-10 thomas
986 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
987 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
988 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
989 53d2bdd3 2022-07-10 thomas if (view->parent)
990 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
991 53d2bdd3 2022-07-10 thomas else
992 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
993 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
994 53d2bdd3 2022-07-10 thomas view->count = 0;
995 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
996 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
997 53d2bdd3 2022-07-10 thomas view->count = 0;
998 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
999 53d2bdd3 2022-07-10 thomas }
1000 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1001 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1002 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1003 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1004 53d2bdd3 2022-07-10 thomas if (err)
1005 53d2bdd3 2022-07-10 thomas return err;
1006 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1007 53d2bdd3 2022-07-10 thomas } else {
1008 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1009 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1010 53d2bdd3 2022-07-10 thomas if (view->parent)
1011 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1012 53d2bdd3 2022-07-10 thomas else
1013 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1014 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1015 53d2bdd3 2022-07-10 thomas view->count = 0;
1016 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1017 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1018 53d2bdd3 2022-07-10 thomas view->count = 0;
1019 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1020 53d2bdd3 2022-07-10 thomas }
1021 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1022 53d2bdd3 2022-07-10 thomas }
1023 53d2bdd3 2022-07-10 thomas
1024 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1025 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1026 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1027 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1028 53d2bdd3 2022-07-10 thomas
1029 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1030 53d2bdd3 2022-07-10 thomas if (err)
1031 53d2bdd3 2022-07-10 thomas return err;
1032 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1033 53d2bdd3 2022-07-10 thomas if (err)
1034 53d2bdd3 2022-07-10 thomas return err;
1035 53d2bdd3 2022-07-10 thomas
1036 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1037 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1038 53d2bdd3 2022-07-10 thomas if (err)
1039 53d2bdd3 2022-07-10 thomas return err;
1040 53d2bdd3 2022-07-10 thomas }
1041 53d2bdd3 2022-07-10 thomas
1042 3a0139e8 2022-07-22 thomas if (v->resize)
1043 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1044 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1045 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1046 53d2bdd3 2022-07-10 thomas
1047 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1048 53d2bdd3 2022-07-10 thomas
1049 53d2bdd3 2022-07-10 thomas return err;
1050 53d2bdd3 2022-07-10 thomas }
1051 53d2bdd3 2022-07-10 thomas
1052 53d2bdd3 2022-07-10 thomas static void
1053 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1054 53d2bdd3 2022-07-10 thomas {
1055 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1056 53d2bdd3 2022-07-10 thomas
1057 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1058 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1059 669b5ffa 2018-10-07 stsp }
1060 669b5ffa 2018-10-07 stsp
1061 669b5ffa 2018-10-07 stsp static const struct got_error *
1062 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1063 669b5ffa 2018-10-07 stsp {
1064 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1065 669b5ffa 2018-10-07 stsp
1066 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1067 669b5ffa 2018-10-07 stsp return NULL;
1068 669b5ffa 2018-10-07 stsp
1069 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1070 669b5ffa 2018-10-07 stsp view->child = NULL;
1071 669b5ffa 2018-10-07 stsp return err;
1072 669b5ffa 2018-10-07 stsp }
1073 669b5ffa 2018-10-07 stsp
1074 40236d76 2022-06-23 thomas static const struct got_error *
1075 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1076 669b5ffa 2018-10-07 stsp {
1077 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1078 53d2bdd3 2022-07-10 thomas
1079 669b5ffa 2018-10-07 stsp view->child = child;
1080 669b5ffa 2018-10-07 stsp child->parent = view;
1081 40236d76 2022-06-23 thomas
1082 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1083 53d2bdd3 2022-07-10 thomas if (err)
1084 53d2bdd3 2022-07-10 thomas return err;
1085 53d2bdd3 2022-07-10 thomas
1086 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1087 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1088 53d2bdd3 2022-07-10 thomas
1089 53d2bdd3 2022-07-10 thomas return err;
1090 bfddd0d9 2018-09-29 stsp }
1091 2a31b33b 2022-07-23 thomas
1092 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1093 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1094 2a31b33b 2022-07-23 thomas
1095 2a31b33b 2022-07-23 thomas static const struct got_error *
1096 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1097 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1098 2a31b33b 2022-07-23 thomas {
1099 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1100 2a31b33b 2022-07-23 thomas const struct got_error *err;
1101 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1102 2a31b33b 2022-07-23 thomas
1103 2a31b33b 2022-07-23 thomas *requested = NULL;
1104 2a31b33b 2022-07-23 thomas
1105 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view))
1106 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1107 bfddd0d9 2018-09-29 stsp
1108 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1109 2a31b33b 2022-07-23 thomas if (err)
1110 2a31b33b 2022-07-23 thomas return err;
1111 2a31b33b 2022-07-23 thomas
1112 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1113 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1114 2a31b33b 2022-07-23 thomas if (err)
1115 2a31b33b 2022-07-23 thomas return err;
1116 2a31b33b 2022-07-23 thomas }
1117 2a31b33b 2022-07-23 thomas
1118 2a31b33b 2022-07-23 thomas view->focussed = 0;
1119 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1120 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1121 2a31b33b 2022-07-23 thomas new_view->nlines = view->lines - y;
1122 2a31b33b 2022-07-23 thomas
1123 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view)) {
1124 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1125 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1126 2a31b33b 2022-07-23 thomas if (err)
1127 2a31b33b 2022-07-23 thomas return err;
1128 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1129 2a31b33b 2022-07-23 thomas if (err)
1130 2a31b33b 2022-07-23 thomas return err;
1131 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1132 2a31b33b 2022-07-23 thomas } else
1133 2a31b33b 2022-07-23 thomas *requested = new_view;
1134 2a31b33b 2022-07-23 thomas
1135 2a31b33b 2022-07-23 thomas return NULL;
1136 2a31b33b 2022-07-23 thomas }
1137 2a31b33b 2022-07-23 thomas
1138 34bc9ec9 2019-02-22 stsp static void
1139 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1140 25791caa 2018-10-24 stsp {
1141 25791caa 2018-10-24 stsp int cols, lines;
1142 25791caa 2018-10-24 stsp struct winsize size;
1143 25791caa 2018-10-24 stsp
1144 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1145 25791caa 2018-10-24 stsp cols = 80; /* Default */
1146 25791caa 2018-10-24 stsp lines = 24;
1147 25791caa 2018-10-24 stsp } else {
1148 25791caa 2018-10-24 stsp cols = size.ws_col;
1149 25791caa 2018-10-24 stsp lines = size.ws_row;
1150 25791caa 2018-10-24 stsp }
1151 25791caa 2018-10-24 stsp resize_term(lines, cols);
1152 2b49a8ae 2019-06-22 stsp }
1153 2b49a8ae 2019-06-22 stsp
1154 2b49a8ae 2019-06-22 stsp static const struct got_error *
1155 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1156 2b49a8ae 2019-06-22 stsp {
1157 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1158 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1159 2b49a8ae 2019-06-22 stsp char pattern[1024];
1160 2b49a8ae 2019-06-22 stsp int ret;
1161 c0c4acc8 2021-01-24 stsp
1162 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1163 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1164 c0c4acc8 2021-01-24 stsp view->searching = 0;
1165 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1166 c0c4acc8 2021-01-24 stsp }
1167 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1168 2b49a8ae 2019-06-22 stsp
1169 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1170 2b49a8ae 2019-06-22 stsp return NULL;
1171 2b49a8ae 2019-06-22 stsp
1172 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1173 a5d43cac 2022-07-01 thomas v = view->child;
1174 2b49a8ae 2019-06-22 stsp
1175 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1176 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1177 a5d43cac 2022-07-01 thomas
1178 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1179 2b49a8ae 2019-06-22 stsp nocbreak();
1180 2b49a8ae 2019-06-22 stsp echo();
1181 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1182 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1183 2b49a8ae 2019-06-22 stsp cbreak();
1184 2b49a8ae 2019-06-22 stsp noecho();
1185 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1186 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1187 2b49a8ae 2019-06-22 stsp return NULL;
1188 2b49a8ae 2019-06-22 stsp
1189 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1190 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1191 7c32bd05 2019-06-22 stsp if (err) {
1192 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1193 7c32bd05 2019-06-22 stsp return err;
1194 7c32bd05 2019-06-22 stsp }
1195 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1196 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1197 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1198 2b49a8ae 2019-06-22 stsp view->search_next(view);
1199 2b49a8ae 2019-06-22 stsp }
1200 2b49a8ae 2019-06-22 stsp
1201 2b49a8ae 2019-06-22 stsp return NULL;
1202 64486692 2022-07-07 thomas }
1203 64486692 2022-07-07 thomas
1204 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1205 64486692 2022-07-07 thomas static const struct got_error *
1206 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1207 64486692 2022-07-07 thomas {
1208 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1209 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1210 64486692 2022-07-07 thomas
1211 64486692 2022-07-07 thomas if (view->parent)
1212 64486692 2022-07-07 thomas v = view->parent;
1213 64486692 2022-07-07 thomas else
1214 64486692 2022-07-07 thomas v = view;
1215 64486692 2022-07-07 thomas
1216 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1217 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1218 ddbc4d37 2022-07-12 thomas else
1219 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1220 64486692 2022-07-07 thomas
1221 ddbc4d37 2022-07-12 thomas if (!v->child)
1222 ddbc4d37 2022-07-12 thomas return NULL;
1223 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1224 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1225 ddbc4d37 2022-07-12 thomas
1226 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1227 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1228 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1229 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1230 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1231 64486692 2022-07-07 thomas
1232 ddbc4d37 2022-07-12 thomas
1233 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1234 64486692 2022-07-07 thomas v->ncols = COLS;
1235 64486692 2022-07-07 thomas v->child->ncols = COLS;
1236 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1237 64486692 2022-07-07 thomas
1238 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1239 64486692 2022-07-07 thomas if (err)
1240 64486692 2022-07-07 thomas return err;
1241 64486692 2022-07-07 thomas }
1242 64486692 2022-07-07 thomas v->child->mode = v->mode;
1243 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1244 64486692 2022-07-07 thomas v->focus_child = 1;
1245 64486692 2022-07-07 thomas
1246 64486692 2022-07-07 thomas err = view_fullscreen(v);
1247 64486692 2022-07-07 thomas if (err)
1248 64486692 2022-07-07 thomas return err;
1249 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1250 64486692 2022-07-07 thomas if (err)
1251 64486692 2022-07-07 thomas return err;
1252 64486692 2022-07-07 thomas
1253 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1254 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1255 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1256 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1257 cf1fe301 2022-07-29 thomas if (err)
1258 cf1fe301 2022-07-29 thomas return err;
1259 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1260 cf1fe301 2022-07-29 thomas if (err)
1261 cf1fe301 2022-07-29 thomas return err;
1262 ddbc4d37 2022-07-12 thomas } else {
1263 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1264 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1265 64486692 2022-07-07 thomas }
1266 f4e6231a 2022-07-22 thomas if (v->resize)
1267 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1268 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1269 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1270 64486692 2022-07-07 thomas
1271 64486692 2022-07-07 thomas return err;
1272 0cf4efb1 2018-09-29 stsp }
1273 6d0fee91 2018-08-01 stsp
1274 07b0611c 2022-06-23 thomas /*
1275 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1276 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1277 07b0611c 2022-06-23 thomas */
1278 07b0611c 2022-06-23 thomas static int
1279 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1280 07b0611c 2022-06-23 thomas {
1281 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1282 a5d43cac 2022-07-01 thomas int x, n = 0;
1283 07b0611c 2022-06-23 thomas
1284 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1285 a5d43cac 2022-07-01 thomas v = view->child;
1286 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1287 a5d43cac 2022-07-01 thomas v = view->parent;
1288 a5d43cac 2022-07-01 thomas
1289 07b0611c 2022-06-23 thomas view->count = 0;
1290 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1291 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1292 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1293 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1294 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1295 07b0611c 2022-06-23 thomas
1296 07b0611c 2022-06-23 thomas do {
1297 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1298 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1299 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1300 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1301 a5d43cac 2022-07-01 thomas }
1302 a5d43cac 2022-07-01 thomas
1303 07b0611c 2022-06-23 thomas /*
1304 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1305 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1306 07b0611c 2022-06-23 thomas */
1307 07b0611c 2022-06-23 thomas if (n >= 9999999)
1308 07b0611c 2022-06-23 thomas n = 9999999;
1309 07b0611c 2022-06-23 thomas else
1310 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1311 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1312 07b0611c 2022-06-23 thomas
1313 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1314 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1315 07dd3ed3 2022-08-06 thomas n = 0;
1316 07dd3ed3 2022-08-06 thomas c = 0;
1317 07dd3ed3 2022-08-06 thomas }
1318 07dd3ed3 2022-08-06 thomas
1319 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1320 07b0611c 2022-06-23 thomas view->count = n;
1321 07b0611c 2022-06-23 thomas
1322 07b0611c 2022-06-23 thomas return c;
1323 07b0611c 2022-06-23 thomas }
1324 07b0611c 2022-06-23 thomas
1325 0cf4efb1 2018-09-29 stsp static const struct got_error *
1326 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1327 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1328 e5a0f69f 2018-08-18 stsp {
1329 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1330 669b5ffa 2018-10-07 stsp struct tog_view *v;
1331 1a76625f 2018-10-22 stsp int ch, errcode;
1332 e5a0f69f 2018-08-18 stsp
1333 e5a0f69f 2018-08-18 stsp *new = NULL;
1334 8f4ed634 2020-03-26 stsp
1335 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1336 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1337 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1338 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1339 07b0611c 2022-06-23 thomas view->count = 0;
1340 07b0611c 2022-06-23 thomas }
1341 e5a0f69f 2018-08-18 stsp
1342 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1343 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1344 82954512 2020-02-03 stsp if (errcode)
1345 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1346 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1347 3da8ef85 2021-09-21 stsp sched_yield();
1348 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1349 82954512 2020-02-03 stsp if (errcode)
1350 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1351 82954512 2020-02-03 stsp "pthread_mutex_lock");
1352 60493ae3 2019-06-20 stsp view->search_next(view);
1353 60493ae3 2019-06-20 stsp return NULL;
1354 60493ae3 2019-06-20 stsp }
1355 60493ae3 2019-06-20 stsp
1356 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1357 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1358 1a76625f 2018-10-22 stsp if (errcode)
1359 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1360 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1361 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1362 634cb454 2022-07-03 thomas cbreak();
1363 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1364 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1365 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1366 634cb454 2022-07-03 thomas view->count = 0;
1367 634cb454 2022-07-03 thomas else
1368 634cb454 2022-07-03 thomas ch = view->ch;
1369 634cb454 2022-07-03 thomas } else {
1370 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1371 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1372 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1373 07b0611c 2022-06-23 thomas }
1374 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1375 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1376 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1377 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1378 1a76625f 2018-10-22 stsp if (errcode)
1379 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1380 25791caa 2018-10-24 stsp
1381 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1382 25791caa 2018-10-24 stsp tog_resizeterm();
1383 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1384 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1385 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1386 25791caa 2018-10-24 stsp err = view_resize(v);
1387 25791caa 2018-10-24 stsp if (err)
1388 25791caa 2018-10-24 stsp return err;
1389 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1390 25791caa 2018-10-24 stsp if (err)
1391 25791caa 2018-10-24 stsp return err;
1392 cdfcfb03 2020-12-06 stsp if (v->child) {
1393 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1394 cdfcfb03 2020-12-06 stsp if (err)
1395 cdfcfb03 2020-12-06 stsp return err;
1396 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1397 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1398 cdfcfb03 2020-12-06 stsp if (err)
1399 cdfcfb03 2020-12-06 stsp return err;
1400 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1401 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1402 53d2bdd3 2022-07-10 thomas if (err)
1403 53d2bdd3 2022-07-10 thomas return err;
1404 53d2bdd3 2022-07-10 thomas }
1405 cdfcfb03 2020-12-06 stsp }
1406 25791caa 2018-10-24 stsp }
1407 25791caa 2018-10-24 stsp }
1408 25791caa 2018-10-24 stsp
1409 e5a0f69f 2018-08-18 stsp switch (ch) {
1410 1e37a5c2 2019-05-12 jcs case '\t':
1411 07b0611c 2022-06-23 thomas view->count = 0;
1412 1e37a5c2 2019-05-12 jcs if (view->child) {
1413 e78dc838 2020-12-04 stsp view->focussed = 0;
1414 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1415 e78dc838 2020-12-04 stsp view->focus_child = 1;
1416 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1417 e78dc838 2020-12-04 stsp view->focussed = 0;
1418 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1419 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1420 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1421 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1422 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1423 3a0139e8 2022-07-22 thomas 0);
1424 a5d43cac 2022-07-01 thomas if (err)
1425 a5d43cac 2022-07-01 thomas return err;
1426 a5d43cac 2022-07-01 thomas }
1427 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1428 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1429 a5d43cac 2022-07-01 thomas if (err)
1430 a5d43cac 2022-07-01 thomas return err;
1431 a5d43cac 2022-07-01 thomas }
1432 1e37a5c2 2019-05-12 jcs }
1433 1e37a5c2 2019-05-12 jcs break;
1434 1e37a5c2 2019-05-12 jcs case 'q':
1435 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1436 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1437 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1438 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1439 a5d43cac 2022-07-01 thomas if (err)
1440 a5d43cac 2022-07-01 thomas break;
1441 a5d43cac 2022-07-01 thomas }
1442 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1443 a5d43cac 2022-07-01 thomas }
1444 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1445 9970f7fc 2020-12-03 stsp view->dying = 1;
1446 1e37a5c2 2019-05-12 jcs break;
1447 1e37a5c2 2019-05-12 jcs case 'Q':
1448 1e37a5c2 2019-05-12 jcs *done = 1;
1449 1e37a5c2 2019-05-12 jcs break;
1450 1c5e5faa 2022-06-23 thomas case 'F':
1451 07b0611c 2022-06-23 thomas view->count = 0;
1452 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1453 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1454 1e37a5c2 2019-05-12 jcs break;
1455 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1456 e78dc838 2020-12-04 stsp view->focussed = 0;
1457 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1458 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1459 53d2bdd3 2022-07-10 thomas } else {
1460 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1461 53d2bdd3 2022-07-10 thomas if (!err)
1462 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1463 53d2bdd3 2022-07-10 thomas }
1464 1e37a5c2 2019-05-12 jcs if (err)
1465 1e37a5c2 2019-05-12 jcs break;
1466 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1467 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1468 1e37a5c2 2019-05-12 jcs } else {
1469 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1470 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1471 e78dc838 2020-12-04 stsp view->focussed = 1;
1472 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1473 1e37a5c2 2019-05-12 jcs } else {
1474 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1475 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1476 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1477 53d2bdd3 2022-07-10 thomas if (!err)
1478 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1479 669b5ffa 2018-10-07 stsp }
1480 1e37a5c2 2019-05-12 jcs if (err)
1481 1e37a5c2 2019-05-12 jcs break;
1482 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1483 a5d43cac 2022-07-01 thomas }
1484 a5d43cac 2022-07-01 thomas if (err)
1485 a5d43cac 2022-07-01 thomas break;
1486 3a0139e8 2022-07-22 thomas if (view->resize) {
1487 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1488 a5d43cac 2022-07-01 thomas if (err)
1489 a5d43cac 2022-07-01 thomas break;
1490 1e37a5c2 2019-05-12 jcs }
1491 a5d43cac 2022-07-01 thomas if (view->parent)
1492 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1493 a5d43cac 2022-07-01 thomas if (!err)
1494 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1495 1e37a5c2 2019-05-12 jcs break;
1496 64486692 2022-07-07 thomas case 'S':
1497 53d2bdd3 2022-07-10 thomas view->count = 0;
1498 64486692 2022-07-07 thomas err = switch_split(view);
1499 53d2bdd3 2022-07-10 thomas break;
1500 53d2bdd3 2022-07-10 thomas case '-':
1501 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1502 64486692 2022-07-07 thomas break;
1503 53d2bdd3 2022-07-10 thomas case '+':
1504 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1505 53d2bdd3 2022-07-10 thomas break;
1506 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1507 60493ae3 2019-06-20 stsp break;
1508 60493ae3 2019-06-20 stsp case '/':
1509 07b0611c 2022-06-23 thomas view->count = 0;
1510 60493ae3 2019-06-20 stsp if (view->search_start)
1511 2b49a8ae 2019-06-22 stsp view_search_start(view);
1512 60493ae3 2019-06-20 stsp else
1513 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1514 1e37a5c2 2019-05-12 jcs break;
1515 b1bf1435 2019-06-21 stsp case 'N':
1516 60493ae3 2019-06-20 stsp case 'n':
1517 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1518 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1519 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1520 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1521 60493ae3 2019-06-20 stsp view->search_next(view);
1522 60493ae3 2019-06-20 stsp } else
1523 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1524 adf4c9e0 2022-07-03 thomas break;
1525 adf4c9e0 2022-07-03 thomas case 'A':
1526 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1527 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1528 adf4c9e0 2022-07-03 thomas else
1529 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1530 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1531 adf4c9e0 2022-07-03 thomas if (v->reset) {
1532 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1533 adf4c9e0 2022-07-03 thomas if (err)
1534 adf4c9e0 2022-07-03 thomas return err;
1535 adf4c9e0 2022-07-03 thomas }
1536 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1537 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1538 adf4c9e0 2022-07-03 thomas if (err)
1539 adf4c9e0 2022-07-03 thomas return err;
1540 adf4c9e0 2022-07-03 thomas }
1541 adf4c9e0 2022-07-03 thomas }
1542 60493ae3 2019-06-20 stsp break;
1543 1e37a5c2 2019-05-12 jcs default:
1544 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1545 1e37a5c2 2019-05-12 jcs break;
1546 e5a0f69f 2018-08-18 stsp }
1547 e5a0f69f 2018-08-18 stsp
1548 e5a0f69f 2018-08-18 stsp return err;
1549 bcbd79e2 2018-08-19 stsp }
1550 bcbd79e2 2018-08-19 stsp
1551 ef20f542 2022-06-26 thomas static int
1552 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1553 a3404814 2018-09-02 stsp {
1554 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1555 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1556 669b5ffa 2018-10-07 stsp return 0;
1557 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1558 669b5ffa 2018-10-07 stsp return 0;
1559 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1560 a3404814 2018-09-02 stsp return 0;
1561 a3404814 2018-09-02 stsp
1562 669b5ffa 2018-10-07 stsp return view->focussed;
1563 a3404814 2018-09-02 stsp }
1564 a3404814 2018-09-02 stsp
1565 bcbd79e2 2018-08-19 stsp static const struct got_error *
1566 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1567 e5a0f69f 2018-08-18 stsp {
1568 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1569 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1570 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1571 64486692 2022-07-07 thomas char *mode;
1572 fd823528 2018-10-22 stsp int fast_refresh = 10;
1573 1a76625f 2018-10-22 stsp int done = 0, errcode;
1574 e5a0f69f 2018-08-18 stsp
1575 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1576 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1577 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1578 64486692 2022-07-07 thomas else
1579 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1580 64486692 2022-07-07 thomas
1581 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1582 1a76625f 2018-10-22 stsp if (errcode)
1583 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1584 1a76625f 2018-10-22 stsp
1585 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1586 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1587 e5a0f69f 2018-08-18 stsp
1588 1004088d 2018-09-29 stsp view->focussed = 1;
1589 878940b7 2018-09-29 stsp err = view->show(view);
1590 0cf4efb1 2018-09-29 stsp if (err)
1591 0cf4efb1 2018-09-29 stsp return err;
1592 0cf4efb1 2018-09-29 stsp update_panels();
1593 0cf4efb1 2018-09-29 stsp doupdate();
1594 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1595 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1596 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1597 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1598 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1599 fd823528 2018-10-22 stsp
1600 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1601 e5a0f69f 2018-08-18 stsp if (err)
1602 e5a0f69f 2018-08-18 stsp break;
1603 9970f7fc 2020-12-03 stsp if (view->dying) {
1604 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1605 669b5ffa 2018-10-07 stsp
1606 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1607 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1608 9970f7fc 2020-12-03 stsp entry);
1609 e78dc838 2020-12-04 stsp else if (view->parent)
1610 669b5ffa 2018-10-07 stsp prev = view->parent;
1611 669b5ffa 2018-10-07 stsp
1612 e78dc838 2020-12-04 stsp if (view->parent) {
1613 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1614 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1615 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1616 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1617 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1618 40236d76 2022-06-23 thomas if (err)
1619 40236d76 2022-06-23 thomas break;
1620 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1621 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1622 e78dc838 2020-12-04 stsp } else
1623 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1624 669b5ffa 2018-10-07 stsp
1625 9970f7fc 2020-12-03 stsp err = view_close(view);
1626 fb59748f 2020-12-05 stsp if (err)
1627 e5a0f69f 2018-08-18 stsp goto done;
1628 669b5ffa 2018-10-07 stsp
1629 e78dc838 2020-12-04 stsp view = NULL;
1630 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1631 e78dc838 2020-12-04 stsp if (v->focussed)
1632 e78dc838 2020-12-04 stsp break;
1633 0cf4efb1 2018-09-29 stsp }
1634 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1635 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1636 e78dc838 2020-12-04 stsp if (prev)
1637 e78dc838 2020-12-04 stsp view = prev;
1638 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1639 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1640 e78dc838 2020-12-04 stsp tog_view_list_head);
1641 e78dc838 2020-12-04 stsp }
1642 e78dc838 2020-12-04 stsp if (view) {
1643 e78dc838 2020-12-04 stsp if (view->focus_child) {
1644 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1645 e78dc838 2020-12-04 stsp view = view->child;
1646 e78dc838 2020-12-04 stsp } else
1647 e78dc838 2020-12-04 stsp view->focussed = 1;
1648 e78dc838 2020-12-04 stsp }
1649 e78dc838 2020-12-04 stsp }
1650 e5a0f69f 2018-08-18 stsp }
1651 bcbd79e2 2018-08-19 stsp if (new_view) {
1652 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1653 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1654 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1655 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1656 86c66b02 2018-10-18 stsp continue;
1657 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1658 86c66b02 2018-10-18 stsp err = view_close(v);
1659 86c66b02 2018-10-18 stsp if (err)
1660 86c66b02 2018-10-18 stsp goto done;
1661 86c66b02 2018-10-18 stsp break;
1662 86c66b02 2018-10-18 stsp }
1663 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1664 fed7eaa8 2018-10-24 stsp view = new_view;
1665 7cd52833 2022-06-23 thomas }
1666 669b5ffa 2018-10-07 stsp if (view) {
1667 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1668 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1669 e78dc838 2020-12-04 stsp view = view->child;
1670 e78dc838 2020-12-04 stsp } else {
1671 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1672 e78dc838 2020-12-04 stsp view = view->parent;
1673 1a76625f 2018-10-22 stsp }
1674 e78dc838 2020-12-04 stsp show_panel(view->panel);
1675 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1676 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1677 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1678 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1679 669b5ffa 2018-10-07 stsp if (err)
1680 1a76625f 2018-10-22 stsp goto done;
1681 669b5ffa 2018-10-07 stsp }
1682 669b5ffa 2018-10-07 stsp err = view->show(view);
1683 0cf4efb1 2018-09-29 stsp if (err)
1684 1a76625f 2018-10-22 stsp goto done;
1685 669b5ffa 2018-10-07 stsp if (view->child) {
1686 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1687 669b5ffa 2018-10-07 stsp if (err)
1688 1a76625f 2018-10-22 stsp goto done;
1689 669b5ffa 2018-10-07 stsp }
1690 1a76625f 2018-10-22 stsp update_panels();
1691 1a76625f 2018-10-22 stsp doupdate();
1692 0cf4efb1 2018-09-29 stsp }
1693 e5a0f69f 2018-08-18 stsp }
1694 e5a0f69f 2018-08-18 stsp done:
1695 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1696 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1697 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1698 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1699 f2d749db 2022-07-12 thomas close_err = view_close(view);
1700 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1701 f2d749db 2022-07-12 thomas err = close_err;
1702 e5a0f69f 2018-08-18 stsp }
1703 1a76625f 2018-10-22 stsp
1704 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1705 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1706 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1707 1a76625f 2018-10-22 stsp
1708 e5a0f69f 2018-08-18 stsp return err;
1709 ea5e7bb5 2018-08-01 stsp }
1710 ea5e7bb5 2018-08-01 stsp
1711 4ed7e80c 2018-05-20 stsp __dead static void
1712 9f7d7167 2018-04-29 stsp usage_log(void)
1713 9f7d7167 2018-04-29 stsp {
1714 80ddbec8 2018-04-29 stsp endwin();
1715 c70c5802 2018-08-01 stsp fprintf(stderr,
1716 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1717 9f7d7167 2018-04-29 stsp getprogname());
1718 9f7d7167 2018-04-29 stsp exit(1);
1719 80ddbec8 2018-04-29 stsp }
1720 80ddbec8 2018-04-29 stsp
1721 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1722 80ddbec8 2018-04-29 stsp static const struct got_error *
1723 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1724 963b370f 2018-05-20 stsp {
1725 00dfcb92 2018-06-11 stsp char *vis = NULL;
1726 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1727 963b370f 2018-05-20 stsp
1728 963b370f 2018-05-20 stsp *ws = NULL;
1729 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1730 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1731 00dfcb92 2018-06-11 stsp int vislen;
1732 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1733 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1734 00dfcb92 2018-06-11 stsp
1735 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1736 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1737 00dfcb92 2018-06-11 stsp if (err)
1738 00dfcb92 2018-06-11 stsp return err;
1739 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1740 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1741 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1742 a7f50699 2018-06-11 stsp goto done;
1743 a7f50699 2018-06-11 stsp }
1744 00dfcb92 2018-06-11 stsp }
1745 963b370f 2018-05-20 stsp
1746 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1747 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1748 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1749 a7f50699 2018-06-11 stsp goto done;
1750 a7f50699 2018-06-11 stsp }
1751 963b370f 2018-05-20 stsp
1752 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1753 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1754 a7f50699 2018-06-11 stsp done:
1755 00dfcb92 2018-06-11 stsp free(vis);
1756 963b370f 2018-05-20 stsp if (err) {
1757 963b370f 2018-05-20 stsp free(*ws);
1758 963b370f 2018-05-20 stsp *ws = NULL;
1759 963b370f 2018-05-20 stsp *wlen = 0;
1760 963b370f 2018-05-20 stsp }
1761 963b370f 2018-05-20 stsp return err;
1762 05171be4 2022-06-23 thomas }
1763 05171be4 2022-06-23 thomas
1764 05171be4 2022-06-23 thomas static const struct got_error *
1765 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1766 05171be4 2022-06-23 thomas {
1767 05171be4 2022-06-23 thomas char *dst;
1768 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1769 05171be4 2022-06-23 thomas
1770 05171be4 2022-06-23 thomas *ptr = NULL;
1771 05171be4 2022-06-23 thomas n = len = strlen(src);
1772 83316834 2022-06-23 thomas dst = malloc(n + 1);
1773 05171be4 2022-06-23 thomas if (dst == NULL)
1774 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1775 05171be4 2022-06-23 thomas
1776 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1777 05171be4 2022-06-23 thomas const char c = src[idx];
1778 05171be4 2022-06-23 thomas
1779 05171be4 2022-06-23 thomas if (c == '\t') {
1780 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1781 b235ad5d 2022-06-23 thomas char *p;
1782 b235ad5d 2022-06-23 thomas
1783 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1784 83316834 2022-06-23 thomas if (p == NULL) {
1785 83316834 2022-06-23 thomas free(dst);
1786 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1787 83316834 2022-06-23 thomas
1788 83316834 2022-06-23 thomas }
1789 83316834 2022-06-23 thomas dst = p;
1790 05171be4 2022-06-23 thomas n += nb;
1791 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1792 05171be4 2022-06-23 thomas sz += nb;
1793 05171be4 2022-06-23 thomas } else
1794 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1795 05171be4 2022-06-23 thomas ++idx;
1796 05171be4 2022-06-23 thomas }
1797 05171be4 2022-06-23 thomas
1798 05171be4 2022-06-23 thomas dst[sz] = '\0';
1799 05171be4 2022-06-23 thomas *ptr = dst;
1800 05171be4 2022-06-23 thomas return NULL;
1801 963b370f 2018-05-20 stsp }
1802 963b370f 2018-05-20 stsp
1803 8d208d34 2022-06-23 thomas /*
1804 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1805 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1806 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1807 8d208d34 2022-06-23 thomas * *rcol.
1808 f91a2b48 2022-06-23 thomas */
1809 8d208d34 2022-06-23 thomas static int
1810 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1811 8d208d34 2022-06-23 thomas {
1812 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1813 f91a2b48 2022-06-23 thomas
1814 8d208d34 2022-06-23 thomas if (n == 0) {
1815 8d208d34 2022-06-23 thomas *rcol = cols;
1816 8d208d34 2022-06-23 thomas return off;
1817 8d208d34 2022-06-23 thomas }
1818 f91a2b48 2022-06-23 thomas
1819 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1820 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1821 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1822 8d208d34 2022-06-23 thomas else
1823 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1824 f91a2b48 2022-06-23 thomas
1825 8d208d34 2022-06-23 thomas if (width == -1) {
1826 8d208d34 2022-06-23 thomas width = 1;
1827 8d208d34 2022-06-23 thomas wline[i] = L'.';
1828 f91a2b48 2022-06-23 thomas }
1829 f91a2b48 2022-06-23 thomas
1830 8d208d34 2022-06-23 thomas if (cols + width > n)
1831 8d208d34 2022-06-23 thomas break;
1832 8d208d34 2022-06-23 thomas cols += width;
1833 f91a2b48 2022-06-23 thomas }
1834 f91a2b48 2022-06-23 thomas
1835 8d208d34 2022-06-23 thomas *rcol = cols;
1836 8d208d34 2022-06-23 thomas return i;
1837 f91a2b48 2022-06-23 thomas }
1838 f91a2b48 2022-06-23 thomas
1839 f91a2b48 2022-06-23 thomas /*
1840 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1841 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1842 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1843 f91a2b48 2022-06-23 thomas */
1844 f91a2b48 2022-06-23 thomas static const struct got_error *
1845 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1846 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1847 f91a2b48 2022-06-23 thomas {
1848 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1849 8d208d34 2022-06-23 thomas int cols;
1850 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1851 05171be4 2022-06-23 thomas char *exstr = NULL;
1852 963b370f 2018-05-20 stsp size_t wlen;
1853 8d208d34 2022-06-23 thomas int i, scrollx;
1854 963b370f 2018-05-20 stsp
1855 963b370f 2018-05-20 stsp *wlinep = NULL;
1856 b700b5d6 2018-07-10 stsp *widthp = 0;
1857 963b370f 2018-05-20 stsp
1858 05171be4 2022-06-23 thomas if (expand) {
1859 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1860 05171be4 2022-06-23 thomas if (err)
1861 05171be4 2022-06-23 thomas return err;
1862 05171be4 2022-06-23 thomas }
1863 05171be4 2022-06-23 thomas
1864 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1865 05171be4 2022-06-23 thomas free(exstr);
1866 963b370f 2018-05-20 stsp if (err)
1867 963b370f 2018-05-20 stsp return err;
1868 963b370f 2018-05-20 stsp
1869 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1870 f91a2b48 2022-06-23 thomas
1871 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1872 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1873 3f670bfb 2020-12-10 stsp wlen--;
1874 3f670bfb 2020-12-10 stsp }
1875 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1876 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1877 3f670bfb 2020-12-10 stsp wlen--;
1878 3f670bfb 2020-12-10 stsp }
1879 3f670bfb 2020-12-10 stsp
1880 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1881 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1882 27a741e5 2019-09-11 stsp
1883 b700b5d6 2018-07-10 stsp if (widthp)
1884 b700b5d6 2018-07-10 stsp *widthp = cols;
1885 f91a2b48 2022-06-23 thomas if (scrollxp)
1886 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1887 963b370f 2018-05-20 stsp if (err)
1888 963b370f 2018-05-20 stsp free(wline);
1889 963b370f 2018-05-20 stsp else
1890 963b370f 2018-05-20 stsp *wlinep = wline;
1891 963b370f 2018-05-20 stsp return err;
1892 963b370f 2018-05-20 stsp }
1893 963b370f 2018-05-20 stsp
1894 8b473291 2019-02-21 stsp static const struct got_error*
1895 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1896 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1897 8b473291 2019-02-21 stsp {
1898 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1899 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1900 8b473291 2019-02-21 stsp char *s;
1901 8b473291 2019-02-21 stsp const char *name;
1902 8b473291 2019-02-21 stsp
1903 8b473291 2019-02-21 stsp *refs_str = NULL;
1904 8b473291 2019-02-21 stsp
1905 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1906 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1907 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1908 52b5abe1 2019-08-13 stsp int cmp;
1909 52b5abe1 2019-08-13 stsp
1910 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1911 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1912 8b473291 2019-02-21 stsp continue;
1913 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1914 8b473291 2019-02-21 stsp name += 5;
1915 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1916 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1917 7143d404 2019-03-12 stsp continue;
1918 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1919 8b473291 2019-02-21 stsp name += 6;
1920 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1921 8b473291 2019-02-21 stsp name += 8;
1922 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1923 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1924 79cc719f 2020-04-24 stsp continue;
1925 79cc719f 2020-04-24 stsp }
1926 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1927 48cae60d 2020-09-22 stsp if (err)
1928 48cae60d 2020-09-22 stsp break;
1929 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1930 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1931 5d844a1e 2019-08-13 stsp if (err) {
1932 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1933 48cae60d 2020-09-22 stsp free(ref_id);
1934 5d844a1e 2019-08-13 stsp break;
1935 48cae60d 2020-09-22 stsp }
1936 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1937 5d844a1e 2019-08-13 stsp err = NULL;
1938 5d844a1e 2019-08-13 stsp tag = NULL;
1939 5d844a1e 2019-08-13 stsp }
1940 52b5abe1 2019-08-13 stsp }
1941 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1942 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1943 48cae60d 2020-09-22 stsp free(ref_id);
1944 52b5abe1 2019-08-13 stsp if (tag)
1945 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1946 52b5abe1 2019-08-13 stsp if (cmp != 0)
1947 52b5abe1 2019-08-13 stsp continue;
1948 8b473291 2019-02-21 stsp s = *refs_str;
1949 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1950 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1951 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1952 8b473291 2019-02-21 stsp free(s);
1953 8b473291 2019-02-21 stsp *refs_str = NULL;
1954 8b473291 2019-02-21 stsp break;
1955 8b473291 2019-02-21 stsp }
1956 8b473291 2019-02-21 stsp free(s);
1957 8b473291 2019-02-21 stsp }
1958 8b473291 2019-02-21 stsp
1959 8b473291 2019-02-21 stsp return err;
1960 8b473291 2019-02-21 stsp }
1961 8b473291 2019-02-21 stsp
1962 963b370f 2018-05-20 stsp static const struct got_error *
1963 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1964 27a741e5 2019-09-11 stsp int col_tab_align)
1965 5813d178 2019-03-09 stsp {
1966 e6b8b890 2020-12-29 naddy char *smallerthan;
1967 5813d178 2019-03-09 stsp
1968 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1969 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1970 5813d178 2019-03-09 stsp author = smallerthan + 1;
1971 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1972 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1973 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1974 5813d178 2019-03-09 stsp }
1975 5813d178 2019-03-09 stsp
1976 5813d178 2019-03-09 stsp static const struct got_error *
1977 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1978 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1979 8fdc79fe 2020-12-01 naddy int author_display_cols)
1980 80ddbec8 2018-04-29 stsp {
1981 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1982 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1983 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1984 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1985 5813d178 2019-03-09 stsp char *author = NULL;
1986 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1987 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1988 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1989 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1990 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1991 ccb26ccd 2018-11-05 stsp struct tm tm;
1992 45d799e2 2018-12-23 stsp time_t committer_time;
1993 11b20872 2019-11-08 stsp struct tog_color *tc;
1994 80ddbec8 2018-04-29 stsp
1995 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1996 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1997 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1998 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1999 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2000 b39d25c7 2018-07-10 stsp
2001 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2002 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2003 b39d25c7 2018-07-10 stsp else
2004 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2005 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2006 11b20872 2019-11-08 stsp if (tc)
2007 11b20872 2019-11-08 stsp wattr_on(view->window,
2008 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2009 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2010 11b20872 2019-11-08 stsp if (tc)
2011 11b20872 2019-11-08 stsp wattr_off(view->window,
2012 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2013 27a741e5 2019-09-11 stsp col = limit;
2014 b39d25c7 2018-07-10 stsp if (col > avail)
2015 b39d25c7 2018-07-10 stsp goto done;
2016 6570a66d 2019-11-08 stsp
2017 6570a66d 2019-11-08 stsp if (avail >= 120) {
2018 6570a66d 2019-11-08 stsp char *id_str;
2019 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2020 6570a66d 2019-11-08 stsp if (err)
2021 6570a66d 2019-11-08 stsp goto done;
2022 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2023 11b20872 2019-11-08 stsp if (tc)
2024 11b20872 2019-11-08 stsp wattr_on(view->window,
2025 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2026 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2027 11b20872 2019-11-08 stsp if (tc)
2028 11b20872 2019-11-08 stsp wattr_off(view->window,
2029 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2030 6570a66d 2019-11-08 stsp free(id_str);
2031 6570a66d 2019-11-08 stsp col += 9;
2032 6570a66d 2019-11-08 stsp if (col > avail)
2033 6570a66d 2019-11-08 stsp goto done;
2034 6570a66d 2019-11-08 stsp }
2035 b39d25c7 2018-07-10 stsp
2036 f69c5a46 2022-07-19 thomas if (s->use_committer)
2037 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2038 f69c5a46 2022-07-19 thomas else
2039 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2040 5813d178 2019-03-09 stsp if (author == NULL) {
2041 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2042 80ddbec8 2018-04-29 stsp goto done;
2043 80ddbec8 2018-04-29 stsp }
2044 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2045 bb737323 2018-05-20 stsp if (err)
2046 bb737323 2018-05-20 stsp goto done;
2047 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2048 11b20872 2019-11-08 stsp if (tc)
2049 11b20872 2019-11-08 stsp wattr_on(view->window,
2050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2051 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2052 11b20872 2019-11-08 stsp if (tc)
2053 11b20872 2019-11-08 stsp wattr_off(view->window,
2054 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2055 bb737323 2018-05-20 stsp col += author_width;
2056 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2057 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2058 bb737323 2018-05-20 stsp col++;
2059 bb737323 2018-05-20 stsp author_width++;
2060 bb737323 2018-05-20 stsp }
2061 9c2eaf34 2018-05-20 stsp if (col > avail)
2062 9c2eaf34 2018-05-20 stsp goto done;
2063 80ddbec8 2018-04-29 stsp
2064 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2065 5943eee2 2019-08-13 stsp if (err)
2066 6d9fbc00 2018-04-29 stsp goto done;
2067 bb737323 2018-05-20 stsp logmsg = logmsg0;
2068 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2069 bb737323 2018-05-20 stsp logmsg++;
2070 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2071 bb737323 2018-05-20 stsp if (newline)
2072 bb737323 2018-05-20 stsp *newline = '\0';
2073 f91a2b48 2022-06-23 thomas limit = avail - col;
2074 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2075 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2076 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2077 f91a2b48 2022-06-23 thomas limit, col, 1);
2078 331b1a16 2022-06-23 thomas if (err)
2079 331b1a16 2022-06-23 thomas goto done;
2080 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2081 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2082 27a741e5 2019-09-11 stsp while (col < avail) {
2083 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2084 bb737323 2018-05-20 stsp col++;
2085 881b2d3e 2018-04-30 stsp }
2086 80ddbec8 2018-04-29 stsp done:
2087 80ddbec8 2018-04-29 stsp free(logmsg0);
2088 bb737323 2018-05-20 stsp free(wlogmsg);
2089 5813d178 2019-03-09 stsp free(author);
2090 bb737323 2018-05-20 stsp free(wauthor);
2091 80ddbec8 2018-04-29 stsp free(line);
2092 80ddbec8 2018-04-29 stsp return err;
2093 80ddbec8 2018-04-29 stsp }
2094 26ed57b2 2018-05-19 stsp
2095 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2096 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2097 899d86c2 2018-05-10 stsp struct got_object_id *id)
2098 80ddbec8 2018-04-29 stsp {
2099 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2100 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2101 80ddbec8 2018-04-29 stsp
2102 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2103 80ddbec8 2018-04-29 stsp if (entry == NULL)
2104 899d86c2 2018-05-10 stsp return NULL;
2105 99db9666 2018-05-07 stsp
2106 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2107 d68b9737 2022-09-05 thomas if (dup == NULL) {
2108 d68b9737 2022-09-05 thomas free(entry);
2109 d68b9737 2022-09-05 thomas return NULL;
2110 d68b9737 2022-09-05 thomas }
2111 d68b9737 2022-09-05 thomas
2112 d68b9737 2022-09-05 thomas entry->id = dup;
2113 99db9666 2018-05-07 stsp entry->commit = commit;
2114 899d86c2 2018-05-10 stsp return entry;
2115 99db9666 2018-05-07 stsp }
2116 80ddbec8 2018-04-29 stsp
2117 99db9666 2018-05-07 stsp static void
2118 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2119 99db9666 2018-05-07 stsp {
2120 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2121 99db9666 2018-05-07 stsp
2122 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2123 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2124 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2125 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2126 d68b9737 2022-09-05 thomas free(entry->id);
2127 99db9666 2018-05-07 stsp free(entry);
2128 99db9666 2018-05-07 stsp }
2129 99db9666 2018-05-07 stsp
2130 99db9666 2018-05-07 stsp static void
2131 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2132 99db9666 2018-05-07 stsp {
2133 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2134 99db9666 2018-05-07 stsp pop_commit(commits);
2135 c4972b91 2018-05-07 stsp }
2136 c4972b91 2018-05-07 stsp
2137 c4972b91 2018-05-07 stsp static const struct got_error *
2138 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2139 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2140 13add988 2019-10-15 stsp {
2141 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2142 13add988 2019-10-15 stsp regmatch_t regmatch;
2143 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2144 13add988 2019-10-15 stsp
2145 13add988 2019-10-15 stsp *have_match = 0;
2146 13add988 2019-10-15 stsp
2147 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2148 13add988 2019-10-15 stsp if (err)
2149 13add988 2019-10-15 stsp return err;
2150 13add988 2019-10-15 stsp
2151 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2152 13add988 2019-10-15 stsp if (err)
2153 13add988 2019-10-15 stsp goto done;
2154 13add988 2019-10-15 stsp
2155 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2156 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2157 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2158 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2159 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2160 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2161 13add988 2019-10-15 stsp *have_match = 1;
2162 13add988 2019-10-15 stsp done:
2163 13add988 2019-10-15 stsp free(id_str);
2164 13add988 2019-10-15 stsp free(logmsg);
2165 13add988 2019-10-15 stsp return err;
2166 13add988 2019-10-15 stsp }
2167 13add988 2019-10-15 stsp
2168 13add988 2019-10-15 stsp static const struct got_error *
2169 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2170 c4972b91 2018-05-07 stsp {
2171 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2172 9ba79e04 2018-06-11 stsp
2173 1a76625f 2018-10-22 stsp /*
2174 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2175 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2176 1a76625f 2018-10-22 stsp * while updating the display.
2177 1a76625f 2018-10-22 stsp */
2178 4e0d2870 2020-12-07 naddy do {
2179 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2180 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2181 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2182 1a76625f 2018-10-22 stsp int errcode;
2183 899d86c2 2018-05-10 stsp
2184 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2185 4e0d2870 2020-12-07 naddy NULL, NULL);
2186 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2187 ecb28ae0 2018-07-16 stsp break;
2188 899d86c2 2018-05-10 stsp
2189 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2190 9ba79e04 2018-06-11 stsp if (err)
2191 9ba79e04 2018-06-11 stsp break;
2192 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2193 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2194 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2195 9ba79e04 2018-06-11 stsp break;
2196 9ba79e04 2018-06-11 stsp }
2197 93e45b7c 2018-09-24 stsp
2198 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2199 1a76625f 2018-10-22 stsp if (errcode) {
2200 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2201 13add988 2019-10-15 stsp "pthread_mutex_lock");
2202 1a76625f 2018-10-22 stsp break;
2203 1a76625f 2018-10-22 stsp }
2204 1a76625f 2018-10-22 stsp
2205 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2206 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2207 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2208 1a76625f 2018-10-22 stsp
2209 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2210 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2211 7c1452c1 2020-03-26 stsp int have_match;
2212 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2213 7c1452c1 2020-03-26 stsp if (err)
2214 7c1452c1 2020-03-26 stsp break;
2215 7c1452c1 2020-03-26 stsp if (have_match)
2216 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2217 13add988 2019-10-15 stsp }
2218 13add988 2019-10-15 stsp
2219 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2220 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2221 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2222 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2223 7c1452c1 2020-03-26 stsp if (err)
2224 13add988 2019-10-15 stsp break;
2225 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2226 899d86c2 2018-05-10 stsp
2227 9ba79e04 2018-06-11 stsp return err;
2228 0553a4e3 2018-04-30 stsp }
2229 0553a4e3 2018-04-30 stsp
2230 2b779855 2020-12-05 naddy static void
2231 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2232 2b779855 2020-12-05 naddy {
2233 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2234 2b779855 2020-12-05 naddy int ncommits = 0;
2235 2b779855 2020-12-05 naddy
2236 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2237 2b779855 2020-12-05 naddy while (entry) {
2238 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2239 2b779855 2020-12-05 naddy s->selected_entry = entry;
2240 2b779855 2020-12-05 naddy break;
2241 2b779855 2020-12-05 naddy }
2242 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2243 2b779855 2020-12-05 naddy ncommits++;
2244 2b779855 2020-12-05 naddy }
2245 2b779855 2020-12-05 naddy }
2246 2b779855 2020-12-05 naddy
2247 0553a4e3 2018-04-30 stsp static const struct got_error *
2248 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2249 0553a4e3 2018-04-30 stsp {
2250 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2251 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2252 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2253 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2254 60493ae3 2019-06-20 stsp int width;
2255 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2256 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2257 8b473291 2019-02-21 stsp char *refs_str = NULL;
2258 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2259 11b20872 2019-11-08 stsp struct tog_color *tc;
2260 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2261 bd3f8225 2022-08-12 thomas
2262 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2263 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2264 0553a4e3 2018-04-30 stsp
2265 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2266 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2267 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2268 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2269 1a76625f 2018-10-22 stsp if (err)
2270 ecb28ae0 2018-07-16 stsp return err;
2271 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2272 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2273 d2075bf3 2020-12-25 stsp if (refs) {
2274 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2275 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2276 d2075bf3 2020-12-25 stsp if (err)
2277 d2075bf3 2020-12-25 stsp goto done;
2278 d2075bf3 2020-12-25 stsp }
2279 867c6645 2018-07-10 stsp }
2280 359bfafd 2019-02-22 stsp
2281 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2282 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2283 1a76625f 2018-10-22 stsp
2284 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2285 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2286 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2287 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2288 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2289 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2290 8f4ed634 2020-03-26 stsp goto done;
2291 8f4ed634 2020-03-26 stsp }
2292 8f4ed634 2020-03-26 stsp } else {
2293 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2294 f9686aa5 2020-03-27 stsp
2295 f9686aa5 2020-03-27 stsp if (view->searching) {
2296 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2297 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2298 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2299 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2300 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2301 f9686aa5 2020-03-27 stsp search_str = "searching...";
2302 f9686aa5 2020-03-27 stsp }
2303 f9686aa5 2020-03-27 stsp
2304 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2305 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2306 f9686aa5 2020-03-27 stsp search_str ? search_str :
2307 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2308 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2309 8f4ed634 2020-03-26 stsp goto done;
2310 8f4ed634 2020-03-26 stsp }
2311 8b473291 2019-02-21 stsp }
2312 1a76625f 2018-10-22 stsp
2313 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2314 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2315 91198554 2022-06-23 thomas "........................................",
2316 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2317 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2318 1a76625f 2018-10-22 stsp header = NULL;
2319 1a76625f 2018-10-22 stsp goto done;
2320 1a76625f 2018-10-22 stsp }
2321 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2322 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2323 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2324 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2325 1a76625f 2018-10-22 stsp header = NULL;
2326 1a76625f 2018-10-22 stsp goto done;
2327 ecb28ae0 2018-07-16 stsp }
2328 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2329 1a76625f 2018-10-22 stsp if (err)
2330 1a76625f 2018-10-22 stsp goto done;
2331 867c6645 2018-07-10 stsp
2332 2814baeb 2018-08-01 stsp werase(view->window);
2333 867c6645 2018-07-10 stsp
2334 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2335 a3404814 2018-09-02 stsp wstandout(view->window);
2336 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2337 11b20872 2019-11-08 stsp if (tc)
2338 11b20872 2019-11-08 stsp wattr_on(view->window,
2339 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2340 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2341 11b20872 2019-11-08 stsp if (tc)
2342 11b20872 2019-11-08 stsp wattr_off(view->window,
2343 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2344 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2345 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2346 1a76625f 2018-10-22 stsp width++;
2347 1a76625f 2018-10-22 stsp }
2348 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2349 a3404814 2018-09-02 stsp wstandend(view->window);
2350 ecb28ae0 2018-07-16 stsp free(wline);
2351 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2352 1a76625f 2018-10-22 stsp goto done;
2353 0553a4e3 2018-04-30 stsp
2354 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2355 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2356 5813d178 2019-03-09 stsp ncommits = 0;
2357 05171be4 2022-06-23 thomas view->maxx = 0;
2358 5813d178 2019-03-09 stsp while (entry) {
2359 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2360 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2361 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2362 5813d178 2019-03-09 stsp int width;
2363 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2364 5813d178 2019-03-09 stsp break;
2365 f69c5a46 2022-07-19 thomas if (s->use_committer)
2366 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2367 f69c5a46 2022-07-19 thomas else
2368 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2369 5813d178 2019-03-09 stsp if (author == NULL) {
2370 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2371 5813d178 2019-03-09 stsp goto done;
2372 5813d178 2019-03-09 stsp }
2373 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2374 27a741e5 2019-09-11 stsp date_display_cols);
2375 5813d178 2019-03-09 stsp if (author_cols < width)
2376 5813d178 2019-03-09 stsp author_cols = width;
2377 5813d178 2019-03-09 stsp free(wauthor);
2378 5813d178 2019-03-09 stsp free(author);
2379 ef944b8b 2022-07-21 thomas if (err)
2380 ef944b8b 2022-07-21 thomas goto done;
2381 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2382 05171be4 2022-06-23 thomas if (err)
2383 05171be4 2022-06-23 thomas goto done;
2384 05171be4 2022-06-23 thomas msg = msg0;
2385 05171be4 2022-06-23 thomas while (*msg == '\n')
2386 05171be4 2022-06-23 thomas ++msg;
2387 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2388 331b1a16 2022-06-23 thomas *eol = '\0';
2389 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2390 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2391 331b1a16 2022-06-23 thomas if (err)
2392 331b1a16 2022-06-23 thomas goto done;
2393 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2394 05171be4 2022-06-23 thomas free(msg0);
2395 331b1a16 2022-06-23 thomas free(wmsg);
2396 7ca04879 2019-10-19 stsp ncommits++;
2397 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2398 5813d178 2019-03-09 stsp }
2399 5813d178 2019-03-09 stsp
2400 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2401 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2402 867c6645 2018-07-10 stsp ncommits = 0;
2403 899d86c2 2018-05-10 stsp while (entry) {
2404 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2405 899d86c2 2018-05-10 stsp break;
2406 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2407 2814baeb 2018-08-01 stsp wstandout(view->window);
2408 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2409 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2410 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2411 2814baeb 2018-08-01 stsp wstandend(view->window);
2412 0553a4e3 2018-04-30 stsp if (err)
2413 60493ae3 2019-06-20 stsp goto done;
2414 0553a4e3 2018-04-30 stsp ncommits++;
2415 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2416 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2417 80ddbec8 2018-04-29 stsp }
2418 80ddbec8 2018-04-29 stsp
2419 a5d43cac 2022-07-01 thomas view_border(view);
2420 1a76625f 2018-10-22 stsp done:
2421 1a76625f 2018-10-22 stsp free(id_str);
2422 8b473291 2019-02-21 stsp free(refs_str);
2423 1a76625f 2018-10-22 stsp free(ncommits_str);
2424 1a76625f 2018-10-22 stsp free(header);
2425 80ddbec8 2018-04-29 stsp return err;
2426 9f7d7167 2018-04-29 stsp }
2427 07b55e75 2018-05-10 stsp
2428 07b55e75 2018-05-10 stsp static void
2429 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2430 07b55e75 2018-05-10 stsp {
2431 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2432 07b55e75 2018-05-10 stsp int nscrolled = 0;
2433 07b55e75 2018-05-10 stsp
2434 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2435 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2436 07b55e75 2018-05-10 stsp return;
2437 9f7d7167 2018-04-29 stsp
2438 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2439 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2440 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2441 07b55e75 2018-05-10 stsp if (entry) {
2442 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2443 07b55e75 2018-05-10 stsp nscrolled++;
2444 07b55e75 2018-05-10 stsp }
2445 07b55e75 2018-05-10 stsp }
2446 aa075928 2018-05-10 stsp }
2447 aa075928 2018-05-10 stsp
2448 aa075928 2018-05-10 stsp static const struct got_error *
2449 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2450 aa075928 2018-05-10 stsp {
2451 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2452 5e224a3e 2019-02-22 stsp int errcode;
2453 8a42fca8 2019-02-22 stsp
2454 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2455 aa075928 2018-05-10 stsp
2456 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2457 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2458 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2459 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2460 7aafa0d1 2019-02-22 stsp if (errcode)
2461 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2462 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2463 7c1452c1 2020-03-26 stsp
2464 7c1452c1 2020-03-26 stsp /*
2465 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2466 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2467 7c1452c1 2020-03-26 stsp */
2468 7c1452c1 2020-03-26 stsp if (!wait)
2469 7c1452c1 2020-03-26 stsp break;
2470 7c1452c1 2020-03-26 stsp
2471 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2472 ffe38506 2020-12-01 naddy show_log_view(view);
2473 7c1452c1 2020-03-26 stsp update_panels();
2474 7c1452c1 2020-03-26 stsp doupdate();
2475 7c1452c1 2020-03-26 stsp
2476 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2477 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2478 82954512 2020-02-03 stsp if (errcode)
2479 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2480 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2481 82954512 2020-02-03 stsp
2482 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2483 ffe38506 2020-12-01 naddy show_log_view(view);
2484 7c1452c1 2020-03-26 stsp update_panels();
2485 7c1452c1 2020-03-26 stsp doupdate();
2486 5e224a3e 2019-02-22 stsp }
2487 5e224a3e 2019-02-22 stsp
2488 5e224a3e 2019-02-22 stsp return NULL;
2489 a5d43cac 2022-07-01 thomas }
2490 a5d43cac 2022-07-01 thomas
2491 a5d43cac 2022-07-01 thomas static const struct got_error *
2492 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2493 a5d43cac 2022-07-01 thomas {
2494 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2495 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2496 fe731b51 2022-07-14 thomas
2497 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2498 fe731b51 2022-07-14 thomas return NULL;
2499 a5d43cac 2022-07-01 thomas
2500 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2501 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2502 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2503 a5d43cac 2022-07-01 thomas
2504 a5d43cac 2022-07-01 thomas return err;
2505 5e224a3e 2019-02-22 stsp }
2506 5e224a3e 2019-02-22 stsp
2507 5e224a3e 2019-02-22 stsp static const struct got_error *
2508 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2509 5e224a3e 2019-02-22 stsp {
2510 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2511 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2512 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2513 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2514 5e224a3e 2019-02-22 stsp
2515 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2516 5e224a3e 2019-02-22 stsp return NULL;
2517 5e224a3e 2019-02-22 stsp
2518 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2519 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2520 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2521 08ebd0a9 2019-02-22 stsp /*
2522 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2523 08ebd0a9 2019-02-22 stsp */
2524 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2525 07dd3ed3 2022-08-06 thomas ncommits_needed - s->commits.ncommits;
2526 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2527 5e224a3e 2019-02-22 stsp if (err)
2528 5e224a3e 2019-02-22 stsp return err;
2529 7aafa0d1 2019-02-22 stsp }
2530 b295e71b 2019-02-22 stsp
2531 7aafa0d1 2019-02-22 stsp do {
2532 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2533 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2534 88048b54 2019-02-21 stsp break;
2535 88048b54 2019-02-21 stsp
2536 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2537 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2538 aa075928 2018-05-10 stsp
2539 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2540 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2541 dd0a52c1 2018-05-20 stsp break;
2542 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2543 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2544 aa075928 2018-05-10 stsp
2545 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2546 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2547 a5d43cac 2022-07-01 thomas else
2548 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2549 a5d43cac 2022-07-01 thomas
2550 dd0a52c1 2018-05-20 stsp return err;
2551 07b55e75 2018-05-10 stsp }
2552 4a7f7875 2018-05-10 stsp
2553 cd0acaa7 2018-05-20 stsp static const struct got_error *
2554 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2555 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2556 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2557 cd0acaa7 2018-05-20 stsp {
2558 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2559 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2560 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2561 cd0acaa7 2018-05-20 stsp
2562 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2563 15a94983 2018-12-23 stsp if (diff_view == NULL)
2564 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2565 ea5e7bb5 2018-08-01 stsp
2566 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2567 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2568 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2569 e5a0f69f 2018-08-18 stsp if (err == NULL)
2570 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2571 cd0acaa7 2018-05-20 stsp return err;
2572 4a7f7875 2018-05-10 stsp }
2573 4a7f7875 2018-05-10 stsp
2574 80ddbec8 2018-04-29 stsp static const struct got_error *
2575 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2576 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2577 9343a5fb 2018-06-23 stsp {
2578 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2579 941e9f74 2019-05-21 stsp
2580 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2581 941e9f74 2019-05-21 stsp if (parent == NULL)
2582 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2583 941e9f74 2019-05-21 stsp
2584 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2585 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2586 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2587 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2588 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2589 941e9f74 2019-05-21 stsp s->tree = subtree;
2590 941e9f74 2019-05-21 stsp s->selected = 0;
2591 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2592 941e9f74 2019-05-21 stsp return NULL;
2593 941e9f74 2019-05-21 stsp }
2594 941e9f74 2019-05-21 stsp
2595 941e9f74 2019-05-21 stsp static const struct got_error *
2596 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2597 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2598 941e9f74 2019-05-21 stsp {
2599 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2600 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2601 941e9f74 2019-05-21 stsp const char *p;
2602 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2603 9343a5fb 2018-06-23 stsp
2604 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2605 941e9f74 2019-05-21 stsp p = path;
2606 941e9f74 2019-05-21 stsp while (*p) {
2607 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2608 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2609 56e0773d 2019-11-28 stsp char *te_name;
2610 33cbf02b 2020-01-12 stsp
2611 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2612 33cbf02b 2020-01-12 stsp p++;
2613 941e9f74 2019-05-21 stsp
2614 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2615 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2616 941e9f74 2019-05-21 stsp if (slash == NULL)
2617 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2618 33cbf02b 2020-01-12 stsp else
2619 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2620 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2621 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2622 56e0773d 2019-11-28 stsp break;
2623 941e9f74 2019-05-21 stsp }
2624 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2625 56e0773d 2019-11-28 stsp if (te == NULL) {
2626 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2627 56e0773d 2019-11-28 stsp free(te_name);
2628 941e9f74 2019-05-21 stsp break;
2629 941e9f74 2019-05-21 stsp }
2630 56e0773d 2019-11-28 stsp free(te_name);
2631 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2632 941e9f74 2019-05-21 stsp
2633 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2634 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2635 b03c880f 2019-05-21 stsp
2636 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2637 941e9f74 2019-05-21 stsp if (slash)
2638 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2639 941e9f74 2019-05-21 stsp else
2640 941e9f74 2019-05-21 stsp subpath = strdup(path);
2641 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2642 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2643 941e9f74 2019-05-21 stsp break;
2644 941e9f74 2019-05-21 stsp }
2645 941e9f74 2019-05-21 stsp
2646 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2647 941e9f74 2019-05-21 stsp subpath);
2648 941e9f74 2019-05-21 stsp if (err)
2649 941e9f74 2019-05-21 stsp break;
2650 941e9f74 2019-05-21 stsp
2651 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2652 941e9f74 2019-05-21 stsp free(tree_id);
2653 941e9f74 2019-05-21 stsp if (err)
2654 941e9f74 2019-05-21 stsp break;
2655 941e9f74 2019-05-21 stsp
2656 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2657 941e9f74 2019-05-21 stsp if (err) {
2658 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2659 941e9f74 2019-05-21 stsp break;
2660 941e9f74 2019-05-21 stsp }
2661 941e9f74 2019-05-21 stsp if (slash == NULL)
2662 941e9f74 2019-05-21 stsp break;
2663 941e9f74 2019-05-21 stsp free(subpath);
2664 941e9f74 2019-05-21 stsp subpath = NULL;
2665 941e9f74 2019-05-21 stsp p = slash;
2666 941e9f74 2019-05-21 stsp }
2667 941e9f74 2019-05-21 stsp
2668 941e9f74 2019-05-21 stsp free(subpath);
2669 1a76625f 2018-10-22 stsp return err;
2670 61266923 2020-01-14 stsp }
2671 61266923 2020-01-14 stsp
2672 61266923 2020-01-14 stsp static const struct got_error *
2673 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2674 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2675 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2676 55cccc34 2020-02-20 stsp {
2677 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2678 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2679 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2680 55cccc34 2020-02-20 stsp
2681 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2682 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2683 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2684 55cccc34 2020-02-20 stsp
2685 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2686 bc573f3b 2021-07-10 stsp if (err)
2687 55cccc34 2020-02-20 stsp return err;
2688 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2689 55cccc34 2020-02-20 stsp
2690 55cccc34 2020-02-20 stsp *new_view = tree_view;
2691 55cccc34 2020-02-20 stsp
2692 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2693 55cccc34 2020-02-20 stsp return NULL;
2694 55cccc34 2020-02-20 stsp
2695 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2696 55cccc34 2020-02-20 stsp }
2697 55cccc34 2020-02-20 stsp
2698 55cccc34 2020-02-20 stsp static const struct got_error *
2699 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2700 61266923 2020-01-14 stsp {
2701 61266923 2020-01-14 stsp sigset_t sigset;
2702 61266923 2020-01-14 stsp int errcode;
2703 61266923 2020-01-14 stsp
2704 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2705 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2706 61266923 2020-01-14 stsp
2707 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2708 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2709 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2710 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2711 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2712 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2713 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2714 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2715 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2716 61266923 2020-01-14 stsp
2717 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2718 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2719 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2720 61266923 2020-01-14 stsp
2721 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2722 61266923 2020-01-14 stsp if (errcode)
2723 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2724 61266923 2020-01-14 stsp
2725 61266923 2020-01-14 stsp return NULL;
2726 1a76625f 2018-10-22 stsp }
2727 1a76625f 2018-10-22 stsp
2728 1a76625f 2018-10-22 stsp static void *
2729 1a76625f 2018-10-22 stsp log_thread(void *arg)
2730 1a76625f 2018-10-22 stsp {
2731 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2732 1a76625f 2018-10-22 stsp int errcode = 0;
2733 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2734 1a76625f 2018-10-22 stsp int done = 0;
2735 61266923 2020-01-14 stsp
2736 f2d749db 2022-07-12 thomas /*
2737 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2738 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2739 f2d749db 2022-07-12 thomas */
2740 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2741 f2d749db 2022-07-12 thomas if (errcode) {
2742 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2743 61266923 2020-01-14 stsp return (void *)err;
2744 f2d749db 2022-07-12 thomas }
2745 1a76625f 2018-10-22 stsp
2746 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2747 f2d749db 2022-07-12 thomas if (err) {
2748 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2749 f2d749db 2022-07-12 thomas goto done;
2750 f2d749db 2022-07-12 thomas }
2751 f2d749db 2022-07-12 thomas
2752 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2753 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2754 f2d749db 2022-07-12 thomas if (errcode) {
2755 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2756 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2757 f2d749db 2022-07-12 thomas goto done;
2758 f2d749db 2022-07-12 thomas }
2759 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2760 1a76625f 2018-10-22 stsp if (err) {
2761 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2762 f2d749db 2022-07-12 thomas goto done;
2763 1a76625f 2018-10-22 stsp err = NULL;
2764 1a76625f 2018-10-22 stsp done = 1;
2765 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2766 1a76625f 2018-10-22 stsp a->commits_needed--;
2767 1a76625f 2018-10-22 stsp
2768 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2769 3abe8080 2019-04-10 stsp if (errcode) {
2770 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2771 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2772 f2d749db 2022-07-12 thomas goto done;
2773 3abe8080 2019-04-10 stsp } else if (*a->quit)
2774 1a76625f 2018-10-22 stsp done = 1;
2775 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2776 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2777 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2778 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2779 1a76625f 2018-10-22 stsp }
2780 1a76625f 2018-10-22 stsp
2781 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2782 7c1452c1 2020-03-26 stsp if (errcode) {
2783 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2784 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2785 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2786 f2d749db 2022-07-12 thomas goto done;
2787 7c1452c1 2020-03-26 stsp }
2788 7c1452c1 2020-03-26 stsp
2789 1a76625f 2018-10-22 stsp if (done)
2790 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2791 7c1452c1 2020-03-26 stsp else {
2792 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2793 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2794 7c1452c1 2020-03-26 stsp &tog_mutex);
2795 f2d749db 2022-07-12 thomas if (errcode) {
2796 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2797 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2798 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2799 f2d749db 2022-07-12 thomas goto done;
2800 f2d749db 2022-07-12 thomas }
2801 21355643 2020-12-06 stsp if (*a->quit)
2802 21355643 2020-12-06 stsp done = 1;
2803 7c1452c1 2020-03-26 stsp }
2804 1a76625f 2018-10-22 stsp }
2805 1a76625f 2018-10-22 stsp }
2806 3abe8080 2019-04-10 stsp a->log_complete = 1;
2807 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2808 f2d749db 2022-07-12 thomas if (errcode)
2809 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2810 f2d749db 2022-07-12 thomas done:
2811 f2d749db 2022-07-12 thomas if (err) {
2812 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2813 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2814 f2d749db 2022-07-12 thomas }
2815 1a76625f 2018-10-22 stsp return (void *)err;
2816 1a76625f 2018-10-22 stsp }
2817 1a76625f 2018-10-22 stsp
2818 1a76625f 2018-10-22 stsp static const struct got_error *
2819 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2820 1a76625f 2018-10-22 stsp {
2821 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2822 1a76625f 2018-10-22 stsp int errcode;
2823 1a76625f 2018-10-22 stsp
2824 1a76625f 2018-10-22 stsp if (s->thread) {
2825 1a76625f 2018-10-22 stsp s->quit = 1;
2826 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2827 1a76625f 2018-10-22 stsp if (errcode)
2828 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2829 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2830 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2831 1a76625f 2018-10-22 stsp if (errcode)
2832 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2833 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2834 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2835 1a76625f 2018-10-22 stsp if (errcode)
2836 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2837 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2838 1a76625f 2018-10-22 stsp if (errcode)
2839 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2840 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2841 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2842 1a76625f 2018-10-22 stsp }
2843 1a76625f 2018-10-22 stsp
2844 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2845 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2846 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2847 1af5eddf 2022-06-23 thomas }
2848 1af5eddf 2022-06-23 thomas
2849 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2850 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2851 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2852 1af5eddf 2022-06-23 thomas if (err == NULL)
2853 1af5eddf 2022-06-23 thomas err = pack_err;
2854 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2855 1a76625f 2018-10-22 stsp }
2856 1a76625f 2018-10-22 stsp
2857 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2858 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2859 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2860 1a76625f 2018-10-22 stsp }
2861 1a76625f 2018-10-22 stsp
2862 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2863 9343a5fb 2018-06-23 stsp }
2864 9343a5fb 2018-06-23 stsp
2865 9343a5fb 2018-06-23 stsp static const struct got_error *
2866 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2867 1a76625f 2018-10-22 stsp {
2868 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2869 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2870 276b94a1 2020-11-13 naddy int errcode;
2871 1a76625f 2018-10-22 stsp
2872 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2873 276b94a1 2020-11-13 naddy
2874 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2875 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2876 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2877 276b94a1 2020-11-13 naddy
2878 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2879 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2880 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2881 276b94a1 2020-11-13 naddy
2882 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2883 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2884 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2885 1a76625f 2018-10-22 stsp free(s->start_id);
2886 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2887 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2888 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2889 1a76625f 2018-10-22 stsp return err;
2890 1a76625f 2018-10-22 stsp }
2891 1a76625f 2018-10-22 stsp
2892 1a76625f 2018-10-22 stsp static const struct got_error *
2893 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2894 60493ae3 2019-06-20 stsp {
2895 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2896 60493ae3 2019-06-20 stsp
2897 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2898 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2899 60493ae3 2019-06-20 stsp return NULL;
2900 60493ae3 2019-06-20 stsp }
2901 60493ae3 2019-06-20 stsp
2902 60493ae3 2019-06-20 stsp static const struct got_error *
2903 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2904 60493ae3 2019-06-20 stsp {
2905 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2906 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2907 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2908 60493ae3 2019-06-20 stsp
2909 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2910 f9686aa5 2020-03-27 stsp show_log_view(view);
2911 f9686aa5 2020-03-27 stsp update_panels();
2912 f9686aa5 2020-03-27 stsp doupdate();
2913 f9686aa5 2020-03-27 stsp
2914 96e2b566 2019-07-08 stsp if (s->search_entry) {
2915 13add988 2019-10-15 stsp int errcode, ch;
2916 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2917 13add988 2019-10-15 stsp if (errcode)
2918 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2919 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2920 13add988 2019-10-15 stsp ch = wgetch(view->window);
2921 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2922 13add988 2019-10-15 stsp if (errcode)
2923 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2924 13add988 2019-10-15 stsp "pthread_mutex_lock");
2925 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2926 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2927 678cbce5 2019-07-28 stsp return NULL;
2928 678cbce5 2019-07-28 stsp }
2929 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2930 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2931 96e2b566 2019-07-08 stsp else
2932 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2933 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2934 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2935 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2936 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2937 df5e841d 2022-06-23 thomas
2938 df5e841d 2022-06-23 thomas /*
2939 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2940 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2941 1f4d5162 2022-06-23 thomas * might have changed.
2942 df5e841d 2022-06-23 thomas */
2943 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2944 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2945 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2946 df5e841d 2022-06-23 thomas else
2947 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2948 b74feda6 2022-06-23 thomas } else {
2949 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2950 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2951 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2952 df5e841d 2022-06-23 thomas else
2953 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2954 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2955 b74feda6 2022-06-23 thomas }
2956 20be8d96 2019-06-21 stsp } else {
2957 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2958 20be8d96 2019-06-21 stsp }
2959 60493ae3 2019-06-20 stsp
2960 60493ae3 2019-06-20 stsp while (1) {
2961 13add988 2019-10-15 stsp int have_match = 0;
2962 13add988 2019-10-15 stsp
2963 60493ae3 2019-06-20 stsp if (entry == NULL) {
2964 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2965 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2966 f9967bca 2020-03-27 stsp view->search_next_done =
2967 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2968 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2969 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2970 f801134a 2019-06-25 stsp return NULL;
2971 60493ae3 2019-06-20 stsp }
2972 96e2b566 2019-07-08 stsp /*
2973 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2974 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2975 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2976 96e2b566 2019-07-08 stsp */
2977 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2978 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2979 60493ae3 2019-06-20 stsp }
2980 60493ae3 2019-06-20 stsp
2981 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2982 13add988 2019-10-15 stsp &view->regex);
2983 5943eee2 2019-08-13 stsp if (err)
2984 13add988 2019-10-15 stsp break;
2985 13add988 2019-10-15 stsp if (have_match) {
2986 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2987 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2988 60493ae3 2019-06-20 stsp break;
2989 60493ae3 2019-06-20 stsp }
2990 13add988 2019-10-15 stsp
2991 96e2b566 2019-07-08 stsp s->search_entry = entry;
2992 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2993 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2994 b1bf1435 2019-06-21 stsp else
2995 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2996 60493ae3 2019-06-20 stsp }
2997 60493ae3 2019-06-20 stsp
2998 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2999 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3000 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3001 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3002 60493ae3 2019-06-20 stsp if (err)
3003 60493ae3 2019-06-20 stsp return err;
3004 ead14cbe 2019-06-21 stsp cur++;
3005 ead14cbe 2019-06-21 stsp }
3006 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3007 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3008 60493ae3 2019-06-20 stsp if (err)
3009 60493ae3 2019-06-20 stsp return err;
3010 ead14cbe 2019-06-21 stsp cur--;
3011 60493ae3 2019-06-20 stsp }
3012 60493ae3 2019-06-20 stsp }
3013 60493ae3 2019-06-20 stsp
3014 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3015 96e2b566 2019-07-08 stsp
3016 60493ae3 2019-06-20 stsp return NULL;
3017 60493ae3 2019-06-20 stsp }
3018 60493ae3 2019-06-20 stsp
3019 60493ae3 2019-06-20 stsp static const struct got_error *
3020 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3021 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3022 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3023 80ddbec8 2018-04-29 stsp {
3024 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3025 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3026 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3027 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3028 1a76625f 2018-10-22 stsp int errcode;
3029 80ddbec8 2018-04-29 stsp
3030 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3031 f135c941 2020-02-20 stsp free(s->in_repo_path);
3032 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3033 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3034 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3035 f135c941 2020-02-20 stsp }
3036 ecb28ae0 2018-07-16 stsp
3037 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3038 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3039 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3040 78756c87 2020-11-24 stsp
3041 fb2756b9 2018-08-04 stsp s->repo = repo;
3042 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3043 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3044 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3045 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3046 9cd7cbd1 2020-12-07 stsp goto done;
3047 9cd7cbd1 2020-12-07 stsp }
3048 9cd7cbd1 2020-12-07 stsp }
3049 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3050 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3051 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3052 5036bf37 2018-09-24 stsp goto done;
3053 5036bf37 2018-09-24 stsp }
3054 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3055 e5a0f69f 2018-08-18 stsp
3056 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3057 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3058 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3059 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3060 11b20872 2019-11-08 stsp if (err)
3061 11b20872 2019-11-08 stsp goto done;
3062 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3063 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3064 11b20872 2019-11-08 stsp if (err) {
3065 11b20872 2019-11-08 stsp free_colors(&s->colors);
3066 11b20872 2019-11-08 stsp goto done;
3067 11b20872 2019-11-08 stsp }
3068 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3069 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3070 11b20872 2019-11-08 stsp if (err) {
3071 11b20872 2019-11-08 stsp free_colors(&s->colors);
3072 11b20872 2019-11-08 stsp goto done;
3073 11b20872 2019-11-08 stsp }
3074 11b20872 2019-11-08 stsp }
3075 11b20872 2019-11-08 stsp
3076 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3077 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3078 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3079 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3080 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3081 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3082 1a76625f 2018-10-22 stsp
3083 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3084 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3085 1af5eddf 2022-06-23 thomas if (err)
3086 1af5eddf 2022-06-23 thomas goto done;
3087 1af5eddf 2022-06-23 thomas }
3088 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3089 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3090 7cd52833 2022-06-23 thomas if (err)
3091 7cd52833 2022-06-23 thomas goto done;
3092 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3093 b672a97a 2020-01-27 stsp !s->log_branches);
3094 1a76625f 2018-10-22 stsp if (err)
3095 1a76625f 2018-10-22 stsp goto done;
3096 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3097 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3098 c5b78334 2020-01-12 stsp if (err)
3099 c5b78334 2020-01-12 stsp goto done;
3100 1a76625f 2018-10-22 stsp
3101 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3102 1a76625f 2018-10-22 stsp if (errcode) {
3103 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3104 1a76625f 2018-10-22 stsp goto done;
3105 1a76625f 2018-10-22 stsp }
3106 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3107 7c1452c1 2020-03-26 stsp if (errcode) {
3108 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3109 7c1452c1 2020-03-26 stsp goto done;
3110 7c1452c1 2020-03-26 stsp }
3111 1a76625f 2018-10-22 stsp
3112 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3113 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3114 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3115 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3116 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3117 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3118 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3119 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3120 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3121 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3122 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3123 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3124 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3125 ba4f502b 2018-08-04 stsp done:
3126 1a76625f 2018-10-22 stsp if (err)
3127 1a76625f 2018-10-22 stsp close_log_view(view);
3128 ba4f502b 2018-08-04 stsp return err;
3129 ba4f502b 2018-08-04 stsp }
3130 ba4f502b 2018-08-04 stsp
3131 e5a0f69f 2018-08-18 stsp static const struct got_error *
3132 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3133 ba4f502b 2018-08-04 stsp {
3134 f2f6d207 2020-11-24 stsp const struct got_error *err;
3135 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3136 ba4f502b 2018-08-04 stsp
3137 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3138 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3139 2b380cc8 2018-10-24 stsp &s->thread_args);
3140 2b380cc8 2018-10-24 stsp if (errcode)
3141 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3142 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3143 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3144 f2f6d207 2020-11-24 stsp if (err)
3145 f2f6d207 2020-11-24 stsp return err;
3146 f2f6d207 2020-11-24 stsp }
3147 2b380cc8 2018-10-24 stsp }
3148 2b380cc8 2018-10-24 stsp
3149 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3150 b31f89ff 2022-07-01 thomas }
3151 b31f89ff 2022-07-01 thomas
3152 b31f89ff 2022-07-01 thomas static void
3153 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3154 b31f89ff 2022-07-01 thomas {
3155 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3156 b31f89ff 2022-07-01 thomas
3157 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3158 b31f89ff 2022-07-01 thomas view->count = 0;
3159 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3160 b31f89ff 2022-07-01 thomas return;
3161 b31f89ff 2022-07-01 thomas
3162 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3163 b31f89ff 2022-07-01 thomas || home)
3164 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3165 b31f89ff 2022-07-01 thomas
3166 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3167 b31f89ff 2022-07-01 thomas --s->selected;
3168 b31f89ff 2022-07-01 thomas else
3169 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3170 b31f89ff 2022-07-01 thomas
3171 b31f89ff 2022-07-01 thomas select_commit(s);
3172 b31f89ff 2022-07-01 thomas return;
3173 b31f89ff 2022-07-01 thomas }
3174 b31f89ff 2022-07-01 thomas
3175 b31f89ff 2022-07-01 thomas static const struct got_error *
3176 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3177 b31f89ff 2022-07-01 thomas {
3178 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3179 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3180 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3181 b31f89ff 2022-07-01 thomas
3182 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3183 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3184 b31f89ff 2022-07-01 thomas return NULL;
3185 b31f89ff 2022-07-01 thomas
3186 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3187 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3188 b31f89ff 2022-07-01 thomas
3189 7ed048bd 2022-08-12 thomas if (!page) {
3190 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3191 b31f89ff 2022-07-01 thomas ++s->selected;
3192 b31f89ff 2022-07-01 thomas else
3193 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3194 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3195 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3196 7ed048bd 2022-08-12 thomas int n;
3197 7ed048bd 2022-08-12 thomas
3198 7ed048bd 2022-08-12 thomas s->selected = 0;
3199 7ed048bd 2022-08-12 thomas entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3200 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3201 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3202 7ed048bd 2022-08-12 thomas if (entry == NULL)
3203 7ed048bd 2022-08-12 thomas break;
3204 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3205 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3206 7ed048bd 2022-08-12 thomas }
3207 7ed048bd 2022-08-12 thomas if (n > 0)
3208 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3209 b31f89ff 2022-07-01 thomas } else {
3210 bd3f8225 2022-08-12 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1 &&
3211 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3212 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3213 bd3f8225 2022-08-12 thomas s->commits.ncommits - s->selected_entry->idx - 1);
3214 bd3f8225 2022-08-12 thomas else
3215 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3216 b31f89ff 2022-07-01 thomas }
3217 b31f89ff 2022-07-01 thomas if (err)
3218 b31f89ff 2022-07-01 thomas return err;
3219 b31f89ff 2022-07-01 thomas
3220 a5d43cac 2022-07-01 thomas /*
3221 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3222 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3223 a5d43cac 2022-07-01 thomas */
3224 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3225 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3226 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3227 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3228 25100026 2022-08-13 thomas }
3229 a5d43cac 2022-07-01 thomas
3230 b31f89ff 2022-07-01 thomas select_commit(s);
3231 b31f89ff 2022-07-01 thomas
3232 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3233 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3234 b31f89ff 2022-07-01 thomas view->count = 0;
3235 b31f89ff 2022-07-01 thomas
3236 b31f89ff 2022-07-01 thomas return NULL;
3237 e5a0f69f 2018-08-18 stsp }
3238 04cc582a 2018-08-01 stsp
3239 a5d43cac 2022-07-01 thomas static void
3240 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3241 a5d43cac 2022-07-01 thomas {
3242 24415785 2022-07-03 thomas *x = 0;
3243 24415785 2022-07-03 thomas *y = 0;
3244 24415785 2022-07-03 thomas
3245 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3246 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3247 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3248 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3249 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3250 53d2bdd3 2022-07-10 thomas else
3251 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3252 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3253 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3254 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3255 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3256 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3257 53d2bdd3 2022-07-10 thomas else
3258 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3259 53d2bdd3 2022-07-10 thomas }
3260 a5d43cac 2022-07-01 thomas }
3261 a5d43cac 2022-07-01 thomas
3262 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3263 e5a0f69f 2018-08-18 stsp static const struct got_error *
3264 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3265 a5d43cac 2022-07-01 thomas {
3266 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3267 a5d43cac 2022-07-01 thomas
3268 a5d43cac 2022-07-01 thomas view->nlines = y;
3269 64486692 2022-07-07 thomas view->ncols = COLS;
3270 a5d43cac 2022-07-01 thomas err = view_resize(view);
3271 a5d43cac 2022-07-01 thomas if (err)
3272 a5d43cac 2022-07-01 thomas return err;
3273 a5d43cac 2022-07-01 thomas
3274 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3275 a5d43cac 2022-07-01 thomas
3276 a5d43cac 2022-07-01 thomas return err;
3277 07dd3ed3 2022-08-06 thomas }
3278 07dd3ed3 2022-08-06 thomas
3279 07dd3ed3 2022-08-06 thomas static const struct got_error *
3280 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3281 07dd3ed3 2022-08-06 thomas {
3282 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3283 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3284 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3285 25100026 2022-08-13 thomas
3286 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3287 25100026 2022-08-13 thomas return NULL;
3288 07dd3ed3 2022-08-06 thomas
3289 07dd3ed3 2022-08-06 thomas g = view->gline;
3290 07dd3ed3 2022-08-06 thomas view->gline = 0;
3291 07dd3ed3 2022-08-06 thomas
3292 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3293 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3294 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3295 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3296 07dd3ed3 2022-08-06 thomas select_commit(s);
3297 07dd3ed3 2022-08-06 thomas return NULL;
3298 07dd3ed3 2022-08-06 thomas }
3299 07dd3ed3 2022-08-06 thomas
3300 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3301 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3302 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3303 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3304 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3305 07dd3ed3 2022-08-06 thomas if (err)
3306 07dd3ed3 2022-08-06 thomas return err;
3307 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3308 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3309 07dd3ed3 2022-08-06 thomas
3310 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3311 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3312 07dd3ed3 2022-08-06 thomas
3313 07dd3ed3 2022-08-06 thomas select_commit(s);
3314 07dd3ed3 2022-08-06 thomas return NULL;
3315 07dd3ed3 2022-08-06 thomas
3316 a5d43cac 2022-07-01 thomas }
3317 a5d43cac 2022-07-01 thomas
3318 a5d43cac 2022-07-01 thomas static const struct got_error *
3319 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3320 e5a0f69f 2018-08-18 stsp {
3321 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3322 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3323 7ed048bd 2022-08-12 thomas int eos, nscroll;
3324 80ddbec8 2018-04-29 stsp
3325 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3326 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3327 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3328 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3329 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3330 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3331 fb280deb 2021-08-30 stsp }
3332 c0be8933 2022-08-12 thomas if (err)
3333 c0be8933 2022-08-12 thomas return err;
3334 528dedf3 2021-08-30 stsp }
3335 068ab281 2022-07-01 thomas
3336 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3337 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3338 068ab281 2022-07-01 thomas --eos; /* border */
3339 07dd3ed3 2022-08-06 thomas
3340 07dd3ed3 2022-08-06 thomas if (view->gline)
3341 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3342 068ab281 2022-07-01 thomas
3343 528dedf3 2021-08-30 stsp switch (ch) {
3344 1e37a5c2 2019-05-12 jcs case 'q':
3345 1e37a5c2 2019-05-12 jcs s->quit = 1;
3346 05171be4 2022-06-23 thomas break;
3347 05171be4 2022-06-23 thomas case '0':
3348 05171be4 2022-06-23 thomas view->x = 0;
3349 05171be4 2022-06-23 thomas break;
3350 05171be4 2022-06-23 thomas case '$':
3351 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3352 07b0611c 2022-06-23 thomas view->count = 0;
3353 05171be4 2022-06-23 thomas break;
3354 05171be4 2022-06-23 thomas case KEY_RIGHT:
3355 05171be4 2022-06-23 thomas case 'l':
3356 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3357 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3358 07b0611c 2022-06-23 thomas else
3359 07b0611c 2022-06-23 thomas view->count = 0;
3360 1e37a5c2 2019-05-12 jcs break;
3361 05171be4 2022-06-23 thomas case KEY_LEFT:
3362 05171be4 2022-06-23 thomas case 'h':
3363 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3364 07b0611c 2022-06-23 thomas if (view->x <= 0)
3365 07b0611c 2022-06-23 thomas view->count = 0;
3366 05171be4 2022-06-23 thomas break;
3367 1e37a5c2 2019-05-12 jcs case 'k':
3368 1e37a5c2 2019-05-12 jcs case KEY_UP:
3369 1e37a5c2 2019-05-12 jcs case '<':
3370 1e37a5c2 2019-05-12 jcs case ',':
3371 f7140bf5 2021-10-17 thomas case CTRL('p'):
3372 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3373 912a3f79 2021-08-30 j break;
3374 912a3f79 2021-08-30 j case 'g':
3375 912a3f79 2021-08-30 j case KEY_HOME:
3376 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3377 07b0611c 2022-06-23 thomas view->count = 0;
3378 1e37a5c2 2019-05-12 jcs break;
3379 70f17a53 2022-06-13 thomas case CTRL('u'):
3380 23427b14 2022-06-23 thomas case 'u':
3381 70f17a53 2022-06-13 thomas nscroll /= 2;
3382 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3383 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3384 a4292ac5 2019-05-12 jcs case CTRL('b'):
3385 1c5e5faa 2022-06-23 thomas case 'b':
3386 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3387 1e37a5c2 2019-05-12 jcs break;
3388 1e37a5c2 2019-05-12 jcs case 'j':
3389 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3390 1e37a5c2 2019-05-12 jcs case '>':
3391 1e37a5c2 2019-05-12 jcs case '.':
3392 f7140bf5 2021-10-17 thomas case CTRL('n'):
3393 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3394 912a3f79 2021-08-30 j break;
3395 f69c5a46 2022-07-19 thomas case '@':
3396 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3397 f69c5a46 2022-07-19 thomas break;
3398 912a3f79 2021-08-30 j case 'G':
3399 912a3f79 2021-08-30 j case KEY_END: {
3400 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3401 912a3f79 2021-08-30 j * traverse them all. */
3402 07b0611c 2022-06-23 thomas view->count = 0;
3403 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3404 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3405 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3406 7ed048bd 2022-08-12 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3407 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3408 1e37a5c2 2019-05-12 jcs break;
3409 912a3f79 2021-08-30 j }
3410 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3411 23427b14 2022-06-23 thomas case 'd':
3412 70f17a53 2022-06-13 thomas nscroll /= 2;
3413 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3414 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3415 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3416 4c2d69cb 2022-06-23 thomas case 'f':
3417 b31f89ff 2022-07-01 thomas case ' ':
3418 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3419 1e37a5c2 2019-05-12 jcs break;
3420 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3421 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3422 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3423 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3424 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3425 2b779855 2020-12-05 naddy select_commit(s);
3426 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3427 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3428 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3429 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3430 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3431 0bf7f153 2020-12-02 naddy }
3432 1e37a5c2 2019-05-12 jcs break;
3433 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3434 444d5325 2022-07-03 thomas case '\r':
3435 07b0611c 2022-06-23 thomas view->count = 0;
3436 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3437 e5a0f69f 2018-08-18 stsp break;
3438 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3439 1e37a5c2 2019-05-12 jcs break;
3440 1be4947a 2022-07-22 thomas case 'T':
3441 07b0611c 2022-06-23 thomas view->count = 0;
3442 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3443 5036bf37 2018-09-24 stsp break;
3444 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3445 1e37a5c2 2019-05-12 jcs break;
3446 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3447 21355643 2020-12-06 stsp case CTRL('l'):
3448 21355643 2020-12-06 stsp case 'B':
3449 07b0611c 2022-06-23 thomas view->count = 0;
3450 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3451 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3452 1e37a5c2 2019-05-12 jcs break;
3453 21355643 2020-12-06 stsp err = stop_log_thread(s);
3454 74cfe85e 2020-10-20 stsp if (err)
3455 74cfe85e 2020-10-20 stsp return err;
3456 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3457 21355643 2020-12-06 stsp char *parent_path;
3458 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3459 21355643 2020-12-06 stsp if (err)
3460 21355643 2020-12-06 stsp return err;
3461 21355643 2020-12-06 stsp free(s->in_repo_path);
3462 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3463 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3464 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3465 21355643 2020-12-06 stsp struct got_object_id *start_id;
3466 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3467 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3468 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3469 21355643 2020-12-06 stsp if (err)
3470 21355643 2020-12-06 stsp return err;
3471 21355643 2020-12-06 stsp free(s->start_id);
3472 21355643 2020-12-06 stsp s->start_id = start_id;
3473 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3474 21355643 2020-12-06 stsp } else /* 'B' */
3475 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3476 21355643 2020-12-06 stsp
3477 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3478 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3479 bf7e79b3 2022-06-23 thomas if (err)
3480 bf7e79b3 2022-06-23 thomas return err;
3481 bf7e79b3 2022-06-23 thomas }
3482 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3483 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3484 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3485 74cfe85e 2020-10-20 stsp if (err)
3486 21355643 2020-12-06 stsp return err;
3487 51a10b52 2020-12-26 stsp tog_free_refs();
3488 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3489 ca51c541 2020-12-07 stsp if (err)
3490 ca51c541 2020-12-07 stsp return err;
3491 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3492 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3493 d01904d4 2019-06-25 stsp if (err)
3494 d01904d4 2019-06-25 stsp return err;
3495 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3496 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3497 b672a97a 2020-01-27 stsp if (err)
3498 b672a97a 2020-01-27 stsp return err;
3499 21355643 2020-12-06 stsp free_commits(&s->commits);
3500 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3501 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3502 21355643 2020-12-06 stsp s->selected_entry = NULL;
3503 21355643 2020-12-06 stsp s->selected = 0;
3504 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3505 21355643 2020-12-06 stsp s->quit = 0;
3506 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3507 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3508 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3509 94ecf40d 2022-07-22 thomas view->offset = 0;
3510 d01904d4 2019-06-25 stsp break;
3511 1be4947a 2022-07-22 thomas case 'R':
3512 07b0611c 2022-06-23 thomas view->count = 0;
3513 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3514 6458efa5 2020-11-24 stsp break;
3515 1e37a5c2 2019-05-12 jcs default:
3516 07b0611c 2022-06-23 thomas view->count = 0;
3517 1e37a5c2 2019-05-12 jcs break;
3518 899d86c2 2018-05-10 stsp }
3519 e5a0f69f 2018-08-18 stsp
3520 80ddbec8 2018-04-29 stsp return err;
3521 80ddbec8 2018-04-29 stsp }
3522 80ddbec8 2018-04-29 stsp
3523 4ed7e80c 2018-05-20 stsp static const struct got_error *
3524 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3525 c2db6724 2019-01-04 stsp {
3526 c2db6724 2019-01-04 stsp const struct got_error *error;
3527 c2db6724 2019-01-04 stsp
3528 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3529 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3530 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3531 37c06ea4 2019-07-15 stsp #endif
3532 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3533 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3534 c2db6724 2019-01-04 stsp
3535 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3536 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3537 c2db6724 2019-01-04 stsp
3538 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3539 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3540 c2db6724 2019-01-04 stsp
3541 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3542 c2db6724 2019-01-04 stsp if (error != NULL)
3543 c2db6724 2019-01-04 stsp return error;
3544 c2db6724 2019-01-04 stsp
3545 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3546 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3547 c2db6724 2019-01-04 stsp
3548 c2db6724 2019-01-04 stsp return NULL;
3549 c2db6724 2019-01-04 stsp }
3550 c2db6724 2019-01-04 stsp
3551 a915003a 2019-02-05 stsp static void
3552 a915003a 2019-02-05 stsp init_curses(void)
3553 a915003a 2019-02-05 stsp {
3554 296152d1 2022-05-31 thomas /*
3555 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3556 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3557 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3558 296152d1 2022-05-31 thomas */
3559 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3560 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3561 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3562 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3563 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3564 296152d1 2022-05-31 thomas
3565 a915003a 2019-02-05 stsp initscr();
3566 a915003a 2019-02-05 stsp cbreak();
3567 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3568 a915003a 2019-02-05 stsp noecho();
3569 a915003a 2019-02-05 stsp nonl();
3570 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3571 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3572 a915003a 2019-02-05 stsp curs_set(0);
3573 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3574 6d17833f 2019-11-08 stsp start_color();
3575 6d17833f 2019-11-08 stsp use_default_colors();
3576 6d17833f 2019-11-08 stsp }
3577 a915003a 2019-02-05 stsp }
3578 a915003a 2019-02-05 stsp
3579 c2db6724 2019-01-04 stsp static const struct got_error *
3580 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3581 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3582 f135c941 2020-02-20 stsp {
3583 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3584 f135c941 2020-02-20 stsp
3585 f135c941 2020-02-20 stsp if (argc == 0) {
3586 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3587 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3588 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3589 f135c941 2020-02-20 stsp return NULL;
3590 f135c941 2020-02-20 stsp }
3591 f135c941 2020-02-20 stsp
3592 f135c941 2020-02-20 stsp if (worktree) {
3593 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3594 bfd61697 2020-11-14 stsp char *p;
3595 f135c941 2020-02-20 stsp
3596 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3597 f135c941 2020-02-20 stsp if (err)
3598 f135c941 2020-02-20 stsp return err;
3599 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3600 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3601 bfd61697 2020-11-14 stsp p) == -1) {
3602 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3603 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3604 f135c941 2020-02-20 stsp }
3605 f135c941 2020-02-20 stsp free(p);
3606 f135c941 2020-02-20 stsp } else
3607 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3608 f135c941 2020-02-20 stsp
3609 f135c941 2020-02-20 stsp return err;
3610 f135c941 2020-02-20 stsp }
3611 f135c941 2020-02-20 stsp
3612 f135c941 2020-02-20 stsp static const struct got_error *
3613 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3614 9f7d7167 2018-04-29 stsp {
3615 80ddbec8 2018-04-29 stsp const struct got_error *error;
3616 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3617 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3618 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3619 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3620 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3621 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3622 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3623 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3624 04cc582a 2018-08-01 stsp struct tog_view *view;
3625 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3626 80ddbec8 2018-04-29 stsp
3627 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3628 80ddbec8 2018-04-29 stsp switch (ch) {
3629 b672a97a 2020-01-27 stsp case 'b':
3630 b672a97a 2020-01-27 stsp log_branches = 1;
3631 b672a97a 2020-01-27 stsp break;
3632 80ddbec8 2018-04-29 stsp case 'c':
3633 80ddbec8 2018-04-29 stsp start_commit = optarg;
3634 80ddbec8 2018-04-29 stsp break;
3635 ecb28ae0 2018-07-16 stsp case 'r':
3636 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3637 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3638 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3639 9ba1d308 2019-10-21 stsp optarg);
3640 ecb28ae0 2018-07-16 stsp break;
3641 80ddbec8 2018-04-29 stsp default:
3642 17020d27 2019-03-07 stsp usage_log();
3643 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3644 80ddbec8 2018-04-29 stsp }
3645 80ddbec8 2018-04-29 stsp }
3646 80ddbec8 2018-04-29 stsp
3647 80ddbec8 2018-04-29 stsp argc -= optind;
3648 80ddbec8 2018-04-29 stsp argv += optind;
3649 80ddbec8 2018-04-29 stsp
3650 f135c941 2020-02-20 stsp if (argc > 1)
3651 f135c941 2020-02-20 stsp usage_log();
3652 963f97a1 2019-03-18 stsp
3653 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3654 7cd52833 2022-06-23 thomas if (error != NULL)
3655 7cd52833 2022-06-23 thomas goto done;
3656 7cd52833 2022-06-23 thomas
3657 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3658 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3659 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3660 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3661 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3662 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3663 c156c7a4 2020-12-18 stsp goto done;
3664 a1fbf39a 2019-08-11 stsp if (worktree)
3665 6962eb72 2020-02-20 stsp repo_path =
3666 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3667 a1fbf39a 2019-08-11 stsp else
3668 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3669 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3670 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3671 c156c7a4 2020-12-18 stsp goto done;
3672 c156c7a4 2020-12-18 stsp }
3673 ecb28ae0 2018-07-16 stsp }
3674 ecb28ae0 2018-07-16 stsp
3675 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3676 80ddbec8 2018-04-29 stsp if (error != NULL)
3677 ecb28ae0 2018-07-16 stsp goto done;
3678 80ddbec8 2018-04-29 stsp
3679 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3680 f135c941 2020-02-20 stsp repo, worktree);
3681 f135c941 2020-02-20 stsp if (error)
3682 f135c941 2020-02-20 stsp goto done;
3683 f135c941 2020-02-20 stsp
3684 f135c941 2020-02-20 stsp init_curses();
3685 f135c941 2020-02-20 stsp
3686 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3687 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3688 c02c541e 2019-03-29 stsp if (error)
3689 c02c541e 2019-03-29 stsp goto done;
3690 c02c541e 2019-03-29 stsp
3691 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3692 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3693 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3694 87670572 2020-12-26 naddy if (error)
3695 87670572 2020-12-26 naddy goto done;
3696 87670572 2020-12-26 naddy }
3697 51a10b52 2020-12-26 stsp
3698 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3699 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3700 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3701 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3702 d8f38dc4 2020-12-05 stsp if (error)
3703 d8f38dc4 2020-12-05 stsp goto done;
3704 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3705 d8f38dc4 2020-12-05 stsp } else {
3706 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3707 d8f38dc4 2020-12-05 stsp if (error == NULL)
3708 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3709 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3710 d8f38dc4 2020-12-05 stsp goto done;
3711 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3712 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3713 d8f38dc4 2020-12-05 stsp if (error)
3714 d8f38dc4 2020-12-05 stsp goto done;
3715 d8f38dc4 2020-12-05 stsp }
3716 8b473291 2019-02-21 stsp
3717 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3718 04cc582a 2018-08-01 stsp if (view == NULL) {
3719 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3720 04cc582a 2018-08-01 stsp goto done;
3721 04cc582a 2018-08-01 stsp }
3722 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3723 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3724 ba4f502b 2018-08-04 stsp if (error)
3725 ba4f502b 2018-08-04 stsp goto done;
3726 2fc00ff4 2019-08-31 stsp if (worktree) {
3727 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3728 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3729 2fc00ff4 2019-08-31 stsp worktree = NULL;
3730 2fc00ff4 2019-08-31 stsp }
3731 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3732 ecb28ae0 2018-07-16 stsp done:
3733 f135c941 2020-02-20 stsp free(in_repo_path);
3734 ecb28ae0 2018-07-16 stsp free(repo_path);
3735 ecb28ae0 2018-07-16 stsp free(cwd);
3736 899d86c2 2018-05-10 stsp free(start_id);
3737 d8f38dc4 2020-12-05 stsp free(label);
3738 d8f38dc4 2020-12-05 stsp if (ref)
3739 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3740 1d0f4054 2021-06-17 stsp if (repo) {
3741 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3742 1d0f4054 2021-06-17 stsp if (error == NULL)
3743 1d0f4054 2021-06-17 stsp error = close_err;
3744 1d0f4054 2021-06-17 stsp }
3745 ec142235 2019-03-07 stsp if (worktree)
3746 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3747 7cd52833 2022-06-23 thomas if (pack_fds) {
3748 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3749 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3750 7cd52833 2022-06-23 thomas if (error == NULL)
3751 7cd52833 2022-06-23 thomas error = pack_err;
3752 7cd52833 2022-06-23 thomas }
3753 51a10b52 2020-12-26 stsp tog_free_refs();
3754 80ddbec8 2018-04-29 stsp return error;
3755 9f7d7167 2018-04-29 stsp }
3756 9f7d7167 2018-04-29 stsp
3757 4ed7e80c 2018-05-20 stsp __dead static void
3758 9f7d7167 2018-04-29 stsp usage_diff(void)
3759 9f7d7167 2018-04-29 stsp {
3760 80ddbec8 2018-04-29 stsp endwin();
3761 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3762 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
3763 9f7d7167 2018-04-29 stsp exit(1);
3764 b304db33 2018-05-20 stsp }
3765 b304db33 2018-05-20 stsp
3766 6d17833f 2019-11-08 stsp static int
3767 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3768 41605754 2020-11-12 stsp regmatch_t *regmatch)
3769 6d17833f 2019-11-08 stsp {
3770 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3771 6d17833f 2019-11-08 stsp }
3772 6d17833f 2019-11-08 stsp
3773 ef20f542 2022-06-26 thomas static struct tog_color *
3774 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3775 6d17833f 2019-11-08 stsp {
3776 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3777 6d17833f 2019-11-08 stsp
3778 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3779 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3780 6d17833f 2019-11-08 stsp return tc;
3781 6d17833f 2019-11-08 stsp }
3782 6d17833f 2019-11-08 stsp
3783 6d17833f 2019-11-08 stsp return NULL;
3784 6d17833f 2019-11-08 stsp }
3785 6d17833f 2019-11-08 stsp
3786 4ed7e80c 2018-05-20 stsp static const struct got_error *
3787 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3788 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3789 41605754 2020-11-12 stsp {
3790 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3791 666f7b10 2022-06-23 thomas char *exstr = NULL;
3792 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3793 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3794 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3795 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3796 41605754 2020-11-12 stsp
3797 41605754 2020-11-12 stsp *wtotal = 0;
3798 cbea3800 2022-06-23 thomas
3799 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3800 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3801 41605754 2020-11-12 stsp
3802 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3803 666f7b10 2022-06-23 thomas if (err)
3804 666f7b10 2022-06-23 thomas return err;
3805 666f7b10 2022-06-23 thomas
3806 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3807 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3808 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3809 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3810 666f7b10 2022-06-23 thomas goto done;
3811 666f7b10 2022-06-23 thomas }
3812 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3813 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3814 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3815 cbea3800 2022-06-23 thomas goto done;
3816 cbea3800 2022-06-23 thomas }
3817 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3818 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3819 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3820 cbea3800 2022-06-23 thomas goto done;
3821 cbea3800 2022-06-23 thomas }
3822 05171be4 2022-06-23 thomas
3823 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3824 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3825 cbea3800 2022-06-23 thomas col_tab_align, 1);
3826 cbea3800 2022-06-23 thomas if (err)
3827 cbea3800 2022-06-23 thomas goto done;
3828 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3829 05171be4 2022-06-23 thomas if (n) {
3830 cbea3800 2022-06-23 thomas free(wline);
3831 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3832 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3833 cbea3800 2022-06-23 thomas if (err)
3834 cbea3800 2022-06-23 thomas goto done;
3835 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3836 cbea3800 2022-06-23 thomas wlimit -= width;
3837 cbea3800 2022-06-23 thomas *wtotal += width;
3838 41605754 2020-11-12 stsp }
3839 41605754 2020-11-12 stsp
3840 41605754 2020-11-12 stsp if (wlimit > 0) {
3841 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3842 cbea3800 2022-06-23 thomas size_t wlen;
3843 cbea3800 2022-06-23 thomas
3844 cbea3800 2022-06-23 thomas free(wline);
3845 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3846 cbea3800 2022-06-23 thomas col_tab_align, 1);
3847 cbea3800 2022-06-23 thomas if (err)
3848 cbea3800 2022-06-23 thomas goto done;
3849 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3850 cbea3800 2022-06-23 thomas while (i < wlen) {
3851 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3852 cbea3800 2022-06-23 thomas if (width == -1) {
3853 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3854 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3855 cbea3800 2022-06-23 thomas goto done;
3856 cbea3800 2022-06-23 thomas }
3857 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3858 cbea3800 2022-06-23 thomas break;
3859 1c5e5faa 2022-06-23 thomas w += width;
3860 cbea3800 2022-06-23 thomas i++;
3861 41605754 2020-11-12 stsp }
3862 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3863 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3864 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3865 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3866 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3867 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3868 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3869 41605754 2020-11-12 stsp }
3870 41605754 2020-11-12 stsp }
3871 41605754 2020-11-12 stsp
3872 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3873 cbea3800 2022-06-23 thomas free(wline);
3874 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3875 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3876 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3877 cbea3800 2022-06-23 thomas col_tab_align, 1);
3878 cbea3800 2022-06-23 thomas if (err)
3879 cbea3800 2022-06-23 thomas goto done;
3880 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3881 cbea3800 2022-06-23 thomas } else {
3882 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3883 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3884 cbea3800 2022-06-23 thomas if (err)
3885 cbea3800 2022-06-23 thomas goto done;
3886 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3887 cbea3800 2022-06-23 thomas }
3888 cbea3800 2022-06-23 thomas *wtotal += width2;
3889 41605754 2020-11-12 stsp }
3890 cbea3800 2022-06-23 thomas done:
3891 05171be4 2022-06-23 thomas free(wline);
3892 666f7b10 2022-06-23 thomas free(exstr);
3893 cbea3800 2022-06-23 thomas free(seg0);
3894 cbea3800 2022-06-23 thomas free(seg1);
3895 cbea3800 2022-06-23 thomas free(seg2);
3896 cbea3800 2022-06-23 thomas return err;
3897 41605754 2020-11-12 stsp }
3898 07dd3ed3 2022-08-06 thomas
3899 07dd3ed3 2022-08-06 thomas static int
3900 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
3901 07dd3ed3 2022-08-06 thomas {
3902 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
3903 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
3904 07dd3ed3 2022-08-06 thomas
3905 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
3906 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
3907 41605754 2020-11-12 stsp
3908 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3909 07dd3ed3 2022-08-06 thomas selected = first;
3910 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3911 07dd3ed3 2022-08-06 thomas f = s->f;
3912 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
3913 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
3914 07dd3ed3 2022-08-06 thomas
3915 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3916 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
3917 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3918 07dd3ed3 2022-08-06 thomas f = s->blame.f;
3919 07dd3ed3 2022-08-06 thomas } else
3920 07dd3ed3 2022-08-06 thomas return 0;
3921 07dd3ed3 2022-08-06 thomas
3922 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
3923 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
3924 07dd3ed3 2022-08-06 thomas return 0;
3925 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3926 07dd3ed3 2022-08-06 thomas rewind(f);
3927 07dd3ed3 2022-08-06 thomas *eof = 0;
3928 07dd3ed3 2022-08-06 thomas *first = 1;
3929 07dd3ed3 2022-08-06 thomas *lineno = 0;
3930 07dd3ed3 2022-08-06 thomas *nprinted = 0;
3931 07dd3ed3 2022-08-06 thomas return 0;
3932 07dd3ed3 2022-08-06 thomas }
3933 07dd3ed3 2022-08-06 thomas
3934 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
3935 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
3936 07dd3ed3 2022-08-06 thomas view->gline = 0;
3937 07dd3ed3 2022-08-06 thomas
3938 07dd3ed3 2022-08-06 thomas return 1;
3939 07dd3ed3 2022-08-06 thomas }
3940 07dd3ed3 2022-08-06 thomas
3941 41605754 2020-11-12 stsp static const struct got_error *
3942 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3943 26ed57b2 2018-05-19 stsp {
3944 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3945 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3946 61e69b96 2018-05-20 stsp const struct got_error *err;
3947 82c78e96 2022-08-06 thomas int nprinted = 0;
3948 b304db33 2018-05-20 stsp char *line;
3949 826082fe 2020-12-10 stsp size_t linesize = 0;
3950 826082fe 2020-12-10 stsp ssize_t linelen;
3951 61e69b96 2018-05-20 stsp wchar_t *wline;
3952 e0b650dd 2018-05-20 stsp int width;
3953 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3954 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3955 fe621944 2020-11-10 stsp off_t line_offset;
3956 26ed57b2 2018-05-19 stsp
3957 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
3958 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
3959 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3960 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3961 fe621944 2020-11-10 stsp
3962 f7d12f7e 2018-08-01 stsp werase(view->window);
3963 a3404814 2018-09-02 stsp
3964 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
3965 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
3966 07dd3ed3 2022-08-06 thomas
3967 a3404814 2018-09-02 stsp if (header) {
3968 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3969 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
3970 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
3971 07dd3ed3 2022-08-06 thomas
3972 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3973 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3974 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3975 f91a2b48 2022-06-23 thomas 0, 0);
3976 135a2da0 2020-11-11 stsp free(line);
3977 135a2da0 2020-11-11 stsp if (err)
3978 a3404814 2018-09-02 stsp return err;
3979 a3404814 2018-09-02 stsp
3980 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3981 a3404814 2018-09-02 stsp wstandout(view->window);
3982 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3983 e54cc94a 2020-11-11 stsp free(wline);
3984 e54cc94a 2020-11-11 stsp wline = NULL;
3985 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3986 a3404814 2018-09-02 stsp wstandend(view->window);
3987 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3988 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3989 26ed57b2 2018-05-19 stsp
3990 a3404814 2018-09-02 stsp if (max_lines <= 1)
3991 a3404814 2018-09-02 stsp return NULL;
3992 a3404814 2018-09-02 stsp max_lines--;
3993 a3404814 2018-09-02 stsp }
3994 a3404814 2018-09-02 stsp
3995 89f1a395 2020-12-01 naddy s->eof = 0;
3996 05171be4 2022-06-23 thomas view->maxx = 0;
3997 826082fe 2020-12-10 stsp line = NULL;
3998 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3999 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4000 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4001 6568f0aa 2022-08-06 thomas
4002 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4003 826082fe 2020-12-10 stsp if (linelen == -1) {
4004 826082fe 2020-12-10 stsp if (feof(s->f)) {
4005 826082fe 2020-12-10 stsp s->eof = 1;
4006 826082fe 2020-12-10 stsp break;
4007 826082fe 2020-12-10 stsp }
4008 826082fe 2020-12-10 stsp free(line);
4009 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4010 61e69b96 2018-05-20 stsp }
4011 05171be4 2022-06-23 thomas
4012 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4013 07dd3ed3 2022-08-06 thomas continue;
4014 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4015 07dd3ed3 2022-08-06 thomas continue;
4016 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4017 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4018 07dd3ed3 2022-08-06 thomas
4019 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4020 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4021 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4022 cbea3800 2022-06-23 thomas if (err) {
4023 cbea3800 2022-06-23 thomas free(line);
4024 cbea3800 2022-06-23 thomas return err;
4025 cbea3800 2022-06-23 thomas }
4026 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4027 cbea3800 2022-06-23 thomas free(wline);
4028 cbea3800 2022-06-23 thomas wline = NULL;
4029 cbea3800 2022-06-23 thomas
4030 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4031 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4032 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4033 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4034 07dd3ed3 2022-08-06 thomas if (attr)
4035 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4036 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4037 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4038 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4039 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4040 41605754 2020-11-12 stsp if (err) {
4041 41605754 2020-11-12 stsp free(line);
4042 41605754 2020-11-12 stsp return err;
4043 41605754 2020-11-12 stsp }
4044 41605754 2020-11-12 stsp } else {
4045 f1c0ec19 2022-06-23 thomas int skip;
4046 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4047 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4048 f1c0ec19 2022-06-23 thomas if (err) {
4049 f1c0ec19 2022-06-23 thomas free(line);
4050 f1c0ec19 2022-06-23 thomas return err;
4051 f1c0ec19 2022-06-23 thomas }
4052 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4053 f1c0ec19 2022-06-23 thomas free(wline);
4054 41605754 2020-11-12 stsp wline = NULL;
4055 41605754 2020-11-12 stsp }
4056 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4057 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4058 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4059 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4060 07dd3ed3 2022-08-06 thomas } else {
4061 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4062 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4063 07dd3ed3 2022-08-06 thomas }
4064 07dd3ed3 2022-08-06 thomas if (attr)
4065 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4066 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4067 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4068 826082fe 2020-12-10 stsp }
4069 826082fe 2020-12-10 stsp free(line);
4070 fe621944 2020-11-10 stsp if (nprinted >= 1)
4071 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4072 89f1a395 2020-12-01 naddy (nprinted - 1);
4073 fe621944 2020-11-10 stsp else
4074 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4075 26ed57b2 2018-05-19 stsp
4076 a5d43cac 2022-07-01 thomas view_border(view);
4077 c3e9aa98 2019-05-13 jcs
4078 89f1a395 2020-12-01 naddy if (s->eof) {
4079 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4080 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4081 c3e9aa98 2019-05-13 jcs nprinted++;
4082 c3e9aa98 2019-05-13 jcs }
4083 c3e9aa98 2019-05-13 jcs
4084 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4085 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4086 c3e9aa98 2019-05-13 jcs if (err) {
4087 c3e9aa98 2019-05-13 jcs return err;
4088 c3e9aa98 2019-05-13 jcs }
4089 26ed57b2 2018-05-19 stsp
4090 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4091 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4092 e54cc94a 2020-11-11 stsp free(wline);
4093 e54cc94a 2020-11-11 stsp wline = NULL;
4094 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4095 c3e9aa98 2019-05-13 jcs }
4096 c3e9aa98 2019-05-13 jcs
4097 26ed57b2 2018-05-19 stsp return NULL;
4098 abd2672a 2018-12-23 stsp }
4099 abd2672a 2018-12-23 stsp
4100 abd2672a 2018-12-23 stsp static char *
4101 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4102 abd2672a 2018-12-23 stsp {
4103 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4104 09867e48 2019-08-13 stsp char *p, *s;
4105 09867e48 2019-08-13 stsp
4106 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4107 09867e48 2019-08-13 stsp if (tm == NULL)
4108 09867e48 2019-08-13 stsp return NULL;
4109 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4110 09867e48 2019-08-13 stsp if (s == NULL)
4111 09867e48 2019-08-13 stsp return NULL;
4112 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4113 abd2672a 2018-12-23 stsp if (p)
4114 abd2672a 2018-12-23 stsp *p = '\0';
4115 abd2672a 2018-12-23 stsp return s;
4116 9f7d7167 2018-04-29 stsp }
4117 9f7d7167 2018-04-29 stsp
4118 4ed7e80c 2018-05-20 stsp static const struct got_error *
4119 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4120 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4121 0208f208 2020-05-05 stsp {
4122 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4123 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4124 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4125 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4126 0208f208 2020-05-05 stsp
4127 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4128 0208f208 2020-05-05 stsp if (qid != NULL) {
4129 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4130 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4131 ec242592 2022-04-22 thomas &qid->id);
4132 0208f208 2020-05-05 stsp if (err)
4133 0208f208 2020-05-05 stsp return err;
4134 0208f208 2020-05-05 stsp
4135 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4136 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4137 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4138 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4139 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4140 aa8b5dd0 2021-08-01 stsp }
4141 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4142 0208f208 2020-05-05 stsp
4143 0208f208 2020-05-05 stsp }
4144 0208f208 2020-05-05 stsp
4145 0208f208 2020-05-05 stsp if (tree_id1) {
4146 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4147 0208f208 2020-05-05 stsp if (err)
4148 0208f208 2020-05-05 stsp goto done;
4149 0208f208 2020-05-05 stsp }
4150 0208f208 2020-05-05 stsp
4151 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4152 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4153 0208f208 2020-05-05 stsp if (err)
4154 0208f208 2020-05-05 stsp goto done;
4155 0208f208 2020-05-05 stsp
4156 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4157 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4158 0208f208 2020-05-05 stsp done:
4159 0208f208 2020-05-05 stsp if (tree1)
4160 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4161 0208f208 2020-05-05 stsp if (tree2)
4162 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4163 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4164 0208f208 2020-05-05 stsp return err;
4165 0208f208 2020-05-05 stsp }
4166 0208f208 2020-05-05 stsp
4167 0208f208 2020-05-05 stsp static const struct got_error *
4168 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4169 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4170 abd2672a 2018-12-23 stsp {
4171 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4172 fe621944 2020-11-10 stsp
4173 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4174 fe621944 2020-11-10 stsp if (p == NULL)
4175 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4176 82c78e96 2022-08-06 thomas *lines = p;
4177 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4178 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4179 fe621944 2020-11-10 stsp (*nlines)++;
4180 82c78e96 2022-08-06 thomas
4181 fe621944 2020-11-10 stsp return NULL;
4182 fe621944 2020-11-10 stsp }
4183 fe621944 2020-11-10 stsp
4184 fe621944 2020-11-10 stsp static const struct got_error *
4185 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4186 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4187 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4188 fe621944 2020-11-10 stsp {
4189 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4190 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4191 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4192 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4193 45d799e2 2018-12-23 stsp time_t committer_time;
4194 45d799e2 2018-12-23 stsp const char *author, *committer;
4195 8b473291 2019-02-21 stsp char *refs_str = NULL;
4196 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4197 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4198 fe621944 2020-11-10 stsp off_t outoff = 0;
4199 fe621944 2020-11-10 stsp int n;
4200 abd2672a 2018-12-23 stsp
4201 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4202 0208f208 2020-05-05 stsp
4203 8b473291 2019-02-21 stsp if (refs) {
4204 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4205 8b473291 2019-02-21 stsp if (err)
4206 8b473291 2019-02-21 stsp return err;
4207 8b473291 2019-02-21 stsp }
4208 8b473291 2019-02-21 stsp
4209 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4210 abd2672a 2018-12-23 stsp if (err)
4211 abd2672a 2018-12-23 stsp return err;
4212 abd2672a 2018-12-23 stsp
4213 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4214 15a94983 2018-12-23 stsp if (err) {
4215 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4216 15a94983 2018-12-23 stsp goto done;
4217 15a94983 2018-12-23 stsp }
4218 abd2672a 2018-12-23 stsp
4219 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4220 fe621944 2020-11-10 stsp if (err)
4221 fe621944 2020-11-10 stsp goto done;
4222 fe621944 2020-11-10 stsp
4223 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4224 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4225 fe621944 2020-11-10 stsp if (n < 0) {
4226 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4227 abd2672a 2018-12-23 stsp goto done;
4228 abd2672a 2018-12-23 stsp }
4229 fe621944 2020-11-10 stsp outoff += n;
4230 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4231 fe621944 2020-11-10 stsp if (err)
4232 fe621944 2020-11-10 stsp goto done;
4233 fe621944 2020-11-10 stsp
4234 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4235 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4236 fe621944 2020-11-10 stsp if (n < 0) {
4237 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4238 abd2672a 2018-12-23 stsp goto done;
4239 abd2672a 2018-12-23 stsp }
4240 fe621944 2020-11-10 stsp outoff += n;
4241 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4242 fe621944 2020-11-10 stsp if (err)
4243 fe621944 2020-11-10 stsp goto done;
4244 fe621944 2020-11-10 stsp
4245 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4246 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4247 fe621944 2020-11-10 stsp if (datestr) {
4248 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4249 fe621944 2020-11-10 stsp if (n < 0) {
4250 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4251 fe621944 2020-11-10 stsp goto done;
4252 fe621944 2020-11-10 stsp }
4253 fe621944 2020-11-10 stsp outoff += n;
4254 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4255 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4256 fe621944 2020-11-10 stsp if (err)
4257 fe621944 2020-11-10 stsp goto done;
4258 abd2672a 2018-12-23 stsp }
4259 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4260 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4261 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4262 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4263 fe621944 2020-11-10 stsp if (n < 0) {
4264 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4265 fe621944 2020-11-10 stsp goto done;
4266 fe621944 2020-11-10 stsp }
4267 fe621944 2020-11-10 stsp outoff += n;
4268 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4269 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_AUTHOR);
4270 fe621944 2020-11-10 stsp if (err)
4271 fe621944 2020-11-10 stsp goto done;
4272 abd2672a 2018-12-23 stsp }
4273 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4274 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4275 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4276 ac4dc263 2021-09-24 thomas int pn = 1;
4277 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4278 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4279 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4280 ac4dc263 2021-09-24 thomas if (err)
4281 ac4dc263 2021-09-24 thomas goto done;
4282 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4283 ac4dc263 2021-09-24 thomas if (n < 0) {
4284 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4285 ac4dc263 2021-09-24 thomas goto done;
4286 ac4dc263 2021-09-24 thomas }
4287 ac4dc263 2021-09-24 thomas outoff += n;
4288 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4289 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4290 ac4dc263 2021-09-24 thomas if (err)
4291 ac4dc263 2021-09-24 thomas goto done;
4292 ac4dc263 2021-09-24 thomas free(id_str);
4293 ac4dc263 2021-09-24 thomas id_str = NULL;
4294 ac4dc263 2021-09-24 thomas }
4295 ac4dc263 2021-09-24 thomas }
4296 ac4dc263 2021-09-24 thomas
4297 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4298 5943eee2 2019-08-13 stsp if (err)
4299 5943eee2 2019-08-13 stsp goto done;
4300 fe621944 2020-11-10 stsp s = logmsg;
4301 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4302 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4303 fe621944 2020-11-10 stsp if (n < 0) {
4304 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4305 fe621944 2020-11-10 stsp goto done;
4306 fe621944 2020-11-10 stsp }
4307 fe621944 2020-11-10 stsp outoff += n;
4308 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4309 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4310 fe621944 2020-11-10 stsp if (err)
4311 fe621944 2020-11-10 stsp goto done;
4312 abd2672a 2018-12-23 stsp }
4313 fe621944 2020-11-10 stsp
4314 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4315 0208f208 2020-05-05 stsp if (err)
4316 0208f208 2020-05-05 stsp goto done;
4317 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4318 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4319 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4320 fe621944 2020-11-10 stsp if (n < 0) {
4321 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4322 fe621944 2020-11-10 stsp goto done;
4323 fe621944 2020-11-10 stsp }
4324 fe621944 2020-11-10 stsp outoff += n;
4325 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4326 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4327 fe621944 2020-11-10 stsp if (err)
4328 fe621944 2020-11-10 stsp goto done;
4329 0208f208 2020-05-05 stsp free((char *)pe->path);
4330 0208f208 2020-05-05 stsp free(pe->data);
4331 0208f208 2020-05-05 stsp }
4332 fe621944 2020-11-10 stsp
4333 0208f208 2020-05-05 stsp fputc('\n', outfile);
4334 fe621944 2020-11-10 stsp outoff++;
4335 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4336 abd2672a 2018-12-23 stsp done:
4337 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4338 abd2672a 2018-12-23 stsp free(id_str);
4339 5943eee2 2019-08-13 stsp free(logmsg);
4340 8b473291 2019-02-21 stsp free(refs_str);
4341 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4342 7510f233 2020-08-09 stsp if (err) {
4343 82c78e96 2022-08-06 thomas free(*lines);
4344 82c78e96 2022-08-06 thomas *lines = NULL;
4345 ae6a6978 2020-08-09 stsp *nlines = 0;
4346 7510f233 2020-08-09 stsp }
4347 fe621944 2020-11-10 stsp return err;
4348 abd2672a 2018-12-23 stsp }
4349 abd2672a 2018-12-23 stsp
4350 abd2672a 2018-12-23 stsp static const struct got_error *
4351 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4352 26ed57b2 2018-05-19 stsp {
4353 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4354 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4355 15a94983 2018-12-23 stsp int obj_type;
4356 fe621944 2020-11-10 stsp
4357 82c78e96 2022-08-06 thomas free(s->lines);
4358 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4359 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4360 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4361 fe621944 2020-11-10 stsp s->nlines = 0;
4362 26ed57b2 2018-05-19 stsp
4363 511a516b 2018-05-19 stsp f = got_opentemp();
4364 48ae06ee 2018-10-18 stsp if (f == NULL) {
4365 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4366 48ae06ee 2018-10-18 stsp goto done;
4367 48ae06ee 2018-10-18 stsp }
4368 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4369 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4370 fb43ecf1 2019-02-11 stsp goto done;
4371 fb43ecf1 2019-02-11 stsp }
4372 48ae06ee 2018-10-18 stsp s->f = f;
4373 26ed57b2 2018-05-19 stsp
4374 15a94983 2018-12-23 stsp if (s->id1)
4375 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4376 15a94983 2018-12-23 stsp else
4377 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4378 15a94983 2018-12-23 stsp if (err)
4379 15a94983 2018-12-23 stsp goto done;
4380 15a94983 2018-12-23 stsp
4381 15a94983 2018-12-23 stsp switch (obj_type) {
4382 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4383 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4384 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4385 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4386 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4387 26ed57b2 2018-05-19 stsp break;
4388 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4389 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4390 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4391 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4392 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4393 26ed57b2 2018-05-19 stsp break;
4394 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4395 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4396 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4397 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4398 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4399 abd2672a 2018-12-23 stsp
4400 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4401 abd2672a 2018-12-23 stsp if (err)
4402 3ffacbe1 2020-02-02 tracey goto done;
4403 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4404 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4405 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4406 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4407 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4408 f44b1f58 2020-02-02 tracey if (err)
4409 f44b1f58 2020-02-02 tracey goto done;
4410 f44b1f58 2020-02-02 tracey } else {
4411 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4412 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4413 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4414 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4415 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4416 82c78e96 2022-08-06 thomas s->f);
4417 f44b1f58 2020-02-02 tracey if (err)
4418 f44b1f58 2020-02-02 tracey goto done;
4419 f5404e4e 2020-02-02 tracey break;
4420 15a087fe 2019-02-21 stsp }
4421 abd2672a 2018-12-23 stsp }
4422 abd2672a 2018-12-23 stsp }
4423 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4424 abd2672a 2018-12-23 stsp
4425 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4426 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4427 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4428 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4429 26ed57b2 2018-05-19 stsp break;
4430 abd2672a 2018-12-23 stsp }
4431 26ed57b2 2018-05-19 stsp default:
4432 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4433 48ae06ee 2018-10-18 stsp break;
4434 26ed57b2 2018-05-19 stsp }
4435 48ae06ee 2018-10-18 stsp done:
4436 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4437 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4438 48ae06ee 2018-10-18 stsp return err;
4439 48ae06ee 2018-10-18 stsp }
4440 26ed57b2 2018-05-19 stsp
4441 f5215bb9 2019-02-22 stsp static void
4442 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4443 f5215bb9 2019-02-22 stsp {
4444 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4445 f5215bb9 2019-02-22 stsp update_panels();
4446 f5215bb9 2019-02-22 stsp doupdate();
4447 f44b1f58 2020-02-02 tracey }
4448 f44b1f58 2020-02-02 tracey
4449 f44b1f58 2020-02-02 tracey static const struct got_error *
4450 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4451 f44b1f58 2020-02-02 tracey {
4452 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4453 f44b1f58 2020-02-02 tracey
4454 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4455 f44b1f58 2020-02-02 tracey return NULL;
4456 f44b1f58 2020-02-02 tracey }
4457 f44b1f58 2020-02-02 tracey
4458 f44b1f58 2020-02-02 tracey static const struct got_error *
4459 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4460 f44b1f58 2020-02-02 tracey {
4461 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4462 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4463 f44b1f58 2020-02-02 tracey int lineno;
4464 78eecffc 2022-06-23 thomas char *line = NULL;
4465 826082fe 2020-12-10 stsp size_t linesize = 0;
4466 826082fe 2020-12-10 stsp ssize_t linelen;
4467 f44b1f58 2020-02-02 tracey
4468 f44b1f58 2020-02-02 tracey if (!view->searching) {
4469 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4470 f44b1f58 2020-02-02 tracey return NULL;
4471 f44b1f58 2020-02-02 tracey }
4472 f44b1f58 2020-02-02 tracey
4473 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4474 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4475 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4476 f44b1f58 2020-02-02 tracey else
4477 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4478 4b3f9dac 2021-12-17 thomas } else
4479 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4480 f44b1f58 2020-02-02 tracey
4481 f44b1f58 2020-02-02 tracey while (1) {
4482 f44b1f58 2020-02-02 tracey off_t offset;
4483 f44b1f58 2020-02-02 tracey
4484 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4485 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4486 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4487 f44b1f58 2020-02-02 tracey break;
4488 f44b1f58 2020-02-02 tracey }
4489 f44b1f58 2020-02-02 tracey
4490 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4491 f44b1f58 2020-02-02 tracey lineno = 1;
4492 f44b1f58 2020-02-02 tracey else
4493 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4494 f44b1f58 2020-02-02 tracey }
4495 f44b1f58 2020-02-02 tracey
4496 82c78e96 2022-08-06 thomas offset = s->lines[lineno - 1].offset;
4497 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4498 f44b1f58 2020-02-02 tracey free(line);
4499 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4500 f44b1f58 2020-02-02 tracey }
4501 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4502 78eecffc 2022-06-23 thomas if (linelen != -1) {
4503 78eecffc 2022-06-23 thomas char *exstr;
4504 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4505 78eecffc 2022-06-23 thomas if (err)
4506 78eecffc 2022-06-23 thomas break;
4507 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4508 78eecffc 2022-06-23 thomas &view->regmatch)) {
4509 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4510 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4511 78eecffc 2022-06-23 thomas free(exstr);
4512 78eecffc 2022-06-23 thomas break;
4513 78eecffc 2022-06-23 thomas }
4514 78eecffc 2022-06-23 thomas free(exstr);
4515 f44b1f58 2020-02-02 tracey }
4516 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4517 f44b1f58 2020-02-02 tracey lineno++;
4518 f44b1f58 2020-02-02 tracey else
4519 f44b1f58 2020-02-02 tracey lineno--;
4520 f44b1f58 2020-02-02 tracey }
4521 826082fe 2020-12-10 stsp free(line);
4522 f44b1f58 2020-02-02 tracey
4523 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4524 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4525 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4526 f44b1f58 2020-02-02 tracey }
4527 f44b1f58 2020-02-02 tracey
4528 05171be4 2022-06-23 thomas return err;
4529 f5215bb9 2019-02-22 stsp }
4530 f5215bb9 2019-02-22 stsp
4531 48ae06ee 2018-10-18 stsp static const struct got_error *
4532 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4533 a0f32f33 2022-06-13 thomas {
4534 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4535 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4536 a0f32f33 2022-06-13 thomas
4537 a0f32f33 2022-06-13 thomas free(s->id1);
4538 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4539 a0f32f33 2022-06-13 thomas free(s->id2);
4540 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4541 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4542 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4543 a0f32f33 2022-06-13 thomas s->f = NULL;
4544 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4545 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4546 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4547 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4548 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4549 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4550 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4551 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4552 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4553 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4554 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4555 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4556 82c78e96 2022-08-06 thomas free(s->lines);
4557 82c78e96 2022-08-06 thomas s->lines = NULL;
4558 a0f32f33 2022-06-13 thomas s->nlines = 0;
4559 a0f32f33 2022-06-13 thomas return err;
4560 a0f32f33 2022-06-13 thomas }
4561 a0f32f33 2022-06-13 thomas
4562 a0f32f33 2022-06-13 thomas static const struct got_error *
4563 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4564 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4565 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4566 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4567 48ae06ee 2018-10-18 stsp {
4568 48ae06ee 2018-10-18 stsp const struct got_error *err;
4569 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4570 5dc9f4bc 2018-08-04 stsp
4571 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4572 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4573 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4574 a0f32f33 2022-06-13 thomas
4575 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4576 15a94983 2018-12-23 stsp int type1, type2;
4577 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4578 15a94983 2018-12-23 stsp if (err)
4579 15a94983 2018-12-23 stsp return err;
4580 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4581 15a94983 2018-12-23 stsp if (err)
4582 15a94983 2018-12-23 stsp return err;
4583 15a94983 2018-12-23 stsp
4584 15a94983 2018-12-23 stsp if (type1 != type2)
4585 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4586 15a94983 2018-12-23 stsp }
4587 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4588 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4589 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4590 f44b1f58 2020-02-02 tracey s->repo = repo;
4591 f44b1f58 2020-02-02 tracey s->id1 = id1;
4592 f44b1f58 2020-02-02 tracey s->id2 = id2;
4593 3dbaef42 2020-11-24 stsp s->label1 = label1;
4594 3dbaef42 2020-11-24 stsp s->label2 = label2;
4595 48ae06ee 2018-10-18 stsp
4596 15a94983 2018-12-23 stsp if (id1) {
4597 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4598 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4599 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4600 48ae06ee 2018-10-18 stsp } else
4601 5465d566 2020-02-01 tracey s->id1 = NULL;
4602 48ae06ee 2018-10-18 stsp
4603 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4604 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4605 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4606 a0f32f33 2022-06-13 thomas goto done;
4607 48ae06ee 2018-10-18 stsp }
4608 a0f32f33 2022-06-13 thomas
4609 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4610 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4611 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4612 9a1d7435 2022-06-23 thomas goto done;
4613 9a1d7435 2022-06-23 thomas }
4614 9a1d7435 2022-06-23 thomas
4615 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4616 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4617 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4618 a0f32f33 2022-06-13 thomas goto done;
4619 a0f32f33 2022-06-13 thomas }
4620 a0f32f33 2022-06-13 thomas
4621 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4622 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4623 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4624 19a6a6b5 2022-07-01 thomas goto done;
4625 19a6a6b5 2022-07-01 thomas }
4626 19a6a6b5 2022-07-01 thomas
4627 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4628 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4629 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4630 19a6a6b5 2022-07-01 thomas goto done;
4631 19a6a6b5 2022-07-01 thomas }
4632 19a6a6b5 2022-07-01 thomas
4633 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4634 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4635 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4636 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4637 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4638 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4639 5465d566 2020-02-01 tracey s->repo = repo;
4640 6d17833f 2019-11-08 stsp
4641 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4642 6568f0aa 2022-08-06 thomas int rc;
4643 6d17833f 2019-11-08 stsp
4644 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
4645 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4646 6568f0aa 2022-08-06 thomas if (rc != ERR)
4647 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
4648 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4649 6568f0aa 2022-08-06 thomas if (rc != ERR)
4650 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
4651 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4652 6568f0aa 2022-08-06 thomas if (rc != ERR)
4653 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
4654 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4655 6568f0aa 2022-08-06 thomas if (rc != ERR)
4656 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
4657 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4658 6568f0aa 2022-08-06 thomas if (rc != ERR)
4659 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4660 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4661 6568f0aa 2022-08-06 thomas if (rc != ERR)
4662 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4663 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4664 6568f0aa 2022-08-06 thomas if (rc != ERR)
4665 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4666 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
4667 6568f0aa 2022-08-06 thomas if (rc != ERR)
4668 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
4669 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
4670 6568f0aa 2022-08-06 thomas if (rc == ERR) {
4671 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
4672 a0f32f33 2022-06-13 thomas goto done;
4673 6568f0aa 2022-08-06 thomas }
4674 6d17833f 2019-11-08 stsp }
4675 5dc9f4bc 2018-08-04 stsp
4676 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4677 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4678 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4679 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4680 f5215bb9 2019-02-22 stsp
4681 5465d566 2020-02-01 tracey err = create_diff(s);
4682 48ae06ee 2018-10-18 stsp
4683 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4684 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4685 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4686 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4687 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4688 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4689 a0f32f33 2022-06-13 thomas done:
4690 a0f32f33 2022-06-13 thomas if (err)
4691 a0f32f33 2022-06-13 thomas close_diff_view(view);
4692 e5a0f69f 2018-08-18 stsp return err;
4693 5dc9f4bc 2018-08-04 stsp }
4694 5dc9f4bc 2018-08-04 stsp
4695 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4696 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4697 5dc9f4bc 2018-08-04 stsp {
4698 a3404814 2018-09-02 stsp const struct got_error *err;
4699 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4700 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4701 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4702 a3404814 2018-09-02 stsp
4703 a3404814 2018-09-02 stsp if (s->id1) {
4704 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4705 a3404814 2018-09-02 stsp if (err)
4706 a3404814 2018-09-02 stsp return err;
4707 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
4708 3dbaef42 2020-11-24 stsp } else
4709 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4710 3dbaef42 2020-11-24 stsp
4711 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4712 a3404814 2018-09-02 stsp if (err)
4713 a3404814 2018-09-02 stsp return err;
4714 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
4715 26ed57b2 2018-05-19 stsp
4716 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4717 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4718 a3404814 2018-09-02 stsp free(id_str1);
4719 a3404814 2018-09-02 stsp free(id_str2);
4720 a3404814 2018-09-02 stsp return err;
4721 a3404814 2018-09-02 stsp }
4722 a3404814 2018-09-02 stsp free(id_str1);
4723 a3404814 2018-09-02 stsp free(id_str2);
4724 a3404814 2018-09-02 stsp
4725 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4726 267bb3b8 2021-08-01 stsp free(header);
4727 267bb3b8 2021-08-01 stsp return err;
4728 15a087fe 2019-02-21 stsp }
4729 15a087fe 2019-02-21 stsp
4730 15a087fe 2019-02-21 stsp static const struct got_error *
4731 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4732 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4733 15a087fe 2019-02-21 stsp {
4734 d7a04538 2019-02-21 stsp const struct got_error *err;
4735 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4736 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4737 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4738 15a087fe 2019-02-21 stsp
4739 15a087fe 2019-02-21 stsp free(s->id2);
4740 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4741 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4742 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4743 15a087fe 2019-02-21 stsp
4744 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4745 d7a04538 2019-02-21 stsp if (err)
4746 d7a04538 2019-02-21 stsp return err;
4747 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4748 15a087fe 2019-02-21 stsp free(s->id1);
4749 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4750 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4751 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4752 15a087fe 2019-02-21 stsp return NULL;
4753 0cf4efb1 2018-09-29 stsp }
4754 0cf4efb1 2018-09-29 stsp
4755 0cf4efb1 2018-09-29 stsp static const struct got_error *
4756 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4757 adf4c9e0 2022-07-03 thomas {
4758 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4759 adf4c9e0 2022-07-03 thomas
4760 adf4c9e0 2022-07-03 thomas view->count = 0;
4761 adf4c9e0 2022-07-03 thomas wclear(view->window);
4762 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4763 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4764 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4765 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4766 adf4c9e0 2022-07-03 thomas return create_diff(s);
4767 82c78e96 2022-08-06 thomas }
4768 82c78e96 2022-08-06 thomas
4769 82c78e96 2022-08-06 thomas static void
4770 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4771 82c78e96 2022-08-06 thomas {
4772 82c78e96 2022-08-06 thomas int start, i;
4773 82c78e96 2022-08-06 thomas
4774 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
4775 82c78e96 2022-08-06 thomas
4776 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4777 82c78e96 2022-08-06 thomas if (i == 0)
4778 82c78e96 2022-08-06 thomas i = s->nlines - 1;
4779 82c78e96 2022-08-06 thomas if (--i == start)
4780 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4781 82c78e96 2022-08-06 thomas }
4782 82c78e96 2022-08-06 thomas
4783 82c78e96 2022-08-06 thomas s->selected_line = 1;
4784 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4785 adf4c9e0 2022-07-03 thomas }
4786 adf4c9e0 2022-07-03 thomas
4787 82c78e96 2022-08-06 thomas static void
4788 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4789 82c78e96 2022-08-06 thomas {
4790 82c78e96 2022-08-06 thomas int start, i;
4791 82c78e96 2022-08-06 thomas
4792 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
4793 82c78e96 2022-08-06 thomas
4794 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4795 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
4796 82c78e96 2022-08-06 thomas i = 0;
4797 82c78e96 2022-08-06 thomas if (++i == start)
4798 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4799 82c78e96 2022-08-06 thomas }
4800 82c78e96 2022-08-06 thomas
4801 82c78e96 2022-08-06 thomas s->selected_line = 1;
4802 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4803 82c78e96 2022-08-06 thomas }
4804 82c78e96 2022-08-06 thomas
4805 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4806 4fc71f3b 2022-07-12 thomas int, int, int);
4807 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4808 4fc71f3b 2022-07-12 thomas int, int);
4809 4fc71f3b 2022-07-12 thomas
4810 adf4c9e0 2022-07-03 thomas static const struct got_error *
4811 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4812 e5a0f69f 2018-08-18 stsp {
4813 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4814 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4815 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4816 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4817 826082fe 2020-12-10 stsp char *line = NULL;
4818 826082fe 2020-12-10 stsp size_t linesize = 0;
4819 826082fe 2020-12-10 stsp ssize_t linelen;
4820 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4821 e5a0f69f 2018-08-18 stsp
4822 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
4823 82c78e96 2022-08-06 thomas
4824 e5a0f69f 2018-08-18 stsp switch (ch) {
4825 05171be4 2022-06-23 thomas case '0':
4826 05171be4 2022-06-23 thomas view->x = 0;
4827 05171be4 2022-06-23 thomas break;
4828 05171be4 2022-06-23 thomas case '$':
4829 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4830 07b0611c 2022-06-23 thomas view->count = 0;
4831 05171be4 2022-06-23 thomas break;
4832 05171be4 2022-06-23 thomas case KEY_RIGHT:
4833 05171be4 2022-06-23 thomas case 'l':
4834 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4835 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4836 07b0611c 2022-06-23 thomas else
4837 07b0611c 2022-06-23 thomas view->count = 0;
4838 05171be4 2022-06-23 thomas break;
4839 05171be4 2022-06-23 thomas case KEY_LEFT:
4840 05171be4 2022-06-23 thomas case 'h':
4841 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4842 07b0611c 2022-06-23 thomas if (view->x <= 0)
4843 07b0611c 2022-06-23 thomas view->count = 0;
4844 05171be4 2022-06-23 thomas break;
4845 64453f7e 2020-11-21 stsp case 'a':
4846 3dbaef42 2020-11-24 stsp case 'w':
4847 3dbaef42 2020-11-24 stsp if (ch == 'a')
4848 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4849 3dbaef42 2020-11-24 stsp if (ch == 'w')
4850 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4851 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4852 912a3f79 2021-08-30 j break;
4853 912a3f79 2021-08-30 j case 'g':
4854 912a3f79 2021-08-30 j case KEY_HOME:
4855 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4856 07b0611c 2022-06-23 thomas view->count = 0;
4857 64453f7e 2020-11-21 stsp break;
4858 912a3f79 2021-08-30 j case 'G':
4859 912a3f79 2021-08-30 j case KEY_END:
4860 07b0611c 2022-06-23 thomas view->count = 0;
4861 912a3f79 2021-08-30 j if (s->eof)
4862 912a3f79 2021-08-30 j break;
4863 912a3f79 2021-08-30 j
4864 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4865 912a3f79 2021-08-30 j s->eof = 1;
4866 912a3f79 2021-08-30 j break;
4867 1e37a5c2 2019-05-12 jcs case 'k':
4868 1e37a5c2 2019-05-12 jcs case KEY_UP:
4869 f7140bf5 2021-10-17 thomas case CTRL('p'):
4870 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4871 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4872 07b0611c 2022-06-23 thomas else
4873 07b0611c 2022-06-23 thomas view->count = 0;
4874 1e37a5c2 2019-05-12 jcs break;
4875 70f17a53 2022-06-13 thomas case CTRL('u'):
4876 23427b14 2022-06-23 thomas case 'u':
4877 70f17a53 2022-06-13 thomas nscroll /= 2;
4878 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4879 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4880 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4881 1c5e5faa 2022-06-23 thomas case 'b':
4882 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4883 07b0611c 2022-06-23 thomas view->count = 0;
4884 26ed57b2 2018-05-19 stsp break;
4885 07b0611c 2022-06-23 thomas }
4886 1e37a5c2 2019-05-12 jcs i = 0;
4887 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4888 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4889 1e37a5c2 2019-05-12 jcs break;
4890 1e37a5c2 2019-05-12 jcs case 'j':
4891 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4892 f7140bf5 2021-10-17 thomas case CTRL('n'):
4893 1e37a5c2 2019-05-12 jcs if (!s->eof)
4894 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4895 07b0611c 2022-06-23 thomas else
4896 07b0611c 2022-06-23 thomas view->count = 0;
4897 1e37a5c2 2019-05-12 jcs break;
4898 70f17a53 2022-06-13 thomas case CTRL('d'):
4899 23427b14 2022-06-23 thomas case 'd':
4900 70f17a53 2022-06-13 thomas nscroll /= 2;
4901 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4902 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4903 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4904 1c5e5faa 2022-06-23 thomas case 'f':
4905 1e37a5c2 2019-05-12 jcs case ' ':
4906 07b0611c 2022-06-23 thomas if (s->eof) {
4907 07b0611c 2022-06-23 thomas view->count = 0;
4908 1e37a5c2 2019-05-12 jcs break;
4909 07b0611c 2022-06-23 thomas }
4910 1e37a5c2 2019-05-12 jcs i = 0;
4911 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4912 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4913 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4914 826082fe 2020-12-10 stsp if (linelen == -1) {
4915 826082fe 2020-12-10 stsp if (feof(s->f)) {
4916 826082fe 2020-12-10 stsp s->eof = 1;
4917 826082fe 2020-12-10 stsp } else
4918 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4919 34bc9ec9 2019-02-22 stsp break;
4920 826082fe 2020-12-10 stsp }
4921 1e37a5c2 2019-05-12 jcs }
4922 826082fe 2020-12-10 stsp free(line);
4923 1e37a5c2 2019-05-12 jcs break;
4924 82c78e96 2022-08-06 thomas case '(':
4925 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
4926 82c78e96 2022-08-06 thomas break;
4927 82c78e96 2022-08-06 thomas case ')':
4928 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
4929 82c78e96 2022-08-06 thomas break;
4930 82c78e96 2022-08-06 thomas case '{':
4931 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
4932 82c78e96 2022-08-06 thomas break;
4933 82c78e96 2022-08-06 thomas case '}':
4934 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
4935 82c78e96 2022-08-06 thomas break;
4936 1e37a5c2 2019-05-12 jcs case '[':
4937 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4938 1e37a5c2 2019-05-12 jcs s->diff_context--;
4939 aa61903a 2021-12-31 thomas s->matched_line = 0;
4940 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4941 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4942 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4943 27829c9e 2020-11-21 stsp s->nlines) {
4944 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4945 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4946 27829c9e 2020-11-21 stsp }
4947 07b0611c 2022-06-23 thomas } else
4948 07b0611c 2022-06-23 thomas view->count = 0;
4949 1e37a5c2 2019-05-12 jcs break;
4950 1e37a5c2 2019-05-12 jcs case ']':
4951 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4952 1e37a5c2 2019-05-12 jcs s->diff_context++;
4953 aa61903a 2021-12-31 thomas s->matched_line = 0;
4954 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4955 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4956 07b0611c 2022-06-23 thomas } else
4957 07b0611c 2022-06-23 thomas view->count = 0;
4958 1e37a5c2 2019-05-12 jcs break;
4959 1e37a5c2 2019-05-12 jcs case '<':
4960 1e37a5c2 2019-05-12 jcs case ',':
4961 777aae21 2022-07-20 thomas case 'K':
4962 4fc71f3b 2022-07-12 thomas up = 1;
4963 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4964 4fc71f3b 2022-07-12 thomas case '>':
4965 4fc71f3b 2022-07-12 thomas case '.':
4966 777aae21 2022-07-20 thomas case 'J':
4967 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4968 07b0611c 2022-06-23 thomas view->count = 0;
4969 48ae06ee 2018-10-18 stsp break;
4970 07b0611c 2022-06-23 thomas }
4971 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4972 6524637e 2019-02-21 stsp
4973 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4974 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4975 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4976 15a087fe 2019-02-21 stsp
4977 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4978 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4979 4fc71f3b 2022-07-12 thomas if (err)
4980 4fc71f3b 2022-07-12 thomas break;
4981 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4982 15a087fe 2019-02-21 stsp
4983 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4984 4fc71f3b 2022-07-12 thomas break;
4985 15a087fe 2019-02-21 stsp
4986 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4987 4fc71f3b 2022-07-12 thomas if (err)
4988 4fc71f3b 2022-07-12 thomas break;
4989 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4990 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4991 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4992 5e224a3e 2019-02-22 stsp
4993 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4994 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4995 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4996 4fc71f3b 2022-07-12 thomas
4997 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4998 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4999 4fc71f3b 2022-07-12 thomas if (err)
5000 4fc71f3b 2022-07-12 thomas break;
5001 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5002 5a8b5076 2020-12-05 stsp
5003 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5004 4fc71f3b 2022-07-12 thomas break;
5005 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5006 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5007 4fc71f3b 2022-07-12 thomas bs->selected_line);
5008 4fc71f3b 2022-07-12 thomas if (id == NULL)
5009 4fc71f3b 2022-07-12 thomas break;
5010 15a087fe 2019-02-21 stsp
5011 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5012 4fc71f3b 2022-07-12 thomas break;
5013 15a087fe 2019-02-21 stsp
5014 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5015 4fc71f3b 2022-07-12 thomas if (err)
5016 4fc71f3b 2022-07-12 thomas break;
5017 4fc71f3b 2022-07-12 thomas }
5018 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5019 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5020 aa61903a 2021-12-31 thomas s->matched_line = 0;
5021 05171be4 2022-06-23 thomas view->x = 0;
5022 1e37a5c2 2019-05-12 jcs
5023 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5024 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5025 1e37a5c2 2019-05-12 jcs break;
5026 1e37a5c2 2019-05-12 jcs default:
5027 07b0611c 2022-06-23 thomas view->count = 0;
5028 1e37a5c2 2019-05-12 jcs break;
5029 26ed57b2 2018-05-19 stsp }
5030 e5a0f69f 2018-08-18 stsp
5031 bcbd79e2 2018-08-19 stsp return err;
5032 26ed57b2 2018-05-19 stsp }
5033 26ed57b2 2018-05-19 stsp
5034 4ed7e80c 2018-05-20 stsp static const struct got_error *
5035 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5036 9f7d7167 2018-04-29 stsp {
5037 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5038 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5039 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5040 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5041 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5042 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5043 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5044 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5045 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5046 3dbaef42 2020-11-24 stsp const char *errstr;
5047 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5048 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5049 70ac5f84 2019-03-28 stsp
5050 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5051 26ed57b2 2018-05-19 stsp switch (ch) {
5052 64453f7e 2020-11-21 stsp case 'a':
5053 64453f7e 2020-11-21 stsp force_text_diff = 1;
5054 3dbaef42 2020-11-24 stsp break;
5055 3dbaef42 2020-11-24 stsp case 'C':
5056 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5057 3dbaef42 2020-11-24 stsp &errstr);
5058 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5059 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5060 8f666e67 2022-02-12 thomas errstr, errstr);
5061 64453f7e 2020-11-21 stsp break;
5062 09b5bff8 2020-02-23 naddy case 'r':
5063 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5064 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5065 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5066 09b5bff8 2020-02-23 naddy optarg);
5067 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5068 09b5bff8 2020-02-23 naddy break;
5069 3dbaef42 2020-11-24 stsp case 'w':
5070 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5071 3dbaef42 2020-11-24 stsp break;
5072 26ed57b2 2018-05-19 stsp default:
5073 17020d27 2019-03-07 stsp usage_diff();
5074 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5075 26ed57b2 2018-05-19 stsp }
5076 26ed57b2 2018-05-19 stsp }
5077 26ed57b2 2018-05-19 stsp
5078 26ed57b2 2018-05-19 stsp argc -= optind;
5079 26ed57b2 2018-05-19 stsp argv += optind;
5080 26ed57b2 2018-05-19 stsp
5081 26ed57b2 2018-05-19 stsp if (argc == 0) {
5082 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5083 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5084 15a94983 2018-12-23 stsp id_str1 = argv[0];
5085 15a94983 2018-12-23 stsp id_str2 = argv[1];
5086 26ed57b2 2018-05-19 stsp } else
5087 26ed57b2 2018-05-19 stsp usage_diff();
5088 eb6600df 2019-01-04 stsp
5089 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5090 7cd52833 2022-06-23 thomas if (error)
5091 7cd52833 2022-06-23 thomas goto done;
5092 7cd52833 2022-06-23 thomas
5093 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5094 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5095 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5096 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5097 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5098 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5099 c156c7a4 2020-12-18 stsp goto done;
5100 a273ac94 2020-02-23 naddy if (worktree)
5101 a273ac94 2020-02-23 naddy repo_path =
5102 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5103 a273ac94 2020-02-23 naddy else
5104 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5105 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5106 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5107 c156c7a4 2020-12-18 stsp goto done;
5108 c156c7a4 2020-12-18 stsp }
5109 a273ac94 2020-02-23 naddy }
5110 a273ac94 2020-02-23 naddy
5111 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5112 eb6600df 2019-01-04 stsp if (error)
5113 eb6600df 2019-01-04 stsp goto done;
5114 26ed57b2 2018-05-19 stsp
5115 a273ac94 2020-02-23 naddy init_curses();
5116 a273ac94 2020-02-23 naddy
5117 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5118 51a10b52 2020-12-26 stsp if (error)
5119 51a10b52 2020-12-26 stsp goto done;
5120 51a10b52 2020-12-26 stsp
5121 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5122 26ed57b2 2018-05-19 stsp if (error)
5123 26ed57b2 2018-05-19 stsp goto done;
5124 26ed57b2 2018-05-19 stsp
5125 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5126 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5127 26ed57b2 2018-05-19 stsp if (error)
5128 26ed57b2 2018-05-19 stsp goto done;
5129 26ed57b2 2018-05-19 stsp
5130 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5131 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5132 26ed57b2 2018-05-19 stsp if (error)
5133 26ed57b2 2018-05-19 stsp goto done;
5134 26ed57b2 2018-05-19 stsp
5135 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5136 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5137 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5138 ea5e7bb5 2018-08-01 stsp goto done;
5139 ea5e7bb5 2018-08-01 stsp }
5140 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5141 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5142 5dc9f4bc 2018-08-04 stsp if (error)
5143 5dc9f4bc 2018-08-04 stsp goto done;
5144 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5145 26ed57b2 2018-05-19 stsp done:
5146 3dbaef42 2020-11-24 stsp free(label1);
5147 3dbaef42 2020-11-24 stsp free(label2);
5148 c02c541e 2019-03-29 stsp free(repo_path);
5149 a273ac94 2020-02-23 naddy free(cwd);
5150 1d0f4054 2021-06-17 stsp if (repo) {
5151 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5152 1d0f4054 2021-06-17 stsp if (error == NULL)
5153 1d0f4054 2021-06-17 stsp error = close_err;
5154 1d0f4054 2021-06-17 stsp }
5155 a273ac94 2020-02-23 naddy if (worktree)
5156 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5157 7cd52833 2022-06-23 thomas if (pack_fds) {
5158 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5159 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5160 7cd52833 2022-06-23 thomas if (error == NULL)
5161 7cd52833 2022-06-23 thomas error = pack_err;
5162 7cd52833 2022-06-23 thomas }
5163 51a10b52 2020-12-26 stsp tog_free_refs();
5164 26ed57b2 2018-05-19 stsp return error;
5165 9f7d7167 2018-04-29 stsp }
5166 9f7d7167 2018-04-29 stsp
5167 4ed7e80c 2018-05-20 stsp __dead static void
5168 9f7d7167 2018-04-29 stsp usage_blame(void)
5169 9f7d7167 2018-04-29 stsp {
5170 80ddbec8 2018-04-29 stsp endwin();
5171 91198554 2022-06-23 thomas fprintf(stderr,
5172 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5173 9f7d7167 2018-04-29 stsp getprogname());
5174 9f7d7167 2018-04-29 stsp exit(1);
5175 9f7d7167 2018-04-29 stsp }
5176 84451b3e 2018-07-10 stsp
5177 84451b3e 2018-07-10 stsp struct tog_blame_line {
5178 84451b3e 2018-07-10 stsp int annotated;
5179 84451b3e 2018-07-10 stsp struct got_object_id *id;
5180 84451b3e 2018-07-10 stsp };
5181 9f7d7167 2018-04-29 stsp
5182 4ed7e80c 2018-05-20 stsp static const struct got_error *
5183 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5184 84451b3e 2018-07-10 stsp {
5185 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5186 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5187 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5188 84451b3e 2018-07-10 stsp const struct got_error *err;
5189 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5190 826082fe 2020-12-10 stsp char *line = NULL;
5191 826082fe 2020-12-10 stsp size_t linesize = 0;
5192 826082fe 2020-12-10 stsp ssize_t linelen;
5193 84451b3e 2018-07-10 stsp wchar_t *wline;
5194 27a741e5 2019-09-11 stsp int width;
5195 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5196 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5197 ab089a2a 2018-07-12 stsp char *id_str;
5198 11b20872 2019-11-08 stsp struct tog_color *tc;
5199 ab089a2a 2018-07-12 stsp
5200 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5201 ab089a2a 2018-07-12 stsp if (err)
5202 ab089a2a 2018-07-12 stsp return err;
5203 84451b3e 2018-07-10 stsp
5204 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5205 f7d12f7e 2018-08-01 stsp werase(view->window);
5206 84451b3e 2018-07-10 stsp
5207 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5208 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5209 ab089a2a 2018-07-12 stsp free(id_str);
5210 ab089a2a 2018-07-12 stsp return err;
5211 ab089a2a 2018-07-12 stsp }
5212 ab089a2a 2018-07-12 stsp
5213 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5214 ab089a2a 2018-07-12 stsp free(line);
5215 2550e4c3 2018-07-13 stsp line = NULL;
5216 1cae65b4 2019-09-22 stsp if (err)
5217 1cae65b4 2019-09-22 stsp return err;
5218 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5219 a3404814 2018-09-02 stsp wstandout(view->window);
5220 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5221 11b20872 2019-11-08 stsp if (tc)
5222 11b20872 2019-11-08 stsp wattr_on(view->window,
5223 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5224 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5225 11b20872 2019-11-08 stsp if (tc)
5226 11b20872 2019-11-08 stsp wattr_off(view->window,
5227 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5228 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5229 a3404814 2018-09-02 stsp wstandend(view->window);
5230 2550e4c3 2018-07-13 stsp free(wline);
5231 2550e4c3 2018-07-13 stsp wline = NULL;
5232 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5233 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5234 ab089a2a 2018-07-12 stsp
5235 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5236 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5237 07dd3ed3 2022-08-06 thomas
5238 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5239 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5240 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5241 ab089a2a 2018-07-12 stsp free(id_str);
5242 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5243 ab089a2a 2018-07-12 stsp }
5244 ab089a2a 2018-07-12 stsp free(id_str);
5245 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5246 3f60a8ef 2018-07-10 stsp free(line);
5247 2550e4c3 2018-07-13 stsp line = NULL;
5248 3f60a8ef 2018-07-10 stsp if (err)
5249 3f60a8ef 2018-07-10 stsp return err;
5250 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5251 2550e4c3 2018-07-13 stsp free(wline);
5252 2550e4c3 2018-07-13 stsp wline = NULL;
5253 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5254 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5255 3f60a8ef 2018-07-10 stsp
5256 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5257 05171be4 2022-06-23 thomas view->maxx = 0;
5258 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5259 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5260 826082fe 2020-12-10 stsp if (linelen == -1) {
5261 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5262 826082fe 2020-12-10 stsp s->eof = 1;
5263 826082fe 2020-12-10 stsp break;
5264 826082fe 2020-12-10 stsp }
5265 84451b3e 2018-07-10 stsp free(line);
5266 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5267 84451b3e 2018-07-10 stsp }
5268 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5269 07dd3ed3 2022-08-06 thomas continue;
5270 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5271 826082fe 2020-12-10 stsp continue;
5272 cbea3800 2022-06-23 thomas
5273 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5274 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5275 cbea3800 2022-06-23 thomas if (err) {
5276 cbea3800 2022-06-23 thomas free(line);
5277 cbea3800 2022-06-23 thomas return err;
5278 cbea3800 2022-06-23 thomas }
5279 cbea3800 2022-06-23 thomas free(wline);
5280 cbea3800 2022-06-23 thomas wline = NULL;
5281 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5282 84451b3e 2018-07-10 stsp
5283 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5284 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5285 b700b5d6 2018-07-10 stsp
5286 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5287 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5288 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5289 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5290 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5291 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5292 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5293 8d0fe45a 2019-08-12 stsp char *id_str;
5294 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5295 91198554 2022-06-23 thomas blame_line->id);
5296 8d0fe45a 2019-08-12 stsp if (err) {
5297 8d0fe45a 2019-08-12 stsp free(line);
5298 8d0fe45a 2019-08-12 stsp return err;
5299 8d0fe45a 2019-08-12 stsp }
5300 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5301 11b20872 2019-11-08 stsp if (tc)
5302 11b20872 2019-11-08 stsp wattr_on(view->window,
5303 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5304 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5305 11b20872 2019-11-08 stsp if (tc)
5306 11b20872 2019-11-08 stsp wattr_off(view->window,
5307 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5308 8d0fe45a 2019-08-12 stsp free(id_str);
5309 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5310 8d0fe45a 2019-08-12 stsp } else {
5311 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5312 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5313 84451b3e 2018-07-10 stsp }
5314 ee41ec32 2018-07-10 stsp } else {
5315 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5316 ee41ec32 2018-07-10 stsp prev_id = NULL;
5317 ee41ec32 2018-07-10 stsp }
5318 84451b3e 2018-07-10 stsp
5319 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5320 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5321 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5322 27a741e5 2019-09-11 stsp
5323 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5324 41605754 2020-11-12 stsp width = 9;
5325 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5326 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5327 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5328 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5329 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5330 41605754 2020-11-12 stsp if (err) {
5331 41605754 2020-11-12 stsp free(line);
5332 41605754 2020-11-12 stsp return err;
5333 41605754 2020-11-12 stsp }
5334 41605754 2020-11-12 stsp width += 9;
5335 41605754 2020-11-12 stsp } else {
5336 923086fd 2022-06-23 thomas int skip;
5337 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5338 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5339 923086fd 2022-06-23 thomas if (err) {
5340 923086fd 2022-06-23 thomas free(line);
5341 923086fd 2022-06-23 thomas return err;
5342 05171be4 2022-06-23 thomas }
5343 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5344 02bce7e2 2022-06-23 thomas width += 9;
5345 41605754 2020-11-12 stsp free(wline);
5346 41605754 2020-11-12 stsp wline = NULL;
5347 41605754 2020-11-12 stsp }
5348 41605754 2020-11-12 stsp
5349 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5350 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5351 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5352 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5353 84451b3e 2018-07-10 stsp }
5354 826082fe 2020-12-10 stsp free(line);
5355 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5356 84451b3e 2018-07-10 stsp
5357 a5d43cac 2022-07-01 thomas view_border(view);
5358 84451b3e 2018-07-10 stsp
5359 84451b3e 2018-07-10 stsp return NULL;
5360 84451b3e 2018-07-10 stsp }
5361 84451b3e 2018-07-10 stsp
5362 84451b3e 2018-07-10 stsp static const struct got_error *
5363 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5364 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5365 84451b3e 2018-07-10 stsp {
5366 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5367 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5368 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5369 1a76625f 2018-10-22 stsp int errcode;
5370 84451b3e 2018-07-10 stsp
5371 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5372 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5373 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5374 84451b3e 2018-07-10 stsp
5375 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5376 1a76625f 2018-10-22 stsp if (errcode)
5377 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5378 84451b3e 2018-07-10 stsp
5379 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5380 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5381 d68a0a7d 2018-07-10 stsp goto done;
5382 d68a0a7d 2018-07-10 stsp }
5383 d68a0a7d 2018-07-10 stsp
5384 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5385 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5386 d68a0a7d 2018-07-10 stsp
5387 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5388 d68a0a7d 2018-07-10 stsp if (line->annotated)
5389 d68a0a7d 2018-07-10 stsp goto done;
5390 d68a0a7d 2018-07-10 stsp
5391 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5392 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5393 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5394 84451b3e 2018-07-10 stsp goto done;
5395 84451b3e 2018-07-10 stsp }
5396 84451b3e 2018-07-10 stsp line->annotated = 1;
5397 84451b3e 2018-07-10 stsp done:
5398 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5399 1a76625f 2018-10-22 stsp if (errcode)
5400 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5401 84451b3e 2018-07-10 stsp return err;
5402 84451b3e 2018-07-10 stsp }
5403 84451b3e 2018-07-10 stsp
5404 84451b3e 2018-07-10 stsp static void *
5405 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5406 84451b3e 2018-07-10 stsp {
5407 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5408 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5409 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5410 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5411 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5412 00a8878e 2022-07-01 thomas
5413 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5414 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5415 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5416 9117a7b7 2022-07-01 thomas
5417 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5418 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5419 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5420 9117a7b7 2022-07-01 thomas goto done;
5421 9117a7b7 2022-07-01 thomas }
5422 18430de3 2018-07-10 stsp
5423 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5424 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5425 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5426 9117a7b7 2022-07-01 thomas goto done;
5427 9117a7b7 2022-07-01 thomas }
5428 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5429 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5430 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5431 9117a7b7 2022-07-01 thomas goto done;
5432 9117a7b7 2022-07-01 thomas }
5433 9117a7b7 2022-07-01 thomas
5434 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5435 61266923 2020-01-14 stsp if (err)
5436 9117a7b7 2022-07-01 thomas goto done;
5437 61266923 2020-01-14 stsp
5438 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5439 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5440 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5441 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5442 fc06ba56 2019-08-22 stsp err = NULL;
5443 18430de3 2018-07-10 stsp
5444 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5445 9117a7b7 2022-07-01 thomas if (errcode) {
5446 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5447 9117a7b7 2022-07-01 thomas goto done;
5448 9117a7b7 2022-07-01 thomas }
5449 18430de3 2018-07-10 stsp
5450 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5451 1d0f4054 2021-06-17 stsp if (err == NULL)
5452 1d0f4054 2021-06-17 stsp err = close_err;
5453 c9beca56 2018-07-22 stsp ta->repo = NULL;
5454 c9beca56 2018-07-22 stsp *ta->complete = 1;
5455 18430de3 2018-07-10 stsp
5456 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5457 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5458 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5459 18430de3 2018-07-10 stsp
5460 9117a7b7 2022-07-01 thomas done:
5461 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5462 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5463 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5464 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5465 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5466 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5467 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5468 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5469 00a8878e 2022-07-01 thomas
5470 18430de3 2018-07-10 stsp return (void *)err;
5471 84451b3e 2018-07-10 stsp }
5472 84451b3e 2018-07-10 stsp
5473 245d91c1 2018-07-12 stsp static struct got_object_id *
5474 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5475 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5476 245d91c1 2018-07-12 stsp {
5477 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5478 8d0fe45a 2019-08-12 stsp
5479 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5480 8d0fe45a 2019-08-12 stsp return NULL;
5481 b880a918 2018-07-10 stsp
5482 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5483 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5484 4fc71f3b 2022-07-12 thomas return NULL;
5485 4fc71f3b 2022-07-12 thomas
5486 4fc71f3b 2022-07-12 thomas return line->id;
5487 4fc71f3b 2022-07-12 thomas }
5488 4fc71f3b 2022-07-12 thomas
5489 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5490 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5491 4fc71f3b 2022-07-12 thomas int lineno)
5492 4fc71f3b 2022-07-12 thomas {
5493 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5494 4fc71f3b 2022-07-12 thomas
5495 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5496 4fc71f3b 2022-07-12 thomas return NULL;
5497 4fc71f3b 2022-07-12 thomas
5498 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5499 245d91c1 2018-07-12 stsp if (!line->annotated)
5500 245d91c1 2018-07-12 stsp return NULL;
5501 245d91c1 2018-07-12 stsp
5502 245d91c1 2018-07-12 stsp return line->id;
5503 b880a918 2018-07-10 stsp }
5504 245d91c1 2018-07-12 stsp
5505 b880a918 2018-07-10 stsp static const struct got_error *
5506 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5507 a70480e0 2018-06-23 stsp {
5508 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5509 245d91c1 2018-07-12 stsp int i;
5510 245d91c1 2018-07-12 stsp
5511 245d91c1 2018-07-12 stsp if (blame->thread) {
5512 1a76625f 2018-10-22 stsp int errcode;
5513 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5514 1a76625f 2018-10-22 stsp if (errcode)
5515 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5516 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5517 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5518 1a76625f 2018-10-22 stsp if (errcode)
5519 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5520 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5521 1a76625f 2018-10-22 stsp if (errcode)
5522 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5523 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5524 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5525 245d91c1 2018-07-12 stsp err = NULL;
5526 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5527 245d91c1 2018-07-12 stsp }
5528 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5529 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5530 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5531 1d0f4054 2021-06-17 stsp if (err == NULL)
5532 1d0f4054 2021-06-17 stsp err = close_err;
5533 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5534 245d91c1 2018-07-12 stsp }
5535 245d91c1 2018-07-12 stsp if (blame->f) {
5536 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5537 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5538 245d91c1 2018-07-12 stsp blame->f = NULL;
5539 245d91c1 2018-07-12 stsp }
5540 57670559 2018-12-23 stsp if (blame->lines) {
5541 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5542 57670559 2018-12-23 stsp free(blame->lines[i].id);
5543 57670559 2018-12-23 stsp free(blame->lines);
5544 57670559 2018-12-23 stsp blame->lines = NULL;
5545 57670559 2018-12-23 stsp }
5546 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5547 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5548 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5549 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5550 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5551 7cd52833 2022-06-23 thomas if (err == NULL)
5552 7cd52833 2022-06-23 thomas err = pack_err;
5553 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5554 7cd52833 2022-06-23 thomas }
5555 245d91c1 2018-07-12 stsp return err;
5556 245d91c1 2018-07-12 stsp }
5557 245d91c1 2018-07-12 stsp
5558 245d91c1 2018-07-12 stsp static const struct got_error *
5559 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5560 fc06ba56 2019-08-22 stsp {
5561 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5562 fc06ba56 2019-08-22 stsp int *done = arg;
5563 fc06ba56 2019-08-22 stsp int errcode;
5564 fc06ba56 2019-08-22 stsp
5565 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5566 fc06ba56 2019-08-22 stsp if (errcode)
5567 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5568 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5569 fc06ba56 2019-08-22 stsp
5570 fc06ba56 2019-08-22 stsp if (*done)
5571 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5572 fc06ba56 2019-08-22 stsp
5573 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5574 fc06ba56 2019-08-22 stsp if (errcode)
5575 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5576 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5577 fc06ba56 2019-08-22 stsp
5578 fc06ba56 2019-08-22 stsp return err;
5579 fc06ba56 2019-08-22 stsp }
5580 fc06ba56 2019-08-22 stsp
5581 fc06ba56 2019-08-22 stsp static const struct got_error *
5582 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5583 245d91c1 2018-07-12 stsp {
5584 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5585 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5586 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5587 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5588 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5589 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5590 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5591 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5592 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5593 a70480e0 2018-06-23 stsp
5594 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5595 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5596 27d434c2 2018-09-15 stsp if (err)
5597 15a94983 2018-12-23 stsp return err;
5598 f4ae6ddb 2022-07-01 thomas
5599 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5600 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5601 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5602 f4ae6ddb 2022-07-01 thomas goto done;
5603 f4ae6ddb 2022-07-01 thomas }
5604 945f9229 2022-04-16 thomas
5605 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5606 945f9229 2022-04-16 thomas if (err)
5607 945f9229 2022-04-16 thomas goto done;
5608 27d434c2 2018-09-15 stsp
5609 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5610 84451b3e 2018-07-10 stsp if (err)
5611 84451b3e 2018-07-10 stsp goto done;
5612 27d434c2 2018-09-15 stsp
5613 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5614 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5615 84451b3e 2018-07-10 stsp goto done;
5616 84451b3e 2018-07-10 stsp }
5617 a70480e0 2018-06-23 stsp
5618 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5619 a70480e0 2018-06-23 stsp if (err)
5620 a70480e0 2018-06-23 stsp goto done;
5621 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5622 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5623 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5624 84451b3e 2018-07-10 stsp goto done;
5625 84451b3e 2018-07-10 stsp }
5626 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5627 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5628 1fddf795 2021-01-20 stsp if (err)
5629 1fddf795 2021-01-20 stsp goto done;
5630 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5631 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5632 84451b3e 2018-07-10 stsp goto done;
5633 1fddf795 2021-01-20 stsp }
5634 b02560ec 2019-08-19 stsp
5635 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5636 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5637 b02560ec 2019-08-19 stsp blame->nlines--;
5638 a70480e0 2018-06-23 stsp
5639 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5640 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5641 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5642 84451b3e 2018-07-10 stsp goto done;
5643 84451b3e 2018-07-10 stsp }
5644 a70480e0 2018-06-23 stsp
5645 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5646 bd24772e 2018-07-11 stsp if (err)
5647 bd24772e 2018-07-11 stsp goto done;
5648 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5649 7cd52833 2022-06-23 thomas pack_fds);
5650 7cd52833 2022-06-23 thomas if (err)
5651 7cd52833 2022-06-23 thomas goto done;
5652 bd24772e 2018-07-11 stsp
5653 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5654 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5655 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5656 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5657 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5658 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5659 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5660 245d91c1 2018-07-12 stsp goto done;
5661 245d91c1 2018-07-12 stsp }
5662 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5663 245d91c1 2018-07-12 stsp
5664 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5665 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5666 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5667 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5668 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5669 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5670 a5388363 2020-12-01 naddy s->blame_complete = 0;
5671 f5a09613 2020-12-13 naddy
5672 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5673 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5674 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5675 f5a09613 2020-12-13 naddy s->selected_line = 1;
5676 f5a09613 2020-12-13 naddy }
5677 aa61903a 2021-12-31 thomas s->matched_line = 0;
5678 245d91c1 2018-07-12 stsp
5679 245d91c1 2018-07-12 stsp done:
5680 945f9229 2022-04-16 thomas if (commit)
5681 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5682 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5683 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5684 245d91c1 2018-07-12 stsp if (blob)
5685 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5686 27d434c2 2018-09-15 stsp free(obj_id);
5687 245d91c1 2018-07-12 stsp if (err)
5688 245d91c1 2018-07-12 stsp stop_blame(blame);
5689 245d91c1 2018-07-12 stsp return err;
5690 245d91c1 2018-07-12 stsp }
5691 245d91c1 2018-07-12 stsp
5692 245d91c1 2018-07-12 stsp static const struct got_error *
5693 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5694 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5695 245d91c1 2018-07-12 stsp {
5696 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5697 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5698 dbc6a6b6 2018-07-12 stsp
5699 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5700 245d91c1 2018-07-12 stsp
5701 c4843652 2019-08-12 stsp s->path = strdup(path);
5702 c4843652 2019-08-12 stsp if (s->path == NULL)
5703 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5704 c4843652 2019-08-12 stsp
5705 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5706 c4843652 2019-08-12 stsp if (err) {
5707 c4843652 2019-08-12 stsp free(s->path);
5708 7cbe629d 2018-08-04 stsp return err;
5709 c4843652 2019-08-12 stsp }
5710 245d91c1 2018-07-12 stsp
5711 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5712 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5713 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5714 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5715 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5716 fb2756b9 2018-08-04 stsp s->repo = repo;
5717 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5718 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5719 7cbe629d 2018-08-04 stsp
5720 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5721 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5722 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5723 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5724 11b20872 2019-11-08 stsp if (err)
5725 11b20872 2019-11-08 stsp return err;
5726 11b20872 2019-11-08 stsp }
5727 11b20872 2019-11-08 stsp
5728 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5729 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5730 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5731 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5732 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5733 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5734 e5a0f69f 2018-08-18 stsp
5735 a5388363 2020-12-01 naddy return run_blame(view);
5736 7cbe629d 2018-08-04 stsp }
5737 7cbe629d 2018-08-04 stsp
5738 e5a0f69f 2018-08-18 stsp static const struct got_error *
5739 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5740 7cbe629d 2018-08-04 stsp {
5741 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5742 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5743 7cbe629d 2018-08-04 stsp
5744 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5745 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5746 e5a0f69f 2018-08-18 stsp
5747 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5748 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5749 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5750 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5751 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5752 7cbe629d 2018-08-04 stsp }
5753 e5a0f69f 2018-08-18 stsp
5754 e5a0f69f 2018-08-18 stsp free(s->path);
5755 11b20872 2019-11-08 stsp free_colors(&s->colors);
5756 e5a0f69f 2018-08-18 stsp return err;
5757 7cbe629d 2018-08-04 stsp }
5758 7cbe629d 2018-08-04 stsp
5759 7cbe629d 2018-08-04 stsp static const struct got_error *
5760 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5761 6c4c42e0 2019-06-24 stsp {
5762 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5763 6c4c42e0 2019-06-24 stsp
5764 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5765 6c4c42e0 2019-06-24 stsp return NULL;
5766 6c4c42e0 2019-06-24 stsp }
5767 6c4c42e0 2019-06-24 stsp
5768 6c4c42e0 2019-06-24 stsp static const struct got_error *
5769 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5770 6c4c42e0 2019-06-24 stsp {
5771 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5772 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5773 6c4c42e0 2019-06-24 stsp int lineno;
5774 78eecffc 2022-06-23 thomas char *line = NULL;
5775 826082fe 2020-12-10 stsp size_t linesize = 0;
5776 826082fe 2020-12-10 stsp ssize_t linelen;
5777 6c4c42e0 2019-06-24 stsp
5778 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5779 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5780 6c4c42e0 2019-06-24 stsp return NULL;
5781 6c4c42e0 2019-06-24 stsp }
5782 6c4c42e0 2019-06-24 stsp
5783 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5784 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5785 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5786 6c4c42e0 2019-06-24 stsp else
5787 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5788 4b3f9dac 2021-12-17 thomas } else
5789 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5790 6c4c42e0 2019-06-24 stsp
5791 6c4c42e0 2019-06-24 stsp while (1) {
5792 6c4c42e0 2019-06-24 stsp off_t offset;
5793 6c4c42e0 2019-06-24 stsp
5794 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5795 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5796 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5797 6c4c42e0 2019-06-24 stsp break;
5798 6c4c42e0 2019-06-24 stsp }
5799 2246482e 2019-06-25 stsp
5800 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5801 6c4c42e0 2019-06-24 stsp lineno = 1;
5802 6c4c42e0 2019-06-24 stsp else
5803 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5804 6c4c42e0 2019-06-24 stsp }
5805 6c4c42e0 2019-06-24 stsp
5806 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5807 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5808 6c4c42e0 2019-06-24 stsp free(line);
5809 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5810 6c4c42e0 2019-06-24 stsp }
5811 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5812 78eecffc 2022-06-23 thomas if (linelen != -1) {
5813 78eecffc 2022-06-23 thomas char *exstr;
5814 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5815 78eecffc 2022-06-23 thomas if (err)
5816 78eecffc 2022-06-23 thomas break;
5817 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5818 78eecffc 2022-06-23 thomas &view->regmatch)) {
5819 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5820 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5821 78eecffc 2022-06-23 thomas free(exstr);
5822 78eecffc 2022-06-23 thomas break;
5823 78eecffc 2022-06-23 thomas }
5824 78eecffc 2022-06-23 thomas free(exstr);
5825 6c4c42e0 2019-06-24 stsp }
5826 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5827 6c4c42e0 2019-06-24 stsp lineno++;
5828 6c4c42e0 2019-06-24 stsp else
5829 6c4c42e0 2019-06-24 stsp lineno--;
5830 6c4c42e0 2019-06-24 stsp }
5831 826082fe 2020-12-10 stsp free(line);
5832 6c4c42e0 2019-06-24 stsp
5833 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5834 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5835 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5836 6c4c42e0 2019-06-24 stsp }
5837 6c4c42e0 2019-06-24 stsp
5838 05171be4 2022-06-23 thomas return err;
5839 6c4c42e0 2019-06-24 stsp }
5840 6c4c42e0 2019-06-24 stsp
5841 6c4c42e0 2019-06-24 stsp static const struct got_error *
5842 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5843 7cbe629d 2018-08-04 stsp {
5844 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5845 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5846 2b380cc8 2018-10-24 stsp int errcode;
5847 2b380cc8 2018-10-24 stsp
5848 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5849 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5850 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5851 2b380cc8 2018-10-24 stsp if (errcode)
5852 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5853 51fe7530 2019-08-19 stsp
5854 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5855 2b380cc8 2018-10-24 stsp }
5856 e5a0f69f 2018-08-18 stsp
5857 51fe7530 2019-08-19 stsp if (s->blame_complete)
5858 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5859 51fe7530 2019-08-19 stsp
5860 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5861 e5a0f69f 2018-08-18 stsp
5862 a5d43cac 2022-07-01 thomas view_border(view);
5863 e5a0f69f 2018-08-18 stsp return err;
5864 e5a0f69f 2018-08-18 stsp }
5865 e5a0f69f 2018-08-18 stsp
5866 e5a0f69f 2018-08-18 stsp static const struct got_error *
5867 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5868 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5869 eaeaa612 2022-07-20 thomas {
5870 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5871 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5872 eaeaa612 2022-07-20 thomas
5873 eaeaa612 2022-07-20 thomas *new_view = NULL;
5874 eaeaa612 2022-07-20 thomas
5875 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5876 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5877 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5878 eaeaa612 2022-07-20 thomas
5879 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5880 eaeaa612 2022-07-20 thomas if (err)
5881 eaeaa612 2022-07-20 thomas view_close(log_view);
5882 eaeaa612 2022-07-20 thomas else
5883 eaeaa612 2022-07-20 thomas *new_view = log_view;
5884 eaeaa612 2022-07-20 thomas
5885 eaeaa612 2022-07-20 thomas return err;
5886 eaeaa612 2022-07-20 thomas }
5887 eaeaa612 2022-07-20 thomas
5888 eaeaa612 2022-07-20 thomas static const struct got_error *
5889 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5890 e5a0f69f 2018-08-18 stsp {
5891 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5892 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
5893 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5894 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5895 a5d43cac 2022-07-01 thomas
5896 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5897 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5898 a5d43cac 2022-07-01 thomas --eos; /* border */
5899 7cbe629d 2018-08-04 stsp
5900 e5a0f69f 2018-08-18 stsp switch (ch) {
5901 05171be4 2022-06-23 thomas case '0':
5902 05171be4 2022-06-23 thomas view->x = 0;
5903 05171be4 2022-06-23 thomas break;
5904 05171be4 2022-06-23 thomas case '$':
5905 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5906 07b0611c 2022-06-23 thomas view->count = 0;
5907 05171be4 2022-06-23 thomas break;
5908 05171be4 2022-06-23 thomas case KEY_RIGHT:
5909 05171be4 2022-06-23 thomas case 'l':
5910 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5911 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5912 07b0611c 2022-06-23 thomas else
5913 07b0611c 2022-06-23 thomas view->count = 0;
5914 05171be4 2022-06-23 thomas break;
5915 05171be4 2022-06-23 thomas case KEY_LEFT:
5916 05171be4 2022-06-23 thomas case 'h':
5917 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5918 07b0611c 2022-06-23 thomas if (view->x <= 0)
5919 07b0611c 2022-06-23 thomas view->count = 0;
5920 05171be4 2022-06-23 thomas break;
5921 1e37a5c2 2019-05-12 jcs case 'q':
5922 1e37a5c2 2019-05-12 jcs s->done = 1;
5923 4deef56f 2021-09-02 naddy break;
5924 4deef56f 2021-09-02 naddy case 'g':
5925 4deef56f 2021-09-02 naddy case KEY_HOME:
5926 4deef56f 2021-09-02 naddy s->selected_line = 1;
5927 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5928 07b0611c 2022-06-23 thomas view->count = 0;
5929 4deef56f 2021-09-02 naddy break;
5930 4deef56f 2021-09-02 naddy case 'G':
5931 4deef56f 2021-09-02 naddy case KEY_END:
5932 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5933 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5934 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5935 4deef56f 2021-09-02 naddy } else {
5936 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5937 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5938 4deef56f 2021-09-02 naddy }
5939 07b0611c 2022-06-23 thomas view->count = 0;
5940 1e37a5c2 2019-05-12 jcs break;
5941 1e37a5c2 2019-05-12 jcs case 'k':
5942 1e37a5c2 2019-05-12 jcs case KEY_UP:
5943 f7140bf5 2021-10-17 thomas case CTRL('p'):
5944 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5945 1e37a5c2 2019-05-12 jcs s->selected_line--;
5946 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5947 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5948 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5949 07b0611c 2022-06-23 thomas else
5950 07b0611c 2022-06-23 thomas view->count = 0;
5951 1e37a5c2 2019-05-12 jcs break;
5952 70f17a53 2022-06-13 thomas case CTRL('u'):
5953 23427b14 2022-06-23 thomas case 'u':
5954 70f17a53 2022-06-13 thomas nscroll /= 2;
5955 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5956 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5957 ea025d1d 2020-02-22 naddy case CTRL('b'):
5958 1c5e5faa 2022-06-23 thomas case 'b':
5959 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5960 07b0611c 2022-06-23 thomas if (view->count > 1)
5961 07b0611c 2022-06-23 thomas nscroll += nscroll;
5962 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5963 07b0611c 2022-06-23 thomas view->count = 0;
5964 e5a0f69f 2018-08-18 stsp break;
5965 1e37a5c2 2019-05-12 jcs }
5966 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5967 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5968 1e37a5c2 2019-05-12 jcs else
5969 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5970 1e37a5c2 2019-05-12 jcs break;
5971 1e37a5c2 2019-05-12 jcs case 'j':
5972 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5973 f7140bf5 2021-10-17 thomas case CTRL('n'):
5974 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5975 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5976 1e37a5c2 2019-05-12 jcs s->selected_line++;
5977 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5978 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5979 07b0611c 2022-06-23 thomas else
5980 07b0611c 2022-06-23 thomas view->count = 0;
5981 1e37a5c2 2019-05-12 jcs break;
5982 1c5e5faa 2022-06-23 thomas case 'c':
5983 1e37a5c2 2019-05-12 jcs case 'p': {
5984 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5985 07b0611c 2022-06-23 thomas
5986 07b0611c 2022-06-23 thomas view->count = 0;
5987 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5988 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5989 1e37a5c2 2019-05-12 jcs if (id == NULL)
5990 e5a0f69f 2018-08-18 stsp break;
5991 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5992 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5993 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5994 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5995 1e37a5c2 2019-05-12 jcs int obj_type;
5996 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5997 1e37a5c2 2019-05-12 jcs s->repo, id);
5998 e5a0f69f 2018-08-18 stsp if (err)
5999 e5a0f69f 2018-08-18 stsp break;
6000 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6001 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6002 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6003 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6004 e5a0f69f 2018-08-18 stsp break;
6005 e5a0f69f 2018-08-18 stsp }
6006 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6007 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6008 ec242592 2022-04-22 thomas s->repo, &pid->id);
6009 945f9229 2022-04-16 thomas if (err)
6010 945f9229 2022-04-16 thomas break;
6011 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6012 945f9229 2022-04-16 thomas pcommit, s->path);
6013 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6014 e5a0f69f 2018-08-18 stsp if (err) {
6015 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6016 1e37a5c2 2019-05-12 jcs err = NULL;
6017 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6018 e5a0f69f 2018-08-18 stsp break;
6019 e5a0f69f 2018-08-18 stsp }
6020 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6021 1e37a5c2 2019-05-12 jcs blob_id);
6022 1e37a5c2 2019-05-12 jcs free(blob_id);
6023 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6024 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6025 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6026 e5a0f69f 2018-08-18 stsp break;
6027 1e37a5c2 2019-05-12 jcs }
6028 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6029 ec242592 2022-04-22 thomas &pid->id);
6030 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6031 1e37a5c2 2019-05-12 jcs } else {
6032 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6033 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6034 1e37a5c2 2019-05-12 jcs break;
6035 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6036 1e37a5c2 2019-05-12 jcs id);
6037 1e37a5c2 2019-05-12 jcs }
6038 1e37a5c2 2019-05-12 jcs if (err)
6039 e5a0f69f 2018-08-18 stsp break;
6040 1e37a5c2 2019-05-12 jcs s->done = 1;
6041 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6042 1e37a5c2 2019-05-12 jcs s->done = 0;
6043 1e37a5c2 2019-05-12 jcs if (thread_err)
6044 1e37a5c2 2019-05-12 jcs break;
6045 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6046 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6047 a5388363 2020-12-01 naddy err = run_blame(view);
6048 1e37a5c2 2019-05-12 jcs if (err)
6049 1e37a5c2 2019-05-12 jcs break;
6050 1e37a5c2 2019-05-12 jcs break;
6051 1e37a5c2 2019-05-12 jcs }
6052 1c5e5faa 2022-06-23 thomas case 'C': {
6053 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6054 07b0611c 2022-06-23 thomas
6055 07b0611c 2022-06-23 thomas view->count = 0;
6056 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6057 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6058 1e37a5c2 2019-05-12 jcs break;
6059 1e37a5c2 2019-05-12 jcs s->done = 1;
6060 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6061 1e37a5c2 2019-05-12 jcs s->done = 0;
6062 1e37a5c2 2019-05-12 jcs if (thread_err)
6063 1e37a5c2 2019-05-12 jcs break;
6064 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6065 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6066 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6067 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6068 a5388363 2020-12-01 naddy err = run_blame(view);
6069 1e37a5c2 2019-05-12 jcs if (err)
6070 1e37a5c2 2019-05-12 jcs break;
6071 1e37a5c2 2019-05-12 jcs break;
6072 1e37a5c2 2019-05-12 jcs }
6073 2a31b33b 2022-07-23 thomas case 'L':
6074 eaeaa612 2022-07-20 thomas view->count = 0;
6075 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6076 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6077 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6078 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6079 eaeaa612 2022-07-20 thomas break;
6080 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6081 1e37a5c2 2019-05-12 jcs case '\r': {
6082 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6083 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6084 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6085 07b0611c 2022-06-23 thomas
6086 07b0611c 2022-06-23 thomas view->count = 0;
6087 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6088 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6089 1e37a5c2 2019-05-12 jcs if (id == NULL)
6090 1e37a5c2 2019-05-12 jcs break;
6091 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6092 1e37a5c2 2019-05-12 jcs if (err)
6093 1e37a5c2 2019-05-12 jcs break;
6094 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6095 4fc71f3b 2022-07-12 thomas if (*new_view) {
6096 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6097 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6098 4fc71f3b 2022-07-12 thomas if (err)
6099 4fc71f3b 2022-07-12 thomas break;
6100 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6101 4fc71f3b 2022-07-12 thomas } else {
6102 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6103 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6104 a5d43cac 2022-07-01 thomas
6105 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6106 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6107 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6108 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6109 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6110 4fc71f3b 2022-07-12 thomas break;
6111 4fc71f3b 2022-07-12 thomas }
6112 15a94983 2018-12-23 stsp }
6113 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6114 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6115 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6116 1e37a5c2 2019-05-12 jcs if (err) {
6117 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6118 1e37a5c2 2019-05-12 jcs break;
6119 1e37a5c2 2019-05-12 jcs }
6120 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6121 4fc71f3b 2022-07-12 thomas s->selected_line;
6122 4fc71f3b 2022-07-12 thomas if (*new_view)
6123 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6124 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6125 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6126 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6127 a5d43cac 2022-07-01 thomas if (err)
6128 a5d43cac 2022-07-01 thomas break;
6129 a5d43cac 2022-07-01 thomas }
6130 a5d43cac 2022-07-01 thomas
6131 e78dc838 2020-12-04 stsp view->focussed = 0;
6132 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6133 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6134 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6135 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6136 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6137 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6138 1e37a5c2 2019-05-12 jcs if (err)
6139 34bc9ec9 2019-02-22 stsp break;
6140 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6141 40236d76 2022-06-23 thomas if (err)
6142 40236d76 2022-06-23 thomas break;
6143 e78dc838 2020-12-04 stsp view->focus_child = 1;
6144 1e37a5c2 2019-05-12 jcs } else
6145 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6146 1e37a5c2 2019-05-12 jcs if (err)
6147 e5a0f69f 2018-08-18 stsp break;
6148 1e37a5c2 2019-05-12 jcs break;
6149 1e37a5c2 2019-05-12 jcs }
6150 70f17a53 2022-06-13 thomas case CTRL('d'):
6151 23427b14 2022-06-23 thomas case 'd':
6152 70f17a53 2022-06-13 thomas nscroll /= 2;
6153 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6154 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6155 ea025d1d 2020-02-22 naddy case CTRL('f'):
6156 1c5e5faa 2022-06-23 thomas case 'f':
6157 1e37a5c2 2019-05-12 jcs case ' ':
6158 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6159 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6160 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6161 07b0611c 2022-06-23 thomas view->count = 0;
6162 e5a0f69f 2018-08-18 stsp break;
6163 1e37a5c2 2019-05-12 jcs }
6164 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6165 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6166 70f17a53 2022-06-13 thomas s->selected_line +=
6167 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6168 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6169 1e37a5c2 2019-05-12 jcs }
6170 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6171 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6172 1e37a5c2 2019-05-12 jcs else
6173 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6174 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6175 1e37a5c2 2019-05-12 jcs break;
6176 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6177 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6178 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6179 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6180 1e37a5c2 2019-05-12 jcs }
6181 1e37a5c2 2019-05-12 jcs break;
6182 1e37a5c2 2019-05-12 jcs default:
6183 07b0611c 2022-06-23 thomas view->count = 0;
6184 1e37a5c2 2019-05-12 jcs break;
6185 a70480e0 2018-06-23 stsp }
6186 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6187 adf4c9e0 2022-07-03 thomas }
6188 adf4c9e0 2022-07-03 thomas
6189 adf4c9e0 2022-07-03 thomas static const struct got_error *
6190 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6191 adf4c9e0 2022-07-03 thomas {
6192 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6193 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6194 adf4c9e0 2022-07-03 thomas
6195 adf4c9e0 2022-07-03 thomas view->count = 0;
6196 adf4c9e0 2022-07-03 thomas s->done = 1;
6197 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6198 adf4c9e0 2022-07-03 thomas s->done = 0;
6199 adf4c9e0 2022-07-03 thomas if (err)
6200 adf4c9e0 2022-07-03 thomas return err;
6201 adf4c9e0 2022-07-03 thomas return run_blame(view);
6202 a70480e0 2018-06-23 stsp }
6203 a70480e0 2018-06-23 stsp
6204 a70480e0 2018-06-23 stsp static const struct got_error *
6205 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6206 9f7d7167 2018-04-29 stsp {
6207 a70480e0 2018-06-23 stsp const struct got_error *error;
6208 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6209 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6210 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6211 0587e10c 2020-07-23 stsp char *link_target = NULL;
6212 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6213 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6214 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6215 a70480e0 2018-06-23 stsp int ch;
6216 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6217 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6218 a70480e0 2018-06-23 stsp
6219 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6220 a70480e0 2018-06-23 stsp switch (ch) {
6221 a70480e0 2018-06-23 stsp case 'c':
6222 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6223 a70480e0 2018-06-23 stsp break;
6224 69069811 2018-08-02 stsp case 'r':
6225 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6226 69069811 2018-08-02 stsp if (repo_path == NULL)
6227 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6228 9ba1d308 2019-10-21 stsp optarg);
6229 69069811 2018-08-02 stsp break;
6230 a70480e0 2018-06-23 stsp default:
6231 17020d27 2019-03-07 stsp usage_blame();
6232 a70480e0 2018-06-23 stsp /* NOTREACHED */
6233 a70480e0 2018-06-23 stsp }
6234 a70480e0 2018-06-23 stsp }
6235 a70480e0 2018-06-23 stsp
6236 a70480e0 2018-06-23 stsp argc -= optind;
6237 a70480e0 2018-06-23 stsp argv += optind;
6238 a70480e0 2018-06-23 stsp
6239 f135c941 2020-02-20 stsp if (argc != 1)
6240 a70480e0 2018-06-23 stsp usage_blame();
6241 6962eb72 2020-02-20 stsp
6242 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6243 7cd52833 2022-06-23 thomas if (error != NULL)
6244 7cd52833 2022-06-23 thomas goto done;
6245 7cd52833 2022-06-23 thomas
6246 69069811 2018-08-02 stsp if (repo_path == NULL) {
6247 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6248 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6249 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6250 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6251 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6252 c156c7a4 2020-12-18 stsp goto done;
6253 f135c941 2020-02-20 stsp if (worktree)
6254 eb41ed75 2019-02-05 stsp repo_path =
6255 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6256 f135c941 2020-02-20 stsp else
6257 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6258 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6259 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6260 c156c7a4 2020-12-18 stsp goto done;
6261 c156c7a4 2020-12-18 stsp }
6262 f135c941 2020-02-20 stsp }
6263 a915003a 2019-02-05 stsp
6264 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6265 c02c541e 2019-03-29 stsp if (error != NULL)
6266 8e94dd5b 2019-01-04 stsp goto done;
6267 69069811 2018-08-02 stsp
6268 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6269 f135c941 2020-02-20 stsp worktree);
6270 c02c541e 2019-03-29 stsp if (error)
6271 92205607 2019-01-04 stsp goto done;
6272 69069811 2018-08-02 stsp
6273 f135c941 2020-02-20 stsp init_curses();
6274 f135c941 2020-02-20 stsp
6275 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6276 51a10b52 2020-12-26 stsp if (error)
6277 51a10b52 2020-12-26 stsp goto done;
6278 51a10b52 2020-12-26 stsp
6279 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6280 eb41ed75 2019-02-05 stsp if (error)
6281 69069811 2018-08-02 stsp goto done;
6282 a70480e0 2018-06-23 stsp
6283 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6284 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6285 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6286 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6287 a70480e0 2018-06-23 stsp if (error != NULL)
6288 66b4983c 2018-06-23 stsp goto done;
6289 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6290 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6291 a70480e0 2018-06-23 stsp } else {
6292 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6293 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6294 a70480e0 2018-06-23 stsp }
6295 a19e88aa 2018-06-23 stsp if (error != NULL)
6296 8b473291 2019-02-21 stsp goto done;
6297 8b473291 2019-02-21 stsp
6298 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6299 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6300 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6301 e1cd8fed 2018-08-01 stsp goto done;
6302 e1cd8fed 2018-08-01 stsp }
6303 0587e10c 2020-07-23 stsp
6304 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6305 945f9229 2022-04-16 thomas if (error)
6306 945f9229 2022-04-16 thomas goto done;
6307 945f9229 2022-04-16 thomas
6308 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6309 945f9229 2022-04-16 thomas commit, repo);
6310 7cbe629d 2018-08-04 stsp if (error)
6311 7cbe629d 2018-08-04 stsp goto done;
6312 0587e10c 2020-07-23 stsp
6313 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6314 78756c87 2020-11-24 stsp commit_id, repo);
6315 0587e10c 2020-07-23 stsp if (error)
6316 0587e10c 2020-07-23 stsp goto done;
6317 12314ad4 2019-08-31 stsp if (worktree) {
6318 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6319 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6320 12314ad4 2019-08-31 stsp worktree = NULL;
6321 12314ad4 2019-08-31 stsp }
6322 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6323 a70480e0 2018-06-23 stsp done:
6324 69069811 2018-08-02 stsp free(repo_path);
6325 f135c941 2020-02-20 stsp free(in_repo_path);
6326 0587e10c 2020-07-23 stsp free(link_target);
6327 69069811 2018-08-02 stsp free(cwd);
6328 a70480e0 2018-06-23 stsp free(commit_id);
6329 945f9229 2022-04-16 thomas if (commit)
6330 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6331 eb41ed75 2019-02-05 stsp if (worktree)
6332 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6333 1d0f4054 2021-06-17 stsp if (repo) {
6334 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6335 1d0f4054 2021-06-17 stsp if (error == NULL)
6336 1d0f4054 2021-06-17 stsp error = close_err;
6337 1d0f4054 2021-06-17 stsp }
6338 7cd52833 2022-06-23 thomas if (pack_fds) {
6339 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6340 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6341 7cd52833 2022-06-23 thomas if (error == NULL)
6342 7cd52833 2022-06-23 thomas error = pack_err;
6343 7cd52833 2022-06-23 thomas }
6344 51a10b52 2020-12-26 stsp tog_free_refs();
6345 a70480e0 2018-06-23 stsp return error;
6346 ffd1d5e5 2018-06-23 stsp }
6347 ffd1d5e5 2018-06-23 stsp
6348 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6349 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6350 ffd1d5e5 2018-06-23 stsp {
6351 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6352 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6353 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6354 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6355 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6356 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6357 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6358 ffd1d5e5 2018-06-23 stsp
6359 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6360 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6361 a5d43cac 2022-07-01 thomas --limit; /* border */
6362 ffd1d5e5 2018-06-23 stsp
6363 f7d12f7e 2018-08-01 stsp werase(view->window);
6364 ffd1d5e5 2018-06-23 stsp
6365 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6366 ffd1d5e5 2018-06-23 stsp return NULL;
6367 ffd1d5e5 2018-06-23 stsp
6368 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6369 f91a2b48 2022-06-23 thomas 0, 0);
6370 ffd1d5e5 2018-06-23 stsp if (err)
6371 ffd1d5e5 2018-06-23 stsp return err;
6372 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6373 a3404814 2018-09-02 stsp wstandout(view->window);
6374 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6375 11b20872 2019-11-08 stsp if (tc)
6376 11b20872 2019-11-08 stsp wattr_on(view->window,
6377 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6378 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6379 11b20872 2019-11-08 stsp if (tc)
6380 11b20872 2019-11-08 stsp wattr_off(view->window,
6381 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6382 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6383 a3404814 2018-09-02 stsp wstandend(view->window);
6384 2550e4c3 2018-07-13 stsp free(wline);
6385 2550e4c3 2018-07-13 stsp wline = NULL;
6386 07dd3ed3 2022-08-06 thomas
6387 6f5f393a 2022-08-12 thomas i += s->selected;
6388 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6389 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6390 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6391 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6392 07dd3ed3 2022-08-06 thomas }
6393 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6394 07dd3ed3 2022-08-06 thomas wprintw(view->window, " [%d/%d]", i,
6395 07dd3ed3 2022-08-06 thomas nentries + (s->tree == s->root ? 0 : 1)); /* ".." in !root tree */
6396 07dd3ed3 2022-08-06 thomas
6397 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6398 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6399 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6400 ffd1d5e5 2018-06-23 stsp return NULL;
6401 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6402 f91a2b48 2022-06-23 thomas 0, 0);
6403 ce52c690 2018-06-23 stsp if (err)
6404 ce52c690 2018-06-23 stsp return err;
6405 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6406 2550e4c3 2018-07-13 stsp free(wline);
6407 2550e4c3 2018-07-13 stsp wline = NULL;
6408 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6409 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6410 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6411 ffd1d5e5 2018-06-23 stsp return NULL;
6412 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6413 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6414 a1eca9bb 2018-06-23 stsp return NULL;
6415 ffd1d5e5 2018-06-23 stsp
6416 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6417 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6418 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6419 0cf4efb1 2018-09-29 stsp if (view->focussed)
6420 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6421 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6422 ffd1d5e5 2018-06-23 stsp }
6423 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6424 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6425 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6426 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6427 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6428 ffd1d5e5 2018-06-23 stsp return NULL;
6429 ffd1d5e5 2018-06-23 stsp n = 1;
6430 ffd1d5e5 2018-06-23 stsp } else {
6431 ffd1d5e5 2018-06-23 stsp n = 0;
6432 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6433 ffd1d5e5 2018-06-23 stsp }
6434 ffd1d5e5 2018-06-23 stsp
6435 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6436 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6437 848d6979 2019-08-12 stsp const char *modestr = "";
6438 56e0773d 2019-11-28 stsp mode_t mode;
6439 1d13200f 2018-07-12 stsp
6440 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6441 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6442 56e0773d 2019-11-28 stsp
6443 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6444 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6445 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6446 1d13200f 2018-07-12 stsp if (err)
6447 638f9024 2019-05-13 stsp return got_error_from_errno(
6448 230a42bd 2019-05-11 jcs "got_object_id_str");
6449 1d13200f 2018-07-12 stsp }
6450 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6451 63c5ca5d 2019-08-24 stsp modestr = "$";
6452 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6453 0d6c6ee3 2020-05-20 stsp int i;
6454 0d6c6ee3 2020-05-20 stsp
6455 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6456 d86d3b18 2020-12-01 naddy te, s->repo);
6457 0d6c6ee3 2020-05-20 stsp if (err) {
6458 0d6c6ee3 2020-05-20 stsp free(id_str);
6459 0d6c6ee3 2020-05-20 stsp return err;
6460 0d6c6ee3 2020-05-20 stsp }
6461 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6462 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6463 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6464 0d6c6ee3 2020-05-20 stsp }
6465 848d6979 2019-08-12 stsp modestr = "@";
6466 0d6c6ee3 2020-05-20 stsp }
6467 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6468 848d6979 2019-08-12 stsp modestr = "/";
6469 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6470 848d6979 2019-08-12 stsp modestr = "*";
6471 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6472 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6473 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6474 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6475 1d13200f 2018-07-12 stsp free(id_str);
6476 0d6c6ee3 2020-05-20 stsp free(link_target);
6477 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6478 1d13200f 2018-07-12 stsp }
6479 1d13200f 2018-07-12 stsp free(id_str);
6480 0d6c6ee3 2020-05-20 stsp free(link_target);
6481 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6482 f91a2b48 2022-06-23 thomas 0, 0);
6483 ffd1d5e5 2018-06-23 stsp if (err) {
6484 ffd1d5e5 2018-06-23 stsp free(line);
6485 ffd1d5e5 2018-06-23 stsp break;
6486 ffd1d5e5 2018-06-23 stsp }
6487 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6488 0cf4efb1 2018-09-29 stsp if (view->focussed)
6489 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6490 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6491 ffd1d5e5 2018-06-23 stsp }
6492 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6493 f26dddb7 2019-11-08 stsp if (tc)
6494 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6495 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6496 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6497 f26dddb7 2019-11-08 stsp if (tc)
6498 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6499 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6500 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6501 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6502 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6503 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6504 ffd1d5e5 2018-06-23 stsp free(line);
6505 2550e4c3 2018-07-13 stsp free(wline);
6506 2550e4c3 2018-07-13 stsp wline = NULL;
6507 ffd1d5e5 2018-06-23 stsp n++;
6508 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6509 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6510 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6511 ffd1d5e5 2018-06-23 stsp break;
6512 ffd1d5e5 2018-06-23 stsp }
6513 ffd1d5e5 2018-06-23 stsp
6514 ffd1d5e5 2018-06-23 stsp return err;
6515 ffd1d5e5 2018-06-23 stsp }
6516 ffd1d5e5 2018-06-23 stsp
6517 ffd1d5e5 2018-06-23 stsp static void
6518 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6519 ffd1d5e5 2018-06-23 stsp {
6520 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6521 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6522 fa86c4bf 2020-11-29 stsp int i = 0;
6523 ffd1d5e5 2018-06-23 stsp
6524 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6525 ffd1d5e5 2018-06-23 stsp return;
6526 ffd1d5e5 2018-06-23 stsp
6527 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6528 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6529 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6530 fa86c4bf 2020-11-29 stsp if (!isroot)
6531 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6532 fa86c4bf 2020-11-29 stsp break;
6533 fa86c4bf 2020-11-29 stsp }
6534 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6535 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6536 ffd1d5e5 2018-06-23 stsp }
6537 ffd1d5e5 2018-06-23 stsp }
6538 ffd1d5e5 2018-06-23 stsp
6539 a5d43cac 2022-07-01 thomas static const struct got_error *
6540 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6541 ffd1d5e5 2018-06-23 stsp {
6542 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6543 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6544 ffd1d5e5 2018-06-23 stsp int n = 0;
6545 ffd1d5e5 2018-06-23 stsp
6546 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6547 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6548 694d3271 2020-12-01 naddy s->first_displayed_entry);
6549 694d3271 2020-12-01 naddy else
6550 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6551 56e0773d 2019-11-28 stsp
6552 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6553 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6554 07dd3ed3 2022-08-06 thomas if (last) {
6555 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6556 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6557 07dd3ed3 2022-08-06 thomas }
6558 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6559 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6560 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6561 768394f3 2019-01-24 stsp }
6562 ffd1d5e5 2018-06-23 stsp }
6563 a5d43cac 2022-07-01 thomas
6564 a5d43cac 2022-07-01 thomas return NULL;
6565 ffd1d5e5 2018-06-23 stsp }
6566 ffd1d5e5 2018-06-23 stsp
6567 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6568 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6569 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6570 ffd1d5e5 2018-06-23 stsp {
6571 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6572 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6573 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6574 ffd1d5e5 2018-06-23 stsp
6575 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6576 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6577 56e0773d 2019-11-28 stsp + 1 /* slash */;
6578 ce52c690 2018-06-23 stsp if (te)
6579 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6580 ce52c690 2018-06-23 stsp
6581 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6582 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6583 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6584 ffd1d5e5 2018-06-23 stsp
6585 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6586 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6587 d9765a41 2018-06-23 stsp while (pt) {
6588 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6589 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6590 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6591 cb2ebc8a 2018-06-23 stsp goto done;
6592 cb2ebc8a 2018-06-23 stsp }
6593 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6594 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6595 cb2ebc8a 2018-06-23 stsp goto done;
6596 cb2ebc8a 2018-06-23 stsp }
6597 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6598 ffd1d5e5 2018-06-23 stsp }
6599 ce52c690 2018-06-23 stsp if (te) {
6600 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6601 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6602 ce52c690 2018-06-23 stsp goto done;
6603 ce52c690 2018-06-23 stsp }
6604 cb2ebc8a 2018-06-23 stsp }
6605 ce52c690 2018-06-23 stsp done:
6606 ce52c690 2018-06-23 stsp if (err) {
6607 ce52c690 2018-06-23 stsp free(*path);
6608 ce52c690 2018-06-23 stsp *path = NULL;
6609 ce52c690 2018-06-23 stsp }
6610 ce52c690 2018-06-23 stsp return err;
6611 ce52c690 2018-06-23 stsp }
6612 ce52c690 2018-06-23 stsp
6613 ce52c690 2018-06-23 stsp static const struct got_error *
6614 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6615 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6616 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6617 ce52c690 2018-06-23 stsp {
6618 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6619 ce52c690 2018-06-23 stsp char *path;
6620 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6621 a0de39f3 2019-08-09 stsp
6622 a0de39f3 2019-08-09 stsp *new_view = NULL;
6623 69efd4c4 2018-07-18 stsp
6624 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6625 ce52c690 2018-06-23 stsp if (err)
6626 ce52c690 2018-06-23 stsp return err;
6627 ffd1d5e5 2018-06-23 stsp
6628 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6629 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6630 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6631 83ce39e3 2019-08-12 stsp goto done;
6632 83ce39e3 2019-08-12 stsp }
6633 cdf1ee82 2018-08-01 stsp
6634 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6635 e5a0f69f 2018-08-18 stsp if (err) {
6636 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6637 fc06ba56 2019-08-22 stsp err = NULL;
6638 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6639 e5a0f69f 2018-08-18 stsp } else
6640 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6641 83ce39e3 2019-08-12 stsp done:
6642 83ce39e3 2019-08-12 stsp free(path);
6643 69efd4c4 2018-07-18 stsp return err;
6644 69efd4c4 2018-07-18 stsp }
6645 69efd4c4 2018-07-18 stsp
6646 69efd4c4 2018-07-18 stsp static const struct got_error *
6647 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6648 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6649 69efd4c4 2018-07-18 stsp {
6650 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6651 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6652 69efd4c4 2018-07-18 stsp char *path;
6653 a0de39f3 2019-08-09 stsp
6654 a0de39f3 2019-08-09 stsp *new_view = NULL;
6655 69efd4c4 2018-07-18 stsp
6656 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6657 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6658 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6659 e5a0f69f 2018-08-18 stsp
6660 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6661 69efd4c4 2018-07-18 stsp if (err)
6662 69efd4c4 2018-07-18 stsp return err;
6663 69efd4c4 2018-07-18 stsp
6664 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6665 4e97c21c 2020-12-06 stsp path, 0);
6666 ba4f502b 2018-08-04 stsp if (err)
6667 e5a0f69f 2018-08-18 stsp view_close(log_view);
6668 e5a0f69f 2018-08-18 stsp else
6669 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6670 cb2ebc8a 2018-06-23 stsp free(path);
6671 cb2ebc8a 2018-06-23 stsp return err;
6672 ffd1d5e5 2018-06-23 stsp }
6673 ffd1d5e5 2018-06-23 stsp
6674 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6675 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6676 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6677 ffd1d5e5 2018-06-23 stsp {
6678 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6679 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6680 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6681 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6682 ffd1d5e5 2018-06-23 stsp
6683 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6684 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6685 bc573f3b 2021-07-10 stsp
6686 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6687 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6688 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6689 ffd1d5e5 2018-06-23 stsp
6690 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6691 bc573f3b 2021-07-10 stsp if (err)
6692 bc573f3b 2021-07-10 stsp goto done;
6693 bc573f3b 2021-07-10 stsp
6694 bc573f3b 2021-07-10 stsp /*
6695 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6696 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6697 bc573f3b 2021-07-10 stsp * closed on demand.
6698 bc573f3b 2021-07-10 stsp */
6699 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6700 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6701 bc573f3b 2021-07-10 stsp if (err)
6702 bc573f3b 2021-07-10 stsp goto done;
6703 bc573f3b 2021-07-10 stsp s->tree = s->root;
6704 bc573f3b 2021-07-10 stsp
6705 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6706 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6707 ffd1d5e5 2018-06-23 stsp goto done;
6708 ffd1d5e5 2018-06-23 stsp
6709 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6710 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6711 ffd1d5e5 2018-06-23 stsp goto done;
6712 ffd1d5e5 2018-06-23 stsp }
6713 ffd1d5e5 2018-06-23 stsp
6714 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6715 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6716 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6717 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6718 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6719 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6720 9cd7cbd1 2020-12-07 stsp goto done;
6721 9cd7cbd1 2020-12-07 stsp }
6722 9cd7cbd1 2020-12-07 stsp }
6723 fb2756b9 2018-08-04 stsp s->repo = repo;
6724 c0b01bdb 2019-11-08 stsp
6725 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6726 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6727 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6728 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6729 c0b01bdb 2019-11-08 stsp if (err)
6730 c0b01bdb 2019-11-08 stsp goto done;
6731 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6732 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6733 bc573f3b 2021-07-10 stsp if (err)
6734 c0b01bdb 2019-11-08 stsp goto done;
6735 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6736 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6737 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6738 bc573f3b 2021-07-10 stsp if (err)
6739 c0b01bdb 2019-11-08 stsp goto done;
6740 e5a0f69f 2018-08-18 stsp
6741 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6742 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6743 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6744 bc573f3b 2021-07-10 stsp if (err)
6745 11b20872 2019-11-08 stsp goto done;
6746 11b20872 2019-11-08 stsp
6747 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6748 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6749 bc573f3b 2021-07-10 stsp if (err)
6750 c0b01bdb 2019-11-08 stsp goto done;
6751 c0b01bdb 2019-11-08 stsp }
6752 c0b01bdb 2019-11-08 stsp
6753 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6754 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6755 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6756 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6757 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6758 ad80ab7b 2018-08-04 stsp done:
6759 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6760 bc573f3b 2021-07-10 stsp if (commit)
6761 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6762 bc573f3b 2021-07-10 stsp if (err)
6763 bc573f3b 2021-07-10 stsp close_tree_view(view);
6764 ad80ab7b 2018-08-04 stsp return err;
6765 ad80ab7b 2018-08-04 stsp }
6766 ad80ab7b 2018-08-04 stsp
6767 e5a0f69f 2018-08-18 stsp static const struct got_error *
6768 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6769 ad80ab7b 2018-08-04 stsp {
6770 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6771 ad80ab7b 2018-08-04 stsp
6772 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6773 fb2756b9 2018-08-04 stsp free(s->tree_label);
6774 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6775 6484ec90 2018-09-29 stsp free(s->commit_id);
6776 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6777 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6778 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6779 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6780 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6781 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6782 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6783 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6784 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6785 ad80ab7b 2018-08-04 stsp free(parent);
6786 ad80ab7b 2018-08-04 stsp
6787 ad80ab7b 2018-08-04 stsp }
6788 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6789 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6790 bc573f3b 2021-07-10 stsp if (s->root)
6791 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6792 7c32bd05 2019-06-22 stsp return NULL;
6793 7c32bd05 2019-06-22 stsp }
6794 7c32bd05 2019-06-22 stsp
6795 7c32bd05 2019-06-22 stsp static const struct got_error *
6796 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6797 7c32bd05 2019-06-22 stsp {
6798 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6799 7c32bd05 2019-06-22 stsp
6800 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6801 7c32bd05 2019-06-22 stsp return NULL;
6802 7c32bd05 2019-06-22 stsp }
6803 7c32bd05 2019-06-22 stsp
6804 7c32bd05 2019-06-22 stsp static int
6805 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6806 7c32bd05 2019-06-22 stsp {
6807 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6808 7c32bd05 2019-06-22 stsp
6809 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6810 56e0773d 2019-11-28 stsp 0) == 0;
6811 7c32bd05 2019-06-22 stsp }
6812 7c32bd05 2019-06-22 stsp
6813 7c32bd05 2019-06-22 stsp static const struct got_error *
6814 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6815 7c32bd05 2019-06-22 stsp {
6816 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6817 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6818 7c32bd05 2019-06-22 stsp
6819 7c32bd05 2019-06-22 stsp if (!view->searching) {
6820 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6821 7c32bd05 2019-06-22 stsp return NULL;
6822 7c32bd05 2019-06-22 stsp }
6823 7c32bd05 2019-06-22 stsp
6824 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6825 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6826 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6827 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6828 56e0773d 2019-11-28 stsp s->selected_entry);
6829 7c32bd05 2019-06-22 stsp else
6830 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6831 56e0773d 2019-11-28 stsp } else {
6832 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6833 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6834 56e0773d 2019-11-28 stsp else
6835 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6836 56e0773d 2019-11-28 stsp s->selected_entry);
6837 7c32bd05 2019-06-22 stsp }
6838 7c32bd05 2019-06-22 stsp } else {
6839 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6840 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6841 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6842 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6843 56e0773d 2019-11-28 stsp else
6844 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6845 7c32bd05 2019-06-22 stsp }
6846 7c32bd05 2019-06-22 stsp
6847 7c32bd05 2019-06-22 stsp while (1) {
6848 56e0773d 2019-11-28 stsp if (te == NULL) {
6849 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6850 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6851 ac66afb8 2019-06-24 stsp return NULL;
6852 ac66afb8 2019-06-24 stsp }
6853 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6854 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6855 56e0773d 2019-11-28 stsp else
6856 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6857 7c32bd05 2019-06-22 stsp }
6858 7c32bd05 2019-06-22 stsp
6859 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6860 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6861 56e0773d 2019-11-28 stsp s->matched_entry = te;
6862 7c32bd05 2019-06-22 stsp break;
6863 7c32bd05 2019-06-22 stsp }
6864 7c32bd05 2019-06-22 stsp
6865 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6866 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6867 56e0773d 2019-11-28 stsp else
6868 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6869 7c32bd05 2019-06-22 stsp }
6870 e5a0f69f 2018-08-18 stsp
6871 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6872 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6873 7c32bd05 2019-06-22 stsp s->selected = 0;
6874 7c32bd05 2019-06-22 stsp }
6875 7c32bd05 2019-06-22 stsp
6876 e5a0f69f 2018-08-18 stsp return NULL;
6877 ad80ab7b 2018-08-04 stsp }
6878 ad80ab7b 2018-08-04 stsp
6879 ad80ab7b 2018-08-04 stsp static const struct got_error *
6880 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6881 ad80ab7b 2018-08-04 stsp {
6882 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6883 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6884 e5a0f69f 2018-08-18 stsp char *parent_path;
6885 ad80ab7b 2018-08-04 stsp
6886 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6887 e5a0f69f 2018-08-18 stsp if (err)
6888 e5a0f69f 2018-08-18 stsp return err;
6889 ffd1d5e5 2018-06-23 stsp
6890 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6891 e5a0f69f 2018-08-18 stsp free(parent_path);
6892 669b5ffa 2018-10-07 stsp
6893 a5d43cac 2022-07-01 thomas view_border(view);
6894 e5a0f69f 2018-08-18 stsp return err;
6895 07dd3ed3 2022-08-06 thomas }
6896 07dd3ed3 2022-08-06 thomas
6897 07dd3ed3 2022-08-06 thomas static const struct got_error *
6898 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
6899 07dd3ed3 2022-08-06 thomas {
6900 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
6901 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
6902 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
6903 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
6904 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
6905 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
6906 07dd3ed3 2022-08-06 thomas
6907 07dd3ed3 2022-08-06 thomas g = view->gline;
6908 07dd3ed3 2022-08-06 thomas view->gline = 0;
6909 07dd3ed3 2022-08-06 thomas
6910 07dd3ed3 2022-08-06 thomas if (g == 0)
6911 07dd3ed3 2022-08-06 thomas g = 1;
6912 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
6913 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6914 07dd3ed3 2022-08-06 thomas
6915 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
6916 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
6917 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
6918 07dd3ed3 2022-08-06 thomas
6919 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
6920 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6921 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
6922 07dd3ed3 2022-08-06 thomas }
6923 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
6924 07dd3ed3 2022-08-06 thomas last += off;
6925 07dd3ed3 2022-08-06 thomas
6926 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
6927 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6928 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
6929 07dd3ed3 2022-08-06 thomas }
6930 07dd3ed3 2022-08-06 thomas
6931 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
6932 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
6933 07dd3ed3 2022-08-06 thomas i += off;
6934 07dd3ed3 2022-08-06 thomas }
6935 07dd3ed3 2022-08-06 thomas
6936 07dd3ed3 2022-08-06 thomas if (i < g) {
6937 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
6938 07dd3ed3 2022-08-06 thomas if (err)
6939 07dd3ed3 2022-08-06 thomas return err;
6940 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
6941 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
6942 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
6943 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
6944 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6945 07dd3ed3 2022-08-06 thomas first += off;
6946 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6947 07dd3ed3 2022-08-06 thomas }
6948 07dd3ed3 2022-08-06 thomas } else if (i > g)
6949 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
6950 07dd3ed3 2022-08-06 thomas
6951 07dd3ed3 2022-08-06 thomas if (g < nlines &&
6952 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6953 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
6954 07dd3ed3 2022-08-06 thomas
6955 07dd3ed3 2022-08-06 thomas return NULL;
6956 e5a0f69f 2018-08-18 stsp }
6957 ce52c690 2018-06-23 stsp
6958 e5a0f69f 2018-08-18 stsp static const struct got_error *
6959 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6960 e5a0f69f 2018-08-18 stsp {
6961 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6962 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6963 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6964 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
6965 ffd1d5e5 2018-06-23 stsp
6966 07dd3ed3 2022-08-06 thomas if (view->gline)
6967 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
6968 07dd3ed3 2022-08-06 thomas
6969 e5a0f69f 2018-08-18 stsp switch (ch) {
6970 1e37a5c2 2019-05-12 jcs case 'i':
6971 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6972 07b0611c 2022-06-23 thomas view->count = 0;
6973 1e37a5c2 2019-05-12 jcs break;
6974 1be4947a 2022-07-22 thomas case 'L':
6975 07b0611c 2022-06-23 thomas view->count = 0;
6976 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6977 ffd1d5e5 2018-06-23 stsp break;
6978 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6979 152c1c93 2020-11-29 stsp break;
6980 1be4947a 2022-07-22 thomas case 'R':
6981 07b0611c 2022-06-23 thomas view->count = 0;
6982 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
6983 e4526bf5 2021-09-03 naddy break;
6984 e4526bf5 2021-09-03 naddy case 'g':
6985 e4526bf5 2021-09-03 naddy case KEY_HOME:
6986 e4526bf5 2021-09-03 naddy s->selected = 0;
6987 07b0611c 2022-06-23 thomas view->count = 0;
6988 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6989 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6990 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6991 e4526bf5 2021-09-03 naddy else
6992 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6993 1e37a5c2 2019-05-12 jcs break;
6994 e4526bf5 2021-09-03 naddy case 'G':
6995 a5d43cac 2022-07-01 thomas case KEY_END: {
6996 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6997 a5d43cac 2022-07-01 thomas
6998 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6999 a5d43cac 2022-07-01 thomas --eos; /* border */
7000 e4526bf5 2021-09-03 naddy s->selected = 0;
7001 07b0611c 2022-06-23 thomas view->count = 0;
7002 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7003 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7004 e4526bf5 2021-09-03 naddy if (te == NULL) {
7005 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7006 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7007 e4526bf5 2021-09-03 naddy n++;
7008 e4526bf5 2021-09-03 naddy }
7009 e4526bf5 2021-09-03 naddy break;
7010 e4526bf5 2021-09-03 naddy }
7011 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7012 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7013 e4526bf5 2021-09-03 naddy }
7014 e4526bf5 2021-09-03 naddy if (n > 0)
7015 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7016 e4526bf5 2021-09-03 naddy break;
7017 a5d43cac 2022-07-01 thomas }
7018 1e37a5c2 2019-05-12 jcs case 'k':
7019 1e37a5c2 2019-05-12 jcs case KEY_UP:
7020 f7140bf5 2021-10-17 thomas case CTRL('p'):
7021 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7022 1e37a5c2 2019-05-12 jcs s->selected--;
7023 fa86c4bf 2020-11-29 stsp break;
7024 1e37a5c2 2019-05-12 jcs }
7025 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7026 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7027 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7028 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7029 07b0611c 2022-06-23 thomas view->count = 0;
7030 1e37a5c2 2019-05-12 jcs break;
7031 70f17a53 2022-06-13 thomas case CTRL('u'):
7032 23427b14 2022-06-23 thomas case 'u':
7033 70f17a53 2022-06-13 thomas nscroll /= 2;
7034 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7035 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7036 ea025d1d 2020-02-22 naddy case CTRL('b'):
7037 1c5e5faa 2022-06-23 thomas case 'b':
7038 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7039 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7040 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7041 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7042 fa86c4bf 2020-11-29 stsp } else {
7043 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7044 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7045 fa86c4bf 2020-11-29 stsp }
7046 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7047 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7048 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7049 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7050 07b0611c 2022-06-23 thomas view->count = 0;
7051 1e37a5c2 2019-05-12 jcs break;
7052 1e37a5c2 2019-05-12 jcs case 'j':
7053 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7054 f7140bf5 2021-10-17 thomas case CTRL('n'):
7055 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7056 1e37a5c2 2019-05-12 jcs s->selected++;
7057 1e37a5c2 2019-05-12 jcs break;
7058 1e37a5c2 2019-05-12 jcs }
7059 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7060 07b0611c 2022-06-23 thomas == NULL) {
7061 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7062 07b0611c 2022-06-23 thomas view->count = 0;
7063 1e37a5c2 2019-05-12 jcs break;
7064 07b0611c 2022-06-23 thomas }
7065 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7066 1e37a5c2 2019-05-12 jcs break;
7067 70f17a53 2022-06-13 thomas case CTRL('d'):
7068 23427b14 2022-06-23 thomas case 'd':
7069 70f17a53 2022-06-13 thomas nscroll /= 2;
7070 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7071 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7072 ea025d1d 2020-02-22 naddy case CTRL('f'):
7073 1c5e5faa 2022-06-23 thomas case 'f':
7074 4c2d69cb 2022-06-23 thomas case ' ':
7075 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7076 1e37a5c2 2019-05-12 jcs == NULL) {
7077 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7078 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7079 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7080 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7081 07b0611c 2022-06-23 thomas else
7082 07b0611c 2022-06-23 thomas view->count = 0;
7083 1e37a5c2 2019-05-12 jcs break;
7084 1e37a5c2 2019-05-12 jcs }
7085 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7086 1e37a5c2 2019-05-12 jcs break;
7087 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7088 1e37a5c2 2019-05-12 jcs case '\r':
7089 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7090 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7091 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7092 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7093 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7094 07b0611c 2022-06-23 thomas view->count = 0;
7095 1e37a5c2 2019-05-12 jcs break;
7096 07b0611c 2022-06-23 thomas }
7097 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7098 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7099 1e37a5c2 2019-05-12 jcs entry);
7100 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7101 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7102 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7103 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7104 1e37a5c2 2019-05-12 jcs s->selected_entry =
7105 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7106 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7107 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7108 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7109 a5d43cac 2022-07-01 thomas if (err)
7110 a5d43cac 2022-07-01 thomas break;
7111 a5d43cac 2022-07-01 thomas }
7112 1e37a5c2 2019-05-12 jcs free(parent);
7113 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7114 56e0773d 2019-11-28 stsp s->selected_entry))) {
7115 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7116 07b0611c 2022-06-23 thomas view->count = 0;
7117 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7118 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7119 1e37a5c2 2019-05-12 jcs if (err)
7120 1e37a5c2 2019-05-12 jcs break;
7121 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7122 941e9f74 2019-05-21 stsp if (err) {
7123 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7124 1e37a5c2 2019-05-12 jcs break;
7125 1e37a5c2 2019-05-12 jcs }
7126 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7127 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7128 1e37a5c2 2019-05-12 jcs break;
7129 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7130 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7131 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7132 07b0611c 2022-06-23 thomas view->count = 0;
7133 1e37a5c2 2019-05-12 jcs break;
7134 1e37a5c2 2019-05-12 jcs default:
7135 07b0611c 2022-06-23 thomas view->count = 0;
7136 1e37a5c2 2019-05-12 jcs break;
7137 ffd1d5e5 2018-06-23 stsp }
7138 e5a0f69f 2018-08-18 stsp
7139 ffd1d5e5 2018-06-23 stsp return err;
7140 9f7d7167 2018-04-29 stsp }
7141 9f7d7167 2018-04-29 stsp
7142 ffd1d5e5 2018-06-23 stsp __dead static void
7143 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7144 ffd1d5e5 2018-06-23 stsp {
7145 ffd1d5e5 2018-06-23 stsp endwin();
7146 91198554 2022-06-23 thomas fprintf(stderr,
7147 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7148 ffd1d5e5 2018-06-23 stsp getprogname());
7149 ffd1d5e5 2018-06-23 stsp exit(1);
7150 ffd1d5e5 2018-06-23 stsp }
7151 ffd1d5e5 2018-06-23 stsp
7152 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7153 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7154 ffd1d5e5 2018-06-23 stsp {
7155 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7156 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7157 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7158 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7159 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7160 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7161 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7162 4e97c21c 2020-12-06 stsp char *label = NULL;
7163 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7164 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7165 ffd1d5e5 2018-06-23 stsp int ch;
7166 5221c383 2018-08-01 stsp struct tog_view *view;
7167 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7168 70ac5f84 2019-03-28 stsp
7169 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7170 ffd1d5e5 2018-06-23 stsp switch (ch) {
7171 ffd1d5e5 2018-06-23 stsp case 'c':
7172 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7173 74283ab8 2020-02-07 stsp break;
7174 74283ab8 2020-02-07 stsp case 'r':
7175 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7176 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7177 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7178 74283ab8 2020-02-07 stsp optarg);
7179 ffd1d5e5 2018-06-23 stsp break;
7180 ffd1d5e5 2018-06-23 stsp default:
7181 e99e2d15 2020-11-24 naddy usage_tree();
7182 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7183 ffd1d5e5 2018-06-23 stsp }
7184 ffd1d5e5 2018-06-23 stsp }
7185 ffd1d5e5 2018-06-23 stsp
7186 ffd1d5e5 2018-06-23 stsp argc -= optind;
7187 ffd1d5e5 2018-06-23 stsp argv += optind;
7188 ffd1d5e5 2018-06-23 stsp
7189 55cccc34 2020-02-20 stsp if (argc > 1)
7190 e99e2d15 2020-11-24 naddy usage_tree();
7191 7cd52833 2022-06-23 thomas
7192 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7193 7cd52833 2022-06-23 thomas if (error != NULL)
7194 7cd52833 2022-06-23 thomas goto done;
7195 74283ab8 2020-02-07 stsp
7196 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7197 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7198 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7199 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7200 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7201 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7202 c156c7a4 2020-12-18 stsp goto done;
7203 55cccc34 2020-02-20 stsp if (worktree)
7204 52185f70 2019-02-05 stsp repo_path =
7205 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7206 55cccc34 2020-02-20 stsp else
7207 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7208 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7209 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7210 c156c7a4 2020-12-18 stsp goto done;
7211 c156c7a4 2020-12-18 stsp }
7212 55cccc34 2020-02-20 stsp }
7213 a915003a 2019-02-05 stsp
7214 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7215 c02c541e 2019-03-29 stsp if (error != NULL)
7216 52185f70 2019-02-05 stsp goto done;
7217 d188b9a6 2019-01-04 stsp
7218 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7219 55cccc34 2020-02-20 stsp repo, worktree);
7220 55cccc34 2020-02-20 stsp if (error)
7221 55cccc34 2020-02-20 stsp goto done;
7222 55cccc34 2020-02-20 stsp
7223 55cccc34 2020-02-20 stsp init_curses();
7224 55cccc34 2020-02-20 stsp
7225 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7226 c02c541e 2019-03-29 stsp if (error)
7227 52185f70 2019-02-05 stsp goto done;
7228 ffd1d5e5 2018-06-23 stsp
7229 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7230 51a10b52 2020-12-26 stsp if (error)
7231 51a10b52 2020-12-26 stsp goto done;
7232 51a10b52 2020-12-26 stsp
7233 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7234 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7235 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7236 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7237 4e97c21c 2020-12-06 stsp if (error)
7238 4e97c21c 2020-12-06 stsp goto done;
7239 4e97c21c 2020-12-06 stsp head_ref_name = label;
7240 4e97c21c 2020-12-06 stsp } else {
7241 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7242 4e97c21c 2020-12-06 stsp if (error == NULL)
7243 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7244 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7245 4e97c21c 2020-12-06 stsp goto done;
7246 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7247 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7248 4e97c21c 2020-12-06 stsp if (error)
7249 4e97c21c 2020-12-06 stsp goto done;
7250 4e97c21c 2020-12-06 stsp }
7251 ffd1d5e5 2018-06-23 stsp
7252 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7253 945f9229 2022-04-16 thomas if (error)
7254 945f9229 2022-04-16 thomas goto done;
7255 945f9229 2022-04-16 thomas
7256 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7257 5221c383 2018-08-01 stsp if (view == NULL) {
7258 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7259 5221c383 2018-08-01 stsp goto done;
7260 5221c383 2018-08-01 stsp }
7261 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7262 ad80ab7b 2018-08-04 stsp if (error)
7263 ad80ab7b 2018-08-04 stsp goto done;
7264 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7265 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7266 d91faf3b 2020-12-01 naddy in_repo_path);
7267 55cccc34 2020-02-20 stsp if (error)
7268 55cccc34 2020-02-20 stsp goto done;
7269 55cccc34 2020-02-20 stsp }
7270 55cccc34 2020-02-20 stsp
7271 55cccc34 2020-02-20 stsp if (worktree) {
7272 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7273 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7274 55cccc34 2020-02-20 stsp worktree = NULL;
7275 55cccc34 2020-02-20 stsp }
7276 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7277 ffd1d5e5 2018-06-23 stsp done:
7278 52185f70 2019-02-05 stsp free(repo_path);
7279 e4a0e26d 2020-02-20 stsp free(cwd);
7280 ffd1d5e5 2018-06-23 stsp free(commit_id);
7281 4e97c21c 2020-12-06 stsp free(label);
7282 486cd271 2020-12-06 stsp if (ref)
7283 486cd271 2020-12-06 stsp got_ref_close(ref);
7284 1d0f4054 2021-06-17 stsp if (repo) {
7285 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7286 1d0f4054 2021-06-17 stsp if (error == NULL)
7287 1d0f4054 2021-06-17 stsp error = close_err;
7288 1d0f4054 2021-06-17 stsp }
7289 7cd52833 2022-06-23 thomas if (pack_fds) {
7290 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7291 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7292 7cd52833 2022-06-23 thomas if (error == NULL)
7293 7cd52833 2022-06-23 thomas error = pack_err;
7294 7cd52833 2022-06-23 thomas }
7295 51a10b52 2020-12-26 stsp tog_free_refs();
7296 ffd1d5e5 2018-06-23 stsp return error;
7297 6458efa5 2020-11-24 stsp }
7298 6458efa5 2020-11-24 stsp
7299 6458efa5 2020-11-24 stsp static const struct got_error *
7300 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7301 6458efa5 2020-11-24 stsp {
7302 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7303 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7304 6458efa5 2020-11-24 stsp
7305 6458efa5 2020-11-24 stsp s->nrefs = 0;
7306 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7307 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7308 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7309 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7310 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7311 6458efa5 2020-11-24 stsp continue;
7312 6458efa5 2020-11-24 stsp
7313 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7314 6458efa5 2020-11-24 stsp if (re == NULL)
7315 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7316 6458efa5 2020-11-24 stsp
7317 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7318 8924d611 2020-12-26 stsp if (re->ref == NULL)
7319 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7320 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7321 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7322 6458efa5 2020-11-24 stsp }
7323 6458efa5 2020-11-24 stsp
7324 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7325 6458efa5 2020-11-24 stsp return NULL;
7326 6458efa5 2020-11-24 stsp }
7327 6458efa5 2020-11-24 stsp
7328 ef20f542 2022-06-26 thomas static void
7329 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7330 6458efa5 2020-11-24 stsp {
7331 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7332 6458efa5 2020-11-24 stsp
7333 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7334 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7335 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7336 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7337 6458efa5 2020-11-24 stsp free(re);
7338 6458efa5 2020-11-24 stsp }
7339 6458efa5 2020-11-24 stsp }
7340 6458efa5 2020-11-24 stsp
7341 6458efa5 2020-11-24 stsp static const struct got_error *
7342 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7343 6458efa5 2020-11-24 stsp {
7344 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7345 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7346 6458efa5 2020-11-24 stsp
7347 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7348 6458efa5 2020-11-24 stsp s->repo = repo;
7349 6458efa5 2020-11-24 stsp
7350 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7351 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7352 6458efa5 2020-11-24 stsp
7353 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7354 6458efa5 2020-11-24 stsp if (err)
7355 6458efa5 2020-11-24 stsp return err;
7356 34ba6917 2020-11-30 stsp
7357 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7358 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7359 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7360 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7361 6458efa5 2020-11-24 stsp if (err)
7362 6458efa5 2020-11-24 stsp goto done;
7363 6458efa5 2020-11-24 stsp
7364 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7365 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7366 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7367 6458efa5 2020-11-24 stsp if (err)
7368 6458efa5 2020-11-24 stsp goto done;
7369 6458efa5 2020-11-24 stsp
7370 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7371 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7372 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7373 2183bbf6 2022-01-23 thomas if (err)
7374 2183bbf6 2022-01-23 thomas goto done;
7375 2183bbf6 2022-01-23 thomas
7376 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7377 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7378 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7379 6458efa5 2020-11-24 stsp if (err)
7380 6458efa5 2020-11-24 stsp goto done;
7381 6458efa5 2020-11-24 stsp }
7382 6458efa5 2020-11-24 stsp
7383 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7384 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7385 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7386 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7387 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7388 6458efa5 2020-11-24 stsp done:
7389 6458efa5 2020-11-24 stsp if (err)
7390 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7391 6458efa5 2020-11-24 stsp return err;
7392 6458efa5 2020-11-24 stsp }
7393 6458efa5 2020-11-24 stsp
7394 6458efa5 2020-11-24 stsp static const struct got_error *
7395 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7396 6458efa5 2020-11-24 stsp {
7397 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7398 6458efa5 2020-11-24 stsp
7399 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7400 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7401 6458efa5 2020-11-24 stsp
7402 6458efa5 2020-11-24 stsp return NULL;
7403 9f7d7167 2018-04-29 stsp }
7404 ce5b7c56 2019-07-09 stsp
7405 6458efa5 2020-11-24 stsp static const struct got_error *
7406 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7407 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7408 6458efa5 2020-11-24 stsp {
7409 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7410 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7411 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7412 6458efa5 2020-11-24 stsp int obj_type;
7413 6458efa5 2020-11-24 stsp
7414 c42c9805 2020-11-24 stsp *commit_id = NULL;
7415 6458efa5 2020-11-24 stsp
7416 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7417 6458efa5 2020-11-24 stsp if (err)
7418 6458efa5 2020-11-24 stsp return err;
7419 6458efa5 2020-11-24 stsp
7420 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7421 6458efa5 2020-11-24 stsp if (err)
7422 6458efa5 2020-11-24 stsp goto done;
7423 6458efa5 2020-11-24 stsp
7424 6458efa5 2020-11-24 stsp switch (obj_type) {
7425 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7426 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7427 6458efa5 2020-11-24 stsp break;
7428 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7429 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7430 6458efa5 2020-11-24 stsp if (err)
7431 6458efa5 2020-11-24 stsp goto done;
7432 c42c9805 2020-11-24 stsp free(obj_id);
7433 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7434 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7435 c42c9805 2020-11-24 stsp if (err)
7436 6458efa5 2020-11-24 stsp goto done;
7437 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7438 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7439 c42c9805 2020-11-24 stsp goto done;
7440 c42c9805 2020-11-24 stsp }
7441 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7442 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7443 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7444 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7445 c42c9805 2020-11-24 stsp goto done;
7446 c42c9805 2020-11-24 stsp }
7447 6458efa5 2020-11-24 stsp break;
7448 6458efa5 2020-11-24 stsp default:
7449 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7450 c42c9805 2020-11-24 stsp break;
7451 6458efa5 2020-11-24 stsp }
7452 6458efa5 2020-11-24 stsp
7453 c42c9805 2020-11-24 stsp done:
7454 c42c9805 2020-11-24 stsp if (tag)
7455 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7456 c42c9805 2020-11-24 stsp if (err) {
7457 c42c9805 2020-11-24 stsp free(*commit_id);
7458 c42c9805 2020-11-24 stsp *commit_id = NULL;
7459 c42c9805 2020-11-24 stsp }
7460 c42c9805 2020-11-24 stsp return err;
7461 c42c9805 2020-11-24 stsp }
7462 c42c9805 2020-11-24 stsp
7463 c42c9805 2020-11-24 stsp static const struct got_error *
7464 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7465 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7466 c42c9805 2020-11-24 stsp {
7467 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7468 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7469 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7470 c42c9805 2020-11-24 stsp
7471 c42c9805 2020-11-24 stsp *new_view = NULL;
7472 c42c9805 2020-11-24 stsp
7473 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7474 c42c9805 2020-11-24 stsp if (err) {
7475 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7476 c42c9805 2020-11-24 stsp return err;
7477 c42c9805 2020-11-24 stsp else
7478 c42c9805 2020-11-24 stsp return NULL;
7479 c42c9805 2020-11-24 stsp }
7480 c42c9805 2020-11-24 stsp
7481 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7482 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7483 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7484 6458efa5 2020-11-24 stsp goto done;
7485 6458efa5 2020-11-24 stsp }
7486 6458efa5 2020-11-24 stsp
7487 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7488 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7489 6458efa5 2020-11-24 stsp done:
7490 6458efa5 2020-11-24 stsp if (err)
7491 6458efa5 2020-11-24 stsp view_close(log_view);
7492 6458efa5 2020-11-24 stsp else
7493 6458efa5 2020-11-24 stsp *new_view = log_view;
7494 c42c9805 2020-11-24 stsp free(commit_id);
7495 6458efa5 2020-11-24 stsp return err;
7496 6458efa5 2020-11-24 stsp }
7497 6458efa5 2020-11-24 stsp
7498 ce5b7c56 2019-07-09 stsp static void
7499 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7500 6458efa5 2020-11-24 stsp {
7501 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7502 34ba6917 2020-11-30 stsp int i = 0;
7503 6458efa5 2020-11-24 stsp
7504 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7505 6458efa5 2020-11-24 stsp return;
7506 6458efa5 2020-11-24 stsp
7507 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7508 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7509 34ba6917 2020-11-30 stsp if (re == NULL)
7510 34ba6917 2020-11-30 stsp break;
7511 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7512 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7513 6458efa5 2020-11-24 stsp }
7514 6458efa5 2020-11-24 stsp }
7515 6458efa5 2020-11-24 stsp
7516 a5d43cac 2022-07-01 thomas static const struct got_error *
7517 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7518 6458efa5 2020-11-24 stsp {
7519 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7520 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7521 6458efa5 2020-11-24 stsp int n = 0;
7522 6458efa5 2020-11-24 stsp
7523 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7524 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7525 6458efa5 2020-11-24 stsp else
7526 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7527 6458efa5 2020-11-24 stsp
7528 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7529 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7530 07dd3ed3 2022-08-06 thomas if (last) {
7531 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7532 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7533 07dd3ed3 2022-08-06 thomas }
7534 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7535 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7536 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7537 6458efa5 2020-11-24 stsp }
7538 6458efa5 2020-11-24 stsp }
7539 a5d43cac 2022-07-01 thomas
7540 a5d43cac 2022-07-01 thomas return NULL;
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 search_start_ref_view(struct tog_view *view)
7545 6458efa5 2020-11-24 stsp {
7546 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7547 6458efa5 2020-11-24 stsp
7548 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7549 6458efa5 2020-11-24 stsp return NULL;
7550 6458efa5 2020-11-24 stsp }
7551 6458efa5 2020-11-24 stsp
7552 6458efa5 2020-11-24 stsp static int
7553 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7554 6458efa5 2020-11-24 stsp {
7555 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7556 6458efa5 2020-11-24 stsp
7557 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7558 6458efa5 2020-11-24 stsp 0) == 0;
7559 6458efa5 2020-11-24 stsp }
7560 6458efa5 2020-11-24 stsp
7561 6458efa5 2020-11-24 stsp static const struct got_error *
7562 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7563 6458efa5 2020-11-24 stsp {
7564 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7565 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7566 6458efa5 2020-11-24 stsp
7567 6458efa5 2020-11-24 stsp if (!view->searching) {
7568 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7569 6458efa5 2020-11-24 stsp return NULL;
7570 6458efa5 2020-11-24 stsp }
7571 6458efa5 2020-11-24 stsp
7572 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7573 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7574 6458efa5 2020-11-24 stsp if (s->selected_entry)
7575 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7576 6458efa5 2020-11-24 stsp else
7577 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7578 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7579 6458efa5 2020-11-24 stsp } else {
7580 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7581 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7582 6458efa5 2020-11-24 stsp else
7583 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7584 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7585 6458efa5 2020-11-24 stsp }
7586 6458efa5 2020-11-24 stsp } else {
7587 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7588 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7589 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7590 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7591 6458efa5 2020-11-24 stsp else
7592 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7593 6458efa5 2020-11-24 stsp }
7594 6458efa5 2020-11-24 stsp
7595 6458efa5 2020-11-24 stsp while (1) {
7596 6458efa5 2020-11-24 stsp if (re == NULL) {
7597 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7598 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7599 6458efa5 2020-11-24 stsp return NULL;
7600 6458efa5 2020-11-24 stsp }
7601 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7602 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7603 6458efa5 2020-11-24 stsp else
7604 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7605 6458efa5 2020-11-24 stsp }
7606 6458efa5 2020-11-24 stsp
7607 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7608 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7609 6458efa5 2020-11-24 stsp s->matched_entry = re;
7610 6458efa5 2020-11-24 stsp break;
7611 6458efa5 2020-11-24 stsp }
7612 6458efa5 2020-11-24 stsp
7613 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7614 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7615 6458efa5 2020-11-24 stsp else
7616 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7617 6458efa5 2020-11-24 stsp }
7618 6458efa5 2020-11-24 stsp
7619 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7620 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7621 6458efa5 2020-11-24 stsp s->selected = 0;
7622 6458efa5 2020-11-24 stsp }
7623 6458efa5 2020-11-24 stsp
7624 6458efa5 2020-11-24 stsp return NULL;
7625 6458efa5 2020-11-24 stsp }
7626 6458efa5 2020-11-24 stsp
7627 6458efa5 2020-11-24 stsp static const struct got_error *
7628 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7629 6458efa5 2020-11-24 stsp {
7630 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7631 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7632 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7633 6458efa5 2020-11-24 stsp char *line = NULL;
7634 6458efa5 2020-11-24 stsp wchar_t *wline;
7635 6458efa5 2020-11-24 stsp struct tog_color *tc;
7636 6458efa5 2020-11-24 stsp int width, n;
7637 6458efa5 2020-11-24 stsp int limit = view->nlines;
7638 6458efa5 2020-11-24 stsp
7639 6458efa5 2020-11-24 stsp werase(view->window);
7640 6458efa5 2020-11-24 stsp
7641 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7642 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7643 a5d43cac 2022-07-01 thomas --limit; /* border */
7644 6458efa5 2020-11-24 stsp
7645 6458efa5 2020-11-24 stsp if (limit == 0)
7646 6458efa5 2020-11-24 stsp return NULL;
7647 6458efa5 2020-11-24 stsp
7648 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7649 6458efa5 2020-11-24 stsp
7650 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7651 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7652 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7653 6458efa5 2020-11-24 stsp
7654 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7655 6458efa5 2020-11-24 stsp if (err) {
7656 6458efa5 2020-11-24 stsp free(line);
7657 6458efa5 2020-11-24 stsp return err;
7658 6458efa5 2020-11-24 stsp }
7659 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7660 6458efa5 2020-11-24 stsp wstandout(view->window);
7661 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7662 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7663 6458efa5 2020-11-24 stsp wstandend(view->window);
7664 6458efa5 2020-11-24 stsp free(wline);
7665 6458efa5 2020-11-24 stsp wline = NULL;
7666 6458efa5 2020-11-24 stsp free(line);
7667 6458efa5 2020-11-24 stsp line = NULL;
7668 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7669 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7670 6458efa5 2020-11-24 stsp if (--limit <= 0)
7671 6458efa5 2020-11-24 stsp return NULL;
7672 6458efa5 2020-11-24 stsp
7673 6458efa5 2020-11-24 stsp n = 0;
7674 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7675 6458efa5 2020-11-24 stsp char *line = NULL;
7676 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7677 6458efa5 2020-11-24 stsp
7678 84227eb1 2022-06-23 thomas if (s->show_date) {
7679 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7680 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7681 84227eb1 2022-06-23 thomas struct got_object_id *id;
7682 84227eb1 2022-06-23 thomas struct tm tm;
7683 84227eb1 2022-06-23 thomas time_t t;
7684 84227eb1 2022-06-23 thomas
7685 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7686 84227eb1 2022-06-23 thomas if (err)
7687 84227eb1 2022-06-23 thomas return err;
7688 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7689 84227eb1 2022-06-23 thomas if (err) {
7690 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7691 84227eb1 2022-06-23 thomas free(id);
7692 84227eb1 2022-06-23 thomas return err;
7693 84227eb1 2022-06-23 thomas }
7694 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7695 84227eb1 2022-06-23 thomas id);
7696 84227eb1 2022-06-23 thomas if (err) {
7697 84227eb1 2022-06-23 thomas free(id);
7698 84227eb1 2022-06-23 thomas return err;
7699 84227eb1 2022-06-23 thomas }
7700 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7701 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7702 84227eb1 2022-06-23 thomas } else {
7703 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7704 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7705 84227eb1 2022-06-23 thomas }
7706 84227eb1 2022-06-23 thomas free(id);
7707 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7708 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7709 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7710 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7711 84227eb1 2022-06-23 thomas }
7712 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7713 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7714 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7715 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7716 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7717 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7718 6458efa5 2020-11-24 stsp struct got_object_id *id;
7719 6458efa5 2020-11-24 stsp char *id_str;
7720 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7721 6458efa5 2020-11-24 stsp if (err)
7722 6458efa5 2020-11-24 stsp return err;
7723 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7724 6458efa5 2020-11-24 stsp if (err) {
7725 6458efa5 2020-11-24 stsp free(id);
7726 6458efa5 2020-11-24 stsp return err;
7727 6458efa5 2020-11-24 stsp }
7728 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7729 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7730 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7731 6458efa5 2020-11-24 stsp free(id);
7732 6458efa5 2020-11-24 stsp free(id_str);
7733 6458efa5 2020-11-24 stsp return err;
7734 6458efa5 2020-11-24 stsp }
7735 6458efa5 2020-11-24 stsp free(id);
7736 6458efa5 2020-11-24 stsp free(id_str);
7737 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7738 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7739 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7740 6458efa5 2020-11-24 stsp
7741 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7742 f91a2b48 2022-06-23 thomas 0, 0);
7743 6458efa5 2020-11-24 stsp if (err) {
7744 6458efa5 2020-11-24 stsp free(line);
7745 6458efa5 2020-11-24 stsp return err;
7746 6458efa5 2020-11-24 stsp }
7747 6458efa5 2020-11-24 stsp if (n == s->selected) {
7748 6458efa5 2020-11-24 stsp if (view->focussed)
7749 6458efa5 2020-11-24 stsp wstandout(view->window);
7750 6458efa5 2020-11-24 stsp s->selected_entry = re;
7751 6458efa5 2020-11-24 stsp }
7752 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7753 6458efa5 2020-11-24 stsp if (tc)
7754 6458efa5 2020-11-24 stsp wattr_on(view->window,
7755 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7756 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7757 6458efa5 2020-11-24 stsp if (tc)
7758 6458efa5 2020-11-24 stsp wattr_off(view->window,
7759 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7760 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7761 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7762 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7763 6458efa5 2020-11-24 stsp wstandend(view->window);
7764 6458efa5 2020-11-24 stsp free(line);
7765 6458efa5 2020-11-24 stsp free(wline);
7766 6458efa5 2020-11-24 stsp wline = NULL;
7767 6458efa5 2020-11-24 stsp n++;
7768 6458efa5 2020-11-24 stsp s->ndisplayed++;
7769 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7770 6458efa5 2020-11-24 stsp
7771 6458efa5 2020-11-24 stsp limit--;
7772 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7773 6458efa5 2020-11-24 stsp }
7774 6458efa5 2020-11-24 stsp
7775 a5d43cac 2022-07-01 thomas view_border(view);
7776 6458efa5 2020-11-24 stsp return err;
7777 6458efa5 2020-11-24 stsp }
7778 6458efa5 2020-11-24 stsp
7779 6458efa5 2020-11-24 stsp static const struct got_error *
7780 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7781 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7782 c42c9805 2020-11-24 stsp {
7783 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7784 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7785 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7786 c42c9805 2020-11-24 stsp
7787 c42c9805 2020-11-24 stsp *new_view = NULL;
7788 c42c9805 2020-11-24 stsp
7789 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7790 c42c9805 2020-11-24 stsp if (err) {
7791 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7792 c42c9805 2020-11-24 stsp return err;
7793 c42c9805 2020-11-24 stsp else
7794 c42c9805 2020-11-24 stsp return NULL;
7795 c42c9805 2020-11-24 stsp }
7796 c42c9805 2020-11-24 stsp
7797 c42c9805 2020-11-24 stsp
7798 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7799 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7800 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7801 c42c9805 2020-11-24 stsp goto done;
7802 c42c9805 2020-11-24 stsp }
7803 c42c9805 2020-11-24 stsp
7804 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7805 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7806 c42c9805 2020-11-24 stsp if (err)
7807 c42c9805 2020-11-24 stsp goto done;
7808 c42c9805 2020-11-24 stsp
7809 c42c9805 2020-11-24 stsp *new_view = tree_view;
7810 c42c9805 2020-11-24 stsp done:
7811 c42c9805 2020-11-24 stsp free(commit_id);
7812 c42c9805 2020-11-24 stsp return err;
7813 07dd3ed3 2022-08-06 thomas }
7814 07dd3ed3 2022-08-06 thomas
7815 07dd3ed3 2022-08-06 thomas static const struct got_error *
7816 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
7817 07dd3ed3 2022-08-06 thomas {
7818 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7819 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
7820 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
7821 07dd3ed3 2022-08-06 thomas
7822 07dd3ed3 2022-08-06 thomas g = view->gline;
7823 07dd3ed3 2022-08-06 thomas view->gline = 0;
7824 07dd3ed3 2022-08-06 thomas
7825 07dd3ed3 2022-08-06 thomas if (g == 0)
7826 07dd3ed3 2022-08-06 thomas g = 1;
7827 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
7828 07dd3ed3 2022-08-06 thomas g = s->nrefs;
7829 07dd3ed3 2022-08-06 thomas
7830 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
7831 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
7832 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
7833 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7834 07dd3ed3 2022-08-06 thomas return NULL;
7835 07dd3ed3 2022-08-06 thomas }
7836 07dd3ed3 2022-08-06 thomas
7837 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
7838 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
7839 07dd3ed3 2022-08-06 thomas if (err)
7840 07dd3ed3 2022-08-06 thomas return err;
7841 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7842 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
7843 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
7844 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7845 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
7846 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
7847 07dd3ed3 2022-08-06 thomas
7848 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
7849 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7850 07dd3ed3 2022-08-06 thomas
7851 07dd3ed3 2022-08-06 thomas return NULL;
7852 07dd3ed3 2022-08-06 thomas
7853 c42c9805 2020-11-24 stsp }
7854 07dd3ed3 2022-08-06 thomas
7855 c42c9805 2020-11-24 stsp static const struct got_error *
7856 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7857 6458efa5 2020-11-24 stsp {
7858 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7859 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7860 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7861 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
7862 6458efa5 2020-11-24 stsp
7863 07dd3ed3 2022-08-06 thomas if (view->gline)
7864 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
7865 07dd3ed3 2022-08-06 thomas
7866 6458efa5 2020-11-24 stsp switch (ch) {
7867 6458efa5 2020-11-24 stsp case 'i':
7868 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7869 07b0611c 2022-06-23 thomas view->count = 0;
7870 3bfadbd4 2021-11-20 thomas break;
7871 84227eb1 2022-06-23 thomas case 'm':
7872 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7873 07b0611c 2022-06-23 thomas view->count = 0;
7874 84227eb1 2022-06-23 thomas break;
7875 98182bd0 2021-11-20 thomas case 'o':
7876 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7877 07b0611c 2022-06-23 thomas view->count = 0;
7878 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7879 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7880 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7881 c591440f 2021-11-20 thomas if (err)
7882 c591440f 2021-11-20 thomas break;
7883 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7884 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7885 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7886 3bfadbd4 2021-11-20 thomas if (err)
7887 3bfadbd4 2021-11-20 thomas break;
7888 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7889 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7890 6458efa5 2020-11-24 stsp break;
7891 6458efa5 2020-11-24 stsp case KEY_ENTER:
7892 6458efa5 2020-11-24 stsp case '\r':
7893 07b0611c 2022-06-23 thomas view->count = 0;
7894 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7895 a5d43cac 2022-07-01 thomas break;
7896 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7897 6458efa5 2020-11-24 stsp break;
7898 1be4947a 2022-07-22 thomas case 'T':
7899 07b0611c 2022-06-23 thomas view->count = 0;
7900 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7901 c42c9805 2020-11-24 stsp break;
7902 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
7903 c42c9805 2020-11-24 stsp break;
7904 e4526bf5 2021-09-03 naddy case 'g':
7905 e4526bf5 2021-09-03 naddy case KEY_HOME:
7906 e4526bf5 2021-09-03 naddy s->selected = 0;
7907 07b0611c 2022-06-23 thomas view->count = 0;
7908 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7909 e4526bf5 2021-09-03 naddy break;
7910 e4526bf5 2021-09-03 naddy case 'G':
7911 a5d43cac 2022-07-01 thomas case KEY_END: {
7912 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7913 a5d43cac 2022-07-01 thomas
7914 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7915 a5d43cac 2022-07-01 thomas --eos; /* border */
7916 e4526bf5 2021-09-03 naddy s->selected = 0;
7917 07b0611c 2022-06-23 thomas view->count = 0;
7918 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7919 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7920 e4526bf5 2021-09-03 naddy if (re == NULL)
7921 e4526bf5 2021-09-03 naddy break;
7922 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7923 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7924 e4526bf5 2021-09-03 naddy }
7925 e4526bf5 2021-09-03 naddy if (n > 0)
7926 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7927 e4526bf5 2021-09-03 naddy break;
7928 a5d43cac 2022-07-01 thomas }
7929 6458efa5 2020-11-24 stsp case 'k':
7930 6458efa5 2020-11-24 stsp case KEY_UP:
7931 f7140bf5 2021-10-17 thomas case CTRL('p'):
7932 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7933 6458efa5 2020-11-24 stsp s->selected--;
7934 6458efa5 2020-11-24 stsp break;
7935 34ba6917 2020-11-30 stsp }
7936 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7937 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7938 07b0611c 2022-06-23 thomas view->count = 0;
7939 6458efa5 2020-11-24 stsp break;
7940 70f17a53 2022-06-13 thomas case CTRL('u'):
7941 23427b14 2022-06-23 thomas case 'u':
7942 70f17a53 2022-06-13 thomas nscroll /= 2;
7943 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7944 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7945 6458efa5 2020-11-24 stsp case CTRL('b'):
7946 1c5e5faa 2022-06-23 thomas case 'b':
7947 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7948 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7949 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7950 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7951 07b0611c 2022-06-23 thomas view->count = 0;
7952 6458efa5 2020-11-24 stsp break;
7953 6458efa5 2020-11-24 stsp case 'j':
7954 6458efa5 2020-11-24 stsp case KEY_DOWN:
7955 f7140bf5 2021-10-17 thomas case CTRL('n'):
7956 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7957 6458efa5 2020-11-24 stsp s->selected++;
7958 6458efa5 2020-11-24 stsp break;
7959 6458efa5 2020-11-24 stsp }
7960 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7961 6458efa5 2020-11-24 stsp /* can't scroll any further */
7962 07b0611c 2022-06-23 thomas view->count = 0;
7963 6458efa5 2020-11-24 stsp break;
7964 07b0611c 2022-06-23 thomas }
7965 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7966 6458efa5 2020-11-24 stsp break;
7967 70f17a53 2022-06-13 thomas case CTRL('d'):
7968 23427b14 2022-06-23 thomas case 'd':
7969 70f17a53 2022-06-13 thomas nscroll /= 2;
7970 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7971 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7972 6458efa5 2020-11-24 stsp case CTRL('f'):
7973 1c5e5faa 2022-06-23 thomas case 'f':
7974 4c2d69cb 2022-06-23 thomas case ' ':
7975 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7976 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7977 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7978 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7979 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7980 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7981 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7982 07b0611c 2022-06-23 thomas view->count = 0;
7983 6458efa5 2020-11-24 stsp break;
7984 6458efa5 2020-11-24 stsp }
7985 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7986 6458efa5 2020-11-24 stsp break;
7987 6458efa5 2020-11-24 stsp case CTRL('l'):
7988 07b0611c 2022-06-23 thomas view->count = 0;
7989 8924d611 2020-12-26 stsp tog_free_refs();
7990 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7991 8924d611 2020-12-26 stsp if (err)
7992 8924d611 2020-12-26 stsp break;
7993 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7994 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7995 6458efa5 2020-11-24 stsp break;
7996 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7997 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7998 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7999 6458efa5 2020-11-24 stsp break;
8000 6458efa5 2020-11-24 stsp default:
8001 07b0611c 2022-06-23 thomas view->count = 0;
8002 6458efa5 2020-11-24 stsp break;
8003 6458efa5 2020-11-24 stsp }
8004 6458efa5 2020-11-24 stsp
8005 6458efa5 2020-11-24 stsp return err;
8006 6458efa5 2020-11-24 stsp }
8007 6458efa5 2020-11-24 stsp
8008 6458efa5 2020-11-24 stsp __dead static void
8009 6458efa5 2020-11-24 stsp usage_ref(void)
8010 6458efa5 2020-11-24 stsp {
8011 6458efa5 2020-11-24 stsp endwin();
8012 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8013 6458efa5 2020-11-24 stsp getprogname());
8014 6458efa5 2020-11-24 stsp exit(1);
8015 6458efa5 2020-11-24 stsp }
8016 6458efa5 2020-11-24 stsp
8017 6458efa5 2020-11-24 stsp static const struct got_error *
8018 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8019 6458efa5 2020-11-24 stsp {
8020 6458efa5 2020-11-24 stsp const struct got_error *error;
8021 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8022 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8023 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8024 6458efa5 2020-11-24 stsp int ch;
8025 6458efa5 2020-11-24 stsp struct tog_view *view;
8026 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8027 6458efa5 2020-11-24 stsp
8028 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8029 6458efa5 2020-11-24 stsp switch (ch) {
8030 6458efa5 2020-11-24 stsp case 'r':
8031 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8032 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8033 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8034 6458efa5 2020-11-24 stsp optarg);
8035 6458efa5 2020-11-24 stsp break;
8036 6458efa5 2020-11-24 stsp default:
8037 e99e2d15 2020-11-24 naddy usage_ref();
8038 6458efa5 2020-11-24 stsp /* NOTREACHED */
8039 6458efa5 2020-11-24 stsp }
8040 6458efa5 2020-11-24 stsp }
8041 6458efa5 2020-11-24 stsp
8042 6458efa5 2020-11-24 stsp argc -= optind;
8043 6458efa5 2020-11-24 stsp argv += optind;
8044 6458efa5 2020-11-24 stsp
8045 6458efa5 2020-11-24 stsp if (argc > 1)
8046 e99e2d15 2020-11-24 naddy usage_ref();
8047 7cd52833 2022-06-23 thomas
8048 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8049 7cd52833 2022-06-23 thomas if (error != NULL)
8050 7cd52833 2022-06-23 thomas goto done;
8051 6458efa5 2020-11-24 stsp
8052 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8053 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8054 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8055 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8056 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8057 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8058 c156c7a4 2020-12-18 stsp goto done;
8059 6458efa5 2020-11-24 stsp if (worktree)
8060 6458efa5 2020-11-24 stsp repo_path =
8061 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8062 6458efa5 2020-11-24 stsp else
8063 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8064 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8065 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8066 c156c7a4 2020-12-18 stsp goto done;
8067 c156c7a4 2020-12-18 stsp }
8068 6458efa5 2020-11-24 stsp }
8069 6458efa5 2020-11-24 stsp
8070 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8071 6458efa5 2020-11-24 stsp if (error != NULL)
8072 6458efa5 2020-11-24 stsp goto done;
8073 6458efa5 2020-11-24 stsp
8074 6458efa5 2020-11-24 stsp init_curses();
8075 6458efa5 2020-11-24 stsp
8076 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8077 51a10b52 2020-12-26 stsp if (error)
8078 51a10b52 2020-12-26 stsp goto done;
8079 51a10b52 2020-12-26 stsp
8080 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8081 6458efa5 2020-11-24 stsp if (error)
8082 6458efa5 2020-11-24 stsp goto done;
8083 6458efa5 2020-11-24 stsp
8084 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8085 6458efa5 2020-11-24 stsp if (view == NULL) {
8086 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8087 6458efa5 2020-11-24 stsp goto done;
8088 6458efa5 2020-11-24 stsp }
8089 6458efa5 2020-11-24 stsp
8090 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8091 6458efa5 2020-11-24 stsp if (error)
8092 6458efa5 2020-11-24 stsp goto done;
8093 6458efa5 2020-11-24 stsp
8094 6458efa5 2020-11-24 stsp if (worktree) {
8095 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8096 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8097 6458efa5 2020-11-24 stsp worktree = NULL;
8098 6458efa5 2020-11-24 stsp }
8099 6458efa5 2020-11-24 stsp error = view_loop(view);
8100 6458efa5 2020-11-24 stsp done:
8101 6458efa5 2020-11-24 stsp free(repo_path);
8102 6458efa5 2020-11-24 stsp free(cwd);
8103 1d0f4054 2021-06-17 stsp if (repo) {
8104 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8105 1d0f4054 2021-06-17 stsp if (close_err)
8106 1d0f4054 2021-06-17 stsp error = close_err;
8107 1d0f4054 2021-06-17 stsp }
8108 7cd52833 2022-06-23 thomas if (pack_fds) {
8109 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8110 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8111 7cd52833 2022-06-23 thomas if (error == NULL)
8112 7cd52833 2022-06-23 thomas error = pack_err;
8113 7cd52833 2022-06-23 thomas }
8114 51a10b52 2020-12-26 stsp tog_free_refs();
8115 6458efa5 2020-11-24 stsp return error;
8116 6458efa5 2020-11-24 stsp }
8117 6458efa5 2020-11-24 stsp
8118 2a31b33b 2022-07-23 thomas static const struct got_error *
8119 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8120 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8121 2a31b33b 2022-07-23 thomas {
8122 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8123 2a31b33b 2022-07-23 thomas
8124 2a31b33b 2022-07-23 thomas *new_view = NULL;
8125 2a31b33b 2022-07-23 thomas
8126 2a31b33b 2022-07-23 thomas switch (request) {
8127 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8128 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8129 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8130 2a31b33b 2022-07-23 thomas
8131 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8132 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8133 2a31b33b 2022-07-23 thomas view, s->repo);
8134 2a31b33b 2022-07-23 thomas } else
8135 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8136 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8137 2a31b33b 2022-07-23 thomas break;
8138 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8139 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8140 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8141 2a31b33b 2022-07-23 thomas
8142 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8143 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8144 2a31b33b 2022-07-23 thomas s->repo);
8145 2a31b33b 2022-07-23 thomas } else
8146 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8147 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8148 2a31b33b 2022-07-23 thomas break;
8149 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8150 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8151 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8152 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8153 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8154 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8155 2a31b33b 2022-07-23 thomas &view->state.tree);
8156 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8157 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8158 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8159 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8160 2a31b33b 2022-07-23 thomas else
8161 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8162 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8163 2a31b33b 2022-07-23 thomas break;
8164 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8165 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8166 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8167 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8168 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8169 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8170 2a31b33b 2022-07-23 thomas view->state.log.repo);
8171 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8172 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8173 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8174 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8175 2a31b33b 2022-07-23 thomas else
8176 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8177 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8178 2a31b33b 2022-07-23 thomas break;
8179 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
8180 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8181 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
8182 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
8183 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8184 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
8185 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8186 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
8187 2a31b33b 2022-07-23 thomas else
8188 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
8189 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8190 2a31b33b 2022-07-23 thomas if (err)
8191 2a31b33b 2022-07-23 thomas view_close(*new_view);
8192 2a31b33b 2022-07-23 thomas break;
8193 2a31b33b 2022-07-23 thomas default:
8194 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8195 2a31b33b 2022-07-23 thomas }
8196 2a31b33b 2022-07-23 thomas
8197 2a31b33b 2022-07-23 thomas return err;
8198 2a31b33b 2022-07-23 thomas }
8199 2a31b33b 2022-07-23 thomas
8200 a5d43cac 2022-07-01 thomas /*
8201 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8202 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8203 a5d43cac 2022-07-01 thomas */
8204 6458efa5 2020-11-24 stsp static void
8205 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8206 a5d43cac 2022-07-01 thomas {
8207 a5d43cac 2022-07-01 thomas switch (view->type) {
8208 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8209 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8210 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8211 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8212 a5d43cac 2022-07-01 thomas 1);
8213 a5d43cac 2022-07-01 thomas break;
8214 a5d43cac 2022-07-01 thomas }
8215 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8216 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8217 a5d43cac 2022-07-01 thomas else
8218 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8219 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8220 a5d43cac 2022-07-01 thomas break;
8221 a5d43cac 2022-07-01 thomas }
8222 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8223 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8224 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8225 a5d43cac 2022-07-01 thomas break;
8226 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8227 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8228 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8229 a5d43cac 2022-07-01 thomas break;
8230 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8231 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8232 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8233 a5d43cac 2022-07-01 thomas break;
8234 a5d43cac 2022-07-01 thomas default:
8235 a5d43cac 2022-07-01 thomas break;
8236 a5d43cac 2022-07-01 thomas }
8237 a5d43cac 2022-07-01 thomas
8238 a5d43cac 2022-07-01 thomas view->offset = 0;
8239 a5d43cac 2022-07-01 thomas }
8240 a5d43cac 2022-07-01 thomas
8241 a5d43cac 2022-07-01 thomas /*
8242 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8243 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8244 a5d43cac 2022-07-01 thomas */
8245 a5d43cac 2022-07-01 thomas static const struct got_error *
8246 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8247 a5d43cac 2022-07-01 thomas {
8248 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8249 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8250 a5d43cac 2022-07-01 thomas int *selected = NULL;
8251 a5d43cac 2022-07-01 thomas int header, offset;
8252 a5d43cac 2022-07-01 thomas
8253 a5d43cac 2022-07-01 thomas switch (view->type) {
8254 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8255 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8256 a5d43cac 2022-07-01 thomas header = 3;
8257 a5d43cac 2022-07-01 thomas scrolld = NULL;
8258 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8259 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8260 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8261 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8262 a5d43cac 2022-07-01 thomas view->offset = offset;
8263 a5d43cac 2022-07-01 thomas }
8264 a5d43cac 2022-07-01 thomas break;
8265 a5d43cac 2022-07-01 thomas }
8266 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8267 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8268 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8269 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8270 a5d43cac 2022-07-01 thomas selected = &s->selected;
8271 a5d43cac 2022-07-01 thomas break;
8272 a5d43cac 2022-07-01 thomas }
8273 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8274 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8275 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8276 a5d43cac 2022-07-01 thomas header = 3;
8277 a5d43cac 2022-07-01 thomas selected = &s->selected;
8278 a5d43cac 2022-07-01 thomas break;
8279 a5d43cac 2022-07-01 thomas }
8280 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8281 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8282 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8283 a5d43cac 2022-07-01 thomas header = 5;
8284 a5d43cac 2022-07-01 thomas selected = &s->selected;
8285 a5d43cac 2022-07-01 thomas break;
8286 a5d43cac 2022-07-01 thomas }
8287 a5d43cac 2022-07-01 thomas default:
8288 a5d43cac 2022-07-01 thomas selected = NULL;
8289 a5d43cac 2022-07-01 thomas scrolld = NULL;
8290 a5d43cac 2022-07-01 thomas header = 0;
8291 a5d43cac 2022-07-01 thomas break;
8292 a5d43cac 2022-07-01 thomas }
8293 a5d43cac 2022-07-01 thomas
8294 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8295 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8296 a5d43cac 2022-07-01 thomas view->offset = offset;
8297 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8298 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8299 a5d43cac 2022-07-01 thomas *selected -= offset;
8300 a5d43cac 2022-07-01 thomas }
8301 a5d43cac 2022-07-01 thomas }
8302 a5d43cac 2022-07-01 thomas
8303 a5d43cac 2022-07-01 thomas return err;
8304 a5d43cac 2022-07-01 thomas }
8305 a5d43cac 2022-07-01 thomas
8306 a5d43cac 2022-07-01 thomas static void
8307 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8308 ce5b7c56 2019-07-09 stsp {
8309 6059809a 2020-12-17 stsp size_t i;
8310 9f7d7167 2018-04-29 stsp
8311 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8312 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8313 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8314 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8315 ce5b7c56 2019-07-09 stsp }
8316 6879ba42 2020-10-01 naddy fputc('\n', fp);
8317 ce5b7c56 2019-07-09 stsp }
8318 ce5b7c56 2019-07-09 stsp
8319 4ed7e80c 2018-05-20 stsp __dead static void
8320 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8321 9f7d7167 2018-04-29 stsp {
8322 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8323 6879ba42 2020-10-01 naddy
8324 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8325 6879ba42 2020-10-01 naddy getprogname());
8326 6879ba42 2020-10-01 naddy if (hflag) {
8327 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8328 6879ba42 2020-10-01 naddy list_commands(fp);
8329 ee85c5e8 2020-02-29 stsp }
8330 6879ba42 2020-10-01 naddy exit(status);
8331 9f7d7167 2018-04-29 stsp }
8332 9f7d7167 2018-04-29 stsp
8333 c2301be8 2018-04-30 stsp static char **
8334 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8335 c2301be8 2018-04-30 stsp {
8336 ee85c5e8 2020-02-29 stsp va_list ap;
8337 c2301be8 2018-04-30 stsp char **argv;
8338 ee85c5e8 2020-02-29 stsp int i;
8339 c2301be8 2018-04-30 stsp
8340 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8341 ee85c5e8 2020-02-29 stsp
8342 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8343 c2301be8 2018-04-30 stsp if (argv == NULL)
8344 c2301be8 2018-04-30 stsp err(1, "calloc");
8345 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8346 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8347 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8348 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8349 c2301be8 2018-04-30 stsp }
8350 c2301be8 2018-04-30 stsp
8351 ee85c5e8 2020-02-29 stsp va_end(ap);
8352 c2301be8 2018-04-30 stsp return argv;
8353 ee85c5e8 2020-02-29 stsp }
8354 ee85c5e8 2020-02-29 stsp
8355 ee85c5e8 2020-02-29 stsp /*
8356 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8357 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8358 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8359 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8360 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8361 ee85c5e8 2020-02-29 stsp */
8362 ee85c5e8 2020-02-29 stsp static const struct got_error *
8363 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8364 ee85c5e8 2020-02-29 stsp {
8365 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8366 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8367 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8368 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8369 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8370 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8371 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8372 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8373 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8374 84de9106 2020-12-26 stsp
8375 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8376 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8377 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8378 ee85c5e8 2020-02-29 stsp
8379 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8380 7cd52833 2022-06-23 thomas if (error != NULL)
8381 7cd52833 2022-06-23 thomas goto done;
8382 7cd52833 2022-06-23 thomas
8383 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8384 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8385 ee85c5e8 2020-02-29 stsp goto done;
8386 ee85c5e8 2020-02-29 stsp
8387 ee85c5e8 2020-02-29 stsp if (worktree)
8388 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8389 ee85c5e8 2020-02-29 stsp else
8390 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8391 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8392 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8393 ee85c5e8 2020-02-29 stsp goto done;
8394 ee85c5e8 2020-02-29 stsp }
8395 ee85c5e8 2020-02-29 stsp
8396 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8397 ee85c5e8 2020-02-29 stsp if (error != NULL)
8398 ee85c5e8 2020-02-29 stsp goto done;
8399 ee85c5e8 2020-02-29 stsp
8400 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8401 ee85c5e8 2020-02-29 stsp repo, worktree);
8402 ee85c5e8 2020-02-29 stsp if (error)
8403 ee85c5e8 2020-02-29 stsp goto done;
8404 ee85c5e8 2020-02-29 stsp
8405 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8406 84de9106 2020-12-26 stsp if (error)
8407 84de9106 2020-12-26 stsp goto done;
8408 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8409 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8410 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8411 ee85c5e8 2020-02-29 stsp if (error)
8412 ee85c5e8 2020-02-29 stsp goto done;
8413 ee85c5e8 2020-02-29 stsp
8414 ee85c5e8 2020-02-29 stsp if (worktree) {
8415 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8416 ee85c5e8 2020-02-29 stsp worktree = NULL;
8417 ee85c5e8 2020-02-29 stsp }
8418 ee85c5e8 2020-02-29 stsp
8419 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8420 945f9229 2022-04-16 thomas if (error)
8421 945f9229 2022-04-16 thomas goto done;
8422 945f9229 2022-04-16 thomas
8423 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8424 ee85c5e8 2020-02-29 stsp if (error) {
8425 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8426 ee85c5e8 2020-02-29 stsp goto done;
8427 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8428 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8429 6879ba42 2020-10-01 naddy usage(1, 1);
8430 ee85c5e8 2020-02-29 stsp /* not reached */
8431 ee85c5e8 2020-02-29 stsp }
8432 ee85c5e8 2020-02-29 stsp
8433 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8434 ee85c5e8 2020-02-29 stsp if (error)
8435 ee85c5e8 2020-02-29 stsp goto done;
8436 ee85c5e8 2020-02-29 stsp
8437 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8438 ee85c5e8 2020-02-29 stsp argc = 4;
8439 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8440 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8441 ee85c5e8 2020-02-29 stsp done:
8442 1d0f4054 2021-06-17 stsp if (repo) {
8443 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8444 1d0f4054 2021-06-17 stsp if (error == NULL)
8445 1d0f4054 2021-06-17 stsp error = close_err;
8446 1d0f4054 2021-06-17 stsp }
8447 945f9229 2022-04-16 thomas if (commit)
8448 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8449 ee85c5e8 2020-02-29 stsp if (worktree)
8450 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8451 7cd52833 2022-06-23 thomas if (pack_fds) {
8452 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8453 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8454 7cd52833 2022-06-23 thomas if (error == NULL)
8455 7cd52833 2022-06-23 thomas error = pack_err;
8456 7cd52833 2022-06-23 thomas }
8457 ee85c5e8 2020-02-29 stsp free(id);
8458 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8459 ee85c5e8 2020-02-29 stsp free(commit_id);
8460 ee85c5e8 2020-02-29 stsp free(cwd);
8461 ee85c5e8 2020-02-29 stsp free(repo_path);
8462 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8463 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8464 ee85c5e8 2020-02-29 stsp int i;
8465 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8466 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8467 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8468 ee85c5e8 2020-02-29 stsp }
8469 87670572 2020-12-26 naddy tog_free_refs();
8470 ee85c5e8 2020-02-29 stsp return error;
8471 c2301be8 2018-04-30 stsp }
8472 c2301be8 2018-04-30 stsp
8473 9f7d7167 2018-04-29 stsp int
8474 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8475 9f7d7167 2018-04-29 stsp {
8476 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8477 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8478 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8479 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8480 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8481 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8482 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8483 83cd27f8 2020-01-13 stsp };
8484 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8485 9f7d7167 2018-04-29 stsp
8486 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
8487 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
8488 cacf1690 2022-09-06 thomas
8489 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8490 9f7d7167 2018-04-29 stsp
8491 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8492 9f7d7167 2018-04-29 stsp switch (ch) {
8493 9f7d7167 2018-04-29 stsp case 'h':
8494 9f7d7167 2018-04-29 stsp hflag = 1;
8495 9f7d7167 2018-04-29 stsp break;
8496 53ccebc2 2019-07-30 stsp case 'V':
8497 53ccebc2 2019-07-30 stsp Vflag = 1;
8498 53ccebc2 2019-07-30 stsp break;
8499 9f7d7167 2018-04-29 stsp default:
8500 6879ba42 2020-10-01 naddy usage(hflag, 1);
8501 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8502 9f7d7167 2018-04-29 stsp }
8503 9f7d7167 2018-04-29 stsp }
8504 9f7d7167 2018-04-29 stsp
8505 9f7d7167 2018-04-29 stsp argc -= optind;
8506 9f7d7167 2018-04-29 stsp argv += optind;
8507 9814e6a3 2020-09-27 naddy optind = 1;
8508 c2301be8 2018-04-30 stsp optreset = 1;
8509 9f7d7167 2018-04-29 stsp
8510 53ccebc2 2019-07-30 stsp if (Vflag) {
8511 53ccebc2 2019-07-30 stsp got_version_print_str();
8512 6879ba42 2020-10-01 naddy return 0;
8513 53ccebc2 2019-07-30 stsp }
8514 4010e238 2020-12-04 stsp
8515 4010e238 2020-12-04 stsp #ifndef PROFILE
8516 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8517 4010e238 2020-12-04 stsp NULL) == -1)
8518 4010e238 2020-12-04 stsp err(1, "pledge");
8519 4010e238 2020-12-04 stsp #endif
8520 53ccebc2 2019-07-30 stsp
8521 c2301be8 2018-04-30 stsp if (argc == 0) {
8522 f29d3e89 2018-06-23 stsp if (hflag)
8523 6879ba42 2020-10-01 naddy usage(hflag, 0);
8524 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8525 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8526 c2301be8 2018-04-30 stsp argc = 1;
8527 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8528 c2301be8 2018-04-30 stsp } else {
8529 6059809a 2020-12-17 stsp size_t i;
8530 9f7d7167 2018-04-29 stsp
8531 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8532 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8533 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8534 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8535 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8536 9f7d7167 2018-04-29 stsp break;
8537 9f7d7167 2018-04-29 stsp }
8538 9f7d7167 2018-04-29 stsp }
8539 ee85c5e8 2020-02-29 stsp }
8540 3642c4c6 2019-07-09 stsp
8541 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8542 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8543 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8544 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8545 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8546 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8547 adf4c9e0 2022-07-03 thomas }
8548 adf4c9e0 2022-07-03 thomas
8549 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8550 ee85c5e8 2020-02-29 stsp if (argc != 1)
8551 6879ba42 2020-10-01 naddy usage(0, 1);
8552 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8553 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8554 ee85c5e8 2020-02-29 stsp } else {
8555 ee85c5e8 2020-02-29 stsp if (hflag)
8556 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8557 ee85c5e8 2020-02-29 stsp else
8558 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8559 9f7d7167 2018-04-29 stsp }
8560 9f7d7167 2018-04-29 stsp
8561 9f7d7167 2018-04-29 stsp endwin();
8562 b46c1e04 2020-09-20 naddy putchar('\n');
8563 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8564 a2f4a359 2020-02-28 stsp int i;
8565 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8566 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8567 a2f4a359 2020-02-28 stsp free(cmd_argv);
8568 a2f4a359 2020-02-28 stsp }
8569 a2f4a359 2020-02-28 stsp
8570 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8571 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8572 9f7d7167 2018-04-29 stsp return 0;
8573 9f7d7167 2018-04-29 stsp }