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 11b20872 2019-11-08 stsp
322 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
323 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
324 3dbaef42 2020-11-24 stsp const char *label1, *label2;
325 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
326 19a6a6b5 2022-07-01 thomas int fd1, fd2;
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 bddb1296 2019-11-08 stsp struct tog_colors colors;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey off_t *line_offsets;
337 f44b1f58 2020-02-02 tracey int matched_line;
338 f44b1f58 2020-02-02 tracey int selected_line;
339 15a087fe 2019-02-21 stsp
340 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
341 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
342 b01e7d3b 2018-08-04 stsp };
343 b01e7d3b 2018-08-04 stsp
344 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
346 1a76625f 2018-10-22 stsp
347 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
348 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
349 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
350 1a76625f 2018-10-22 stsp int commits_needed;
351 fb280deb 2021-08-30 stsp int load_all;
352 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
353 1a76625f 2018-10-22 stsp struct commit_queue *commits;
354 1a76625f 2018-10-22 stsp const char *in_repo_path;
355 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
356 1a76625f 2018-10-22 stsp struct got_repository *repo;
357 1af5eddf 2022-06-23 thomas int *pack_fds;
358 1a76625f 2018-10-22 stsp int log_complete;
359 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
361 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
362 13add988 2019-10-15 stsp int *searching;
363 13add988 2019-10-15 stsp int *search_next_done;
364 13add988 2019-10-15 stsp regex_t *regex;
365 1a76625f 2018-10-22 stsp };
366 1a76625f 2018-10-22 stsp
367 1a76625f 2018-10-22 stsp struct tog_log_view_state {
368 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
371 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
372 b01e7d3b 2018-08-04 stsp int selected;
373 b01e7d3b 2018-08-04 stsp char *in_repo_path;
374 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
375 b672a97a 2020-01-27 stsp int log_branches;
376 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
377 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
378 1a76625f 2018-10-22 stsp sig_atomic_t quit;
379 1a76625f 2018-10-22 stsp pthread_t thread;
380 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
381 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
382 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
383 11b20872 2019-11-08 stsp struct tog_colors colors;
384 f69c5a46 2022-07-19 thomas int use_committer;
385 ba4f502b 2018-08-04 stsp };
386 11b20872 2019-11-08 stsp
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
394 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
395 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
396 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
397 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
400 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
401 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
402 ba4f502b 2018-08-04 stsp
403 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
404 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
405 e9424729 2018-08-04 stsp int nlines;
406 e9424729 2018-08-04 stsp
407 e9424729 2018-08-04 stsp struct tog_view *view;
408 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
409 e9424729 2018-08-04 stsp int *quit;
410 e9424729 2018-08-04 stsp };
411 e9424729 2018-08-04 stsp
412 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
413 e9424729 2018-08-04 stsp const char *path;
414 e9424729 2018-08-04 stsp struct got_repository *repo;
415 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
416 e9424729 2018-08-04 stsp int *complete;
417 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
418 fc06ba56 2019-08-22 stsp void *cancel_arg;
419 e9424729 2018-08-04 stsp };
420 e9424729 2018-08-04 stsp
421 e9424729 2018-08-04 stsp struct tog_blame {
422 e9424729 2018-08-04 stsp FILE *f;
423 be659d10 2020-11-18 stsp off_t filesize;
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
425 6fcac457 2018-11-19 stsp int nlines;
426 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
427 e9424729 2018-08-04 stsp pthread_t thread;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
429 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
430 e9424729 2018-08-04 stsp const char *path;
431 7cd52833 2022-06-23 thomas int *pack_fds;
432 e9424729 2018-08-04 stsp };
433 e9424729 2018-08-04 stsp
434 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
435 7cbe629d 2018-08-04 stsp int first_displayed_line;
436 7cbe629d 2018-08-04 stsp int last_displayed_line;
437 7cbe629d 2018-08-04 stsp int selected_line;
438 4fc71f3b 2022-07-12 thomas int last_diffed_line;
439 7cbe629d 2018-08-04 stsp int blame_complete;
440 e5a0f69f 2018-08-18 stsp int eof;
441 e5a0f69f 2018-08-18 stsp int done;
442 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
443 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
444 e5a0f69f 2018-08-18 stsp char *path;
445 7cbe629d 2018-08-04 stsp struct got_repository *repo;
446 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
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 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
526 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
527 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
528 9970f7fc 2020-12-03 stsp int dying;
529 669b5ffa 2018-10-07 stsp struct tog_view *parent;
530 669b5ffa 2018-10-07 stsp struct tog_view *child;
531 5dc9f4bc 2018-08-04 stsp
532 e78dc838 2020-12-04 stsp /*
533 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
534 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
535 e78dc838 2020-12-04 stsp * between parent and child.
536 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
537 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
538 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
539 e78dc838 2020-12-04 stsp * situations.
540 e78dc838 2020-12-04 stsp */
541 e78dc838 2020-12-04 stsp int focus_child;
542 e78dc838 2020-12-04 stsp
543 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
544 5dc9f4bc 2018-08-04 stsp /* type-specific state */
545 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
546 5dc9f4bc 2018-08-04 stsp union {
547 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
548 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
549 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
550 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
551 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
552 5dc9f4bc 2018-08-04 stsp } state;
553 e5a0f69f 2018-08-18 stsp
554 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
556 e78dc838 2020-12-04 stsp struct tog_view *, int);
557 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
558 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
559 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
560 60493ae3 2019-06-20 stsp
561 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
563 c0c4acc8 2021-01-24 stsp int search_started;
564 60493ae3 2019-06-20 stsp int searching;
565 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
567 60493ae3 2019-06-20 stsp int search_next_done;
568 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
570 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
571 1803e47f 2019-06-22 stsp regex_t regex;
572 41605754 2020-11-12 stsp regmatch_t regmatch;
573 cc3c9aac 2018-08-01 stsp };
574 cd0acaa7 2018-05-20 stsp
575 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
576 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
577 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
578 78756c87 2020-11-24 stsp struct got_repository *);
579 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
584 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp
587 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
588 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
589 78756c87 2020-11-24 stsp const char *, const char *, int);
590 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
591 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
592 e78dc838 2020-12-04 stsp struct tog_view *, int);
593 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
594 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
595 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
597 e5a0f69f 2018-08-18 stsp
598 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
599 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
600 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
601 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
602 e78dc838 2020-12-04 stsp struct tog_view *, int);
603 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
604 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
605 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
607 e5a0f69f 2018-08-18 stsp
608 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
609 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
610 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
611 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
612 e78dc838 2020-12-04 stsp struct tog_view *, int);
613 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
614 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
616 6458efa5 2020-11-24 stsp
617 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
618 6458efa5 2020-11-24 stsp struct got_repository *);
619 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
621 e78dc838 2020-12-04 stsp struct tog_view *, int);
622 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
623 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
625 25791caa 2018-10-24 stsp
626 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
627 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
628 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
629 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
631 25791caa 2018-10-24 stsp
632 25791caa 2018-10-24 stsp static void
633 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
634 25791caa 2018-10-24 stsp {
635 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
636 83baff54 2019-08-12 stsp }
637 83baff54 2019-08-12 stsp
638 83baff54 2019-08-12 stsp static void
639 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
640 83baff54 2019-08-12 stsp {
641 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
642 25791caa 2018-10-24 stsp }
643 26ed57b2 2018-05-19 stsp
644 61266923 2020-01-14 stsp static void
645 61266923 2020-01-14 stsp tog_sigcont(int signo)
646 61266923 2020-01-14 stsp {
647 61266923 2020-01-14 stsp tog_sigcont_received = 1;
648 61266923 2020-01-14 stsp }
649 61266923 2020-01-14 stsp
650 296152d1 2022-05-31 thomas static void
651 296152d1 2022-05-31 thomas tog_sigint(int signo)
652 296152d1 2022-05-31 thomas {
653 296152d1 2022-05-31 thomas tog_sigint_received = 1;
654 296152d1 2022-05-31 thomas }
655 296152d1 2022-05-31 thomas
656 296152d1 2022-05-31 thomas static void
657 296152d1 2022-05-31 thomas tog_sigterm(int signo)
658 296152d1 2022-05-31 thomas {
659 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
660 296152d1 2022-05-31 thomas }
661 296152d1 2022-05-31 thomas
662 296152d1 2022-05-31 thomas static int
663 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
664 296152d1 2022-05-31 thomas {
665 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
666 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
667 296152d1 2022-05-31 thomas }
668 296152d1 2022-05-31 thomas
669 e5a0f69f 2018-08-18 stsp static const struct got_error *
670 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
671 ea5e7bb5 2018-08-01 stsp {
672 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
673 e5a0f69f 2018-08-18 stsp
674 669b5ffa 2018-10-07 stsp if (view->child) {
675 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
676 669b5ffa 2018-10-07 stsp view->child = NULL;
677 669b5ffa 2018-10-07 stsp }
678 e5a0f69f 2018-08-18 stsp if (view->close)
679 e5a0f69f 2018-08-18 stsp err = view->close(view);
680 ea5e7bb5 2018-08-01 stsp if (view->panel)
681 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
682 ea5e7bb5 2018-08-01 stsp if (view->window)
683 ea5e7bb5 2018-08-01 stsp delwin(view->window);
684 ea5e7bb5 2018-08-01 stsp free(view);
685 f2d749db 2022-07-12 thomas return err ? err : child_err;
686 ea5e7bb5 2018-08-01 stsp }
687 ea5e7bb5 2018-08-01 stsp
688 ea5e7bb5 2018-08-01 stsp static struct tog_view *
689 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
690 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
691 ea5e7bb5 2018-08-01 stsp {
692 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
693 ea5e7bb5 2018-08-01 stsp
694 ea5e7bb5 2018-08-01 stsp if (view == NULL)
695 ea5e7bb5 2018-08-01 stsp return NULL;
696 ea5e7bb5 2018-08-01 stsp
697 d6b05b5b 2018-08-04 stsp view->type = type;
698 f7d12f7e 2018-08-01 stsp view->lines = LINES;
699 f7d12f7e 2018-08-01 stsp view->cols = COLS;
700 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
701 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
702 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
703 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
704 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
705 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
706 96a765a8 2018-08-04 stsp view_close(view);
707 ea5e7bb5 2018-08-01 stsp return NULL;
708 ea5e7bb5 2018-08-01 stsp }
709 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
710 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
711 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
712 96a765a8 2018-08-04 stsp view_close(view);
713 ea5e7bb5 2018-08-01 stsp return NULL;
714 ea5e7bb5 2018-08-01 stsp }
715 ea5e7bb5 2018-08-01 stsp
716 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
717 ea5e7bb5 2018-08-01 stsp return view;
718 cdf1ee82 2018-08-01 stsp }
719 cdf1ee82 2018-08-01 stsp
720 0cf4efb1 2018-09-29 stsp static int
721 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
722 0cf4efb1 2018-09-29 stsp {
723 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
724 0cf4efb1 2018-09-29 stsp return 0;
725 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
726 5c60c32a 2018-10-18 stsp }
727 5c60c32a 2018-10-18 stsp
728 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
729 a5d43cac 2022-07-01 thomas static int
730 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
731 a5d43cac 2022-07-01 thomas {
732 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
733 a5d43cac 2022-07-01 thomas }
734 a5d43cac 2022-07-01 thomas
735 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
736 5c60c32a 2018-10-18 stsp
737 5c60c32a 2018-10-18 stsp static const struct got_error *
738 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
739 5c60c32a 2018-10-18 stsp {
740 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
741 5c60c32a 2018-10-18 stsp
742 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
743 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
744 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
745 53d2bdd3 2022-07-10 thomas else
746 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
747 a5d43cac 2022-07-01 thomas view->begin_x = 0;
748 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
749 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
750 53d2bdd3 2022-07-10 thomas view->cols > 119)
751 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
752 53d2bdd3 2022-07-10 thomas else
753 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
754 a5d43cac 2022-07-01 thomas view->begin_y = 0;
755 a5d43cac 2022-07-01 thomas }
756 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
757 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
758 5c60c32a 2018-10-18 stsp view->lines = LINES;
759 5c60c32a 2018-10-18 stsp view->cols = COLS;
760 5c60c32a 2018-10-18 stsp err = view_resize(view);
761 5c60c32a 2018-10-18 stsp if (err)
762 5c60c32a 2018-10-18 stsp return err;
763 5c60c32a 2018-10-18 stsp
764 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
765 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
766 a5d43cac 2022-07-01 thomas
767 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
768 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
769 5c60c32a 2018-10-18 stsp
770 5c60c32a 2018-10-18 stsp return NULL;
771 5c60c32a 2018-10-18 stsp }
772 5c60c32a 2018-10-18 stsp
773 5c60c32a 2018-10-18 stsp static const struct got_error *
774 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
775 5c60c32a 2018-10-18 stsp {
776 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
777 5c60c32a 2018-10-18 stsp
778 5c60c32a 2018-10-18 stsp view->begin_x = 0;
779 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
780 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
781 5c60c32a 2018-10-18 stsp view->ncols = COLS;
782 5c60c32a 2018-10-18 stsp view->lines = LINES;
783 5c60c32a 2018-10-18 stsp view->cols = COLS;
784 5c60c32a 2018-10-18 stsp err = view_resize(view);
785 5c60c32a 2018-10-18 stsp if (err)
786 5c60c32a 2018-10-18 stsp return err;
787 5c60c32a 2018-10-18 stsp
788 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
789 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
790 5c60c32a 2018-10-18 stsp
791 5c60c32a 2018-10-18 stsp return NULL;
792 0cf4efb1 2018-09-29 stsp }
793 0cf4efb1 2018-09-29 stsp
794 5c60c32a 2018-10-18 stsp static int
795 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
796 5c60c32a 2018-10-18 stsp {
797 5c60c32a 2018-10-18 stsp return view->parent == NULL;
798 5c60c32a 2018-10-18 stsp }
799 5c60c32a 2018-10-18 stsp
800 b65b3ea0 2022-06-23 thomas static int
801 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
802 b65b3ea0 2022-06-23 thomas {
803 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
804 e44940c3 2022-07-01 thomas }
805 e44940c3 2022-07-01 thomas
806 e44940c3 2022-07-01 thomas static int
807 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
808 e44940c3 2022-07-01 thomas {
809 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
810 b65b3ea0 2022-06-23 thomas }
811 b65b3ea0 2022-06-23 thomas
812 444d5325 2022-07-03 thomas static int
813 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
814 444d5325 2022-07-03 thomas {
815 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
816 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
817 444d5325 2022-07-03 thomas }
818 444d5325 2022-07-03 thomas
819 a5d43cac 2022-07-01 thomas static void
820 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
821 a5d43cac 2022-07-01 thomas {
822 a5d43cac 2022-07-01 thomas PANEL *panel;
823 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
824 b65b3ea0 2022-06-23 thomas
825 a5d43cac 2022-07-01 thomas if (view->parent)
826 a5d43cac 2022-07-01 thomas return view_border(view->parent);
827 a5d43cac 2022-07-01 thomas
828 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
829 a5d43cac 2022-07-01 thomas if (panel == NULL)
830 a5d43cac 2022-07-01 thomas return;
831 a5d43cac 2022-07-01 thomas
832 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
833 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
834 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
835 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
836 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
837 a5d43cac 2022-07-01 thomas else
838 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
839 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
840 a5d43cac 2022-07-01 thomas }
841 a5d43cac 2022-07-01 thomas
842 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
843 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
844 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
846 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
847 a5d43cac 2022-07-01 thomas
848 4d8c2215 2018-08-19 stsp static const struct got_error *
849 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
850 f7d12f7e 2018-08-01 stsp {
851 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
852 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
853 f7d12f7e 2018-08-01 stsp
854 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
855 a5d43cac 2022-07-01 thomas
856 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
857 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
858 0cf4efb1 2018-09-29 stsp else
859 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
860 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
861 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
862 0cf4efb1 2018-09-29 stsp else
863 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
864 6d0fee91 2018-08-01 stsp
865 4918811f 2022-07-01 thomas if (view->child) {
866 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
867 a5d43cac 2022-07-01 thomas
868 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
869 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
870 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
871 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
872 40236d76 2022-06-23 thomas ncols = COLS;
873 40236d76 2022-06-23 thomas
874 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
875 5c60c32a 2018-10-18 stsp if (view->child->focussed)
876 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
877 5c60c32a 2018-10-18 stsp else
878 5c60c32a 2018-10-18 stsp show_panel(view->panel);
879 5c60c32a 2018-10-18 stsp } else {
880 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
881 40236d76 2022-06-23 thomas
882 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
883 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
884 a5d43cac 2022-07-01 thomas }
885 a5d43cac 2022-07-01 thomas /*
886 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
887 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
888 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
889 a5d43cac 2022-07-01 thomas */
890 a5d43cac 2022-07-01 thomas if (hs) {
891 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
892 a5d43cac 2022-07-01 thomas if (err)
893 a5d43cac 2022-07-01 thomas return err;
894 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
895 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
896 a5d43cac 2022-07-01 thomas if (err)
897 a5d43cac 2022-07-01 thomas return err;
898 a5d43cac 2022-07-01 thomas }
899 a5d43cac 2022-07-01 thomas view_border(view);
900 a5d43cac 2022-07-01 thomas update_panels();
901 a5d43cac 2022-07-01 thomas doupdate();
902 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
903 a5d43cac 2022-07-01 thomas nlines = view->nlines;
904 a5d43cac 2022-07-01 thomas }
905 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
906 40236d76 2022-06-23 thomas ncols = COLS;
907 669b5ffa 2018-10-07 stsp
908 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
909 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
910 ea0bff04 2022-07-19 thomas if (err)
911 ea0bff04 2022-07-19 thomas return err;
912 ea0bff04 2022-07-19 thomas }
913 ea0bff04 2022-07-19 thomas
914 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
915 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
916 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
917 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
918 40236d76 2022-06-23 thomas wclear(view->window);
919 40236d76 2022-06-23 thomas
920 40236d76 2022-06-23 thomas view->nlines = nlines;
921 40236d76 2022-06-23 thomas view->ncols = ncols;
922 40236d76 2022-06-23 thomas view->lines = LINES;
923 40236d76 2022-06-23 thomas view->cols = COLS;
924 40236d76 2022-06-23 thomas
925 5c60c32a 2018-10-18 stsp return NULL;
926 ea0bff04 2022-07-19 thomas }
927 ea0bff04 2022-07-19 thomas
928 ea0bff04 2022-07-19 thomas static const struct got_error *
929 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
930 ea0bff04 2022-07-19 thomas {
931 ea0bff04 2022-07-19 thomas struct tog_log_view_state *s = &view->state.log;
932 ea0bff04 2022-07-19 thomas const struct got_error *err = NULL;
933 ea0bff04 2022-07-19 thomas int n = s->selected_entry->idx + view->lines - s->selected;
934 ea0bff04 2022-07-19 thomas
935 ea0bff04 2022-07-19 thomas /*
936 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
937 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
938 ea0bff04 2022-07-19 thomas */
939 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
940 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
941 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
942 ea0bff04 2022-07-19 thomas }
943 ea0bff04 2022-07-19 thomas
944 ea0bff04 2022-07-19 thomas return err;
945 97cb21cd 2022-07-12 thomas }
946 97cb21cd 2022-07-12 thomas
947 97cb21cd 2022-07-12 thomas static void
948 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
949 97cb21cd 2022-07-12 thomas {
950 97cb21cd 2022-07-12 thomas if (n == 0)
951 97cb21cd 2022-07-12 thomas return;
952 97cb21cd 2022-07-12 thomas
953 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
954 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
955 97cb21cd 2022-07-12 thomas view->parent->offset += n;
956 97cb21cd 2022-07-12 thomas else
957 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
958 97cb21cd 2022-07-12 thomas } else if (view->offset) {
959 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
960 97cb21cd 2022-07-12 thomas view->offset -= n;
961 97cb21cd 2022-07-12 thomas else
962 97cb21cd 2022-07-12 thomas view->offset = 0;
963 97cb21cd 2022-07-12 thomas }
964 53d2bdd3 2022-07-10 thomas }
965 53d2bdd3 2022-07-10 thomas
966 53d2bdd3 2022-07-10 thomas static const struct got_error *
967 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
968 53d2bdd3 2022-07-10 thomas {
969 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
970 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
971 53d2bdd3 2022-07-10 thomas
972 53d2bdd3 2022-07-10 thomas if (view->parent)
973 53d2bdd3 2022-07-10 thomas v = view->parent;
974 53d2bdd3 2022-07-10 thomas else
975 53d2bdd3 2022-07-10 thomas v = view;
976 53d2bdd3 2022-07-10 thomas
977 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
978 53d2bdd3 2022-07-10 thomas return NULL;
979 53d2bdd3 2022-07-10 thomas
980 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
981 53d2bdd3 2022-07-10 thomas
982 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
983 ae98518f 2022-07-12 thomas int y = v->child->begin_y;
984 ae98518f 2022-07-12 thomas
985 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
986 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
987 53d2bdd3 2022-07-10 thomas if (view->parent)
988 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
989 53d2bdd3 2022-07-10 thomas else
990 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
991 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
992 53d2bdd3 2022-07-10 thomas view->count = 0;
993 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
994 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
995 53d2bdd3 2022-07-10 thomas view->count = 0;
996 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
997 53d2bdd3 2022-07-10 thomas }
998 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
999 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1000 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1001 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1002 53d2bdd3 2022-07-10 thomas if (err)
1003 53d2bdd3 2022-07-10 thomas return err;
1004 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1005 fe731b51 2022-07-14 thomas if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1006 ae98518f 2022-07-12 thomas v->child->nscrolled = y - v->child->begin_y;
1007 fe731b51 2022-07-14 thomas else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1008 fe731b51 2022-07-14 thomas v->nscrolled = v->child->begin_y - y;
1009 53d2bdd3 2022-07-10 thomas } else {
1010 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1011 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1012 53d2bdd3 2022-07-10 thomas if (view->parent)
1013 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1014 53d2bdd3 2022-07-10 thomas else
1015 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1016 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1017 53d2bdd3 2022-07-10 thomas view->count = 0;
1018 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1019 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1020 53d2bdd3 2022-07-10 thomas view->count = 0;
1021 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1022 53d2bdd3 2022-07-10 thomas }
1023 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1024 53d2bdd3 2022-07-10 thomas }
1025 53d2bdd3 2022-07-10 thomas
1026 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1027 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1028 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1029 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1030 53d2bdd3 2022-07-10 thomas
1031 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1032 53d2bdd3 2022-07-10 thomas if (err)
1033 53d2bdd3 2022-07-10 thomas return err;
1034 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1035 53d2bdd3 2022-07-10 thomas if (err)
1036 53d2bdd3 2022-07-10 thomas return err;
1037 53d2bdd3 2022-07-10 thomas
1038 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1039 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1040 53d2bdd3 2022-07-10 thomas if (err)
1041 53d2bdd3 2022-07-10 thomas return err;
1042 53d2bdd3 2022-07-10 thomas }
1043 53d2bdd3 2022-07-10 thomas
1044 fe731b51 2022-07-14 thomas if (v->nscrolled)
1045 53d2bdd3 2022-07-10 thomas err = request_log_commits(v);
1046 fe731b51 2022-07-14 thomas else if (v->child->nscrolled)
1047 53d2bdd3 2022-07-10 thomas err = request_log_commits(v->child);
1048 53d2bdd3 2022-07-10 thomas
1049 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1050 53d2bdd3 2022-07-10 thomas
1051 53d2bdd3 2022-07-10 thomas return err;
1052 53d2bdd3 2022-07-10 thomas }
1053 53d2bdd3 2022-07-10 thomas
1054 53d2bdd3 2022-07-10 thomas static void
1055 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1056 53d2bdd3 2022-07-10 thomas {
1057 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1058 53d2bdd3 2022-07-10 thomas
1059 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1060 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1061 669b5ffa 2018-10-07 stsp }
1062 669b5ffa 2018-10-07 stsp
1063 669b5ffa 2018-10-07 stsp static const struct got_error *
1064 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1065 669b5ffa 2018-10-07 stsp {
1066 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1067 669b5ffa 2018-10-07 stsp
1068 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1069 669b5ffa 2018-10-07 stsp return NULL;
1070 669b5ffa 2018-10-07 stsp
1071 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1072 669b5ffa 2018-10-07 stsp view->child = NULL;
1073 669b5ffa 2018-10-07 stsp return err;
1074 669b5ffa 2018-10-07 stsp }
1075 669b5ffa 2018-10-07 stsp
1076 40236d76 2022-06-23 thomas static const struct got_error *
1077 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1078 669b5ffa 2018-10-07 stsp {
1079 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1080 53d2bdd3 2022-07-10 thomas
1081 669b5ffa 2018-10-07 stsp view->child = child;
1082 669b5ffa 2018-10-07 stsp child->parent = view;
1083 40236d76 2022-06-23 thomas
1084 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1085 53d2bdd3 2022-07-10 thomas if (err)
1086 53d2bdd3 2022-07-10 thomas return err;
1087 53d2bdd3 2022-07-10 thomas
1088 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1089 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1090 53d2bdd3 2022-07-10 thomas
1091 53d2bdd3 2022-07-10 thomas return err;
1092 bfddd0d9 2018-09-29 stsp }
1093 bfddd0d9 2018-09-29 stsp
1094 34bc9ec9 2019-02-22 stsp static void
1095 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1096 25791caa 2018-10-24 stsp {
1097 25791caa 2018-10-24 stsp int cols, lines;
1098 25791caa 2018-10-24 stsp struct winsize size;
1099 25791caa 2018-10-24 stsp
1100 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1101 25791caa 2018-10-24 stsp cols = 80; /* Default */
1102 25791caa 2018-10-24 stsp lines = 24;
1103 25791caa 2018-10-24 stsp } else {
1104 25791caa 2018-10-24 stsp cols = size.ws_col;
1105 25791caa 2018-10-24 stsp lines = size.ws_row;
1106 25791caa 2018-10-24 stsp }
1107 25791caa 2018-10-24 stsp resize_term(lines, cols);
1108 2b49a8ae 2019-06-22 stsp }
1109 2b49a8ae 2019-06-22 stsp
1110 2b49a8ae 2019-06-22 stsp static const struct got_error *
1111 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1112 2b49a8ae 2019-06-22 stsp {
1113 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1114 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1115 2b49a8ae 2019-06-22 stsp char pattern[1024];
1116 2b49a8ae 2019-06-22 stsp int ret;
1117 c0c4acc8 2021-01-24 stsp
1118 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1119 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1120 c0c4acc8 2021-01-24 stsp view->searching = 0;
1121 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1122 c0c4acc8 2021-01-24 stsp }
1123 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1124 2b49a8ae 2019-06-22 stsp
1125 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1126 2b49a8ae 2019-06-22 stsp return NULL;
1127 2b49a8ae 2019-06-22 stsp
1128 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1129 a5d43cac 2022-07-01 thomas v = view->child;
1130 2b49a8ae 2019-06-22 stsp
1131 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1132 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1133 a5d43cac 2022-07-01 thomas
1134 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1135 2b49a8ae 2019-06-22 stsp nocbreak();
1136 2b49a8ae 2019-06-22 stsp echo();
1137 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1138 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1139 2b49a8ae 2019-06-22 stsp cbreak();
1140 2b49a8ae 2019-06-22 stsp noecho();
1141 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1142 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1143 2b49a8ae 2019-06-22 stsp return NULL;
1144 2b49a8ae 2019-06-22 stsp
1145 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1146 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1147 7c32bd05 2019-06-22 stsp if (err) {
1148 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1149 7c32bd05 2019-06-22 stsp return err;
1150 7c32bd05 2019-06-22 stsp }
1151 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1152 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1153 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1154 2b49a8ae 2019-06-22 stsp view->search_next(view);
1155 2b49a8ae 2019-06-22 stsp }
1156 2b49a8ae 2019-06-22 stsp
1157 2b49a8ae 2019-06-22 stsp return NULL;
1158 64486692 2022-07-07 thomas }
1159 64486692 2022-07-07 thomas
1160 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1161 64486692 2022-07-07 thomas static const struct got_error *
1162 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1163 64486692 2022-07-07 thomas {
1164 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1165 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1166 64486692 2022-07-07 thomas
1167 64486692 2022-07-07 thomas if (view->parent)
1168 64486692 2022-07-07 thomas v = view->parent;
1169 64486692 2022-07-07 thomas else
1170 64486692 2022-07-07 thomas v = view;
1171 64486692 2022-07-07 thomas
1172 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1173 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1174 ddbc4d37 2022-07-12 thomas else
1175 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1176 64486692 2022-07-07 thomas
1177 ddbc4d37 2022-07-12 thomas if (!v->child)
1178 ddbc4d37 2022-07-12 thomas return NULL;
1179 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1180 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1181 ddbc4d37 2022-07-12 thomas
1182 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1183 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1184 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1185 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1186 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1187 64486692 2022-07-07 thomas
1188 ddbc4d37 2022-07-12 thomas
1189 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1190 64486692 2022-07-07 thomas v->ncols = COLS;
1191 64486692 2022-07-07 thomas v->child->ncols = COLS;
1192 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1193 64486692 2022-07-07 thomas
1194 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1195 64486692 2022-07-07 thomas if (err)
1196 64486692 2022-07-07 thomas return err;
1197 64486692 2022-07-07 thomas }
1198 64486692 2022-07-07 thomas v->child->mode = v->mode;
1199 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1200 64486692 2022-07-07 thomas v->focus_child = 1;
1201 64486692 2022-07-07 thomas
1202 64486692 2022-07-07 thomas err = view_fullscreen(v);
1203 64486692 2022-07-07 thomas if (err)
1204 64486692 2022-07-07 thomas return err;
1205 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1206 64486692 2022-07-07 thomas if (err)
1207 64486692 2022-07-07 thomas return err;
1208 64486692 2022-07-07 thomas
1209 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1210 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1211 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1212 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1213 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1214 ddbc4d37 2022-07-12 thomas } else {
1215 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1216 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1217 64486692 2022-07-07 thomas }
1218 ae98518f 2022-07-12 thomas if (v->type == TOG_VIEW_LOG && v->nscrolled)
1219 ddbc4d37 2022-07-12 thomas err = request_log_commits(v);
1220 ae98518f 2022-07-12 thomas else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1221 ddbc4d37 2022-07-12 thomas err = request_log_commits(v->child);
1222 64486692 2022-07-07 thomas
1223 64486692 2022-07-07 thomas return err;
1224 0cf4efb1 2018-09-29 stsp }
1225 6d0fee91 2018-08-01 stsp
1226 07b0611c 2022-06-23 thomas /*
1227 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1228 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1229 07b0611c 2022-06-23 thomas */
1230 07b0611c 2022-06-23 thomas static int
1231 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1232 07b0611c 2022-06-23 thomas {
1233 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1234 a5d43cac 2022-07-01 thomas int x, n = 0;
1235 07b0611c 2022-06-23 thomas
1236 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1237 a5d43cac 2022-07-01 thomas v = view->child;
1238 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1239 a5d43cac 2022-07-01 thomas v = view->parent;
1240 a5d43cac 2022-07-01 thomas
1241 07b0611c 2022-06-23 thomas view->count = 0;
1242 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1243 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1244 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1245 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1246 07b0611c 2022-06-23 thomas
1247 07b0611c 2022-06-23 thomas do {
1248 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1249 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1250 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1251 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1252 a5d43cac 2022-07-01 thomas }
1253 a5d43cac 2022-07-01 thomas
1254 07b0611c 2022-06-23 thomas /*
1255 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1256 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1257 07b0611c 2022-06-23 thomas */
1258 07b0611c 2022-06-23 thomas if (n >= 9999999)
1259 07b0611c 2022-06-23 thomas n = 9999999;
1260 07b0611c 2022-06-23 thomas else
1261 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1262 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1263 07b0611c 2022-06-23 thomas
1264 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1265 07b0611c 2022-06-23 thomas view->count = n;
1266 07b0611c 2022-06-23 thomas
1267 07b0611c 2022-06-23 thomas return c;
1268 07b0611c 2022-06-23 thomas }
1269 07b0611c 2022-06-23 thomas
1270 0cf4efb1 2018-09-29 stsp static const struct got_error *
1271 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1272 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1273 e5a0f69f 2018-08-18 stsp {
1274 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1275 669b5ffa 2018-10-07 stsp struct tog_view *v;
1276 1a76625f 2018-10-22 stsp int ch, errcode;
1277 e5a0f69f 2018-08-18 stsp
1278 e5a0f69f 2018-08-18 stsp *new = NULL;
1279 8f4ed634 2020-03-26 stsp
1280 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1281 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1282 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1283 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1284 07b0611c 2022-06-23 thomas view->count = 0;
1285 07b0611c 2022-06-23 thomas }
1286 e5a0f69f 2018-08-18 stsp
1287 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1288 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1289 82954512 2020-02-03 stsp if (errcode)
1290 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1291 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1292 3da8ef85 2021-09-21 stsp sched_yield();
1293 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1294 82954512 2020-02-03 stsp if (errcode)
1295 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1296 82954512 2020-02-03 stsp "pthread_mutex_lock");
1297 60493ae3 2019-06-20 stsp view->search_next(view);
1298 60493ae3 2019-06-20 stsp return NULL;
1299 60493ae3 2019-06-20 stsp }
1300 60493ae3 2019-06-20 stsp
1301 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1302 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1303 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1304 1a76625f 2018-10-22 stsp if (errcode)
1305 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1306 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1307 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1308 634cb454 2022-07-03 thomas cbreak();
1309 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1310 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1311 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1312 634cb454 2022-07-03 thomas view->count = 0;
1313 634cb454 2022-07-03 thomas else
1314 634cb454 2022-07-03 thomas ch = view->ch;
1315 634cb454 2022-07-03 thomas } else {
1316 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1317 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1318 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1319 07b0611c 2022-06-23 thomas }
1320 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1321 1a76625f 2018-10-22 stsp if (errcode)
1322 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1323 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1324 25791caa 2018-10-24 stsp
1325 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1326 25791caa 2018-10-24 stsp tog_resizeterm();
1327 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1328 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1329 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1330 25791caa 2018-10-24 stsp err = view_resize(v);
1331 25791caa 2018-10-24 stsp if (err)
1332 25791caa 2018-10-24 stsp return err;
1333 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1334 25791caa 2018-10-24 stsp if (err)
1335 25791caa 2018-10-24 stsp return err;
1336 cdfcfb03 2020-12-06 stsp if (v->child) {
1337 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1338 cdfcfb03 2020-12-06 stsp if (err)
1339 cdfcfb03 2020-12-06 stsp return err;
1340 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1341 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1342 cdfcfb03 2020-12-06 stsp if (err)
1343 cdfcfb03 2020-12-06 stsp return err;
1344 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1345 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1346 53d2bdd3 2022-07-10 thomas if (err)
1347 53d2bdd3 2022-07-10 thomas return err;
1348 53d2bdd3 2022-07-10 thomas }
1349 cdfcfb03 2020-12-06 stsp }
1350 25791caa 2018-10-24 stsp }
1351 25791caa 2018-10-24 stsp }
1352 25791caa 2018-10-24 stsp
1353 e5a0f69f 2018-08-18 stsp switch (ch) {
1354 1e37a5c2 2019-05-12 jcs case '\t':
1355 07b0611c 2022-06-23 thomas view->count = 0;
1356 1e37a5c2 2019-05-12 jcs if (view->child) {
1357 e78dc838 2020-12-04 stsp view->focussed = 0;
1358 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1359 e78dc838 2020-12-04 stsp view->focus_child = 1;
1360 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1361 e78dc838 2020-12-04 stsp view->focussed = 0;
1362 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1363 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1364 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1365 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1366 a5d43cac 2022-07-01 thomas view->parent->type == TOG_VIEW_LOG) {
1367 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1368 a5d43cac 2022-07-01 thomas if (err)
1369 a5d43cac 2022-07-01 thomas return err;
1370 a5d43cac 2022-07-01 thomas }
1371 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1372 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1373 a5d43cac 2022-07-01 thomas if (err)
1374 a5d43cac 2022-07-01 thomas return err;
1375 a5d43cac 2022-07-01 thomas }
1376 1e37a5c2 2019-05-12 jcs }
1377 1e37a5c2 2019-05-12 jcs break;
1378 1e37a5c2 2019-05-12 jcs case 'q':
1379 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1380 a5d43cac 2022-07-01 thomas if (view->parent->type == TOG_VIEW_LOG) {
1381 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1382 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1383 a5d43cac 2022-07-01 thomas if (err)
1384 a5d43cac 2022-07-01 thomas break;
1385 a5d43cac 2022-07-01 thomas }
1386 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1387 a5d43cac 2022-07-01 thomas }
1388 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1389 9970f7fc 2020-12-03 stsp view->dying = 1;
1390 1e37a5c2 2019-05-12 jcs break;
1391 1e37a5c2 2019-05-12 jcs case 'Q':
1392 1e37a5c2 2019-05-12 jcs *done = 1;
1393 1e37a5c2 2019-05-12 jcs break;
1394 1c5e5faa 2022-06-23 thomas case 'F':
1395 07b0611c 2022-06-23 thomas view->count = 0;
1396 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1397 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1398 1e37a5c2 2019-05-12 jcs break;
1399 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1400 e78dc838 2020-12-04 stsp view->focussed = 0;
1401 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1402 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1403 53d2bdd3 2022-07-10 thomas } else {
1404 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1405 53d2bdd3 2022-07-10 thomas if (!err)
1406 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1407 53d2bdd3 2022-07-10 thomas }
1408 1e37a5c2 2019-05-12 jcs if (err)
1409 1e37a5c2 2019-05-12 jcs break;
1410 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1411 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1412 1e37a5c2 2019-05-12 jcs } else {
1413 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1414 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1415 e78dc838 2020-12-04 stsp view->focussed = 1;
1416 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1417 1e37a5c2 2019-05-12 jcs } else {
1418 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1419 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1420 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1421 53d2bdd3 2022-07-10 thomas if (!err)
1422 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1423 669b5ffa 2018-10-07 stsp }
1424 1e37a5c2 2019-05-12 jcs if (err)
1425 1e37a5c2 2019-05-12 jcs break;
1426 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1427 a5d43cac 2022-07-01 thomas }
1428 a5d43cac 2022-07-01 thomas if (err)
1429 a5d43cac 2022-07-01 thomas break;
1430 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG) {
1431 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
1432 a5d43cac 2022-07-01 thomas if (err)
1433 a5d43cac 2022-07-01 thomas break;
1434 1e37a5c2 2019-05-12 jcs }
1435 a5d43cac 2022-07-01 thomas if (view->parent)
1436 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1437 a5d43cac 2022-07-01 thomas if (!err)
1438 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1439 1e37a5c2 2019-05-12 jcs break;
1440 64486692 2022-07-07 thomas case 'S':
1441 53d2bdd3 2022-07-10 thomas view->count = 0;
1442 64486692 2022-07-07 thomas err = switch_split(view);
1443 53d2bdd3 2022-07-10 thomas break;
1444 53d2bdd3 2022-07-10 thomas case '-':
1445 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1446 64486692 2022-07-07 thomas break;
1447 53d2bdd3 2022-07-10 thomas case '+':
1448 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1449 53d2bdd3 2022-07-10 thomas break;
1450 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1451 60493ae3 2019-06-20 stsp break;
1452 60493ae3 2019-06-20 stsp case '/':
1453 07b0611c 2022-06-23 thomas view->count = 0;
1454 60493ae3 2019-06-20 stsp if (view->search_start)
1455 2b49a8ae 2019-06-22 stsp view_search_start(view);
1456 60493ae3 2019-06-20 stsp else
1457 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1458 1e37a5c2 2019-05-12 jcs break;
1459 b1bf1435 2019-06-21 stsp case 'N':
1460 60493ae3 2019-06-20 stsp case 'n':
1461 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1462 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1463 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1464 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1465 60493ae3 2019-06-20 stsp view->search_next(view);
1466 60493ae3 2019-06-20 stsp } else
1467 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1468 adf4c9e0 2022-07-03 thomas break;
1469 adf4c9e0 2022-07-03 thomas case 'A':
1470 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1471 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1472 adf4c9e0 2022-07-03 thomas else
1473 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1474 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1475 adf4c9e0 2022-07-03 thomas if (v->reset) {
1476 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1477 adf4c9e0 2022-07-03 thomas if (err)
1478 adf4c9e0 2022-07-03 thomas return err;
1479 adf4c9e0 2022-07-03 thomas }
1480 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1481 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1482 adf4c9e0 2022-07-03 thomas if (err)
1483 adf4c9e0 2022-07-03 thomas return err;
1484 adf4c9e0 2022-07-03 thomas }
1485 adf4c9e0 2022-07-03 thomas }
1486 60493ae3 2019-06-20 stsp break;
1487 1e37a5c2 2019-05-12 jcs default:
1488 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1489 1e37a5c2 2019-05-12 jcs break;
1490 e5a0f69f 2018-08-18 stsp }
1491 e5a0f69f 2018-08-18 stsp
1492 e5a0f69f 2018-08-18 stsp return err;
1493 bcbd79e2 2018-08-19 stsp }
1494 bcbd79e2 2018-08-19 stsp
1495 ef20f542 2022-06-26 thomas static int
1496 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1497 a3404814 2018-09-02 stsp {
1498 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1499 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1500 669b5ffa 2018-10-07 stsp return 0;
1501 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1502 669b5ffa 2018-10-07 stsp return 0;
1503 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1504 a3404814 2018-09-02 stsp return 0;
1505 a3404814 2018-09-02 stsp
1506 669b5ffa 2018-10-07 stsp return view->focussed;
1507 a3404814 2018-09-02 stsp }
1508 a3404814 2018-09-02 stsp
1509 bcbd79e2 2018-08-19 stsp static const struct got_error *
1510 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1511 e5a0f69f 2018-08-18 stsp {
1512 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1513 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1514 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1515 64486692 2022-07-07 thomas char *mode;
1516 fd823528 2018-10-22 stsp int fast_refresh = 10;
1517 1a76625f 2018-10-22 stsp int done = 0, errcode;
1518 e5a0f69f 2018-08-18 stsp
1519 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1520 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1521 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1522 64486692 2022-07-07 thomas else
1523 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1524 64486692 2022-07-07 thomas
1525 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1526 1a76625f 2018-10-22 stsp if (errcode)
1527 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1528 1a76625f 2018-10-22 stsp
1529 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1530 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1531 e5a0f69f 2018-08-18 stsp
1532 1004088d 2018-09-29 stsp view->focussed = 1;
1533 878940b7 2018-09-29 stsp err = view->show(view);
1534 0cf4efb1 2018-09-29 stsp if (err)
1535 0cf4efb1 2018-09-29 stsp return err;
1536 0cf4efb1 2018-09-29 stsp update_panels();
1537 0cf4efb1 2018-09-29 stsp doupdate();
1538 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1539 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1540 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1541 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1542 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1543 fd823528 2018-10-22 stsp
1544 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1545 e5a0f69f 2018-08-18 stsp if (err)
1546 e5a0f69f 2018-08-18 stsp break;
1547 9970f7fc 2020-12-03 stsp if (view->dying) {
1548 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1549 669b5ffa 2018-10-07 stsp
1550 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1551 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1552 9970f7fc 2020-12-03 stsp entry);
1553 e78dc838 2020-12-04 stsp else if (view->parent)
1554 669b5ffa 2018-10-07 stsp prev = view->parent;
1555 669b5ffa 2018-10-07 stsp
1556 e78dc838 2020-12-04 stsp if (view->parent) {
1557 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1558 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1559 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1560 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1561 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1562 40236d76 2022-06-23 thomas if (err)
1563 40236d76 2022-06-23 thomas break;
1564 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1565 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1566 e78dc838 2020-12-04 stsp } else
1567 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1568 669b5ffa 2018-10-07 stsp
1569 9970f7fc 2020-12-03 stsp err = view_close(view);
1570 fb59748f 2020-12-05 stsp if (err)
1571 e5a0f69f 2018-08-18 stsp goto done;
1572 669b5ffa 2018-10-07 stsp
1573 e78dc838 2020-12-04 stsp view = NULL;
1574 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1575 e78dc838 2020-12-04 stsp if (v->focussed)
1576 e78dc838 2020-12-04 stsp break;
1577 0cf4efb1 2018-09-29 stsp }
1578 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1579 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1580 e78dc838 2020-12-04 stsp if (prev)
1581 e78dc838 2020-12-04 stsp view = prev;
1582 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1583 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1584 e78dc838 2020-12-04 stsp tog_view_list_head);
1585 e78dc838 2020-12-04 stsp }
1586 e78dc838 2020-12-04 stsp if (view) {
1587 e78dc838 2020-12-04 stsp if (view->focus_child) {
1588 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1589 e78dc838 2020-12-04 stsp view = view->child;
1590 e78dc838 2020-12-04 stsp } else
1591 e78dc838 2020-12-04 stsp view->focussed = 1;
1592 e78dc838 2020-12-04 stsp }
1593 e78dc838 2020-12-04 stsp }
1594 e5a0f69f 2018-08-18 stsp }
1595 bcbd79e2 2018-08-19 stsp if (new_view) {
1596 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1597 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1598 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1599 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1600 86c66b02 2018-10-18 stsp continue;
1601 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1602 86c66b02 2018-10-18 stsp err = view_close(v);
1603 86c66b02 2018-10-18 stsp if (err)
1604 86c66b02 2018-10-18 stsp goto done;
1605 86c66b02 2018-10-18 stsp break;
1606 86c66b02 2018-10-18 stsp }
1607 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1608 fed7eaa8 2018-10-24 stsp view = new_view;
1609 7cd52833 2022-06-23 thomas }
1610 669b5ffa 2018-10-07 stsp if (view) {
1611 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1612 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1613 e78dc838 2020-12-04 stsp view = view->child;
1614 e78dc838 2020-12-04 stsp } else {
1615 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1616 e78dc838 2020-12-04 stsp view = view->parent;
1617 1a76625f 2018-10-22 stsp }
1618 e78dc838 2020-12-04 stsp show_panel(view->panel);
1619 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1620 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1621 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1622 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1623 669b5ffa 2018-10-07 stsp if (err)
1624 1a76625f 2018-10-22 stsp goto done;
1625 669b5ffa 2018-10-07 stsp }
1626 669b5ffa 2018-10-07 stsp err = view->show(view);
1627 0cf4efb1 2018-09-29 stsp if (err)
1628 1a76625f 2018-10-22 stsp goto done;
1629 669b5ffa 2018-10-07 stsp if (view->child) {
1630 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1631 669b5ffa 2018-10-07 stsp if (err)
1632 1a76625f 2018-10-22 stsp goto done;
1633 669b5ffa 2018-10-07 stsp }
1634 1a76625f 2018-10-22 stsp update_panels();
1635 1a76625f 2018-10-22 stsp doupdate();
1636 0cf4efb1 2018-09-29 stsp }
1637 e5a0f69f 2018-08-18 stsp }
1638 e5a0f69f 2018-08-18 stsp done:
1639 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1640 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1641 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1642 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1643 f2d749db 2022-07-12 thomas close_err = view_close(view);
1644 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1645 f2d749db 2022-07-12 thomas err = close_err;
1646 e5a0f69f 2018-08-18 stsp }
1647 1a76625f 2018-10-22 stsp
1648 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1649 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1650 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1651 1a76625f 2018-10-22 stsp
1652 e5a0f69f 2018-08-18 stsp return err;
1653 ea5e7bb5 2018-08-01 stsp }
1654 ea5e7bb5 2018-08-01 stsp
1655 4ed7e80c 2018-05-20 stsp __dead static void
1656 9f7d7167 2018-04-29 stsp usage_log(void)
1657 9f7d7167 2018-04-29 stsp {
1658 80ddbec8 2018-04-29 stsp endwin();
1659 c70c5802 2018-08-01 stsp fprintf(stderr,
1660 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1661 9f7d7167 2018-04-29 stsp getprogname());
1662 9f7d7167 2018-04-29 stsp exit(1);
1663 80ddbec8 2018-04-29 stsp }
1664 80ddbec8 2018-04-29 stsp
1665 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1666 80ddbec8 2018-04-29 stsp static const struct got_error *
1667 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1668 963b370f 2018-05-20 stsp {
1669 00dfcb92 2018-06-11 stsp char *vis = NULL;
1670 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1671 963b370f 2018-05-20 stsp
1672 963b370f 2018-05-20 stsp *ws = NULL;
1673 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1674 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1675 00dfcb92 2018-06-11 stsp int vislen;
1676 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1677 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1678 00dfcb92 2018-06-11 stsp
1679 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1680 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1681 00dfcb92 2018-06-11 stsp if (err)
1682 00dfcb92 2018-06-11 stsp return err;
1683 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1684 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1685 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1686 a7f50699 2018-06-11 stsp goto done;
1687 a7f50699 2018-06-11 stsp }
1688 00dfcb92 2018-06-11 stsp }
1689 963b370f 2018-05-20 stsp
1690 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1691 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1692 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1693 a7f50699 2018-06-11 stsp goto done;
1694 a7f50699 2018-06-11 stsp }
1695 963b370f 2018-05-20 stsp
1696 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1697 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1698 a7f50699 2018-06-11 stsp done:
1699 00dfcb92 2018-06-11 stsp free(vis);
1700 963b370f 2018-05-20 stsp if (err) {
1701 963b370f 2018-05-20 stsp free(*ws);
1702 963b370f 2018-05-20 stsp *ws = NULL;
1703 963b370f 2018-05-20 stsp *wlen = 0;
1704 963b370f 2018-05-20 stsp }
1705 963b370f 2018-05-20 stsp return err;
1706 05171be4 2022-06-23 thomas }
1707 05171be4 2022-06-23 thomas
1708 05171be4 2022-06-23 thomas static const struct got_error *
1709 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1710 05171be4 2022-06-23 thomas {
1711 05171be4 2022-06-23 thomas char *dst;
1712 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1713 05171be4 2022-06-23 thomas
1714 05171be4 2022-06-23 thomas *ptr = NULL;
1715 05171be4 2022-06-23 thomas n = len = strlen(src);
1716 83316834 2022-06-23 thomas dst = malloc(n + 1);
1717 05171be4 2022-06-23 thomas if (dst == NULL)
1718 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1719 05171be4 2022-06-23 thomas
1720 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1721 05171be4 2022-06-23 thomas const char c = src[idx];
1722 05171be4 2022-06-23 thomas
1723 05171be4 2022-06-23 thomas if (c == '\t') {
1724 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1725 b235ad5d 2022-06-23 thomas char *p;
1726 b235ad5d 2022-06-23 thomas
1727 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1728 83316834 2022-06-23 thomas if (p == NULL) {
1729 83316834 2022-06-23 thomas free(dst);
1730 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1731 83316834 2022-06-23 thomas
1732 83316834 2022-06-23 thomas }
1733 83316834 2022-06-23 thomas dst = p;
1734 05171be4 2022-06-23 thomas n += nb;
1735 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1736 05171be4 2022-06-23 thomas sz += nb;
1737 05171be4 2022-06-23 thomas } else
1738 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1739 05171be4 2022-06-23 thomas ++idx;
1740 05171be4 2022-06-23 thomas }
1741 05171be4 2022-06-23 thomas
1742 05171be4 2022-06-23 thomas dst[sz] = '\0';
1743 05171be4 2022-06-23 thomas *ptr = dst;
1744 05171be4 2022-06-23 thomas return NULL;
1745 963b370f 2018-05-20 stsp }
1746 963b370f 2018-05-20 stsp
1747 8d208d34 2022-06-23 thomas /*
1748 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1749 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1750 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1751 8d208d34 2022-06-23 thomas * *rcol.
1752 f91a2b48 2022-06-23 thomas */
1753 8d208d34 2022-06-23 thomas static int
1754 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1755 8d208d34 2022-06-23 thomas {
1756 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1757 f91a2b48 2022-06-23 thomas
1758 8d208d34 2022-06-23 thomas if (n == 0) {
1759 8d208d34 2022-06-23 thomas *rcol = cols;
1760 8d208d34 2022-06-23 thomas return off;
1761 8d208d34 2022-06-23 thomas }
1762 f91a2b48 2022-06-23 thomas
1763 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1764 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1765 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1766 8d208d34 2022-06-23 thomas else
1767 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1768 f91a2b48 2022-06-23 thomas
1769 8d208d34 2022-06-23 thomas if (width == -1) {
1770 8d208d34 2022-06-23 thomas width = 1;
1771 8d208d34 2022-06-23 thomas wline[i] = L'.';
1772 f91a2b48 2022-06-23 thomas }
1773 f91a2b48 2022-06-23 thomas
1774 8d208d34 2022-06-23 thomas if (cols + width > n)
1775 8d208d34 2022-06-23 thomas break;
1776 8d208d34 2022-06-23 thomas cols += width;
1777 f91a2b48 2022-06-23 thomas }
1778 f91a2b48 2022-06-23 thomas
1779 8d208d34 2022-06-23 thomas *rcol = cols;
1780 8d208d34 2022-06-23 thomas return i;
1781 f91a2b48 2022-06-23 thomas }
1782 f91a2b48 2022-06-23 thomas
1783 f91a2b48 2022-06-23 thomas /*
1784 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1785 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1786 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1787 f91a2b48 2022-06-23 thomas */
1788 f91a2b48 2022-06-23 thomas static const struct got_error *
1789 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1790 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1791 f91a2b48 2022-06-23 thomas {
1792 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1793 8d208d34 2022-06-23 thomas int cols;
1794 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1795 05171be4 2022-06-23 thomas char *exstr = NULL;
1796 963b370f 2018-05-20 stsp size_t wlen;
1797 8d208d34 2022-06-23 thomas int i, scrollx;
1798 963b370f 2018-05-20 stsp
1799 963b370f 2018-05-20 stsp *wlinep = NULL;
1800 b700b5d6 2018-07-10 stsp *widthp = 0;
1801 963b370f 2018-05-20 stsp
1802 05171be4 2022-06-23 thomas if (expand) {
1803 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1804 05171be4 2022-06-23 thomas if (err)
1805 05171be4 2022-06-23 thomas return err;
1806 05171be4 2022-06-23 thomas }
1807 05171be4 2022-06-23 thomas
1808 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1809 05171be4 2022-06-23 thomas free(exstr);
1810 963b370f 2018-05-20 stsp if (err)
1811 963b370f 2018-05-20 stsp return err;
1812 963b370f 2018-05-20 stsp
1813 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1814 f91a2b48 2022-06-23 thomas
1815 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1816 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1817 3f670bfb 2020-12-10 stsp wlen--;
1818 3f670bfb 2020-12-10 stsp }
1819 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1820 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1821 3f670bfb 2020-12-10 stsp wlen--;
1822 3f670bfb 2020-12-10 stsp }
1823 3f670bfb 2020-12-10 stsp
1824 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1825 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1826 27a741e5 2019-09-11 stsp
1827 b700b5d6 2018-07-10 stsp if (widthp)
1828 b700b5d6 2018-07-10 stsp *widthp = cols;
1829 f91a2b48 2022-06-23 thomas if (scrollxp)
1830 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1831 963b370f 2018-05-20 stsp if (err)
1832 963b370f 2018-05-20 stsp free(wline);
1833 963b370f 2018-05-20 stsp else
1834 963b370f 2018-05-20 stsp *wlinep = wline;
1835 963b370f 2018-05-20 stsp return err;
1836 963b370f 2018-05-20 stsp }
1837 963b370f 2018-05-20 stsp
1838 8b473291 2019-02-21 stsp static const struct got_error*
1839 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1840 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1841 8b473291 2019-02-21 stsp {
1842 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1843 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1844 8b473291 2019-02-21 stsp char *s;
1845 8b473291 2019-02-21 stsp const char *name;
1846 8b473291 2019-02-21 stsp
1847 8b473291 2019-02-21 stsp *refs_str = NULL;
1848 8b473291 2019-02-21 stsp
1849 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1850 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1851 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1852 52b5abe1 2019-08-13 stsp int cmp;
1853 52b5abe1 2019-08-13 stsp
1854 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1855 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1856 8b473291 2019-02-21 stsp continue;
1857 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1858 8b473291 2019-02-21 stsp name += 5;
1859 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1860 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1861 7143d404 2019-03-12 stsp continue;
1862 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1863 8b473291 2019-02-21 stsp name += 6;
1864 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1865 8b473291 2019-02-21 stsp name += 8;
1866 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1867 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1868 79cc719f 2020-04-24 stsp continue;
1869 79cc719f 2020-04-24 stsp }
1870 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1871 48cae60d 2020-09-22 stsp if (err)
1872 48cae60d 2020-09-22 stsp break;
1873 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1874 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1875 5d844a1e 2019-08-13 stsp if (err) {
1876 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1877 48cae60d 2020-09-22 stsp free(ref_id);
1878 5d844a1e 2019-08-13 stsp break;
1879 48cae60d 2020-09-22 stsp }
1880 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1881 5d844a1e 2019-08-13 stsp err = NULL;
1882 5d844a1e 2019-08-13 stsp tag = NULL;
1883 5d844a1e 2019-08-13 stsp }
1884 52b5abe1 2019-08-13 stsp }
1885 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1886 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1887 48cae60d 2020-09-22 stsp free(ref_id);
1888 52b5abe1 2019-08-13 stsp if (tag)
1889 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1890 52b5abe1 2019-08-13 stsp if (cmp != 0)
1891 52b5abe1 2019-08-13 stsp continue;
1892 8b473291 2019-02-21 stsp s = *refs_str;
1893 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1894 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1895 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1896 8b473291 2019-02-21 stsp free(s);
1897 8b473291 2019-02-21 stsp *refs_str = NULL;
1898 8b473291 2019-02-21 stsp break;
1899 8b473291 2019-02-21 stsp }
1900 8b473291 2019-02-21 stsp free(s);
1901 8b473291 2019-02-21 stsp }
1902 8b473291 2019-02-21 stsp
1903 8b473291 2019-02-21 stsp return err;
1904 8b473291 2019-02-21 stsp }
1905 8b473291 2019-02-21 stsp
1906 963b370f 2018-05-20 stsp static const struct got_error *
1907 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1908 27a741e5 2019-09-11 stsp int col_tab_align)
1909 5813d178 2019-03-09 stsp {
1910 e6b8b890 2020-12-29 naddy char *smallerthan;
1911 5813d178 2019-03-09 stsp
1912 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1913 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1914 5813d178 2019-03-09 stsp author = smallerthan + 1;
1915 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1916 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1917 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1918 5813d178 2019-03-09 stsp }
1919 5813d178 2019-03-09 stsp
1920 5813d178 2019-03-09 stsp static const struct got_error *
1921 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1922 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1923 8fdc79fe 2020-12-01 naddy int author_display_cols)
1924 80ddbec8 2018-04-29 stsp {
1925 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1926 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1927 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1928 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1929 5813d178 2019-03-09 stsp char *author = NULL;
1930 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1931 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1932 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1933 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1934 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1935 ccb26ccd 2018-11-05 stsp struct tm tm;
1936 45d799e2 2018-12-23 stsp time_t committer_time;
1937 11b20872 2019-11-08 stsp struct tog_color *tc;
1938 80ddbec8 2018-04-29 stsp
1939 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1940 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1941 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1942 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1943 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1944 b39d25c7 2018-07-10 stsp
1945 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1946 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1947 b39d25c7 2018-07-10 stsp else
1948 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1949 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1950 11b20872 2019-11-08 stsp if (tc)
1951 11b20872 2019-11-08 stsp wattr_on(view->window,
1952 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1953 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1954 11b20872 2019-11-08 stsp if (tc)
1955 11b20872 2019-11-08 stsp wattr_off(view->window,
1956 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1957 27a741e5 2019-09-11 stsp col = limit;
1958 b39d25c7 2018-07-10 stsp if (col > avail)
1959 b39d25c7 2018-07-10 stsp goto done;
1960 6570a66d 2019-11-08 stsp
1961 6570a66d 2019-11-08 stsp if (avail >= 120) {
1962 6570a66d 2019-11-08 stsp char *id_str;
1963 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1964 6570a66d 2019-11-08 stsp if (err)
1965 6570a66d 2019-11-08 stsp goto done;
1966 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1967 11b20872 2019-11-08 stsp if (tc)
1968 11b20872 2019-11-08 stsp wattr_on(view->window,
1969 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1970 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1971 11b20872 2019-11-08 stsp if (tc)
1972 11b20872 2019-11-08 stsp wattr_off(view->window,
1973 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1974 6570a66d 2019-11-08 stsp free(id_str);
1975 6570a66d 2019-11-08 stsp col += 9;
1976 6570a66d 2019-11-08 stsp if (col > avail)
1977 6570a66d 2019-11-08 stsp goto done;
1978 6570a66d 2019-11-08 stsp }
1979 b39d25c7 2018-07-10 stsp
1980 f69c5a46 2022-07-19 thomas if (s->use_committer)
1981 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
1982 f69c5a46 2022-07-19 thomas else
1983 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
1984 5813d178 2019-03-09 stsp if (author == NULL) {
1985 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1986 80ddbec8 2018-04-29 stsp goto done;
1987 80ddbec8 2018-04-29 stsp }
1988 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1989 bb737323 2018-05-20 stsp if (err)
1990 bb737323 2018-05-20 stsp goto done;
1991 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1992 11b20872 2019-11-08 stsp if (tc)
1993 11b20872 2019-11-08 stsp wattr_on(view->window,
1994 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1995 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1996 11b20872 2019-11-08 stsp if (tc)
1997 11b20872 2019-11-08 stsp wattr_off(view->window,
1998 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1999 bb737323 2018-05-20 stsp col += author_width;
2000 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2001 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2002 bb737323 2018-05-20 stsp col++;
2003 bb737323 2018-05-20 stsp author_width++;
2004 bb737323 2018-05-20 stsp }
2005 9c2eaf34 2018-05-20 stsp if (col > avail)
2006 9c2eaf34 2018-05-20 stsp goto done;
2007 80ddbec8 2018-04-29 stsp
2008 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2009 5943eee2 2019-08-13 stsp if (err)
2010 6d9fbc00 2018-04-29 stsp goto done;
2011 bb737323 2018-05-20 stsp logmsg = logmsg0;
2012 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2013 bb737323 2018-05-20 stsp logmsg++;
2014 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2015 bb737323 2018-05-20 stsp if (newline)
2016 bb737323 2018-05-20 stsp *newline = '\0';
2017 f91a2b48 2022-06-23 thomas limit = avail - col;
2018 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2019 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2020 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2021 f91a2b48 2022-06-23 thomas limit, col, 1);
2022 331b1a16 2022-06-23 thomas if (err)
2023 331b1a16 2022-06-23 thomas goto done;
2024 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2025 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2026 27a741e5 2019-09-11 stsp while (col < avail) {
2027 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2028 bb737323 2018-05-20 stsp col++;
2029 881b2d3e 2018-04-30 stsp }
2030 80ddbec8 2018-04-29 stsp done:
2031 80ddbec8 2018-04-29 stsp free(logmsg0);
2032 bb737323 2018-05-20 stsp free(wlogmsg);
2033 5813d178 2019-03-09 stsp free(author);
2034 bb737323 2018-05-20 stsp free(wauthor);
2035 80ddbec8 2018-04-29 stsp free(line);
2036 80ddbec8 2018-04-29 stsp return err;
2037 80ddbec8 2018-04-29 stsp }
2038 26ed57b2 2018-05-19 stsp
2039 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2040 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2041 899d86c2 2018-05-10 stsp struct got_object_id *id)
2042 80ddbec8 2018-04-29 stsp {
2043 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2044 80ddbec8 2018-04-29 stsp
2045 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2046 80ddbec8 2018-04-29 stsp if (entry == NULL)
2047 899d86c2 2018-05-10 stsp return NULL;
2048 99db9666 2018-05-07 stsp
2049 899d86c2 2018-05-10 stsp entry->id = id;
2050 99db9666 2018-05-07 stsp entry->commit = commit;
2051 899d86c2 2018-05-10 stsp return entry;
2052 99db9666 2018-05-07 stsp }
2053 80ddbec8 2018-04-29 stsp
2054 99db9666 2018-05-07 stsp static void
2055 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2056 99db9666 2018-05-07 stsp {
2057 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2058 99db9666 2018-05-07 stsp
2059 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2060 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2061 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2062 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2063 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2064 99db9666 2018-05-07 stsp free(entry);
2065 99db9666 2018-05-07 stsp }
2066 99db9666 2018-05-07 stsp
2067 99db9666 2018-05-07 stsp static void
2068 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2069 99db9666 2018-05-07 stsp {
2070 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2071 99db9666 2018-05-07 stsp pop_commit(commits);
2072 c4972b91 2018-05-07 stsp }
2073 c4972b91 2018-05-07 stsp
2074 c4972b91 2018-05-07 stsp static const struct got_error *
2075 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2076 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2077 13add988 2019-10-15 stsp {
2078 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2079 13add988 2019-10-15 stsp regmatch_t regmatch;
2080 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2081 13add988 2019-10-15 stsp
2082 13add988 2019-10-15 stsp *have_match = 0;
2083 13add988 2019-10-15 stsp
2084 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2085 13add988 2019-10-15 stsp if (err)
2086 13add988 2019-10-15 stsp return err;
2087 13add988 2019-10-15 stsp
2088 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2089 13add988 2019-10-15 stsp if (err)
2090 13add988 2019-10-15 stsp goto done;
2091 13add988 2019-10-15 stsp
2092 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2093 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2094 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2095 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2096 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2097 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2098 13add988 2019-10-15 stsp *have_match = 1;
2099 13add988 2019-10-15 stsp done:
2100 13add988 2019-10-15 stsp free(id_str);
2101 13add988 2019-10-15 stsp free(logmsg);
2102 13add988 2019-10-15 stsp return err;
2103 13add988 2019-10-15 stsp }
2104 13add988 2019-10-15 stsp
2105 13add988 2019-10-15 stsp static const struct got_error *
2106 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2107 c4972b91 2018-05-07 stsp {
2108 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2109 9ba79e04 2018-06-11 stsp
2110 1a76625f 2018-10-22 stsp /*
2111 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2112 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2113 1a76625f 2018-10-22 stsp * while updating the display.
2114 1a76625f 2018-10-22 stsp */
2115 4e0d2870 2020-12-07 naddy do {
2116 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2117 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2118 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2119 1a76625f 2018-10-22 stsp int errcode;
2120 899d86c2 2018-05-10 stsp
2121 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2122 4e0d2870 2020-12-07 naddy NULL, NULL);
2123 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2124 ecb28ae0 2018-07-16 stsp break;
2125 899d86c2 2018-05-10 stsp
2126 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2127 9ba79e04 2018-06-11 stsp if (err)
2128 9ba79e04 2018-06-11 stsp break;
2129 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2130 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2131 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2132 9ba79e04 2018-06-11 stsp break;
2133 9ba79e04 2018-06-11 stsp }
2134 93e45b7c 2018-09-24 stsp
2135 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2136 1a76625f 2018-10-22 stsp if (errcode) {
2137 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2138 13add988 2019-10-15 stsp "pthread_mutex_lock");
2139 1a76625f 2018-10-22 stsp break;
2140 1a76625f 2018-10-22 stsp }
2141 1a76625f 2018-10-22 stsp
2142 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2143 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2144 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2145 1a76625f 2018-10-22 stsp
2146 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2147 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2148 7c1452c1 2020-03-26 stsp int have_match;
2149 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2150 7c1452c1 2020-03-26 stsp if (err)
2151 7c1452c1 2020-03-26 stsp break;
2152 7c1452c1 2020-03-26 stsp if (have_match)
2153 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2154 13add988 2019-10-15 stsp }
2155 13add988 2019-10-15 stsp
2156 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2157 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2158 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2159 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2160 7c1452c1 2020-03-26 stsp if (err)
2161 13add988 2019-10-15 stsp break;
2162 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2163 899d86c2 2018-05-10 stsp
2164 9ba79e04 2018-06-11 stsp return err;
2165 0553a4e3 2018-04-30 stsp }
2166 0553a4e3 2018-04-30 stsp
2167 2b779855 2020-12-05 naddy static void
2168 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2169 2b779855 2020-12-05 naddy {
2170 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2171 2b779855 2020-12-05 naddy int ncommits = 0;
2172 2b779855 2020-12-05 naddy
2173 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2174 2b779855 2020-12-05 naddy while (entry) {
2175 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2176 2b779855 2020-12-05 naddy s->selected_entry = entry;
2177 2b779855 2020-12-05 naddy break;
2178 2b779855 2020-12-05 naddy }
2179 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2180 2b779855 2020-12-05 naddy ncommits++;
2181 2b779855 2020-12-05 naddy }
2182 2b779855 2020-12-05 naddy }
2183 2b779855 2020-12-05 naddy
2184 0553a4e3 2018-04-30 stsp static const struct got_error *
2185 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2186 0553a4e3 2018-04-30 stsp {
2187 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2188 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2189 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2190 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2191 60493ae3 2019-06-20 stsp int width;
2192 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2193 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2194 8b473291 2019-02-21 stsp char *refs_str = NULL;
2195 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2196 11b20872 2019-11-08 stsp struct tog_color *tc;
2197 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2198 0553a4e3 2018-04-30 stsp
2199 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2200 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2201 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2202 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2203 1a76625f 2018-10-22 stsp if (err)
2204 ecb28ae0 2018-07-16 stsp return err;
2205 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2206 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2207 d2075bf3 2020-12-25 stsp if (refs) {
2208 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2209 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2210 d2075bf3 2020-12-25 stsp if (err)
2211 d2075bf3 2020-12-25 stsp goto done;
2212 d2075bf3 2020-12-25 stsp }
2213 867c6645 2018-07-10 stsp }
2214 359bfafd 2019-02-22 stsp
2215 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2216 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2217 1a76625f 2018-10-22 stsp
2218 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2219 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2220 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2221 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2222 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2223 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2224 8f4ed634 2020-03-26 stsp goto done;
2225 8f4ed634 2020-03-26 stsp }
2226 8f4ed634 2020-03-26 stsp } else {
2227 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2228 f9686aa5 2020-03-27 stsp
2229 f9686aa5 2020-03-27 stsp if (view->searching) {
2230 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2231 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2232 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2233 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2234 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2235 f9686aa5 2020-03-27 stsp search_str = "searching...";
2236 f9686aa5 2020-03-27 stsp }
2237 f9686aa5 2020-03-27 stsp
2238 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2239 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2240 f9686aa5 2020-03-27 stsp search_str ? search_str :
2241 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2242 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2243 8f4ed634 2020-03-26 stsp goto done;
2244 8f4ed634 2020-03-26 stsp }
2245 8b473291 2019-02-21 stsp }
2246 1a76625f 2018-10-22 stsp
2247 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2248 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2249 91198554 2022-06-23 thomas "........................................",
2250 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2251 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2252 1a76625f 2018-10-22 stsp header = NULL;
2253 1a76625f 2018-10-22 stsp goto done;
2254 1a76625f 2018-10-22 stsp }
2255 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2256 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2257 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2258 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2259 1a76625f 2018-10-22 stsp header = NULL;
2260 1a76625f 2018-10-22 stsp goto done;
2261 ecb28ae0 2018-07-16 stsp }
2262 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2263 1a76625f 2018-10-22 stsp if (err)
2264 1a76625f 2018-10-22 stsp goto done;
2265 867c6645 2018-07-10 stsp
2266 2814baeb 2018-08-01 stsp werase(view->window);
2267 867c6645 2018-07-10 stsp
2268 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2269 a3404814 2018-09-02 stsp wstandout(view->window);
2270 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2271 11b20872 2019-11-08 stsp if (tc)
2272 11b20872 2019-11-08 stsp wattr_on(view->window,
2273 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2274 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2275 11b20872 2019-11-08 stsp if (tc)
2276 11b20872 2019-11-08 stsp wattr_off(view->window,
2277 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2278 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2279 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2280 1a76625f 2018-10-22 stsp width++;
2281 1a76625f 2018-10-22 stsp }
2282 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2283 a3404814 2018-09-02 stsp wstandend(view->window);
2284 ecb28ae0 2018-07-16 stsp free(wline);
2285 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2286 1a76625f 2018-10-22 stsp goto done;
2287 0553a4e3 2018-04-30 stsp
2288 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2289 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2290 5813d178 2019-03-09 stsp ncommits = 0;
2291 05171be4 2022-06-23 thomas view->maxx = 0;
2292 5813d178 2019-03-09 stsp while (entry) {
2293 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2294 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2295 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2296 5813d178 2019-03-09 stsp int width;
2297 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2298 5813d178 2019-03-09 stsp break;
2299 f69c5a46 2022-07-19 thomas if (s->use_committer)
2300 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2301 f69c5a46 2022-07-19 thomas else
2302 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2303 5813d178 2019-03-09 stsp if (author == NULL) {
2304 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2305 5813d178 2019-03-09 stsp goto done;
2306 5813d178 2019-03-09 stsp }
2307 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2308 27a741e5 2019-09-11 stsp date_display_cols);
2309 5813d178 2019-03-09 stsp if (author_cols < width)
2310 5813d178 2019-03-09 stsp author_cols = width;
2311 5813d178 2019-03-09 stsp free(wauthor);
2312 5813d178 2019-03-09 stsp free(author);
2313 ef944b8b 2022-07-21 thomas if (err)
2314 ef944b8b 2022-07-21 thomas goto done;
2315 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2316 05171be4 2022-06-23 thomas if (err)
2317 05171be4 2022-06-23 thomas goto done;
2318 05171be4 2022-06-23 thomas msg = msg0;
2319 05171be4 2022-06-23 thomas while (*msg == '\n')
2320 05171be4 2022-06-23 thomas ++msg;
2321 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2322 331b1a16 2022-06-23 thomas *eol = '\0';
2323 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2324 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2325 331b1a16 2022-06-23 thomas if (err)
2326 331b1a16 2022-06-23 thomas goto done;
2327 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2328 05171be4 2022-06-23 thomas free(msg0);
2329 331b1a16 2022-06-23 thomas free(wmsg);
2330 7ca04879 2019-10-19 stsp ncommits++;
2331 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2332 5813d178 2019-03-09 stsp }
2333 5813d178 2019-03-09 stsp
2334 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2335 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2336 867c6645 2018-07-10 stsp ncommits = 0;
2337 899d86c2 2018-05-10 stsp while (entry) {
2338 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2339 899d86c2 2018-05-10 stsp break;
2340 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2341 2814baeb 2018-08-01 stsp wstandout(view->window);
2342 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2343 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2344 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2345 2814baeb 2018-08-01 stsp wstandend(view->window);
2346 0553a4e3 2018-04-30 stsp if (err)
2347 60493ae3 2019-06-20 stsp goto done;
2348 0553a4e3 2018-04-30 stsp ncommits++;
2349 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2350 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2351 80ddbec8 2018-04-29 stsp }
2352 80ddbec8 2018-04-29 stsp
2353 a5d43cac 2022-07-01 thomas view_border(view);
2354 1a76625f 2018-10-22 stsp done:
2355 1a76625f 2018-10-22 stsp free(id_str);
2356 8b473291 2019-02-21 stsp free(refs_str);
2357 1a76625f 2018-10-22 stsp free(ncommits_str);
2358 1a76625f 2018-10-22 stsp free(header);
2359 80ddbec8 2018-04-29 stsp return err;
2360 9f7d7167 2018-04-29 stsp }
2361 07b55e75 2018-05-10 stsp
2362 07b55e75 2018-05-10 stsp static void
2363 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2364 07b55e75 2018-05-10 stsp {
2365 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2366 07b55e75 2018-05-10 stsp int nscrolled = 0;
2367 07b55e75 2018-05-10 stsp
2368 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2369 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2370 07b55e75 2018-05-10 stsp return;
2371 9f7d7167 2018-04-29 stsp
2372 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2373 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2374 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2375 07b55e75 2018-05-10 stsp if (entry) {
2376 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2377 07b55e75 2018-05-10 stsp nscrolled++;
2378 07b55e75 2018-05-10 stsp }
2379 07b55e75 2018-05-10 stsp }
2380 aa075928 2018-05-10 stsp }
2381 aa075928 2018-05-10 stsp
2382 aa075928 2018-05-10 stsp static const struct got_error *
2383 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2384 aa075928 2018-05-10 stsp {
2385 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2386 5e224a3e 2019-02-22 stsp int errcode;
2387 8a42fca8 2019-02-22 stsp
2388 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2389 aa075928 2018-05-10 stsp
2390 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2391 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2392 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2393 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2394 7aafa0d1 2019-02-22 stsp if (errcode)
2395 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2396 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2397 7c1452c1 2020-03-26 stsp
2398 7c1452c1 2020-03-26 stsp /*
2399 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2400 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2401 7c1452c1 2020-03-26 stsp */
2402 7c1452c1 2020-03-26 stsp if (!wait)
2403 7c1452c1 2020-03-26 stsp break;
2404 7c1452c1 2020-03-26 stsp
2405 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2406 ffe38506 2020-12-01 naddy show_log_view(view);
2407 7c1452c1 2020-03-26 stsp update_panels();
2408 7c1452c1 2020-03-26 stsp doupdate();
2409 7c1452c1 2020-03-26 stsp
2410 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2411 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2412 82954512 2020-02-03 stsp if (errcode)
2413 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2414 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2415 82954512 2020-02-03 stsp
2416 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2417 ffe38506 2020-12-01 naddy show_log_view(view);
2418 7c1452c1 2020-03-26 stsp update_panels();
2419 7c1452c1 2020-03-26 stsp doupdate();
2420 5e224a3e 2019-02-22 stsp }
2421 5e224a3e 2019-02-22 stsp
2422 5e224a3e 2019-02-22 stsp return NULL;
2423 a5d43cac 2022-07-01 thomas }
2424 a5d43cac 2022-07-01 thomas
2425 a5d43cac 2022-07-01 thomas static const struct got_error *
2426 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2427 a5d43cac 2022-07-01 thomas {
2428 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2429 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2430 fe731b51 2022-07-14 thomas
2431 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2432 fe731b51 2022-07-14 thomas return NULL;
2433 a5d43cac 2022-07-01 thomas
2434 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2435 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2436 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2437 a5d43cac 2022-07-01 thomas
2438 a5d43cac 2022-07-01 thomas return err;
2439 5e224a3e 2019-02-22 stsp }
2440 5e224a3e 2019-02-22 stsp
2441 5e224a3e 2019-02-22 stsp static const struct got_error *
2442 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2443 5e224a3e 2019-02-22 stsp {
2444 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2445 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2446 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2447 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2448 5e224a3e 2019-02-22 stsp
2449 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2450 5e224a3e 2019-02-22 stsp return NULL;
2451 5e224a3e 2019-02-22 stsp
2452 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2453 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2454 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2455 08ebd0a9 2019-02-22 stsp /*
2456 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2457 08ebd0a9 2019-02-22 stsp */
2458 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2459 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2460 5e224a3e 2019-02-22 stsp if (err)
2461 5e224a3e 2019-02-22 stsp return err;
2462 7aafa0d1 2019-02-22 stsp }
2463 b295e71b 2019-02-22 stsp
2464 7aafa0d1 2019-02-22 stsp do {
2465 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2466 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2467 88048b54 2019-02-21 stsp break;
2468 88048b54 2019-02-21 stsp
2469 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2470 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2471 aa075928 2018-05-10 stsp
2472 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2473 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2474 dd0a52c1 2018-05-20 stsp break;
2475 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2476 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2477 aa075928 2018-05-10 stsp
2478 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2479 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2480 a5d43cac 2022-07-01 thomas else
2481 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2482 a5d43cac 2022-07-01 thomas
2483 dd0a52c1 2018-05-20 stsp return err;
2484 07b55e75 2018-05-10 stsp }
2485 4a7f7875 2018-05-10 stsp
2486 cd0acaa7 2018-05-20 stsp static const struct got_error *
2487 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2488 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2489 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2490 cd0acaa7 2018-05-20 stsp {
2491 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2492 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2493 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2494 cd0acaa7 2018-05-20 stsp
2495 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2496 15a94983 2018-12-23 stsp if (diff_view == NULL)
2497 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2498 ea5e7bb5 2018-08-01 stsp
2499 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2500 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2501 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2502 e5a0f69f 2018-08-18 stsp if (err == NULL)
2503 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2504 cd0acaa7 2018-05-20 stsp return err;
2505 4a7f7875 2018-05-10 stsp }
2506 4a7f7875 2018-05-10 stsp
2507 80ddbec8 2018-04-29 stsp static const struct got_error *
2508 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2509 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2510 9343a5fb 2018-06-23 stsp {
2511 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2512 941e9f74 2019-05-21 stsp
2513 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2514 941e9f74 2019-05-21 stsp if (parent == NULL)
2515 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2516 941e9f74 2019-05-21 stsp
2517 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2518 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2519 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2520 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2521 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2522 941e9f74 2019-05-21 stsp s->tree = subtree;
2523 941e9f74 2019-05-21 stsp s->selected = 0;
2524 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2525 941e9f74 2019-05-21 stsp return NULL;
2526 941e9f74 2019-05-21 stsp }
2527 941e9f74 2019-05-21 stsp
2528 941e9f74 2019-05-21 stsp static const struct got_error *
2529 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2530 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2531 941e9f74 2019-05-21 stsp {
2532 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2533 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2534 941e9f74 2019-05-21 stsp const char *p;
2535 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2536 9343a5fb 2018-06-23 stsp
2537 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2538 941e9f74 2019-05-21 stsp p = path;
2539 941e9f74 2019-05-21 stsp while (*p) {
2540 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2541 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2542 56e0773d 2019-11-28 stsp char *te_name;
2543 33cbf02b 2020-01-12 stsp
2544 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2545 33cbf02b 2020-01-12 stsp p++;
2546 941e9f74 2019-05-21 stsp
2547 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2548 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2549 941e9f74 2019-05-21 stsp if (slash == NULL)
2550 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2551 33cbf02b 2020-01-12 stsp else
2552 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2553 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2554 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2555 56e0773d 2019-11-28 stsp break;
2556 941e9f74 2019-05-21 stsp }
2557 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2558 56e0773d 2019-11-28 stsp if (te == NULL) {
2559 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2560 56e0773d 2019-11-28 stsp free(te_name);
2561 941e9f74 2019-05-21 stsp break;
2562 941e9f74 2019-05-21 stsp }
2563 56e0773d 2019-11-28 stsp free(te_name);
2564 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2565 941e9f74 2019-05-21 stsp
2566 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2567 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2568 b03c880f 2019-05-21 stsp
2569 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2570 941e9f74 2019-05-21 stsp if (slash)
2571 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2572 941e9f74 2019-05-21 stsp else
2573 941e9f74 2019-05-21 stsp subpath = strdup(path);
2574 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2575 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2576 941e9f74 2019-05-21 stsp break;
2577 941e9f74 2019-05-21 stsp }
2578 941e9f74 2019-05-21 stsp
2579 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2580 941e9f74 2019-05-21 stsp subpath);
2581 941e9f74 2019-05-21 stsp if (err)
2582 941e9f74 2019-05-21 stsp break;
2583 941e9f74 2019-05-21 stsp
2584 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2585 941e9f74 2019-05-21 stsp free(tree_id);
2586 941e9f74 2019-05-21 stsp if (err)
2587 941e9f74 2019-05-21 stsp break;
2588 941e9f74 2019-05-21 stsp
2589 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2590 941e9f74 2019-05-21 stsp if (err) {
2591 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2592 941e9f74 2019-05-21 stsp break;
2593 941e9f74 2019-05-21 stsp }
2594 941e9f74 2019-05-21 stsp if (slash == NULL)
2595 941e9f74 2019-05-21 stsp break;
2596 941e9f74 2019-05-21 stsp free(subpath);
2597 941e9f74 2019-05-21 stsp subpath = NULL;
2598 941e9f74 2019-05-21 stsp p = slash;
2599 941e9f74 2019-05-21 stsp }
2600 941e9f74 2019-05-21 stsp
2601 941e9f74 2019-05-21 stsp free(subpath);
2602 1a76625f 2018-10-22 stsp return err;
2603 61266923 2020-01-14 stsp }
2604 61266923 2020-01-14 stsp
2605 61266923 2020-01-14 stsp static const struct got_error *
2606 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2607 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2608 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2609 55cccc34 2020-02-20 stsp {
2610 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2611 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2612 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2613 55cccc34 2020-02-20 stsp
2614 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2615 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2616 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2617 55cccc34 2020-02-20 stsp
2618 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2619 bc573f3b 2021-07-10 stsp if (err)
2620 55cccc34 2020-02-20 stsp return err;
2621 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2622 55cccc34 2020-02-20 stsp
2623 55cccc34 2020-02-20 stsp *new_view = tree_view;
2624 55cccc34 2020-02-20 stsp
2625 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2626 55cccc34 2020-02-20 stsp return NULL;
2627 55cccc34 2020-02-20 stsp
2628 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2629 55cccc34 2020-02-20 stsp }
2630 55cccc34 2020-02-20 stsp
2631 55cccc34 2020-02-20 stsp static const struct got_error *
2632 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2633 61266923 2020-01-14 stsp {
2634 61266923 2020-01-14 stsp sigset_t sigset;
2635 61266923 2020-01-14 stsp int errcode;
2636 61266923 2020-01-14 stsp
2637 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2638 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2639 61266923 2020-01-14 stsp
2640 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2641 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2642 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2643 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2644 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2645 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2646 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2647 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2648 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2649 61266923 2020-01-14 stsp
2650 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2651 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2652 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2653 61266923 2020-01-14 stsp
2654 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2655 61266923 2020-01-14 stsp if (errcode)
2656 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2657 61266923 2020-01-14 stsp
2658 61266923 2020-01-14 stsp return NULL;
2659 1a76625f 2018-10-22 stsp }
2660 1a76625f 2018-10-22 stsp
2661 1a76625f 2018-10-22 stsp static void *
2662 1a76625f 2018-10-22 stsp log_thread(void *arg)
2663 1a76625f 2018-10-22 stsp {
2664 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2665 1a76625f 2018-10-22 stsp int errcode = 0;
2666 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2667 1a76625f 2018-10-22 stsp int done = 0;
2668 61266923 2020-01-14 stsp
2669 f2d749db 2022-07-12 thomas /*
2670 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2671 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2672 f2d749db 2022-07-12 thomas */
2673 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2674 f2d749db 2022-07-12 thomas if (errcode) {
2675 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2676 61266923 2020-01-14 stsp return (void *)err;
2677 f2d749db 2022-07-12 thomas }
2678 1a76625f 2018-10-22 stsp
2679 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2680 f2d749db 2022-07-12 thomas if (err) {
2681 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2682 f2d749db 2022-07-12 thomas goto done;
2683 f2d749db 2022-07-12 thomas }
2684 f2d749db 2022-07-12 thomas
2685 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2686 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2687 f2d749db 2022-07-12 thomas if (errcode) {
2688 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2689 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2690 f2d749db 2022-07-12 thomas goto done;
2691 f2d749db 2022-07-12 thomas }
2692 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2693 1a76625f 2018-10-22 stsp if (err) {
2694 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2695 f2d749db 2022-07-12 thomas goto done;
2696 1a76625f 2018-10-22 stsp err = NULL;
2697 1a76625f 2018-10-22 stsp done = 1;
2698 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2699 1a76625f 2018-10-22 stsp a->commits_needed--;
2700 1a76625f 2018-10-22 stsp
2701 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2702 3abe8080 2019-04-10 stsp if (errcode) {
2703 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2704 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2705 f2d749db 2022-07-12 thomas goto done;
2706 3abe8080 2019-04-10 stsp } else if (*a->quit)
2707 1a76625f 2018-10-22 stsp done = 1;
2708 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2709 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2710 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2711 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2712 1a76625f 2018-10-22 stsp }
2713 1a76625f 2018-10-22 stsp
2714 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2715 7c1452c1 2020-03-26 stsp if (errcode) {
2716 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2717 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2718 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2719 f2d749db 2022-07-12 thomas goto done;
2720 7c1452c1 2020-03-26 stsp }
2721 7c1452c1 2020-03-26 stsp
2722 1a76625f 2018-10-22 stsp if (done)
2723 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2724 7c1452c1 2020-03-26 stsp else {
2725 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2726 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2727 7c1452c1 2020-03-26 stsp &tog_mutex);
2728 f2d749db 2022-07-12 thomas if (errcode) {
2729 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2730 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2731 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2732 f2d749db 2022-07-12 thomas goto done;
2733 f2d749db 2022-07-12 thomas }
2734 21355643 2020-12-06 stsp if (*a->quit)
2735 21355643 2020-12-06 stsp done = 1;
2736 7c1452c1 2020-03-26 stsp }
2737 1a76625f 2018-10-22 stsp }
2738 1a76625f 2018-10-22 stsp }
2739 3abe8080 2019-04-10 stsp a->log_complete = 1;
2740 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2741 f2d749db 2022-07-12 thomas if (errcode)
2742 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2743 f2d749db 2022-07-12 thomas done:
2744 f2d749db 2022-07-12 thomas if (err) {
2745 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2746 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2747 f2d749db 2022-07-12 thomas }
2748 1a76625f 2018-10-22 stsp return (void *)err;
2749 1a76625f 2018-10-22 stsp }
2750 1a76625f 2018-10-22 stsp
2751 1a76625f 2018-10-22 stsp static const struct got_error *
2752 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2753 1a76625f 2018-10-22 stsp {
2754 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2755 1a76625f 2018-10-22 stsp int errcode;
2756 1a76625f 2018-10-22 stsp
2757 1a76625f 2018-10-22 stsp if (s->thread) {
2758 1a76625f 2018-10-22 stsp s->quit = 1;
2759 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2760 1a76625f 2018-10-22 stsp if (errcode)
2761 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2762 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2763 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2764 1a76625f 2018-10-22 stsp if (errcode)
2765 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2766 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2767 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2768 1a76625f 2018-10-22 stsp if (errcode)
2769 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2770 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2771 1a76625f 2018-10-22 stsp if (errcode)
2772 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2773 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2774 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2775 1a76625f 2018-10-22 stsp }
2776 1a76625f 2018-10-22 stsp
2777 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2778 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2779 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2780 1af5eddf 2022-06-23 thomas }
2781 1af5eddf 2022-06-23 thomas
2782 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2783 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2784 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2785 1af5eddf 2022-06-23 thomas if (err == NULL)
2786 1af5eddf 2022-06-23 thomas err = pack_err;
2787 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2788 1a76625f 2018-10-22 stsp }
2789 1a76625f 2018-10-22 stsp
2790 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2791 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2792 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2793 1a76625f 2018-10-22 stsp }
2794 1a76625f 2018-10-22 stsp
2795 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2796 9343a5fb 2018-06-23 stsp }
2797 9343a5fb 2018-06-23 stsp
2798 9343a5fb 2018-06-23 stsp static const struct got_error *
2799 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2800 1a76625f 2018-10-22 stsp {
2801 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2802 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2803 276b94a1 2020-11-13 naddy int errcode;
2804 1a76625f 2018-10-22 stsp
2805 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2806 276b94a1 2020-11-13 naddy
2807 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2808 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2809 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2810 276b94a1 2020-11-13 naddy
2811 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2812 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2813 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2814 276b94a1 2020-11-13 naddy
2815 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2816 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2817 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2818 1a76625f 2018-10-22 stsp free(s->start_id);
2819 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2820 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2821 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2822 1a76625f 2018-10-22 stsp return err;
2823 1a76625f 2018-10-22 stsp }
2824 1a76625f 2018-10-22 stsp
2825 1a76625f 2018-10-22 stsp static const struct got_error *
2826 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2827 60493ae3 2019-06-20 stsp {
2828 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2829 60493ae3 2019-06-20 stsp
2830 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2831 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2832 60493ae3 2019-06-20 stsp return NULL;
2833 60493ae3 2019-06-20 stsp }
2834 60493ae3 2019-06-20 stsp
2835 60493ae3 2019-06-20 stsp static const struct got_error *
2836 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2837 60493ae3 2019-06-20 stsp {
2838 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2839 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2840 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2841 60493ae3 2019-06-20 stsp
2842 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2843 f9686aa5 2020-03-27 stsp show_log_view(view);
2844 f9686aa5 2020-03-27 stsp update_panels();
2845 f9686aa5 2020-03-27 stsp doupdate();
2846 f9686aa5 2020-03-27 stsp
2847 96e2b566 2019-07-08 stsp if (s->search_entry) {
2848 13add988 2019-10-15 stsp int errcode, ch;
2849 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2850 13add988 2019-10-15 stsp if (errcode)
2851 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2852 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2853 13add988 2019-10-15 stsp ch = wgetch(view->window);
2854 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2855 13add988 2019-10-15 stsp if (errcode)
2856 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2857 13add988 2019-10-15 stsp "pthread_mutex_lock");
2858 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2859 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2860 678cbce5 2019-07-28 stsp return NULL;
2861 678cbce5 2019-07-28 stsp }
2862 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2863 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2864 96e2b566 2019-07-08 stsp else
2865 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2866 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2867 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2868 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2869 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2870 df5e841d 2022-06-23 thomas
2871 df5e841d 2022-06-23 thomas /*
2872 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2873 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2874 1f4d5162 2022-06-23 thomas * might have changed.
2875 df5e841d 2022-06-23 thomas */
2876 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2877 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2878 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2879 df5e841d 2022-06-23 thomas else
2880 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2881 b74feda6 2022-06-23 thomas } else {
2882 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2883 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2884 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2885 df5e841d 2022-06-23 thomas else
2886 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2887 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2888 b74feda6 2022-06-23 thomas }
2889 20be8d96 2019-06-21 stsp } else {
2890 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2891 20be8d96 2019-06-21 stsp }
2892 60493ae3 2019-06-20 stsp
2893 60493ae3 2019-06-20 stsp while (1) {
2894 13add988 2019-10-15 stsp int have_match = 0;
2895 13add988 2019-10-15 stsp
2896 60493ae3 2019-06-20 stsp if (entry == NULL) {
2897 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2898 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2899 f9967bca 2020-03-27 stsp view->search_next_done =
2900 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2901 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2902 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2903 f801134a 2019-06-25 stsp return NULL;
2904 60493ae3 2019-06-20 stsp }
2905 96e2b566 2019-07-08 stsp /*
2906 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2907 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2908 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2909 96e2b566 2019-07-08 stsp */
2910 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2911 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2912 60493ae3 2019-06-20 stsp }
2913 60493ae3 2019-06-20 stsp
2914 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2915 13add988 2019-10-15 stsp &view->regex);
2916 5943eee2 2019-08-13 stsp if (err)
2917 13add988 2019-10-15 stsp break;
2918 13add988 2019-10-15 stsp if (have_match) {
2919 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2920 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2921 60493ae3 2019-06-20 stsp break;
2922 60493ae3 2019-06-20 stsp }
2923 13add988 2019-10-15 stsp
2924 96e2b566 2019-07-08 stsp s->search_entry = entry;
2925 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2926 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2927 b1bf1435 2019-06-21 stsp else
2928 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2929 60493ae3 2019-06-20 stsp }
2930 60493ae3 2019-06-20 stsp
2931 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2932 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2933 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2934 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2935 60493ae3 2019-06-20 stsp if (err)
2936 60493ae3 2019-06-20 stsp return err;
2937 ead14cbe 2019-06-21 stsp cur++;
2938 ead14cbe 2019-06-21 stsp }
2939 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2940 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2941 60493ae3 2019-06-20 stsp if (err)
2942 60493ae3 2019-06-20 stsp return err;
2943 ead14cbe 2019-06-21 stsp cur--;
2944 60493ae3 2019-06-20 stsp }
2945 60493ae3 2019-06-20 stsp }
2946 60493ae3 2019-06-20 stsp
2947 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2948 96e2b566 2019-07-08 stsp
2949 60493ae3 2019-06-20 stsp return NULL;
2950 60493ae3 2019-06-20 stsp }
2951 60493ae3 2019-06-20 stsp
2952 60493ae3 2019-06-20 stsp static const struct got_error *
2953 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2954 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2955 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2956 80ddbec8 2018-04-29 stsp {
2957 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2958 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2959 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2960 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2961 1a76625f 2018-10-22 stsp int errcode;
2962 80ddbec8 2018-04-29 stsp
2963 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2964 f135c941 2020-02-20 stsp free(s->in_repo_path);
2965 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2966 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2967 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2968 f135c941 2020-02-20 stsp }
2969 ecb28ae0 2018-07-16 stsp
2970 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2971 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2972 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2973 78756c87 2020-11-24 stsp
2974 fb2756b9 2018-08-04 stsp s->repo = repo;
2975 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2976 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2977 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2978 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2979 9cd7cbd1 2020-12-07 stsp goto done;
2980 9cd7cbd1 2020-12-07 stsp }
2981 9cd7cbd1 2020-12-07 stsp }
2982 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2983 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2984 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2985 5036bf37 2018-09-24 stsp goto done;
2986 5036bf37 2018-09-24 stsp }
2987 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2988 e5a0f69f 2018-08-18 stsp
2989 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2990 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2991 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2992 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2993 11b20872 2019-11-08 stsp if (err)
2994 11b20872 2019-11-08 stsp goto done;
2995 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2996 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2997 11b20872 2019-11-08 stsp if (err) {
2998 11b20872 2019-11-08 stsp free_colors(&s->colors);
2999 11b20872 2019-11-08 stsp goto done;
3000 11b20872 2019-11-08 stsp }
3001 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3002 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3003 11b20872 2019-11-08 stsp if (err) {
3004 11b20872 2019-11-08 stsp free_colors(&s->colors);
3005 11b20872 2019-11-08 stsp goto done;
3006 11b20872 2019-11-08 stsp }
3007 11b20872 2019-11-08 stsp }
3008 11b20872 2019-11-08 stsp
3009 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3010 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3011 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3012 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3013 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3014 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3015 1a76625f 2018-10-22 stsp
3016 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3017 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3018 1af5eddf 2022-06-23 thomas if (err)
3019 1af5eddf 2022-06-23 thomas goto done;
3020 1af5eddf 2022-06-23 thomas }
3021 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3022 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3023 7cd52833 2022-06-23 thomas if (err)
3024 7cd52833 2022-06-23 thomas goto done;
3025 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3026 b672a97a 2020-01-27 stsp !s->log_branches);
3027 1a76625f 2018-10-22 stsp if (err)
3028 1a76625f 2018-10-22 stsp goto done;
3029 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3030 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3031 c5b78334 2020-01-12 stsp if (err)
3032 c5b78334 2020-01-12 stsp goto done;
3033 1a76625f 2018-10-22 stsp
3034 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3035 1a76625f 2018-10-22 stsp if (errcode) {
3036 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3037 1a76625f 2018-10-22 stsp goto done;
3038 1a76625f 2018-10-22 stsp }
3039 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3040 7c1452c1 2020-03-26 stsp if (errcode) {
3041 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3042 7c1452c1 2020-03-26 stsp goto done;
3043 7c1452c1 2020-03-26 stsp }
3044 1a76625f 2018-10-22 stsp
3045 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3046 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3047 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3048 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3049 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3050 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3051 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3052 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3053 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3054 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3055 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3056 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3057 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3058 ba4f502b 2018-08-04 stsp done:
3059 1a76625f 2018-10-22 stsp if (err)
3060 1a76625f 2018-10-22 stsp close_log_view(view);
3061 ba4f502b 2018-08-04 stsp return err;
3062 ba4f502b 2018-08-04 stsp }
3063 ba4f502b 2018-08-04 stsp
3064 e5a0f69f 2018-08-18 stsp static const struct got_error *
3065 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3066 ba4f502b 2018-08-04 stsp {
3067 f2f6d207 2020-11-24 stsp const struct got_error *err;
3068 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3069 ba4f502b 2018-08-04 stsp
3070 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3071 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3072 2b380cc8 2018-10-24 stsp &s->thread_args);
3073 2b380cc8 2018-10-24 stsp if (errcode)
3074 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3075 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3076 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3077 f2f6d207 2020-11-24 stsp if (err)
3078 f2f6d207 2020-11-24 stsp return err;
3079 f2f6d207 2020-11-24 stsp }
3080 2b380cc8 2018-10-24 stsp }
3081 2b380cc8 2018-10-24 stsp
3082 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3083 b31f89ff 2022-07-01 thomas }
3084 b31f89ff 2022-07-01 thomas
3085 b31f89ff 2022-07-01 thomas static void
3086 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3087 b31f89ff 2022-07-01 thomas {
3088 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3089 b31f89ff 2022-07-01 thomas
3090 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3091 b31f89ff 2022-07-01 thomas view->count = 0;
3092 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3093 b31f89ff 2022-07-01 thomas return;
3094 b31f89ff 2022-07-01 thomas
3095 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3096 b31f89ff 2022-07-01 thomas || home)
3097 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3098 b31f89ff 2022-07-01 thomas
3099 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3100 b31f89ff 2022-07-01 thomas --s->selected;
3101 b31f89ff 2022-07-01 thomas else
3102 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3103 b31f89ff 2022-07-01 thomas
3104 b31f89ff 2022-07-01 thomas select_commit(s);
3105 b31f89ff 2022-07-01 thomas return;
3106 b31f89ff 2022-07-01 thomas }
3107 b31f89ff 2022-07-01 thomas
3108 b31f89ff 2022-07-01 thomas static const struct got_error *
3109 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3110 b31f89ff 2022-07-01 thomas {
3111 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3112 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3113 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3114 b31f89ff 2022-07-01 thomas
3115 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3116 b31f89ff 2022-07-01 thomas if (first == NULL) {
3117 b31f89ff 2022-07-01 thomas view->count = 0;
3118 b31f89ff 2022-07-01 thomas return NULL;
3119 b31f89ff 2022-07-01 thomas }
3120 b31f89ff 2022-07-01 thomas
3121 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3122 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3123 b31f89ff 2022-07-01 thomas return NULL;
3124 b31f89ff 2022-07-01 thomas
3125 b31f89ff 2022-07-01 thomas if (!page) {
3126 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3127 b31f89ff 2022-07-01 thomas
3128 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3129 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3130 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3131 b31f89ff 2022-07-01 thomas ++s->selected;
3132 b31f89ff 2022-07-01 thomas else
3133 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3134 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3135 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3136 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3137 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3138 b31f89ff 2022-07-01 thomas else
3139 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3140 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3141 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3142 b31f89ff 2022-07-01 thomas } else {
3143 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3144 b31f89ff 2022-07-01 thomas if (err)
3145 b31f89ff 2022-07-01 thomas return err;
3146 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3147 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3148 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3149 b31f89ff 2022-07-01 thomas }
3150 b31f89ff 2022-07-01 thomas }
3151 b31f89ff 2022-07-01 thomas if (err)
3152 b31f89ff 2022-07-01 thomas return err;
3153 b31f89ff 2022-07-01 thomas
3154 a5d43cac 2022-07-01 thomas /*
3155 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3156 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3157 a5d43cac 2022-07-01 thomas */
3158 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3159 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3160 a5d43cac 2022-07-01 thomas
3161 b31f89ff 2022-07-01 thomas select_commit(s);
3162 b31f89ff 2022-07-01 thomas
3163 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3164 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3165 b31f89ff 2022-07-01 thomas view->count = 0;
3166 b31f89ff 2022-07-01 thomas
3167 b31f89ff 2022-07-01 thomas return NULL;
3168 e5a0f69f 2018-08-18 stsp }
3169 04cc582a 2018-08-01 stsp
3170 a5d43cac 2022-07-01 thomas static void
3171 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3172 a5d43cac 2022-07-01 thomas {
3173 24415785 2022-07-03 thomas *x = 0;
3174 24415785 2022-07-03 thomas *y = 0;
3175 24415785 2022-07-03 thomas
3176 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3177 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3178 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3179 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3180 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3181 53d2bdd3 2022-07-10 thomas else
3182 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3183 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3184 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3185 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3186 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3187 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3188 53d2bdd3 2022-07-10 thomas else
3189 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3190 53d2bdd3 2022-07-10 thomas }
3191 a5d43cac 2022-07-01 thomas }
3192 a5d43cac 2022-07-01 thomas
3193 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3194 e5a0f69f 2018-08-18 stsp static const struct got_error *
3195 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3196 a5d43cac 2022-07-01 thomas {
3197 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3198 a5d43cac 2022-07-01 thomas
3199 a5d43cac 2022-07-01 thomas view->nlines = y;
3200 64486692 2022-07-07 thomas view->ncols = COLS;
3201 a5d43cac 2022-07-01 thomas err = view_resize(view);
3202 a5d43cac 2022-07-01 thomas if (err)
3203 a5d43cac 2022-07-01 thomas return err;
3204 a5d43cac 2022-07-01 thomas
3205 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3206 a5d43cac 2022-07-01 thomas
3207 a5d43cac 2022-07-01 thomas return err;
3208 a5d43cac 2022-07-01 thomas }
3209 a5d43cac 2022-07-01 thomas
3210 a5d43cac 2022-07-01 thomas static const struct got_error *
3211 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3212 e5a0f69f 2018-08-18 stsp {
3213 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3214 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3215 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3216 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3217 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3218 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
3219 80ddbec8 2018-04-29 stsp
3220 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3221 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3222 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3223 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3224 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3225 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3226 fb280deb 2021-08-30 stsp }
3227 b31f89ff 2022-07-01 thomas return err;
3228 528dedf3 2021-08-30 stsp }
3229 068ab281 2022-07-01 thomas
3230 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3231 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3232 068ab281 2022-07-01 thomas --eos; /* border */
3233 068ab281 2022-07-01 thomas
3234 528dedf3 2021-08-30 stsp switch (ch) {
3235 1e37a5c2 2019-05-12 jcs case 'q':
3236 1e37a5c2 2019-05-12 jcs s->quit = 1;
3237 05171be4 2022-06-23 thomas break;
3238 05171be4 2022-06-23 thomas case '0':
3239 05171be4 2022-06-23 thomas view->x = 0;
3240 05171be4 2022-06-23 thomas break;
3241 05171be4 2022-06-23 thomas case '$':
3242 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3243 07b0611c 2022-06-23 thomas view->count = 0;
3244 05171be4 2022-06-23 thomas break;
3245 05171be4 2022-06-23 thomas case KEY_RIGHT:
3246 05171be4 2022-06-23 thomas case 'l':
3247 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3248 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3249 07b0611c 2022-06-23 thomas else
3250 07b0611c 2022-06-23 thomas view->count = 0;
3251 1e37a5c2 2019-05-12 jcs break;
3252 05171be4 2022-06-23 thomas case KEY_LEFT:
3253 05171be4 2022-06-23 thomas case 'h':
3254 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3255 07b0611c 2022-06-23 thomas if (view->x <= 0)
3256 07b0611c 2022-06-23 thomas view->count = 0;
3257 05171be4 2022-06-23 thomas break;
3258 1e37a5c2 2019-05-12 jcs case 'k':
3259 1e37a5c2 2019-05-12 jcs case KEY_UP:
3260 1e37a5c2 2019-05-12 jcs case '<':
3261 1e37a5c2 2019-05-12 jcs case ',':
3262 f7140bf5 2021-10-17 thomas case CTRL('p'):
3263 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3264 912a3f79 2021-08-30 j break;
3265 912a3f79 2021-08-30 j case 'g':
3266 912a3f79 2021-08-30 j case KEY_HOME:
3267 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3268 07b0611c 2022-06-23 thomas view->count = 0;
3269 1e37a5c2 2019-05-12 jcs break;
3270 70f17a53 2022-06-13 thomas case CTRL('u'):
3271 23427b14 2022-06-23 thomas case 'u':
3272 70f17a53 2022-06-13 thomas nscroll /= 2;
3273 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3274 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3275 a4292ac5 2019-05-12 jcs case CTRL('b'):
3276 1c5e5faa 2022-06-23 thomas case 'b':
3277 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3278 1e37a5c2 2019-05-12 jcs break;
3279 1e37a5c2 2019-05-12 jcs case 'j':
3280 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3281 1e37a5c2 2019-05-12 jcs case '>':
3282 1e37a5c2 2019-05-12 jcs case '.':
3283 f7140bf5 2021-10-17 thomas case CTRL('n'):
3284 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3285 912a3f79 2021-08-30 j break;
3286 f69c5a46 2022-07-19 thomas case '@':
3287 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3288 f69c5a46 2022-07-19 thomas break;
3289 912a3f79 2021-08-30 j case 'G':
3290 912a3f79 2021-08-30 j case KEY_END: {
3291 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3292 912a3f79 2021-08-30 j * traverse them all. */
3293 07b0611c 2022-06-23 thomas view->count = 0;
3294 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3295 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3296 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3297 80ddbec8 2018-04-29 stsp }
3298 912a3f79 2021-08-30 j
3299 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3300 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3301 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3302 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3303 f3bc9f1d 2021-09-05 naddy break;
3304 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3305 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3306 f3bc9f1d 2021-09-05 naddy }
3307 f3bc9f1d 2021-09-05 naddy if (n > 0)
3308 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3309 2b779855 2020-12-05 naddy select_commit(s);
3310 1e37a5c2 2019-05-12 jcs break;
3311 912a3f79 2021-08-30 j }
3312 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3313 23427b14 2022-06-23 thomas case 'd':
3314 70f17a53 2022-06-13 thomas nscroll /= 2;
3315 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3316 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3317 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3318 4c2d69cb 2022-06-23 thomas case 'f':
3319 b31f89ff 2022-07-01 thomas case ' ':
3320 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3321 1e37a5c2 2019-05-12 jcs break;
3322 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3323 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3324 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3325 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3326 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3327 2b779855 2020-12-05 naddy select_commit(s);
3328 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3329 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3330 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3331 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3332 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3333 0bf7f153 2020-12-02 naddy }
3334 1e37a5c2 2019-05-12 jcs break;
3335 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3336 444d5325 2022-07-03 thomas case '\r':
3337 07b0611c 2022-06-23 thomas view->count = 0;
3338 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3339 e5a0f69f 2018-08-18 stsp break;
3340 a5d43cac 2022-07-01 thomas
3341 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3342 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3343 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3344 a5d43cac 2022-07-01 thomas
3345 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3346 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3347 78756c87 2020-11-24 stsp view, s->repo);
3348 1e37a5c2 2019-05-12 jcs if (err)
3349 1e37a5c2 2019-05-12 jcs break;
3350 a5d43cac 2022-07-01 thomas
3351 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3352 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3353 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3354 a5d43cac 2022-07-01 thomas if (err)
3355 a5d43cac 2022-07-01 thomas break;
3356 a5d43cac 2022-07-01 thomas }
3357 a5d43cac 2022-07-01 thomas
3358 e78dc838 2020-12-04 stsp view->focussed = 0;
3359 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3360 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3361 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3362 a5d43cac 2022-07-01 thomas
3363 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3364 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
3365 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3366 f7013a22 2018-10-24 stsp if (err)
3367 1e37a5c2 2019-05-12 jcs return err;
3368 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3369 40236d76 2022-06-23 thomas if (err)
3370 40236d76 2022-06-23 thomas return err;
3371 e78dc838 2020-12-04 stsp view->focus_child = 1;
3372 1e37a5c2 2019-05-12 jcs } else
3373 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3374 1e37a5c2 2019-05-12 jcs break;
3375 1e37a5c2 2019-05-12 jcs case 't':
3376 07b0611c 2022-06-23 thomas view->count = 0;
3377 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3378 5036bf37 2018-09-24 stsp break;
3379 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3380 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3381 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3382 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3383 4e97c21c 2020-12-06 stsp s->repo);
3384 1e37a5c2 2019-05-12 jcs if (err)
3385 e5a0f69f 2018-08-18 stsp break;
3386 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3387 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3388 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3389 444d5325 2022-07-03 thomas if (err)
3390 444d5325 2022-07-03 thomas break;
3391 444d5325 2022-07-03 thomas }
3392 e78dc838 2020-12-04 stsp view->focussed = 0;
3393 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3394 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3395 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3396 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3397 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
3398 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3399 1e37a5c2 2019-05-12 jcs if (err)
3400 1e37a5c2 2019-05-12 jcs return err;
3401 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3402 40236d76 2022-06-23 thomas if (err)
3403 40236d76 2022-06-23 thomas return err;
3404 e78dc838 2020-12-04 stsp view->focus_child = 1;
3405 1e37a5c2 2019-05-12 jcs } else
3406 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3407 1e37a5c2 2019-05-12 jcs break;
3408 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3409 21355643 2020-12-06 stsp case CTRL('l'):
3410 21355643 2020-12-06 stsp case 'B':
3411 07b0611c 2022-06-23 thomas view->count = 0;
3412 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3413 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3414 1e37a5c2 2019-05-12 jcs break;
3415 21355643 2020-12-06 stsp err = stop_log_thread(s);
3416 74cfe85e 2020-10-20 stsp if (err)
3417 74cfe85e 2020-10-20 stsp return err;
3418 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3419 21355643 2020-12-06 stsp char *parent_path;
3420 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3421 21355643 2020-12-06 stsp if (err)
3422 21355643 2020-12-06 stsp return err;
3423 21355643 2020-12-06 stsp free(s->in_repo_path);
3424 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3425 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3426 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3427 21355643 2020-12-06 stsp struct got_object_id *start_id;
3428 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3429 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3430 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3431 21355643 2020-12-06 stsp if (err)
3432 21355643 2020-12-06 stsp return err;
3433 21355643 2020-12-06 stsp free(s->start_id);
3434 21355643 2020-12-06 stsp s->start_id = start_id;
3435 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3436 21355643 2020-12-06 stsp } else /* 'B' */
3437 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3438 21355643 2020-12-06 stsp
3439 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3440 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3441 bf7e79b3 2022-06-23 thomas if (err)
3442 bf7e79b3 2022-06-23 thomas return err;
3443 bf7e79b3 2022-06-23 thomas }
3444 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3445 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3446 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3447 74cfe85e 2020-10-20 stsp if (err)
3448 21355643 2020-12-06 stsp return err;
3449 51a10b52 2020-12-26 stsp tog_free_refs();
3450 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3451 ca51c541 2020-12-07 stsp if (err)
3452 ca51c541 2020-12-07 stsp return err;
3453 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3454 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3455 d01904d4 2019-06-25 stsp if (err)
3456 d01904d4 2019-06-25 stsp return err;
3457 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3458 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3459 b672a97a 2020-01-27 stsp if (err)
3460 b672a97a 2020-01-27 stsp return err;
3461 21355643 2020-12-06 stsp free_commits(&s->commits);
3462 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3463 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3464 21355643 2020-12-06 stsp s->selected_entry = NULL;
3465 21355643 2020-12-06 stsp s->selected = 0;
3466 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3467 21355643 2020-12-06 stsp s->quit = 0;
3468 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3469 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3470 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3471 d01904d4 2019-06-25 stsp break;
3472 6458efa5 2020-11-24 stsp case 'r':
3473 07b0611c 2022-06-23 thomas view->count = 0;
3474 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3475 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3476 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3477 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3478 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3479 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3480 6458efa5 2020-11-24 stsp if (err) {
3481 6458efa5 2020-11-24 stsp view_close(ref_view);
3482 6458efa5 2020-11-24 stsp return err;
3483 6458efa5 2020-11-24 stsp }
3484 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3485 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3486 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3487 444d5325 2022-07-03 thomas if (err)
3488 444d5325 2022-07-03 thomas break;
3489 444d5325 2022-07-03 thomas }
3490 e78dc838 2020-12-04 stsp view->focussed = 0;
3491 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3492 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3493 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3494 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3495 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
3496 6458efa5 2020-11-24 stsp err = view_close_child(view);
3497 6458efa5 2020-11-24 stsp if (err)
3498 6458efa5 2020-11-24 stsp return err;
3499 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3500 40236d76 2022-06-23 thomas if (err)
3501 40236d76 2022-06-23 thomas return err;
3502 e78dc838 2020-12-04 stsp view->focus_child = 1;
3503 6458efa5 2020-11-24 stsp } else
3504 6458efa5 2020-11-24 stsp *new_view = ref_view;
3505 6458efa5 2020-11-24 stsp break;
3506 1e37a5c2 2019-05-12 jcs default:
3507 07b0611c 2022-06-23 thomas view->count = 0;
3508 1e37a5c2 2019-05-12 jcs break;
3509 899d86c2 2018-05-10 stsp }
3510 e5a0f69f 2018-08-18 stsp
3511 80ddbec8 2018-04-29 stsp return err;
3512 80ddbec8 2018-04-29 stsp }
3513 80ddbec8 2018-04-29 stsp
3514 4ed7e80c 2018-05-20 stsp static const struct got_error *
3515 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3516 c2db6724 2019-01-04 stsp {
3517 c2db6724 2019-01-04 stsp const struct got_error *error;
3518 c2db6724 2019-01-04 stsp
3519 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3520 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3521 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3522 37c06ea4 2019-07-15 stsp #endif
3523 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3524 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3525 c2db6724 2019-01-04 stsp
3526 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3527 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3528 c2db6724 2019-01-04 stsp
3529 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3530 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3531 c2db6724 2019-01-04 stsp
3532 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3533 c2db6724 2019-01-04 stsp if (error != NULL)
3534 c2db6724 2019-01-04 stsp return error;
3535 c2db6724 2019-01-04 stsp
3536 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3537 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3538 c2db6724 2019-01-04 stsp
3539 c2db6724 2019-01-04 stsp return NULL;
3540 c2db6724 2019-01-04 stsp }
3541 c2db6724 2019-01-04 stsp
3542 a915003a 2019-02-05 stsp static void
3543 a915003a 2019-02-05 stsp init_curses(void)
3544 a915003a 2019-02-05 stsp {
3545 296152d1 2022-05-31 thomas /*
3546 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3547 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3548 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3549 296152d1 2022-05-31 thomas */
3550 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3551 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3552 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3553 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3554 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3555 296152d1 2022-05-31 thomas
3556 a915003a 2019-02-05 stsp initscr();
3557 a915003a 2019-02-05 stsp cbreak();
3558 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3559 a915003a 2019-02-05 stsp noecho();
3560 a915003a 2019-02-05 stsp nonl();
3561 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3562 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3563 a915003a 2019-02-05 stsp curs_set(0);
3564 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3565 6d17833f 2019-11-08 stsp start_color();
3566 6d17833f 2019-11-08 stsp use_default_colors();
3567 6d17833f 2019-11-08 stsp }
3568 a915003a 2019-02-05 stsp }
3569 a915003a 2019-02-05 stsp
3570 c2db6724 2019-01-04 stsp static const struct got_error *
3571 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3572 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3573 f135c941 2020-02-20 stsp {
3574 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3575 f135c941 2020-02-20 stsp
3576 f135c941 2020-02-20 stsp if (argc == 0) {
3577 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3578 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3579 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3580 f135c941 2020-02-20 stsp return NULL;
3581 f135c941 2020-02-20 stsp }
3582 f135c941 2020-02-20 stsp
3583 f135c941 2020-02-20 stsp if (worktree) {
3584 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3585 bfd61697 2020-11-14 stsp char *p;
3586 f135c941 2020-02-20 stsp
3587 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3588 f135c941 2020-02-20 stsp if (err)
3589 f135c941 2020-02-20 stsp return err;
3590 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3591 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3592 bfd61697 2020-11-14 stsp p) == -1) {
3593 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3594 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3595 f135c941 2020-02-20 stsp }
3596 f135c941 2020-02-20 stsp free(p);
3597 f135c941 2020-02-20 stsp } else
3598 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3599 f135c941 2020-02-20 stsp
3600 f135c941 2020-02-20 stsp return err;
3601 f135c941 2020-02-20 stsp }
3602 f135c941 2020-02-20 stsp
3603 f135c941 2020-02-20 stsp static const struct got_error *
3604 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3605 9f7d7167 2018-04-29 stsp {
3606 80ddbec8 2018-04-29 stsp const struct got_error *error;
3607 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3608 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3609 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3610 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3611 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3612 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3613 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3614 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3615 04cc582a 2018-08-01 stsp struct tog_view *view;
3616 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3617 80ddbec8 2018-04-29 stsp
3618 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3619 80ddbec8 2018-04-29 stsp switch (ch) {
3620 b672a97a 2020-01-27 stsp case 'b':
3621 b672a97a 2020-01-27 stsp log_branches = 1;
3622 b672a97a 2020-01-27 stsp break;
3623 80ddbec8 2018-04-29 stsp case 'c':
3624 80ddbec8 2018-04-29 stsp start_commit = optarg;
3625 80ddbec8 2018-04-29 stsp break;
3626 ecb28ae0 2018-07-16 stsp case 'r':
3627 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3628 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3629 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3630 9ba1d308 2019-10-21 stsp optarg);
3631 ecb28ae0 2018-07-16 stsp break;
3632 80ddbec8 2018-04-29 stsp default:
3633 17020d27 2019-03-07 stsp usage_log();
3634 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3635 80ddbec8 2018-04-29 stsp }
3636 80ddbec8 2018-04-29 stsp }
3637 80ddbec8 2018-04-29 stsp
3638 80ddbec8 2018-04-29 stsp argc -= optind;
3639 80ddbec8 2018-04-29 stsp argv += optind;
3640 80ddbec8 2018-04-29 stsp
3641 f135c941 2020-02-20 stsp if (argc > 1)
3642 f135c941 2020-02-20 stsp usage_log();
3643 963f97a1 2019-03-18 stsp
3644 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3645 7cd52833 2022-06-23 thomas if (error != NULL)
3646 7cd52833 2022-06-23 thomas goto done;
3647 7cd52833 2022-06-23 thomas
3648 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3649 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3650 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3651 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3652 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3653 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3654 c156c7a4 2020-12-18 stsp goto done;
3655 a1fbf39a 2019-08-11 stsp if (worktree)
3656 6962eb72 2020-02-20 stsp repo_path =
3657 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3658 a1fbf39a 2019-08-11 stsp else
3659 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3660 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3661 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3662 c156c7a4 2020-12-18 stsp goto done;
3663 c156c7a4 2020-12-18 stsp }
3664 ecb28ae0 2018-07-16 stsp }
3665 ecb28ae0 2018-07-16 stsp
3666 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3667 80ddbec8 2018-04-29 stsp if (error != NULL)
3668 ecb28ae0 2018-07-16 stsp goto done;
3669 80ddbec8 2018-04-29 stsp
3670 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3671 f135c941 2020-02-20 stsp repo, worktree);
3672 f135c941 2020-02-20 stsp if (error)
3673 f135c941 2020-02-20 stsp goto done;
3674 f135c941 2020-02-20 stsp
3675 f135c941 2020-02-20 stsp init_curses();
3676 f135c941 2020-02-20 stsp
3677 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3678 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3679 c02c541e 2019-03-29 stsp if (error)
3680 c02c541e 2019-03-29 stsp goto done;
3681 c02c541e 2019-03-29 stsp
3682 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3683 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3684 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3685 87670572 2020-12-26 naddy if (error)
3686 87670572 2020-12-26 naddy goto done;
3687 87670572 2020-12-26 naddy }
3688 51a10b52 2020-12-26 stsp
3689 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3690 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3691 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3692 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3693 d8f38dc4 2020-12-05 stsp if (error)
3694 d8f38dc4 2020-12-05 stsp goto done;
3695 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3696 d8f38dc4 2020-12-05 stsp } else {
3697 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3698 d8f38dc4 2020-12-05 stsp if (error == NULL)
3699 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3700 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3701 d8f38dc4 2020-12-05 stsp goto done;
3702 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3703 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3704 d8f38dc4 2020-12-05 stsp if (error)
3705 d8f38dc4 2020-12-05 stsp goto done;
3706 d8f38dc4 2020-12-05 stsp }
3707 8b473291 2019-02-21 stsp
3708 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3709 04cc582a 2018-08-01 stsp if (view == NULL) {
3710 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3711 04cc582a 2018-08-01 stsp goto done;
3712 04cc582a 2018-08-01 stsp }
3713 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3714 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3715 ba4f502b 2018-08-04 stsp if (error)
3716 ba4f502b 2018-08-04 stsp goto done;
3717 2fc00ff4 2019-08-31 stsp if (worktree) {
3718 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3719 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3720 2fc00ff4 2019-08-31 stsp worktree = NULL;
3721 2fc00ff4 2019-08-31 stsp }
3722 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3723 ecb28ae0 2018-07-16 stsp done:
3724 f135c941 2020-02-20 stsp free(in_repo_path);
3725 ecb28ae0 2018-07-16 stsp free(repo_path);
3726 ecb28ae0 2018-07-16 stsp free(cwd);
3727 899d86c2 2018-05-10 stsp free(start_id);
3728 d8f38dc4 2020-12-05 stsp free(label);
3729 d8f38dc4 2020-12-05 stsp if (ref)
3730 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3731 1d0f4054 2021-06-17 stsp if (repo) {
3732 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3733 1d0f4054 2021-06-17 stsp if (error == NULL)
3734 1d0f4054 2021-06-17 stsp error = close_err;
3735 1d0f4054 2021-06-17 stsp }
3736 ec142235 2019-03-07 stsp if (worktree)
3737 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3738 7cd52833 2022-06-23 thomas if (pack_fds) {
3739 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3740 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3741 7cd52833 2022-06-23 thomas if (error == NULL)
3742 7cd52833 2022-06-23 thomas error = pack_err;
3743 7cd52833 2022-06-23 thomas }
3744 51a10b52 2020-12-26 stsp tog_free_refs();
3745 80ddbec8 2018-04-29 stsp return error;
3746 9f7d7167 2018-04-29 stsp }
3747 9f7d7167 2018-04-29 stsp
3748 4ed7e80c 2018-05-20 stsp __dead static void
3749 9f7d7167 2018-04-29 stsp usage_diff(void)
3750 9f7d7167 2018-04-29 stsp {
3751 80ddbec8 2018-04-29 stsp endwin();
3752 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3753 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3754 9f7d7167 2018-04-29 stsp exit(1);
3755 b304db33 2018-05-20 stsp }
3756 b304db33 2018-05-20 stsp
3757 6d17833f 2019-11-08 stsp static int
3758 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3759 41605754 2020-11-12 stsp regmatch_t *regmatch)
3760 6d17833f 2019-11-08 stsp {
3761 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3762 6d17833f 2019-11-08 stsp }
3763 6d17833f 2019-11-08 stsp
3764 ef20f542 2022-06-26 thomas static struct tog_color *
3765 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3766 6d17833f 2019-11-08 stsp {
3767 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3768 6d17833f 2019-11-08 stsp
3769 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3770 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3771 6d17833f 2019-11-08 stsp return tc;
3772 6d17833f 2019-11-08 stsp }
3773 6d17833f 2019-11-08 stsp
3774 6d17833f 2019-11-08 stsp return NULL;
3775 6d17833f 2019-11-08 stsp }
3776 6d17833f 2019-11-08 stsp
3777 4ed7e80c 2018-05-20 stsp static const struct got_error *
3778 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3779 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3780 41605754 2020-11-12 stsp {
3781 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3782 666f7b10 2022-06-23 thomas char *exstr = NULL;
3783 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3784 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3785 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3786 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3787 41605754 2020-11-12 stsp
3788 41605754 2020-11-12 stsp *wtotal = 0;
3789 cbea3800 2022-06-23 thomas
3790 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3791 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3792 41605754 2020-11-12 stsp
3793 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3794 666f7b10 2022-06-23 thomas if (err)
3795 666f7b10 2022-06-23 thomas return err;
3796 666f7b10 2022-06-23 thomas
3797 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3798 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3799 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3800 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3801 666f7b10 2022-06-23 thomas goto done;
3802 666f7b10 2022-06-23 thomas }
3803 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3804 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3805 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3806 cbea3800 2022-06-23 thomas goto done;
3807 cbea3800 2022-06-23 thomas }
3808 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3809 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3810 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3811 cbea3800 2022-06-23 thomas goto done;
3812 cbea3800 2022-06-23 thomas }
3813 05171be4 2022-06-23 thomas
3814 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3815 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3816 cbea3800 2022-06-23 thomas col_tab_align, 1);
3817 cbea3800 2022-06-23 thomas if (err)
3818 cbea3800 2022-06-23 thomas goto done;
3819 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3820 05171be4 2022-06-23 thomas if (n) {
3821 cbea3800 2022-06-23 thomas free(wline);
3822 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3823 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3824 cbea3800 2022-06-23 thomas if (err)
3825 cbea3800 2022-06-23 thomas goto done;
3826 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3827 cbea3800 2022-06-23 thomas wlimit -= width;
3828 cbea3800 2022-06-23 thomas *wtotal += width;
3829 41605754 2020-11-12 stsp }
3830 41605754 2020-11-12 stsp
3831 41605754 2020-11-12 stsp if (wlimit > 0) {
3832 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3833 cbea3800 2022-06-23 thomas size_t wlen;
3834 cbea3800 2022-06-23 thomas
3835 cbea3800 2022-06-23 thomas free(wline);
3836 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3837 cbea3800 2022-06-23 thomas col_tab_align, 1);
3838 cbea3800 2022-06-23 thomas if (err)
3839 cbea3800 2022-06-23 thomas goto done;
3840 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3841 cbea3800 2022-06-23 thomas while (i < wlen) {
3842 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3843 cbea3800 2022-06-23 thomas if (width == -1) {
3844 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3845 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3846 cbea3800 2022-06-23 thomas goto done;
3847 cbea3800 2022-06-23 thomas }
3848 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3849 cbea3800 2022-06-23 thomas break;
3850 1c5e5faa 2022-06-23 thomas w += width;
3851 cbea3800 2022-06-23 thomas i++;
3852 41605754 2020-11-12 stsp }
3853 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3854 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3855 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3856 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3857 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3858 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3859 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3860 41605754 2020-11-12 stsp }
3861 41605754 2020-11-12 stsp }
3862 41605754 2020-11-12 stsp
3863 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3864 cbea3800 2022-06-23 thomas free(wline);
3865 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3866 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3867 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3868 cbea3800 2022-06-23 thomas col_tab_align, 1);
3869 cbea3800 2022-06-23 thomas if (err)
3870 cbea3800 2022-06-23 thomas goto done;
3871 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3872 cbea3800 2022-06-23 thomas } else {
3873 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3874 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3875 cbea3800 2022-06-23 thomas if (err)
3876 cbea3800 2022-06-23 thomas goto done;
3877 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3878 cbea3800 2022-06-23 thomas }
3879 cbea3800 2022-06-23 thomas *wtotal += width2;
3880 41605754 2020-11-12 stsp }
3881 cbea3800 2022-06-23 thomas done:
3882 05171be4 2022-06-23 thomas free(wline);
3883 666f7b10 2022-06-23 thomas free(exstr);
3884 cbea3800 2022-06-23 thomas free(seg0);
3885 cbea3800 2022-06-23 thomas free(seg1);
3886 cbea3800 2022-06-23 thomas free(seg2);
3887 cbea3800 2022-06-23 thomas return err;
3888 41605754 2020-11-12 stsp }
3889 41605754 2020-11-12 stsp
3890 41605754 2020-11-12 stsp static const struct got_error *
3891 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3892 26ed57b2 2018-05-19 stsp {
3893 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3894 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3895 61e69b96 2018-05-20 stsp const struct got_error *err;
3896 fe621944 2020-11-10 stsp int nprinted = 0;
3897 b304db33 2018-05-20 stsp char *line;
3898 826082fe 2020-12-10 stsp size_t linesize = 0;
3899 826082fe 2020-12-10 stsp ssize_t linelen;
3900 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3901 61e69b96 2018-05-20 stsp wchar_t *wline;
3902 e0b650dd 2018-05-20 stsp int width;
3903 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3904 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3905 fe621944 2020-11-10 stsp off_t line_offset;
3906 26ed57b2 2018-05-19 stsp
3907 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3908 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3909 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3910 fe621944 2020-11-10 stsp
3911 f7d12f7e 2018-08-01 stsp werase(view->window);
3912 a3404814 2018-09-02 stsp
3913 a3404814 2018-09-02 stsp if (header) {
3914 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3915 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3916 135a2da0 2020-11-11 stsp header) == -1)
3917 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3918 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3919 f91a2b48 2022-06-23 thomas 0, 0);
3920 135a2da0 2020-11-11 stsp free(line);
3921 135a2da0 2020-11-11 stsp if (err)
3922 a3404814 2018-09-02 stsp return err;
3923 a3404814 2018-09-02 stsp
3924 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3925 a3404814 2018-09-02 stsp wstandout(view->window);
3926 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3927 e54cc94a 2020-11-11 stsp free(wline);
3928 e54cc94a 2020-11-11 stsp wline = NULL;
3929 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3930 a3404814 2018-09-02 stsp wstandend(view->window);
3931 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3932 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3933 26ed57b2 2018-05-19 stsp
3934 a3404814 2018-09-02 stsp if (max_lines <= 1)
3935 a3404814 2018-09-02 stsp return NULL;
3936 a3404814 2018-09-02 stsp max_lines--;
3937 a3404814 2018-09-02 stsp }
3938 a3404814 2018-09-02 stsp
3939 89f1a395 2020-12-01 naddy s->eof = 0;
3940 05171be4 2022-06-23 thomas view->maxx = 0;
3941 826082fe 2020-12-10 stsp line = NULL;
3942 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3943 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3944 826082fe 2020-12-10 stsp if (linelen == -1) {
3945 826082fe 2020-12-10 stsp if (feof(s->f)) {
3946 826082fe 2020-12-10 stsp s->eof = 1;
3947 826082fe 2020-12-10 stsp break;
3948 826082fe 2020-12-10 stsp }
3949 826082fe 2020-12-10 stsp free(line);
3950 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3951 61e69b96 2018-05-20 stsp }
3952 05171be4 2022-06-23 thomas
3953 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3954 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3955 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3956 cbea3800 2022-06-23 thomas if (err) {
3957 cbea3800 2022-06-23 thomas free(line);
3958 cbea3800 2022-06-23 thomas return err;
3959 cbea3800 2022-06-23 thomas }
3960 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3961 cbea3800 2022-06-23 thomas free(wline);
3962 cbea3800 2022-06-23 thomas wline = NULL;
3963 cbea3800 2022-06-23 thomas
3964 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3965 f26dddb7 2019-11-08 stsp if (tc)
3966 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3967 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3968 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3969 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3970 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3971 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3972 41605754 2020-11-12 stsp if (err) {
3973 41605754 2020-11-12 stsp free(line);
3974 41605754 2020-11-12 stsp return err;
3975 41605754 2020-11-12 stsp }
3976 41605754 2020-11-12 stsp } else {
3977 f1c0ec19 2022-06-23 thomas int skip;
3978 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3979 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3980 f1c0ec19 2022-06-23 thomas if (err) {
3981 f1c0ec19 2022-06-23 thomas free(line);
3982 f1c0ec19 2022-06-23 thomas return err;
3983 f1c0ec19 2022-06-23 thomas }
3984 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3985 f1c0ec19 2022-06-23 thomas free(wline);
3986 41605754 2020-11-12 stsp wline = NULL;
3987 41605754 2020-11-12 stsp }
3988 f26dddb7 2019-11-08 stsp if (tc)
3989 6d17833f 2019-11-08 stsp wattr_off(view->window,
3990 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3991 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3992 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3993 fe621944 2020-11-10 stsp nprinted++;
3994 826082fe 2020-12-10 stsp }
3995 826082fe 2020-12-10 stsp free(line);
3996 fe621944 2020-11-10 stsp if (nprinted >= 1)
3997 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3998 89f1a395 2020-12-01 naddy (nprinted - 1);
3999 fe621944 2020-11-10 stsp else
4000 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4001 26ed57b2 2018-05-19 stsp
4002 a5d43cac 2022-07-01 thomas view_border(view);
4003 c3e9aa98 2019-05-13 jcs
4004 89f1a395 2020-12-01 naddy if (s->eof) {
4005 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4006 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4007 c3e9aa98 2019-05-13 jcs nprinted++;
4008 c3e9aa98 2019-05-13 jcs }
4009 c3e9aa98 2019-05-13 jcs
4010 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4011 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4012 c3e9aa98 2019-05-13 jcs if (err) {
4013 c3e9aa98 2019-05-13 jcs return err;
4014 c3e9aa98 2019-05-13 jcs }
4015 26ed57b2 2018-05-19 stsp
4016 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4017 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4018 e54cc94a 2020-11-11 stsp free(wline);
4019 e54cc94a 2020-11-11 stsp wline = NULL;
4020 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4021 c3e9aa98 2019-05-13 jcs }
4022 c3e9aa98 2019-05-13 jcs
4023 26ed57b2 2018-05-19 stsp return NULL;
4024 abd2672a 2018-12-23 stsp }
4025 abd2672a 2018-12-23 stsp
4026 abd2672a 2018-12-23 stsp static char *
4027 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4028 abd2672a 2018-12-23 stsp {
4029 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4030 09867e48 2019-08-13 stsp char *p, *s;
4031 09867e48 2019-08-13 stsp
4032 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4033 09867e48 2019-08-13 stsp if (tm == NULL)
4034 09867e48 2019-08-13 stsp return NULL;
4035 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4036 09867e48 2019-08-13 stsp if (s == NULL)
4037 09867e48 2019-08-13 stsp return NULL;
4038 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4039 abd2672a 2018-12-23 stsp if (p)
4040 abd2672a 2018-12-23 stsp *p = '\0';
4041 abd2672a 2018-12-23 stsp return s;
4042 9f7d7167 2018-04-29 stsp }
4043 9f7d7167 2018-04-29 stsp
4044 4ed7e80c 2018-05-20 stsp static const struct got_error *
4045 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4046 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4047 0208f208 2020-05-05 stsp {
4048 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4049 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4050 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4051 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4052 0208f208 2020-05-05 stsp
4053 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4054 0208f208 2020-05-05 stsp if (qid != NULL) {
4055 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4056 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4057 ec242592 2022-04-22 thomas &qid->id);
4058 0208f208 2020-05-05 stsp if (err)
4059 0208f208 2020-05-05 stsp return err;
4060 0208f208 2020-05-05 stsp
4061 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4062 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4063 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4064 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4065 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4066 aa8b5dd0 2021-08-01 stsp }
4067 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4068 0208f208 2020-05-05 stsp
4069 0208f208 2020-05-05 stsp }
4070 0208f208 2020-05-05 stsp
4071 0208f208 2020-05-05 stsp if (tree_id1) {
4072 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4073 0208f208 2020-05-05 stsp if (err)
4074 0208f208 2020-05-05 stsp goto done;
4075 0208f208 2020-05-05 stsp }
4076 0208f208 2020-05-05 stsp
4077 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4078 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4079 0208f208 2020-05-05 stsp if (err)
4080 0208f208 2020-05-05 stsp goto done;
4081 0208f208 2020-05-05 stsp
4082 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4083 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4084 0208f208 2020-05-05 stsp done:
4085 0208f208 2020-05-05 stsp if (tree1)
4086 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4087 0208f208 2020-05-05 stsp if (tree2)
4088 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4089 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4090 0208f208 2020-05-05 stsp return err;
4091 0208f208 2020-05-05 stsp }
4092 0208f208 2020-05-05 stsp
4093 0208f208 2020-05-05 stsp static const struct got_error *
4094 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4095 abd2672a 2018-12-23 stsp {
4096 fe621944 2020-11-10 stsp off_t *p;
4097 fe621944 2020-11-10 stsp
4098 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4099 fe621944 2020-11-10 stsp if (p == NULL)
4100 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4101 fe621944 2020-11-10 stsp *line_offsets = p;
4102 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4103 fe621944 2020-11-10 stsp (*nlines)++;
4104 fe621944 2020-11-10 stsp return NULL;
4105 fe621944 2020-11-10 stsp }
4106 fe621944 2020-11-10 stsp
4107 fe621944 2020-11-10 stsp static const struct got_error *
4108 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4109 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4110 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4111 fe621944 2020-11-10 stsp {
4112 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4113 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4114 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4115 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4116 45d799e2 2018-12-23 stsp time_t committer_time;
4117 45d799e2 2018-12-23 stsp const char *author, *committer;
4118 8b473291 2019-02-21 stsp char *refs_str = NULL;
4119 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4120 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4121 fe621944 2020-11-10 stsp off_t outoff = 0;
4122 fe621944 2020-11-10 stsp int n;
4123 abd2672a 2018-12-23 stsp
4124 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4125 0208f208 2020-05-05 stsp
4126 8b473291 2019-02-21 stsp if (refs) {
4127 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4128 8b473291 2019-02-21 stsp if (err)
4129 8b473291 2019-02-21 stsp return err;
4130 8b473291 2019-02-21 stsp }
4131 8b473291 2019-02-21 stsp
4132 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4133 abd2672a 2018-12-23 stsp if (err)
4134 abd2672a 2018-12-23 stsp return err;
4135 abd2672a 2018-12-23 stsp
4136 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4137 15a94983 2018-12-23 stsp if (err) {
4138 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4139 15a94983 2018-12-23 stsp goto done;
4140 15a94983 2018-12-23 stsp }
4141 abd2672a 2018-12-23 stsp
4142 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4143 fe621944 2020-11-10 stsp if (err)
4144 fe621944 2020-11-10 stsp goto done;
4145 fe621944 2020-11-10 stsp
4146 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4147 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4148 fe621944 2020-11-10 stsp if (n < 0) {
4149 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4150 abd2672a 2018-12-23 stsp goto done;
4151 abd2672a 2018-12-23 stsp }
4152 fe621944 2020-11-10 stsp outoff += n;
4153 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4154 fe621944 2020-11-10 stsp if (err)
4155 fe621944 2020-11-10 stsp goto done;
4156 fe621944 2020-11-10 stsp
4157 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4158 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4159 fe621944 2020-11-10 stsp if (n < 0) {
4160 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4161 abd2672a 2018-12-23 stsp goto done;
4162 abd2672a 2018-12-23 stsp }
4163 fe621944 2020-11-10 stsp outoff += n;
4164 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4165 fe621944 2020-11-10 stsp if (err)
4166 fe621944 2020-11-10 stsp goto done;
4167 fe621944 2020-11-10 stsp
4168 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4169 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4170 fe621944 2020-11-10 stsp if (datestr) {
4171 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4172 fe621944 2020-11-10 stsp if (n < 0) {
4173 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4174 fe621944 2020-11-10 stsp goto done;
4175 fe621944 2020-11-10 stsp }
4176 fe621944 2020-11-10 stsp outoff += n;
4177 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4178 fe621944 2020-11-10 stsp if (err)
4179 fe621944 2020-11-10 stsp goto done;
4180 abd2672a 2018-12-23 stsp }
4181 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4182 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4183 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4184 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4185 fe621944 2020-11-10 stsp if (n < 0) {
4186 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4187 fe621944 2020-11-10 stsp goto done;
4188 fe621944 2020-11-10 stsp }
4189 fe621944 2020-11-10 stsp outoff += n;
4190 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4191 fe621944 2020-11-10 stsp if (err)
4192 fe621944 2020-11-10 stsp goto done;
4193 abd2672a 2018-12-23 stsp }
4194 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4195 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4196 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4197 ac4dc263 2021-09-24 thomas int pn = 1;
4198 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4199 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4200 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4201 ac4dc263 2021-09-24 thomas if (err)
4202 ac4dc263 2021-09-24 thomas goto done;
4203 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4204 ac4dc263 2021-09-24 thomas if (n < 0) {
4205 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4206 ac4dc263 2021-09-24 thomas goto done;
4207 ac4dc263 2021-09-24 thomas }
4208 ac4dc263 2021-09-24 thomas outoff += n;
4209 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4210 ac4dc263 2021-09-24 thomas if (err)
4211 ac4dc263 2021-09-24 thomas goto done;
4212 ac4dc263 2021-09-24 thomas free(id_str);
4213 ac4dc263 2021-09-24 thomas id_str = NULL;
4214 ac4dc263 2021-09-24 thomas }
4215 ac4dc263 2021-09-24 thomas }
4216 ac4dc263 2021-09-24 thomas
4217 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4218 5943eee2 2019-08-13 stsp if (err)
4219 5943eee2 2019-08-13 stsp goto done;
4220 fe621944 2020-11-10 stsp s = logmsg;
4221 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4222 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4223 fe621944 2020-11-10 stsp if (n < 0) {
4224 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4225 fe621944 2020-11-10 stsp goto done;
4226 fe621944 2020-11-10 stsp }
4227 fe621944 2020-11-10 stsp outoff += n;
4228 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4229 fe621944 2020-11-10 stsp if (err)
4230 fe621944 2020-11-10 stsp goto done;
4231 abd2672a 2018-12-23 stsp }
4232 fe621944 2020-11-10 stsp
4233 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4234 0208f208 2020-05-05 stsp if (err)
4235 0208f208 2020-05-05 stsp goto done;
4236 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4237 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4238 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4239 fe621944 2020-11-10 stsp if (n < 0) {
4240 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4241 fe621944 2020-11-10 stsp goto done;
4242 fe621944 2020-11-10 stsp }
4243 fe621944 2020-11-10 stsp outoff += n;
4244 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4245 fe621944 2020-11-10 stsp if (err)
4246 fe621944 2020-11-10 stsp goto done;
4247 0208f208 2020-05-05 stsp free((char *)pe->path);
4248 0208f208 2020-05-05 stsp free(pe->data);
4249 0208f208 2020-05-05 stsp }
4250 fe621944 2020-11-10 stsp
4251 0208f208 2020-05-05 stsp fputc('\n', outfile);
4252 fe621944 2020-11-10 stsp outoff++;
4253 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4254 abd2672a 2018-12-23 stsp done:
4255 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4256 abd2672a 2018-12-23 stsp free(id_str);
4257 5943eee2 2019-08-13 stsp free(logmsg);
4258 8b473291 2019-02-21 stsp free(refs_str);
4259 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4260 7510f233 2020-08-09 stsp if (err) {
4261 ae6a6978 2020-08-09 stsp free(*line_offsets);
4262 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4263 ae6a6978 2020-08-09 stsp *nlines = 0;
4264 7510f233 2020-08-09 stsp }
4265 fe621944 2020-11-10 stsp return err;
4266 abd2672a 2018-12-23 stsp }
4267 abd2672a 2018-12-23 stsp
4268 abd2672a 2018-12-23 stsp static const struct got_error *
4269 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4270 26ed57b2 2018-05-19 stsp {
4271 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4272 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4273 15a94983 2018-12-23 stsp int obj_type;
4274 fe621944 2020-11-10 stsp
4275 fe621944 2020-11-10 stsp free(s->line_offsets);
4276 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4277 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4278 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4279 fe621944 2020-11-10 stsp s->nlines = 0;
4280 26ed57b2 2018-05-19 stsp
4281 511a516b 2018-05-19 stsp f = got_opentemp();
4282 48ae06ee 2018-10-18 stsp if (f == NULL) {
4283 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4284 48ae06ee 2018-10-18 stsp goto done;
4285 48ae06ee 2018-10-18 stsp }
4286 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4287 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4288 fb43ecf1 2019-02-11 stsp goto done;
4289 fb43ecf1 2019-02-11 stsp }
4290 48ae06ee 2018-10-18 stsp s->f = f;
4291 26ed57b2 2018-05-19 stsp
4292 15a94983 2018-12-23 stsp if (s->id1)
4293 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4294 15a94983 2018-12-23 stsp else
4295 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4296 15a94983 2018-12-23 stsp if (err)
4297 15a94983 2018-12-23 stsp goto done;
4298 15a94983 2018-12-23 stsp
4299 15a94983 2018-12-23 stsp switch (obj_type) {
4300 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4301 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4302 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4303 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4304 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4305 26ed57b2 2018-05-19 stsp break;
4306 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4307 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4308 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4309 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4310 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4311 26ed57b2 2018-05-19 stsp break;
4312 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4313 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4314 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4315 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4316 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4317 abd2672a 2018-12-23 stsp
4318 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4319 abd2672a 2018-12-23 stsp if (err)
4320 3ffacbe1 2020-02-02 tracey goto done;
4321 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4322 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4323 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4324 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4325 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4326 f44b1f58 2020-02-02 tracey if (err)
4327 f44b1f58 2020-02-02 tracey goto done;
4328 f44b1f58 2020-02-02 tracey } else {
4329 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4330 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4331 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4332 fe621944 2020-11-10 stsp err = write_commit_info(
4333 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4334 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4335 f44b1f58 2020-02-02 tracey if (err)
4336 f44b1f58 2020-02-02 tracey goto done;
4337 f5404e4e 2020-02-02 tracey break;
4338 15a087fe 2019-02-21 stsp }
4339 abd2672a 2018-12-23 stsp }
4340 abd2672a 2018-12-23 stsp }
4341 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4342 abd2672a 2018-12-23 stsp
4343 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4344 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4345 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4346 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4347 26ed57b2 2018-05-19 stsp break;
4348 abd2672a 2018-12-23 stsp }
4349 26ed57b2 2018-05-19 stsp default:
4350 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4351 48ae06ee 2018-10-18 stsp break;
4352 26ed57b2 2018-05-19 stsp }
4353 f44b1f58 2020-02-02 tracey if (err)
4354 f44b1f58 2020-02-02 tracey goto done;
4355 48ae06ee 2018-10-18 stsp done:
4356 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4357 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4358 48ae06ee 2018-10-18 stsp return err;
4359 48ae06ee 2018-10-18 stsp }
4360 26ed57b2 2018-05-19 stsp
4361 f5215bb9 2019-02-22 stsp static void
4362 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4363 f5215bb9 2019-02-22 stsp {
4364 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4365 f5215bb9 2019-02-22 stsp update_panels();
4366 f5215bb9 2019-02-22 stsp doupdate();
4367 f44b1f58 2020-02-02 tracey }
4368 f44b1f58 2020-02-02 tracey
4369 f44b1f58 2020-02-02 tracey static const struct got_error *
4370 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4371 f44b1f58 2020-02-02 tracey {
4372 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4373 f44b1f58 2020-02-02 tracey
4374 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4375 f44b1f58 2020-02-02 tracey return NULL;
4376 f44b1f58 2020-02-02 tracey }
4377 f44b1f58 2020-02-02 tracey
4378 f44b1f58 2020-02-02 tracey static const struct got_error *
4379 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4380 f44b1f58 2020-02-02 tracey {
4381 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4382 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4383 f44b1f58 2020-02-02 tracey int lineno;
4384 78eecffc 2022-06-23 thomas char *line = NULL;
4385 826082fe 2020-12-10 stsp size_t linesize = 0;
4386 826082fe 2020-12-10 stsp ssize_t linelen;
4387 f44b1f58 2020-02-02 tracey
4388 f44b1f58 2020-02-02 tracey if (!view->searching) {
4389 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4390 f44b1f58 2020-02-02 tracey return NULL;
4391 f44b1f58 2020-02-02 tracey }
4392 f44b1f58 2020-02-02 tracey
4393 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4394 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4395 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4396 f44b1f58 2020-02-02 tracey else
4397 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4398 4b3f9dac 2021-12-17 thomas } else
4399 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4400 f44b1f58 2020-02-02 tracey
4401 f44b1f58 2020-02-02 tracey while (1) {
4402 f44b1f58 2020-02-02 tracey off_t offset;
4403 f44b1f58 2020-02-02 tracey
4404 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4405 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4406 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4407 f44b1f58 2020-02-02 tracey break;
4408 f44b1f58 2020-02-02 tracey }
4409 f44b1f58 2020-02-02 tracey
4410 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4411 f44b1f58 2020-02-02 tracey lineno = 1;
4412 f44b1f58 2020-02-02 tracey else
4413 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4414 f44b1f58 2020-02-02 tracey }
4415 f44b1f58 2020-02-02 tracey
4416 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4417 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4418 f44b1f58 2020-02-02 tracey free(line);
4419 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4420 f44b1f58 2020-02-02 tracey }
4421 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4422 78eecffc 2022-06-23 thomas if (linelen != -1) {
4423 78eecffc 2022-06-23 thomas char *exstr;
4424 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4425 78eecffc 2022-06-23 thomas if (err)
4426 78eecffc 2022-06-23 thomas break;
4427 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4428 78eecffc 2022-06-23 thomas &view->regmatch)) {
4429 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4430 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4431 78eecffc 2022-06-23 thomas free(exstr);
4432 78eecffc 2022-06-23 thomas break;
4433 78eecffc 2022-06-23 thomas }
4434 78eecffc 2022-06-23 thomas free(exstr);
4435 f44b1f58 2020-02-02 tracey }
4436 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4437 f44b1f58 2020-02-02 tracey lineno++;
4438 f44b1f58 2020-02-02 tracey else
4439 f44b1f58 2020-02-02 tracey lineno--;
4440 f44b1f58 2020-02-02 tracey }
4441 826082fe 2020-12-10 stsp free(line);
4442 f44b1f58 2020-02-02 tracey
4443 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4444 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4445 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4446 f44b1f58 2020-02-02 tracey }
4447 f44b1f58 2020-02-02 tracey
4448 05171be4 2022-06-23 thomas return err;
4449 f5215bb9 2019-02-22 stsp }
4450 f5215bb9 2019-02-22 stsp
4451 48ae06ee 2018-10-18 stsp static const struct got_error *
4452 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4453 a0f32f33 2022-06-13 thomas {
4454 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4455 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4456 a0f32f33 2022-06-13 thomas
4457 a0f32f33 2022-06-13 thomas free(s->id1);
4458 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4459 a0f32f33 2022-06-13 thomas free(s->id2);
4460 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4461 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4462 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4463 a0f32f33 2022-06-13 thomas s->f = NULL;
4464 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4465 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4466 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4467 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4468 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4469 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4470 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4471 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4472 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4473 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4474 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4475 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4476 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4477 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4478 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4479 a0f32f33 2022-06-13 thomas s->nlines = 0;
4480 a0f32f33 2022-06-13 thomas return err;
4481 a0f32f33 2022-06-13 thomas }
4482 a0f32f33 2022-06-13 thomas
4483 a0f32f33 2022-06-13 thomas static const struct got_error *
4484 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4485 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4486 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4487 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4488 48ae06ee 2018-10-18 stsp {
4489 48ae06ee 2018-10-18 stsp const struct got_error *err;
4490 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4491 5dc9f4bc 2018-08-04 stsp
4492 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4493 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4494 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4495 a0f32f33 2022-06-13 thomas
4496 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4497 15a94983 2018-12-23 stsp int type1, type2;
4498 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4499 15a94983 2018-12-23 stsp if (err)
4500 15a94983 2018-12-23 stsp return err;
4501 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4502 15a94983 2018-12-23 stsp if (err)
4503 15a94983 2018-12-23 stsp return err;
4504 15a94983 2018-12-23 stsp
4505 15a94983 2018-12-23 stsp if (type1 != type2)
4506 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4507 15a94983 2018-12-23 stsp }
4508 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4509 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4510 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4511 f44b1f58 2020-02-02 tracey s->repo = repo;
4512 f44b1f58 2020-02-02 tracey s->id1 = id1;
4513 f44b1f58 2020-02-02 tracey s->id2 = id2;
4514 3dbaef42 2020-11-24 stsp s->label1 = label1;
4515 3dbaef42 2020-11-24 stsp s->label2 = label2;
4516 48ae06ee 2018-10-18 stsp
4517 15a94983 2018-12-23 stsp if (id1) {
4518 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4519 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4520 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4521 48ae06ee 2018-10-18 stsp } else
4522 5465d566 2020-02-01 tracey s->id1 = NULL;
4523 48ae06ee 2018-10-18 stsp
4524 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4525 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4526 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4527 a0f32f33 2022-06-13 thomas goto done;
4528 48ae06ee 2018-10-18 stsp }
4529 a0f32f33 2022-06-13 thomas
4530 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4531 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4532 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4533 9a1d7435 2022-06-23 thomas goto done;
4534 9a1d7435 2022-06-23 thomas }
4535 9a1d7435 2022-06-23 thomas
4536 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4537 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4538 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4539 a0f32f33 2022-06-13 thomas goto done;
4540 a0f32f33 2022-06-13 thomas }
4541 a0f32f33 2022-06-13 thomas
4542 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4543 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4544 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4545 19a6a6b5 2022-07-01 thomas goto done;
4546 19a6a6b5 2022-07-01 thomas }
4547 19a6a6b5 2022-07-01 thomas
4548 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4549 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4550 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4551 19a6a6b5 2022-07-01 thomas goto done;
4552 19a6a6b5 2022-07-01 thomas }
4553 19a6a6b5 2022-07-01 thomas
4554 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4555 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4556 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4557 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4558 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4559 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4560 5465d566 2020-02-01 tracey s->repo = repo;
4561 6d17833f 2019-11-08 stsp
4562 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4563 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4564 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4565 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4566 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4567 6d17833f 2019-11-08 stsp if (err)
4568 a0f32f33 2022-06-13 thomas goto done;
4569 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4570 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4571 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4572 a0f32f33 2022-06-13 thomas if (err)
4573 a0f32f33 2022-06-13 thomas goto done;
4574 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4575 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4576 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4577 a0f32f33 2022-06-13 thomas if (err)
4578 a0f32f33 2022-06-13 thomas goto done;
4579 6d17833f 2019-11-08 stsp
4580 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4581 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4582 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4583 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4584 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4585 a0f32f33 2022-06-13 thomas if (err)
4586 a0f32f33 2022-06-13 thomas goto done;
4587 11b20872 2019-11-08 stsp
4588 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4589 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4590 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4591 a0f32f33 2022-06-13 thomas if (err)
4592 a0f32f33 2022-06-13 thomas goto done;
4593 11b20872 2019-11-08 stsp
4594 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4595 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4596 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4597 a0f32f33 2022-06-13 thomas if (err)
4598 a0f32f33 2022-06-13 thomas goto done;
4599 6d17833f 2019-11-08 stsp }
4600 5dc9f4bc 2018-08-04 stsp
4601 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4602 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4603 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4604 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4605 f5215bb9 2019-02-22 stsp
4606 5465d566 2020-02-01 tracey err = create_diff(s);
4607 48ae06ee 2018-10-18 stsp
4608 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4609 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4610 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4611 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4612 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4613 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4614 a0f32f33 2022-06-13 thomas done:
4615 a0f32f33 2022-06-13 thomas if (err)
4616 a0f32f33 2022-06-13 thomas close_diff_view(view);
4617 e5a0f69f 2018-08-18 stsp return err;
4618 5dc9f4bc 2018-08-04 stsp }
4619 5dc9f4bc 2018-08-04 stsp
4620 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4621 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4622 5dc9f4bc 2018-08-04 stsp {
4623 a3404814 2018-09-02 stsp const struct got_error *err;
4624 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4625 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4626 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4627 a3404814 2018-09-02 stsp
4628 a3404814 2018-09-02 stsp if (s->id1) {
4629 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4630 a3404814 2018-09-02 stsp if (err)
4631 a3404814 2018-09-02 stsp return err;
4632 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4633 3dbaef42 2020-11-24 stsp } else
4634 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4635 3dbaef42 2020-11-24 stsp
4636 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4637 a3404814 2018-09-02 stsp if (err)
4638 a3404814 2018-09-02 stsp return err;
4639 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4640 26ed57b2 2018-05-19 stsp
4641 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4642 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4643 a3404814 2018-09-02 stsp free(id_str1);
4644 a3404814 2018-09-02 stsp free(id_str2);
4645 a3404814 2018-09-02 stsp return err;
4646 a3404814 2018-09-02 stsp }
4647 a3404814 2018-09-02 stsp free(id_str1);
4648 a3404814 2018-09-02 stsp free(id_str2);
4649 a3404814 2018-09-02 stsp
4650 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4651 267bb3b8 2021-08-01 stsp free(header);
4652 267bb3b8 2021-08-01 stsp return err;
4653 15a087fe 2019-02-21 stsp }
4654 15a087fe 2019-02-21 stsp
4655 15a087fe 2019-02-21 stsp static const struct got_error *
4656 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4657 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4658 15a087fe 2019-02-21 stsp {
4659 d7a04538 2019-02-21 stsp const struct got_error *err;
4660 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4661 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4662 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4663 15a087fe 2019-02-21 stsp
4664 15a087fe 2019-02-21 stsp free(s->id2);
4665 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4666 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4667 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4668 15a087fe 2019-02-21 stsp
4669 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4670 d7a04538 2019-02-21 stsp if (err)
4671 d7a04538 2019-02-21 stsp return err;
4672 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4673 15a087fe 2019-02-21 stsp free(s->id1);
4674 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4675 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4676 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4677 15a087fe 2019-02-21 stsp return NULL;
4678 0cf4efb1 2018-09-29 stsp }
4679 0cf4efb1 2018-09-29 stsp
4680 0cf4efb1 2018-09-29 stsp static const struct got_error *
4681 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4682 adf4c9e0 2022-07-03 thomas {
4683 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4684 adf4c9e0 2022-07-03 thomas
4685 adf4c9e0 2022-07-03 thomas view->count = 0;
4686 adf4c9e0 2022-07-03 thomas wclear(view->window);
4687 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4688 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4689 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4690 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4691 adf4c9e0 2022-07-03 thomas return create_diff(s);
4692 adf4c9e0 2022-07-03 thomas }
4693 adf4c9e0 2022-07-03 thomas
4694 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4695 4fc71f3b 2022-07-12 thomas int, int, int);
4696 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4697 4fc71f3b 2022-07-12 thomas int, int);
4698 4fc71f3b 2022-07-12 thomas
4699 adf4c9e0 2022-07-03 thomas static const struct got_error *
4700 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4701 e5a0f69f 2018-08-18 stsp {
4702 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4703 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4704 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4705 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4706 826082fe 2020-12-10 stsp char *line = NULL;
4707 826082fe 2020-12-10 stsp size_t linesize = 0;
4708 826082fe 2020-12-10 stsp ssize_t linelen;
4709 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4710 e5a0f69f 2018-08-18 stsp
4711 e5a0f69f 2018-08-18 stsp switch (ch) {
4712 05171be4 2022-06-23 thomas case '0':
4713 05171be4 2022-06-23 thomas view->x = 0;
4714 05171be4 2022-06-23 thomas break;
4715 05171be4 2022-06-23 thomas case '$':
4716 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4717 07b0611c 2022-06-23 thomas view->count = 0;
4718 05171be4 2022-06-23 thomas break;
4719 05171be4 2022-06-23 thomas case KEY_RIGHT:
4720 05171be4 2022-06-23 thomas case 'l':
4721 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4722 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4723 07b0611c 2022-06-23 thomas else
4724 07b0611c 2022-06-23 thomas view->count = 0;
4725 05171be4 2022-06-23 thomas break;
4726 05171be4 2022-06-23 thomas case KEY_LEFT:
4727 05171be4 2022-06-23 thomas case 'h':
4728 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4729 07b0611c 2022-06-23 thomas if (view->x <= 0)
4730 07b0611c 2022-06-23 thomas view->count = 0;
4731 05171be4 2022-06-23 thomas break;
4732 64453f7e 2020-11-21 stsp case 'a':
4733 3dbaef42 2020-11-24 stsp case 'w':
4734 3dbaef42 2020-11-24 stsp if (ch == 'a')
4735 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4736 3dbaef42 2020-11-24 stsp if (ch == 'w')
4737 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4738 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4739 912a3f79 2021-08-30 j break;
4740 912a3f79 2021-08-30 j case 'g':
4741 912a3f79 2021-08-30 j case KEY_HOME:
4742 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4743 07b0611c 2022-06-23 thomas view->count = 0;
4744 64453f7e 2020-11-21 stsp break;
4745 912a3f79 2021-08-30 j case 'G':
4746 912a3f79 2021-08-30 j case KEY_END:
4747 07b0611c 2022-06-23 thomas view->count = 0;
4748 912a3f79 2021-08-30 j if (s->eof)
4749 912a3f79 2021-08-30 j break;
4750 912a3f79 2021-08-30 j
4751 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4752 912a3f79 2021-08-30 j s->eof = 1;
4753 912a3f79 2021-08-30 j break;
4754 1e37a5c2 2019-05-12 jcs case 'k':
4755 1e37a5c2 2019-05-12 jcs case KEY_UP:
4756 f7140bf5 2021-10-17 thomas case CTRL('p'):
4757 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4758 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4759 07b0611c 2022-06-23 thomas else
4760 07b0611c 2022-06-23 thomas view->count = 0;
4761 1e37a5c2 2019-05-12 jcs break;
4762 70f17a53 2022-06-13 thomas case CTRL('u'):
4763 23427b14 2022-06-23 thomas case 'u':
4764 70f17a53 2022-06-13 thomas nscroll /= 2;
4765 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4766 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4767 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4768 1c5e5faa 2022-06-23 thomas case 'b':
4769 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4770 07b0611c 2022-06-23 thomas view->count = 0;
4771 26ed57b2 2018-05-19 stsp break;
4772 07b0611c 2022-06-23 thomas }
4773 1e37a5c2 2019-05-12 jcs i = 0;
4774 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4775 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4776 1e37a5c2 2019-05-12 jcs break;
4777 1e37a5c2 2019-05-12 jcs case 'j':
4778 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4779 f7140bf5 2021-10-17 thomas case CTRL('n'):
4780 1e37a5c2 2019-05-12 jcs if (!s->eof)
4781 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4782 07b0611c 2022-06-23 thomas else
4783 07b0611c 2022-06-23 thomas view->count = 0;
4784 1e37a5c2 2019-05-12 jcs break;
4785 70f17a53 2022-06-13 thomas case CTRL('d'):
4786 23427b14 2022-06-23 thomas case 'd':
4787 70f17a53 2022-06-13 thomas nscroll /= 2;
4788 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4789 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4790 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4791 1c5e5faa 2022-06-23 thomas case 'f':
4792 1e37a5c2 2019-05-12 jcs case ' ':
4793 07b0611c 2022-06-23 thomas if (s->eof) {
4794 07b0611c 2022-06-23 thomas view->count = 0;
4795 1e37a5c2 2019-05-12 jcs break;
4796 07b0611c 2022-06-23 thomas }
4797 1e37a5c2 2019-05-12 jcs i = 0;
4798 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4799 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4800 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4801 826082fe 2020-12-10 stsp if (linelen == -1) {
4802 826082fe 2020-12-10 stsp if (feof(s->f)) {
4803 826082fe 2020-12-10 stsp s->eof = 1;
4804 826082fe 2020-12-10 stsp } else
4805 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4806 34bc9ec9 2019-02-22 stsp break;
4807 826082fe 2020-12-10 stsp }
4808 1e37a5c2 2019-05-12 jcs }
4809 826082fe 2020-12-10 stsp free(line);
4810 1e37a5c2 2019-05-12 jcs break;
4811 1e37a5c2 2019-05-12 jcs case '[':
4812 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4813 1e37a5c2 2019-05-12 jcs s->diff_context--;
4814 aa61903a 2021-12-31 thomas s->matched_line = 0;
4815 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4816 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4817 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4818 27829c9e 2020-11-21 stsp s->nlines) {
4819 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4820 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4821 27829c9e 2020-11-21 stsp }
4822 07b0611c 2022-06-23 thomas } else
4823 07b0611c 2022-06-23 thomas view->count = 0;
4824 1e37a5c2 2019-05-12 jcs break;
4825 1e37a5c2 2019-05-12 jcs case ']':
4826 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4827 1e37a5c2 2019-05-12 jcs s->diff_context++;
4828 aa61903a 2021-12-31 thomas s->matched_line = 0;
4829 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4830 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4831 07b0611c 2022-06-23 thomas } else
4832 07b0611c 2022-06-23 thomas view->count = 0;
4833 1e37a5c2 2019-05-12 jcs break;
4834 1e37a5c2 2019-05-12 jcs case '<':
4835 1e37a5c2 2019-05-12 jcs case ',':
4836 777aae21 2022-07-20 thomas case 'K':
4837 4fc71f3b 2022-07-12 thomas up = 1;
4838 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4839 4fc71f3b 2022-07-12 thomas case '>':
4840 4fc71f3b 2022-07-12 thomas case '.':
4841 777aae21 2022-07-20 thomas case 'J':
4842 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4843 07b0611c 2022-06-23 thomas view->count = 0;
4844 48ae06ee 2018-10-18 stsp break;
4845 07b0611c 2022-06-23 thomas }
4846 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4847 6524637e 2019-02-21 stsp
4848 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4849 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4850 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4851 15a087fe 2019-02-21 stsp
4852 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4853 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4854 4fc71f3b 2022-07-12 thomas if (err)
4855 4fc71f3b 2022-07-12 thomas break;
4856 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4857 15a087fe 2019-02-21 stsp
4858 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4859 4fc71f3b 2022-07-12 thomas break;
4860 15a087fe 2019-02-21 stsp
4861 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4862 4fc71f3b 2022-07-12 thomas if (err)
4863 4fc71f3b 2022-07-12 thomas break;
4864 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4865 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4866 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4867 5e224a3e 2019-02-22 stsp
4868 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4869 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4870 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4871 4fc71f3b 2022-07-12 thomas
4872 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4873 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4874 4fc71f3b 2022-07-12 thomas if (err)
4875 4fc71f3b 2022-07-12 thomas break;
4876 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4877 5a8b5076 2020-12-05 stsp
4878 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4879 4fc71f3b 2022-07-12 thomas break;
4880 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4881 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4882 4fc71f3b 2022-07-12 thomas bs->selected_line);
4883 4fc71f3b 2022-07-12 thomas if (id == NULL)
4884 4fc71f3b 2022-07-12 thomas break;
4885 15a087fe 2019-02-21 stsp
4886 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4887 4fc71f3b 2022-07-12 thomas break;
4888 15a087fe 2019-02-21 stsp
4889 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4890 4fc71f3b 2022-07-12 thomas if (err)
4891 4fc71f3b 2022-07-12 thomas break;
4892 4fc71f3b 2022-07-12 thomas }
4893 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4894 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4895 aa61903a 2021-12-31 thomas s->matched_line = 0;
4896 05171be4 2022-06-23 thomas view->x = 0;
4897 1e37a5c2 2019-05-12 jcs
4898 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4899 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4900 1e37a5c2 2019-05-12 jcs break;
4901 1e37a5c2 2019-05-12 jcs default:
4902 07b0611c 2022-06-23 thomas view->count = 0;
4903 1e37a5c2 2019-05-12 jcs break;
4904 26ed57b2 2018-05-19 stsp }
4905 e5a0f69f 2018-08-18 stsp
4906 bcbd79e2 2018-08-19 stsp return err;
4907 26ed57b2 2018-05-19 stsp }
4908 26ed57b2 2018-05-19 stsp
4909 4ed7e80c 2018-05-20 stsp static const struct got_error *
4910 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4911 9f7d7167 2018-04-29 stsp {
4912 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4913 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4914 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4915 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4916 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4917 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4918 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4919 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4920 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4921 3dbaef42 2020-11-24 stsp const char *errstr;
4922 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4923 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4924 70ac5f84 2019-03-28 stsp
4925 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4926 26ed57b2 2018-05-19 stsp switch (ch) {
4927 64453f7e 2020-11-21 stsp case 'a':
4928 64453f7e 2020-11-21 stsp force_text_diff = 1;
4929 3dbaef42 2020-11-24 stsp break;
4930 3dbaef42 2020-11-24 stsp case 'C':
4931 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4932 3dbaef42 2020-11-24 stsp &errstr);
4933 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4934 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4935 8f666e67 2022-02-12 thomas errstr, errstr);
4936 64453f7e 2020-11-21 stsp break;
4937 09b5bff8 2020-02-23 naddy case 'r':
4938 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4939 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4940 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4941 09b5bff8 2020-02-23 naddy optarg);
4942 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4943 09b5bff8 2020-02-23 naddy break;
4944 3dbaef42 2020-11-24 stsp case 'w':
4945 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4946 3dbaef42 2020-11-24 stsp break;
4947 26ed57b2 2018-05-19 stsp default:
4948 17020d27 2019-03-07 stsp usage_diff();
4949 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4950 26ed57b2 2018-05-19 stsp }
4951 26ed57b2 2018-05-19 stsp }
4952 26ed57b2 2018-05-19 stsp
4953 26ed57b2 2018-05-19 stsp argc -= optind;
4954 26ed57b2 2018-05-19 stsp argv += optind;
4955 26ed57b2 2018-05-19 stsp
4956 26ed57b2 2018-05-19 stsp if (argc == 0) {
4957 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4958 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4959 15a94983 2018-12-23 stsp id_str1 = argv[0];
4960 15a94983 2018-12-23 stsp id_str2 = argv[1];
4961 26ed57b2 2018-05-19 stsp } else
4962 26ed57b2 2018-05-19 stsp usage_diff();
4963 eb6600df 2019-01-04 stsp
4964 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4965 7cd52833 2022-06-23 thomas if (error)
4966 7cd52833 2022-06-23 thomas goto done;
4967 7cd52833 2022-06-23 thomas
4968 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4969 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4970 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4971 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4972 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4973 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4974 c156c7a4 2020-12-18 stsp goto done;
4975 a273ac94 2020-02-23 naddy if (worktree)
4976 a273ac94 2020-02-23 naddy repo_path =
4977 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4978 a273ac94 2020-02-23 naddy else
4979 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4980 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4981 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4982 c156c7a4 2020-12-18 stsp goto done;
4983 c156c7a4 2020-12-18 stsp }
4984 a273ac94 2020-02-23 naddy }
4985 a273ac94 2020-02-23 naddy
4986 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4987 eb6600df 2019-01-04 stsp if (error)
4988 eb6600df 2019-01-04 stsp goto done;
4989 26ed57b2 2018-05-19 stsp
4990 a273ac94 2020-02-23 naddy init_curses();
4991 a273ac94 2020-02-23 naddy
4992 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4993 51a10b52 2020-12-26 stsp if (error)
4994 51a10b52 2020-12-26 stsp goto done;
4995 51a10b52 2020-12-26 stsp
4996 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4997 26ed57b2 2018-05-19 stsp if (error)
4998 26ed57b2 2018-05-19 stsp goto done;
4999 26ed57b2 2018-05-19 stsp
5000 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5001 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5002 26ed57b2 2018-05-19 stsp if (error)
5003 26ed57b2 2018-05-19 stsp goto done;
5004 26ed57b2 2018-05-19 stsp
5005 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5006 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5007 26ed57b2 2018-05-19 stsp if (error)
5008 26ed57b2 2018-05-19 stsp goto done;
5009 26ed57b2 2018-05-19 stsp
5010 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5011 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5012 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5013 ea5e7bb5 2018-08-01 stsp goto done;
5014 ea5e7bb5 2018-08-01 stsp }
5015 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5016 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5017 5dc9f4bc 2018-08-04 stsp if (error)
5018 5dc9f4bc 2018-08-04 stsp goto done;
5019 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5020 26ed57b2 2018-05-19 stsp done:
5021 3dbaef42 2020-11-24 stsp free(label1);
5022 3dbaef42 2020-11-24 stsp free(label2);
5023 c02c541e 2019-03-29 stsp free(repo_path);
5024 a273ac94 2020-02-23 naddy free(cwd);
5025 1d0f4054 2021-06-17 stsp if (repo) {
5026 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5027 1d0f4054 2021-06-17 stsp if (error == NULL)
5028 1d0f4054 2021-06-17 stsp error = close_err;
5029 1d0f4054 2021-06-17 stsp }
5030 a273ac94 2020-02-23 naddy if (worktree)
5031 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5032 7cd52833 2022-06-23 thomas if (pack_fds) {
5033 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5034 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5035 7cd52833 2022-06-23 thomas if (error == NULL)
5036 7cd52833 2022-06-23 thomas error = pack_err;
5037 7cd52833 2022-06-23 thomas }
5038 51a10b52 2020-12-26 stsp tog_free_refs();
5039 26ed57b2 2018-05-19 stsp return error;
5040 9f7d7167 2018-04-29 stsp }
5041 9f7d7167 2018-04-29 stsp
5042 4ed7e80c 2018-05-20 stsp __dead static void
5043 9f7d7167 2018-04-29 stsp usage_blame(void)
5044 9f7d7167 2018-04-29 stsp {
5045 80ddbec8 2018-04-29 stsp endwin();
5046 91198554 2022-06-23 thomas fprintf(stderr,
5047 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5048 9f7d7167 2018-04-29 stsp getprogname());
5049 9f7d7167 2018-04-29 stsp exit(1);
5050 9f7d7167 2018-04-29 stsp }
5051 84451b3e 2018-07-10 stsp
5052 84451b3e 2018-07-10 stsp struct tog_blame_line {
5053 84451b3e 2018-07-10 stsp int annotated;
5054 84451b3e 2018-07-10 stsp struct got_object_id *id;
5055 84451b3e 2018-07-10 stsp };
5056 9f7d7167 2018-04-29 stsp
5057 4ed7e80c 2018-05-20 stsp static const struct got_error *
5058 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5059 84451b3e 2018-07-10 stsp {
5060 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5061 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5062 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5063 84451b3e 2018-07-10 stsp const struct got_error *err;
5064 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5065 826082fe 2020-12-10 stsp char *line = NULL;
5066 826082fe 2020-12-10 stsp size_t linesize = 0;
5067 826082fe 2020-12-10 stsp ssize_t linelen;
5068 84451b3e 2018-07-10 stsp wchar_t *wline;
5069 27a741e5 2019-09-11 stsp int width;
5070 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5071 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5072 ab089a2a 2018-07-12 stsp char *id_str;
5073 11b20872 2019-11-08 stsp struct tog_color *tc;
5074 ab089a2a 2018-07-12 stsp
5075 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5076 ab089a2a 2018-07-12 stsp if (err)
5077 ab089a2a 2018-07-12 stsp return err;
5078 84451b3e 2018-07-10 stsp
5079 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5080 f7d12f7e 2018-08-01 stsp werase(view->window);
5081 84451b3e 2018-07-10 stsp
5082 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5083 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5084 ab089a2a 2018-07-12 stsp free(id_str);
5085 ab089a2a 2018-07-12 stsp return err;
5086 ab089a2a 2018-07-12 stsp }
5087 ab089a2a 2018-07-12 stsp
5088 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5089 ab089a2a 2018-07-12 stsp free(line);
5090 2550e4c3 2018-07-13 stsp line = NULL;
5091 1cae65b4 2019-09-22 stsp if (err)
5092 1cae65b4 2019-09-22 stsp return err;
5093 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5094 a3404814 2018-09-02 stsp wstandout(view->window);
5095 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5096 11b20872 2019-11-08 stsp if (tc)
5097 11b20872 2019-11-08 stsp wattr_on(view->window,
5098 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5099 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5100 11b20872 2019-11-08 stsp if (tc)
5101 11b20872 2019-11-08 stsp wattr_off(view->window,
5102 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5103 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5104 a3404814 2018-09-02 stsp wstandend(view->window);
5105 2550e4c3 2018-07-13 stsp free(wline);
5106 2550e4c3 2018-07-13 stsp wline = NULL;
5107 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5108 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5109 ab089a2a 2018-07-12 stsp
5110 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5111 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5112 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5113 ab089a2a 2018-07-12 stsp free(id_str);
5114 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5115 ab089a2a 2018-07-12 stsp }
5116 ab089a2a 2018-07-12 stsp free(id_str);
5117 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5118 3f60a8ef 2018-07-10 stsp free(line);
5119 2550e4c3 2018-07-13 stsp line = NULL;
5120 3f60a8ef 2018-07-10 stsp if (err)
5121 3f60a8ef 2018-07-10 stsp return err;
5122 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5123 2550e4c3 2018-07-13 stsp free(wline);
5124 2550e4c3 2018-07-13 stsp wline = NULL;
5125 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5126 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5127 3f60a8ef 2018-07-10 stsp
5128 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5129 05171be4 2022-06-23 thomas view->maxx = 0;
5130 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5131 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5132 826082fe 2020-12-10 stsp if (linelen == -1) {
5133 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5134 826082fe 2020-12-10 stsp s->eof = 1;
5135 826082fe 2020-12-10 stsp break;
5136 826082fe 2020-12-10 stsp }
5137 84451b3e 2018-07-10 stsp free(line);
5138 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5139 84451b3e 2018-07-10 stsp }
5140 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5141 826082fe 2020-12-10 stsp continue;
5142 cbea3800 2022-06-23 thomas
5143 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5144 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5145 cbea3800 2022-06-23 thomas if (err) {
5146 cbea3800 2022-06-23 thomas free(line);
5147 cbea3800 2022-06-23 thomas return err;
5148 cbea3800 2022-06-23 thomas }
5149 cbea3800 2022-06-23 thomas free(wline);
5150 cbea3800 2022-06-23 thomas wline = NULL;
5151 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5152 84451b3e 2018-07-10 stsp
5153 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5154 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5155 b700b5d6 2018-07-10 stsp
5156 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5157 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5158 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5159 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5160 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5161 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5162 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5163 8d0fe45a 2019-08-12 stsp char *id_str;
5164 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5165 91198554 2022-06-23 thomas blame_line->id);
5166 8d0fe45a 2019-08-12 stsp if (err) {
5167 8d0fe45a 2019-08-12 stsp free(line);
5168 8d0fe45a 2019-08-12 stsp return err;
5169 8d0fe45a 2019-08-12 stsp }
5170 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5171 11b20872 2019-11-08 stsp if (tc)
5172 11b20872 2019-11-08 stsp wattr_on(view->window,
5173 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5174 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5175 11b20872 2019-11-08 stsp if (tc)
5176 11b20872 2019-11-08 stsp wattr_off(view->window,
5177 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5178 8d0fe45a 2019-08-12 stsp free(id_str);
5179 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5180 8d0fe45a 2019-08-12 stsp } else {
5181 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5182 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5183 84451b3e 2018-07-10 stsp }
5184 ee41ec32 2018-07-10 stsp } else {
5185 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5186 ee41ec32 2018-07-10 stsp prev_id = NULL;
5187 ee41ec32 2018-07-10 stsp }
5188 84451b3e 2018-07-10 stsp
5189 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5190 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5191 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5192 27a741e5 2019-09-11 stsp
5193 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5194 41605754 2020-11-12 stsp width = 9;
5195 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5196 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5197 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5198 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5199 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5200 41605754 2020-11-12 stsp if (err) {
5201 41605754 2020-11-12 stsp free(line);
5202 41605754 2020-11-12 stsp return err;
5203 41605754 2020-11-12 stsp }
5204 41605754 2020-11-12 stsp width += 9;
5205 41605754 2020-11-12 stsp } else {
5206 923086fd 2022-06-23 thomas int skip;
5207 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5208 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5209 923086fd 2022-06-23 thomas if (err) {
5210 923086fd 2022-06-23 thomas free(line);
5211 923086fd 2022-06-23 thomas return err;
5212 05171be4 2022-06-23 thomas }
5213 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5214 02bce7e2 2022-06-23 thomas width += 9;
5215 41605754 2020-11-12 stsp free(wline);
5216 41605754 2020-11-12 stsp wline = NULL;
5217 41605754 2020-11-12 stsp }
5218 41605754 2020-11-12 stsp
5219 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5220 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5221 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5222 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5223 84451b3e 2018-07-10 stsp }
5224 826082fe 2020-12-10 stsp free(line);
5225 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5226 84451b3e 2018-07-10 stsp
5227 a5d43cac 2022-07-01 thomas view_border(view);
5228 84451b3e 2018-07-10 stsp
5229 84451b3e 2018-07-10 stsp return NULL;
5230 84451b3e 2018-07-10 stsp }
5231 84451b3e 2018-07-10 stsp
5232 84451b3e 2018-07-10 stsp static const struct got_error *
5233 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5234 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5235 84451b3e 2018-07-10 stsp {
5236 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5237 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5238 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5239 1a76625f 2018-10-22 stsp int errcode;
5240 84451b3e 2018-07-10 stsp
5241 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5242 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5243 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5244 84451b3e 2018-07-10 stsp
5245 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5246 1a76625f 2018-10-22 stsp if (errcode)
5247 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5248 84451b3e 2018-07-10 stsp
5249 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5250 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5251 d68a0a7d 2018-07-10 stsp goto done;
5252 d68a0a7d 2018-07-10 stsp }
5253 d68a0a7d 2018-07-10 stsp
5254 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5255 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5256 d68a0a7d 2018-07-10 stsp
5257 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5258 d68a0a7d 2018-07-10 stsp if (line->annotated)
5259 d68a0a7d 2018-07-10 stsp goto done;
5260 d68a0a7d 2018-07-10 stsp
5261 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5262 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5263 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5264 84451b3e 2018-07-10 stsp goto done;
5265 84451b3e 2018-07-10 stsp }
5266 84451b3e 2018-07-10 stsp line->annotated = 1;
5267 84451b3e 2018-07-10 stsp done:
5268 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5269 1a76625f 2018-10-22 stsp if (errcode)
5270 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5271 84451b3e 2018-07-10 stsp return err;
5272 84451b3e 2018-07-10 stsp }
5273 84451b3e 2018-07-10 stsp
5274 84451b3e 2018-07-10 stsp static void *
5275 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5276 84451b3e 2018-07-10 stsp {
5277 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5278 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5279 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5280 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5281 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5282 00a8878e 2022-07-01 thomas
5283 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5284 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5285 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5286 9117a7b7 2022-07-01 thomas
5287 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5288 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5289 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5290 9117a7b7 2022-07-01 thomas goto done;
5291 9117a7b7 2022-07-01 thomas }
5292 18430de3 2018-07-10 stsp
5293 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5294 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5295 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5296 9117a7b7 2022-07-01 thomas goto done;
5297 9117a7b7 2022-07-01 thomas }
5298 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5299 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5300 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5301 9117a7b7 2022-07-01 thomas goto done;
5302 9117a7b7 2022-07-01 thomas }
5303 9117a7b7 2022-07-01 thomas
5304 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5305 61266923 2020-01-14 stsp if (err)
5306 9117a7b7 2022-07-01 thomas goto done;
5307 61266923 2020-01-14 stsp
5308 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5309 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5310 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5311 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5312 fc06ba56 2019-08-22 stsp err = NULL;
5313 18430de3 2018-07-10 stsp
5314 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5315 9117a7b7 2022-07-01 thomas if (errcode) {
5316 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5317 9117a7b7 2022-07-01 thomas goto done;
5318 9117a7b7 2022-07-01 thomas }
5319 18430de3 2018-07-10 stsp
5320 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5321 1d0f4054 2021-06-17 stsp if (err == NULL)
5322 1d0f4054 2021-06-17 stsp err = close_err;
5323 c9beca56 2018-07-22 stsp ta->repo = NULL;
5324 c9beca56 2018-07-22 stsp *ta->complete = 1;
5325 18430de3 2018-07-10 stsp
5326 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5327 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5328 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5329 18430de3 2018-07-10 stsp
5330 9117a7b7 2022-07-01 thomas done:
5331 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5332 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5333 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5334 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5335 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5336 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5337 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5338 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5339 00a8878e 2022-07-01 thomas
5340 18430de3 2018-07-10 stsp return (void *)err;
5341 84451b3e 2018-07-10 stsp }
5342 84451b3e 2018-07-10 stsp
5343 245d91c1 2018-07-12 stsp static struct got_object_id *
5344 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5345 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5346 245d91c1 2018-07-12 stsp {
5347 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5348 8d0fe45a 2019-08-12 stsp
5349 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5350 8d0fe45a 2019-08-12 stsp return NULL;
5351 b880a918 2018-07-10 stsp
5352 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5353 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5354 4fc71f3b 2022-07-12 thomas return NULL;
5355 4fc71f3b 2022-07-12 thomas
5356 4fc71f3b 2022-07-12 thomas return line->id;
5357 4fc71f3b 2022-07-12 thomas }
5358 4fc71f3b 2022-07-12 thomas
5359 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5360 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5361 4fc71f3b 2022-07-12 thomas int lineno)
5362 4fc71f3b 2022-07-12 thomas {
5363 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5364 4fc71f3b 2022-07-12 thomas
5365 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5366 4fc71f3b 2022-07-12 thomas return NULL;
5367 4fc71f3b 2022-07-12 thomas
5368 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5369 245d91c1 2018-07-12 stsp if (!line->annotated)
5370 245d91c1 2018-07-12 stsp return NULL;
5371 245d91c1 2018-07-12 stsp
5372 245d91c1 2018-07-12 stsp return line->id;
5373 b880a918 2018-07-10 stsp }
5374 245d91c1 2018-07-12 stsp
5375 b880a918 2018-07-10 stsp static const struct got_error *
5376 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5377 a70480e0 2018-06-23 stsp {
5378 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5379 245d91c1 2018-07-12 stsp int i;
5380 245d91c1 2018-07-12 stsp
5381 245d91c1 2018-07-12 stsp if (blame->thread) {
5382 1a76625f 2018-10-22 stsp int errcode;
5383 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5384 1a76625f 2018-10-22 stsp if (errcode)
5385 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5386 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5387 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5388 1a76625f 2018-10-22 stsp if (errcode)
5389 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5390 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5391 1a76625f 2018-10-22 stsp if (errcode)
5392 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5393 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5394 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5395 245d91c1 2018-07-12 stsp err = NULL;
5396 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5397 245d91c1 2018-07-12 stsp }
5398 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5399 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5400 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5401 1d0f4054 2021-06-17 stsp if (err == NULL)
5402 1d0f4054 2021-06-17 stsp err = close_err;
5403 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5404 245d91c1 2018-07-12 stsp }
5405 245d91c1 2018-07-12 stsp if (blame->f) {
5406 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5407 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5408 245d91c1 2018-07-12 stsp blame->f = NULL;
5409 245d91c1 2018-07-12 stsp }
5410 57670559 2018-12-23 stsp if (blame->lines) {
5411 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5412 57670559 2018-12-23 stsp free(blame->lines[i].id);
5413 57670559 2018-12-23 stsp free(blame->lines);
5414 57670559 2018-12-23 stsp blame->lines = NULL;
5415 57670559 2018-12-23 stsp }
5416 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5417 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5418 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5419 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5420 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5421 7cd52833 2022-06-23 thomas if (err == NULL)
5422 7cd52833 2022-06-23 thomas err = pack_err;
5423 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5424 7cd52833 2022-06-23 thomas }
5425 245d91c1 2018-07-12 stsp return err;
5426 245d91c1 2018-07-12 stsp }
5427 245d91c1 2018-07-12 stsp
5428 245d91c1 2018-07-12 stsp static const struct got_error *
5429 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5430 fc06ba56 2019-08-22 stsp {
5431 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5432 fc06ba56 2019-08-22 stsp int *done = arg;
5433 fc06ba56 2019-08-22 stsp int errcode;
5434 fc06ba56 2019-08-22 stsp
5435 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5436 fc06ba56 2019-08-22 stsp if (errcode)
5437 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5438 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5439 fc06ba56 2019-08-22 stsp
5440 fc06ba56 2019-08-22 stsp if (*done)
5441 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5442 fc06ba56 2019-08-22 stsp
5443 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5444 fc06ba56 2019-08-22 stsp if (errcode)
5445 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5446 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5447 fc06ba56 2019-08-22 stsp
5448 fc06ba56 2019-08-22 stsp return err;
5449 fc06ba56 2019-08-22 stsp }
5450 fc06ba56 2019-08-22 stsp
5451 fc06ba56 2019-08-22 stsp static const struct got_error *
5452 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5453 245d91c1 2018-07-12 stsp {
5454 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5455 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5456 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5457 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5458 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5459 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5460 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5461 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5462 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5463 a70480e0 2018-06-23 stsp
5464 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5465 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5466 27d434c2 2018-09-15 stsp if (err)
5467 15a94983 2018-12-23 stsp return err;
5468 f4ae6ddb 2022-07-01 thomas
5469 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5470 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5471 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5472 f4ae6ddb 2022-07-01 thomas goto done;
5473 f4ae6ddb 2022-07-01 thomas }
5474 945f9229 2022-04-16 thomas
5475 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5476 945f9229 2022-04-16 thomas if (err)
5477 945f9229 2022-04-16 thomas goto done;
5478 27d434c2 2018-09-15 stsp
5479 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5480 84451b3e 2018-07-10 stsp if (err)
5481 84451b3e 2018-07-10 stsp goto done;
5482 27d434c2 2018-09-15 stsp
5483 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5484 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5485 84451b3e 2018-07-10 stsp goto done;
5486 84451b3e 2018-07-10 stsp }
5487 a70480e0 2018-06-23 stsp
5488 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5489 a70480e0 2018-06-23 stsp if (err)
5490 a70480e0 2018-06-23 stsp goto done;
5491 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5492 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5493 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5494 84451b3e 2018-07-10 stsp goto done;
5495 84451b3e 2018-07-10 stsp }
5496 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5497 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5498 1fddf795 2021-01-20 stsp if (err)
5499 1fddf795 2021-01-20 stsp goto done;
5500 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5501 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5502 84451b3e 2018-07-10 stsp goto done;
5503 1fddf795 2021-01-20 stsp }
5504 b02560ec 2019-08-19 stsp
5505 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5506 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5507 b02560ec 2019-08-19 stsp blame->nlines--;
5508 a70480e0 2018-06-23 stsp
5509 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5510 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5511 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5512 84451b3e 2018-07-10 stsp goto done;
5513 84451b3e 2018-07-10 stsp }
5514 a70480e0 2018-06-23 stsp
5515 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5516 bd24772e 2018-07-11 stsp if (err)
5517 bd24772e 2018-07-11 stsp goto done;
5518 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5519 7cd52833 2022-06-23 thomas pack_fds);
5520 7cd52833 2022-06-23 thomas if (err)
5521 7cd52833 2022-06-23 thomas goto done;
5522 bd24772e 2018-07-11 stsp
5523 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5524 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5525 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5526 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5527 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5528 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5529 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5530 245d91c1 2018-07-12 stsp goto done;
5531 245d91c1 2018-07-12 stsp }
5532 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5533 245d91c1 2018-07-12 stsp
5534 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5535 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5536 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5537 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5538 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5539 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5540 a5388363 2020-12-01 naddy s->blame_complete = 0;
5541 f5a09613 2020-12-13 naddy
5542 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5543 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5544 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5545 f5a09613 2020-12-13 naddy s->selected_line = 1;
5546 f5a09613 2020-12-13 naddy }
5547 aa61903a 2021-12-31 thomas s->matched_line = 0;
5548 245d91c1 2018-07-12 stsp
5549 245d91c1 2018-07-12 stsp done:
5550 945f9229 2022-04-16 thomas if (commit)
5551 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5552 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5553 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5554 245d91c1 2018-07-12 stsp if (blob)
5555 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5556 27d434c2 2018-09-15 stsp free(obj_id);
5557 245d91c1 2018-07-12 stsp if (err)
5558 245d91c1 2018-07-12 stsp stop_blame(blame);
5559 245d91c1 2018-07-12 stsp return err;
5560 245d91c1 2018-07-12 stsp }
5561 245d91c1 2018-07-12 stsp
5562 245d91c1 2018-07-12 stsp static const struct got_error *
5563 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5564 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5565 245d91c1 2018-07-12 stsp {
5566 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5567 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5568 dbc6a6b6 2018-07-12 stsp
5569 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5570 245d91c1 2018-07-12 stsp
5571 c4843652 2019-08-12 stsp s->path = strdup(path);
5572 c4843652 2019-08-12 stsp if (s->path == NULL)
5573 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5574 c4843652 2019-08-12 stsp
5575 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5576 c4843652 2019-08-12 stsp if (err) {
5577 c4843652 2019-08-12 stsp free(s->path);
5578 7cbe629d 2018-08-04 stsp return err;
5579 c4843652 2019-08-12 stsp }
5580 245d91c1 2018-07-12 stsp
5581 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5582 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5583 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5584 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5585 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5586 fb2756b9 2018-08-04 stsp s->repo = repo;
5587 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5588 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5589 7cbe629d 2018-08-04 stsp
5590 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5591 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5592 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5593 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5594 11b20872 2019-11-08 stsp if (err)
5595 11b20872 2019-11-08 stsp return err;
5596 11b20872 2019-11-08 stsp }
5597 11b20872 2019-11-08 stsp
5598 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5599 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5600 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5601 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5602 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5603 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5604 e5a0f69f 2018-08-18 stsp
5605 a5388363 2020-12-01 naddy return run_blame(view);
5606 7cbe629d 2018-08-04 stsp }
5607 7cbe629d 2018-08-04 stsp
5608 e5a0f69f 2018-08-18 stsp static const struct got_error *
5609 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5610 7cbe629d 2018-08-04 stsp {
5611 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5612 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5613 7cbe629d 2018-08-04 stsp
5614 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5615 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5616 e5a0f69f 2018-08-18 stsp
5617 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5618 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5619 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5620 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5621 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5622 7cbe629d 2018-08-04 stsp }
5623 e5a0f69f 2018-08-18 stsp
5624 e5a0f69f 2018-08-18 stsp free(s->path);
5625 11b20872 2019-11-08 stsp free_colors(&s->colors);
5626 e5a0f69f 2018-08-18 stsp return err;
5627 7cbe629d 2018-08-04 stsp }
5628 7cbe629d 2018-08-04 stsp
5629 7cbe629d 2018-08-04 stsp static const struct got_error *
5630 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5631 6c4c42e0 2019-06-24 stsp {
5632 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5633 6c4c42e0 2019-06-24 stsp
5634 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5635 6c4c42e0 2019-06-24 stsp return NULL;
5636 6c4c42e0 2019-06-24 stsp }
5637 6c4c42e0 2019-06-24 stsp
5638 6c4c42e0 2019-06-24 stsp static const struct got_error *
5639 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5640 6c4c42e0 2019-06-24 stsp {
5641 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5642 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5643 6c4c42e0 2019-06-24 stsp int lineno;
5644 78eecffc 2022-06-23 thomas char *line = NULL;
5645 826082fe 2020-12-10 stsp size_t linesize = 0;
5646 826082fe 2020-12-10 stsp ssize_t linelen;
5647 6c4c42e0 2019-06-24 stsp
5648 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5649 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5650 6c4c42e0 2019-06-24 stsp return NULL;
5651 6c4c42e0 2019-06-24 stsp }
5652 6c4c42e0 2019-06-24 stsp
5653 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5654 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5655 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5656 6c4c42e0 2019-06-24 stsp else
5657 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5658 4b3f9dac 2021-12-17 thomas } else
5659 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5660 6c4c42e0 2019-06-24 stsp
5661 6c4c42e0 2019-06-24 stsp while (1) {
5662 6c4c42e0 2019-06-24 stsp off_t offset;
5663 6c4c42e0 2019-06-24 stsp
5664 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5665 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5666 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5667 6c4c42e0 2019-06-24 stsp break;
5668 6c4c42e0 2019-06-24 stsp }
5669 2246482e 2019-06-25 stsp
5670 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5671 6c4c42e0 2019-06-24 stsp lineno = 1;
5672 6c4c42e0 2019-06-24 stsp else
5673 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5674 6c4c42e0 2019-06-24 stsp }
5675 6c4c42e0 2019-06-24 stsp
5676 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5677 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5678 6c4c42e0 2019-06-24 stsp free(line);
5679 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5680 6c4c42e0 2019-06-24 stsp }
5681 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5682 78eecffc 2022-06-23 thomas if (linelen != -1) {
5683 78eecffc 2022-06-23 thomas char *exstr;
5684 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5685 78eecffc 2022-06-23 thomas if (err)
5686 78eecffc 2022-06-23 thomas break;
5687 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5688 78eecffc 2022-06-23 thomas &view->regmatch)) {
5689 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5690 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5691 78eecffc 2022-06-23 thomas free(exstr);
5692 78eecffc 2022-06-23 thomas break;
5693 78eecffc 2022-06-23 thomas }
5694 78eecffc 2022-06-23 thomas free(exstr);
5695 6c4c42e0 2019-06-24 stsp }
5696 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5697 6c4c42e0 2019-06-24 stsp lineno++;
5698 6c4c42e0 2019-06-24 stsp else
5699 6c4c42e0 2019-06-24 stsp lineno--;
5700 6c4c42e0 2019-06-24 stsp }
5701 826082fe 2020-12-10 stsp free(line);
5702 6c4c42e0 2019-06-24 stsp
5703 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5704 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5705 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5706 6c4c42e0 2019-06-24 stsp }
5707 6c4c42e0 2019-06-24 stsp
5708 05171be4 2022-06-23 thomas return err;
5709 6c4c42e0 2019-06-24 stsp }
5710 6c4c42e0 2019-06-24 stsp
5711 6c4c42e0 2019-06-24 stsp static const struct got_error *
5712 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5713 7cbe629d 2018-08-04 stsp {
5714 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5715 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5716 2b380cc8 2018-10-24 stsp int errcode;
5717 2b380cc8 2018-10-24 stsp
5718 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5719 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5720 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5721 2b380cc8 2018-10-24 stsp if (errcode)
5722 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5723 51fe7530 2019-08-19 stsp
5724 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5725 2b380cc8 2018-10-24 stsp }
5726 e5a0f69f 2018-08-18 stsp
5727 51fe7530 2019-08-19 stsp if (s->blame_complete)
5728 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5729 51fe7530 2019-08-19 stsp
5730 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5731 e5a0f69f 2018-08-18 stsp
5732 a5d43cac 2022-07-01 thomas view_border(view);
5733 e5a0f69f 2018-08-18 stsp return err;
5734 e5a0f69f 2018-08-18 stsp }
5735 e5a0f69f 2018-08-18 stsp
5736 e5a0f69f 2018-08-18 stsp static const struct got_error *
5737 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5738 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5739 eaeaa612 2022-07-20 thomas {
5740 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5741 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5742 eaeaa612 2022-07-20 thomas
5743 eaeaa612 2022-07-20 thomas *new_view = NULL;
5744 eaeaa612 2022-07-20 thomas
5745 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5746 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5747 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5748 eaeaa612 2022-07-20 thomas
5749 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5750 eaeaa612 2022-07-20 thomas if (err)
5751 eaeaa612 2022-07-20 thomas view_close(log_view);
5752 eaeaa612 2022-07-20 thomas else
5753 eaeaa612 2022-07-20 thomas *new_view = log_view;
5754 eaeaa612 2022-07-20 thomas
5755 eaeaa612 2022-07-20 thomas return err;
5756 eaeaa612 2022-07-20 thomas }
5757 eaeaa612 2022-07-20 thomas
5758 eaeaa612 2022-07-20 thomas static const struct got_error *
5759 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5760 e5a0f69f 2018-08-18 stsp {
5761 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5762 eaeaa612 2022-07-20 thomas struct tog_view *diff_view, *log_view;
5763 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5764 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5765 a5d43cac 2022-07-01 thomas
5766 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5767 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5768 a5d43cac 2022-07-01 thomas --eos; /* border */
5769 7cbe629d 2018-08-04 stsp
5770 e5a0f69f 2018-08-18 stsp switch (ch) {
5771 05171be4 2022-06-23 thomas case '0':
5772 05171be4 2022-06-23 thomas view->x = 0;
5773 05171be4 2022-06-23 thomas break;
5774 05171be4 2022-06-23 thomas case '$':
5775 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5776 07b0611c 2022-06-23 thomas view->count = 0;
5777 05171be4 2022-06-23 thomas break;
5778 05171be4 2022-06-23 thomas case KEY_RIGHT:
5779 05171be4 2022-06-23 thomas case 'l':
5780 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5781 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5782 07b0611c 2022-06-23 thomas else
5783 07b0611c 2022-06-23 thomas view->count = 0;
5784 05171be4 2022-06-23 thomas break;
5785 05171be4 2022-06-23 thomas case KEY_LEFT:
5786 05171be4 2022-06-23 thomas case 'h':
5787 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5788 07b0611c 2022-06-23 thomas if (view->x <= 0)
5789 07b0611c 2022-06-23 thomas view->count = 0;
5790 05171be4 2022-06-23 thomas break;
5791 1e37a5c2 2019-05-12 jcs case 'q':
5792 1e37a5c2 2019-05-12 jcs s->done = 1;
5793 4deef56f 2021-09-02 naddy break;
5794 4deef56f 2021-09-02 naddy case 'g':
5795 4deef56f 2021-09-02 naddy case KEY_HOME:
5796 4deef56f 2021-09-02 naddy s->selected_line = 1;
5797 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5798 07b0611c 2022-06-23 thomas view->count = 0;
5799 4deef56f 2021-09-02 naddy break;
5800 4deef56f 2021-09-02 naddy case 'G':
5801 4deef56f 2021-09-02 naddy case KEY_END:
5802 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5803 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5804 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5805 4deef56f 2021-09-02 naddy } else {
5806 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5807 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5808 4deef56f 2021-09-02 naddy }
5809 07b0611c 2022-06-23 thomas view->count = 0;
5810 1e37a5c2 2019-05-12 jcs break;
5811 1e37a5c2 2019-05-12 jcs case 'k':
5812 1e37a5c2 2019-05-12 jcs case KEY_UP:
5813 f7140bf5 2021-10-17 thomas case CTRL('p'):
5814 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5815 1e37a5c2 2019-05-12 jcs s->selected_line--;
5816 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5817 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5818 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5819 07b0611c 2022-06-23 thomas else
5820 07b0611c 2022-06-23 thomas view->count = 0;
5821 1e37a5c2 2019-05-12 jcs break;
5822 70f17a53 2022-06-13 thomas case CTRL('u'):
5823 23427b14 2022-06-23 thomas case 'u':
5824 70f17a53 2022-06-13 thomas nscroll /= 2;
5825 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5826 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5827 ea025d1d 2020-02-22 naddy case CTRL('b'):
5828 1c5e5faa 2022-06-23 thomas case 'b':
5829 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5830 07b0611c 2022-06-23 thomas if (view->count > 1)
5831 07b0611c 2022-06-23 thomas nscroll += nscroll;
5832 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5833 07b0611c 2022-06-23 thomas view->count = 0;
5834 e5a0f69f 2018-08-18 stsp break;
5835 1e37a5c2 2019-05-12 jcs }
5836 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5837 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5838 1e37a5c2 2019-05-12 jcs else
5839 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5840 1e37a5c2 2019-05-12 jcs break;
5841 1e37a5c2 2019-05-12 jcs case 'j':
5842 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5843 f7140bf5 2021-10-17 thomas case CTRL('n'):
5844 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5845 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5846 1e37a5c2 2019-05-12 jcs s->selected_line++;
5847 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5848 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5849 07b0611c 2022-06-23 thomas else
5850 07b0611c 2022-06-23 thomas view->count = 0;
5851 1e37a5c2 2019-05-12 jcs break;
5852 1c5e5faa 2022-06-23 thomas case 'c':
5853 1e37a5c2 2019-05-12 jcs case 'p': {
5854 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5855 07b0611c 2022-06-23 thomas
5856 07b0611c 2022-06-23 thomas view->count = 0;
5857 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5858 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5859 1e37a5c2 2019-05-12 jcs if (id == NULL)
5860 e5a0f69f 2018-08-18 stsp break;
5861 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5862 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5863 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5864 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5865 1e37a5c2 2019-05-12 jcs int obj_type;
5866 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5867 1e37a5c2 2019-05-12 jcs s->repo, id);
5868 e5a0f69f 2018-08-18 stsp if (err)
5869 e5a0f69f 2018-08-18 stsp break;
5870 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5871 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5872 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5873 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5874 e5a0f69f 2018-08-18 stsp break;
5875 e5a0f69f 2018-08-18 stsp }
5876 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5877 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5878 ec242592 2022-04-22 thomas s->repo, &pid->id);
5879 945f9229 2022-04-16 thomas if (err)
5880 945f9229 2022-04-16 thomas break;
5881 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5882 945f9229 2022-04-16 thomas pcommit, s->path);
5883 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5884 e5a0f69f 2018-08-18 stsp if (err) {
5885 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5886 1e37a5c2 2019-05-12 jcs err = NULL;
5887 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5888 e5a0f69f 2018-08-18 stsp break;
5889 e5a0f69f 2018-08-18 stsp }
5890 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5891 1e37a5c2 2019-05-12 jcs blob_id);
5892 1e37a5c2 2019-05-12 jcs free(blob_id);
5893 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5894 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5895 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5896 e5a0f69f 2018-08-18 stsp break;
5897 1e37a5c2 2019-05-12 jcs }
5898 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5899 ec242592 2022-04-22 thomas &pid->id);
5900 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5901 1e37a5c2 2019-05-12 jcs } else {
5902 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5903 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5904 1e37a5c2 2019-05-12 jcs break;
5905 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5906 1e37a5c2 2019-05-12 jcs id);
5907 1e37a5c2 2019-05-12 jcs }
5908 1e37a5c2 2019-05-12 jcs if (err)
5909 e5a0f69f 2018-08-18 stsp break;
5910 1e37a5c2 2019-05-12 jcs s->done = 1;
5911 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5912 1e37a5c2 2019-05-12 jcs s->done = 0;
5913 1e37a5c2 2019-05-12 jcs if (thread_err)
5914 1e37a5c2 2019-05-12 jcs break;
5915 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5916 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5917 a5388363 2020-12-01 naddy err = run_blame(view);
5918 1e37a5c2 2019-05-12 jcs if (err)
5919 1e37a5c2 2019-05-12 jcs break;
5920 1e37a5c2 2019-05-12 jcs break;
5921 1e37a5c2 2019-05-12 jcs }
5922 1c5e5faa 2022-06-23 thomas case 'C': {
5923 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5924 07b0611c 2022-06-23 thomas
5925 07b0611c 2022-06-23 thomas view->count = 0;
5926 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5927 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5928 1e37a5c2 2019-05-12 jcs break;
5929 1e37a5c2 2019-05-12 jcs s->done = 1;
5930 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5931 1e37a5c2 2019-05-12 jcs s->done = 0;
5932 1e37a5c2 2019-05-12 jcs if (thread_err)
5933 1e37a5c2 2019-05-12 jcs break;
5934 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5935 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5936 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5937 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5938 a5388363 2020-12-01 naddy err = run_blame(view);
5939 1e37a5c2 2019-05-12 jcs if (err)
5940 1e37a5c2 2019-05-12 jcs break;
5941 1e37a5c2 2019-05-12 jcs break;
5942 1e37a5c2 2019-05-12 jcs }
5943 eaeaa612 2022-07-20 thomas case 'L': {
5944 eaeaa612 2022-07-20 thomas struct got_object_id *id = NULL;
5945 eaeaa612 2022-07-20 thomas
5946 eaeaa612 2022-07-20 thomas view->count = 0;
5947 eaeaa612 2022-07-20 thomas id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5948 eaeaa612 2022-07-20 thomas s->first_displayed_line, s->selected_line);
5949 eaeaa612 2022-07-20 thomas if (id == NULL)
5950 eaeaa612 2022-07-20 thomas break;
5951 eaeaa612 2022-07-20 thomas
5952 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view))
5953 eaeaa612 2022-07-20 thomas view_get_split(view, &begin_y, &begin_x);
5954 eaeaa612 2022-07-20 thomas err = log_annotated_line(&log_view, begin_y, begin_x,
5955 eaeaa612 2022-07-20 thomas s->repo, id);
5956 eaeaa612 2022-07-20 thomas if (err)
5957 eaeaa612 2022-07-20 thomas break;
5958 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view) &&
5959 eaeaa612 2022-07-20 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5960 eaeaa612 2022-07-20 thomas err = view_init_hsplit(view, begin_y);
5961 eaeaa612 2022-07-20 thomas if (err)
5962 eaeaa612 2022-07-20 thomas break;
5963 eaeaa612 2022-07-20 thomas }
5964 eaeaa612 2022-07-20 thomas
5965 eaeaa612 2022-07-20 thomas view->focussed = 0;
5966 eaeaa612 2022-07-20 thomas log_view->focussed = 1;
5967 eaeaa612 2022-07-20 thomas log_view->mode = view->mode;
5968 eaeaa612 2022-07-20 thomas log_view->nlines = view->lines - begin_y;
5969 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view)) {
5970 eaeaa612 2022-07-20 thomas view_transfer_size(log_view, view);
5971 eaeaa612 2022-07-20 thomas err = view_close_child(view);
5972 eaeaa612 2022-07-20 thomas if (err)
5973 eaeaa612 2022-07-20 thomas return err;
5974 eaeaa612 2022-07-20 thomas err = view_set_child(view, log_view);
5975 eaeaa612 2022-07-20 thomas if (err)
5976 eaeaa612 2022-07-20 thomas return err;
5977 eaeaa612 2022-07-20 thomas view->focus_child = 1;
5978 eaeaa612 2022-07-20 thomas } else
5979 eaeaa612 2022-07-20 thomas *new_view = log_view;
5980 eaeaa612 2022-07-20 thomas break;
5981 eaeaa612 2022-07-20 thomas }
5982 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5983 1e37a5c2 2019-05-12 jcs case '\r': {
5984 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5985 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5986 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5987 07b0611c 2022-06-23 thomas
5988 07b0611c 2022-06-23 thomas view->count = 0;
5989 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5990 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5991 1e37a5c2 2019-05-12 jcs if (id == NULL)
5992 1e37a5c2 2019-05-12 jcs break;
5993 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5994 1e37a5c2 2019-05-12 jcs if (err)
5995 1e37a5c2 2019-05-12 jcs break;
5996 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5997 4fc71f3b 2022-07-12 thomas if (*new_view) {
5998 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5999 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6000 4fc71f3b 2022-07-12 thomas if (err)
6001 4fc71f3b 2022-07-12 thomas break;
6002 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6003 4fc71f3b 2022-07-12 thomas } else {
6004 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6005 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6006 a5d43cac 2022-07-01 thomas
6007 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6008 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6009 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6010 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6011 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6012 4fc71f3b 2022-07-12 thomas break;
6013 4fc71f3b 2022-07-12 thomas }
6014 15a94983 2018-12-23 stsp }
6015 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6016 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6017 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6018 1e37a5c2 2019-05-12 jcs if (err) {
6019 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6020 1e37a5c2 2019-05-12 jcs break;
6021 1e37a5c2 2019-05-12 jcs }
6022 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6023 4fc71f3b 2022-07-12 thomas s->selected_line;
6024 4fc71f3b 2022-07-12 thomas if (*new_view)
6025 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6026 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6027 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6028 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6029 a5d43cac 2022-07-01 thomas if (err)
6030 a5d43cac 2022-07-01 thomas break;
6031 a5d43cac 2022-07-01 thomas }
6032 a5d43cac 2022-07-01 thomas
6033 e78dc838 2020-12-04 stsp view->focussed = 0;
6034 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6035 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6036 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6037 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6038 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6039 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6040 1e37a5c2 2019-05-12 jcs if (err)
6041 34bc9ec9 2019-02-22 stsp break;
6042 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6043 40236d76 2022-06-23 thomas if (err)
6044 40236d76 2022-06-23 thomas break;
6045 e78dc838 2020-12-04 stsp view->focus_child = 1;
6046 1e37a5c2 2019-05-12 jcs } else
6047 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6048 1e37a5c2 2019-05-12 jcs if (err)
6049 e5a0f69f 2018-08-18 stsp break;
6050 1e37a5c2 2019-05-12 jcs break;
6051 1e37a5c2 2019-05-12 jcs }
6052 70f17a53 2022-06-13 thomas case CTRL('d'):
6053 23427b14 2022-06-23 thomas case 'd':
6054 70f17a53 2022-06-13 thomas nscroll /= 2;
6055 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6056 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6057 ea025d1d 2020-02-22 naddy case CTRL('f'):
6058 1c5e5faa 2022-06-23 thomas case 'f':
6059 1e37a5c2 2019-05-12 jcs case ' ':
6060 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6061 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6062 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6063 07b0611c 2022-06-23 thomas view->count = 0;
6064 e5a0f69f 2018-08-18 stsp break;
6065 1e37a5c2 2019-05-12 jcs }
6066 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6067 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6068 70f17a53 2022-06-13 thomas s->selected_line +=
6069 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6070 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6071 1e37a5c2 2019-05-12 jcs }
6072 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6073 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6074 1e37a5c2 2019-05-12 jcs else
6075 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6076 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6077 1e37a5c2 2019-05-12 jcs break;
6078 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6079 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6080 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6081 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6082 1e37a5c2 2019-05-12 jcs }
6083 1e37a5c2 2019-05-12 jcs break;
6084 1e37a5c2 2019-05-12 jcs default:
6085 07b0611c 2022-06-23 thomas view->count = 0;
6086 1e37a5c2 2019-05-12 jcs break;
6087 a70480e0 2018-06-23 stsp }
6088 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6089 adf4c9e0 2022-07-03 thomas }
6090 adf4c9e0 2022-07-03 thomas
6091 adf4c9e0 2022-07-03 thomas static const struct got_error *
6092 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6093 adf4c9e0 2022-07-03 thomas {
6094 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6095 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6096 adf4c9e0 2022-07-03 thomas
6097 adf4c9e0 2022-07-03 thomas view->count = 0;
6098 adf4c9e0 2022-07-03 thomas s->done = 1;
6099 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6100 adf4c9e0 2022-07-03 thomas s->done = 0;
6101 adf4c9e0 2022-07-03 thomas if (err)
6102 adf4c9e0 2022-07-03 thomas return err;
6103 adf4c9e0 2022-07-03 thomas return run_blame(view);
6104 a70480e0 2018-06-23 stsp }
6105 a70480e0 2018-06-23 stsp
6106 a70480e0 2018-06-23 stsp static const struct got_error *
6107 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6108 9f7d7167 2018-04-29 stsp {
6109 a70480e0 2018-06-23 stsp const struct got_error *error;
6110 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6111 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6112 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6113 0587e10c 2020-07-23 stsp char *link_target = NULL;
6114 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6115 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6116 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6117 a70480e0 2018-06-23 stsp int ch;
6118 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6119 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6120 a70480e0 2018-06-23 stsp
6121 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6122 a70480e0 2018-06-23 stsp switch (ch) {
6123 a70480e0 2018-06-23 stsp case 'c':
6124 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6125 a70480e0 2018-06-23 stsp break;
6126 69069811 2018-08-02 stsp case 'r':
6127 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6128 69069811 2018-08-02 stsp if (repo_path == NULL)
6129 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6130 9ba1d308 2019-10-21 stsp optarg);
6131 69069811 2018-08-02 stsp break;
6132 a70480e0 2018-06-23 stsp default:
6133 17020d27 2019-03-07 stsp usage_blame();
6134 a70480e0 2018-06-23 stsp /* NOTREACHED */
6135 a70480e0 2018-06-23 stsp }
6136 a70480e0 2018-06-23 stsp }
6137 a70480e0 2018-06-23 stsp
6138 a70480e0 2018-06-23 stsp argc -= optind;
6139 a70480e0 2018-06-23 stsp argv += optind;
6140 a70480e0 2018-06-23 stsp
6141 f135c941 2020-02-20 stsp if (argc != 1)
6142 a70480e0 2018-06-23 stsp usage_blame();
6143 6962eb72 2020-02-20 stsp
6144 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6145 7cd52833 2022-06-23 thomas if (error != NULL)
6146 7cd52833 2022-06-23 thomas goto done;
6147 7cd52833 2022-06-23 thomas
6148 69069811 2018-08-02 stsp if (repo_path == NULL) {
6149 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6150 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6151 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6152 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6153 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6154 c156c7a4 2020-12-18 stsp goto done;
6155 f135c941 2020-02-20 stsp if (worktree)
6156 eb41ed75 2019-02-05 stsp repo_path =
6157 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6158 f135c941 2020-02-20 stsp else
6159 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6160 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6161 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6162 c156c7a4 2020-12-18 stsp goto done;
6163 c156c7a4 2020-12-18 stsp }
6164 f135c941 2020-02-20 stsp }
6165 a915003a 2019-02-05 stsp
6166 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6167 c02c541e 2019-03-29 stsp if (error != NULL)
6168 8e94dd5b 2019-01-04 stsp goto done;
6169 69069811 2018-08-02 stsp
6170 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6171 f135c941 2020-02-20 stsp worktree);
6172 c02c541e 2019-03-29 stsp if (error)
6173 92205607 2019-01-04 stsp goto done;
6174 69069811 2018-08-02 stsp
6175 f135c941 2020-02-20 stsp init_curses();
6176 f135c941 2020-02-20 stsp
6177 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6178 51a10b52 2020-12-26 stsp if (error)
6179 51a10b52 2020-12-26 stsp goto done;
6180 51a10b52 2020-12-26 stsp
6181 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6182 eb41ed75 2019-02-05 stsp if (error)
6183 69069811 2018-08-02 stsp goto done;
6184 a70480e0 2018-06-23 stsp
6185 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6186 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6187 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6188 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6189 a70480e0 2018-06-23 stsp if (error != NULL)
6190 66b4983c 2018-06-23 stsp goto done;
6191 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6192 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6193 a70480e0 2018-06-23 stsp } else {
6194 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6195 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6196 a70480e0 2018-06-23 stsp }
6197 a19e88aa 2018-06-23 stsp if (error != NULL)
6198 8b473291 2019-02-21 stsp goto done;
6199 8b473291 2019-02-21 stsp
6200 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6201 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6202 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6203 e1cd8fed 2018-08-01 stsp goto done;
6204 e1cd8fed 2018-08-01 stsp }
6205 0587e10c 2020-07-23 stsp
6206 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6207 945f9229 2022-04-16 thomas if (error)
6208 945f9229 2022-04-16 thomas goto done;
6209 945f9229 2022-04-16 thomas
6210 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6211 945f9229 2022-04-16 thomas commit, repo);
6212 7cbe629d 2018-08-04 stsp if (error)
6213 7cbe629d 2018-08-04 stsp goto done;
6214 0587e10c 2020-07-23 stsp
6215 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6216 78756c87 2020-11-24 stsp commit_id, repo);
6217 0587e10c 2020-07-23 stsp if (error)
6218 0587e10c 2020-07-23 stsp goto done;
6219 12314ad4 2019-08-31 stsp if (worktree) {
6220 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6221 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6222 12314ad4 2019-08-31 stsp worktree = NULL;
6223 12314ad4 2019-08-31 stsp }
6224 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6225 a70480e0 2018-06-23 stsp done:
6226 69069811 2018-08-02 stsp free(repo_path);
6227 f135c941 2020-02-20 stsp free(in_repo_path);
6228 0587e10c 2020-07-23 stsp free(link_target);
6229 69069811 2018-08-02 stsp free(cwd);
6230 a70480e0 2018-06-23 stsp free(commit_id);
6231 945f9229 2022-04-16 thomas if (commit)
6232 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6233 eb41ed75 2019-02-05 stsp if (worktree)
6234 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6235 1d0f4054 2021-06-17 stsp if (repo) {
6236 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6237 1d0f4054 2021-06-17 stsp if (error == NULL)
6238 1d0f4054 2021-06-17 stsp error = close_err;
6239 1d0f4054 2021-06-17 stsp }
6240 7cd52833 2022-06-23 thomas if (pack_fds) {
6241 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6242 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6243 7cd52833 2022-06-23 thomas if (error == NULL)
6244 7cd52833 2022-06-23 thomas error = pack_err;
6245 7cd52833 2022-06-23 thomas }
6246 51a10b52 2020-12-26 stsp tog_free_refs();
6247 a70480e0 2018-06-23 stsp return error;
6248 ffd1d5e5 2018-06-23 stsp }
6249 ffd1d5e5 2018-06-23 stsp
6250 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6251 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6252 ffd1d5e5 2018-06-23 stsp {
6253 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6254 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6255 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6256 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6257 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6258 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6259 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6260 ffd1d5e5 2018-06-23 stsp
6261 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6262 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6263 a5d43cac 2022-07-01 thomas --limit; /* border */
6264 ffd1d5e5 2018-06-23 stsp
6265 f7d12f7e 2018-08-01 stsp werase(view->window);
6266 ffd1d5e5 2018-06-23 stsp
6267 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6268 ffd1d5e5 2018-06-23 stsp return NULL;
6269 ffd1d5e5 2018-06-23 stsp
6270 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6271 f91a2b48 2022-06-23 thomas 0, 0);
6272 ffd1d5e5 2018-06-23 stsp if (err)
6273 ffd1d5e5 2018-06-23 stsp return err;
6274 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6275 a3404814 2018-09-02 stsp wstandout(view->window);
6276 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6277 11b20872 2019-11-08 stsp if (tc)
6278 11b20872 2019-11-08 stsp wattr_on(view->window,
6279 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6280 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6281 11b20872 2019-11-08 stsp if (tc)
6282 11b20872 2019-11-08 stsp wattr_off(view->window,
6283 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6284 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6285 a3404814 2018-09-02 stsp wstandend(view->window);
6286 2550e4c3 2018-07-13 stsp free(wline);
6287 2550e4c3 2018-07-13 stsp wline = NULL;
6288 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6289 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6290 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6291 ffd1d5e5 2018-06-23 stsp return NULL;
6292 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6293 f91a2b48 2022-06-23 thomas 0, 0);
6294 ce52c690 2018-06-23 stsp if (err)
6295 ce52c690 2018-06-23 stsp return err;
6296 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6297 2550e4c3 2018-07-13 stsp free(wline);
6298 2550e4c3 2018-07-13 stsp wline = NULL;
6299 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6300 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6301 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6302 ffd1d5e5 2018-06-23 stsp return NULL;
6303 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6304 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6305 a1eca9bb 2018-06-23 stsp return NULL;
6306 ffd1d5e5 2018-06-23 stsp
6307 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6308 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6309 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6310 0cf4efb1 2018-09-29 stsp if (view->focussed)
6311 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6312 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6313 ffd1d5e5 2018-06-23 stsp }
6314 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6315 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6316 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6317 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6318 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6319 ffd1d5e5 2018-06-23 stsp return NULL;
6320 ffd1d5e5 2018-06-23 stsp n = 1;
6321 ffd1d5e5 2018-06-23 stsp } else {
6322 ffd1d5e5 2018-06-23 stsp n = 0;
6323 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6324 ffd1d5e5 2018-06-23 stsp }
6325 ffd1d5e5 2018-06-23 stsp
6326 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6327 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6328 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6329 848d6979 2019-08-12 stsp const char *modestr = "";
6330 56e0773d 2019-11-28 stsp mode_t mode;
6331 1d13200f 2018-07-12 stsp
6332 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6333 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6334 56e0773d 2019-11-28 stsp
6335 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6336 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6337 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6338 1d13200f 2018-07-12 stsp if (err)
6339 638f9024 2019-05-13 stsp return got_error_from_errno(
6340 230a42bd 2019-05-11 jcs "got_object_id_str");
6341 1d13200f 2018-07-12 stsp }
6342 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6343 63c5ca5d 2019-08-24 stsp modestr = "$";
6344 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6345 0d6c6ee3 2020-05-20 stsp int i;
6346 0d6c6ee3 2020-05-20 stsp
6347 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6348 d86d3b18 2020-12-01 naddy te, s->repo);
6349 0d6c6ee3 2020-05-20 stsp if (err) {
6350 0d6c6ee3 2020-05-20 stsp free(id_str);
6351 0d6c6ee3 2020-05-20 stsp return err;
6352 0d6c6ee3 2020-05-20 stsp }
6353 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6354 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6355 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6356 0d6c6ee3 2020-05-20 stsp }
6357 848d6979 2019-08-12 stsp modestr = "@";
6358 0d6c6ee3 2020-05-20 stsp }
6359 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6360 848d6979 2019-08-12 stsp modestr = "/";
6361 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6362 848d6979 2019-08-12 stsp modestr = "*";
6363 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6364 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6365 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6366 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6367 1d13200f 2018-07-12 stsp free(id_str);
6368 0d6c6ee3 2020-05-20 stsp free(link_target);
6369 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6370 1d13200f 2018-07-12 stsp }
6371 1d13200f 2018-07-12 stsp free(id_str);
6372 0d6c6ee3 2020-05-20 stsp free(link_target);
6373 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6374 f91a2b48 2022-06-23 thomas 0, 0);
6375 ffd1d5e5 2018-06-23 stsp if (err) {
6376 ffd1d5e5 2018-06-23 stsp free(line);
6377 ffd1d5e5 2018-06-23 stsp break;
6378 ffd1d5e5 2018-06-23 stsp }
6379 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6380 0cf4efb1 2018-09-29 stsp if (view->focussed)
6381 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6382 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6383 ffd1d5e5 2018-06-23 stsp }
6384 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6385 f26dddb7 2019-11-08 stsp if (tc)
6386 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6387 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6388 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6389 f26dddb7 2019-11-08 stsp if (tc)
6390 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6391 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6392 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6393 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6394 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6395 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6396 ffd1d5e5 2018-06-23 stsp free(line);
6397 2550e4c3 2018-07-13 stsp free(wline);
6398 2550e4c3 2018-07-13 stsp wline = NULL;
6399 ffd1d5e5 2018-06-23 stsp n++;
6400 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6401 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6402 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6403 ffd1d5e5 2018-06-23 stsp break;
6404 ffd1d5e5 2018-06-23 stsp }
6405 ffd1d5e5 2018-06-23 stsp
6406 ffd1d5e5 2018-06-23 stsp return err;
6407 ffd1d5e5 2018-06-23 stsp }
6408 ffd1d5e5 2018-06-23 stsp
6409 ffd1d5e5 2018-06-23 stsp static void
6410 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6411 ffd1d5e5 2018-06-23 stsp {
6412 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6413 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6414 fa86c4bf 2020-11-29 stsp int i = 0;
6415 ffd1d5e5 2018-06-23 stsp
6416 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6417 ffd1d5e5 2018-06-23 stsp return;
6418 ffd1d5e5 2018-06-23 stsp
6419 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6420 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6421 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6422 fa86c4bf 2020-11-29 stsp if (!isroot)
6423 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6424 fa86c4bf 2020-11-29 stsp break;
6425 fa86c4bf 2020-11-29 stsp }
6426 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6427 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6428 ffd1d5e5 2018-06-23 stsp }
6429 ffd1d5e5 2018-06-23 stsp }
6430 ffd1d5e5 2018-06-23 stsp
6431 a5d43cac 2022-07-01 thomas static const struct got_error *
6432 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6433 ffd1d5e5 2018-06-23 stsp {
6434 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6435 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6436 ffd1d5e5 2018-06-23 stsp int n = 0;
6437 ffd1d5e5 2018-06-23 stsp
6438 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6439 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6440 694d3271 2020-12-01 naddy s->first_displayed_entry);
6441 694d3271 2020-12-01 naddy else
6442 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6443 56e0773d 2019-11-28 stsp
6444 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6445 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6446 a5d43cac 2022-07-01 thomas if (last)
6447 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6448 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6449 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6450 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6451 768394f3 2019-01-24 stsp }
6452 ffd1d5e5 2018-06-23 stsp }
6453 a5d43cac 2022-07-01 thomas
6454 a5d43cac 2022-07-01 thomas return NULL;
6455 ffd1d5e5 2018-06-23 stsp }
6456 ffd1d5e5 2018-06-23 stsp
6457 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6458 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6459 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6460 ffd1d5e5 2018-06-23 stsp {
6461 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6462 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6463 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6464 ffd1d5e5 2018-06-23 stsp
6465 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6466 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6467 56e0773d 2019-11-28 stsp + 1 /* slash */;
6468 ce52c690 2018-06-23 stsp if (te)
6469 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6470 ce52c690 2018-06-23 stsp
6471 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6472 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6473 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6474 ffd1d5e5 2018-06-23 stsp
6475 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6476 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6477 d9765a41 2018-06-23 stsp while (pt) {
6478 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6479 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6480 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6481 cb2ebc8a 2018-06-23 stsp goto done;
6482 cb2ebc8a 2018-06-23 stsp }
6483 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6484 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6485 cb2ebc8a 2018-06-23 stsp goto done;
6486 cb2ebc8a 2018-06-23 stsp }
6487 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6488 ffd1d5e5 2018-06-23 stsp }
6489 ce52c690 2018-06-23 stsp if (te) {
6490 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6491 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6492 ce52c690 2018-06-23 stsp goto done;
6493 ce52c690 2018-06-23 stsp }
6494 cb2ebc8a 2018-06-23 stsp }
6495 ce52c690 2018-06-23 stsp done:
6496 ce52c690 2018-06-23 stsp if (err) {
6497 ce52c690 2018-06-23 stsp free(*path);
6498 ce52c690 2018-06-23 stsp *path = NULL;
6499 ce52c690 2018-06-23 stsp }
6500 ce52c690 2018-06-23 stsp return err;
6501 ce52c690 2018-06-23 stsp }
6502 ce52c690 2018-06-23 stsp
6503 ce52c690 2018-06-23 stsp static const struct got_error *
6504 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6505 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6506 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6507 ce52c690 2018-06-23 stsp {
6508 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6509 ce52c690 2018-06-23 stsp char *path;
6510 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6511 a0de39f3 2019-08-09 stsp
6512 a0de39f3 2019-08-09 stsp *new_view = NULL;
6513 69efd4c4 2018-07-18 stsp
6514 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6515 ce52c690 2018-06-23 stsp if (err)
6516 ce52c690 2018-06-23 stsp return err;
6517 ffd1d5e5 2018-06-23 stsp
6518 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6519 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6520 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6521 83ce39e3 2019-08-12 stsp goto done;
6522 83ce39e3 2019-08-12 stsp }
6523 cdf1ee82 2018-08-01 stsp
6524 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6525 e5a0f69f 2018-08-18 stsp if (err) {
6526 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6527 fc06ba56 2019-08-22 stsp err = NULL;
6528 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6529 e5a0f69f 2018-08-18 stsp } else
6530 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6531 83ce39e3 2019-08-12 stsp done:
6532 83ce39e3 2019-08-12 stsp free(path);
6533 69efd4c4 2018-07-18 stsp return err;
6534 69efd4c4 2018-07-18 stsp }
6535 69efd4c4 2018-07-18 stsp
6536 69efd4c4 2018-07-18 stsp static const struct got_error *
6537 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6538 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6539 69efd4c4 2018-07-18 stsp {
6540 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6541 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6542 69efd4c4 2018-07-18 stsp char *path;
6543 a0de39f3 2019-08-09 stsp
6544 a0de39f3 2019-08-09 stsp *new_view = NULL;
6545 69efd4c4 2018-07-18 stsp
6546 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6547 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6548 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6549 e5a0f69f 2018-08-18 stsp
6550 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6551 69efd4c4 2018-07-18 stsp if (err)
6552 69efd4c4 2018-07-18 stsp return err;
6553 69efd4c4 2018-07-18 stsp
6554 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6555 4e97c21c 2020-12-06 stsp path, 0);
6556 ba4f502b 2018-08-04 stsp if (err)
6557 e5a0f69f 2018-08-18 stsp view_close(log_view);
6558 e5a0f69f 2018-08-18 stsp else
6559 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6560 cb2ebc8a 2018-06-23 stsp free(path);
6561 cb2ebc8a 2018-06-23 stsp return err;
6562 ffd1d5e5 2018-06-23 stsp }
6563 ffd1d5e5 2018-06-23 stsp
6564 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6565 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6566 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6567 ffd1d5e5 2018-06-23 stsp {
6568 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6569 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6570 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6571 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6572 ffd1d5e5 2018-06-23 stsp
6573 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6574 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6575 bc573f3b 2021-07-10 stsp
6576 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6577 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6578 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6579 ffd1d5e5 2018-06-23 stsp
6580 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6581 bc573f3b 2021-07-10 stsp if (err)
6582 bc573f3b 2021-07-10 stsp goto done;
6583 bc573f3b 2021-07-10 stsp
6584 bc573f3b 2021-07-10 stsp /*
6585 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6586 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6587 bc573f3b 2021-07-10 stsp * closed on demand.
6588 bc573f3b 2021-07-10 stsp */
6589 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6590 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6591 bc573f3b 2021-07-10 stsp if (err)
6592 bc573f3b 2021-07-10 stsp goto done;
6593 bc573f3b 2021-07-10 stsp s->tree = s->root;
6594 bc573f3b 2021-07-10 stsp
6595 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6596 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6597 ffd1d5e5 2018-06-23 stsp goto done;
6598 ffd1d5e5 2018-06-23 stsp
6599 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6600 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6601 ffd1d5e5 2018-06-23 stsp goto done;
6602 ffd1d5e5 2018-06-23 stsp }
6603 ffd1d5e5 2018-06-23 stsp
6604 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6605 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6606 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6607 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6608 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6609 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6610 9cd7cbd1 2020-12-07 stsp goto done;
6611 9cd7cbd1 2020-12-07 stsp }
6612 9cd7cbd1 2020-12-07 stsp }
6613 fb2756b9 2018-08-04 stsp s->repo = repo;
6614 c0b01bdb 2019-11-08 stsp
6615 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6616 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6617 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6618 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6619 c0b01bdb 2019-11-08 stsp if (err)
6620 c0b01bdb 2019-11-08 stsp goto done;
6621 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6622 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6623 bc573f3b 2021-07-10 stsp if (err)
6624 c0b01bdb 2019-11-08 stsp goto done;
6625 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6626 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6627 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6628 bc573f3b 2021-07-10 stsp if (err)
6629 c0b01bdb 2019-11-08 stsp goto done;
6630 e5a0f69f 2018-08-18 stsp
6631 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6632 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6633 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6634 bc573f3b 2021-07-10 stsp if (err)
6635 11b20872 2019-11-08 stsp goto done;
6636 11b20872 2019-11-08 stsp
6637 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6638 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6639 bc573f3b 2021-07-10 stsp if (err)
6640 c0b01bdb 2019-11-08 stsp goto done;
6641 c0b01bdb 2019-11-08 stsp }
6642 c0b01bdb 2019-11-08 stsp
6643 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6644 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6645 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6646 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6647 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6648 ad80ab7b 2018-08-04 stsp done:
6649 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6650 bc573f3b 2021-07-10 stsp if (commit)
6651 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6652 bc573f3b 2021-07-10 stsp if (err)
6653 bc573f3b 2021-07-10 stsp close_tree_view(view);
6654 ad80ab7b 2018-08-04 stsp return err;
6655 ad80ab7b 2018-08-04 stsp }
6656 ad80ab7b 2018-08-04 stsp
6657 e5a0f69f 2018-08-18 stsp static const struct got_error *
6658 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6659 ad80ab7b 2018-08-04 stsp {
6660 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6661 ad80ab7b 2018-08-04 stsp
6662 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6663 fb2756b9 2018-08-04 stsp free(s->tree_label);
6664 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6665 6484ec90 2018-09-29 stsp free(s->commit_id);
6666 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6667 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6668 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6669 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6670 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6671 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6672 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6673 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6674 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6675 ad80ab7b 2018-08-04 stsp free(parent);
6676 ad80ab7b 2018-08-04 stsp
6677 ad80ab7b 2018-08-04 stsp }
6678 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6679 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6680 bc573f3b 2021-07-10 stsp if (s->root)
6681 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6682 7c32bd05 2019-06-22 stsp return NULL;
6683 7c32bd05 2019-06-22 stsp }
6684 7c32bd05 2019-06-22 stsp
6685 7c32bd05 2019-06-22 stsp static const struct got_error *
6686 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6687 7c32bd05 2019-06-22 stsp {
6688 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6689 7c32bd05 2019-06-22 stsp
6690 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6691 7c32bd05 2019-06-22 stsp return NULL;
6692 7c32bd05 2019-06-22 stsp }
6693 7c32bd05 2019-06-22 stsp
6694 7c32bd05 2019-06-22 stsp static int
6695 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6696 7c32bd05 2019-06-22 stsp {
6697 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6698 7c32bd05 2019-06-22 stsp
6699 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6700 56e0773d 2019-11-28 stsp 0) == 0;
6701 7c32bd05 2019-06-22 stsp }
6702 7c32bd05 2019-06-22 stsp
6703 7c32bd05 2019-06-22 stsp static const struct got_error *
6704 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6705 7c32bd05 2019-06-22 stsp {
6706 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6707 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6708 7c32bd05 2019-06-22 stsp
6709 7c32bd05 2019-06-22 stsp if (!view->searching) {
6710 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6711 7c32bd05 2019-06-22 stsp return NULL;
6712 7c32bd05 2019-06-22 stsp }
6713 7c32bd05 2019-06-22 stsp
6714 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6715 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6716 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6717 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6718 56e0773d 2019-11-28 stsp s->selected_entry);
6719 7c32bd05 2019-06-22 stsp else
6720 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6721 56e0773d 2019-11-28 stsp } else {
6722 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6723 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6724 56e0773d 2019-11-28 stsp else
6725 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6726 56e0773d 2019-11-28 stsp s->selected_entry);
6727 7c32bd05 2019-06-22 stsp }
6728 7c32bd05 2019-06-22 stsp } else {
6729 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6730 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6731 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6732 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6733 56e0773d 2019-11-28 stsp else
6734 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6735 7c32bd05 2019-06-22 stsp }
6736 7c32bd05 2019-06-22 stsp
6737 7c32bd05 2019-06-22 stsp while (1) {
6738 56e0773d 2019-11-28 stsp if (te == NULL) {
6739 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6740 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6741 ac66afb8 2019-06-24 stsp return NULL;
6742 ac66afb8 2019-06-24 stsp }
6743 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6744 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6745 56e0773d 2019-11-28 stsp else
6746 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6747 7c32bd05 2019-06-22 stsp }
6748 7c32bd05 2019-06-22 stsp
6749 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6750 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6751 56e0773d 2019-11-28 stsp s->matched_entry = te;
6752 7c32bd05 2019-06-22 stsp break;
6753 7c32bd05 2019-06-22 stsp }
6754 7c32bd05 2019-06-22 stsp
6755 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6756 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6757 56e0773d 2019-11-28 stsp else
6758 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6759 7c32bd05 2019-06-22 stsp }
6760 e5a0f69f 2018-08-18 stsp
6761 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6762 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6763 7c32bd05 2019-06-22 stsp s->selected = 0;
6764 7c32bd05 2019-06-22 stsp }
6765 7c32bd05 2019-06-22 stsp
6766 e5a0f69f 2018-08-18 stsp return NULL;
6767 ad80ab7b 2018-08-04 stsp }
6768 ad80ab7b 2018-08-04 stsp
6769 ad80ab7b 2018-08-04 stsp static const struct got_error *
6770 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6771 ad80ab7b 2018-08-04 stsp {
6772 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6773 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6774 e5a0f69f 2018-08-18 stsp char *parent_path;
6775 ad80ab7b 2018-08-04 stsp
6776 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6777 e5a0f69f 2018-08-18 stsp if (err)
6778 e5a0f69f 2018-08-18 stsp return err;
6779 ffd1d5e5 2018-06-23 stsp
6780 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6781 e5a0f69f 2018-08-18 stsp free(parent_path);
6782 669b5ffa 2018-10-07 stsp
6783 a5d43cac 2022-07-01 thomas view_border(view);
6784 e5a0f69f 2018-08-18 stsp return err;
6785 e5a0f69f 2018-08-18 stsp }
6786 ce52c690 2018-06-23 stsp
6787 e5a0f69f 2018-08-18 stsp static const struct got_error *
6788 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6789 e5a0f69f 2018-08-18 stsp {
6790 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6791 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6792 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6793 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6794 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6795 ffd1d5e5 2018-06-23 stsp
6796 e5a0f69f 2018-08-18 stsp switch (ch) {
6797 1e37a5c2 2019-05-12 jcs case 'i':
6798 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6799 07b0611c 2022-06-23 thomas view->count = 0;
6800 1e37a5c2 2019-05-12 jcs break;
6801 1e37a5c2 2019-05-12 jcs case 'l':
6802 07b0611c 2022-06-23 thomas view->count = 0;
6803 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6804 ffd1d5e5 2018-06-23 stsp break;
6805 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6806 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6807 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6808 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6809 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6810 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6811 444d5325 2022-07-03 thomas if (err)
6812 444d5325 2022-07-03 thomas break;
6813 444d5325 2022-07-03 thomas }
6814 e78dc838 2020-12-04 stsp view->focussed = 0;
6815 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6816 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6817 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6818 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6819 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
6820 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6821 1e37a5c2 2019-05-12 jcs if (err)
6822 1e37a5c2 2019-05-12 jcs return err;
6823 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6824 40236d76 2022-06-23 thomas if (err)
6825 40236d76 2022-06-23 thomas return err;
6826 e78dc838 2020-12-04 stsp view->focus_child = 1;
6827 1e37a5c2 2019-05-12 jcs } else
6828 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6829 152c1c93 2020-11-29 stsp break;
6830 152c1c93 2020-11-29 stsp case 'r':
6831 07b0611c 2022-06-23 thomas view->count = 0;
6832 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6833 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6834 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6835 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6836 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6837 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6838 152c1c93 2020-11-29 stsp if (err) {
6839 152c1c93 2020-11-29 stsp view_close(ref_view);
6840 152c1c93 2020-11-29 stsp return err;
6841 152c1c93 2020-11-29 stsp }
6842 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6843 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6844 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6845 444d5325 2022-07-03 thomas if (err)
6846 444d5325 2022-07-03 thomas break;
6847 444d5325 2022-07-03 thomas }
6848 e78dc838 2020-12-04 stsp view->focussed = 0;
6849 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6850 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6851 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6852 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6853 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
6854 152c1c93 2020-11-29 stsp err = view_close_child(view);
6855 152c1c93 2020-11-29 stsp if (err)
6856 152c1c93 2020-11-29 stsp return err;
6857 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6858 40236d76 2022-06-23 thomas if (err)
6859 40236d76 2022-06-23 thomas return err;
6860 e78dc838 2020-12-04 stsp view->focus_child = 1;
6861 152c1c93 2020-11-29 stsp } else
6862 152c1c93 2020-11-29 stsp *new_view = ref_view;
6863 e4526bf5 2021-09-03 naddy break;
6864 e4526bf5 2021-09-03 naddy case 'g':
6865 e4526bf5 2021-09-03 naddy case KEY_HOME:
6866 e4526bf5 2021-09-03 naddy s->selected = 0;
6867 07b0611c 2022-06-23 thomas view->count = 0;
6868 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6869 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6870 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6871 e4526bf5 2021-09-03 naddy else
6872 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6873 1e37a5c2 2019-05-12 jcs break;
6874 e4526bf5 2021-09-03 naddy case 'G':
6875 a5d43cac 2022-07-01 thomas case KEY_END: {
6876 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6877 a5d43cac 2022-07-01 thomas
6878 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6879 a5d43cac 2022-07-01 thomas --eos; /* border */
6880 e4526bf5 2021-09-03 naddy s->selected = 0;
6881 07b0611c 2022-06-23 thomas view->count = 0;
6882 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6883 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6884 e4526bf5 2021-09-03 naddy if (te == NULL) {
6885 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6886 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6887 e4526bf5 2021-09-03 naddy n++;
6888 e4526bf5 2021-09-03 naddy }
6889 e4526bf5 2021-09-03 naddy break;
6890 e4526bf5 2021-09-03 naddy }
6891 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6892 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6893 e4526bf5 2021-09-03 naddy }
6894 e4526bf5 2021-09-03 naddy if (n > 0)
6895 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6896 e4526bf5 2021-09-03 naddy break;
6897 a5d43cac 2022-07-01 thomas }
6898 1e37a5c2 2019-05-12 jcs case 'k':
6899 1e37a5c2 2019-05-12 jcs case KEY_UP:
6900 f7140bf5 2021-10-17 thomas case CTRL('p'):
6901 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6902 1e37a5c2 2019-05-12 jcs s->selected--;
6903 fa86c4bf 2020-11-29 stsp break;
6904 1e37a5c2 2019-05-12 jcs }
6905 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6906 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6907 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6908 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6909 07b0611c 2022-06-23 thomas view->count = 0;
6910 1e37a5c2 2019-05-12 jcs break;
6911 70f17a53 2022-06-13 thomas case CTRL('u'):
6912 23427b14 2022-06-23 thomas case 'u':
6913 70f17a53 2022-06-13 thomas nscroll /= 2;
6914 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6915 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6916 ea025d1d 2020-02-22 naddy case CTRL('b'):
6917 1c5e5faa 2022-06-23 thomas case 'b':
6918 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6919 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6920 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6921 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6922 fa86c4bf 2020-11-29 stsp } else {
6923 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6924 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6925 fa86c4bf 2020-11-29 stsp }
6926 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6927 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6928 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6929 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6930 07b0611c 2022-06-23 thomas view->count = 0;
6931 1e37a5c2 2019-05-12 jcs break;
6932 1e37a5c2 2019-05-12 jcs case 'j':
6933 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6934 f7140bf5 2021-10-17 thomas case CTRL('n'):
6935 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6936 1e37a5c2 2019-05-12 jcs s->selected++;
6937 1e37a5c2 2019-05-12 jcs break;
6938 1e37a5c2 2019-05-12 jcs }
6939 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6940 07b0611c 2022-06-23 thomas == NULL) {
6941 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6942 07b0611c 2022-06-23 thomas view->count = 0;
6943 1e37a5c2 2019-05-12 jcs break;
6944 07b0611c 2022-06-23 thomas }
6945 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6946 1e37a5c2 2019-05-12 jcs break;
6947 70f17a53 2022-06-13 thomas case CTRL('d'):
6948 23427b14 2022-06-23 thomas case 'd':
6949 70f17a53 2022-06-13 thomas nscroll /= 2;
6950 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6951 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6952 ea025d1d 2020-02-22 naddy case CTRL('f'):
6953 1c5e5faa 2022-06-23 thomas case 'f':
6954 4c2d69cb 2022-06-23 thomas case ' ':
6955 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6956 1e37a5c2 2019-05-12 jcs == NULL) {
6957 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6958 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6959 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6960 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6961 07b0611c 2022-06-23 thomas else
6962 07b0611c 2022-06-23 thomas view->count = 0;
6963 1e37a5c2 2019-05-12 jcs break;
6964 1e37a5c2 2019-05-12 jcs }
6965 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6966 1e37a5c2 2019-05-12 jcs break;
6967 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6968 1e37a5c2 2019-05-12 jcs case '\r':
6969 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6970 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6971 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6972 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6973 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6974 07b0611c 2022-06-23 thomas view->count = 0;
6975 1e37a5c2 2019-05-12 jcs break;
6976 07b0611c 2022-06-23 thomas }
6977 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6978 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6979 1e37a5c2 2019-05-12 jcs entry);
6980 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6981 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6982 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6983 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6984 1e37a5c2 2019-05-12 jcs s->selected_entry =
6985 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6986 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6987 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6988 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6989 a5d43cac 2022-07-01 thomas if (err)
6990 a5d43cac 2022-07-01 thomas break;
6991 a5d43cac 2022-07-01 thomas }
6992 1e37a5c2 2019-05-12 jcs free(parent);
6993 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6994 56e0773d 2019-11-28 stsp s->selected_entry))) {
6995 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6996 07b0611c 2022-06-23 thomas view->count = 0;
6997 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6998 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6999 1e37a5c2 2019-05-12 jcs if (err)
7000 1e37a5c2 2019-05-12 jcs break;
7001 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7002 941e9f74 2019-05-21 stsp if (err) {
7003 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7004 1e37a5c2 2019-05-12 jcs break;
7005 1e37a5c2 2019-05-12 jcs }
7006 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7007 56e0773d 2019-11-28 stsp s->selected_entry))) {
7008 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7009 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
7010 1e37a5c2 2019-05-12 jcs
7011 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
7012 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7013 a5d43cac 2022-07-01 thomas
7014 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
7015 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7016 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7017 1e37a5c2 2019-05-12 jcs if (err)
7018 1e37a5c2 2019-05-12 jcs break;
7019 a5d43cac 2022-07-01 thomas
7020 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7021 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7022 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7023 a5d43cac 2022-07-01 thomas if (err)
7024 a5d43cac 2022-07-01 thomas break;
7025 a5d43cac 2022-07-01 thomas }
7026 a5d43cac 2022-07-01 thomas
7027 07b0611c 2022-06-23 thomas view->count = 0;
7028 e78dc838 2020-12-04 stsp view->focussed = 0;
7029 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7030 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
7031 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
7032 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7033 53d2bdd3 2022-07-10 thomas view_transfer_size(blame_view, view);
7034 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7035 669b5ffa 2018-10-07 stsp if (err)
7036 669b5ffa 2018-10-07 stsp return err;
7037 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
7038 40236d76 2022-06-23 thomas if (err)
7039 40236d76 2022-06-23 thomas return err;
7040 e78dc838 2020-12-04 stsp view->focus_child = 1;
7041 669b5ffa 2018-10-07 stsp } else
7042 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7043 1e37a5c2 2019-05-12 jcs }
7044 1e37a5c2 2019-05-12 jcs break;
7045 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7046 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7047 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7048 07b0611c 2022-06-23 thomas view->count = 0;
7049 1e37a5c2 2019-05-12 jcs break;
7050 1e37a5c2 2019-05-12 jcs default:
7051 07b0611c 2022-06-23 thomas view->count = 0;
7052 1e37a5c2 2019-05-12 jcs break;
7053 ffd1d5e5 2018-06-23 stsp }
7054 e5a0f69f 2018-08-18 stsp
7055 ffd1d5e5 2018-06-23 stsp return err;
7056 9f7d7167 2018-04-29 stsp }
7057 9f7d7167 2018-04-29 stsp
7058 ffd1d5e5 2018-06-23 stsp __dead static void
7059 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7060 ffd1d5e5 2018-06-23 stsp {
7061 ffd1d5e5 2018-06-23 stsp endwin();
7062 91198554 2022-06-23 thomas fprintf(stderr,
7063 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7064 ffd1d5e5 2018-06-23 stsp getprogname());
7065 ffd1d5e5 2018-06-23 stsp exit(1);
7066 ffd1d5e5 2018-06-23 stsp }
7067 ffd1d5e5 2018-06-23 stsp
7068 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7069 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7070 ffd1d5e5 2018-06-23 stsp {
7071 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7072 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7073 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7074 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7075 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7076 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7077 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7078 4e97c21c 2020-12-06 stsp char *label = NULL;
7079 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7080 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7081 ffd1d5e5 2018-06-23 stsp int ch;
7082 5221c383 2018-08-01 stsp struct tog_view *view;
7083 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7084 70ac5f84 2019-03-28 stsp
7085 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7086 ffd1d5e5 2018-06-23 stsp switch (ch) {
7087 ffd1d5e5 2018-06-23 stsp case 'c':
7088 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7089 74283ab8 2020-02-07 stsp break;
7090 74283ab8 2020-02-07 stsp case 'r':
7091 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7092 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7093 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7094 74283ab8 2020-02-07 stsp optarg);
7095 ffd1d5e5 2018-06-23 stsp break;
7096 ffd1d5e5 2018-06-23 stsp default:
7097 e99e2d15 2020-11-24 naddy usage_tree();
7098 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7099 ffd1d5e5 2018-06-23 stsp }
7100 ffd1d5e5 2018-06-23 stsp }
7101 ffd1d5e5 2018-06-23 stsp
7102 ffd1d5e5 2018-06-23 stsp argc -= optind;
7103 ffd1d5e5 2018-06-23 stsp argv += optind;
7104 ffd1d5e5 2018-06-23 stsp
7105 55cccc34 2020-02-20 stsp if (argc > 1)
7106 e99e2d15 2020-11-24 naddy usage_tree();
7107 7cd52833 2022-06-23 thomas
7108 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7109 7cd52833 2022-06-23 thomas if (error != NULL)
7110 7cd52833 2022-06-23 thomas goto done;
7111 74283ab8 2020-02-07 stsp
7112 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7113 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7114 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7115 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7116 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7117 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7118 c156c7a4 2020-12-18 stsp goto done;
7119 55cccc34 2020-02-20 stsp if (worktree)
7120 52185f70 2019-02-05 stsp repo_path =
7121 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7122 55cccc34 2020-02-20 stsp else
7123 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7124 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7125 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7126 c156c7a4 2020-12-18 stsp goto done;
7127 c156c7a4 2020-12-18 stsp }
7128 55cccc34 2020-02-20 stsp }
7129 a915003a 2019-02-05 stsp
7130 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7131 c02c541e 2019-03-29 stsp if (error != NULL)
7132 52185f70 2019-02-05 stsp goto done;
7133 d188b9a6 2019-01-04 stsp
7134 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7135 55cccc34 2020-02-20 stsp repo, worktree);
7136 55cccc34 2020-02-20 stsp if (error)
7137 55cccc34 2020-02-20 stsp goto done;
7138 55cccc34 2020-02-20 stsp
7139 55cccc34 2020-02-20 stsp init_curses();
7140 55cccc34 2020-02-20 stsp
7141 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7142 c02c541e 2019-03-29 stsp if (error)
7143 52185f70 2019-02-05 stsp goto done;
7144 ffd1d5e5 2018-06-23 stsp
7145 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7146 51a10b52 2020-12-26 stsp if (error)
7147 51a10b52 2020-12-26 stsp goto done;
7148 51a10b52 2020-12-26 stsp
7149 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7150 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7151 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7152 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7153 4e97c21c 2020-12-06 stsp if (error)
7154 4e97c21c 2020-12-06 stsp goto done;
7155 4e97c21c 2020-12-06 stsp head_ref_name = label;
7156 4e97c21c 2020-12-06 stsp } else {
7157 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7158 4e97c21c 2020-12-06 stsp if (error == NULL)
7159 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7160 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7161 4e97c21c 2020-12-06 stsp goto done;
7162 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7163 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7164 4e97c21c 2020-12-06 stsp if (error)
7165 4e97c21c 2020-12-06 stsp goto done;
7166 4e97c21c 2020-12-06 stsp }
7167 ffd1d5e5 2018-06-23 stsp
7168 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7169 945f9229 2022-04-16 thomas if (error)
7170 945f9229 2022-04-16 thomas goto done;
7171 945f9229 2022-04-16 thomas
7172 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7173 5221c383 2018-08-01 stsp if (view == NULL) {
7174 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7175 5221c383 2018-08-01 stsp goto done;
7176 5221c383 2018-08-01 stsp }
7177 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7178 ad80ab7b 2018-08-04 stsp if (error)
7179 ad80ab7b 2018-08-04 stsp goto done;
7180 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7181 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7182 d91faf3b 2020-12-01 naddy in_repo_path);
7183 55cccc34 2020-02-20 stsp if (error)
7184 55cccc34 2020-02-20 stsp goto done;
7185 55cccc34 2020-02-20 stsp }
7186 55cccc34 2020-02-20 stsp
7187 55cccc34 2020-02-20 stsp if (worktree) {
7188 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7189 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7190 55cccc34 2020-02-20 stsp worktree = NULL;
7191 55cccc34 2020-02-20 stsp }
7192 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7193 ffd1d5e5 2018-06-23 stsp done:
7194 52185f70 2019-02-05 stsp free(repo_path);
7195 e4a0e26d 2020-02-20 stsp free(cwd);
7196 ffd1d5e5 2018-06-23 stsp free(commit_id);
7197 4e97c21c 2020-12-06 stsp free(label);
7198 486cd271 2020-12-06 stsp if (ref)
7199 486cd271 2020-12-06 stsp got_ref_close(ref);
7200 1d0f4054 2021-06-17 stsp if (repo) {
7201 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7202 1d0f4054 2021-06-17 stsp if (error == NULL)
7203 1d0f4054 2021-06-17 stsp error = close_err;
7204 1d0f4054 2021-06-17 stsp }
7205 7cd52833 2022-06-23 thomas if (pack_fds) {
7206 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7207 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7208 7cd52833 2022-06-23 thomas if (error == NULL)
7209 7cd52833 2022-06-23 thomas error = pack_err;
7210 7cd52833 2022-06-23 thomas }
7211 51a10b52 2020-12-26 stsp tog_free_refs();
7212 ffd1d5e5 2018-06-23 stsp return error;
7213 6458efa5 2020-11-24 stsp }
7214 6458efa5 2020-11-24 stsp
7215 6458efa5 2020-11-24 stsp static const struct got_error *
7216 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7217 6458efa5 2020-11-24 stsp {
7218 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7219 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7220 6458efa5 2020-11-24 stsp
7221 6458efa5 2020-11-24 stsp s->nrefs = 0;
7222 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7223 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7224 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7225 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7226 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7227 6458efa5 2020-11-24 stsp continue;
7228 6458efa5 2020-11-24 stsp
7229 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7230 6458efa5 2020-11-24 stsp if (re == NULL)
7231 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7232 6458efa5 2020-11-24 stsp
7233 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7234 8924d611 2020-12-26 stsp if (re->ref == NULL)
7235 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7236 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7237 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7238 6458efa5 2020-11-24 stsp }
7239 6458efa5 2020-11-24 stsp
7240 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7241 6458efa5 2020-11-24 stsp return NULL;
7242 6458efa5 2020-11-24 stsp }
7243 6458efa5 2020-11-24 stsp
7244 ef20f542 2022-06-26 thomas static void
7245 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7246 6458efa5 2020-11-24 stsp {
7247 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7248 6458efa5 2020-11-24 stsp
7249 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7250 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7251 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7252 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7253 6458efa5 2020-11-24 stsp free(re);
7254 6458efa5 2020-11-24 stsp }
7255 6458efa5 2020-11-24 stsp }
7256 6458efa5 2020-11-24 stsp
7257 6458efa5 2020-11-24 stsp static const struct got_error *
7258 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7259 6458efa5 2020-11-24 stsp {
7260 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7261 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7262 6458efa5 2020-11-24 stsp
7263 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7264 6458efa5 2020-11-24 stsp s->repo = repo;
7265 6458efa5 2020-11-24 stsp
7266 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7267 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7268 6458efa5 2020-11-24 stsp
7269 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7270 6458efa5 2020-11-24 stsp if (err)
7271 6458efa5 2020-11-24 stsp return err;
7272 34ba6917 2020-11-30 stsp
7273 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7274 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7275 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7276 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7277 6458efa5 2020-11-24 stsp if (err)
7278 6458efa5 2020-11-24 stsp goto done;
7279 6458efa5 2020-11-24 stsp
7280 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7281 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7282 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7283 6458efa5 2020-11-24 stsp if (err)
7284 6458efa5 2020-11-24 stsp goto done;
7285 6458efa5 2020-11-24 stsp
7286 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7287 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7288 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7289 2183bbf6 2022-01-23 thomas if (err)
7290 2183bbf6 2022-01-23 thomas goto done;
7291 2183bbf6 2022-01-23 thomas
7292 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7293 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7294 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7295 6458efa5 2020-11-24 stsp if (err)
7296 6458efa5 2020-11-24 stsp goto done;
7297 6458efa5 2020-11-24 stsp }
7298 6458efa5 2020-11-24 stsp
7299 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7300 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7301 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7302 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7303 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7304 6458efa5 2020-11-24 stsp done:
7305 6458efa5 2020-11-24 stsp if (err)
7306 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7307 6458efa5 2020-11-24 stsp return err;
7308 6458efa5 2020-11-24 stsp }
7309 6458efa5 2020-11-24 stsp
7310 6458efa5 2020-11-24 stsp static const struct got_error *
7311 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7312 6458efa5 2020-11-24 stsp {
7313 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7314 6458efa5 2020-11-24 stsp
7315 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7316 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7317 6458efa5 2020-11-24 stsp
7318 6458efa5 2020-11-24 stsp return NULL;
7319 9f7d7167 2018-04-29 stsp }
7320 ce5b7c56 2019-07-09 stsp
7321 6458efa5 2020-11-24 stsp static const struct got_error *
7322 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7323 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7324 6458efa5 2020-11-24 stsp {
7325 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7326 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7327 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7328 6458efa5 2020-11-24 stsp int obj_type;
7329 6458efa5 2020-11-24 stsp
7330 c42c9805 2020-11-24 stsp *commit_id = NULL;
7331 6458efa5 2020-11-24 stsp
7332 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7333 6458efa5 2020-11-24 stsp if (err)
7334 6458efa5 2020-11-24 stsp return err;
7335 6458efa5 2020-11-24 stsp
7336 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7337 6458efa5 2020-11-24 stsp if (err)
7338 6458efa5 2020-11-24 stsp goto done;
7339 6458efa5 2020-11-24 stsp
7340 6458efa5 2020-11-24 stsp switch (obj_type) {
7341 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7342 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7343 6458efa5 2020-11-24 stsp break;
7344 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7345 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7346 6458efa5 2020-11-24 stsp if (err)
7347 6458efa5 2020-11-24 stsp goto done;
7348 c42c9805 2020-11-24 stsp free(obj_id);
7349 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7350 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7351 c42c9805 2020-11-24 stsp if (err)
7352 6458efa5 2020-11-24 stsp goto done;
7353 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7354 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7355 c42c9805 2020-11-24 stsp goto done;
7356 c42c9805 2020-11-24 stsp }
7357 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7358 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7359 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7360 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7361 c42c9805 2020-11-24 stsp goto done;
7362 c42c9805 2020-11-24 stsp }
7363 6458efa5 2020-11-24 stsp break;
7364 6458efa5 2020-11-24 stsp default:
7365 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7366 c42c9805 2020-11-24 stsp break;
7367 6458efa5 2020-11-24 stsp }
7368 6458efa5 2020-11-24 stsp
7369 c42c9805 2020-11-24 stsp done:
7370 c42c9805 2020-11-24 stsp if (tag)
7371 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7372 c42c9805 2020-11-24 stsp if (err) {
7373 c42c9805 2020-11-24 stsp free(*commit_id);
7374 c42c9805 2020-11-24 stsp *commit_id = NULL;
7375 c42c9805 2020-11-24 stsp }
7376 c42c9805 2020-11-24 stsp return err;
7377 c42c9805 2020-11-24 stsp }
7378 c42c9805 2020-11-24 stsp
7379 c42c9805 2020-11-24 stsp static const struct got_error *
7380 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7381 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7382 c42c9805 2020-11-24 stsp {
7383 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7384 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7385 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7386 c42c9805 2020-11-24 stsp
7387 c42c9805 2020-11-24 stsp *new_view = NULL;
7388 c42c9805 2020-11-24 stsp
7389 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7390 c42c9805 2020-11-24 stsp if (err) {
7391 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7392 c42c9805 2020-11-24 stsp return err;
7393 c42c9805 2020-11-24 stsp else
7394 c42c9805 2020-11-24 stsp return NULL;
7395 c42c9805 2020-11-24 stsp }
7396 c42c9805 2020-11-24 stsp
7397 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7398 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7399 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7400 6458efa5 2020-11-24 stsp goto done;
7401 6458efa5 2020-11-24 stsp }
7402 6458efa5 2020-11-24 stsp
7403 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7404 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7405 6458efa5 2020-11-24 stsp done:
7406 6458efa5 2020-11-24 stsp if (err)
7407 6458efa5 2020-11-24 stsp view_close(log_view);
7408 6458efa5 2020-11-24 stsp else
7409 6458efa5 2020-11-24 stsp *new_view = log_view;
7410 c42c9805 2020-11-24 stsp free(commit_id);
7411 6458efa5 2020-11-24 stsp return err;
7412 6458efa5 2020-11-24 stsp }
7413 6458efa5 2020-11-24 stsp
7414 ce5b7c56 2019-07-09 stsp static void
7415 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7416 6458efa5 2020-11-24 stsp {
7417 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7418 34ba6917 2020-11-30 stsp int i = 0;
7419 6458efa5 2020-11-24 stsp
7420 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7421 6458efa5 2020-11-24 stsp return;
7422 6458efa5 2020-11-24 stsp
7423 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7424 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7425 34ba6917 2020-11-30 stsp if (re == NULL)
7426 34ba6917 2020-11-30 stsp break;
7427 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7428 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7429 6458efa5 2020-11-24 stsp }
7430 6458efa5 2020-11-24 stsp }
7431 6458efa5 2020-11-24 stsp
7432 a5d43cac 2022-07-01 thomas static const struct got_error *
7433 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7434 6458efa5 2020-11-24 stsp {
7435 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7436 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7437 6458efa5 2020-11-24 stsp int n = 0;
7438 6458efa5 2020-11-24 stsp
7439 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7440 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7441 6458efa5 2020-11-24 stsp else
7442 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7443 6458efa5 2020-11-24 stsp
7444 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7445 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7446 a5d43cac 2022-07-01 thomas if (last)
7447 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7448 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7449 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7450 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7451 6458efa5 2020-11-24 stsp }
7452 6458efa5 2020-11-24 stsp }
7453 a5d43cac 2022-07-01 thomas
7454 a5d43cac 2022-07-01 thomas return NULL;
7455 6458efa5 2020-11-24 stsp }
7456 6458efa5 2020-11-24 stsp
7457 6458efa5 2020-11-24 stsp static const struct got_error *
7458 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7459 6458efa5 2020-11-24 stsp {
7460 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7461 6458efa5 2020-11-24 stsp
7462 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7463 6458efa5 2020-11-24 stsp return NULL;
7464 6458efa5 2020-11-24 stsp }
7465 6458efa5 2020-11-24 stsp
7466 6458efa5 2020-11-24 stsp static int
7467 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7468 6458efa5 2020-11-24 stsp {
7469 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7470 6458efa5 2020-11-24 stsp
7471 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7472 6458efa5 2020-11-24 stsp 0) == 0;
7473 6458efa5 2020-11-24 stsp }
7474 6458efa5 2020-11-24 stsp
7475 6458efa5 2020-11-24 stsp static const struct got_error *
7476 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7477 6458efa5 2020-11-24 stsp {
7478 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7479 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7480 6458efa5 2020-11-24 stsp
7481 6458efa5 2020-11-24 stsp if (!view->searching) {
7482 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7483 6458efa5 2020-11-24 stsp return NULL;
7484 6458efa5 2020-11-24 stsp }
7485 6458efa5 2020-11-24 stsp
7486 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7487 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7488 6458efa5 2020-11-24 stsp if (s->selected_entry)
7489 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7490 6458efa5 2020-11-24 stsp else
7491 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7492 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7493 6458efa5 2020-11-24 stsp } else {
7494 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7495 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7496 6458efa5 2020-11-24 stsp else
7497 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7498 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7499 6458efa5 2020-11-24 stsp }
7500 6458efa5 2020-11-24 stsp } else {
7501 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7502 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7503 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7504 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7505 6458efa5 2020-11-24 stsp else
7506 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7507 6458efa5 2020-11-24 stsp }
7508 6458efa5 2020-11-24 stsp
7509 6458efa5 2020-11-24 stsp while (1) {
7510 6458efa5 2020-11-24 stsp if (re == NULL) {
7511 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7512 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7513 6458efa5 2020-11-24 stsp return NULL;
7514 6458efa5 2020-11-24 stsp }
7515 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7516 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7517 6458efa5 2020-11-24 stsp else
7518 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7519 6458efa5 2020-11-24 stsp }
7520 6458efa5 2020-11-24 stsp
7521 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7522 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7523 6458efa5 2020-11-24 stsp s->matched_entry = re;
7524 6458efa5 2020-11-24 stsp break;
7525 6458efa5 2020-11-24 stsp }
7526 6458efa5 2020-11-24 stsp
7527 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7528 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7529 6458efa5 2020-11-24 stsp else
7530 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7531 6458efa5 2020-11-24 stsp }
7532 6458efa5 2020-11-24 stsp
7533 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7534 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7535 6458efa5 2020-11-24 stsp s->selected = 0;
7536 6458efa5 2020-11-24 stsp }
7537 6458efa5 2020-11-24 stsp
7538 6458efa5 2020-11-24 stsp return NULL;
7539 6458efa5 2020-11-24 stsp }
7540 6458efa5 2020-11-24 stsp
7541 6458efa5 2020-11-24 stsp static const struct got_error *
7542 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7543 6458efa5 2020-11-24 stsp {
7544 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7545 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7546 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7547 6458efa5 2020-11-24 stsp char *line = NULL;
7548 6458efa5 2020-11-24 stsp wchar_t *wline;
7549 6458efa5 2020-11-24 stsp struct tog_color *tc;
7550 6458efa5 2020-11-24 stsp int width, n;
7551 6458efa5 2020-11-24 stsp int limit = view->nlines;
7552 6458efa5 2020-11-24 stsp
7553 6458efa5 2020-11-24 stsp werase(view->window);
7554 6458efa5 2020-11-24 stsp
7555 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7556 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7557 a5d43cac 2022-07-01 thomas --limit; /* border */
7558 6458efa5 2020-11-24 stsp
7559 6458efa5 2020-11-24 stsp if (limit == 0)
7560 6458efa5 2020-11-24 stsp return NULL;
7561 6458efa5 2020-11-24 stsp
7562 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7563 6458efa5 2020-11-24 stsp
7564 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7565 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7566 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7567 6458efa5 2020-11-24 stsp
7568 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7569 6458efa5 2020-11-24 stsp if (err) {
7570 6458efa5 2020-11-24 stsp free(line);
7571 6458efa5 2020-11-24 stsp return err;
7572 6458efa5 2020-11-24 stsp }
7573 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7574 6458efa5 2020-11-24 stsp wstandout(view->window);
7575 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7576 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7577 6458efa5 2020-11-24 stsp wstandend(view->window);
7578 6458efa5 2020-11-24 stsp free(wline);
7579 6458efa5 2020-11-24 stsp wline = NULL;
7580 6458efa5 2020-11-24 stsp free(line);
7581 6458efa5 2020-11-24 stsp line = NULL;
7582 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7583 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7584 6458efa5 2020-11-24 stsp if (--limit <= 0)
7585 6458efa5 2020-11-24 stsp return NULL;
7586 6458efa5 2020-11-24 stsp
7587 6458efa5 2020-11-24 stsp n = 0;
7588 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7589 6458efa5 2020-11-24 stsp char *line = NULL;
7590 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7591 6458efa5 2020-11-24 stsp
7592 84227eb1 2022-06-23 thomas if (s->show_date) {
7593 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7594 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7595 84227eb1 2022-06-23 thomas struct got_object_id *id;
7596 84227eb1 2022-06-23 thomas struct tm tm;
7597 84227eb1 2022-06-23 thomas time_t t;
7598 84227eb1 2022-06-23 thomas
7599 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7600 84227eb1 2022-06-23 thomas if (err)
7601 84227eb1 2022-06-23 thomas return err;
7602 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7603 84227eb1 2022-06-23 thomas if (err) {
7604 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7605 84227eb1 2022-06-23 thomas free(id);
7606 84227eb1 2022-06-23 thomas return err;
7607 84227eb1 2022-06-23 thomas }
7608 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7609 84227eb1 2022-06-23 thomas id);
7610 84227eb1 2022-06-23 thomas if (err) {
7611 84227eb1 2022-06-23 thomas free(id);
7612 84227eb1 2022-06-23 thomas return err;
7613 84227eb1 2022-06-23 thomas }
7614 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7615 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7616 84227eb1 2022-06-23 thomas } else {
7617 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7618 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7619 84227eb1 2022-06-23 thomas }
7620 84227eb1 2022-06-23 thomas free(id);
7621 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7622 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7623 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7624 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7625 84227eb1 2022-06-23 thomas }
7626 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7627 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7628 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7629 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7630 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7631 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7632 6458efa5 2020-11-24 stsp struct got_object_id *id;
7633 6458efa5 2020-11-24 stsp char *id_str;
7634 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7635 6458efa5 2020-11-24 stsp if (err)
7636 6458efa5 2020-11-24 stsp return err;
7637 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7638 6458efa5 2020-11-24 stsp if (err) {
7639 6458efa5 2020-11-24 stsp free(id);
7640 6458efa5 2020-11-24 stsp return err;
7641 6458efa5 2020-11-24 stsp }
7642 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7643 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7644 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7645 6458efa5 2020-11-24 stsp free(id);
7646 6458efa5 2020-11-24 stsp free(id_str);
7647 6458efa5 2020-11-24 stsp return err;
7648 6458efa5 2020-11-24 stsp }
7649 6458efa5 2020-11-24 stsp free(id);
7650 6458efa5 2020-11-24 stsp free(id_str);
7651 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7652 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7653 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7654 6458efa5 2020-11-24 stsp
7655 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7656 f91a2b48 2022-06-23 thomas 0, 0);
7657 6458efa5 2020-11-24 stsp if (err) {
7658 6458efa5 2020-11-24 stsp free(line);
7659 6458efa5 2020-11-24 stsp return err;
7660 6458efa5 2020-11-24 stsp }
7661 6458efa5 2020-11-24 stsp if (n == s->selected) {
7662 6458efa5 2020-11-24 stsp if (view->focussed)
7663 6458efa5 2020-11-24 stsp wstandout(view->window);
7664 6458efa5 2020-11-24 stsp s->selected_entry = re;
7665 6458efa5 2020-11-24 stsp }
7666 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7667 6458efa5 2020-11-24 stsp if (tc)
7668 6458efa5 2020-11-24 stsp wattr_on(view->window,
7669 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7670 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7671 6458efa5 2020-11-24 stsp if (tc)
7672 6458efa5 2020-11-24 stsp wattr_off(view->window,
7673 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7674 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7675 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7676 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7677 6458efa5 2020-11-24 stsp wstandend(view->window);
7678 6458efa5 2020-11-24 stsp free(line);
7679 6458efa5 2020-11-24 stsp free(wline);
7680 6458efa5 2020-11-24 stsp wline = NULL;
7681 6458efa5 2020-11-24 stsp n++;
7682 6458efa5 2020-11-24 stsp s->ndisplayed++;
7683 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7684 6458efa5 2020-11-24 stsp
7685 6458efa5 2020-11-24 stsp limit--;
7686 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7687 6458efa5 2020-11-24 stsp }
7688 6458efa5 2020-11-24 stsp
7689 a5d43cac 2022-07-01 thomas view_border(view);
7690 6458efa5 2020-11-24 stsp return err;
7691 6458efa5 2020-11-24 stsp }
7692 6458efa5 2020-11-24 stsp
7693 6458efa5 2020-11-24 stsp static const struct got_error *
7694 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7695 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7696 c42c9805 2020-11-24 stsp {
7697 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7698 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7699 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7700 c42c9805 2020-11-24 stsp
7701 c42c9805 2020-11-24 stsp *new_view = NULL;
7702 c42c9805 2020-11-24 stsp
7703 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7704 c42c9805 2020-11-24 stsp if (err) {
7705 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7706 c42c9805 2020-11-24 stsp return err;
7707 c42c9805 2020-11-24 stsp else
7708 c42c9805 2020-11-24 stsp return NULL;
7709 c42c9805 2020-11-24 stsp }
7710 c42c9805 2020-11-24 stsp
7711 c42c9805 2020-11-24 stsp
7712 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7713 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7714 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7715 c42c9805 2020-11-24 stsp goto done;
7716 c42c9805 2020-11-24 stsp }
7717 c42c9805 2020-11-24 stsp
7718 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7719 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7720 c42c9805 2020-11-24 stsp if (err)
7721 c42c9805 2020-11-24 stsp goto done;
7722 c42c9805 2020-11-24 stsp
7723 c42c9805 2020-11-24 stsp *new_view = tree_view;
7724 c42c9805 2020-11-24 stsp done:
7725 c42c9805 2020-11-24 stsp free(commit_id);
7726 c42c9805 2020-11-24 stsp return err;
7727 c42c9805 2020-11-24 stsp }
7728 c42c9805 2020-11-24 stsp static const struct got_error *
7729 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7730 6458efa5 2020-11-24 stsp {
7731 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7732 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7733 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7734 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7735 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7736 6458efa5 2020-11-24 stsp
7737 6458efa5 2020-11-24 stsp switch (ch) {
7738 6458efa5 2020-11-24 stsp case 'i':
7739 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7740 07b0611c 2022-06-23 thomas view->count = 0;
7741 3bfadbd4 2021-11-20 thomas break;
7742 84227eb1 2022-06-23 thomas case 'm':
7743 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7744 07b0611c 2022-06-23 thomas view->count = 0;
7745 84227eb1 2022-06-23 thomas break;
7746 98182bd0 2021-11-20 thomas case 'o':
7747 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7748 07b0611c 2022-06-23 thomas view->count = 0;
7749 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7750 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7751 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7752 c591440f 2021-11-20 thomas if (err)
7753 c591440f 2021-11-20 thomas break;
7754 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7755 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7756 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7757 3bfadbd4 2021-11-20 thomas if (err)
7758 3bfadbd4 2021-11-20 thomas break;
7759 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7760 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7761 6458efa5 2020-11-24 stsp break;
7762 6458efa5 2020-11-24 stsp case KEY_ENTER:
7763 6458efa5 2020-11-24 stsp case '\r':
7764 07b0611c 2022-06-23 thomas view->count = 0;
7765 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7766 6458efa5 2020-11-24 stsp break;
7767 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7768 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7769 a5d43cac 2022-07-01 thomas
7770 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7771 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7772 a5d43cac 2022-07-01 thomas if (err)
7773 a5d43cac 2022-07-01 thomas break;
7774 a5d43cac 2022-07-01 thomas
7775 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7776 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7777 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7778 a5d43cac 2022-07-01 thomas if (err)
7779 a5d43cac 2022-07-01 thomas break;
7780 a5d43cac 2022-07-01 thomas }
7781 a5d43cac 2022-07-01 thomas
7782 e78dc838 2020-12-04 stsp view->focussed = 0;
7783 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7784 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7785 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7786 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7787 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
7788 6458efa5 2020-11-24 stsp err = view_close_child(view);
7789 6458efa5 2020-11-24 stsp if (err)
7790 6458efa5 2020-11-24 stsp return err;
7791 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7792 40236d76 2022-06-23 thomas if (err)
7793 40236d76 2022-06-23 thomas return err;
7794 e78dc838 2020-12-04 stsp view->focus_child = 1;
7795 6458efa5 2020-11-24 stsp } else
7796 6458efa5 2020-11-24 stsp *new_view = log_view;
7797 6458efa5 2020-11-24 stsp break;
7798 c42c9805 2020-11-24 stsp case 't':
7799 07b0611c 2022-06-23 thomas view->count = 0;
7800 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7801 c42c9805 2020-11-24 stsp break;
7802 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7803 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7804 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7805 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7806 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7807 c42c9805 2020-11-24 stsp break;
7808 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7809 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7810 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7811 444d5325 2022-07-03 thomas if (err)
7812 444d5325 2022-07-03 thomas break;
7813 444d5325 2022-07-03 thomas }
7814 e78dc838 2020-12-04 stsp view->focussed = 0;
7815 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7816 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7817 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7818 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7819 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
7820 c42c9805 2020-11-24 stsp err = view_close_child(view);
7821 c42c9805 2020-11-24 stsp if (err)
7822 c42c9805 2020-11-24 stsp return err;
7823 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7824 40236d76 2022-06-23 thomas if (err)
7825 40236d76 2022-06-23 thomas return err;
7826 e78dc838 2020-12-04 stsp view->focus_child = 1;
7827 c42c9805 2020-11-24 stsp } else
7828 c42c9805 2020-11-24 stsp *new_view = tree_view;
7829 c42c9805 2020-11-24 stsp break;
7830 e4526bf5 2021-09-03 naddy case 'g':
7831 e4526bf5 2021-09-03 naddy case KEY_HOME:
7832 e4526bf5 2021-09-03 naddy s->selected = 0;
7833 07b0611c 2022-06-23 thomas view->count = 0;
7834 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7835 e4526bf5 2021-09-03 naddy break;
7836 e4526bf5 2021-09-03 naddy case 'G':
7837 a5d43cac 2022-07-01 thomas case KEY_END: {
7838 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7839 a5d43cac 2022-07-01 thomas
7840 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7841 a5d43cac 2022-07-01 thomas --eos; /* border */
7842 e4526bf5 2021-09-03 naddy s->selected = 0;
7843 07b0611c 2022-06-23 thomas view->count = 0;
7844 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7845 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7846 e4526bf5 2021-09-03 naddy if (re == NULL)
7847 e4526bf5 2021-09-03 naddy break;
7848 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7849 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7850 e4526bf5 2021-09-03 naddy }
7851 e4526bf5 2021-09-03 naddy if (n > 0)
7852 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7853 e4526bf5 2021-09-03 naddy break;
7854 a5d43cac 2022-07-01 thomas }
7855 6458efa5 2020-11-24 stsp case 'k':
7856 6458efa5 2020-11-24 stsp case KEY_UP:
7857 f7140bf5 2021-10-17 thomas case CTRL('p'):
7858 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7859 6458efa5 2020-11-24 stsp s->selected--;
7860 6458efa5 2020-11-24 stsp break;
7861 34ba6917 2020-11-30 stsp }
7862 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7863 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7864 07b0611c 2022-06-23 thomas view->count = 0;
7865 6458efa5 2020-11-24 stsp break;
7866 70f17a53 2022-06-13 thomas case CTRL('u'):
7867 23427b14 2022-06-23 thomas case 'u':
7868 70f17a53 2022-06-13 thomas nscroll /= 2;
7869 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7870 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7871 6458efa5 2020-11-24 stsp case CTRL('b'):
7872 1c5e5faa 2022-06-23 thomas case 'b':
7873 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7874 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7875 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7876 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7877 07b0611c 2022-06-23 thomas view->count = 0;
7878 6458efa5 2020-11-24 stsp break;
7879 6458efa5 2020-11-24 stsp case 'j':
7880 6458efa5 2020-11-24 stsp case KEY_DOWN:
7881 f7140bf5 2021-10-17 thomas case CTRL('n'):
7882 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7883 6458efa5 2020-11-24 stsp s->selected++;
7884 6458efa5 2020-11-24 stsp break;
7885 6458efa5 2020-11-24 stsp }
7886 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7887 6458efa5 2020-11-24 stsp /* can't scroll any further */
7888 07b0611c 2022-06-23 thomas view->count = 0;
7889 6458efa5 2020-11-24 stsp break;
7890 07b0611c 2022-06-23 thomas }
7891 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7892 6458efa5 2020-11-24 stsp break;
7893 70f17a53 2022-06-13 thomas case CTRL('d'):
7894 23427b14 2022-06-23 thomas case 'd':
7895 70f17a53 2022-06-13 thomas nscroll /= 2;
7896 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7897 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7898 6458efa5 2020-11-24 stsp case CTRL('f'):
7899 1c5e5faa 2022-06-23 thomas case 'f':
7900 4c2d69cb 2022-06-23 thomas case ' ':
7901 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7902 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7903 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7904 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7905 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7906 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7907 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7908 07b0611c 2022-06-23 thomas view->count = 0;
7909 6458efa5 2020-11-24 stsp break;
7910 6458efa5 2020-11-24 stsp }
7911 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7912 6458efa5 2020-11-24 stsp break;
7913 6458efa5 2020-11-24 stsp case CTRL('l'):
7914 07b0611c 2022-06-23 thomas view->count = 0;
7915 8924d611 2020-12-26 stsp tog_free_refs();
7916 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7917 8924d611 2020-12-26 stsp if (err)
7918 8924d611 2020-12-26 stsp break;
7919 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7920 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7921 6458efa5 2020-11-24 stsp break;
7922 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7923 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7924 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7925 6458efa5 2020-11-24 stsp break;
7926 6458efa5 2020-11-24 stsp default:
7927 07b0611c 2022-06-23 thomas view->count = 0;
7928 6458efa5 2020-11-24 stsp break;
7929 6458efa5 2020-11-24 stsp }
7930 6458efa5 2020-11-24 stsp
7931 6458efa5 2020-11-24 stsp return err;
7932 6458efa5 2020-11-24 stsp }
7933 6458efa5 2020-11-24 stsp
7934 6458efa5 2020-11-24 stsp __dead static void
7935 6458efa5 2020-11-24 stsp usage_ref(void)
7936 6458efa5 2020-11-24 stsp {
7937 6458efa5 2020-11-24 stsp endwin();
7938 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7939 6458efa5 2020-11-24 stsp getprogname());
7940 6458efa5 2020-11-24 stsp exit(1);
7941 6458efa5 2020-11-24 stsp }
7942 6458efa5 2020-11-24 stsp
7943 6458efa5 2020-11-24 stsp static const struct got_error *
7944 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7945 6458efa5 2020-11-24 stsp {
7946 6458efa5 2020-11-24 stsp const struct got_error *error;
7947 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7948 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7949 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7950 6458efa5 2020-11-24 stsp int ch;
7951 6458efa5 2020-11-24 stsp struct tog_view *view;
7952 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7953 6458efa5 2020-11-24 stsp
7954 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7955 6458efa5 2020-11-24 stsp switch (ch) {
7956 6458efa5 2020-11-24 stsp case 'r':
7957 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7958 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7959 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7960 6458efa5 2020-11-24 stsp optarg);
7961 6458efa5 2020-11-24 stsp break;
7962 6458efa5 2020-11-24 stsp default:
7963 e99e2d15 2020-11-24 naddy usage_ref();
7964 6458efa5 2020-11-24 stsp /* NOTREACHED */
7965 6458efa5 2020-11-24 stsp }
7966 6458efa5 2020-11-24 stsp }
7967 6458efa5 2020-11-24 stsp
7968 6458efa5 2020-11-24 stsp argc -= optind;
7969 6458efa5 2020-11-24 stsp argv += optind;
7970 6458efa5 2020-11-24 stsp
7971 6458efa5 2020-11-24 stsp if (argc > 1)
7972 e99e2d15 2020-11-24 naddy usage_ref();
7973 7cd52833 2022-06-23 thomas
7974 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7975 7cd52833 2022-06-23 thomas if (error != NULL)
7976 7cd52833 2022-06-23 thomas goto done;
7977 6458efa5 2020-11-24 stsp
7978 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7979 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7980 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7981 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7982 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7983 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7984 c156c7a4 2020-12-18 stsp goto done;
7985 6458efa5 2020-11-24 stsp if (worktree)
7986 6458efa5 2020-11-24 stsp repo_path =
7987 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7988 6458efa5 2020-11-24 stsp else
7989 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7990 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7991 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7992 c156c7a4 2020-12-18 stsp goto done;
7993 c156c7a4 2020-12-18 stsp }
7994 6458efa5 2020-11-24 stsp }
7995 6458efa5 2020-11-24 stsp
7996 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7997 6458efa5 2020-11-24 stsp if (error != NULL)
7998 6458efa5 2020-11-24 stsp goto done;
7999 6458efa5 2020-11-24 stsp
8000 6458efa5 2020-11-24 stsp init_curses();
8001 6458efa5 2020-11-24 stsp
8002 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8003 51a10b52 2020-12-26 stsp if (error)
8004 51a10b52 2020-12-26 stsp goto done;
8005 51a10b52 2020-12-26 stsp
8006 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8007 6458efa5 2020-11-24 stsp if (error)
8008 6458efa5 2020-11-24 stsp goto done;
8009 6458efa5 2020-11-24 stsp
8010 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8011 6458efa5 2020-11-24 stsp if (view == NULL) {
8012 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8013 6458efa5 2020-11-24 stsp goto done;
8014 6458efa5 2020-11-24 stsp }
8015 6458efa5 2020-11-24 stsp
8016 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8017 6458efa5 2020-11-24 stsp if (error)
8018 6458efa5 2020-11-24 stsp goto done;
8019 6458efa5 2020-11-24 stsp
8020 6458efa5 2020-11-24 stsp if (worktree) {
8021 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8022 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8023 6458efa5 2020-11-24 stsp worktree = NULL;
8024 6458efa5 2020-11-24 stsp }
8025 6458efa5 2020-11-24 stsp error = view_loop(view);
8026 6458efa5 2020-11-24 stsp done:
8027 6458efa5 2020-11-24 stsp free(repo_path);
8028 6458efa5 2020-11-24 stsp free(cwd);
8029 1d0f4054 2021-06-17 stsp if (repo) {
8030 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8031 1d0f4054 2021-06-17 stsp if (close_err)
8032 1d0f4054 2021-06-17 stsp error = close_err;
8033 1d0f4054 2021-06-17 stsp }
8034 7cd52833 2022-06-23 thomas if (pack_fds) {
8035 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8036 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8037 7cd52833 2022-06-23 thomas if (error == NULL)
8038 7cd52833 2022-06-23 thomas error = pack_err;
8039 7cd52833 2022-06-23 thomas }
8040 51a10b52 2020-12-26 stsp tog_free_refs();
8041 6458efa5 2020-11-24 stsp return error;
8042 6458efa5 2020-11-24 stsp }
8043 6458efa5 2020-11-24 stsp
8044 a5d43cac 2022-07-01 thomas /*
8045 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8046 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8047 a5d43cac 2022-07-01 thomas */
8048 6458efa5 2020-11-24 stsp static void
8049 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8050 a5d43cac 2022-07-01 thomas {
8051 a5d43cac 2022-07-01 thomas switch (view->type) {
8052 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8053 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8054 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8055 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8056 a5d43cac 2022-07-01 thomas 1);
8057 a5d43cac 2022-07-01 thomas break;
8058 a5d43cac 2022-07-01 thomas }
8059 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8060 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8061 a5d43cac 2022-07-01 thomas else
8062 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8063 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8064 a5d43cac 2022-07-01 thomas break;
8065 a5d43cac 2022-07-01 thomas }
8066 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8067 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8068 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8069 a5d43cac 2022-07-01 thomas break;
8070 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8071 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8072 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8073 a5d43cac 2022-07-01 thomas break;
8074 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8075 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8076 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8077 a5d43cac 2022-07-01 thomas break;
8078 a5d43cac 2022-07-01 thomas default:
8079 a5d43cac 2022-07-01 thomas break;
8080 a5d43cac 2022-07-01 thomas }
8081 a5d43cac 2022-07-01 thomas
8082 a5d43cac 2022-07-01 thomas view->offset = 0;
8083 a5d43cac 2022-07-01 thomas }
8084 a5d43cac 2022-07-01 thomas
8085 a5d43cac 2022-07-01 thomas /*
8086 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8087 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8088 a5d43cac 2022-07-01 thomas */
8089 a5d43cac 2022-07-01 thomas static const struct got_error *
8090 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8091 a5d43cac 2022-07-01 thomas {
8092 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8093 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8094 a5d43cac 2022-07-01 thomas int *selected = NULL;
8095 a5d43cac 2022-07-01 thomas int header, offset;
8096 a5d43cac 2022-07-01 thomas
8097 a5d43cac 2022-07-01 thomas switch (view->type) {
8098 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8099 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8100 a5d43cac 2022-07-01 thomas header = 3;
8101 a5d43cac 2022-07-01 thomas scrolld = NULL;
8102 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8103 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8104 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8105 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8106 a5d43cac 2022-07-01 thomas view->offset = offset;
8107 a5d43cac 2022-07-01 thomas }
8108 a5d43cac 2022-07-01 thomas break;
8109 a5d43cac 2022-07-01 thomas }
8110 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8111 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8112 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8113 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8114 a5d43cac 2022-07-01 thomas selected = &s->selected;
8115 a5d43cac 2022-07-01 thomas break;
8116 a5d43cac 2022-07-01 thomas }
8117 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8118 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8119 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8120 a5d43cac 2022-07-01 thomas header = 3;
8121 a5d43cac 2022-07-01 thomas selected = &s->selected;
8122 a5d43cac 2022-07-01 thomas break;
8123 a5d43cac 2022-07-01 thomas }
8124 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8125 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8126 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8127 a5d43cac 2022-07-01 thomas header = 5;
8128 a5d43cac 2022-07-01 thomas selected = &s->selected;
8129 a5d43cac 2022-07-01 thomas break;
8130 a5d43cac 2022-07-01 thomas }
8131 a5d43cac 2022-07-01 thomas default:
8132 a5d43cac 2022-07-01 thomas selected = NULL;
8133 a5d43cac 2022-07-01 thomas scrolld = NULL;
8134 a5d43cac 2022-07-01 thomas header = 0;
8135 a5d43cac 2022-07-01 thomas break;
8136 a5d43cac 2022-07-01 thomas }
8137 a5d43cac 2022-07-01 thomas
8138 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8139 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8140 a5d43cac 2022-07-01 thomas view->offset = offset;
8141 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8142 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8143 a5d43cac 2022-07-01 thomas *selected -= offset;
8144 a5d43cac 2022-07-01 thomas }
8145 a5d43cac 2022-07-01 thomas }
8146 a5d43cac 2022-07-01 thomas
8147 a5d43cac 2022-07-01 thomas return err;
8148 a5d43cac 2022-07-01 thomas }
8149 a5d43cac 2022-07-01 thomas
8150 a5d43cac 2022-07-01 thomas static void
8151 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8152 ce5b7c56 2019-07-09 stsp {
8153 6059809a 2020-12-17 stsp size_t i;
8154 9f7d7167 2018-04-29 stsp
8155 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8156 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8157 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8158 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8159 ce5b7c56 2019-07-09 stsp }
8160 6879ba42 2020-10-01 naddy fputc('\n', fp);
8161 ce5b7c56 2019-07-09 stsp }
8162 ce5b7c56 2019-07-09 stsp
8163 4ed7e80c 2018-05-20 stsp __dead static void
8164 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8165 9f7d7167 2018-04-29 stsp {
8166 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8167 6879ba42 2020-10-01 naddy
8168 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8169 6879ba42 2020-10-01 naddy getprogname());
8170 6879ba42 2020-10-01 naddy if (hflag) {
8171 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8172 6879ba42 2020-10-01 naddy list_commands(fp);
8173 ee85c5e8 2020-02-29 stsp }
8174 6879ba42 2020-10-01 naddy exit(status);
8175 9f7d7167 2018-04-29 stsp }
8176 9f7d7167 2018-04-29 stsp
8177 c2301be8 2018-04-30 stsp static char **
8178 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8179 c2301be8 2018-04-30 stsp {
8180 ee85c5e8 2020-02-29 stsp va_list ap;
8181 c2301be8 2018-04-30 stsp char **argv;
8182 ee85c5e8 2020-02-29 stsp int i;
8183 c2301be8 2018-04-30 stsp
8184 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8185 ee85c5e8 2020-02-29 stsp
8186 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8187 c2301be8 2018-04-30 stsp if (argv == NULL)
8188 c2301be8 2018-04-30 stsp err(1, "calloc");
8189 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8190 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8191 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8192 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8193 c2301be8 2018-04-30 stsp }
8194 c2301be8 2018-04-30 stsp
8195 ee85c5e8 2020-02-29 stsp va_end(ap);
8196 c2301be8 2018-04-30 stsp return argv;
8197 ee85c5e8 2020-02-29 stsp }
8198 ee85c5e8 2020-02-29 stsp
8199 ee85c5e8 2020-02-29 stsp /*
8200 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8201 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8202 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8203 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8204 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8205 ee85c5e8 2020-02-29 stsp */
8206 ee85c5e8 2020-02-29 stsp static const struct got_error *
8207 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8208 ee85c5e8 2020-02-29 stsp {
8209 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8210 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8211 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8212 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8213 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8214 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8215 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8216 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8217 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8218 84de9106 2020-12-26 stsp
8219 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8220 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8221 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8222 ee85c5e8 2020-02-29 stsp
8223 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8224 7cd52833 2022-06-23 thomas if (error != NULL)
8225 7cd52833 2022-06-23 thomas goto done;
8226 7cd52833 2022-06-23 thomas
8227 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8228 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8229 ee85c5e8 2020-02-29 stsp goto done;
8230 ee85c5e8 2020-02-29 stsp
8231 ee85c5e8 2020-02-29 stsp if (worktree)
8232 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8233 ee85c5e8 2020-02-29 stsp else
8234 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8235 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8236 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8237 ee85c5e8 2020-02-29 stsp goto done;
8238 ee85c5e8 2020-02-29 stsp }
8239 ee85c5e8 2020-02-29 stsp
8240 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8241 ee85c5e8 2020-02-29 stsp if (error != NULL)
8242 ee85c5e8 2020-02-29 stsp goto done;
8243 ee85c5e8 2020-02-29 stsp
8244 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8245 ee85c5e8 2020-02-29 stsp repo, worktree);
8246 ee85c5e8 2020-02-29 stsp if (error)
8247 ee85c5e8 2020-02-29 stsp goto done;
8248 ee85c5e8 2020-02-29 stsp
8249 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8250 84de9106 2020-12-26 stsp if (error)
8251 84de9106 2020-12-26 stsp goto done;
8252 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8253 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8254 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8255 ee85c5e8 2020-02-29 stsp if (error)
8256 ee85c5e8 2020-02-29 stsp goto done;
8257 ee85c5e8 2020-02-29 stsp
8258 ee85c5e8 2020-02-29 stsp if (worktree) {
8259 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8260 ee85c5e8 2020-02-29 stsp worktree = NULL;
8261 ee85c5e8 2020-02-29 stsp }
8262 ee85c5e8 2020-02-29 stsp
8263 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8264 945f9229 2022-04-16 thomas if (error)
8265 945f9229 2022-04-16 thomas goto done;
8266 945f9229 2022-04-16 thomas
8267 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8268 ee85c5e8 2020-02-29 stsp if (error) {
8269 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8270 ee85c5e8 2020-02-29 stsp goto done;
8271 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8272 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8273 6879ba42 2020-10-01 naddy usage(1, 1);
8274 ee85c5e8 2020-02-29 stsp /* not reached */
8275 ee85c5e8 2020-02-29 stsp }
8276 ee85c5e8 2020-02-29 stsp
8277 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8278 1d0f4054 2021-06-17 stsp if (error == NULL)
8279 1d0f4054 2021-06-17 stsp error = close_err;
8280 ee85c5e8 2020-02-29 stsp repo = NULL;
8281 ee85c5e8 2020-02-29 stsp
8282 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8283 ee85c5e8 2020-02-29 stsp if (error)
8284 ee85c5e8 2020-02-29 stsp goto done;
8285 ee85c5e8 2020-02-29 stsp
8286 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8287 ee85c5e8 2020-02-29 stsp argc = 4;
8288 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8289 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8290 ee85c5e8 2020-02-29 stsp done:
8291 1d0f4054 2021-06-17 stsp if (repo) {
8292 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8293 1d0f4054 2021-06-17 stsp if (error == NULL)
8294 1d0f4054 2021-06-17 stsp error = close_err;
8295 1d0f4054 2021-06-17 stsp }
8296 945f9229 2022-04-16 thomas if (commit)
8297 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8298 ee85c5e8 2020-02-29 stsp if (worktree)
8299 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8300 7cd52833 2022-06-23 thomas if (pack_fds) {
8301 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8302 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8303 7cd52833 2022-06-23 thomas if (error == NULL)
8304 7cd52833 2022-06-23 thomas error = pack_err;
8305 7cd52833 2022-06-23 thomas }
8306 ee85c5e8 2020-02-29 stsp free(id);
8307 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8308 ee85c5e8 2020-02-29 stsp free(commit_id);
8309 ee85c5e8 2020-02-29 stsp free(cwd);
8310 ee85c5e8 2020-02-29 stsp free(repo_path);
8311 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8312 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8313 ee85c5e8 2020-02-29 stsp int i;
8314 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8315 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8316 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8317 ee85c5e8 2020-02-29 stsp }
8318 87670572 2020-12-26 naddy tog_free_refs();
8319 ee85c5e8 2020-02-29 stsp return error;
8320 c2301be8 2018-04-30 stsp }
8321 c2301be8 2018-04-30 stsp
8322 9f7d7167 2018-04-29 stsp int
8323 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8324 9f7d7167 2018-04-29 stsp {
8325 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8326 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8327 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8328 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8329 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8330 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8331 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8332 83cd27f8 2020-01-13 stsp };
8333 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8334 9f7d7167 2018-04-29 stsp
8335 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8336 9f7d7167 2018-04-29 stsp
8337 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8338 9f7d7167 2018-04-29 stsp switch (ch) {
8339 9f7d7167 2018-04-29 stsp case 'h':
8340 9f7d7167 2018-04-29 stsp hflag = 1;
8341 9f7d7167 2018-04-29 stsp break;
8342 53ccebc2 2019-07-30 stsp case 'V':
8343 53ccebc2 2019-07-30 stsp Vflag = 1;
8344 53ccebc2 2019-07-30 stsp break;
8345 9f7d7167 2018-04-29 stsp default:
8346 6879ba42 2020-10-01 naddy usage(hflag, 1);
8347 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8348 9f7d7167 2018-04-29 stsp }
8349 9f7d7167 2018-04-29 stsp }
8350 9f7d7167 2018-04-29 stsp
8351 9f7d7167 2018-04-29 stsp argc -= optind;
8352 9f7d7167 2018-04-29 stsp argv += optind;
8353 9814e6a3 2020-09-27 naddy optind = 1;
8354 c2301be8 2018-04-30 stsp optreset = 1;
8355 9f7d7167 2018-04-29 stsp
8356 53ccebc2 2019-07-30 stsp if (Vflag) {
8357 53ccebc2 2019-07-30 stsp got_version_print_str();
8358 6879ba42 2020-10-01 naddy return 0;
8359 53ccebc2 2019-07-30 stsp }
8360 4010e238 2020-12-04 stsp
8361 4010e238 2020-12-04 stsp #ifndef PROFILE
8362 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8363 4010e238 2020-12-04 stsp NULL) == -1)
8364 4010e238 2020-12-04 stsp err(1, "pledge");
8365 4010e238 2020-12-04 stsp #endif
8366 53ccebc2 2019-07-30 stsp
8367 c2301be8 2018-04-30 stsp if (argc == 0) {
8368 f29d3e89 2018-06-23 stsp if (hflag)
8369 6879ba42 2020-10-01 naddy usage(hflag, 0);
8370 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8371 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8372 c2301be8 2018-04-30 stsp argc = 1;
8373 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8374 c2301be8 2018-04-30 stsp } else {
8375 6059809a 2020-12-17 stsp size_t i;
8376 9f7d7167 2018-04-29 stsp
8377 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8378 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8379 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8380 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8381 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8382 9f7d7167 2018-04-29 stsp break;
8383 9f7d7167 2018-04-29 stsp }
8384 9f7d7167 2018-04-29 stsp }
8385 ee85c5e8 2020-02-29 stsp }
8386 3642c4c6 2019-07-09 stsp
8387 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8388 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8389 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8390 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8391 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8392 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8393 adf4c9e0 2022-07-03 thomas }
8394 adf4c9e0 2022-07-03 thomas
8395 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8396 ee85c5e8 2020-02-29 stsp if (argc != 1)
8397 6879ba42 2020-10-01 naddy usage(0, 1);
8398 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8399 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8400 ee85c5e8 2020-02-29 stsp } else {
8401 ee85c5e8 2020-02-29 stsp if (hflag)
8402 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8403 ee85c5e8 2020-02-29 stsp else
8404 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8405 9f7d7167 2018-04-29 stsp }
8406 9f7d7167 2018-04-29 stsp
8407 9f7d7167 2018-04-29 stsp endwin();
8408 b46c1e04 2020-09-20 naddy putchar('\n');
8409 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8410 a2f4a359 2020-02-28 stsp int i;
8411 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8412 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8413 a2f4a359 2020-02-28 stsp free(cmd_argv);
8414 a2f4a359 2020-02-28 stsp }
8415 a2f4a359 2020-02-28 stsp
8416 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8417 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8418 9f7d7167 2018-04-29 stsp return 0;
8419 9f7d7167 2018-04-29 stsp }