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 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2314 05171be4 2022-06-23 thomas if (err)
2315 05171be4 2022-06-23 thomas goto done;
2316 05171be4 2022-06-23 thomas msg = msg0;
2317 05171be4 2022-06-23 thomas while (*msg == '\n')
2318 05171be4 2022-06-23 thomas ++msg;
2319 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2320 331b1a16 2022-06-23 thomas *eol = '\0';
2321 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2322 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2323 331b1a16 2022-06-23 thomas if (err)
2324 331b1a16 2022-06-23 thomas goto done;
2325 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2326 05171be4 2022-06-23 thomas free(msg0);
2327 331b1a16 2022-06-23 thomas free(wmsg);
2328 7ca04879 2019-10-19 stsp ncommits++;
2329 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2330 5813d178 2019-03-09 stsp }
2331 5813d178 2019-03-09 stsp
2332 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2333 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2334 867c6645 2018-07-10 stsp ncommits = 0;
2335 899d86c2 2018-05-10 stsp while (entry) {
2336 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2337 899d86c2 2018-05-10 stsp break;
2338 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2339 2814baeb 2018-08-01 stsp wstandout(view->window);
2340 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2341 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2342 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2343 2814baeb 2018-08-01 stsp wstandend(view->window);
2344 0553a4e3 2018-04-30 stsp if (err)
2345 60493ae3 2019-06-20 stsp goto done;
2346 0553a4e3 2018-04-30 stsp ncommits++;
2347 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2348 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2349 80ddbec8 2018-04-29 stsp }
2350 80ddbec8 2018-04-29 stsp
2351 a5d43cac 2022-07-01 thomas view_border(view);
2352 1a76625f 2018-10-22 stsp done:
2353 1a76625f 2018-10-22 stsp free(id_str);
2354 8b473291 2019-02-21 stsp free(refs_str);
2355 1a76625f 2018-10-22 stsp free(ncommits_str);
2356 1a76625f 2018-10-22 stsp free(header);
2357 80ddbec8 2018-04-29 stsp return err;
2358 9f7d7167 2018-04-29 stsp }
2359 07b55e75 2018-05-10 stsp
2360 07b55e75 2018-05-10 stsp static void
2361 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2362 07b55e75 2018-05-10 stsp {
2363 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2364 07b55e75 2018-05-10 stsp int nscrolled = 0;
2365 07b55e75 2018-05-10 stsp
2366 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2367 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2368 07b55e75 2018-05-10 stsp return;
2369 9f7d7167 2018-04-29 stsp
2370 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2371 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2372 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2373 07b55e75 2018-05-10 stsp if (entry) {
2374 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2375 07b55e75 2018-05-10 stsp nscrolled++;
2376 07b55e75 2018-05-10 stsp }
2377 07b55e75 2018-05-10 stsp }
2378 aa075928 2018-05-10 stsp }
2379 aa075928 2018-05-10 stsp
2380 aa075928 2018-05-10 stsp static const struct got_error *
2381 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2382 aa075928 2018-05-10 stsp {
2383 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2384 5e224a3e 2019-02-22 stsp int errcode;
2385 8a42fca8 2019-02-22 stsp
2386 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2387 aa075928 2018-05-10 stsp
2388 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2389 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2390 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2391 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2392 7aafa0d1 2019-02-22 stsp if (errcode)
2393 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2394 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2395 7c1452c1 2020-03-26 stsp
2396 7c1452c1 2020-03-26 stsp /*
2397 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2398 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2399 7c1452c1 2020-03-26 stsp */
2400 7c1452c1 2020-03-26 stsp if (!wait)
2401 7c1452c1 2020-03-26 stsp break;
2402 7c1452c1 2020-03-26 stsp
2403 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2404 ffe38506 2020-12-01 naddy show_log_view(view);
2405 7c1452c1 2020-03-26 stsp update_panels();
2406 7c1452c1 2020-03-26 stsp doupdate();
2407 7c1452c1 2020-03-26 stsp
2408 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2409 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2410 82954512 2020-02-03 stsp if (errcode)
2411 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2412 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2413 82954512 2020-02-03 stsp
2414 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2415 ffe38506 2020-12-01 naddy show_log_view(view);
2416 7c1452c1 2020-03-26 stsp update_panels();
2417 7c1452c1 2020-03-26 stsp doupdate();
2418 5e224a3e 2019-02-22 stsp }
2419 5e224a3e 2019-02-22 stsp
2420 5e224a3e 2019-02-22 stsp return NULL;
2421 a5d43cac 2022-07-01 thomas }
2422 a5d43cac 2022-07-01 thomas
2423 a5d43cac 2022-07-01 thomas static const struct got_error *
2424 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2425 a5d43cac 2022-07-01 thomas {
2426 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2427 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2428 fe731b51 2022-07-14 thomas
2429 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2430 fe731b51 2022-07-14 thomas return NULL;
2431 a5d43cac 2022-07-01 thomas
2432 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2433 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2434 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2435 a5d43cac 2022-07-01 thomas
2436 a5d43cac 2022-07-01 thomas return err;
2437 5e224a3e 2019-02-22 stsp }
2438 5e224a3e 2019-02-22 stsp
2439 5e224a3e 2019-02-22 stsp static const struct got_error *
2440 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2441 5e224a3e 2019-02-22 stsp {
2442 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2443 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2444 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2445 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2446 5e224a3e 2019-02-22 stsp
2447 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2448 5e224a3e 2019-02-22 stsp return NULL;
2449 5e224a3e 2019-02-22 stsp
2450 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2451 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2452 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2453 08ebd0a9 2019-02-22 stsp /*
2454 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2455 08ebd0a9 2019-02-22 stsp */
2456 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2457 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2458 5e224a3e 2019-02-22 stsp if (err)
2459 5e224a3e 2019-02-22 stsp return err;
2460 7aafa0d1 2019-02-22 stsp }
2461 b295e71b 2019-02-22 stsp
2462 7aafa0d1 2019-02-22 stsp do {
2463 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2464 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2465 88048b54 2019-02-21 stsp break;
2466 88048b54 2019-02-21 stsp
2467 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2468 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2469 aa075928 2018-05-10 stsp
2470 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2471 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2472 dd0a52c1 2018-05-20 stsp break;
2473 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2474 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2475 aa075928 2018-05-10 stsp
2476 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2477 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2478 a5d43cac 2022-07-01 thomas else
2479 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2480 a5d43cac 2022-07-01 thomas
2481 dd0a52c1 2018-05-20 stsp return err;
2482 07b55e75 2018-05-10 stsp }
2483 4a7f7875 2018-05-10 stsp
2484 cd0acaa7 2018-05-20 stsp static const struct got_error *
2485 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2486 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2487 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2488 cd0acaa7 2018-05-20 stsp {
2489 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2490 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2491 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2492 cd0acaa7 2018-05-20 stsp
2493 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2494 15a94983 2018-12-23 stsp if (diff_view == NULL)
2495 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2496 ea5e7bb5 2018-08-01 stsp
2497 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2498 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2499 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2500 e5a0f69f 2018-08-18 stsp if (err == NULL)
2501 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2502 cd0acaa7 2018-05-20 stsp return err;
2503 4a7f7875 2018-05-10 stsp }
2504 4a7f7875 2018-05-10 stsp
2505 80ddbec8 2018-04-29 stsp static const struct got_error *
2506 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2507 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2508 9343a5fb 2018-06-23 stsp {
2509 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2510 941e9f74 2019-05-21 stsp
2511 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2512 941e9f74 2019-05-21 stsp if (parent == NULL)
2513 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2514 941e9f74 2019-05-21 stsp
2515 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2516 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2517 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2518 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2519 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2520 941e9f74 2019-05-21 stsp s->tree = subtree;
2521 941e9f74 2019-05-21 stsp s->selected = 0;
2522 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2523 941e9f74 2019-05-21 stsp return NULL;
2524 941e9f74 2019-05-21 stsp }
2525 941e9f74 2019-05-21 stsp
2526 941e9f74 2019-05-21 stsp static const struct got_error *
2527 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2528 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2529 941e9f74 2019-05-21 stsp {
2530 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2531 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2532 941e9f74 2019-05-21 stsp const char *p;
2533 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2534 9343a5fb 2018-06-23 stsp
2535 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2536 941e9f74 2019-05-21 stsp p = path;
2537 941e9f74 2019-05-21 stsp while (*p) {
2538 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2539 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2540 56e0773d 2019-11-28 stsp char *te_name;
2541 33cbf02b 2020-01-12 stsp
2542 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2543 33cbf02b 2020-01-12 stsp p++;
2544 941e9f74 2019-05-21 stsp
2545 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2546 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2547 941e9f74 2019-05-21 stsp if (slash == NULL)
2548 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2549 33cbf02b 2020-01-12 stsp else
2550 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2551 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2552 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2553 56e0773d 2019-11-28 stsp break;
2554 941e9f74 2019-05-21 stsp }
2555 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2556 56e0773d 2019-11-28 stsp if (te == NULL) {
2557 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2558 56e0773d 2019-11-28 stsp free(te_name);
2559 941e9f74 2019-05-21 stsp break;
2560 941e9f74 2019-05-21 stsp }
2561 56e0773d 2019-11-28 stsp free(te_name);
2562 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2563 941e9f74 2019-05-21 stsp
2564 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2565 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2566 b03c880f 2019-05-21 stsp
2567 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2568 941e9f74 2019-05-21 stsp if (slash)
2569 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2570 941e9f74 2019-05-21 stsp else
2571 941e9f74 2019-05-21 stsp subpath = strdup(path);
2572 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2573 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2574 941e9f74 2019-05-21 stsp break;
2575 941e9f74 2019-05-21 stsp }
2576 941e9f74 2019-05-21 stsp
2577 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2578 941e9f74 2019-05-21 stsp subpath);
2579 941e9f74 2019-05-21 stsp if (err)
2580 941e9f74 2019-05-21 stsp break;
2581 941e9f74 2019-05-21 stsp
2582 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2583 941e9f74 2019-05-21 stsp free(tree_id);
2584 941e9f74 2019-05-21 stsp if (err)
2585 941e9f74 2019-05-21 stsp break;
2586 941e9f74 2019-05-21 stsp
2587 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2588 941e9f74 2019-05-21 stsp if (err) {
2589 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2590 941e9f74 2019-05-21 stsp break;
2591 941e9f74 2019-05-21 stsp }
2592 941e9f74 2019-05-21 stsp if (slash == NULL)
2593 941e9f74 2019-05-21 stsp break;
2594 941e9f74 2019-05-21 stsp free(subpath);
2595 941e9f74 2019-05-21 stsp subpath = NULL;
2596 941e9f74 2019-05-21 stsp p = slash;
2597 941e9f74 2019-05-21 stsp }
2598 941e9f74 2019-05-21 stsp
2599 941e9f74 2019-05-21 stsp free(subpath);
2600 1a76625f 2018-10-22 stsp return err;
2601 61266923 2020-01-14 stsp }
2602 61266923 2020-01-14 stsp
2603 61266923 2020-01-14 stsp static const struct got_error *
2604 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2605 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2606 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2607 55cccc34 2020-02-20 stsp {
2608 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2609 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2610 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2611 55cccc34 2020-02-20 stsp
2612 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2613 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2614 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2615 55cccc34 2020-02-20 stsp
2616 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2617 bc573f3b 2021-07-10 stsp if (err)
2618 55cccc34 2020-02-20 stsp return err;
2619 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2620 55cccc34 2020-02-20 stsp
2621 55cccc34 2020-02-20 stsp *new_view = tree_view;
2622 55cccc34 2020-02-20 stsp
2623 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2624 55cccc34 2020-02-20 stsp return NULL;
2625 55cccc34 2020-02-20 stsp
2626 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2627 55cccc34 2020-02-20 stsp }
2628 55cccc34 2020-02-20 stsp
2629 55cccc34 2020-02-20 stsp static const struct got_error *
2630 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2631 61266923 2020-01-14 stsp {
2632 61266923 2020-01-14 stsp sigset_t sigset;
2633 61266923 2020-01-14 stsp int errcode;
2634 61266923 2020-01-14 stsp
2635 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2636 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2637 61266923 2020-01-14 stsp
2638 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2639 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2640 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2641 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2642 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2643 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2644 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2645 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2646 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2647 61266923 2020-01-14 stsp
2648 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2649 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2650 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2651 61266923 2020-01-14 stsp
2652 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2653 61266923 2020-01-14 stsp if (errcode)
2654 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2655 61266923 2020-01-14 stsp
2656 61266923 2020-01-14 stsp return NULL;
2657 1a76625f 2018-10-22 stsp }
2658 1a76625f 2018-10-22 stsp
2659 1a76625f 2018-10-22 stsp static void *
2660 1a76625f 2018-10-22 stsp log_thread(void *arg)
2661 1a76625f 2018-10-22 stsp {
2662 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2663 1a76625f 2018-10-22 stsp int errcode = 0;
2664 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2665 1a76625f 2018-10-22 stsp int done = 0;
2666 61266923 2020-01-14 stsp
2667 f2d749db 2022-07-12 thomas /*
2668 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2669 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2670 f2d749db 2022-07-12 thomas */
2671 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2672 f2d749db 2022-07-12 thomas if (errcode) {
2673 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2674 61266923 2020-01-14 stsp return (void *)err;
2675 f2d749db 2022-07-12 thomas }
2676 1a76625f 2018-10-22 stsp
2677 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2678 f2d749db 2022-07-12 thomas if (err) {
2679 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2680 f2d749db 2022-07-12 thomas goto done;
2681 f2d749db 2022-07-12 thomas }
2682 f2d749db 2022-07-12 thomas
2683 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2684 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2685 f2d749db 2022-07-12 thomas if (errcode) {
2686 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2687 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2688 f2d749db 2022-07-12 thomas goto done;
2689 f2d749db 2022-07-12 thomas }
2690 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2691 1a76625f 2018-10-22 stsp if (err) {
2692 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2693 f2d749db 2022-07-12 thomas goto done;
2694 1a76625f 2018-10-22 stsp err = NULL;
2695 1a76625f 2018-10-22 stsp done = 1;
2696 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2697 1a76625f 2018-10-22 stsp a->commits_needed--;
2698 1a76625f 2018-10-22 stsp
2699 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2700 3abe8080 2019-04-10 stsp if (errcode) {
2701 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2702 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2703 f2d749db 2022-07-12 thomas goto done;
2704 3abe8080 2019-04-10 stsp } else if (*a->quit)
2705 1a76625f 2018-10-22 stsp done = 1;
2706 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2707 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2708 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2709 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2710 1a76625f 2018-10-22 stsp }
2711 1a76625f 2018-10-22 stsp
2712 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2713 7c1452c1 2020-03-26 stsp if (errcode) {
2714 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2715 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2716 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2717 f2d749db 2022-07-12 thomas goto done;
2718 7c1452c1 2020-03-26 stsp }
2719 7c1452c1 2020-03-26 stsp
2720 1a76625f 2018-10-22 stsp if (done)
2721 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2722 7c1452c1 2020-03-26 stsp else {
2723 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2724 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2725 7c1452c1 2020-03-26 stsp &tog_mutex);
2726 f2d749db 2022-07-12 thomas if (errcode) {
2727 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2728 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2729 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2730 f2d749db 2022-07-12 thomas goto done;
2731 f2d749db 2022-07-12 thomas }
2732 21355643 2020-12-06 stsp if (*a->quit)
2733 21355643 2020-12-06 stsp done = 1;
2734 7c1452c1 2020-03-26 stsp }
2735 1a76625f 2018-10-22 stsp }
2736 1a76625f 2018-10-22 stsp }
2737 3abe8080 2019-04-10 stsp a->log_complete = 1;
2738 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2739 f2d749db 2022-07-12 thomas if (errcode)
2740 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2741 f2d749db 2022-07-12 thomas done:
2742 f2d749db 2022-07-12 thomas if (err) {
2743 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2744 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2745 f2d749db 2022-07-12 thomas }
2746 1a76625f 2018-10-22 stsp return (void *)err;
2747 1a76625f 2018-10-22 stsp }
2748 1a76625f 2018-10-22 stsp
2749 1a76625f 2018-10-22 stsp static const struct got_error *
2750 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2751 1a76625f 2018-10-22 stsp {
2752 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2753 1a76625f 2018-10-22 stsp int errcode;
2754 1a76625f 2018-10-22 stsp
2755 1a76625f 2018-10-22 stsp if (s->thread) {
2756 1a76625f 2018-10-22 stsp s->quit = 1;
2757 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2758 1a76625f 2018-10-22 stsp if (errcode)
2759 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2760 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2761 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2762 1a76625f 2018-10-22 stsp if (errcode)
2763 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2764 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2765 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2766 1a76625f 2018-10-22 stsp if (errcode)
2767 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2768 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2769 1a76625f 2018-10-22 stsp if (errcode)
2770 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2771 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2772 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2773 1a76625f 2018-10-22 stsp }
2774 1a76625f 2018-10-22 stsp
2775 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2776 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2777 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2778 1af5eddf 2022-06-23 thomas }
2779 1af5eddf 2022-06-23 thomas
2780 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2781 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2782 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2783 1af5eddf 2022-06-23 thomas if (err == NULL)
2784 1af5eddf 2022-06-23 thomas err = pack_err;
2785 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2786 1a76625f 2018-10-22 stsp }
2787 1a76625f 2018-10-22 stsp
2788 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2789 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2790 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2791 1a76625f 2018-10-22 stsp }
2792 1a76625f 2018-10-22 stsp
2793 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2794 9343a5fb 2018-06-23 stsp }
2795 9343a5fb 2018-06-23 stsp
2796 9343a5fb 2018-06-23 stsp static const struct got_error *
2797 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2798 1a76625f 2018-10-22 stsp {
2799 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2800 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2801 276b94a1 2020-11-13 naddy int errcode;
2802 1a76625f 2018-10-22 stsp
2803 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2804 276b94a1 2020-11-13 naddy
2805 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2806 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2807 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2808 276b94a1 2020-11-13 naddy
2809 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2810 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2811 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2812 276b94a1 2020-11-13 naddy
2813 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2814 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2815 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2816 1a76625f 2018-10-22 stsp free(s->start_id);
2817 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2818 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2819 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2820 1a76625f 2018-10-22 stsp return err;
2821 1a76625f 2018-10-22 stsp }
2822 1a76625f 2018-10-22 stsp
2823 1a76625f 2018-10-22 stsp static const struct got_error *
2824 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2825 60493ae3 2019-06-20 stsp {
2826 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2827 60493ae3 2019-06-20 stsp
2828 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2829 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2830 60493ae3 2019-06-20 stsp return NULL;
2831 60493ae3 2019-06-20 stsp }
2832 60493ae3 2019-06-20 stsp
2833 60493ae3 2019-06-20 stsp static const struct got_error *
2834 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2835 60493ae3 2019-06-20 stsp {
2836 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2837 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2838 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2839 60493ae3 2019-06-20 stsp
2840 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2841 f9686aa5 2020-03-27 stsp show_log_view(view);
2842 f9686aa5 2020-03-27 stsp update_panels();
2843 f9686aa5 2020-03-27 stsp doupdate();
2844 f9686aa5 2020-03-27 stsp
2845 96e2b566 2019-07-08 stsp if (s->search_entry) {
2846 13add988 2019-10-15 stsp int errcode, ch;
2847 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2848 13add988 2019-10-15 stsp if (errcode)
2849 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2850 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2851 13add988 2019-10-15 stsp ch = wgetch(view->window);
2852 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2853 13add988 2019-10-15 stsp if (errcode)
2854 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2855 13add988 2019-10-15 stsp "pthread_mutex_lock");
2856 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2857 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2858 678cbce5 2019-07-28 stsp return NULL;
2859 678cbce5 2019-07-28 stsp }
2860 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2861 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2862 96e2b566 2019-07-08 stsp else
2863 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2864 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2865 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2866 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2867 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2868 df5e841d 2022-06-23 thomas
2869 df5e841d 2022-06-23 thomas /*
2870 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2871 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2872 1f4d5162 2022-06-23 thomas * might have changed.
2873 df5e841d 2022-06-23 thomas */
2874 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2875 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2876 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2877 df5e841d 2022-06-23 thomas else
2878 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2879 b74feda6 2022-06-23 thomas } else {
2880 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2881 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2882 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2883 df5e841d 2022-06-23 thomas else
2884 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2885 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2886 b74feda6 2022-06-23 thomas }
2887 20be8d96 2019-06-21 stsp } else {
2888 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2889 20be8d96 2019-06-21 stsp }
2890 60493ae3 2019-06-20 stsp
2891 60493ae3 2019-06-20 stsp while (1) {
2892 13add988 2019-10-15 stsp int have_match = 0;
2893 13add988 2019-10-15 stsp
2894 60493ae3 2019-06-20 stsp if (entry == NULL) {
2895 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2896 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2897 f9967bca 2020-03-27 stsp view->search_next_done =
2898 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2899 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2900 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2901 f801134a 2019-06-25 stsp return NULL;
2902 60493ae3 2019-06-20 stsp }
2903 96e2b566 2019-07-08 stsp /*
2904 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2905 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2906 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2907 96e2b566 2019-07-08 stsp */
2908 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2909 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2910 60493ae3 2019-06-20 stsp }
2911 60493ae3 2019-06-20 stsp
2912 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2913 13add988 2019-10-15 stsp &view->regex);
2914 5943eee2 2019-08-13 stsp if (err)
2915 13add988 2019-10-15 stsp break;
2916 13add988 2019-10-15 stsp if (have_match) {
2917 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2918 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2919 60493ae3 2019-06-20 stsp break;
2920 60493ae3 2019-06-20 stsp }
2921 13add988 2019-10-15 stsp
2922 96e2b566 2019-07-08 stsp s->search_entry = entry;
2923 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2924 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2925 b1bf1435 2019-06-21 stsp else
2926 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2927 60493ae3 2019-06-20 stsp }
2928 60493ae3 2019-06-20 stsp
2929 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2930 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2931 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2932 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2933 60493ae3 2019-06-20 stsp if (err)
2934 60493ae3 2019-06-20 stsp return err;
2935 ead14cbe 2019-06-21 stsp cur++;
2936 ead14cbe 2019-06-21 stsp }
2937 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2938 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2939 60493ae3 2019-06-20 stsp if (err)
2940 60493ae3 2019-06-20 stsp return err;
2941 ead14cbe 2019-06-21 stsp cur--;
2942 60493ae3 2019-06-20 stsp }
2943 60493ae3 2019-06-20 stsp }
2944 60493ae3 2019-06-20 stsp
2945 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2946 96e2b566 2019-07-08 stsp
2947 60493ae3 2019-06-20 stsp return NULL;
2948 60493ae3 2019-06-20 stsp }
2949 60493ae3 2019-06-20 stsp
2950 60493ae3 2019-06-20 stsp static const struct got_error *
2951 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2952 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2953 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2954 80ddbec8 2018-04-29 stsp {
2955 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2956 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2957 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2958 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2959 1a76625f 2018-10-22 stsp int errcode;
2960 80ddbec8 2018-04-29 stsp
2961 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2962 f135c941 2020-02-20 stsp free(s->in_repo_path);
2963 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2964 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2965 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2966 f135c941 2020-02-20 stsp }
2967 ecb28ae0 2018-07-16 stsp
2968 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2969 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2970 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2971 78756c87 2020-11-24 stsp
2972 fb2756b9 2018-08-04 stsp s->repo = repo;
2973 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2974 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2975 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2976 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2977 9cd7cbd1 2020-12-07 stsp goto done;
2978 9cd7cbd1 2020-12-07 stsp }
2979 9cd7cbd1 2020-12-07 stsp }
2980 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2981 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2982 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2983 5036bf37 2018-09-24 stsp goto done;
2984 5036bf37 2018-09-24 stsp }
2985 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2986 e5a0f69f 2018-08-18 stsp
2987 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2988 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2989 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2990 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2991 11b20872 2019-11-08 stsp if (err)
2992 11b20872 2019-11-08 stsp goto done;
2993 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2994 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2995 11b20872 2019-11-08 stsp if (err) {
2996 11b20872 2019-11-08 stsp free_colors(&s->colors);
2997 11b20872 2019-11-08 stsp goto done;
2998 11b20872 2019-11-08 stsp }
2999 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3000 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3001 11b20872 2019-11-08 stsp if (err) {
3002 11b20872 2019-11-08 stsp free_colors(&s->colors);
3003 11b20872 2019-11-08 stsp goto done;
3004 11b20872 2019-11-08 stsp }
3005 11b20872 2019-11-08 stsp }
3006 11b20872 2019-11-08 stsp
3007 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3008 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3009 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3010 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3011 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3012 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3013 1a76625f 2018-10-22 stsp
3014 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3015 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3016 1af5eddf 2022-06-23 thomas if (err)
3017 1af5eddf 2022-06-23 thomas goto done;
3018 1af5eddf 2022-06-23 thomas }
3019 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3020 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3021 7cd52833 2022-06-23 thomas if (err)
3022 7cd52833 2022-06-23 thomas goto done;
3023 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3024 b672a97a 2020-01-27 stsp !s->log_branches);
3025 1a76625f 2018-10-22 stsp if (err)
3026 1a76625f 2018-10-22 stsp goto done;
3027 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3028 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3029 c5b78334 2020-01-12 stsp if (err)
3030 c5b78334 2020-01-12 stsp goto done;
3031 1a76625f 2018-10-22 stsp
3032 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3033 1a76625f 2018-10-22 stsp if (errcode) {
3034 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3035 1a76625f 2018-10-22 stsp goto done;
3036 1a76625f 2018-10-22 stsp }
3037 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3038 7c1452c1 2020-03-26 stsp if (errcode) {
3039 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3040 7c1452c1 2020-03-26 stsp goto done;
3041 7c1452c1 2020-03-26 stsp }
3042 1a76625f 2018-10-22 stsp
3043 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3044 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3045 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3046 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3047 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3048 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3049 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3050 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3051 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3052 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3053 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3054 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3055 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3056 ba4f502b 2018-08-04 stsp done:
3057 1a76625f 2018-10-22 stsp if (err)
3058 1a76625f 2018-10-22 stsp close_log_view(view);
3059 ba4f502b 2018-08-04 stsp return err;
3060 ba4f502b 2018-08-04 stsp }
3061 ba4f502b 2018-08-04 stsp
3062 e5a0f69f 2018-08-18 stsp static const struct got_error *
3063 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3064 ba4f502b 2018-08-04 stsp {
3065 f2f6d207 2020-11-24 stsp const struct got_error *err;
3066 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3067 ba4f502b 2018-08-04 stsp
3068 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3069 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3070 2b380cc8 2018-10-24 stsp &s->thread_args);
3071 2b380cc8 2018-10-24 stsp if (errcode)
3072 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3073 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3074 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3075 f2f6d207 2020-11-24 stsp if (err)
3076 f2f6d207 2020-11-24 stsp return err;
3077 f2f6d207 2020-11-24 stsp }
3078 2b380cc8 2018-10-24 stsp }
3079 2b380cc8 2018-10-24 stsp
3080 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3081 b31f89ff 2022-07-01 thomas }
3082 b31f89ff 2022-07-01 thomas
3083 b31f89ff 2022-07-01 thomas static void
3084 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3085 b31f89ff 2022-07-01 thomas {
3086 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3087 b31f89ff 2022-07-01 thomas
3088 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3089 b31f89ff 2022-07-01 thomas view->count = 0;
3090 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3091 b31f89ff 2022-07-01 thomas return;
3092 b31f89ff 2022-07-01 thomas
3093 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3094 b31f89ff 2022-07-01 thomas || home)
3095 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3096 b31f89ff 2022-07-01 thomas
3097 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3098 b31f89ff 2022-07-01 thomas --s->selected;
3099 b31f89ff 2022-07-01 thomas else
3100 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3101 b31f89ff 2022-07-01 thomas
3102 b31f89ff 2022-07-01 thomas select_commit(s);
3103 b31f89ff 2022-07-01 thomas return;
3104 b31f89ff 2022-07-01 thomas }
3105 b31f89ff 2022-07-01 thomas
3106 b31f89ff 2022-07-01 thomas static const struct got_error *
3107 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3108 b31f89ff 2022-07-01 thomas {
3109 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3110 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3111 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3112 b31f89ff 2022-07-01 thomas
3113 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3114 b31f89ff 2022-07-01 thomas if (first == NULL) {
3115 b31f89ff 2022-07-01 thomas view->count = 0;
3116 b31f89ff 2022-07-01 thomas return NULL;
3117 b31f89ff 2022-07-01 thomas }
3118 b31f89ff 2022-07-01 thomas
3119 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3120 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3121 b31f89ff 2022-07-01 thomas return NULL;
3122 b31f89ff 2022-07-01 thomas
3123 b31f89ff 2022-07-01 thomas if (!page) {
3124 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3125 b31f89ff 2022-07-01 thomas
3126 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3127 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3128 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3129 b31f89ff 2022-07-01 thomas ++s->selected;
3130 b31f89ff 2022-07-01 thomas else
3131 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3132 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3133 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3134 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3135 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3136 b31f89ff 2022-07-01 thomas else
3137 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3138 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3139 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3140 b31f89ff 2022-07-01 thomas } else {
3141 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3142 b31f89ff 2022-07-01 thomas if (err)
3143 b31f89ff 2022-07-01 thomas return err;
3144 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3145 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3146 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3147 b31f89ff 2022-07-01 thomas }
3148 b31f89ff 2022-07-01 thomas }
3149 b31f89ff 2022-07-01 thomas if (err)
3150 b31f89ff 2022-07-01 thomas return err;
3151 b31f89ff 2022-07-01 thomas
3152 a5d43cac 2022-07-01 thomas /*
3153 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3154 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3155 a5d43cac 2022-07-01 thomas */
3156 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3157 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3158 a5d43cac 2022-07-01 thomas
3159 b31f89ff 2022-07-01 thomas select_commit(s);
3160 b31f89ff 2022-07-01 thomas
3161 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3162 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3163 b31f89ff 2022-07-01 thomas view->count = 0;
3164 b31f89ff 2022-07-01 thomas
3165 b31f89ff 2022-07-01 thomas return NULL;
3166 e5a0f69f 2018-08-18 stsp }
3167 04cc582a 2018-08-01 stsp
3168 a5d43cac 2022-07-01 thomas static void
3169 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3170 a5d43cac 2022-07-01 thomas {
3171 24415785 2022-07-03 thomas *x = 0;
3172 24415785 2022-07-03 thomas *y = 0;
3173 24415785 2022-07-03 thomas
3174 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3175 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3176 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3177 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3178 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3179 53d2bdd3 2022-07-10 thomas else
3180 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3181 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3182 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3183 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3184 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3185 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3186 53d2bdd3 2022-07-10 thomas else
3187 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3188 53d2bdd3 2022-07-10 thomas }
3189 a5d43cac 2022-07-01 thomas }
3190 a5d43cac 2022-07-01 thomas
3191 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3192 e5a0f69f 2018-08-18 stsp static const struct got_error *
3193 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3194 a5d43cac 2022-07-01 thomas {
3195 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3196 a5d43cac 2022-07-01 thomas
3197 a5d43cac 2022-07-01 thomas view->nlines = y;
3198 64486692 2022-07-07 thomas view->ncols = COLS;
3199 a5d43cac 2022-07-01 thomas err = view_resize(view);
3200 a5d43cac 2022-07-01 thomas if (err)
3201 a5d43cac 2022-07-01 thomas return err;
3202 a5d43cac 2022-07-01 thomas
3203 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3204 a5d43cac 2022-07-01 thomas
3205 a5d43cac 2022-07-01 thomas return err;
3206 a5d43cac 2022-07-01 thomas }
3207 a5d43cac 2022-07-01 thomas
3208 a5d43cac 2022-07-01 thomas static const struct got_error *
3209 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3210 e5a0f69f 2018-08-18 stsp {
3211 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3212 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3213 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3214 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3215 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3216 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
3217 80ddbec8 2018-04-29 stsp
3218 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3219 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3220 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3221 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3222 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3223 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3224 fb280deb 2021-08-30 stsp }
3225 b31f89ff 2022-07-01 thomas return err;
3226 528dedf3 2021-08-30 stsp }
3227 068ab281 2022-07-01 thomas
3228 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3229 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3230 068ab281 2022-07-01 thomas --eos; /* border */
3231 068ab281 2022-07-01 thomas
3232 528dedf3 2021-08-30 stsp switch (ch) {
3233 1e37a5c2 2019-05-12 jcs case 'q':
3234 1e37a5c2 2019-05-12 jcs s->quit = 1;
3235 05171be4 2022-06-23 thomas break;
3236 05171be4 2022-06-23 thomas case '0':
3237 05171be4 2022-06-23 thomas view->x = 0;
3238 05171be4 2022-06-23 thomas break;
3239 05171be4 2022-06-23 thomas case '$':
3240 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3241 07b0611c 2022-06-23 thomas view->count = 0;
3242 05171be4 2022-06-23 thomas break;
3243 05171be4 2022-06-23 thomas case KEY_RIGHT:
3244 05171be4 2022-06-23 thomas case 'l':
3245 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3246 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3247 07b0611c 2022-06-23 thomas else
3248 07b0611c 2022-06-23 thomas view->count = 0;
3249 1e37a5c2 2019-05-12 jcs break;
3250 05171be4 2022-06-23 thomas case KEY_LEFT:
3251 05171be4 2022-06-23 thomas case 'h':
3252 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3253 07b0611c 2022-06-23 thomas if (view->x <= 0)
3254 07b0611c 2022-06-23 thomas view->count = 0;
3255 05171be4 2022-06-23 thomas break;
3256 1e37a5c2 2019-05-12 jcs case 'k':
3257 1e37a5c2 2019-05-12 jcs case KEY_UP:
3258 1e37a5c2 2019-05-12 jcs case '<':
3259 1e37a5c2 2019-05-12 jcs case ',':
3260 f7140bf5 2021-10-17 thomas case CTRL('p'):
3261 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3262 912a3f79 2021-08-30 j break;
3263 912a3f79 2021-08-30 j case 'g':
3264 912a3f79 2021-08-30 j case KEY_HOME:
3265 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3266 07b0611c 2022-06-23 thomas view->count = 0;
3267 1e37a5c2 2019-05-12 jcs break;
3268 70f17a53 2022-06-13 thomas case CTRL('u'):
3269 23427b14 2022-06-23 thomas case 'u':
3270 70f17a53 2022-06-13 thomas nscroll /= 2;
3271 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3272 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3273 a4292ac5 2019-05-12 jcs case CTRL('b'):
3274 1c5e5faa 2022-06-23 thomas case 'b':
3275 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3276 1e37a5c2 2019-05-12 jcs break;
3277 1e37a5c2 2019-05-12 jcs case 'j':
3278 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3279 1e37a5c2 2019-05-12 jcs case '>':
3280 1e37a5c2 2019-05-12 jcs case '.':
3281 f7140bf5 2021-10-17 thomas case CTRL('n'):
3282 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3283 912a3f79 2021-08-30 j break;
3284 f69c5a46 2022-07-19 thomas case '@':
3285 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3286 f69c5a46 2022-07-19 thomas break;
3287 912a3f79 2021-08-30 j case 'G':
3288 912a3f79 2021-08-30 j case KEY_END: {
3289 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3290 912a3f79 2021-08-30 j * traverse them all. */
3291 07b0611c 2022-06-23 thomas view->count = 0;
3292 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3293 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3294 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3295 80ddbec8 2018-04-29 stsp }
3296 912a3f79 2021-08-30 j
3297 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3298 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3299 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3300 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3301 f3bc9f1d 2021-09-05 naddy break;
3302 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3303 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3304 f3bc9f1d 2021-09-05 naddy }
3305 f3bc9f1d 2021-09-05 naddy if (n > 0)
3306 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3307 2b779855 2020-12-05 naddy select_commit(s);
3308 1e37a5c2 2019-05-12 jcs break;
3309 912a3f79 2021-08-30 j }
3310 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3311 23427b14 2022-06-23 thomas case 'd':
3312 70f17a53 2022-06-13 thomas nscroll /= 2;
3313 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3314 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3315 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3316 4c2d69cb 2022-06-23 thomas case 'f':
3317 b31f89ff 2022-07-01 thomas case ' ':
3318 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3319 1e37a5c2 2019-05-12 jcs break;
3320 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3321 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3322 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3323 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3324 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3325 2b779855 2020-12-05 naddy select_commit(s);
3326 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3327 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3328 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3329 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3330 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3331 0bf7f153 2020-12-02 naddy }
3332 1e37a5c2 2019-05-12 jcs break;
3333 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3334 444d5325 2022-07-03 thomas case '\r':
3335 07b0611c 2022-06-23 thomas view->count = 0;
3336 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3337 e5a0f69f 2018-08-18 stsp break;
3338 a5d43cac 2022-07-01 thomas
3339 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3340 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3341 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3342 a5d43cac 2022-07-01 thomas
3343 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3344 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3345 78756c87 2020-11-24 stsp view, s->repo);
3346 1e37a5c2 2019-05-12 jcs if (err)
3347 1e37a5c2 2019-05-12 jcs break;
3348 a5d43cac 2022-07-01 thomas
3349 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3350 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3351 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3352 a5d43cac 2022-07-01 thomas if (err)
3353 a5d43cac 2022-07-01 thomas break;
3354 a5d43cac 2022-07-01 thomas }
3355 a5d43cac 2022-07-01 thomas
3356 e78dc838 2020-12-04 stsp view->focussed = 0;
3357 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3358 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3359 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3360 a5d43cac 2022-07-01 thomas
3361 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3362 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
3363 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3364 f7013a22 2018-10-24 stsp if (err)
3365 1e37a5c2 2019-05-12 jcs return err;
3366 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3367 40236d76 2022-06-23 thomas if (err)
3368 40236d76 2022-06-23 thomas return err;
3369 e78dc838 2020-12-04 stsp view->focus_child = 1;
3370 1e37a5c2 2019-05-12 jcs } else
3371 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3372 1e37a5c2 2019-05-12 jcs break;
3373 1e37a5c2 2019-05-12 jcs case 't':
3374 07b0611c 2022-06-23 thomas view->count = 0;
3375 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3376 5036bf37 2018-09-24 stsp break;
3377 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3378 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3379 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3380 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3381 4e97c21c 2020-12-06 stsp s->repo);
3382 1e37a5c2 2019-05-12 jcs if (err)
3383 e5a0f69f 2018-08-18 stsp break;
3384 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3385 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3386 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3387 444d5325 2022-07-03 thomas if (err)
3388 444d5325 2022-07-03 thomas break;
3389 444d5325 2022-07-03 thomas }
3390 e78dc838 2020-12-04 stsp view->focussed = 0;
3391 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3392 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3393 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3394 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3395 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
3396 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3397 1e37a5c2 2019-05-12 jcs if (err)
3398 1e37a5c2 2019-05-12 jcs return err;
3399 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3400 40236d76 2022-06-23 thomas if (err)
3401 40236d76 2022-06-23 thomas return err;
3402 e78dc838 2020-12-04 stsp view->focus_child = 1;
3403 1e37a5c2 2019-05-12 jcs } else
3404 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3405 1e37a5c2 2019-05-12 jcs break;
3406 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3407 21355643 2020-12-06 stsp case CTRL('l'):
3408 21355643 2020-12-06 stsp case 'B':
3409 07b0611c 2022-06-23 thomas view->count = 0;
3410 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3411 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3412 1e37a5c2 2019-05-12 jcs break;
3413 21355643 2020-12-06 stsp err = stop_log_thread(s);
3414 74cfe85e 2020-10-20 stsp if (err)
3415 74cfe85e 2020-10-20 stsp return err;
3416 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3417 21355643 2020-12-06 stsp char *parent_path;
3418 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3419 21355643 2020-12-06 stsp if (err)
3420 21355643 2020-12-06 stsp return err;
3421 21355643 2020-12-06 stsp free(s->in_repo_path);
3422 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3423 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3424 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3425 21355643 2020-12-06 stsp struct got_object_id *start_id;
3426 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3427 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3428 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3429 21355643 2020-12-06 stsp if (err)
3430 21355643 2020-12-06 stsp return err;
3431 21355643 2020-12-06 stsp free(s->start_id);
3432 21355643 2020-12-06 stsp s->start_id = start_id;
3433 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3434 21355643 2020-12-06 stsp } else /* 'B' */
3435 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3436 21355643 2020-12-06 stsp
3437 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3438 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3439 bf7e79b3 2022-06-23 thomas if (err)
3440 bf7e79b3 2022-06-23 thomas return err;
3441 bf7e79b3 2022-06-23 thomas }
3442 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3443 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3444 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3445 74cfe85e 2020-10-20 stsp if (err)
3446 21355643 2020-12-06 stsp return err;
3447 51a10b52 2020-12-26 stsp tog_free_refs();
3448 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3449 ca51c541 2020-12-07 stsp if (err)
3450 ca51c541 2020-12-07 stsp return err;
3451 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3452 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3453 d01904d4 2019-06-25 stsp if (err)
3454 d01904d4 2019-06-25 stsp return err;
3455 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3456 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3457 b672a97a 2020-01-27 stsp if (err)
3458 b672a97a 2020-01-27 stsp return err;
3459 21355643 2020-12-06 stsp free_commits(&s->commits);
3460 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3461 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3462 21355643 2020-12-06 stsp s->selected_entry = NULL;
3463 21355643 2020-12-06 stsp s->selected = 0;
3464 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3465 21355643 2020-12-06 stsp s->quit = 0;
3466 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3467 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3468 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3469 d01904d4 2019-06-25 stsp break;
3470 6458efa5 2020-11-24 stsp case 'r':
3471 07b0611c 2022-06-23 thomas view->count = 0;
3472 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3473 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3474 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3475 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3476 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3477 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3478 6458efa5 2020-11-24 stsp if (err) {
3479 6458efa5 2020-11-24 stsp view_close(ref_view);
3480 6458efa5 2020-11-24 stsp return err;
3481 6458efa5 2020-11-24 stsp }
3482 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3483 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3484 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3485 444d5325 2022-07-03 thomas if (err)
3486 444d5325 2022-07-03 thomas break;
3487 444d5325 2022-07-03 thomas }
3488 e78dc838 2020-12-04 stsp view->focussed = 0;
3489 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3490 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3491 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3492 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3493 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
3494 6458efa5 2020-11-24 stsp err = view_close_child(view);
3495 6458efa5 2020-11-24 stsp if (err)
3496 6458efa5 2020-11-24 stsp return err;
3497 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3498 40236d76 2022-06-23 thomas if (err)
3499 40236d76 2022-06-23 thomas return err;
3500 e78dc838 2020-12-04 stsp view->focus_child = 1;
3501 6458efa5 2020-11-24 stsp } else
3502 6458efa5 2020-11-24 stsp *new_view = ref_view;
3503 6458efa5 2020-11-24 stsp break;
3504 1e37a5c2 2019-05-12 jcs default:
3505 07b0611c 2022-06-23 thomas view->count = 0;
3506 1e37a5c2 2019-05-12 jcs break;
3507 899d86c2 2018-05-10 stsp }
3508 e5a0f69f 2018-08-18 stsp
3509 80ddbec8 2018-04-29 stsp return err;
3510 80ddbec8 2018-04-29 stsp }
3511 80ddbec8 2018-04-29 stsp
3512 4ed7e80c 2018-05-20 stsp static const struct got_error *
3513 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3514 c2db6724 2019-01-04 stsp {
3515 c2db6724 2019-01-04 stsp const struct got_error *error;
3516 c2db6724 2019-01-04 stsp
3517 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3518 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3519 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3520 37c06ea4 2019-07-15 stsp #endif
3521 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3522 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3523 c2db6724 2019-01-04 stsp
3524 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3525 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3526 c2db6724 2019-01-04 stsp
3527 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3528 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3529 c2db6724 2019-01-04 stsp
3530 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3531 c2db6724 2019-01-04 stsp if (error != NULL)
3532 c2db6724 2019-01-04 stsp return error;
3533 c2db6724 2019-01-04 stsp
3534 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3535 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3536 c2db6724 2019-01-04 stsp
3537 c2db6724 2019-01-04 stsp return NULL;
3538 c2db6724 2019-01-04 stsp }
3539 c2db6724 2019-01-04 stsp
3540 a915003a 2019-02-05 stsp static void
3541 a915003a 2019-02-05 stsp init_curses(void)
3542 a915003a 2019-02-05 stsp {
3543 296152d1 2022-05-31 thomas /*
3544 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3545 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3546 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3547 296152d1 2022-05-31 thomas */
3548 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3549 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3550 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3551 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3552 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3553 296152d1 2022-05-31 thomas
3554 a915003a 2019-02-05 stsp initscr();
3555 a915003a 2019-02-05 stsp cbreak();
3556 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3557 a915003a 2019-02-05 stsp noecho();
3558 a915003a 2019-02-05 stsp nonl();
3559 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3560 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3561 a915003a 2019-02-05 stsp curs_set(0);
3562 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3563 6d17833f 2019-11-08 stsp start_color();
3564 6d17833f 2019-11-08 stsp use_default_colors();
3565 6d17833f 2019-11-08 stsp }
3566 a915003a 2019-02-05 stsp }
3567 a915003a 2019-02-05 stsp
3568 c2db6724 2019-01-04 stsp static const struct got_error *
3569 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3570 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3571 f135c941 2020-02-20 stsp {
3572 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3573 f135c941 2020-02-20 stsp
3574 f135c941 2020-02-20 stsp if (argc == 0) {
3575 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3576 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3577 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3578 f135c941 2020-02-20 stsp return NULL;
3579 f135c941 2020-02-20 stsp }
3580 f135c941 2020-02-20 stsp
3581 f135c941 2020-02-20 stsp if (worktree) {
3582 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3583 bfd61697 2020-11-14 stsp char *p;
3584 f135c941 2020-02-20 stsp
3585 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3586 f135c941 2020-02-20 stsp if (err)
3587 f135c941 2020-02-20 stsp return err;
3588 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3589 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3590 bfd61697 2020-11-14 stsp p) == -1) {
3591 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3592 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3593 f135c941 2020-02-20 stsp }
3594 f135c941 2020-02-20 stsp free(p);
3595 f135c941 2020-02-20 stsp } else
3596 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3597 f135c941 2020-02-20 stsp
3598 f135c941 2020-02-20 stsp return err;
3599 f135c941 2020-02-20 stsp }
3600 f135c941 2020-02-20 stsp
3601 f135c941 2020-02-20 stsp static const struct got_error *
3602 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3603 9f7d7167 2018-04-29 stsp {
3604 80ddbec8 2018-04-29 stsp const struct got_error *error;
3605 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3606 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3607 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3608 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3609 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3610 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3611 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3612 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3613 04cc582a 2018-08-01 stsp struct tog_view *view;
3614 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3615 80ddbec8 2018-04-29 stsp
3616 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3617 80ddbec8 2018-04-29 stsp switch (ch) {
3618 b672a97a 2020-01-27 stsp case 'b':
3619 b672a97a 2020-01-27 stsp log_branches = 1;
3620 b672a97a 2020-01-27 stsp break;
3621 80ddbec8 2018-04-29 stsp case 'c':
3622 80ddbec8 2018-04-29 stsp start_commit = optarg;
3623 80ddbec8 2018-04-29 stsp break;
3624 ecb28ae0 2018-07-16 stsp case 'r':
3625 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3626 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3627 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3628 9ba1d308 2019-10-21 stsp optarg);
3629 ecb28ae0 2018-07-16 stsp break;
3630 80ddbec8 2018-04-29 stsp default:
3631 17020d27 2019-03-07 stsp usage_log();
3632 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3633 80ddbec8 2018-04-29 stsp }
3634 80ddbec8 2018-04-29 stsp }
3635 80ddbec8 2018-04-29 stsp
3636 80ddbec8 2018-04-29 stsp argc -= optind;
3637 80ddbec8 2018-04-29 stsp argv += optind;
3638 80ddbec8 2018-04-29 stsp
3639 f135c941 2020-02-20 stsp if (argc > 1)
3640 f135c941 2020-02-20 stsp usage_log();
3641 963f97a1 2019-03-18 stsp
3642 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3643 7cd52833 2022-06-23 thomas if (error != NULL)
3644 7cd52833 2022-06-23 thomas goto done;
3645 7cd52833 2022-06-23 thomas
3646 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3647 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3648 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3649 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3650 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3651 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3652 c156c7a4 2020-12-18 stsp goto done;
3653 a1fbf39a 2019-08-11 stsp if (worktree)
3654 6962eb72 2020-02-20 stsp repo_path =
3655 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3656 a1fbf39a 2019-08-11 stsp else
3657 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3658 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3659 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3660 c156c7a4 2020-12-18 stsp goto done;
3661 c156c7a4 2020-12-18 stsp }
3662 ecb28ae0 2018-07-16 stsp }
3663 ecb28ae0 2018-07-16 stsp
3664 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3665 80ddbec8 2018-04-29 stsp if (error != NULL)
3666 ecb28ae0 2018-07-16 stsp goto done;
3667 80ddbec8 2018-04-29 stsp
3668 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3669 f135c941 2020-02-20 stsp repo, worktree);
3670 f135c941 2020-02-20 stsp if (error)
3671 f135c941 2020-02-20 stsp goto done;
3672 f135c941 2020-02-20 stsp
3673 f135c941 2020-02-20 stsp init_curses();
3674 f135c941 2020-02-20 stsp
3675 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3676 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3677 c02c541e 2019-03-29 stsp if (error)
3678 c02c541e 2019-03-29 stsp goto done;
3679 c02c541e 2019-03-29 stsp
3680 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3681 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3682 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3683 87670572 2020-12-26 naddy if (error)
3684 87670572 2020-12-26 naddy goto done;
3685 87670572 2020-12-26 naddy }
3686 51a10b52 2020-12-26 stsp
3687 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3688 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3689 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3690 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3691 d8f38dc4 2020-12-05 stsp if (error)
3692 d8f38dc4 2020-12-05 stsp goto done;
3693 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3694 d8f38dc4 2020-12-05 stsp } else {
3695 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3696 d8f38dc4 2020-12-05 stsp if (error == NULL)
3697 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3698 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3699 d8f38dc4 2020-12-05 stsp goto done;
3700 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3701 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3702 d8f38dc4 2020-12-05 stsp if (error)
3703 d8f38dc4 2020-12-05 stsp goto done;
3704 d8f38dc4 2020-12-05 stsp }
3705 8b473291 2019-02-21 stsp
3706 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3707 04cc582a 2018-08-01 stsp if (view == NULL) {
3708 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3709 04cc582a 2018-08-01 stsp goto done;
3710 04cc582a 2018-08-01 stsp }
3711 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3712 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3713 ba4f502b 2018-08-04 stsp if (error)
3714 ba4f502b 2018-08-04 stsp goto done;
3715 2fc00ff4 2019-08-31 stsp if (worktree) {
3716 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3717 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3718 2fc00ff4 2019-08-31 stsp worktree = NULL;
3719 2fc00ff4 2019-08-31 stsp }
3720 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3721 ecb28ae0 2018-07-16 stsp done:
3722 f135c941 2020-02-20 stsp free(in_repo_path);
3723 ecb28ae0 2018-07-16 stsp free(repo_path);
3724 ecb28ae0 2018-07-16 stsp free(cwd);
3725 899d86c2 2018-05-10 stsp free(start_id);
3726 d8f38dc4 2020-12-05 stsp free(label);
3727 d8f38dc4 2020-12-05 stsp if (ref)
3728 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3729 1d0f4054 2021-06-17 stsp if (repo) {
3730 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3731 1d0f4054 2021-06-17 stsp if (error == NULL)
3732 1d0f4054 2021-06-17 stsp error = close_err;
3733 1d0f4054 2021-06-17 stsp }
3734 ec142235 2019-03-07 stsp if (worktree)
3735 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3736 7cd52833 2022-06-23 thomas if (pack_fds) {
3737 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3738 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3739 7cd52833 2022-06-23 thomas if (error == NULL)
3740 7cd52833 2022-06-23 thomas error = pack_err;
3741 7cd52833 2022-06-23 thomas }
3742 51a10b52 2020-12-26 stsp tog_free_refs();
3743 80ddbec8 2018-04-29 stsp return error;
3744 9f7d7167 2018-04-29 stsp }
3745 9f7d7167 2018-04-29 stsp
3746 4ed7e80c 2018-05-20 stsp __dead static void
3747 9f7d7167 2018-04-29 stsp usage_diff(void)
3748 9f7d7167 2018-04-29 stsp {
3749 80ddbec8 2018-04-29 stsp endwin();
3750 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3751 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3752 9f7d7167 2018-04-29 stsp exit(1);
3753 b304db33 2018-05-20 stsp }
3754 b304db33 2018-05-20 stsp
3755 6d17833f 2019-11-08 stsp static int
3756 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3757 41605754 2020-11-12 stsp regmatch_t *regmatch)
3758 6d17833f 2019-11-08 stsp {
3759 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3760 6d17833f 2019-11-08 stsp }
3761 6d17833f 2019-11-08 stsp
3762 ef20f542 2022-06-26 thomas static struct tog_color *
3763 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3764 6d17833f 2019-11-08 stsp {
3765 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3766 6d17833f 2019-11-08 stsp
3767 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3768 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3769 6d17833f 2019-11-08 stsp return tc;
3770 6d17833f 2019-11-08 stsp }
3771 6d17833f 2019-11-08 stsp
3772 6d17833f 2019-11-08 stsp return NULL;
3773 6d17833f 2019-11-08 stsp }
3774 6d17833f 2019-11-08 stsp
3775 4ed7e80c 2018-05-20 stsp static const struct got_error *
3776 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3777 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3778 41605754 2020-11-12 stsp {
3779 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3780 666f7b10 2022-06-23 thomas char *exstr = NULL;
3781 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3782 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3783 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3784 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3785 41605754 2020-11-12 stsp
3786 41605754 2020-11-12 stsp *wtotal = 0;
3787 cbea3800 2022-06-23 thomas
3788 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3789 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3790 41605754 2020-11-12 stsp
3791 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3792 666f7b10 2022-06-23 thomas if (err)
3793 666f7b10 2022-06-23 thomas return err;
3794 666f7b10 2022-06-23 thomas
3795 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3796 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3797 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3798 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3799 666f7b10 2022-06-23 thomas goto done;
3800 666f7b10 2022-06-23 thomas }
3801 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3802 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3803 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3804 cbea3800 2022-06-23 thomas goto done;
3805 cbea3800 2022-06-23 thomas }
3806 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3807 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3808 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3809 cbea3800 2022-06-23 thomas goto done;
3810 cbea3800 2022-06-23 thomas }
3811 05171be4 2022-06-23 thomas
3812 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3813 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3814 cbea3800 2022-06-23 thomas col_tab_align, 1);
3815 cbea3800 2022-06-23 thomas if (err)
3816 cbea3800 2022-06-23 thomas goto done;
3817 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3818 05171be4 2022-06-23 thomas if (n) {
3819 cbea3800 2022-06-23 thomas free(wline);
3820 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3821 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3822 cbea3800 2022-06-23 thomas if (err)
3823 cbea3800 2022-06-23 thomas goto done;
3824 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3825 cbea3800 2022-06-23 thomas wlimit -= width;
3826 cbea3800 2022-06-23 thomas *wtotal += width;
3827 41605754 2020-11-12 stsp }
3828 41605754 2020-11-12 stsp
3829 41605754 2020-11-12 stsp if (wlimit > 0) {
3830 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3831 cbea3800 2022-06-23 thomas size_t wlen;
3832 cbea3800 2022-06-23 thomas
3833 cbea3800 2022-06-23 thomas free(wline);
3834 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3835 cbea3800 2022-06-23 thomas col_tab_align, 1);
3836 cbea3800 2022-06-23 thomas if (err)
3837 cbea3800 2022-06-23 thomas goto done;
3838 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3839 cbea3800 2022-06-23 thomas while (i < wlen) {
3840 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3841 cbea3800 2022-06-23 thomas if (width == -1) {
3842 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3843 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3844 cbea3800 2022-06-23 thomas goto done;
3845 cbea3800 2022-06-23 thomas }
3846 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3847 cbea3800 2022-06-23 thomas break;
3848 1c5e5faa 2022-06-23 thomas w += width;
3849 cbea3800 2022-06-23 thomas i++;
3850 41605754 2020-11-12 stsp }
3851 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3852 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3853 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3854 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3855 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3856 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3857 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3858 41605754 2020-11-12 stsp }
3859 41605754 2020-11-12 stsp }
3860 41605754 2020-11-12 stsp
3861 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3862 cbea3800 2022-06-23 thomas free(wline);
3863 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3864 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3865 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3866 cbea3800 2022-06-23 thomas col_tab_align, 1);
3867 cbea3800 2022-06-23 thomas if (err)
3868 cbea3800 2022-06-23 thomas goto done;
3869 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3870 cbea3800 2022-06-23 thomas } else {
3871 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3872 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3873 cbea3800 2022-06-23 thomas if (err)
3874 cbea3800 2022-06-23 thomas goto done;
3875 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3876 cbea3800 2022-06-23 thomas }
3877 cbea3800 2022-06-23 thomas *wtotal += width2;
3878 41605754 2020-11-12 stsp }
3879 cbea3800 2022-06-23 thomas done:
3880 05171be4 2022-06-23 thomas free(wline);
3881 666f7b10 2022-06-23 thomas free(exstr);
3882 cbea3800 2022-06-23 thomas free(seg0);
3883 cbea3800 2022-06-23 thomas free(seg1);
3884 cbea3800 2022-06-23 thomas free(seg2);
3885 cbea3800 2022-06-23 thomas return err;
3886 41605754 2020-11-12 stsp }
3887 41605754 2020-11-12 stsp
3888 41605754 2020-11-12 stsp static const struct got_error *
3889 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3890 26ed57b2 2018-05-19 stsp {
3891 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3892 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3893 61e69b96 2018-05-20 stsp const struct got_error *err;
3894 fe621944 2020-11-10 stsp int nprinted = 0;
3895 b304db33 2018-05-20 stsp char *line;
3896 826082fe 2020-12-10 stsp size_t linesize = 0;
3897 826082fe 2020-12-10 stsp ssize_t linelen;
3898 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3899 61e69b96 2018-05-20 stsp wchar_t *wline;
3900 e0b650dd 2018-05-20 stsp int width;
3901 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3902 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3903 fe621944 2020-11-10 stsp off_t line_offset;
3904 26ed57b2 2018-05-19 stsp
3905 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3906 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3907 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3908 fe621944 2020-11-10 stsp
3909 f7d12f7e 2018-08-01 stsp werase(view->window);
3910 a3404814 2018-09-02 stsp
3911 a3404814 2018-09-02 stsp if (header) {
3912 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3913 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3914 135a2da0 2020-11-11 stsp header) == -1)
3915 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3916 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3917 f91a2b48 2022-06-23 thomas 0, 0);
3918 135a2da0 2020-11-11 stsp free(line);
3919 135a2da0 2020-11-11 stsp if (err)
3920 a3404814 2018-09-02 stsp return err;
3921 a3404814 2018-09-02 stsp
3922 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3923 a3404814 2018-09-02 stsp wstandout(view->window);
3924 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3925 e54cc94a 2020-11-11 stsp free(wline);
3926 e54cc94a 2020-11-11 stsp wline = NULL;
3927 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3928 a3404814 2018-09-02 stsp wstandend(view->window);
3929 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3930 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3931 26ed57b2 2018-05-19 stsp
3932 a3404814 2018-09-02 stsp if (max_lines <= 1)
3933 a3404814 2018-09-02 stsp return NULL;
3934 a3404814 2018-09-02 stsp max_lines--;
3935 a3404814 2018-09-02 stsp }
3936 a3404814 2018-09-02 stsp
3937 89f1a395 2020-12-01 naddy s->eof = 0;
3938 05171be4 2022-06-23 thomas view->maxx = 0;
3939 826082fe 2020-12-10 stsp line = NULL;
3940 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3941 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3942 826082fe 2020-12-10 stsp if (linelen == -1) {
3943 826082fe 2020-12-10 stsp if (feof(s->f)) {
3944 826082fe 2020-12-10 stsp s->eof = 1;
3945 826082fe 2020-12-10 stsp break;
3946 826082fe 2020-12-10 stsp }
3947 826082fe 2020-12-10 stsp free(line);
3948 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3949 61e69b96 2018-05-20 stsp }
3950 05171be4 2022-06-23 thomas
3951 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3952 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3953 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3954 cbea3800 2022-06-23 thomas if (err) {
3955 cbea3800 2022-06-23 thomas free(line);
3956 cbea3800 2022-06-23 thomas return err;
3957 cbea3800 2022-06-23 thomas }
3958 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3959 cbea3800 2022-06-23 thomas free(wline);
3960 cbea3800 2022-06-23 thomas wline = NULL;
3961 cbea3800 2022-06-23 thomas
3962 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3963 f26dddb7 2019-11-08 stsp if (tc)
3964 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3965 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3966 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3967 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3968 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3969 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3970 41605754 2020-11-12 stsp if (err) {
3971 41605754 2020-11-12 stsp free(line);
3972 41605754 2020-11-12 stsp return err;
3973 41605754 2020-11-12 stsp }
3974 41605754 2020-11-12 stsp } else {
3975 f1c0ec19 2022-06-23 thomas int skip;
3976 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3977 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3978 f1c0ec19 2022-06-23 thomas if (err) {
3979 f1c0ec19 2022-06-23 thomas free(line);
3980 f1c0ec19 2022-06-23 thomas return err;
3981 f1c0ec19 2022-06-23 thomas }
3982 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3983 f1c0ec19 2022-06-23 thomas free(wline);
3984 41605754 2020-11-12 stsp wline = NULL;
3985 41605754 2020-11-12 stsp }
3986 f26dddb7 2019-11-08 stsp if (tc)
3987 6d17833f 2019-11-08 stsp wattr_off(view->window,
3988 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3989 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3990 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3991 fe621944 2020-11-10 stsp nprinted++;
3992 826082fe 2020-12-10 stsp }
3993 826082fe 2020-12-10 stsp free(line);
3994 fe621944 2020-11-10 stsp if (nprinted >= 1)
3995 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3996 89f1a395 2020-12-01 naddy (nprinted - 1);
3997 fe621944 2020-11-10 stsp else
3998 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3999 26ed57b2 2018-05-19 stsp
4000 a5d43cac 2022-07-01 thomas view_border(view);
4001 c3e9aa98 2019-05-13 jcs
4002 89f1a395 2020-12-01 naddy if (s->eof) {
4003 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4004 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4005 c3e9aa98 2019-05-13 jcs nprinted++;
4006 c3e9aa98 2019-05-13 jcs }
4007 c3e9aa98 2019-05-13 jcs
4008 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4009 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4010 c3e9aa98 2019-05-13 jcs if (err) {
4011 c3e9aa98 2019-05-13 jcs return err;
4012 c3e9aa98 2019-05-13 jcs }
4013 26ed57b2 2018-05-19 stsp
4014 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4015 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4016 e54cc94a 2020-11-11 stsp free(wline);
4017 e54cc94a 2020-11-11 stsp wline = NULL;
4018 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4019 c3e9aa98 2019-05-13 jcs }
4020 c3e9aa98 2019-05-13 jcs
4021 26ed57b2 2018-05-19 stsp return NULL;
4022 abd2672a 2018-12-23 stsp }
4023 abd2672a 2018-12-23 stsp
4024 abd2672a 2018-12-23 stsp static char *
4025 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4026 abd2672a 2018-12-23 stsp {
4027 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4028 09867e48 2019-08-13 stsp char *p, *s;
4029 09867e48 2019-08-13 stsp
4030 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4031 09867e48 2019-08-13 stsp if (tm == NULL)
4032 09867e48 2019-08-13 stsp return NULL;
4033 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4034 09867e48 2019-08-13 stsp if (s == NULL)
4035 09867e48 2019-08-13 stsp return NULL;
4036 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4037 abd2672a 2018-12-23 stsp if (p)
4038 abd2672a 2018-12-23 stsp *p = '\0';
4039 abd2672a 2018-12-23 stsp return s;
4040 9f7d7167 2018-04-29 stsp }
4041 9f7d7167 2018-04-29 stsp
4042 4ed7e80c 2018-05-20 stsp static const struct got_error *
4043 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4044 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4045 0208f208 2020-05-05 stsp {
4046 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4047 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4048 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4049 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4050 0208f208 2020-05-05 stsp
4051 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4052 0208f208 2020-05-05 stsp if (qid != NULL) {
4053 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4054 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4055 ec242592 2022-04-22 thomas &qid->id);
4056 0208f208 2020-05-05 stsp if (err)
4057 0208f208 2020-05-05 stsp return err;
4058 0208f208 2020-05-05 stsp
4059 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4060 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4061 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4062 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4063 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4064 aa8b5dd0 2021-08-01 stsp }
4065 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4066 0208f208 2020-05-05 stsp
4067 0208f208 2020-05-05 stsp }
4068 0208f208 2020-05-05 stsp
4069 0208f208 2020-05-05 stsp if (tree_id1) {
4070 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4071 0208f208 2020-05-05 stsp if (err)
4072 0208f208 2020-05-05 stsp goto done;
4073 0208f208 2020-05-05 stsp }
4074 0208f208 2020-05-05 stsp
4075 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4076 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4077 0208f208 2020-05-05 stsp if (err)
4078 0208f208 2020-05-05 stsp goto done;
4079 0208f208 2020-05-05 stsp
4080 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4081 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4082 0208f208 2020-05-05 stsp done:
4083 0208f208 2020-05-05 stsp if (tree1)
4084 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4085 0208f208 2020-05-05 stsp if (tree2)
4086 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4087 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4088 0208f208 2020-05-05 stsp return err;
4089 0208f208 2020-05-05 stsp }
4090 0208f208 2020-05-05 stsp
4091 0208f208 2020-05-05 stsp static const struct got_error *
4092 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4093 abd2672a 2018-12-23 stsp {
4094 fe621944 2020-11-10 stsp off_t *p;
4095 fe621944 2020-11-10 stsp
4096 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4097 fe621944 2020-11-10 stsp if (p == NULL)
4098 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4099 fe621944 2020-11-10 stsp *line_offsets = p;
4100 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4101 fe621944 2020-11-10 stsp (*nlines)++;
4102 fe621944 2020-11-10 stsp return NULL;
4103 fe621944 2020-11-10 stsp }
4104 fe621944 2020-11-10 stsp
4105 fe621944 2020-11-10 stsp static const struct got_error *
4106 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4107 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4108 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4109 fe621944 2020-11-10 stsp {
4110 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4111 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4112 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4113 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4114 45d799e2 2018-12-23 stsp time_t committer_time;
4115 45d799e2 2018-12-23 stsp const char *author, *committer;
4116 8b473291 2019-02-21 stsp char *refs_str = NULL;
4117 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4118 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4119 fe621944 2020-11-10 stsp off_t outoff = 0;
4120 fe621944 2020-11-10 stsp int n;
4121 abd2672a 2018-12-23 stsp
4122 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4123 0208f208 2020-05-05 stsp
4124 8b473291 2019-02-21 stsp if (refs) {
4125 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4126 8b473291 2019-02-21 stsp if (err)
4127 8b473291 2019-02-21 stsp return err;
4128 8b473291 2019-02-21 stsp }
4129 8b473291 2019-02-21 stsp
4130 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4131 abd2672a 2018-12-23 stsp if (err)
4132 abd2672a 2018-12-23 stsp return err;
4133 abd2672a 2018-12-23 stsp
4134 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4135 15a94983 2018-12-23 stsp if (err) {
4136 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4137 15a94983 2018-12-23 stsp goto done;
4138 15a94983 2018-12-23 stsp }
4139 abd2672a 2018-12-23 stsp
4140 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4141 fe621944 2020-11-10 stsp if (err)
4142 fe621944 2020-11-10 stsp goto done;
4143 fe621944 2020-11-10 stsp
4144 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4145 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4146 fe621944 2020-11-10 stsp if (n < 0) {
4147 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4148 abd2672a 2018-12-23 stsp goto done;
4149 abd2672a 2018-12-23 stsp }
4150 fe621944 2020-11-10 stsp outoff += n;
4151 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4152 fe621944 2020-11-10 stsp if (err)
4153 fe621944 2020-11-10 stsp goto done;
4154 fe621944 2020-11-10 stsp
4155 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4156 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4157 fe621944 2020-11-10 stsp if (n < 0) {
4158 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4159 abd2672a 2018-12-23 stsp goto done;
4160 abd2672a 2018-12-23 stsp }
4161 fe621944 2020-11-10 stsp outoff += n;
4162 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4163 fe621944 2020-11-10 stsp if (err)
4164 fe621944 2020-11-10 stsp goto done;
4165 fe621944 2020-11-10 stsp
4166 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4167 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4168 fe621944 2020-11-10 stsp if (datestr) {
4169 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4170 fe621944 2020-11-10 stsp if (n < 0) {
4171 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4172 fe621944 2020-11-10 stsp goto done;
4173 fe621944 2020-11-10 stsp }
4174 fe621944 2020-11-10 stsp outoff += n;
4175 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4176 fe621944 2020-11-10 stsp if (err)
4177 fe621944 2020-11-10 stsp goto done;
4178 abd2672a 2018-12-23 stsp }
4179 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4180 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4181 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4182 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4183 fe621944 2020-11-10 stsp if (n < 0) {
4184 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4185 fe621944 2020-11-10 stsp goto done;
4186 fe621944 2020-11-10 stsp }
4187 fe621944 2020-11-10 stsp outoff += n;
4188 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4189 fe621944 2020-11-10 stsp if (err)
4190 fe621944 2020-11-10 stsp goto done;
4191 abd2672a 2018-12-23 stsp }
4192 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4193 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4194 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4195 ac4dc263 2021-09-24 thomas int pn = 1;
4196 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4197 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4198 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4199 ac4dc263 2021-09-24 thomas if (err)
4200 ac4dc263 2021-09-24 thomas goto done;
4201 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4202 ac4dc263 2021-09-24 thomas if (n < 0) {
4203 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4204 ac4dc263 2021-09-24 thomas goto done;
4205 ac4dc263 2021-09-24 thomas }
4206 ac4dc263 2021-09-24 thomas outoff += n;
4207 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4208 ac4dc263 2021-09-24 thomas if (err)
4209 ac4dc263 2021-09-24 thomas goto done;
4210 ac4dc263 2021-09-24 thomas free(id_str);
4211 ac4dc263 2021-09-24 thomas id_str = NULL;
4212 ac4dc263 2021-09-24 thomas }
4213 ac4dc263 2021-09-24 thomas }
4214 ac4dc263 2021-09-24 thomas
4215 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4216 5943eee2 2019-08-13 stsp if (err)
4217 5943eee2 2019-08-13 stsp goto done;
4218 fe621944 2020-11-10 stsp s = logmsg;
4219 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4220 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4221 fe621944 2020-11-10 stsp if (n < 0) {
4222 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4223 fe621944 2020-11-10 stsp goto done;
4224 fe621944 2020-11-10 stsp }
4225 fe621944 2020-11-10 stsp outoff += n;
4226 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4227 fe621944 2020-11-10 stsp if (err)
4228 fe621944 2020-11-10 stsp goto done;
4229 abd2672a 2018-12-23 stsp }
4230 fe621944 2020-11-10 stsp
4231 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4232 0208f208 2020-05-05 stsp if (err)
4233 0208f208 2020-05-05 stsp goto done;
4234 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4235 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4236 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4237 fe621944 2020-11-10 stsp if (n < 0) {
4238 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4239 fe621944 2020-11-10 stsp goto done;
4240 fe621944 2020-11-10 stsp }
4241 fe621944 2020-11-10 stsp outoff += n;
4242 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4243 fe621944 2020-11-10 stsp if (err)
4244 fe621944 2020-11-10 stsp goto done;
4245 0208f208 2020-05-05 stsp free((char *)pe->path);
4246 0208f208 2020-05-05 stsp free(pe->data);
4247 0208f208 2020-05-05 stsp }
4248 fe621944 2020-11-10 stsp
4249 0208f208 2020-05-05 stsp fputc('\n', outfile);
4250 fe621944 2020-11-10 stsp outoff++;
4251 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4252 abd2672a 2018-12-23 stsp done:
4253 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4254 abd2672a 2018-12-23 stsp free(id_str);
4255 5943eee2 2019-08-13 stsp free(logmsg);
4256 8b473291 2019-02-21 stsp free(refs_str);
4257 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4258 7510f233 2020-08-09 stsp if (err) {
4259 ae6a6978 2020-08-09 stsp free(*line_offsets);
4260 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4261 ae6a6978 2020-08-09 stsp *nlines = 0;
4262 7510f233 2020-08-09 stsp }
4263 fe621944 2020-11-10 stsp return err;
4264 abd2672a 2018-12-23 stsp }
4265 abd2672a 2018-12-23 stsp
4266 abd2672a 2018-12-23 stsp static const struct got_error *
4267 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4268 26ed57b2 2018-05-19 stsp {
4269 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4270 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4271 15a94983 2018-12-23 stsp int obj_type;
4272 fe621944 2020-11-10 stsp
4273 fe621944 2020-11-10 stsp free(s->line_offsets);
4274 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4275 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4276 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4277 fe621944 2020-11-10 stsp s->nlines = 0;
4278 26ed57b2 2018-05-19 stsp
4279 511a516b 2018-05-19 stsp f = got_opentemp();
4280 48ae06ee 2018-10-18 stsp if (f == NULL) {
4281 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4282 48ae06ee 2018-10-18 stsp goto done;
4283 48ae06ee 2018-10-18 stsp }
4284 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4285 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4286 fb43ecf1 2019-02-11 stsp goto done;
4287 fb43ecf1 2019-02-11 stsp }
4288 48ae06ee 2018-10-18 stsp s->f = f;
4289 26ed57b2 2018-05-19 stsp
4290 15a94983 2018-12-23 stsp if (s->id1)
4291 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4292 15a94983 2018-12-23 stsp else
4293 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4294 15a94983 2018-12-23 stsp if (err)
4295 15a94983 2018-12-23 stsp goto done;
4296 15a94983 2018-12-23 stsp
4297 15a94983 2018-12-23 stsp switch (obj_type) {
4298 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4299 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4300 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4301 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4302 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4303 26ed57b2 2018-05-19 stsp break;
4304 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4305 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4306 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4307 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4308 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4309 26ed57b2 2018-05-19 stsp break;
4310 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4311 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4312 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4313 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4314 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4315 abd2672a 2018-12-23 stsp
4316 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4317 abd2672a 2018-12-23 stsp if (err)
4318 3ffacbe1 2020-02-02 tracey goto done;
4319 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4320 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4321 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4322 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4323 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4324 f44b1f58 2020-02-02 tracey if (err)
4325 f44b1f58 2020-02-02 tracey goto done;
4326 f44b1f58 2020-02-02 tracey } else {
4327 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4328 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4329 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4330 fe621944 2020-11-10 stsp err = write_commit_info(
4331 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4332 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4333 f44b1f58 2020-02-02 tracey if (err)
4334 f44b1f58 2020-02-02 tracey goto done;
4335 f5404e4e 2020-02-02 tracey break;
4336 15a087fe 2019-02-21 stsp }
4337 abd2672a 2018-12-23 stsp }
4338 abd2672a 2018-12-23 stsp }
4339 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4340 abd2672a 2018-12-23 stsp
4341 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4342 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4343 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4344 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4345 26ed57b2 2018-05-19 stsp break;
4346 abd2672a 2018-12-23 stsp }
4347 26ed57b2 2018-05-19 stsp default:
4348 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4349 48ae06ee 2018-10-18 stsp break;
4350 26ed57b2 2018-05-19 stsp }
4351 f44b1f58 2020-02-02 tracey if (err)
4352 f44b1f58 2020-02-02 tracey goto done;
4353 48ae06ee 2018-10-18 stsp done:
4354 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4355 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4356 48ae06ee 2018-10-18 stsp return err;
4357 48ae06ee 2018-10-18 stsp }
4358 26ed57b2 2018-05-19 stsp
4359 f5215bb9 2019-02-22 stsp static void
4360 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4361 f5215bb9 2019-02-22 stsp {
4362 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4363 f5215bb9 2019-02-22 stsp update_panels();
4364 f5215bb9 2019-02-22 stsp doupdate();
4365 f44b1f58 2020-02-02 tracey }
4366 f44b1f58 2020-02-02 tracey
4367 f44b1f58 2020-02-02 tracey static const struct got_error *
4368 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4369 f44b1f58 2020-02-02 tracey {
4370 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4371 f44b1f58 2020-02-02 tracey
4372 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4373 f44b1f58 2020-02-02 tracey return NULL;
4374 f44b1f58 2020-02-02 tracey }
4375 f44b1f58 2020-02-02 tracey
4376 f44b1f58 2020-02-02 tracey static const struct got_error *
4377 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4378 f44b1f58 2020-02-02 tracey {
4379 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4380 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4381 f44b1f58 2020-02-02 tracey int lineno;
4382 78eecffc 2022-06-23 thomas char *line = NULL;
4383 826082fe 2020-12-10 stsp size_t linesize = 0;
4384 826082fe 2020-12-10 stsp ssize_t linelen;
4385 f44b1f58 2020-02-02 tracey
4386 f44b1f58 2020-02-02 tracey if (!view->searching) {
4387 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4388 f44b1f58 2020-02-02 tracey return NULL;
4389 f44b1f58 2020-02-02 tracey }
4390 f44b1f58 2020-02-02 tracey
4391 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4392 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4393 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4394 f44b1f58 2020-02-02 tracey else
4395 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4396 4b3f9dac 2021-12-17 thomas } else
4397 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4398 f44b1f58 2020-02-02 tracey
4399 f44b1f58 2020-02-02 tracey while (1) {
4400 f44b1f58 2020-02-02 tracey off_t offset;
4401 f44b1f58 2020-02-02 tracey
4402 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4403 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4404 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 f44b1f58 2020-02-02 tracey break;
4406 f44b1f58 2020-02-02 tracey }
4407 f44b1f58 2020-02-02 tracey
4408 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4409 f44b1f58 2020-02-02 tracey lineno = 1;
4410 f44b1f58 2020-02-02 tracey else
4411 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4412 f44b1f58 2020-02-02 tracey }
4413 f44b1f58 2020-02-02 tracey
4414 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4415 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4416 f44b1f58 2020-02-02 tracey free(line);
4417 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4418 f44b1f58 2020-02-02 tracey }
4419 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4420 78eecffc 2022-06-23 thomas if (linelen != -1) {
4421 78eecffc 2022-06-23 thomas char *exstr;
4422 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4423 78eecffc 2022-06-23 thomas if (err)
4424 78eecffc 2022-06-23 thomas break;
4425 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4426 78eecffc 2022-06-23 thomas &view->regmatch)) {
4427 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4428 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4429 78eecffc 2022-06-23 thomas free(exstr);
4430 78eecffc 2022-06-23 thomas break;
4431 78eecffc 2022-06-23 thomas }
4432 78eecffc 2022-06-23 thomas free(exstr);
4433 f44b1f58 2020-02-02 tracey }
4434 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4435 f44b1f58 2020-02-02 tracey lineno++;
4436 f44b1f58 2020-02-02 tracey else
4437 f44b1f58 2020-02-02 tracey lineno--;
4438 f44b1f58 2020-02-02 tracey }
4439 826082fe 2020-12-10 stsp free(line);
4440 f44b1f58 2020-02-02 tracey
4441 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4442 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4443 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4444 f44b1f58 2020-02-02 tracey }
4445 f44b1f58 2020-02-02 tracey
4446 05171be4 2022-06-23 thomas return err;
4447 f5215bb9 2019-02-22 stsp }
4448 f5215bb9 2019-02-22 stsp
4449 48ae06ee 2018-10-18 stsp static const struct got_error *
4450 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4451 a0f32f33 2022-06-13 thomas {
4452 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4453 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4454 a0f32f33 2022-06-13 thomas
4455 a0f32f33 2022-06-13 thomas free(s->id1);
4456 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4457 a0f32f33 2022-06-13 thomas free(s->id2);
4458 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4459 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4460 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4461 a0f32f33 2022-06-13 thomas s->f = NULL;
4462 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4463 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4464 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4465 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4466 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4467 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4468 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4469 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4470 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4471 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4472 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4473 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4474 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4475 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4476 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4477 a0f32f33 2022-06-13 thomas s->nlines = 0;
4478 a0f32f33 2022-06-13 thomas return err;
4479 a0f32f33 2022-06-13 thomas }
4480 a0f32f33 2022-06-13 thomas
4481 a0f32f33 2022-06-13 thomas static const struct got_error *
4482 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4483 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4484 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4485 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4486 48ae06ee 2018-10-18 stsp {
4487 48ae06ee 2018-10-18 stsp const struct got_error *err;
4488 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4489 5dc9f4bc 2018-08-04 stsp
4490 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4491 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4492 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4493 a0f32f33 2022-06-13 thomas
4494 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4495 15a94983 2018-12-23 stsp int type1, type2;
4496 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4497 15a94983 2018-12-23 stsp if (err)
4498 15a94983 2018-12-23 stsp return err;
4499 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4500 15a94983 2018-12-23 stsp if (err)
4501 15a94983 2018-12-23 stsp return err;
4502 15a94983 2018-12-23 stsp
4503 15a94983 2018-12-23 stsp if (type1 != type2)
4504 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4505 15a94983 2018-12-23 stsp }
4506 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4507 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4508 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4509 f44b1f58 2020-02-02 tracey s->repo = repo;
4510 f44b1f58 2020-02-02 tracey s->id1 = id1;
4511 f44b1f58 2020-02-02 tracey s->id2 = id2;
4512 3dbaef42 2020-11-24 stsp s->label1 = label1;
4513 3dbaef42 2020-11-24 stsp s->label2 = label2;
4514 48ae06ee 2018-10-18 stsp
4515 15a94983 2018-12-23 stsp if (id1) {
4516 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4517 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4518 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4519 48ae06ee 2018-10-18 stsp } else
4520 5465d566 2020-02-01 tracey s->id1 = NULL;
4521 48ae06ee 2018-10-18 stsp
4522 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4523 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4524 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4525 a0f32f33 2022-06-13 thomas goto done;
4526 48ae06ee 2018-10-18 stsp }
4527 a0f32f33 2022-06-13 thomas
4528 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4529 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4530 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4531 9a1d7435 2022-06-23 thomas goto done;
4532 9a1d7435 2022-06-23 thomas }
4533 9a1d7435 2022-06-23 thomas
4534 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4535 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4536 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4537 a0f32f33 2022-06-13 thomas goto done;
4538 a0f32f33 2022-06-13 thomas }
4539 a0f32f33 2022-06-13 thomas
4540 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4541 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4542 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4543 19a6a6b5 2022-07-01 thomas goto done;
4544 19a6a6b5 2022-07-01 thomas }
4545 19a6a6b5 2022-07-01 thomas
4546 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4547 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4548 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4549 19a6a6b5 2022-07-01 thomas goto done;
4550 19a6a6b5 2022-07-01 thomas }
4551 19a6a6b5 2022-07-01 thomas
4552 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4553 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4554 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4555 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4556 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4557 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4558 5465d566 2020-02-01 tracey s->repo = repo;
4559 6d17833f 2019-11-08 stsp
4560 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4561 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4562 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4563 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4564 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4565 6d17833f 2019-11-08 stsp if (err)
4566 a0f32f33 2022-06-13 thomas goto done;
4567 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4568 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4569 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4570 a0f32f33 2022-06-13 thomas if (err)
4571 a0f32f33 2022-06-13 thomas goto done;
4572 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4573 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4574 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4575 a0f32f33 2022-06-13 thomas if (err)
4576 a0f32f33 2022-06-13 thomas goto done;
4577 6d17833f 2019-11-08 stsp
4578 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4579 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4580 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4581 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4582 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4583 a0f32f33 2022-06-13 thomas if (err)
4584 a0f32f33 2022-06-13 thomas goto done;
4585 11b20872 2019-11-08 stsp
4586 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4587 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4588 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4589 a0f32f33 2022-06-13 thomas if (err)
4590 a0f32f33 2022-06-13 thomas goto done;
4591 11b20872 2019-11-08 stsp
4592 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4593 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4594 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4595 a0f32f33 2022-06-13 thomas if (err)
4596 a0f32f33 2022-06-13 thomas goto done;
4597 6d17833f 2019-11-08 stsp }
4598 5dc9f4bc 2018-08-04 stsp
4599 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4600 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4601 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4602 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4603 f5215bb9 2019-02-22 stsp
4604 5465d566 2020-02-01 tracey err = create_diff(s);
4605 48ae06ee 2018-10-18 stsp
4606 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4607 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4608 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4609 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4610 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4611 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4612 a0f32f33 2022-06-13 thomas done:
4613 a0f32f33 2022-06-13 thomas if (err)
4614 a0f32f33 2022-06-13 thomas close_diff_view(view);
4615 e5a0f69f 2018-08-18 stsp return err;
4616 5dc9f4bc 2018-08-04 stsp }
4617 5dc9f4bc 2018-08-04 stsp
4618 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4619 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4620 5dc9f4bc 2018-08-04 stsp {
4621 a3404814 2018-09-02 stsp const struct got_error *err;
4622 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4623 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4624 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4625 a3404814 2018-09-02 stsp
4626 a3404814 2018-09-02 stsp if (s->id1) {
4627 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4628 a3404814 2018-09-02 stsp if (err)
4629 a3404814 2018-09-02 stsp return err;
4630 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4631 3dbaef42 2020-11-24 stsp } else
4632 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4633 3dbaef42 2020-11-24 stsp
4634 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4635 a3404814 2018-09-02 stsp if (err)
4636 a3404814 2018-09-02 stsp return err;
4637 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4638 26ed57b2 2018-05-19 stsp
4639 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4640 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4641 a3404814 2018-09-02 stsp free(id_str1);
4642 a3404814 2018-09-02 stsp free(id_str2);
4643 a3404814 2018-09-02 stsp return err;
4644 a3404814 2018-09-02 stsp }
4645 a3404814 2018-09-02 stsp free(id_str1);
4646 a3404814 2018-09-02 stsp free(id_str2);
4647 a3404814 2018-09-02 stsp
4648 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4649 267bb3b8 2021-08-01 stsp free(header);
4650 267bb3b8 2021-08-01 stsp return err;
4651 15a087fe 2019-02-21 stsp }
4652 15a087fe 2019-02-21 stsp
4653 15a087fe 2019-02-21 stsp static const struct got_error *
4654 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4655 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4656 15a087fe 2019-02-21 stsp {
4657 d7a04538 2019-02-21 stsp const struct got_error *err;
4658 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4659 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4660 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4661 15a087fe 2019-02-21 stsp
4662 15a087fe 2019-02-21 stsp free(s->id2);
4663 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4664 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4665 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4666 15a087fe 2019-02-21 stsp
4667 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4668 d7a04538 2019-02-21 stsp if (err)
4669 d7a04538 2019-02-21 stsp return err;
4670 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4671 15a087fe 2019-02-21 stsp free(s->id1);
4672 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4673 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4674 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4675 15a087fe 2019-02-21 stsp return NULL;
4676 0cf4efb1 2018-09-29 stsp }
4677 0cf4efb1 2018-09-29 stsp
4678 0cf4efb1 2018-09-29 stsp static const struct got_error *
4679 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4680 adf4c9e0 2022-07-03 thomas {
4681 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4682 adf4c9e0 2022-07-03 thomas
4683 adf4c9e0 2022-07-03 thomas view->count = 0;
4684 adf4c9e0 2022-07-03 thomas wclear(view->window);
4685 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4686 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4687 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4688 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4689 adf4c9e0 2022-07-03 thomas return create_diff(s);
4690 adf4c9e0 2022-07-03 thomas }
4691 adf4c9e0 2022-07-03 thomas
4692 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4693 4fc71f3b 2022-07-12 thomas int, int, int);
4694 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4695 4fc71f3b 2022-07-12 thomas int, int);
4696 4fc71f3b 2022-07-12 thomas
4697 adf4c9e0 2022-07-03 thomas static const struct got_error *
4698 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4699 e5a0f69f 2018-08-18 stsp {
4700 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4701 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4702 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4703 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4704 826082fe 2020-12-10 stsp char *line = NULL;
4705 826082fe 2020-12-10 stsp size_t linesize = 0;
4706 826082fe 2020-12-10 stsp ssize_t linelen;
4707 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4708 e5a0f69f 2018-08-18 stsp
4709 e5a0f69f 2018-08-18 stsp switch (ch) {
4710 05171be4 2022-06-23 thomas case '0':
4711 05171be4 2022-06-23 thomas view->x = 0;
4712 05171be4 2022-06-23 thomas break;
4713 05171be4 2022-06-23 thomas case '$':
4714 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4715 07b0611c 2022-06-23 thomas view->count = 0;
4716 05171be4 2022-06-23 thomas break;
4717 05171be4 2022-06-23 thomas case KEY_RIGHT:
4718 05171be4 2022-06-23 thomas case 'l':
4719 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4720 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4721 07b0611c 2022-06-23 thomas else
4722 07b0611c 2022-06-23 thomas view->count = 0;
4723 05171be4 2022-06-23 thomas break;
4724 05171be4 2022-06-23 thomas case KEY_LEFT:
4725 05171be4 2022-06-23 thomas case 'h':
4726 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4727 07b0611c 2022-06-23 thomas if (view->x <= 0)
4728 07b0611c 2022-06-23 thomas view->count = 0;
4729 05171be4 2022-06-23 thomas break;
4730 64453f7e 2020-11-21 stsp case 'a':
4731 3dbaef42 2020-11-24 stsp case 'w':
4732 3dbaef42 2020-11-24 stsp if (ch == 'a')
4733 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4734 3dbaef42 2020-11-24 stsp if (ch == 'w')
4735 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4736 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4737 912a3f79 2021-08-30 j break;
4738 912a3f79 2021-08-30 j case 'g':
4739 912a3f79 2021-08-30 j case KEY_HOME:
4740 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4741 07b0611c 2022-06-23 thomas view->count = 0;
4742 64453f7e 2020-11-21 stsp break;
4743 912a3f79 2021-08-30 j case 'G':
4744 912a3f79 2021-08-30 j case KEY_END:
4745 07b0611c 2022-06-23 thomas view->count = 0;
4746 912a3f79 2021-08-30 j if (s->eof)
4747 912a3f79 2021-08-30 j break;
4748 912a3f79 2021-08-30 j
4749 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4750 912a3f79 2021-08-30 j s->eof = 1;
4751 912a3f79 2021-08-30 j break;
4752 1e37a5c2 2019-05-12 jcs case 'k':
4753 1e37a5c2 2019-05-12 jcs case KEY_UP:
4754 f7140bf5 2021-10-17 thomas case CTRL('p'):
4755 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4756 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4757 07b0611c 2022-06-23 thomas else
4758 07b0611c 2022-06-23 thomas view->count = 0;
4759 1e37a5c2 2019-05-12 jcs break;
4760 70f17a53 2022-06-13 thomas case CTRL('u'):
4761 23427b14 2022-06-23 thomas case 'u':
4762 70f17a53 2022-06-13 thomas nscroll /= 2;
4763 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4764 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4765 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4766 1c5e5faa 2022-06-23 thomas case 'b':
4767 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4768 07b0611c 2022-06-23 thomas view->count = 0;
4769 26ed57b2 2018-05-19 stsp break;
4770 07b0611c 2022-06-23 thomas }
4771 1e37a5c2 2019-05-12 jcs i = 0;
4772 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4773 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4774 1e37a5c2 2019-05-12 jcs break;
4775 1e37a5c2 2019-05-12 jcs case 'j':
4776 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4777 f7140bf5 2021-10-17 thomas case CTRL('n'):
4778 1e37a5c2 2019-05-12 jcs if (!s->eof)
4779 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4780 07b0611c 2022-06-23 thomas else
4781 07b0611c 2022-06-23 thomas view->count = 0;
4782 1e37a5c2 2019-05-12 jcs break;
4783 70f17a53 2022-06-13 thomas case CTRL('d'):
4784 23427b14 2022-06-23 thomas case 'd':
4785 70f17a53 2022-06-13 thomas nscroll /= 2;
4786 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4787 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4788 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4789 1c5e5faa 2022-06-23 thomas case 'f':
4790 1e37a5c2 2019-05-12 jcs case ' ':
4791 07b0611c 2022-06-23 thomas if (s->eof) {
4792 07b0611c 2022-06-23 thomas view->count = 0;
4793 1e37a5c2 2019-05-12 jcs break;
4794 07b0611c 2022-06-23 thomas }
4795 1e37a5c2 2019-05-12 jcs i = 0;
4796 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4797 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4798 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4799 826082fe 2020-12-10 stsp if (linelen == -1) {
4800 826082fe 2020-12-10 stsp if (feof(s->f)) {
4801 826082fe 2020-12-10 stsp s->eof = 1;
4802 826082fe 2020-12-10 stsp } else
4803 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4804 34bc9ec9 2019-02-22 stsp break;
4805 826082fe 2020-12-10 stsp }
4806 1e37a5c2 2019-05-12 jcs }
4807 826082fe 2020-12-10 stsp free(line);
4808 1e37a5c2 2019-05-12 jcs break;
4809 1e37a5c2 2019-05-12 jcs case '[':
4810 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4811 1e37a5c2 2019-05-12 jcs s->diff_context--;
4812 aa61903a 2021-12-31 thomas s->matched_line = 0;
4813 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4814 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4815 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4816 27829c9e 2020-11-21 stsp s->nlines) {
4817 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4818 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4819 27829c9e 2020-11-21 stsp }
4820 07b0611c 2022-06-23 thomas } else
4821 07b0611c 2022-06-23 thomas view->count = 0;
4822 1e37a5c2 2019-05-12 jcs break;
4823 1e37a5c2 2019-05-12 jcs case ']':
4824 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4825 1e37a5c2 2019-05-12 jcs s->diff_context++;
4826 aa61903a 2021-12-31 thomas s->matched_line = 0;
4827 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4828 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4829 07b0611c 2022-06-23 thomas } else
4830 07b0611c 2022-06-23 thomas view->count = 0;
4831 1e37a5c2 2019-05-12 jcs break;
4832 1e37a5c2 2019-05-12 jcs case '<':
4833 1e37a5c2 2019-05-12 jcs case ',':
4834 777aae21 2022-07-20 thomas case 'K':
4835 4fc71f3b 2022-07-12 thomas up = 1;
4836 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4837 4fc71f3b 2022-07-12 thomas case '>':
4838 4fc71f3b 2022-07-12 thomas case '.':
4839 777aae21 2022-07-20 thomas case 'J':
4840 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4841 07b0611c 2022-06-23 thomas view->count = 0;
4842 48ae06ee 2018-10-18 stsp break;
4843 07b0611c 2022-06-23 thomas }
4844 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4845 6524637e 2019-02-21 stsp
4846 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4847 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4848 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4849 15a087fe 2019-02-21 stsp
4850 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4851 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4852 4fc71f3b 2022-07-12 thomas if (err)
4853 4fc71f3b 2022-07-12 thomas break;
4854 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4855 15a087fe 2019-02-21 stsp
4856 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4857 4fc71f3b 2022-07-12 thomas break;
4858 15a087fe 2019-02-21 stsp
4859 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4860 4fc71f3b 2022-07-12 thomas if (err)
4861 4fc71f3b 2022-07-12 thomas break;
4862 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4863 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4864 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4865 5e224a3e 2019-02-22 stsp
4866 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4867 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4868 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4869 4fc71f3b 2022-07-12 thomas
4870 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4871 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4872 4fc71f3b 2022-07-12 thomas if (err)
4873 4fc71f3b 2022-07-12 thomas break;
4874 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4875 5a8b5076 2020-12-05 stsp
4876 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4877 4fc71f3b 2022-07-12 thomas break;
4878 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4879 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4880 4fc71f3b 2022-07-12 thomas bs->selected_line);
4881 4fc71f3b 2022-07-12 thomas if (id == NULL)
4882 4fc71f3b 2022-07-12 thomas break;
4883 15a087fe 2019-02-21 stsp
4884 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4885 4fc71f3b 2022-07-12 thomas break;
4886 15a087fe 2019-02-21 stsp
4887 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4888 4fc71f3b 2022-07-12 thomas if (err)
4889 4fc71f3b 2022-07-12 thomas break;
4890 4fc71f3b 2022-07-12 thomas }
4891 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4892 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4893 aa61903a 2021-12-31 thomas s->matched_line = 0;
4894 05171be4 2022-06-23 thomas view->x = 0;
4895 1e37a5c2 2019-05-12 jcs
4896 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4897 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4898 1e37a5c2 2019-05-12 jcs break;
4899 1e37a5c2 2019-05-12 jcs default:
4900 07b0611c 2022-06-23 thomas view->count = 0;
4901 1e37a5c2 2019-05-12 jcs break;
4902 26ed57b2 2018-05-19 stsp }
4903 e5a0f69f 2018-08-18 stsp
4904 bcbd79e2 2018-08-19 stsp return err;
4905 26ed57b2 2018-05-19 stsp }
4906 26ed57b2 2018-05-19 stsp
4907 4ed7e80c 2018-05-20 stsp static const struct got_error *
4908 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4909 9f7d7167 2018-04-29 stsp {
4910 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4911 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4912 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4913 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4914 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4915 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4916 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4917 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4918 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4919 3dbaef42 2020-11-24 stsp const char *errstr;
4920 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4921 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4922 70ac5f84 2019-03-28 stsp
4923 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4924 26ed57b2 2018-05-19 stsp switch (ch) {
4925 64453f7e 2020-11-21 stsp case 'a':
4926 64453f7e 2020-11-21 stsp force_text_diff = 1;
4927 3dbaef42 2020-11-24 stsp break;
4928 3dbaef42 2020-11-24 stsp case 'C':
4929 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4930 3dbaef42 2020-11-24 stsp &errstr);
4931 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4932 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4933 8f666e67 2022-02-12 thomas errstr, errstr);
4934 64453f7e 2020-11-21 stsp break;
4935 09b5bff8 2020-02-23 naddy case 'r':
4936 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4937 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4938 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4939 09b5bff8 2020-02-23 naddy optarg);
4940 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4941 09b5bff8 2020-02-23 naddy break;
4942 3dbaef42 2020-11-24 stsp case 'w':
4943 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4944 3dbaef42 2020-11-24 stsp break;
4945 26ed57b2 2018-05-19 stsp default:
4946 17020d27 2019-03-07 stsp usage_diff();
4947 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4948 26ed57b2 2018-05-19 stsp }
4949 26ed57b2 2018-05-19 stsp }
4950 26ed57b2 2018-05-19 stsp
4951 26ed57b2 2018-05-19 stsp argc -= optind;
4952 26ed57b2 2018-05-19 stsp argv += optind;
4953 26ed57b2 2018-05-19 stsp
4954 26ed57b2 2018-05-19 stsp if (argc == 0) {
4955 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4956 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4957 15a94983 2018-12-23 stsp id_str1 = argv[0];
4958 15a94983 2018-12-23 stsp id_str2 = argv[1];
4959 26ed57b2 2018-05-19 stsp } else
4960 26ed57b2 2018-05-19 stsp usage_diff();
4961 eb6600df 2019-01-04 stsp
4962 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4963 7cd52833 2022-06-23 thomas if (error)
4964 7cd52833 2022-06-23 thomas goto done;
4965 7cd52833 2022-06-23 thomas
4966 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4967 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4968 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4969 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4970 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4971 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4972 c156c7a4 2020-12-18 stsp goto done;
4973 a273ac94 2020-02-23 naddy if (worktree)
4974 a273ac94 2020-02-23 naddy repo_path =
4975 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4976 a273ac94 2020-02-23 naddy else
4977 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4978 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4979 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4980 c156c7a4 2020-12-18 stsp goto done;
4981 c156c7a4 2020-12-18 stsp }
4982 a273ac94 2020-02-23 naddy }
4983 a273ac94 2020-02-23 naddy
4984 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4985 eb6600df 2019-01-04 stsp if (error)
4986 eb6600df 2019-01-04 stsp goto done;
4987 26ed57b2 2018-05-19 stsp
4988 a273ac94 2020-02-23 naddy init_curses();
4989 a273ac94 2020-02-23 naddy
4990 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4991 51a10b52 2020-12-26 stsp if (error)
4992 51a10b52 2020-12-26 stsp goto done;
4993 51a10b52 2020-12-26 stsp
4994 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4995 26ed57b2 2018-05-19 stsp if (error)
4996 26ed57b2 2018-05-19 stsp goto done;
4997 26ed57b2 2018-05-19 stsp
4998 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4999 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5000 26ed57b2 2018-05-19 stsp if (error)
5001 26ed57b2 2018-05-19 stsp goto done;
5002 26ed57b2 2018-05-19 stsp
5003 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5004 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5005 26ed57b2 2018-05-19 stsp if (error)
5006 26ed57b2 2018-05-19 stsp goto done;
5007 26ed57b2 2018-05-19 stsp
5008 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5009 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5010 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5011 ea5e7bb5 2018-08-01 stsp goto done;
5012 ea5e7bb5 2018-08-01 stsp }
5013 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5014 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5015 5dc9f4bc 2018-08-04 stsp if (error)
5016 5dc9f4bc 2018-08-04 stsp goto done;
5017 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5018 26ed57b2 2018-05-19 stsp done:
5019 3dbaef42 2020-11-24 stsp free(label1);
5020 3dbaef42 2020-11-24 stsp free(label2);
5021 c02c541e 2019-03-29 stsp free(repo_path);
5022 a273ac94 2020-02-23 naddy free(cwd);
5023 1d0f4054 2021-06-17 stsp if (repo) {
5024 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5025 1d0f4054 2021-06-17 stsp if (error == NULL)
5026 1d0f4054 2021-06-17 stsp error = close_err;
5027 1d0f4054 2021-06-17 stsp }
5028 a273ac94 2020-02-23 naddy if (worktree)
5029 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5030 7cd52833 2022-06-23 thomas if (pack_fds) {
5031 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5032 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5033 7cd52833 2022-06-23 thomas if (error == NULL)
5034 7cd52833 2022-06-23 thomas error = pack_err;
5035 7cd52833 2022-06-23 thomas }
5036 51a10b52 2020-12-26 stsp tog_free_refs();
5037 26ed57b2 2018-05-19 stsp return error;
5038 9f7d7167 2018-04-29 stsp }
5039 9f7d7167 2018-04-29 stsp
5040 4ed7e80c 2018-05-20 stsp __dead static void
5041 9f7d7167 2018-04-29 stsp usage_blame(void)
5042 9f7d7167 2018-04-29 stsp {
5043 80ddbec8 2018-04-29 stsp endwin();
5044 91198554 2022-06-23 thomas fprintf(stderr,
5045 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5046 9f7d7167 2018-04-29 stsp getprogname());
5047 9f7d7167 2018-04-29 stsp exit(1);
5048 9f7d7167 2018-04-29 stsp }
5049 84451b3e 2018-07-10 stsp
5050 84451b3e 2018-07-10 stsp struct tog_blame_line {
5051 84451b3e 2018-07-10 stsp int annotated;
5052 84451b3e 2018-07-10 stsp struct got_object_id *id;
5053 84451b3e 2018-07-10 stsp };
5054 9f7d7167 2018-04-29 stsp
5055 4ed7e80c 2018-05-20 stsp static const struct got_error *
5056 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5057 84451b3e 2018-07-10 stsp {
5058 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5059 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5060 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5061 84451b3e 2018-07-10 stsp const struct got_error *err;
5062 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5063 826082fe 2020-12-10 stsp char *line = NULL;
5064 826082fe 2020-12-10 stsp size_t linesize = 0;
5065 826082fe 2020-12-10 stsp ssize_t linelen;
5066 84451b3e 2018-07-10 stsp wchar_t *wline;
5067 27a741e5 2019-09-11 stsp int width;
5068 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5069 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5070 ab089a2a 2018-07-12 stsp char *id_str;
5071 11b20872 2019-11-08 stsp struct tog_color *tc;
5072 ab089a2a 2018-07-12 stsp
5073 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5074 ab089a2a 2018-07-12 stsp if (err)
5075 ab089a2a 2018-07-12 stsp return err;
5076 84451b3e 2018-07-10 stsp
5077 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5078 f7d12f7e 2018-08-01 stsp werase(view->window);
5079 84451b3e 2018-07-10 stsp
5080 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5081 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5082 ab089a2a 2018-07-12 stsp free(id_str);
5083 ab089a2a 2018-07-12 stsp return err;
5084 ab089a2a 2018-07-12 stsp }
5085 ab089a2a 2018-07-12 stsp
5086 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5087 ab089a2a 2018-07-12 stsp free(line);
5088 2550e4c3 2018-07-13 stsp line = NULL;
5089 1cae65b4 2019-09-22 stsp if (err)
5090 1cae65b4 2019-09-22 stsp return err;
5091 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5092 a3404814 2018-09-02 stsp wstandout(view->window);
5093 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5094 11b20872 2019-11-08 stsp if (tc)
5095 11b20872 2019-11-08 stsp wattr_on(view->window,
5096 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5097 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5098 11b20872 2019-11-08 stsp if (tc)
5099 11b20872 2019-11-08 stsp wattr_off(view->window,
5100 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5101 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5102 a3404814 2018-09-02 stsp wstandend(view->window);
5103 2550e4c3 2018-07-13 stsp free(wline);
5104 2550e4c3 2018-07-13 stsp wline = NULL;
5105 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5106 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5107 ab089a2a 2018-07-12 stsp
5108 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5109 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5110 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5111 ab089a2a 2018-07-12 stsp free(id_str);
5112 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5113 ab089a2a 2018-07-12 stsp }
5114 ab089a2a 2018-07-12 stsp free(id_str);
5115 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5116 3f60a8ef 2018-07-10 stsp free(line);
5117 2550e4c3 2018-07-13 stsp line = NULL;
5118 3f60a8ef 2018-07-10 stsp if (err)
5119 3f60a8ef 2018-07-10 stsp return err;
5120 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5121 2550e4c3 2018-07-13 stsp free(wline);
5122 2550e4c3 2018-07-13 stsp wline = NULL;
5123 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5124 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5125 3f60a8ef 2018-07-10 stsp
5126 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5127 05171be4 2022-06-23 thomas view->maxx = 0;
5128 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5129 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5130 826082fe 2020-12-10 stsp if (linelen == -1) {
5131 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5132 826082fe 2020-12-10 stsp s->eof = 1;
5133 826082fe 2020-12-10 stsp break;
5134 826082fe 2020-12-10 stsp }
5135 84451b3e 2018-07-10 stsp free(line);
5136 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5137 84451b3e 2018-07-10 stsp }
5138 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5139 826082fe 2020-12-10 stsp continue;
5140 cbea3800 2022-06-23 thomas
5141 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5142 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5143 cbea3800 2022-06-23 thomas if (err) {
5144 cbea3800 2022-06-23 thomas free(line);
5145 cbea3800 2022-06-23 thomas return err;
5146 cbea3800 2022-06-23 thomas }
5147 cbea3800 2022-06-23 thomas free(wline);
5148 cbea3800 2022-06-23 thomas wline = NULL;
5149 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5150 84451b3e 2018-07-10 stsp
5151 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5152 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5153 b700b5d6 2018-07-10 stsp
5154 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5155 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5156 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5157 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5158 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5159 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5160 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5161 8d0fe45a 2019-08-12 stsp char *id_str;
5162 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5163 91198554 2022-06-23 thomas blame_line->id);
5164 8d0fe45a 2019-08-12 stsp if (err) {
5165 8d0fe45a 2019-08-12 stsp free(line);
5166 8d0fe45a 2019-08-12 stsp return err;
5167 8d0fe45a 2019-08-12 stsp }
5168 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5169 11b20872 2019-11-08 stsp if (tc)
5170 11b20872 2019-11-08 stsp wattr_on(view->window,
5171 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5172 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5173 11b20872 2019-11-08 stsp if (tc)
5174 11b20872 2019-11-08 stsp wattr_off(view->window,
5175 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5176 8d0fe45a 2019-08-12 stsp free(id_str);
5177 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5178 8d0fe45a 2019-08-12 stsp } else {
5179 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5180 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5181 84451b3e 2018-07-10 stsp }
5182 ee41ec32 2018-07-10 stsp } else {
5183 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5184 ee41ec32 2018-07-10 stsp prev_id = NULL;
5185 ee41ec32 2018-07-10 stsp }
5186 84451b3e 2018-07-10 stsp
5187 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5188 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5189 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5190 27a741e5 2019-09-11 stsp
5191 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5192 41605754 2020-11-12 stsp width = 9;
5193 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5194 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5195 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5196 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5197 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5198 41605754 2020-11-12 stsp if (err) {
5199 41605754 2020-11-12 stsp free(line);
5200 41605754 2020-11-12 stsp return err;
5201 41605754 2020-11-12 stsp }
5202 41605754 2020-11-12 stsp width += 9;
5203 41605754 2020-11-12 stsp } else {
5204 923086fd 2022-06-23 thomas int skip;
5205 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5206 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5207 923086fd 2022-06-23 thomas if (err) {
5208 923086fd 2022-06-23 thomas free(line);
5209 923086fd 2022-06-23 thomas return err;
5210 05171be4 2022-06-23 thomas }
5211 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5212 02bce7e2 2022-06-23 thomas width += 9;
5213 41605754 2020-11-12 stsp free(wline);
5214 41605754 2020-11-12 stsp wline = NULL;
5215 41605754 2020-11-12 stsp }
5216 41605754 2020-11-12 stsp
5217 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5218 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5219 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5220 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5221 84451b3e 2018-07-10 stsp }
5222 826082fe 2020-12-10 stsp free(line);
5223 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5224 84451b3e 2018-07-10 stsp
5225 a5d43cac 2022-07-01 thomas view_border(view);
5226 84451b3e 2018-07-10 stsp
5227 84451b3e 2018-07-10 stsp return NULL;
5228 84451b3e 2018-07-10 stsp }
5229 84451b3e 2018-07-10 stsp
5230 84451b3e 2018-07-10 stsp static const struct got_error *
5231 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5232 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5233 84451b3e 2018-07-10 stsp {
5234 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5235 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5236 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5237 1a76625f 2018-10-22 stsp int errcode;
5238 84451b3e 2018-07-10 stsp
5239 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5240 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5241 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5242 84451b3e 2018-07-10 stsp
5243 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5244 1a76625f 2018-10-22 stsp if (errcode)
5245 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5246 84451b3e 2018-07-10 stsp
5247 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5248 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5249 d68a0a7d 2018-07-10 stsp goto done;
5250 d68a0a7d 2018-07-10 stsp }
5251 d68a0a7d 2018-07-10 stsp
5252 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5253 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5254 d68a0a7d 2018-07-10 stsp
5255 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5256 d68a0a7d 2018-07-10 stsp if (line->annotated)
5257 d68a0a7d 2018-07-10 stsp goto done;
5258 d68a0a7d 2018-07-10 stsp
5259 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5260 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5261 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5262 84451b3e 2018-07-10 stsp goto done;
5263 84451b3e 2018-07-10 stsp }
5264 84451b3e 2018-07-10 stsp line->annotated = 1;
5265 84451b3e 2018-07-10 stsp done:
5266 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5267 1a76625f 2018-10-22 stsp if (errcode)
5268 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5269 84451b3e 2018-07-10 stsp return err;
5270 84451b3e 2018-07-10 stsp }
5271 84451b3e 2018-07-10 stsp
5272 84451b3e 2018-07-10 stsp static void *
5273 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5274 84451b3e 2018-07-10 stsp {
5275 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5276 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5277 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5278 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5279 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5280 00a8878e 2022-07-01 thomas
5281 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5282 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5283 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5284 9117a7b7 2022-07-01 thomas
5285 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5286 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5287 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5288 9117a7b7 2022-07-01 thomas goto done;
5289 9117a7b7 2022-07-01 thomas }
5290 18430de3 2018-07-10 stsp
5291 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5292 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5293 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5294 9117a7b7 2022-07-01 thomas goto done;
5295 9117a7b7 2022-07-01 thomas }
5296 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5297 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5298 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5299 9117a7b7 2022-07-01 thomas goto done;
5300 9117a7b7 2022-07-01 thomas }
5301 9117a7b7 2022-07-01 thomas
5302 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5303 61266923 2020-01-14 stsp if (err)
5304 9117a7b7 2022-07-01 thomas goto done;
5305 61266923 2020-01-14 stsp
5306 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5307 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5308 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5309 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5310 fc06ba56 2019-08-22 stsp err = NULL;
5311 18430de3 2018-07-10 stsp
5312 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5313 9117a7b7 2022-07-01 thomas if (errcode) {
5314 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5315 9117a7b7 2022-07-01 thomas goto done;
5316 9117a7b7 2022-07-01 thomas }
5317 18430de3 2018-07-10 stsp
5318 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5319 1d0f4054 2021-06-17 stsp if (err == NULL)
5320 1d0f4054 2021-06-17 stsp err = close_err;
5321 c9beca56 2018-07-22 stsp ta->repo = NULL;
5322 c9beca56 2018-07-22 stsp *ta->complete = 1;
5323 18430de3 2018-07-10 stsp
5324 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5325 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5326 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5327 18430de3 2018-07-10 stsp
5328 9117a7b7 2022-07-01 thomas done:
5329 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5330 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5331 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5332 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5333 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5334 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5335 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5336 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5337 00a8878e 2022-07-01 thomas
5338 18430de3 2018-07-10 stsp return (void *)err;
5339 84451b3e 2018-07-10 stsp }
5340 84451b3e 2018-07-10 stsp
5341 245d91c1 2018-07-12 stsp static struct got_object_id *
5342 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5343 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5344 245d91c1 2018-07-12 stsp {
5345 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5346 8d0fe45a 2019-08-12 stsp
5347 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5348 8d0fe45a 2019-08-12 stsp return NULL;
5349 b880a918 2018-07-10 stsp
5350 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5351 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5352 4fc71f3b 2022-07-12 thomas return NULL;
5353 4fc71f3b 2022-07-12 thomas
5354 4fc71f3b 2022-07-12 thomas return line->id;
5355 4fc71f3b 2022-07-12 thomas }
5356 4fc71f3b 2022-07-12 thomas
5357 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5358 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5359 4fc71f3b 2022-07-12 thomas int lineno)
5360 4fc71f3b 2022-07-12 thomas {
5361 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5362 4fc71f3b 2022-07-12 thomas
5363 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5364 4fc71f3b 2022-07-12 thomas return NULL;
5365 4fc71f3b 2022-07-12 thomas
5366 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5367 245d91c1 2018-07-12 stsp if (!line->annotated)
5368 245d91c1 2018-07-12 stsp return NULL;
5369 245d91c1 2018-07-12 stsp
5370 245d91c1 2018-07-12 stsp return line->id;
5371 b880a918 2018-07-10 stsp }
5372 245d91c1 2018-07-12 stsp
5373 b880a918 2018-07-10 stsp static const struct got_error *
5374 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5375 a70480e0 2018-06-23 stsp {
5376 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5377 245d91c1 2018-07-12 stsp int i;
5378 245d91c1 2018-07-12 stsp
5379 245d91c1 2018-07-12 stsp if (blame->thread) {
5380 1a76625f 2018-10-22 stsp int errcode;
5381 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5382 1a76625f 2018-10-22 stsp if (errcode)
5383 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5384 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5385 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5386 1a76625f 2018-10-22 stsp if (errcode)
5387 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5388 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5389 1a76625f 2018-10-22 stsp if (errcode)
5390 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5391 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5392 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5393 245d91c1 2018-07-12 stsp err = NULL;
5394 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5395 245d91c1 2018-07-12 stsp }
5396 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5397 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5398 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5399 1d0f4054 2021-06-17 stsp if (err == NULL)
5400 1d0f4054 2021-06-17 stsp err = close_err;
5401 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5402 245d91c1 2018-07-12 stsp }
5403 245d91c1 2018-07-12 stsp if (blame->f) {
5404 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5405 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5406 245d91c1 2018-07-12 stsp blame->f = NULL;
5407 245d91c1 2018-07-12 stsp }
5408 57670559 2018-12-23 stsp if (blame->lines) {
5409 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5410 57670559 2018-12-23 stsp free(blame->lines[i].id);
5411 57670559 2018-12-23 stsp free(blame->lines);
5412 57670559 2018-12-23 stsp blame->lines = NULL;
5413 57670559 2018-12-23 stsp }
5414 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5415 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5416 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5417 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5418 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5419 7cd52833 2022-06-23 thomas if (err == NULL)
5420 7cd52833 2022-06-23 thomas err = pack_err;
5421 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5422 7cd52833 2022-06-23 thomas }
5423 245d91c1 2018-07-12 stsp return err;
5424 245d91c1 2018-07-12 stsp }
5425 245d91c1 2018-07-12 stsp
5426 245d91c1 2018-07-12 stsp static const struct got_error *
5427 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5428 fc06ba56 2019-08-22 stsp {
5429 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5430 fc06ba56 2019-08-22 stsp int *done = arg;
5431 fc06ba56 2019-08-22 stsp int errcode;
5432 fc06ba56 2019-08-22 stsp
5433 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5434 fc06ba56 2019-08-22 stsp if (errcode)
5435 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5436 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5437 fc06ba56 2019-08-22 stsp
5438 fc06ba56 2019-08-22 stsp if (*done)
5439 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5440 fc06ba56 2019-08-22 stsp
5441 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5442 fc06ba56 2019-08-22 stsp if (errcode)
5443 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5444 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5445 fc06ba56 2019-08-22 stsp
5446 fc06ba56 2019-08-22 stsp return err;
5447 fc06ba56 2019-08-22 stsp }
5448 fc06ba56 2019-08-22 stsp
5449 fc06ba56 2019-08-22 stsp static const struct got_error *
5450 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5451 245d91c1 2018-07-12 stsp {
5452 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5453 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5454 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5455 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5456 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5457 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5458 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5459 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5460 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5461 a70480e0 2018-06-23 stsp
5462 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5463 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5464 27d434c2 2018-09-15 stsp if (err)
5465 15a94983 2018-12-23 stsp return err;
5466 f4ae6ddb 2022-07-01 thomas
5467 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5468 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5469 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5470 f4ae6ddb 2022-07-01 thomas goto done;
5471 f4ae6ddb 2022-07-01 thomas }
5472 945f9229 2022-04-16 thomas
5473 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5474 945f9229 2022-04-16 thomas if (err)
5475 945f9229 2022-04-16 thomas goto done;
5476 27d434c2 2018-09-15 stsp
5477 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5478 84451b3e 2018-07-10 stsp if (err)
5479 84451b3e 2018-07-10 stsp goto done;
5480 27d434c2 2018-09-15 stsp
5481 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5482 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5483 84451b3e 2018-07-10 stsp goto done;
5484 84451b3e 2018-07-10 stsp }
5485 a70480e0 2018-06-23 stsp
5486 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5487 a70480e0 2018-06-23 stsp if (err)
5488 a70480e0 2018-06-23 stsp goto done;
5489 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5490 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5491 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5492 84451b3e 2018-07-10 stsp goto done;
5493 84451b3e 2018-07-10 stsp }
5494 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5495 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5496 1fddf795 2021-01-20 stsp if (err)
5497 1fddf795 2021-01-20 stsp goto done;
5498 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5499 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5500 84451b3e 2018-07-10 stsp goto done;
5501 1fddf795 2021-01-20 stsp }
5502 b02560ec 2019-08-19 stsp
5503 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5504 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5505 b02560ec 2019-08-19 stsp blame->nlines--;
5506 a70480e0 2018-06-23 stsp
5507 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5508 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5509 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5510 84451b3e 2018-07-10 stsp goto done;
5511 84451b3e 2018-07-10 stsp }
5512 a70480e0 2018-06-23 stsp
5513 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5514 bd24772e 2018-07-11 stsp if (err)
5515 bd24772e 2018-07-11 stsp goto done;
5516 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5517 7cd52833 2022-06-23 thomas pack_fds);
5518 7cd52833 2022-06-23 thomas if (err)
5519 7cd52833 2022-06-23 thomas goto done;
5520 bd24772e 2018-07-11 stsp
5521 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5522 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5523 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5524 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5525 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5526 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5527 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5528 245d91c1 2018-07-12 stsp goto done;
5529 245d91c1 2018-07-12 stsp }
5530 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5531 245d91c1 2018-07-12 stsp
5532 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5533 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5534 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5535 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5536 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5537 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5538 a5388363 2020-12-01 naddy s->blame_complete = 0;
5539 f5a09613 2020-12-13 naddy
5540 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5541 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5542 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5543 f5a09613 2020-12-13 naddy s->selected_line = 1;
5544 f5a09613 2020-12-13 naddy }
5545 aa61903a 2021-12-31 thomas s->matched_line = 0;
5546 245d91c1 2018-07-12 stsp
5547 245d91c1 2018-07-12 stsp done:
5548 945f9229 2022-04-16 thomas if (commit)
5549 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5550 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5551 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5552 245d91c1 2018-07-12 stsp if (blob)
5553 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5554 27d434c2 2018-09-15 stsp free(obj_id);
5555 245d91c1 2018-07-12 stsp if (err)
5556 245d91c1 2018-07-12 stsp stop_blame(blame);
5557 245d91c1 2018-07-12 stsp return err;
5558 245d91c1 2018-07-12 stsp }
5559 245d91c1 2018-07-12 stsp
5560 245d91c1 2018-07-12 stsp static const struct got_error *
5561 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5562 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5563 245d91c1 2018-07-12 stsp {
5564 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5565 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5566 dbc6a6b6 2018-07-12 stsp
5567 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5568 245d91c1 2018-07-12 stsp
5569 c4843652 2019-08-12 stsp s->path = strdup(path);
5570 c4843652 2019-08-12 stsp if (s->path == NULL)
5571 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5572 c4843652 2019-08-12 stsp
5573 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5574 c4843652 2019-08-12 stsp if (err) {
5575 c4843652 2019-08-12 stsp free(s->path);
5576 7cbe629d 2018-08-04 stsp return err;
5577 c4843652 2019-08-12 stsp }
5578 245d91c1 2018-07-12 stsp
5579 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5580 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5581 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5582 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5583 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5584 fb2756b9 2018-08-04 stsp s->repo = repo;
5585 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5586 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5587 7cbe629d 2018-08-04 stsp
5588 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5589 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5590 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5591 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5592 11b20872 2019-11-08 stsp if (err)
5593 11b20872 2019-11-08 stsp return err;
5594 11b20872 2019-11-08 stsp }
5595 11b20872 2019-11-08 stsp
5596 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5597 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5598 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5599 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5600 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5601 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5602 e5a0f69f 2018-08-18 stsp
5603 a5388363 2020-12-01 naddy return run_blame(view);
5604 7cbe629d 2018-08-04 stsp }
5605 7cbe629d 2018-08-04 stsp
5606 e5a0f69f 2018-08-18 stsp static const struct got_error *
5607 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5608 7cbe629d 2018-08-04 stsp {
5609 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5610 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5611 7cbe629d 2018-08-04 stsp
5612 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5613 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5614 e5a0f69f 2018-08-18 stsp
5615 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5616 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5617 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5618 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5619 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5620 7cbe629d 2018-08-04 stsp }
5621 e5a0f69f 2018-08-18 stsp
5622 e5a0f69f 2018-08-18 stsp free(s->path);
5623 11b20872 2019-11-08 stsp free_colors(&s->colors);
5624 e5a0f69f 2018-08-18 stsp return err;
5625 7cbe629d 2018-08-04 stsp }
5626 7cbe629d 2018-08-04 stsp
5627 7cbe629d 2018-08-04 stsp static const struct got_error *
5628 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5629 6c4c42e0 2019-06-24 stsp {
5630 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5631 6c4c42e0 2019-06-24 stsp
5632 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5633 6c4c42e0 2019-06-24 stsp return NULL;
5634 6c4c42e0 2019-06-24 stsp }
5635 6c4c42e0 2019-06-24 stsp
5636 6c4c42e0 2019-06-24 stsp static const struct got_error *
5637 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5638 6c4c42e0 2019-06-24 stsp {
5639 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5640 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5641 6c4c42e0 2019-06-24 stsp int lineno;
5642 78eecffc 2022-06-23 thomas char *line = NULL;
5643 826082fe 2020-12-10 stsp size_t linesize = 0;
5644 826082fe 2020-12-10 stsp ssize_t linelen;
5645 6c4c42e0 2019-06-24 stsp
5646 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5647 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5648 6c4c42e0 2019-06-24 stsp return NULL;
5649 6c4c42e0 2019-06-24 stsp }
5650 6c4c42e0 2019-06-24 stsp
5651 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5652 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5653 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5654 6c4c42e0 2019-06-24 stsp else
5655 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5656 4b3f9dac 2021-12-17 thomas } else
5657 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5658 6c4c42e0 2019-06-24 stsp
5659 6c4c42e0 2019-06-24 stsp while (1) {
5660 6c4c42e0 2019-06-24 stsp off_t offset;
5661 6c4c42e0 2019-06-24 stsp
5662 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5663 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5664 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5665 6c4c42e0 2019-06-24 stsp break;
5666 6c4c42e0 2019-06-24 stsp }
5667 2246482e 2019-06-25 stsp
5668 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5669 6c4c42e0 2019-06-24 stsp lineno = 1;
5670 6c4c42e0 2019-06-24 stsp else
5671 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5672 6c4c42e0 2019-06-24 stsp }
5673 6c4c42e0 2019-06-24 stsp
5674 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5675 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5676 6c4c42e0 2019-06-24 stsp free(line);
5677 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5678 6c4c42e0 2019-06-24 stsp }
5679 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5680 78eecffc 2022-06-23 thomas if (linelen != -1) {
5681 78eecffc 2022-06-23 thomas char *exstr;
5682 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5683 78eecffc 2022-06-23 thomas if (err)
5684 78eecffc 2022-06-23 thomas break;
5685 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5686 78eecffc 2022-06-23 thomas &view->regmatch)) {
5687 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5688 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5689 78eecffc 2022-06-23 thomas free(exstr);
5690 78eecffc 2022-06-23 thomas break;
5691 78eecffc 2022-06-23 thomas }
5692 78eecffc 2022-06-23 thomas free(exstr);
5693 6c4c42e0 2019-06-24 stsp }
5694 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5695 6c4c42e0 2019-06-24 stsp lineno++;
5696 6c4c42e0 2019-06-24 stsp else
5697 6c4c42e0 2019-06-24 stsp lineno--;
5698 6c4c42e0 2019-06-24 stsp }
5699 826082fe 2020-12-10 stsp free(line);
5700 6c4c42e0 2019-06-24 stsp
5701 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5702 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5703 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5704 6c4c42e0 2019-06-24 stsp }
5705 6c4c42e0 2019-06-24 stsp
5706 05171be4 2022-06-23 thomas return err;
5707 6c4c42e0 2019-06-24 stsp }
5708 6c4c42e0 2019-06-24 stsp
5709 6c4c42e0 2019-06-24 stsp static const struct got_error *
5710 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5711 7cbe629d 2018-08-04 stsp {
5712 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5713 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5714 2b380cc8 2018-10-24 stsp int errcode;
5715 2b380cc8 2018-10-24 stsp
5716 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5717 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5718 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5719 2b380cc8 2018-10-24 stsp if (errcode)
5720 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5721 51fe7530 2019-08-19 stsp
5722 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5723 2b380cc8 2018-10-24 stsp }
5724 e5a0f69f 2018-08-18 stsp
5725 51fe7530 2019-08-19 stsp if (s->blame_complete)
5726 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5727 51fe7530 2019-08-19 stsp
5728 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5729 e5a0f69f 2018-08-18 stsp
5730 a5d43cac 2022-07-01 thomas view_border(view);
5731 e5a0f69f 2018-08-18 stsp return err;
5732 e5a0f69f 2018-08-18 stsp }
5733 e5a0f69f 2018-08-18 stsp
5734 e5a0f69f 2018-08-18 stsp static const struct got_error *
5735 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5736 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5737 eaeaa612 2022-07-20 thomas {
5738 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5739 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5740 eaeaa612 2022-07-20 thomas
5741 eaeaa612 2022-07-20 thomas *new_view = NULL;
5742 eaeaa612 2022-07-20 thomas
5743 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5744 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5745 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5746 eaeaa612 2022-07-20 thomas
5747 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5748 eaeaa612 2022-07-20 thomas if (err)
5749 eaeaa612 2022-07-20 thomas view_close(log_view);
5750 eaeaa612 2022-07-20 thomas else
5751 eaeaa612 2022-07-20 thomas *new_view = log_view;
5752 eaeaa612 2022-07-20 thomas
5753 eaeaa612 2022-07-20 thomas return err;
5754 eaeaa612 2022-07-20 thomas }
5755 eaeaa612 2022-07-20 thomas
5756 eaeaa612 2022-07-20 thomas static const struct got_error *
5757 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5758 e5a0f69f 2018-08-18 stsp {
5759 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5760 eaeaa612 2022-07-20 thomas struct tog_view *diff_view, *log_view;
5761 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5762 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5763 a5d43cac 2022-07-01 thomas
5764 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5765 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5766 a5d43cac 2022-07-01 thomas --eos; /* border */
5767 7cbe629d 2018-08-04 stsp
5768 e5a0f69f 2018-08-18 stsp switch (ch) {
5769 05171be4 2022-06-23 thomas case '0':
5770 05171be4 2022-06-23 thomas view->x = 0;
5771 05171be4 2022-06-23 thomas break;
5772 05171be4 2022-06-23 thomas case '$':
5773 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5774 07b0611c 2022-06-23 thomas view->count = 0;
5775 05171be4 2022-06-23 thomas break;
5776 05171be4 2022-06-23 thomas case KEY_RIGHT:
5777 05171be4 2022-06-23 thomas case 'l':
5778 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5779 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5780 07b0611c 2022-06-23 thomas else
5781 07b0611c 2022-06-23 thomas view->count = 0;
5782 05171be4 2022-06-23 thomas break;
5783 05171be4 2022-06-23 thomas case KEY_LEFT:
5784 05171be4 2022-06-23 thomas case 'h':
5785 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5786 07b0611c 2022-06-23 thomas if (view->x <= 0)
5787 07b0611c 2022-06-23 thomas view->count = 0;
5788 05171be4 2022-06-23 thomas break;
5789 1e37a5c2 2019-05-12 jcs case 'q':
5790 1e37a5c2 2019-05-12 jcs s->done = 1;
5791 4deef56f 2021-09-02 naddy break;
5792 4deef56f 2021-09-02 naddy case 'g':
5793 4deef56f 2021-09-02 naddy case KEY_HOME:
5794 4deef56f 2021-09-02 naddy s->selected_line = 1;
5795 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5796 07b0611c 2022-06-23 thomas view->count = 0;
5797 4deef56f 2021-09-02 naddy break;
5798 4deef56f 2021-09-02 naddy case 'G':
5799 4deef56f 2021-09-02 naddy case KEY_END:
5800 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5801 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5802 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5803 4deef56f 2021-09-02 naddy } else {
5804 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5805 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5806 4deef56f 2021-09-02 naddy }
5807 07b0611c 2022-06-23 thomas view->count = 0;
5808 1e37a5c2 2019-05-12 jcs break;
5809 1e37a5c2 2019-05-12 jcs case 'k':
5810 1e37a5c2 2019-05-12 jcs case KEY_UP:
5811 f7140bf5 2021-10-17 thomas case CTRL('p'):
5812 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5813 1e37a5c2 2019-05-12 jcs s->selected_line--;
5814 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5815 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5816 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5817 07b0611c 2022-06-23 thomas else
5818 07b0611c 2022-06-23 thomas view->count = 0;
5819 1e37a5c2 2019-05-12 jcs break;
5820 70f17a53 2022-06-13 thomas case CTRL('u'):
5821 23427b14 2022-06-23 thomas case 'u':
5822 70f17a53 2022-06-13 thomas nscroll /= 2;
5823 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5824 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5825 ea025d1d 2020-02-22 naddy case CTRL('b'):
5826 1c5e5faa 2022-06-23 thomas case 'b':
5827 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5828 07b0611c 2022-06-23 thomas if (view->count > 1)
5829 07b0611c 2022-06-23 thomas nscroll += nscroll;
5830 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5831 07b0611c 2022-06-23 thomas view->count = 0;
5832 e5a0f69f 2018-08-18 stsp break;
5833 1e37a5c2 2019-05-12 jcs }
5834 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5835 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5836 1e37a5c2 2019-05-12 jcs else
5837 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5838 1e37a5c2 2019-05-12 jcs break;
5839 1e37a5c2 2019-05-12 jcs case 'j':
5840 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5841 f7140bf5 2021-10-17 thomas case CTRL('n'):
5842 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5843 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5844 1e37a5c2 2019-05-12 jcs s->selected_line++;
5845 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5846 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5847 07b0611c 2022-06-23 thomas else
5848 07b0611c 2022-06-23 thomas view->count = 0;
5849 1e37a5c2 2019-05-12 jcs break;
5850 1c5e5faa 2022-06-23 thomas case 'c':
5851 1e37a5c2 2019-05-12 jcs case 'p': {
5852 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5853 07b0611c 2022-06-23 thomas
5854 07b0611c 2022-06-23 thomas view->count = 0;
5855 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5856 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5857 1e37a5c2 2019-05-12 jcs if (id == NULL)
5858 e5a0f69f 2018-08-18 stsp break;
5859 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5860 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5861 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5862 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5863 1e37a5c2 2019-05-12 jcs int obj_type;
5864 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5865 1e37a5c2 2019-05-12 jcs s->repo, id);
5866 e5a0f69f 2018-08-18 stsp if (err)
5867 e5a0f69f 2018-08-18 stsp break;
5868 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5869 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5870 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5871 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5872 e5a0f69f 2018-08-18 stsp break;
5873 e5a0f69f 2018-08-18 stsp }
5874 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5875 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5876 ec242592 2022-04-22 thomas s->repo, &pid->id);
5877 945f9229 2022-04-16 thomas if (err)
5878 945f9229 2022-04-16 thomas break;
5879 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5880 945f9229 2022-04-16 thomas pcommit, s->path);
5881 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5882 e5a0f69f 2018-08-18 stsp if (err) {
5883 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5884 1e37a5c2 2019-05-12 jcs err = NULL;
5885 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5886 e5a0f69f 2018-08-18 stsp break;
5887 e5a0f69f 2018-08-18 stsp }
5888 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5889 1e37a5c2 2019-05-12 jcs blob_id);
5890 1e37a5c2 2019-05-12 jcs free(blob_id);
5891 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5892 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5893 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5894 e5a0f69f 2018-08-18 stsp break;
5895 1e37a5c2 2019-05-12 jcs }
5896 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5897 ec242592 2022-04-22 thomas &pid->id);
5898 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5899 1e37a5c2 2019-05-12 jcs } else {
5900 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5901 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5902 1e37a5c2 2019-05-12 jcs break;
5903 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5904 1e37a5c2 2019-05-12 jcs id);
5905 1e37a5c2 2019-05-12 jcs }
5906 1e37a5c2 2019-05-12 jcs if (err)
5907 e5a0f69f 2018-08-18 stsp break;
5908 1e37a5c2 2019-05-12 jcs s->done = 1;
5909 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5910 1e37a5c2 2019-05-12 jcs s->done = 0;
5911 1e37a5c2 2019-05-12 jcs if (thread_err)
5912 1e37a5c2 2019-05-12 jcs break;
5913 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5914 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5915 a5388363 2020-12-01 naddy err = run_blame(view);
5916 1e37a5c2 2019-05-12 jcs if (err)
5917 1e37a5c2 2019-05-12 jcs break;
5918 1e37a5c2 2019-05-12 jcs break;
5919 1e37a5c2 2019-05-12 jcs }
5920 1c5e5faa 2022-06-23 thomas case 'C': {
5921 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5922 07b0611c 2022-06-23 thomas
5923 07b0611c 2022-06-23 thomas view->count = 0;
5924 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5925 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5926 1e37a5c2 2019-05-12 jcs break;
5927 1e37a5c2 2019-05-12 jcs s->done = 1;
5928 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5929 1e37a5c2 2019-05-12 jcs s->done = 0;
5930 1e37a5c2 2019-05-12 jcs if (thread_err)
5931 1e37a5c2 2019-05-12 jcs break;
5932 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5933 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5934 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5935 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5936 a5388363 2020-12-01 naddy err = run_blame(view);
5937 1e37a5c2 2019-05-12 jcs if (err)
5938 1e37a5c2 2019-05-12 jcs break;
5939 1e37a5c2 2019-05-12 jcs break;
5940 1e37a5c2 2019-05-12 jcs }
5941 eaeaa612 2022-07-20 thomas case 'L': {
5942 eaeaa612 2022-07-20 thomas struct got_object_id *id = NULL;
5943 eaeaa612 2022-07-20 thomas
5944 eaeaa612 2022-07-20 thomas view->count = 0;
5945 eaeaa612 2022-07-20 thomas id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5946 eaeaa612 2022-07-20 thomas s->first_displayed_line, s->selected_line);
5947 eaeaa612 2022-07-20 thomas if (id == NULL)
5948 eaeaa612 2022-07-20 thomas break;
5949 eaeaa612 2022-07-20 thomas
5950 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view))
5951 eaeaa612 2022-07-20 thomas view_get_split(view, &begin_y, &begin_x);
5952 eaeaa612 2022-07-20 thomas err = log_annotated_line(&log_view, begin_y, begin_x,
5953 eaeaa612 2022-07-20 thomas s->repo, id);
5954 eaeaa612 2022-07-20 thomas if (err)
5955 eaeaa612 2022-07-20 thomas break;
5956 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view) &&
5957 eaeaa612 2022-07-20 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5958 eaeaa612 2022-07-20 thomas err = view_init_hsplit(view, begin_y);
5959 eaeaa612 2022-07-20 thomas if (err)
5960 eaeaa612 2022-07-20 thomas break;
5961 eaeaa612 2022-07-20 thomas }
5962 eaeaa612 2022-07-20 thomas
5963 eaeaa612 2022-07-20 thomas view->focussed = 0;
5964 eaeaa612 2022-07-20 thomas log_view->focussed = 1;
5965 eaeaa612 2022-07-20 thomas log_view->mode = view->mode;
5966 eaeaa612 2022-07-20 thomas log_view->nlines = view->lines - begin_y;
5967 eaeaa612 2022-07-20 thomas if (view_is_parent_view(view)) {
5968 eaeaa612 2022-07-20 thomas view_transfer_size(log_view, view);
5969 eaeaa612 2022-07-20 thomas err = view_close_child(view);
5970 eaeaa612 2022-07-20 thomas if (err)
5971 eaeaa612 2022-07-20 thomas return err;
5972 eaeaa612 2022-07-20 thomas err = view_set_child(view, log_view);
5973 eaeaa612 2022-07-20 thomas if (err)
5974 eaeaa612 2022-07-20 thomas return err;
5975 eaeaa612 2022-07-20 thomas view->focus_child = 1;
5976 eaeaa612 2022-07-20 thomas } else
5977 eaeaa612 2022-07-20 thomas *new_view = log_view;
5978 eaeaa612 2022-07-20 thomas break;
5979 eaeaa612 2022-07-20 thomas }
5980 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5981 1e37a5c2 2019-05-12 jcs case '\r': {
5982 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5983 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5984 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5985 07b0611c 2022-06-23 thomas
5986 07b0611c 2022-06-23 thomas view->count = 0;
5987 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5988 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5989 1e37a5c2 2019-05-12 jcs if (id == NULL)
5990 1e37a5c2 2019-05-12 jcs break;
5991 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5992 1e37a5c2 2019-05-12 jcs if (err)
5993 1e37a5c2 2019-05-12 jcs break;
5994 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5995 4fc71f3b 2022-07-12 thomas if (*new_view) {
5996 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5997 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
5998 4fc71f3b 2022-07-12 thomas if (err)
5999 4fc71f3b 2022-07-12 thomas break;
6000 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6001 4fc71f3b 2022-07-12 thomas } else {
6002 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6003 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6004 a5d43cac 2022-07-01 thomas
6005 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6006 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6007 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6008 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6009 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6010 4fc71f3b 2022-07-12 thomas break;
6011 4fc71f3b 2022-07-12 thomas }
6012 15a94983 2018-12-23 stsp }
6013 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6014 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6015 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6016 1e37a5c2 2019-05-12 jcs if (err) {
6017 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6018 1e37a5c2 2019-05-12 jcs break;
6019 1e37a5c2 2019-05-12 jcs }
6020 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6021 4fc71f3b 2022-07-12 thomas s->selected_line;
6022 4fc71f3b 2022-07-12 thomas if (*new_view)
6023 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6024 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6025 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6026 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6027 a5d43cac 2022-07-01 thomas if (err)
6028 a5d43cac 2022-07-01 thomas break;
6029 a5d43cac 2022-07-01 thomas }
6030 a5d43cac 2022-07-01 thomas
6031 e78dc838 2020-12-04 stsp view->focussed = 0;
6032 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6033 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6034 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6035 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6036 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6037 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6038 1e37a5c2 2019-05-12 jcs if (err)
6039 34bc9ec9 2019-02-22 stsp break;
6040 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6041 40236d76 2022-06-23 thomas if (err)
6042 40236d76 2022-06-23 thomas break;
6043 e78dc838 2020-12-04 stsp view->focus_child = 1;
6044 1e37a5c2 2019-05-12 jcs } else
6045 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6046 1e37a5c2 2019-05-12 jcs if (err)
6047 e5a0f69f 2018-08-18 stsp break;
6048 1e37a5c2 2019-05-12 jcs break;
6049 1e37a5c2 2019-05-12 jcs }
6050 70f17a53 2022-06-13 thomas case CTRL('d'):
6051 23427b14 2022-06-23 thomas case 'd':
6052 70f17a53 2022-06-13 thomas nscroll /= 2;
6053 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6054 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6055 ea025d1d 2020-02-22 naddy case CTRL('f'):
6056 1c5e5faa 2022-06-23 thomas case 'f':
6057 1e37a5c2 2019-05-12 jcs case ' ':
6058 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6059 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6060 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6061 07b0611c 2022-06-23 thomas view->count = 0;
6062 e5a0f69f 2018-08-18 stsp break;
6063 1e37a5c2 2019-05-12 jcs }
6064 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6065 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6066 70f17a53 2022-06-13 thomas s->selected_line +=
6067 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6068 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6069 1e37a5c2 2019-05-12 jcs }
6070 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6071 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6072 1e37a5c2 2019-05-12 jcs else
6073 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6074 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6075 1e37a5c2 2019-05-12 jcs break;
6076 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6077 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6078 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6079 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6080 1e37a5c2 2019-05-12 jcs }
6081 1e37a5c2 2019-05-12 jcs break;
6082 1e37a5c2 2019-05-12 jcs default:
6083 07b0611c 2022-06-23 thomas view->count = 0;
6084 1e37a5c2 2019-05-12 jcs break;
6085 a70480e0 2018-06-23 stsp }
6086 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6087 adf4c9e0 2022-07-03 thomas }
6088 adf4c9e0 2022-07-03 thomas
6089 adf4c9e0 2022-07-03 thomas static const struct got_error *
6090 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6091 adf4c9e0 2022-07-03 thomas {
6092 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6093 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6094 adf4c9e0 2022-07-03 thomas
6095 adf4c9e0 2022-07-03 thomas view->count = 0;
6096 adf4c9e0 2022-07-03 thomas s->done = 1;
6097 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6098 adf4c9e0 2022-07-03 thomas s->done = 0;
6099 adf4c9e0 2022-07-03 thomas if (err)
6100 adf4c9e0 2022-07-03 thomas return err;
6101 adf4c9e0 2022-07-03 thomas return run_blame(view);
6102 a70480e0 2018-06-23 stsp }
6103 a70480e0 2018-06-23 stsp
6104 a70480e0 2018-06-23 stsp static const struct got_error *
6105 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6106 9f7d7167 2018-04-29 stsp {
6107 a70480e0 2018-06-23 stsp const struct got_error *error;
6108 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6109 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6110 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6111 0587e10c 2020-07-23 stsp char *link_target = NULL;
6112 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6113 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6114 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6115 a70480e0 2018-06-23 stsp int ch;
6116 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6117 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6118 a70480e0 2018-06-23 stsp
6119 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6120 a70480e0 2018-06-23 stsp switch (ch) {
6121 a70480e0 2018-06-23 stsp case 'c':
6122 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6123 a70480e0 2018-06-23 stsp break;
6124 69069811 2018-08-02 stsp case 'r':
6125 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6126 69069811 2018-08-02 stsp if (repo_path == NULL)
6127 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6128 9ba1d308 2019-10-21 stsp optarg);
6129 69069811 2018-08-02 stsp break;
6130 a70480e0 2018-06-23 stsp default:
6131 17020d27 2019-03-07 stsp usage_blame();
6132 a70480e0 2018-06-23 stsp /* NOTREACHED */
6133 a70480e0 2018-06-23 stsp }
6134 a70480e0 2018-06-23 stsp }
6135 a70480e0 2018-06-23 stsp
6136 a70480e0 2018-06-23 stsp argc -= optind;
6137 a70480e0 2018-06-23 stsp argv += optind;
6138 a70480e0 2018-06-23 stsp
6139 f135c941 2020-02-20 stsp if (argc != 1)
6140 a70480e0 2018-06-23 stsp usage_blame();
6141 6962eb72 2020-02-20 stsp
6142 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6143 7cd52833 2022-06-23 thomas if (error != NULL)
6144 7cd52833 2022-06-23 thomas goto done;
6145 7cd52833 2022-06-23 thomas
6146 69069811 2018-08-02 stsp if (repo_path == NULL) {
6147 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6148 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6149 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6150 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6151 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6152 c156c7a4 2020-12-18 stsp goto done;
6153 f135c941 2020-02-20 stsp if (worktree)
6154 eb41ed75 2019-02-05 stsp repo_path =
6155 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6156 f135c941 2020-02-20 stsp else
6157 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6158 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6159 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6160 c156c7a4 2020-12-18 stsp goto done;
6161 c156c7a4 2020-12-18 stsp }
6162 f135c941 2020-02-20 stsp }
6163 a915003a 2019-02-05 stsp
6164 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6165 c02c541e 2019-03-29 stsp if (error != NULL)
6166 8e94dd5b 2019-01-04 stsp goto done;
6167 69069811 2018-08-02 stsp
6168 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6169 f135c941 2020-02-20 stsp worktree);
6170 c02c541e 2019-03-29 stsp if (error)
6171 92205607 2019-01-04 stsp goto done;
6172 69069811 2018-08-02 stsp
6173 f135c941 2020-02-20 stsp init_curses();
6174 f135c941 2020-02-20 stsp
6175 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6176 51a10b52 2020-12-26 stsp if (error)
6177 51a10b52 2020-12-26 stsp goto done;
6178 51a10b52 2020-12-26 stsp
6179 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6180 eb41ed75 2019-02-05 stsp if (error)
6181 69069811 2018-08-02 stsp goto done;
6182 a70480e0 2018-06-23 stsp
6183 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6184 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6185 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6186 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6187 a70480e0 2018-06-23 stsp if (error != NULL)
6188 66b4983c 2018-06-23 stsp goto done;
6189 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6190 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6191 a70480e0 2018-06-23 stsp } else {
6192 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6193 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6194 a70480e0 2018-06-23 stsp }
6195 a19e88aa 2018-06-23 stsp if (error != NULL)
6196 8b473291 2019-02-21 stsp goto done;
6197 8b473291 2019-02-21 stsp
6198 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6199 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6200 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6201 e1cd8fed 2018-08-01 stsp goto done;
6202 e1cd8fed 2018-08-01 stsp }
6203 0587e10c 2020-07-23 stsp
6204 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6205 945f9229 2022-04-16 thomas if (error)
6206 945f9229 2022-04-16 thomas goto done;
6207 945f9229 2022-04-16 thomas
6208 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6209 945f9229 2022-04-16 thomas commit, repo);
6210 7cbe629d 2018-08-04 stsp if (error)
6211 7cbe629d 2018-08-04 stsp goto done;
6212 0587e10c 2020-07-23 stsp
6213 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6214 78756c87 2020-11-24 stsp commit_id, repo);
6215 0587e10c 2020-07-23 stsp if (error)
6216 0587e10c 2020-07-23 stsp goto done;
6217 12314ad4 2019-08-31 stsp if (worktree) {
6218 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6219 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6220 12314ad4 2019-08-31 stsp worktree = NULL;
6221 12314ad4 2019-08-31 stsp }
6222 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6223 a70480e0 2018-06-23 stsp done:
6224 69069811 2018-08-02 stsp free(repo_path);
6225 f135c941 2020-02-20 stsp free(in_repo_path);
6226 0587e10c 2020-07-23 stsp free(link_target);
6227 69069811 2018-08-02 stsp free(cwd);
6228 a70480e0 2018-06-23 stsp free(commit_id);
6229 945f9229 2022-04-16 thomas if (commit)
6230 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6231 eb41ed75 2019-02-05 stsp if (worktree)
6232 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6233 1d0f4054 2021-06-17 stsp if (repo) {
6234 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6235 1d0f4054 2021-06-17 stsp if (error == NULL)
6236 1d0f4054 2021-06-17 stsp error = close_err;
6237 1d0f4054 2021-06-17 stsp }
6238 7cd52833 2022-06-23 thomas if (pack_fds) {
6239 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6240 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6241 7cd52833 2022-06-23 thomas if (error == NULL)
6242 7cd52833 2022-06-23 thomas error = pack_err;
6243 7cd52833 2022-06-23 thomas }
6244 51a10b52 2020-12-26 stsp tog_free_refs();
6245 a70480e0 2018-06-23 stsp return error;
6246 ffd1d5e5 2018-06-23 stsp }
6247 ffd1d5e5 2018-06-23 stsp
6248 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6249 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6250 ffd1d5e5 2018-06-23 stsp {
6251 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6252 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6253 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6254 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6255 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6256 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6257 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6258 ffd1d5e5 2018-06-23 stsp
6259 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6260 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6261 a5d43cac 2022-07-01 thomas --limit; /* border */
6262 ffd1d5e5 2018-06-23 stsp
6263 f7d12f7e 2018-08-01 stsp werase(view->window);
6264 ffd1d5e5 2018-06-23 stsp
6265 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6266 ffd1d5e5 2018-06-23 stsp return NULL;
6267 ffd1d5e5 2018-06-23 stsp
6268 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6269 f91a2b48 2022-06-23 thomas 0, 0);
6270 ffd1d5e5 2018-06-23 stsp if (err)
6271 ffd1d5e5 2018-06-23 stsp return err;
6272 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6273 a3404814 2018-09-02 stsp wstandout(view->window);
6274 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6275 11b20872 2019-11-08 stsp if (tc)
6276 11b20872 2019-11-08 stsp wattr_on(view->window,
6277 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6278 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6279 11b20872 2019-11-08 stsp if (tc)
6280 11b20872 2019-11-08 stsp wattr_off(view->window,
6281 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6282 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6283 a3404814 2018-09-02 stsp wstandend(view->window);
6284 2550e4c3 2018-07-13 stsp free(wline);
6285 2550e4c3 2018-07-13 stsp wline = NULL;
6286 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6287 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6288 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6289 ffd1d5e5 2018-06-23 stsp return NULL;
6290 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6291 f91a2b48 2022-06-23 thomas 0, 0);
6292 ce52c690 2018-06-23 stsp if (err)
6293 ce52c690 2018-06-23 stsp return err;
6294 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6295 2550e4c3 2018-07-13 stsp free(wline);
6296 2550e4c3 2018-07-13 stsp wline = NULL;
6297 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6298 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6299 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6300 ffd1d5e5 2018-06-23 stsp return NULL;
6301 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6302 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6303 a1eca9bb 2018-06-23 stsp return NULL;
6304 ffd1d5e5 2018-06-23 stsp
6305 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6306 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6307 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6308 0cf4efb1 2018-09-29 stsp if (view->focussed)
6309 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6310 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6311 ffd1d5e5 2018-06-23 stsp }
6312 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6313 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6314 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6315 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6316 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6317 ffd1d5e5 2018-06-23 stsp return NULL;
6318 ffd1d5e5 2018-06-23 stsp n = 1;
6319 ffd1d5e5 2018-06-23 stsp } else {
6320 ffd1d5e5 2018-06-23 stsp n = 0;
6321 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6322 ffd1d5e5 2018-06-23 stsp }
6323 ffd1d5e5 2018-06-23 stsp
6324 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6325 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6326 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6327 848d6979 2019-08-12 stsp const char *modestr = "";
6328 56e0773d 2019-11-28 stsp mode_t mode;
6329 1d13200f 2018-07-12 stsp
6330 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6331 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6332 56e0773d 2019-11-28 stsp
6333 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6334 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6335 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6336 1d13200f 2018-07-12 stsp if (err)
6337 638f9024 2019-05-13 stsp return got_error_from_errno(
6338 230a42bd 2019-05-11 jcs "got_object_id_str");
6339 1d13200f 2018-07-12 stsp }
6340 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6341 63c5ca5d 2019-08-24 stsp modestr = "$";
6342 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6343 0d6c6ee3 2020-05-20 stsp int i;
6344 0d6c6ee3 2020-05-20 stsp
6345 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6346 d86d3b18 2020-12-01 naddy te, s->repo);
6347 0d6c6ee3 2020-05-20 stsp if (err) {
6348 0d6c6ee3 2020-05-20 stsp free(id_str);
6349 0d6c6ee3 2020-05-20 stsp return err;
6350 0d6c6ee3 2020-05-20 stsp }
6351 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6352 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6353 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6354 0d6c6ee3 2020-05-20 stsp }
6355 848d6979 2019-08-12 stsp modestr = "@";
6356 0d6c6ee3 2020-05-20 stsp }
6357 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6358 848d6979 2019-08-12 stsp modestr = "/";
6359 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6360 848d6979 2019-08-12 stsp modestr = "*";
6361 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6362 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6363 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6364 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6365 1d13200f 2018-07-12 stsp free(id_str);
6366 0d6c6ee3 2020-05-20 stsp free(link_target);
6367 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6368 1d13200f 2018-07-12 stsp }
6369 1d13200f 2018-07-12 stsp free(id_str);
6370 0d6c6ee3 2020-05-20 stsp free(link_target);
6371 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6372 f91a2b48 2022-06-23 thomas 0, 0);
6373 ffd1d5e5 2018-06-23 stsp if (err) {
6374 ffd1d5e5 2018-06-23 stsp free(line);
6375 ffd1d5e5 2018-06-23 stsp break;
6376 ffd1d5e5 2018-06-23 stsp }
6377 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6378 0cf4efb1 2018-09-29 stsp if (view->focussed)
6379 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6380 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6381 ffd1d5e5 2018-06-23 stsp }
6382 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6383 f26dddb7 2019-11-08 stsp if (tc)
6384 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6385 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6386 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6387 f26dddb7 2019-11-08 stsp if (tc)
6388 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6389 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6390 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6391 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6392 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6393 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6394 ffd1d5e5 2018-06-23 stsp free(line);
6395 2550e4c3 2018-07-13 stsp free(wline);
6396 2550e4c3 2018-07-13 stsp wline = NULL;
6397 ffd1d5e5 2018-06-23 stsp n++;
6398 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6399 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6400 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6401 ffd1d5e5 2018-06-23 stsp break;
6402 ffd1d5e5 2018-06-23 stsp }
6403 ffd1d5e5 2018-06-23 stsp
6404 ffd1d5e5 2018-06-23 stsp return err;
6405 ffd1d5e5 2018-06-23 stsp }
6406 ffd1d5e5 2018-06-23 stsp
6407 ffd1d5e5 2018-06-23 stsp static void
6408 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6409 ffd1d5e5 2018-06-23 stsp {
6410 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6411 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6412 fa86c4bf 2020-11-29 stsp int i = 0;
6413 ffd1d5e5 2018-06-23 stsp
6414 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6415 ffd1d5e5 2018-06-23 stsp return;
6416 ffd1d5e5 2018-06-23 stsp
6417 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6418 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6419 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6420 fa86c4bf 2020-11-29 stsp if (!isroot)
6421 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6422 fa86c4bf 2020-11-29 stsp break;
6423 fa86c4bf 2020-11-29 stsp }
6424 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6425 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6426 ffd1d5e5 2018-06-23 stsp }
6427 ffd1d5e5 2018-06-23 stsp }
6428 ffd1d5e5 2018-06-23 stsp
6429 a5d43cac 2022-07-01 thomas static const struct got_error *
6430 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6431 ffd1d5e5 2018-06-23 stsp {
6432 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6433 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6434 ffd1d5e5 2018-06-23 stsp int n = 0;
6435 ffd1d5e5 2018-06-23 stsp
6436 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6437 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6438 694d3271 2020-12-01 naddy s->first_displayed_entry);
6439 694d3271 2020-12-01 naddy else
6440 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6441 56e0773d 2019-11-28 stsp
6442 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6443 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6444 a5d43cac 2022-07-01 thomas if (last)
6445 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6446 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6447 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6448 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6449 768394f3 2019-01-24 stsp }
6450 ffd1d5e5 2018-06-23 stsp }
6451 a5d43cac 2022-07-01 thomas
6452 a5d43cac 2022-07-01 thomas return NULL;
6453 ffd1d5e5 2018-06-23 stsp }
6454 ffd1d5e5 2018-06-23 stsp
6455 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6456 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6457 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6458 ffd1d5e5 2018-06-23 stsp {
6459 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6460 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6461 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6462 ffd1d5e5 2018-06-23 stsp
6463 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6464 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6465 56e0773d 2019-11-28 stsp + 1 /* slash */;
6466 ce52c690 2018-06-23 stsp if (te)
6467 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6468 ce52c690 2018-06-23 stsp
6469 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6470 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6471 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6472 ffd1d5e5 2018-06-23 stsp
6473 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6474 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6475 d9765a41 2018-06-23 stsp while (pt) {
6476 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6477 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6478 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6479 cb2ebc8a 2018-06-23 stsp goto done;
6480 cb2ebc8a 2018-06-23 stsp }
6481 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6482 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6483 cb2ebc8a 2018-06-23 stsp goto done;
6484 cb2ebc8a 2018-06-23 stsp }
6485 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6486 ffd1d5e5 2018-06-23 stsp }
6487 ce52c690 2018-06-23 stsp if (te) {
6488 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6489 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6490 ce52c690 2018-06-23 stsp goto done;
6491 ce52c690 2018-06-23 stsp }
6492 cb2ebc8a 2018-06-23 stsp }
6493 ce52c690 2018-06-23 stsp done:
6494 ce52c690 2018-06-23 stsp if (err) {
6495 ce52c690 2018-06-23 stsp free(*path);
6496 ce52c690 2018-06-23 stsp *path = NULL;
6497 ce52c690 2018-06-23 stsp }
6498 ce52c690 2018-06-23 stsp return err;
6499 ce52c690 2018-06-23 stsp }
6500 ce52c690 2018-06-23 stsp
6501 ce52c690 2018-06-23 stsp static const struct got_error *
6502 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6503 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6504 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6505 ce52c690 2018-06-23 stsp {
6506 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6507 ce52c690 2018-06-23 stsp char *path;
6508 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6509 a0de39f3 2019-08-09 stsp
6510 a0de39f3 2019-08-09 stsp *new_view = NULL;
6511 69efd4c4 2018-07-18 stsp
6512 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6513 ce52c690 2018-06-23 stsp if (err)
6514 ce52c690 2018-06-23 stsp return err;
6515 ffd1d5e5 2018-06-23 stsp
6516 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6517 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6518 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6519 83ce39e3 2019-08-12 stsp goto done;
6520 83ce39e3 2019-08-12 stsp }
6521 cdf1ee82 2018-08-01 stsp
6522 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6523 e5a0f69f 2018-08-18 stsp if (err) {
6524 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6525 fc06ba56 2019-08-22 stsp err = NULL;
6526 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6527 e5a0f69f 2018-08-18 stsp } else
6528 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6529 83ce39e3 2019-08-12 stsp done:
6530 83ce39e3 2019-08-12 stsp free(path);
6531 69efd4c4 2018-07-18 stsp return err;
6532 69efd4c4 2018-07-18 stsp }
6533 69efd4c4 2018-07-18 stsp
6534 69efd4c4 2018-07-18 stsp static const struct got_error *
6535 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6536 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6537 69efd4c4 2018-07-18 stsp {
6538 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6539 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6540 69efd4c4 2018-07-18 stsp char *path;
6541 a0de39f3 2019-08-09 stsp
6542 a0de39f3 2019-08-09 stsp *new_view = NULL;
6543 69efd4c4 2018-07-18 stsp
6544 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6545 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6546 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6547 e5a0f69f 2018-08-18 stsp
6548 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6549 69efd4c4 2018-07-18 stsp if (err)
6550 69efd4c4 2018-07-18 stsp return err;
6551 69efd4c4 2018-07-18 stsp
6552 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6553 4e97c21c 2020-12-06 stsp path, 0);
6554 ba4f502b 2018-08-04 stsp if (err)
6555 e5a0f69f 2018-08-18 stsp view_close(log_view);
6556 e5a0f69f 2018-08-18 stsp else
6557 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6558 cb2ebc8a 2018-06-23 stsp free(path);
6559 cb2ebc8a 2018-06-23 stsp return err;
6560 ffd1d5e5 2018-06-23 stsp }
6561 ffd1d5e5 2018-06-23 stsp
6562 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6563 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6564 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6565 ffd1d5e5 2018-06-23 stsp {
6566 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6567 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6568 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6569 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6570 ffd1d5e5 2018-06-23 stsp
6571 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6572 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6573 bc573f3b 2021-07-10 stsp
6574 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6575 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6576 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6577 ffd1d5e5 2018-06-23 stsp
6578 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6579 bc573f3b 2021-07-10 stsp if (err)
6580 bc573f3b 2021-07-10 stsp goto done;
6581 bc573f3b 2021-07-10 stsp
6582 bc573f3b 2021-07-10 stsp /*
6583 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6584 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6585 bc573f3b 2021-07-10 stsp * closed on demand.
6586 bc573f3b 2021-07-10 stsp */
6587 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6588 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6589 bc573f3b 2021-07-10 stsp if (err)
6590 bc573f3b 2021-07-10 stsp goto done;
6591 bc573f3b 2021-07-10 stsp s->tree = s->root;
6592 bc573f3b 2021-07-10 stsp
6593 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6594 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6595 ffd1d5e5 2018-06-23 stsp goto done;
6596 ffd1d5e5 2018-06-23 stsp
6597 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6598 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6599 ffd1d5e5 2018-06-23 stsp goto done;
6600 ffd1d5e5 2018-06-23 stsp }
6601 ffd1d5e5 2018-06-23 stsp
6602 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6603 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6604 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6605 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6606 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6607 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6608 9cd7cbd1 2020-12-07 stsp goto done;
6609 9cd7cbd1 2020-12-07 stsp }
6610 9cd7cbd1 2020-12-07 stsp }
6611 fb2756b9 2018-08-04 stsp s->repo = repo;
6612 c0b01bdb 2019-11-08 stsp
6613 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6614 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6615 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6616 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6617 c0b01bdb 2019-11-08 stsp if (err)
6618 c0b01bdb 2019-11-08 stsp goto done;
6619 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6620 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6621 bc573f3b 2021-07-10 stsp if (err)
6622 c0b01bdb 2019-11-08 stsp goto done;
6623 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6624 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6625 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6626 bc573f3b 2021-07-10 stsp if (err)
6627 c0b01bdb 2019-11-08 stsp goto done;
6628 e5a0f69f 2018-08-18 stsp
6629 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6630 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6631 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6632 bc573f3b 2021-07-10 stsp if (err)
6633 11b20872 2019-11-08 stsp goto done;
6634 11b20872 2019-11-08 stsp
6635 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6636 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6637 bc573f3b 2021-07-10 stsp if (err)
6638 c0b01bdb 2019-11-08 stsp goto done;
6639 c0b01bdb 2019-11-08 stsp }
6640 c0b01bdb 2019-11-08 stsp
6641 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6642 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6643 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6644 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6645 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6646 ad80ab7b 2018-08-04 stsp done:
6647 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6648 bc573f3b 2021-07-10 stsp if (commit)
6649 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6650 bc573f3b 2021-07-10 stsp if (err)
6651 bc573f3b 2021-07-10 stsp close_tree_view(view);
6652 ad80ab7b 2018-08-04 stsp return err;
6653 ad80ab7b 2018-08-04 stsp }
6654 ad80ab7b 2018-08-04 stsp
6655 e5a0f69f 2018-08-18 stsp static const struct got_error *
6656 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6657 ad80ab7b 2018-08-04 stsp {
6658 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6659 ad80ab7b 2018-08-04 stsp
6660 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6661 fb2756b9 2018-08-04 stsp free(s->tree_label);
6662 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6663 6484ec90 2018-09-29 stsp free(s->commit_id);
6664 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6665 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6666 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6667 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6668 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6669 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6670 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6671 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6672 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6673 ad80ab7b 2018-08-04 stsp free(parent);
6674 ad80ab7b 2018-08-04 stsp
6675 ad80ab7b 2018-08-04 stsp }
6676 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6677 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6678 bc573f3b 2021-07-10 stsp if (s->root)
6679 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6680 7c32bd05 2019-06-22 stsp return NULL;
6681 7c32bd05 2019-06-22 stsp }
6682 7c32bd05 2019-06-22 stsp
6683 7c32bd05 2019-06-22 stsp static const struct got_error *
6684 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6685 7c32bd05 2019-06-22 stsp {
6686 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6687 7c32bd05 2019-06-22 stsp
6688 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6689 7c32bd05 2019-06-22 stsp return NULL;
6690 7c32bd05 2019-06-22 stsp }
6691 7c32bd05 2019-06-22 stsp
6692 7c32bd05 2019-06-22 stsp static int
6693 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6694 7c32bd05 2019-06-22 stsp {
6695 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6696 7c32bd05 2019-06-22 stsp
6697 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6698 56e0773d 2019-11-28 stsp 0) == 0;
6699 7c32bd05 2019-06-22 stsp }
6700 7c32bd05 2019-06-22 stsp
6701 7c32bd05 2019-06-22 stsp static const struct got_error *
6702 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6703 7c32bd05 2019-06-22 stsp {
6704 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6705 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6706 7c32bd05 2019-06-22 stsp
6707 7c32bd05 2019-06-22 stsp if (!view->searching) {
6708 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6709 7c32bd05 2019-06-22 stsp return NULL;
6710 7c32bd05 2019-06-22 stsp }
6711 7c32bd05 2019-06-22 stsp
6712 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6713 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6714 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6715 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6716 56e0773d 2019-11-28 stsp s->selected_entry);
6717 7c32bd05 2019-06-22 stsp else
6718 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6719 56e0773d 2019-11-28 stsp } else {
6720 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6721 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6722 56e0773d 2019-11-28 stsp else
6723 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6724 56e0773d 2019-11-28 stsp s->selected_entry);
6725 7c32bd05 2019-06-22 stsp }
6726 7c32bd05 2019-06-22 stsp } else {
6727 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6728 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6729 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6730 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6731 56e0773d 2019-11-28 stsp else
6732 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6733 7c32bd05 2019-06-22 stsp }
6734 7c32bd05 2019-06-22 stsp
6735 7c32bd05 2019-06-22 stsp while (1) {
6736 56e0773d 2019-11-28 stsp if (te == NULL) {
6737 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6738 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6739 ac66afb8 2019-06-24 stsp return NULL;
6740 ac66afb8 2019-06-24 stsp }
6741 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6742 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6743 56e0773d 2019-11-28 stsp else
6744 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6745 7c32bd05 2019-06-22 stsp }
6746 7c32bd05 2019-06-22 stsp
6747 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6748 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6749 56e0773d 2019-11-28 stsp s->matched_entry = te;
6750 7c32bd05 2019-06-22 stsp break;
6751 7c32bd05 2019-06-22 stsp }
6752 7c32bd05 2019-06-22 stsp
6753 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6754 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6755 56e0773d 2019-11-28 stsp else
6756 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6757 7c32bd05 2019-06-22 stsp }
6758 e5a0f69f 2018-08-18 stsp
6759 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6760 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6761 7c32bd05 2019-06-22 stsp s->selected = 0;
6762 7c32bd05 2019-06-22 stsp }
6763 7c32bd05 2019-06-22 stsp
6764 e5a0f69f 2018-08-18 stsp return NULL;
6765 ad80ab7b 2018-08-04 stsp }
6766 ad80ab7b 2018-08-04 stsp
6767 ad80ab7b 2018-08-04 stsp static const struct got_error *
6768 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6769 ad80ab7b 2018-08-04 stsp {
6770 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6771 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6772 e5a0f69f 2018-08-18 stsp char *parent_path;
6773 ad80ab7b 2018-08-04 stsp
6774 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6775 e5a0f69f 2018-08-18 stsp if (err)
6776 e5a0f69f 2018-08-18 stsp return err;
6777 ffd1d5e5 2018-06-23 stsp
6778 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6779 e5a0f69f 2018-08-18 stsp free(parent_path);
6780 669b5ffa 2018-10-07 stsp
6781 a5d43cac 2022-07-01 thomas view_border(view);
6782 e5a0f69f 2018-08-18 stsp return err;
6783 e5a0f69f 2018-08-18 stsp }
6784 ce52c690 2018-06-23 stsp
6785 e5a0f69f 2018-08-18 stsp static const struct got_error *
6786 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6787 e5a0f69f 2018-08-18 stsp {
6788 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6789 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6790 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6791 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6792 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6793 ffd1d5e5 2018-06-23 stsp
6794 e5a0f69f 2018-08-18 stsp switch (ch) {
6795 1e37a5c2 2019-05-12 jcs case 'i':
6796 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6797 07b0611c 2022-06-23 thomas view->count = 0;
6798 1e37a5c2 2019-05-12 jcs break;
6799 1e37a5c2 2019-05-12 jcs case 'l':
6800 07b0611c 2022-06-23 thomas view->count = 0;
6801 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6802 ffd1d5e5 2018-06-23 stsp break;
6803 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6804 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6805 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6806 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6807 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6808 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6809 444d5325 2022-07-03 thomas if (err)
6810 444d5325 2022-07-03 thomas break;
6811 444d5325 2022-07-03 thomas }
6812 e78dc838 2020-12-04 stsp view->focussed = 0;
6813 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6814 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6815 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6816 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6817 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
6818 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6819 1e37a5c2 2019-05-12 jcs if (err)
6820 1e37a5c2 2019-05-12 jcs return err;
6821 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6822 40236d76 2022-06-23 thomas if (err)
6823 40236d76 2022-06-23 thomas return err;
6824 e78dc838 2020-12-04 stsp view->focus_child = 1;
6825 1e37a5c2 2019-05-12 jcs } else
6826 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6827 152c1c93 2020-11-29 stsp break;
6828 152c1c93 2020-11-29 stsp case 'r':
6829 07b0611c 2022-06-23 thomas view->count = 0;
6830 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6831 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6832 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6833 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6834 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6835 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6836 152c1c93 2020-11-29 stsp if (err) {
6837 152c1c93 2020-11-29 stsp view_close(ref_view);
6838 152c1c93 2020-11-29 stsp return err;
6839 152c1c93 2020-11-29 stsp }
6840 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6841 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6842 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6843 444d5325 2022-07-03 thomas if (err)
6844 444d5325 2022-07-03 thomas break;
6845 444d5325 2022-07-03 thomas }
6846 e78dc838 2020-12-04 stsp view->focussed = 0;
6847 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6848 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6849 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6850 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6851 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
6852 152c1c93 2020-11-29 stsp err = view_close_child(view);
6853 152c1c93 2020-11-29 stsp if (err)
6854 152c1c93 2020-11-29 stsp return err;
6855 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6856 40236d76 2022-06-23 thomas if (err)
6857 40236d76 2022-06-23 thomas return err;
6858 e78dc838 2020-12-04 stsp view->focus_child = 1;
6859 152c1c93 2020-11-29 stsp } else
6860 152c1c93 2020-11-29 stsp *new_view = ref_view;
6861 e4526bf5 2021-09-03 naddy break;
6862 e4526bf5 2021-09-03 naddy case 'g':
6863 e4526bf5 2021-09-03 naddy case KEY_HOME:
6864 e4526bf5 2021-09-03 naddy s->selected = 0;
6865 07b0611c 2022-06-23 thomas view->count = 0;
6866 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6867 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6868 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6869 e4526bf5 2021-09-03 naddy else
6870 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6871 1e37a5c2 2019-05-12 jcs break;
6872 e4526bf5 2021-09-03 naddy case 'G':
6873 a5d43cac 2022-07-01 thomas case KEY_END: {
6874 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6875 a5d43cac 2022-07-01 thomas
6876 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6877 a5d43cac 2022-07-01 thomas --eos; /* border */
6878 e4526bf5 2021-09-03 naddy s->selected = 0;
6879 07b0611c 2022-06-23 thomas view->count = 0;
6880 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6881 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6882 e4526bf5 2021-09-03 naddy if (te == NULL) {
6883 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6884 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6885 e4526bf5 2021-09-03 naddy n++;
6886 e4526bf5 2021-09-03 naddy }
6887 e4526bf5 2021-09-03 naddy break;
6888 e4526bf5 2021-09-03 naddy }
6889 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6890 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6891 e4526bf5 2021-09-03 naddy }
6892 e4526bf5 2021-09-03 naddy if (n > 0)
6893 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6894 e4526bf5 2021-09-03 naddy break;
6895 a5d43cac 2022-07-01 thomas }
6896 1e37a5c2 2019-05-12 jcs case 'k':
6897 1e37a5c2 2019-05-12 jcs case KEY_UP:
6898 f7140bf5 2021-10-17 thomas case CTRL('p'):
6899 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6900 1e37a5c2 2019-05-12 jcs s->selected--;
6901 fa86c4bf 2020-11-29 stsp break;
6902 1e37a5c2 2019-05-12 jcs }
6903 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6904 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6905 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6906 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6907 07b0611c 2022-06-23 thomas view->count = 0;
6908 1e37a5c2 2019-05-12 jcs break;
6909 70f17a53 2022-06-13 thomas case CTRL('u'):
6910 23427b14 2022-06-23 thomas case 'u':
6911 70f17a53 2022-06-13 thomas nscroll /= 2;
6912 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6913 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6914 ea025d1d 2020-02-22 naddy case CTRL('b'):
6915 1c5e5faa 2022-06-23 thomas case 'b':
6916 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6917 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6918 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6919 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6920 fa86c4bf 2020-11-29 stsp } else {
6921 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6922 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6923 fa86c4bf 2020-11-29 stsp }
6924 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6925 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6926 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6927 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6928 07b0611c 2022-06-23 thomas view->count = 0;
6929 1e37a5c2 2019-05-12 jcs break;
6930 1e37a5c2 2019-05-12 jcs case 'j':
6931 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6932 f7140bf5 2021-10-17 thomas case CTRL('n'):
6933 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6934 1e37a5c2 2019-05-12 jcs s->selected++;
6935 1e37a5c2 2019-05-12 jcs break;
6936 1e37a5c2 2019-05-12 jcs }
6937 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6938 07b0611c 2022-06-23 thomas == NULL) {
6939 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6940 07b0611c 2022-06-23 thomas view->count = 0;
6941 1e37a5c2 2019-05-12 jcs break;
6942 07b0611c 2022-06-23 thomas }
6943 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6944 1e37a5c2 2019-05-12 jcs break;
6945 70f17a53 2022-06-13 thomas case CTRL('d'):
6946 23427b14 2022-06-23 thomas case 'd':
6947 70f17a53 2022-06-13 thomas nscroll /= 2;
6948 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6949 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6950 ea025d1d 2020-02-22 naddy case CTRL('f'):
6951 1c5e5faa 2022-06-23 thomas case 'f':
6952 4c2d69cb 2022-06-23 thomas case ' ':
6953 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6954 1e37a5c2 2019-05-12 jcs == NULL) {
6955 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6956 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6957 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6958 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6959 07b0611c 2022-06-23 thomas else
6960 07b0611c 2022-06-23 thomas view->count = 0;
6961 1e37a5c2 2019-05-12 jcs break;
6962 1e37a5c2 2019-05-12 jcs }
6963 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6964 1e37a5c2 2019-05-12 jcs break;
6965 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6966 1e37a5c2 2019-05-12 jcs case '\r':
6967 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6968 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6969 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6970 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6971 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6972 07b0611c 2022-06-23 thomas view->count = 0;
6973 1e37a5c2 2019-05-12 jcs break;
6974 07b0611c 2022-06-23 thomas }
6975 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6976 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6977 1e37a5c2 2019-05-12 jcs entry);
6978 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6979 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6980 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6981 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6982 1e37a5c2 2019-05-12 jcs s->selected_entry =
6983 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6984 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6985 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6986 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6987 a5d43cac 2022-07-01 thomas if (err)
6988 a5d43cac 2022-07-01 thomas break;
6989 a5d43cac 2022-07-01 thomas }
6990 1e37a5c2 2019-05-12 jcs free(parent);
6991 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6992 56e0773d 2019-11-28 stsp s->selected_entry))) {
6993 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6994 07b0611c 2022-06-23 thomas view->count = 0;
6995 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6996 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6997 1e37a5c2 2019-05-12 jcs if (err)
6998 1e37a5c2 2019-05-12 jcs break;
6999 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7000 941e9f74 2019-05-21 stsp if (err) {
7001 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7002 1e37a5c2 2019-05-12 jcs break;
7003 1e37a5c2 2019-05-12 jcs }
7004 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
7005 56e0773d 2019-11-28 stsp s->selected_entry))) {
7006 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
7007 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
7008 1e37a5c2 2019-05-12 jcs
7009 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
7010 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7011 a5d43cac 2022-07-01 thomas
7012 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
7013 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
7014 78756c87 2020-11-24 stsp s->commit_id, s->repo);
7015 1e37a5c2 2019-05-12 jcs if (err)
7016 1e37a5c2 2019-05-12 jcs break;
7017 a5d43cac 2022-07-01 thomas
7018 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7019 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7020 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7021 a5d43cac 2022-07-01 thomas if (err)
7022 a5d43cac 2022-07-01 thomas break;
7023 a5d43cac 2022-07-01 thomas }
7024 a5d43cac 2022-07-01 thomas
7025 07b0611c 2022-06-23 thomas view->count = 0;
7026 e78dc838 2020-12-04 stsp view->focussed = 0;
7027 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
7028 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
7029 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
7030 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
7031 53d2bdd3 2022-07-10 thomas view_transfer_size(blame_view, view);
7032 669b5ffa 2018-10-07 stsp err = view_close_child(view);
7033 669b5ffa 2018-10-07 stsp if (err)
7034 669b5ffa 2018-10-07 stsp return err;
7035 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
7036 40236d76 2022-06-23 thomas if (err)
7037 40236d76 2022-06-23 thomas return err;
7038 e78dc838 2020-12-04 stsp view->focus_child = 1;
7039 669b5ffa 2018-10-07 stsp } else
7040 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
7041 1e37a5c2 2019-05-12 jcs }
7042 1e37a5c2 2019-05-12 jcs break;
7043 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7044 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7045 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7046 07b0611c 2022-06-23 thomas view->count = 0;
7047 1e37a5c2 2019-05-12 jcs break;
7048 1e37a5c2 2019-05-12 jcs default:
7049 07b0611c 2022-06-23 thomas view->count = 0;
7050 1e37a5c2 2019-05-12 jcs break;
7051 ffd1d5e5 2018-06-23 stsp }
7052 e5a0f69f 2018-08-18 stsp
7053 ffd1d5e5 2018-06-23 stsp return err;
7054 9f7d7167 2018-04-29 stsp }
7055 9f7d7167 2018-04-29 stsp
7056 ffd1d5e5 2018-06-23 stsp __dead static void
7057 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7058 ffd1d5e5 2018-06-23 stsp {
7059 ffd1d5e5 2018-06-23 stsp endwin();
7060 91198554 2022-06-23 thomas fprintf(stderr,
7061 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7062 ffd1d5e5 2018-06-23 stsp getprogname());
7063 ffd1d5e5 2018-06-23 stsp exit(1);
7064 ffd1d5e5 2018-06-23 stsp }
7065 ffd1d5e5 2018-06-23 stsp
7066 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7067 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7068 ffd1d5e5 2018-06-23 stsp {
7069 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7070 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7071 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7072 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7073 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7074 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7075 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7076 4e97c21c 2020-12-06 stsp char *label = NULL;
7077 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7078 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7079 ffd1d5e5 2018-06-23 stsp int ch;
7080 5221c383 2018-08-01 stsp struct tog_view *view;
7081 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7082 70ac5f84 2019-03-28 stsp
7083 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7084 ffd1d5e5 2018-06-23 stsp switch (ch) {
7085 ffd1d5e5 2018-06-23 stsp case 'c':
7086 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7087 74283ab8 2020-02-07 stsp break;
7088 74283ab8 2020-02-07 stsp case 'r':
7089 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7090 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7091 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7092 74283ab8 2020-02-07 stsp optarg);
7093 ffd1d5e5 2018-06-23 stsp break;
7094 ffd1d5e5 2018-06-23 stsp default:
7095 e99e2d15 2020-11-24 naddy usage_tree();
7096 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7097 ffd1d5e5 2018-06-23 stsp }
7098 ffd1d5e5 2018-06-23 stsp }
7099 ffd1d5e5 2018-06-23 stsp
7100 ffd1d5e5 2018-06-23 stsp argc -= optind;
7101 ffd1d5e5 2018-06-23 stsp argv += optind;
7102 ffd1d5e5 2018-06-23 stsp
7103 55cccc34 2020-02-20 stsp if (argc > 1)
7104 e99e2d15 2020-11-24 naddy usage_tree();
7105 7cd52833 2022-06-23 thomas
7106 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7107 7cd52833 2022-06-23 thomas if (error != NULL)
7108 7cd52833 2022-06-23 thomas goto done;
7109 74283ab8 2020-02-07 stsp
7110 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7111 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7112 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7113 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7114 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7115 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7116 c156c7a4 2020-12-18 stsp goto done;
7117 55cccc34 2020-02-20 stsp if (worktree)
7118 52185f70 2019-02-05 stsp repo_path =
7119 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7120 55cccc34 2020-02-20 stsp else
7121 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7122 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7123 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7124 c156c7a4 2020-12-18 stsp goto done;
7125 c156c7a4 2020-12-18 stsp }
7126 55cccc34 2020-02-20 stsp }
7127 a915003a 2019-02-05 stsp
7128 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7129 c02c541e 2019-03-29 stsp if (error != NULL)
7130 52185f70 2019-02-05 stsp goto done;
7131 d188b9a6 2019-01-04 stsp
7132 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7133 55cccc34 2020-02-20 stsp repo, worktree);
7134 55cccc34 2020-02-20 stsp if (error)
7135 55cccc34 2020-02-20 stsp goto done;
7136 55cccc34 2020-02-20 stsp
7137 55cccc34 2020-02-20 stsp init_curses();
7138 55cccc34 2020-02-20 stsp
7139 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7140 c02c541e 2019-03-29 stsp if (error)
7141 52185f70 2019-02-05 stsp goto done;
7142 ffd1d5e5 2018-06-23 stsp
7143 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7144 51a10b52 2020-12-26 stsp if (error)
7145 51a10b52 2020-12-26 stsp goto done;
7146 51a10b52 2020-12-26 stsp
7147 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7148 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7149 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7150 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7151 4e97c21c 2020-12-06 stsp if (error)
7152 4e97c21c 2020-12-06 stsp goto done;
7153 4e97c21c 2020-12-06 stsp head_ref_name = label;
7154 4e97c21c 2020-12-06 stsp } else {
7155 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7156 4e97c21c 2020-12-06 stsp if (error == NULL)
7157 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7158 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7159 4e97c21c 2020-12-06 stsp goto done;
7160 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7161 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7162 4e97c21c 2020-12-06 stsp if (error)
7163 4e97c21c 2020-12-06 stsp goto done;
7164 4e97c21c 2020-12-06 stsp }
7165 ffd1d5e5 2018-06-23 stsp
7166 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7167 945f9229 2022-04-16 thomas if (error)
7168 945f9229 2022-04-16 thomas goto done;
7169 945f9229 2022-04-16 thomas
7170 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7171 5221c383 2018-08-01 stsp if (view == NULL) {
7172 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7173 5221c383 2018-08-01 stsp goto done;
7174 5221c383 2018-08-01 stsp }
7175 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7176 ad80ab7b 2018-08-04 stsp if (error)
7177 ad80ab7b 2018-08-04 stsp goto done;
7178 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7179 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7180 d91faf3b 2020-12-01 naddy in_repo_path);
7181 55cccc34 2020-02-20 stsp if (error)
7182 55cccc34 2020-02-20 stsp goto done;
7183 55cccc34 2020-02-20 stsp }
7184 55cccc34 2020-02-20 stsp
7185 55cccc34 2020-02-20 stsp if (worktree) {
7186 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7187 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7188 55cccc34 2020-02-20 stsp worktree = NULL;
7189 55cccc34 2020-02-20 stsp }
7190 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7191 ffd1d5e5 2018-06-23 stsp done:
7192 52185f70 2019-02-05 stsp free(repo_path);
7193 e4a0e26d 2020-02-20 stsp free(cwd);
7194 ffd1d5e5 2018-06-23 stsp free(commit_id);
7195 4e97c21c 2020-12-06 stsp free(label);
7196 486cd271 2020-12-06 stsp if (ref)
7197 486cd271 2020-12-06 stsp got_ref_close(ref);
7198 1d0f4054 2021-06-17 stsp if (repo) {
7199 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7200 1d0f4054 2021-06-17 stsp if (error == NULL)
7201 1d0f4054 2021-06-17 stsp error = close_err;
7202 1d0f4054 2021-06-17 stsp }
7203 7cd52833 2022-06-23 thomas if (pack_fds) {
7204 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7205 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7206 7cd52833 2022-06-23 thomas if (error == NULL)
7207 7cd52833 2022-06-23 thomas error = pack_err;
7208 7cd52833 2022-06-23 thomas }
7209 51a10b52 2020-12-26 stsp tog_free_refs();
7210 ffd1d5e5 2018-06-23 stsp return error;
7211 6458efa5 2020-11-24 stsp }
7212 6458efa5 2020-11-24 stsp
7213 6458efa5 2020-11-24 stsp static const struct got_error *
7214 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7215 6458efa5 2020-11-24 stsp {
7216 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7217 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7218 6458efa5 2020-11-24 stsp
7219 6458efa5 2020-11-24 stsp s->nrefs = 0;
7220 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7221 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7222 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7223 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7224 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7225 6458efa5 2020-11-24 stsp continue;
7226 6458efa5 2020-11-24 stsp
7227 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7228 6458efa5 2020-11-24 stsp if (re == NULL)
7229 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7230 6458efa5 2020-11-24 stsp
7231 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7232 8924d611 2020-12-26 stsp if (re->ref == NULL)
7233 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7234 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7235 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7236 6458efa5 2020-11-24 stsp }
7237 6458efa5 2020-11-24 stsp
7238 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7239 6458efa5 2020-11-24 stsp return NULL;
7240 6458efa5 2020-11-24 stsp }
7241 6458efa5 2020-11-24 stsp
7242 ef20f542 2022-06-26 thomas static void
7243 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7244 6458efa5 2020-11-24 stsp {
7245 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7246 6458efa5 2020-11-24 stsp
7247 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7248 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7249 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7250 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7251 6458efa5 2020-11-24 stsp free(re);
7252 6458efa5 2020-11-24 stsp }
7253 6458efa5 2020-11-24 stsp }
7254 6458efa5 2020-11-24 stsp
7255 6458efa5 2020-11-24 stsp static const struct got_error *
7256 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7257 6458efa5 2020-11-24 stsp {
7258 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7259 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7260 6458efa5 2020-11-24 stsp
7261 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7262 6458efa5 2020-11-24 stsp s->repo = repo;
7263 6458efa5 2020-11-24 stsp
7264 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7265 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7266 6458efa5 2020-11-24 stsp
7267 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7268 6458efa5 2020-11-24 stsp if (err)
7269 6458efa5 2020-11-24 stsp return err;
7270 34ba6917 2020-11-30 stsp
7271 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7272 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7273 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7274 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7275 6458efa5 2020-11-24 stsp if (err)
7276 6458efa5 2020-11-24 stsp goto done;
7277 6458efa5 2020-11-24 stsp
7278 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7279 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7280 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7281 6458efa5 2020-11-24 stsp if (err)
7282 6458efa5 2020-11-24 stsp goto done;
7283 6458efa5 2020-11-24 stsp
7284 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7285 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7286 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7287 2183bbf6 2022-01-23 thomas if (err)
7288 2183bbf6 2022-01-23 thomas goto done;
7289 2183bbf6 2022-01-23 thomas
7290 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7291 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7292 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7293 6458efa5 2020-11-24 stsp if (err)
7294 6458efa5 2020-11-24 stsp goto done;
7295 6458efa5 2020-11-24 stsp }
7296 6458efa5 2020-11-24 stsp
7297 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7298 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7299 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7300 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7301 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7302 6458efa5 2020-11-24 stsp done:
7303 6458efa5 2020-11-24 stsp if (err)
7304 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7305 6458efa5 2020-11-24 stsp return err;
7306 6458efa5 2020-11-24 stsp }
7307 6458efa5 2020-11-24 stsp
7308 6458efa5 2020-11-24 stsp static const struct got_error *
7309 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7310 6458efa5 2020-11-24 stsp {
7311 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7312 6458efa5 2020-11-24 stsp
7313 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7314 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7315 6458efa5 2020-11-24 stsp
7316 6458efa5 2020-11-24 stsp return NULL;
7317 9f7d7167 2018-04-29 stsp }
7318 ce5b7c56 2019-07-09 stsp
7319 6458efa5 2020-11-24 stsp static const struct got_error *
7320 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7321 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7322 6458efa5 2020-11-24 stsp {
7323 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7324 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7325 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7326 6458efa5 2020-11-24 stsp int obj_type;
7327 6458efa5 2020-11-24 stsp
7328 c42c9805 2020-11-24 stsp *commit_id = NULL;
7329 6458efa5 2020-11-24 stsp
7330 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7331 6458efa5 2020-11-24 stsp if (err)
7332 6458efa5 2020-11-24 stsp return err;
7333 6458efa5 2020-11-24 stsp
7334 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7335 6458efa5 2020-11-24 stsp if (err)
7336 6458efa5 2020-11-24 stsp goto done;
7337 6458efa5 2020-11-24 stsp
7338 6458efa5 2020-11-24 stsp switch (obj_type) {
7339 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7340 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7341 6458efa5 2020-11-24 stsp break;
7342 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7343 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7344 6458efa5 2020-11-24 stsp if (err)
7345 6458efa5 2020-11-24 stsp goto done;
7346 c42c9805 2020-11-24 stsp free(obj_id);
7347 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7348 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7349 c42c9805 2020-11-24 stsp if (err)
7350 6458efa5 2020-11-24 stsp goto done;
7351 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7352 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7353 c42c9805 2020-11-24 stsp goto done;
7354 c42c9805 2020-11-24 stsp }
7355 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7356 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7357 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7358 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7359 c42c9805 2020-11-24 stsp goto done;
7360 c42c9805 2020-11-24 stsp }
7361 6458efa5 2020-11-24 stsp break;
7362 6458efa5 2020-11-24 stsp default:
7363 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7364 c42c9805 2020-11-24 stsp break;
7365 6458efa5 2020-11-24 stsp }
7366 6458efa5 2020-11-24 stsp
7367 c42c9805 2020-11-24 stsp done:
7368 c42c9805 2020-11-24 stsp if (tag)
7369 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7370 c42c9805 2020-11-24 stsp if (err) {
7371 c42c9805 2020-11-24 stsp free(*commit_id);
7372 c42c9805 2020-11-24 stsp *commit_id = NULL;
7373 c42c9805 2020-11-24 stsp }
7374 c42c9805 2020-11-24 stsp return err;
7375 c42c9805 2020-11-24 stsp }
7376 c42c9805 2020-11-24 stsp
7377 c42c9805 2020-11-24 stsp static const struct got_error *
7378 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7379 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7380 c42c9805 2020-11-24 stsp {
7381 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7382 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7383 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7384 c42c9805 2020-11-24 stsp
7385 c42c9805 2020-11-24 stsp *new_view = NULL;
7386 c42c9805 2020-11-24 stsp
7387 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7388 c42c9805 2020-11-24 stsp if (err) {
7389 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7390 c42c9805 2020-11-24 stsp return err;
7391 c42c9805 2020-11-24 stsp else
7392 c42c9805 2020-11-24 stsp return NULL;
7393 c42c9805 2020-11-24 stsp }
7394 c42c9805 2020-11-24 stsp
7395 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7396 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7397 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7398 6458efa5 2020-11-24 stsp goto done;
7399 6458efa5 2020-11-24 stsp }
7400 6458efa5 2020-11-24 stsp
7401 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7402 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7403 6458efa5 2020-11-24 stsp done:
7404 6458efa5 2020-11-24 stsp if (err)
7405 6458efa5 2020-11-24 stsp view_close(log_view);
7406 6458efa5 2020-11-24 stsp else
7407 6458efa5 2020-11-24 stsp *new_view = log_view;
7408 c42c9805 2020-11-24 stsp free(commit_id);
7409 6458efa5 2020-11-24 stsp return err;
7410 6458efa5 2020-11-24 stsp }
7411 6458efa5 2020-11-24 stsp
7412 ce5b7c56 2019-07-09 stsp static void
7413 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7414 6458efa5 2020-11-24 stsp {
7415 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7416 34ba6917 2020-11-30 stsp int i = 0;
7417 6458efa5 2020-11-24 stsp
7418 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7419 6458efa5 2020-11-24 stsp return;
7420 6458efa5 2020-11-24 stsp
7421 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7422 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7423 34ba6917 2020-11-30 stsp if (re == NULL)
7424 34ba6917 2020-11-30 stsp break;
7425 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7426 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7427 6458efa5 2020-11-24 stsp }
7428 6458efa5 2020-11-24 stsp }
7429 6458efa5 2020-11-24 stsp
7430 a5d43cac 2022-07-01 thomas static const struct got_error *
7431 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7432 6458efa5 2020-11-24 stsp {
7433 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7434 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7435 6458efa5 2020-11-24 stsp int n = 0;
7436 6458efa5 2020-11-24 stsp
7437 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7438 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7439 6458efa5 2020-11-24 stsp else
7440 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7441 6458efa5 2020-11-24 stsp
7442 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7443 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7444 a5d43cac 2022-07-01 thomas if (last)
7445 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7446 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7447 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7448 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7449 6458efa5 2020-11-24 stsp }
7450 6458efa5 2020-11-24 stsp }
7451 a5d43cac 2022-07-01 thomas
7452 a5d43cac 2022-07-01 thomas return NULL;
7453 6458efa5 2020-11-24 stsp }
7454 6458efa5 2020-11-24 stsp
7455 6458efa5 2020-11-24 stsp static const struct got_error *
7456 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7457 6458efa5 2020-11-24 stsp {
7458 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7459 6458efa5 2020-11-24 stsp
7460 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7461 6458efa5 2020-11-24 stsp return NULL;
7462 6458efa5 2020-11-24 stsp }
7463 6458efa5 2020-11-24 stsp
7464 6458efa5 2020-11-24 stsp static int
7465 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7466 6458efa5 2020-11-24 stsp {
7467 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7468 6458efa5 2020-11-24 stsp
7469 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7470 6458efa5 2020-11-24 stsp 0) == 0;
7471 6458efa5 2020-11-24 stsp }
7472 6458efa5 2020-11-24 stsp
7473 6458efa5 2020-11-24 stsp static const struct got_error *
7474 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7475 6458efa5 2020-11-24 stsp {
7476 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7478 6458efa5 2020-11-24 stsp
7479 6458efa5 2020-11-24 stsp if (!view->searching) {
7480 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7481 6458efa5 2020-11-24 stsp return NULL;
7482 6458efa5 2020-11-24 stsp }
7483 6458efa5 2020-11-24 stsp
7484 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7485 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7486 6458efa5 2020-11-24 stsp if (s->selected_entry)
7487 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7488 6458efa5 2020-11-24 stsp else
7489 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7490 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7491 6458efa5 2020-11-24 stsp } else {
7492 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7493 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7494 6458efa5 2020-11-24 stsp else
7495 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7496 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7497 6458efa5 2020-11-24 stsp }
7498 6458efa5 2020-11-24 stsp } else {
7499 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7500 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7501 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7502 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7503 6458efa5 2020-11-24 stsp else
7504 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7505 6458efa5 2020-11-24 stsp }
7506 6458efa5 2020-11-24 stsp
7507 6458efa5 2020-11-24 stsp while (1) {
7508 6458efa5 2020-11-24 stsp if (re == NULL) {
7509 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7510 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7511 6458efa5 2020-11-24 stsp return NULL;
7512 6458efa5 2020-11-24 stsp }
7513 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7514 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7515 6458efa5 2020-11-24 stsp else
7516 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7517 6458efa5 2020-11-24 stsp }
7518 6458efa5 2020-11-24 stsp
7519 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7520 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7521 6458efa5 2020-11-24 stsp s->matched_entry = re;
7522 6458efa5 2020-11-24 stsp break;
7523 6458efa5 2020-11-24 stsp }
7524 6458efa5 2020-11-24 stsp
7525 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7526 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7527 6458efa5 2020-11-24 stsp else
7528 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7529 6458efa5 2020-11-24 stsp }
7530 6458efa5 2020-11-24 stsp
7531 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7532 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7533 6458efa5 2020-11-24 stsp s->selected = 0;
7534 6458efa5 2020-11-24 stsp }
7535 6458efa5 2020-11-24 stsp
7536 6458efa5 2020-11-24 stsp return NULL;
7537 6458efa5 2020-11-24 stsp }
7538 6458efa5 2020-11-24 stsp
7539 6458efa5 2020-11-24 stsp static const struct got_error *
7540 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7541 6458efa5 2020-11-24 stsp {
7542 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7543 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7544 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7545 6458efa5 2020-11-24 stsp char *line = NULL;
7546 6458efa5 2020-11-24 stsp wchar_t *wline;
7547 6458efa5 2020-11-24 stsp struct tog_color *tc;
7548 6458efa5 2020-11-24 stsp int width, n;
7549 6458efa5 2020-11-24 stsp int limit = view->nlines;
7550 6458efa5 2020-11-24 stsp
7551 6458efa5 2020-11-24 stsp werase(view->window);
7552 6458efa5 2020-11-24 stsp
7553 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7554 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7555 a5d43cac 2022-07-01 thomas --limit; /* border */
7556 6458efa5 2020-11-24 stsp
7557 6458efa5 2020-11-24 stsp if (limit == 0)
7558 6458efa5 2020-11-24 stsp return NULL;
7559 6458efa5 2020-11-24 stsp
7560 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7561 6458efa5 2020-11-24 stsp
7562 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7563 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7564 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7565 6458efa5 2020-11-24 stsp
7566 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7567 6458efa5 2020-11-24 stsp if (err) {
7568 6458efa5 2020-11-24 stsp free(line);
7569 6458efa5 2020-11-24 stsp return err;
7570 6458efa5 2020-11-24 stsp }
7571 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7572 6458efa5 2020-11-24 stsp wstandout(view->window);
7573 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7574 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7575 6458efa5 2020-11-24 stsp wstandend(view->window);
7576 6458efa5 2020-11-24 stsp free(wline);
7577 6458efa5 2020-11-24 stsp wline = NULL;
7578 6458efa5 2020-11-24 stsp free(line);
7579 6458efa5 2020-11-24 stsp line = NULL;
7580 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7581 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7582 6458efa5 2020-11-24 stsp if (--limit <= 0)
7583 6458efa5 2020-11-24 stsp return NULL;
7584 6458efa5 2020-11-24 stsp
7585 6458efa5 2020-11-24 stsp n = 0;
7586 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7587 6458efa5 2020-11-24 stsp char *line = NULL;
7588 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7589 6458efa5 2020-11-24 stsp
7590 84227eb1 2022-06-23 thomas if (s->show_date) {
7591 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7592 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7593 84227eb1 2022-06-23 thomas struct got_object_id *id;
7594 84227eb1 2022-06-23 thomas struct tm tm;
7595 84227eb1 2022-06-23 thomas time_t t;
7596 84227eb1 2022-06-23 thomas
7597 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7598 84227eb1 2022-06-23 thomas if (err)
7599 84227eb1 2022-06-23 thomas return err;
7600 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7601 84227eb1 2022-06-23 thomas if (err) {
7602 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7603 84227eb1 2022-06-23 thomas free(id);
7604 84227eb1 2022-06-23 thomas return err;
7605 84227eb1 2022-06-23 thomas }
7606 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7607 84227eb1 2022-06-23 thomas id);
7608 84227eb1 2022-06-23 thomas if (err) {
7609 84227eb1 2022-06-23 thomas free(id);
7610 84227eb1 2022-06-23 thomas return err;
7611 84227eb1 2022-06-23 thomas }
7612 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7613 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7614 84227eb1 2022-06-23 thomas } else {
7615 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7616 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7617 84227eb1 2022-06-23 thomas }
7618 84227eb1 2022-06-23 thomas free(id);
7619 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7620 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7621 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7622 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7623 84227eb1 2022-06-23 thomas }
7624 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7625 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7626 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7627 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7628 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7629 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7630 6458efa5 2020-11-24 stsp struct got_object_id *id;
7631 6458efa5 2020-11-24 stsp char *id_str;
7632 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7633 6458efa5 2020-11-24 stsp if (err)
7634 6458efa5 2020-11-24 stsp return err;
7635 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7636 6458efa5 2020-11-24 stsp if (err) {
7637 6458efa5 2020-11-24 stsp free(id);
7638 6458efa5 2020-11-24 stsp return err;
7639 6458efa5 2020-11-24 stsp }
7640 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7641 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7642 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7643 6458efa5 2020-11-24 stsp free(id);
7644 6458efa5 2020-11-24 stsp free(id_str);
7645 6458efa5 2020-11-24 stsp return err;
7646 6458efa5 2020-11-24 stsp }
7647 6458efa5 2020-11-24 stsp free(id);
7648 6458efa5 2020-11-24 stsp free(id_str);
7649 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7650 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7651 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7652 6458efa5 2020-11-24 stsp
7653 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7654 f91a2b48 2022-06-23 thomas 0, 0);
7655 6458efa5 2020-11-24 stsp if (err) {
7656 6458efa5 2020-11-24 stsp free(line);
7657 6458efa5 2020-11-24 stsp return err;
7658 6458efa5 2020-11-24 stsp }
7659 6458efa5 2020-11-24 stsp if (n == s->selected) {
7660 6458efa5 2020-11-24 stsp if (view->focussed)
7661 6458efa5 2020-11-24 stsp wstandout(view->window);
7662 6458efa5 2020-11-24 stsp s->selected_entry = re;
7663 6458efa5 2020-11-24 stsp }
7664 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7665 6458efa5 2020-11-24 stsp if (tc)
7666 6458efa5 2020-11-24 stsp wattr_on(view->window,
7667 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7668 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7669 6458efa5 2020-11-24 stsp if (tc)
7670 6458efa5 2020-11-24 stsp wattr_off(view->window,
7671 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7672 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7673 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7674 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7675 6458efa5 2020-11-24 stsp wstandend(view->window);
7676 6458efa5 2020-11-24 stsp free(line);
7677 6458efa5 2020-11-24 stsp free(wline);
7678 6458efa5 2020-11-24 stsp wline = NULL;
7679 6458efa5 2020-11-24 stsp n++;
7680 6458efa5 2020-11-24 stsp s->ndisplayed++;
7681 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7682 6458efa5 2020-11-24 stsp
7683 6458efa5 2020-11-24 stsp limit--;
7684 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7685 6458efa5 2020-11-24 stsp }
7686 6458efa5 2020-11-24 stsp
7687 a5d43cac 2022-07-01 thomas view_border(view);
7688 6458efa5 2020-11-24 stsp return err;
7689 6458efa5 2020-11-24 stsp }
7690 6458efa5 2020-11-24 stsp
7691 6458efa5 2020-11-24 stsp static const struct got_error *
7692 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7693 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7694 c42c9805 2020-11-24 stsp {
7695 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7696 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7697 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7698 c42c9805 2020-11-24 stsp
7699 c42c9805 2020-11-24 stsp *new_view = NULL;
7700 c42c9805 2020-11-24 stsp
7701 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7702 c42c9805 2020-11-24 stsp if (err) {
7703 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7704 c42c9805 2020-11-24 stsp return err;
7705 c42c9805 2020-11-24 stsp else
7706 c42c9805 2020-11-24 stsp return NULL;
7707 c42c9805 2020-11-24 stsp }
7708 c42c9805 2020-11-24 stsp
7709 c42c9805 2020-11-24 stsp
7710 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7711 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7712 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7713 c42c9805 2020-11-24 stsp goto done;
7714 c42c9805 2020-11-24 stsp }
7715 c42c9805 2020-11-24 stsp
7716 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7717 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7718 c42c9805 2020-11-24 stsp if (err)
7719 c42c9805 2020-11-24 stsp goto done;
7720 c42c9805 2020-11-24 stsp
7721 c42c9805 2020-11-24 stsp *new_view = tree_view;
7722 c42c9805 2020-11-24 stsp done:
7723 c42c9805 2020-11-24 stsp free(commit_id);
7724 c42c9805 2020-11-24 stsp return err;
7725 c42c9805 2020-11-24 stsp }
7726 c42c9805 2020-11-24 stsp static const struct got_error *
7727 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7728 6458efa5 2020-11-24 stsp {
7729 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7730 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7731 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7732 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7733 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7734 6458efa5 2020-11-24 stsp
7735 6458efa5 2020-11-24 stsp switch (ch) {
7736 6458efa5 2020-11-24 stsp case 'i':
7737 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7738 07b0611c 2022-06-23 thomas view->count = 0;
7739 3bfadbd4 2021-11-20 thomas break;
7740 84227eb1 2022-06-23 thomas case 'm':
7741 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7742 07b0611c 2022-06-23 thomas view->count = 0;
7743 84227eb1 2022-06-23 thomas break;
7744 98182bd0 2021-11-20 thomas case 'o':
7745 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7746 07b0611c 2022-06-23 thomas view->count = 0;
7747 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7748 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7749 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7750 c591440f 2021-11-20 thomas if (err)
7751 c591440f 2021-11-20 thomas break;
7752 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7753 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7754 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7755 3bfadbd4 2021-11-20 thomas if (err)
7756 3bfadbd4 2021-11-20 thomas break;
7757 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7758 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7759 6458efa5 2020-11-24 stsp break;
7760 6458efa5 2020-11-24 stsp case KEY_ENTER:
7761 6458efa5 2020-11-24 stsp case '\r':
7762 07b0611c 2022-06-23 thomas view->count = 0;
7763 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7764 6458efa5 2020-11-24 stsp break;
7765 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7766 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7767 a5d43cac 2022-07-01 thomas
7768 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7769 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7770 a5d43cac 2022-07-01 thomas if (err)
7771 a5d43cac 2022-07-01 thomas break;
7772 a5d43cac 2022-07-01 thomas
7773 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7774 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7775 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7776 a5d43cac 2022-07-01 thomas if (err)
7777 a5d43cac 2022-07-01 thomas break;
7778 a5d43cac 2022-07-01 thomas }
7779 a5d43cac 2022-07-01 thomas
7780 e78dc838 2020-12-04 stsp view->focussed = 0;
7781 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7782 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7783 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7784 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7785 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
7786 6458efa5 2020-11-24 stsp err = view_close_child(view);
7787 6458efa5 2020-11-24 stsp if (err)
7788 6458efa5 2020-11-24 stsp return err;
7789 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7790 40236d76 2022-06-23 thomas if (err)
7791 40236d76 2022-06-23 thomas return err;
7792 e78dc838 2020-12-04 stsp view->focus_child = 1;
7793 6458efa5 2020-11-24 stsp } else
7794 6458efa5 2020-11-24 stsp *new_view = log_view;
7795 6458efa5 2020-11-24 stsp break;
7796 c42c9805 2020-11-24 stsp case 't':
7797 07b0611c 2022-06-23 thomas view->count = 0;
7798 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7799 c42c9805 2020-11-24 stsp break;
7800 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7801 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7802 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7803 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7804 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7805 c42c9805 2020-11-24 stsp break;
7806 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7807 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7808 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7809 444d5325 2022-07-03 thomas if (err)
7810 444d5325 2022-07-03 thomas break;
7811 444d5325 2022-07-03 thomas }
7812 e78dc838 2020-12-04 stsp view->focussed = 0;
7813 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7814 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7815 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7816 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7817 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
7818 c42c9805 2020-11-24 stsp err = view_close_child(view);
7819 c42c9805 2020-11-24 stsp if (err)
7820 c42c9805 2020-11-24 stsp return err;
7821 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7822 40236d76 2022-06-23 thomas if (err)
7823 40236d76 2022-06-23 thomas return err;
7824 e78dc838 2020-12-04 stsp view->focus_child = 1;
7825 c42c9805 2020-11-24 stsp } else
7826 c42c9805 2020-11-24 stsp *new_view = tree_view;
7827 c42c9805 2020-11-24 stsp break;
7828 e4526bf5 2021-09-03 naddy case 'g':
7829 e4526bf5 2021-09-03 naddy case KEY_HOME:
7830 e4526bf5 2021-09-03 naddy s->selected = 0;
7831 07b0611c 2022-06-23 thomas view->count = 0;
7832 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7833 e4526bf5 2021-09-03 naddy break;
7834 e4526bf5 2021-09-03 naddy case 'G':
7835 a5d43cac 2022-07-01 thomas case KEY_END: {
7836 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7837 a5d43cac 2022-07-01 thomas
7838 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7839 a5d43cac 2022-07-01 thomas --eos; /* border */
7840 e4526bf5 2021-09-03 naddy s->selected = 0;
7841 07b0611c 2022-06-23 thomas view->count = 0;
7842 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7843 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7844 e4526bf5 2021-09-03 naddy if (re == NULL)
7845 e4526bf5 2021-09-03 naddy break;
7846 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7847 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7848 e4526bf5 2021-09-03 naddy }
7849 e4526bf5 2021-09-03 naddy if (n > 0)
7850 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7851 e4526bf5 2021-09-03 naddy break;
7852 a5d43cac 2022-07-01 thomas }
7853 6458efa5 2020-11-24 stsp case 'k':
7854 6458efa5 2020-11-24 stsp case KEY_UP:
7855 f7140bf5 2021-10-17 thomas case CTRL('p'):
7856 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7857 6458efa5 2020-11-24 stsp s->selected--;
7858 6458efa5 2020-11-24 stsp break;
7859 34ba6917 2020-11-30 stsp }
7860 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7861 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7862 07b0611c 2022-06-23 thomas view->count = 0;
7863 6458efa5 2020-11-24 stsp break;
7864 70f17a53 2022-06-13 thomas case CTRL('u'):
7865 23427b14 2022-06-23 thomas case 'u':
7866 70f17a53 2022-06-13 thomas nscroll /= 2;
7867 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7868 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7869 6458efa5 2020-11-24 stsp case CTRL('b'):
7870 1c5e5faa 2022-06-23 thomas case 'b':
7871 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7872 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7873 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7874 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7875 07b0611c 2022-06-23 thomas view->count = 0;
7876 6458efa5 2020-11-24 stsp break;
7877 6458efa5 2020-11-24 stsp case 'j':
7878 6458efa5 2020-11-24 stsp case KEY_DOWN:
7879 f7140bf5 2021-10-17 thomas case CTRL('n'):
7880 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7881 6458efa5 2020-11-24 stsp s->selected++;
7882 6458efa5 2020-11-24 stsp break;
7883 6458efa5 2020-11-24 stsp }
7884 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7885 6458efa5 2020-11-24 stsp /* can't scroll any further */
7886 07b0611c 2022-06-23 thomas view->count = 0;
7887 6458efa5 2020-11-24 stsp break;
7888 07b0611c 2022-06-23 thomas }
7889 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7890 6458efa5 2020-11-24 stsp break;
7891 70f17a53 2022-06-13 thomas case CTRL('d'):
7892 23427b14 2022-06-23 thomas case 'd':
7893 70f17a53 2022-06-13 thomas nscroll /= 2;
7894 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7895 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7896 6458efa5 2020-11-24 stsp case CTRL('f'):
7897 1c5e5faa 2022-06-23 thomas case 'f':
7898 4c2d69cb 2022-06-23 thomas case ' ':
7899 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7900 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7901 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7902 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7903 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7904 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7905 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7906 07b0611c 2022-06-23 thomas view->count = 0;
7907 6458efa5 2020-11-24 stsp break;
7908 6458efa5 2020-11-24 stsp }
7909 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7910 6458efa5 2020-11-24 stsp break;
7911 6458efa5 2020-11-24 stsp case CTRL('l'):
7912 07b0611c 2022-06-23 thomas view->count = 0;
7913 8924d611 2020-12-26 stsp tog_free_refs();
7914 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7915 8924d611 2020-12-26 stsp if (err)
7916 8924d611 2020-12-26 stsp break;
7917 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7918 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7919 6458efa5 2020-11-24 stsp break;
7920 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7921 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7922 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7923 6458efa5 2020-11-24 stsp break;
7924 6458efa5 2020-11-24 stsp default:
7925 07b0611c 2022-06-23 thomas view->count = 0;
7926 6458efa5 2020-11-24 stsp break;
7927 6458efa5 2020-11-24 stsp }
7928 6458efa5 2020-11-24 stsp
7929 6458efa5 2020-11-24 stsp return err;
7930 6458efa5 2020-11-24 stsp }
7931 6458efa5 2020-11-24 stsp
7932 6458efa5 2020-11-24 stsp __dead static void
7933 6458efa5 2020-11-24 stsp usage_ref(void)
7934 6458efa5 2020-11-24 stsp {
7935 6458efa5 2020-11-24 stsp endwin();
7936 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7937 6458efa5 2020-11-24 stsp getprogname());
7938 6458efa5 2020-11-24 stsp exit(1);
7939 6458efa5 2020-11-24 stsp }
7940 6458efa5 2020-11-24 stsp
7941 6458efa5 2020-11-24 stsp static const struct got_error *
7942 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7943 6458efa5 2020-11-24 stsp {
7944 6458efa5 2020-11-24 stsp const struct got_error *error;
7945 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7946 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7947 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7948 6458efa5 2020-11-24 stsp int ch;
7949 6458efa5 2020-11-24 stsp struct tog_view *view;
7950 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7951 6458efa5 2020-11-24 stsp
7952 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7953 6458efa5 2020-11-24 stsp switch (ch) {
7954 6458efa5 2020-11-24 stsp case 'r':
7955 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7956 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7957 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7958 6458efa5 2020-11-24 stsp optarg);
7959 6458efa5 2020-11-24 stsp break;
7960 6458efa5 2020-11-24 stsp default:
7961 e99e2d15 2020-11-24 naddy usage_ref();
7962 6458efa5 2020-11-24 stsp /* NOTREACHED */
7963 6458efa5 2020-11-24 stsp }
7964 6458efa5 2020-11-24 stsp }
7965 6458efa5 2020-11-24 stsp
7966 6458efa5 2020-11-24 stsp argc -= optind;
7967 6458efa5 2020-11-24 stsp argv += optind;
7968 6458efa5 2020-11-24 stsp
7969 6458efa5 2020-11-24 stsp if (argc > 1)
7970 e99e2d15 2020-11-24 naddy usage_ref();
7971 7cd52833 2022-06-23 thomas
7972 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7973 7cd52833 2022-06-23 thomas if (error != NULL)
7974 7cd52833 2022-06-23 thomas goto done;
7975 6458efa5 2020-11-24 stsp
7976 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7977 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7978 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7979 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7980 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7981 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7982 c156c7a4 2020-12-18 stsp goto done;
7983 6458efa5 2020-11-24 stsp if (worktree)
7984 6458efa5 2020-11-24 stsp repo_path =
7985 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7986 6458efa5 2020-11-24 stsp else
7987 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7988 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7989 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7990 c156c7a4 2020-12-18 stsp goto done;
7991 c156c7a4 2020-12-18 stsp }
7992 6458efa5 2020-11-24 stsp }
7993 6458efa5 2020-11-24 stsp
7994 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7995 6458efa5 2020-11-24 stsp if (error != NULL)
7996 6458efa5 2020-11-24 stsp goto done;
7997 6458efa5 2020-11-24 stsp
7998 6458efa5 2020-11-24 stsp init_curses();
7999 6458efa5 2020-11-24 stsp
8000 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8001 51a10b52 2020-12-26 stsp if (error)
8002 51a10b52 2020-12-26 stsp goto done;
8003 51a10b52 2020-12-26 stsp
8004 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8005 6458efa5 2020-11-24 stsp if (error)
8006 6458efa5 2020-11-24 stsp goto done;
8007 6458efa5 2020-11-24 stsp
8008 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8009 6458efa5 2020-11-24 stsp if (view == NULL) {
8010 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8011 6458efa5 2020-11-24 stsp goto done;
8012 6458efa5 2020-11-24 stsp }
8013 6458efa5 2020-11-24 stsp
8014 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8015 6458efa5 2020-11-24 stsp if (error)
8016 6458efa5 2020-11-24 stsp goto done;
8017 6458efa5 2020-11-24 stsp
8018 6458efa5 2020-11-24 stsp if (worktree) {
8019 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8020 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8021 6458efa5 2020-11-24 stsp worktree = NULL;
8022 6458efa5 2020-11-24 stsp }
8023 6458efa5 2020-11-24 stsp error = view_loop(view);
8024 6458efa5 2020-11-24 stsp done:
8025 6458efa5 2020-11-24 stsp free(repo_path);
8026 6458efa5 2020-11-24 stsp free(cwd);
8027 1d0f4054 2021-06-17 stsp if (repo) {
8028 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8029 1d0f4054 2021-06-17 stsp if (close_err)
8030 1d0f4054 2021-06-17 stsp error = close_err;
8031 1d0f4054 2021-06-17 stsp }
8032 7cd52833 2022-06-23 thomas if (pack_fds) {
8033 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8034 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8035 7cd52833 2022-06-23 thomas if (error == NULL)
8036 7cd52833 2022-06-23 thomas error = pack_err;
8037 7cd52833 2022-06-23 thomas }
8038 51a10b52 2020-12-26 stsp tog_free_refs();
8039 6458efa5 2020-11-24 stsp return error;
8040 6458efa5 2020-11-24 stsp }
8041 6458efa5 2020-11-24 stsp
8042 a5d43cac 2022-07-01 thomas /*
8043 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8044 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8045 a5d43cac 2022-07-01 thomas */
8046 6458efa5 2020-11-24 stsp static void
8047 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8048 a5d43cac 2022-07-01 thomas {
8049 a5d43cac 2022-07-01 thomas switch (view->type) {
8050 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8051 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8052 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8053 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8054 a5d43cac 2022-07-01 thomas 1);
8055 a5d43cac 2022-07-01 thomas break;
8056 a5d43cac 2022-07-01 thomas }
8057 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8058 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8059 a5d43cac 2022-07-01 thomas else
8060 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8061 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8062 a5d43cac 2022-07-01 thomas break;
8063 a5d43cac 2022-07-01 thomas }
8064 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8065 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8066 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8067 a5d43cac 2022-07-01 thomas break;
8068 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8069 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8070 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8071 a5d43cac 2022-07-01 thomas break;
8072 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8073 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8074 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8075 a5d43cac 2022-07-01 thomas break;
8076 a5d43cac 2022-07-01 thomas default:
8077 a5d43cac 2022-07-01 thomas break;
8078 a5d43cac 2022-07-01 thomas }
8079 a5d43cac 2022-07-01 thomas
8080 a5d43cac 2022-07-01 thomas view->offset = 0;
8081 a5d43cac 2022-07-01 thomas }
8082 a5d43cac 2022-07-01 thomas
8083 a5d43cac 2022-07-01 thomas /*
8084 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8085 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8086 a5d43cac 2022-07-01 thomas */
8087 a5d43cac 2022-07-01 thomas static const struct got_error *
8088 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8089 a5d43cac 2022-07-01 thomas {
8090 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8091 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8092 a5d43cac 2022-07-01 thomas int *selected = NULL;
8093 a5d43cac 2022-07-01 thomas int header, offset;
8094 a5d43cac 2022-07-01 thomas
8095 a5d43cac 2022-07-01 thomas switch (view->type) {
8096 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8097 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8098 a5d43cac 2022-07-01 thomas header = 3;
8099 a5d43cac 2022-07-01 thomas scrolld = NULL;
8100 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8101 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8102 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8103 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8104 a5d43cac 2022-07-01 thomas view->offset = offset;
8105 a5d43cac 2022-07-01 thomas }
8106 a5d43cac 2022-07-01 thomas break;
8107 a5d43cac 2022-07-01 thomas }
8108 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8109 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8110 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8111 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8112 a5d43cac 2022-07-01 thomas selected = &s->selected;
8113 a5d43cac 2022-07-01 thomas break;
8114 a5d43cac 2022-07-01 thomas }
8115 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8116 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8117 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8118 a5d43cac 2022-07-01 thomas header = 3;
8119 a5d43cac 2022-07-01 thomas selected = &s->selected;
8120 a5d43cac 2022-07-01 thomas break;
8121 a5d43cac 2022-07-01 thomas }
8122 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8123 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8124 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8125 a5d43cac 2022-07-01 thomas header = 5;
8126 a5d43cac 2022-07-01 thomas selected = &s->selected;
8127 a5d43cac 2022-07-01 thomas break;
8128 a5d43cac 2022-07-01 thomas }
8129 a5d43cac 2022-07-01 thomas default:
8130 a5d43cac 2022-07-01 thomas selected = NULL;
8131 a5d43cac 2022-07-01 thomas scrolld = NULL;
8132 a5d43cac 2022-07-01 thomas header = 0;
8133 a5d43cac 2022-07-01 thomas break;
8134 a5d43cac 2022-07-01 thomas }
8135 a5d43cac 2022-07-01 thomas
8136 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8137 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8138 a5d43cac 2022-07-01 thomas view->offset = offset;
8139 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8140 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8141 a5d43cac 2022-07-01 thomas *selected -= offset;
8142 a5d43cac 2022-07-01 thomas }
8143 a5d43cac 2022-07-01 thomas }
8144 a5d43cac 2022-07-01 thomas
8145 a5d43cac 2022-07-01 thomas return err;
8146 a5d43cac 2022-07-01 thomas }
8147 a5d43cac 2022-07-01 thomas
8148 a5d43cac 2022-07-01 thomas static void
8149 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8150 ce5b7c56 2019-07-09 stsp {
8151 6059809a 2020-12-17 stsp size_t i;
8152 9f7d7167 2018-04-29 stsp
8153 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8154 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8155 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8156 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8157 ce5b7c56 2019-07-09 stsp }
8158 6879ba42 2020-10-01 naddy fputc('\n', fp);
8159 ce5b7c56 2019-07-09 stsp }
8160 ce5b7c56 2019-07-09 stsp
8161 4ed7e80c 2018-05-20 stsp __dead static void
8162 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8163 9f7d7167 2018-04-29 stsp {
8164 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8165 6879ba42 2020-10-01 naddy
8166 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8167 6879ba42 2020-10-01 naddy getprogname());
8168 6879ba42 2020-10-01 naddy if (hflag) {
8169 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8170 6879ba42 2020-10-01 naddy list_commands(fp);
8171 ee85c5e8 2020-02-29 stsp }
8172 6879ba42 2020-10-01 naddy exit(status);
8173 9f7d7167 2018-04-29 stsp }
8174 9f7d7167 2018-04-29 stsp
8175 c2301be8 2018-04-30 stsp static char **
8176 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8177 c2301be8 2018-04-30 stsp {
8178 ee85c5e8 2020-02-29 stsp va_list ap;
8179 c2301be8 2018-04-30 stsp char **argv;
8180 ee85c5e8 2020-02-29 stsp int i;
8181 c2301be8 2018-04-30 stsp
8182 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8183 ee85c5e8 2020-02-29 stsp
8184 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8185 c2301be8 2018-04-30 stsp if (argv == NULL)
8186 c2301be8 2018-04-30 stsp err(1, "calloc");
8187 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8188 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8189 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8190 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8191 c2301be8 2018-04-30 stsp }
8192 c2301be8 2018-04-30 stsp
8193 ee85c5e8 2020-02-29 stsp va_end(ap);
8194 c2301be8 2018-04-30 stsp return argv;
8195 ee85c5e8 2020-02-29 stsp }
8196 ee85c5e8 2020-02-29 stsp
8197 ee85c5e8 2020-02-29 stsp /*
8198 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8199 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8200 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8201 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8202 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8203 ee85c5e8 2020-02-29 stsp */
8204 ee85c5e8 2020-02-29 stsp static const struct got_error *
8205 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8206 ee85c5e8 2020-02-29 stsp {
8207 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8208 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8209 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8210 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8211 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8212 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8213 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8214 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8215 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8216 84de9106 2020-12-26 stsp
8217 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8218 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8219 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8220 ee85c5e8 2020-02-29 stsp
8221 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8222 7cd52833 2022-06-23 thomas if (error != NULL)
8223 7cd52833 2022-06-23 thomas goto done;
8224 7cd52833 2022-06-23 thomas
8225 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8226 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8227 ee85c5e8 2020-02-29 stsp goto done;
8228 ee85c5e8 2020-02-29 stsp
8229 ee85c5e8 2020-02-29 stsp if (worktree)
8230 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8231 ee85c5e8 2020-02-29 stsp else
8232 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8233 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8234 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8235 ee85c5e8 2020-02-29 stsp goto done;
8236 ee85c5e8 2020-02-29 stsp }
8237 ee85c5e8 2020-02-29 stsp
8238 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8239 ee85c5e8 2020-02-29 stsp if (error != NULL)
8240 ee85c5e8 2020-02-29 stsp goto done;
8241 ee85c5e8 2020-02-29 stsp
8242 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8243 ee85c5e8 2020-02-29 stsp repo, worktree);
8244 ee85c5e8 2020-02-29 stsp if (error)
8245 ee85c5e8 2020-02-29 stsp goto done;
8246 ee85c5e8 2020-02-29 stsp
8247 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8248 84de9106 2020-12-26 stsp if (error)
8249 84de9106 2020-12-26 stsp goto done;
8250 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8251 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8252 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8253 ee85c5e8 2020-02-29 stsp if (error)
8254 ee85c5e8 2020-02-29 stsp goto done;
8255 ee85c5e8 2020-02-29 stsp
8256 ee85c5e8 2020-02-29 stsp if (worktree) {
8257 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8258 ee85c5e8 2020-02-29 stsp worktree = NULL;
8259 ee85c5e8 2020-02-29 stsp }
8260 ee85c5e8 2020-02-29 stsp
8261 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8262 945f9229 2022-04-16 thomas if (error)
8263 945f9229 2022-04-16 thomas goto done;
8264 945f9229 2022-04-16 thomas
8265 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8266 ee85c5e8 2020-02-29 stsp if (error) {
8267 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8268 ee85c5e8 2020-02-29 stsp goto done;
8269 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8270 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8271 6879ba42 2020-10-01 naddy usage(1, 1);
8272 ee85c5e8 2020-02-29 stsp /* not reached */
8273 ee85c5e8 2020-02-29 stsp }
8274 ee85c5e8 2020-02-29 stsp
8275 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8276 1d0f4054 2021-06-17 stsp if (error == NULL)
8277 1d0f4054 2021-06-17 stsp error = close_err;
8278 ee85c5e8 2020-02-29 stsp repo = NULL;
8279 ee85c5e8 2020-02-29 stsp
8280 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8281 ee85c5e8 2020-02-29 stsp if (error)
8282 ee85c5e8 2020-02-29 stsp goto done;
8283 ee85c5e8 2020-02-29 stsp
8284 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8285 ee85c5e8 2020-02-29 stsp argc = 4;
8286 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8287 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8288 ee85c5e8 2020-02-29 stsp done:
8289 1d0f4054 2021-06-17 stsp if (repo) {
8290 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8291 1d0f4054 2021-06-17 stsp if (error == NULL)
8292 1d0f4054 2021-06-17 stsp error = close_err;
8293 1d0f4054 2021-06-17 stsp }
8294 945f9229 2022-04-16 thomas if (commit)
8295 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8296 ee85c5e8 2020-02-29 stsp if (worktree)
8297 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8298 7cd52833 2022-06-23 thomas if (pack_fds) {
8299 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8300 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8301 7cd52833 2022-06-23 thomas if (error == NULL)
8302 7cd52833 2022-06-23 thomas error = pack_err;
8303 7cd52833 2022-06-23 thomas }
8304 ee85c5e8 2020-02-29 stsp free(id);
8305 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8306 ee85c5e8 2020-02-29 stsp free(commit_id);
8307 ee85c5e8 2020-02-29 stsp free(cwd);
8308 ee85c5e8 2020-02-29 stsp free(repo_path);
8309 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8310 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8311 ee85c5e8 2020-02-29 stsp int i;
8312 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8313 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8314 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8315 ee85c5e8 2020-02-29 stsp }
8316 87670572 2020-12-26 naddy tog_free_refs();
8317 ee85c5e8 2020-02-29 stsp return error;
8318 c2301be8 2018-04-30 stsp }
8319 c2301be8 2018-04-30 stsp
8320 9f7d7167 2018-04-29 stsp int
8321 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8322 9f7d7167 2018-04-29 stsp {
8323 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8324 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8325 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8326 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8327 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8328 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8329 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8330 83cd27f8 2020-01-13 stsp };
8331 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8332 9f7d7167 2018-04-29 stsp
8333 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8334 9f7d7167 2018-04-29 stsp
8335 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8336 9f7d7167 2018-04-29 stsp switch (ch) {
8337 9f7d7167 2018-04-29 stsp case 'h':
8338 9f7d7167 2018-04-29 stsp hflag = 1;
8339 9f7d7167 2018-04-29 stsp break;
8340 53ccebc2 2019-07-30 stsp case 'V':
8341 53ccebc2 2019-07-30 stsp Vflag = 1;
8342 53ccebc2 2019-07-30 stsp break;
8343 9f7d7167 2018-04-29 stsp default:
8344 6879ba42 2020-10-01 naddy usage(hflag, 1);
8345 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8346 9f7d7167 2018-04-29 stsp }
8347 9f7d7167 2018-04-29 stsp }
8348 9f7d7167 2018-04-29 stsp
8349 9f7d7167 2018-04-29 stsp argc -= optind;
8350 9f7d7167 2018-04-29 stsp argv += optind;
8351 9814e6a3 2020-09-27 naddy optind = 1;
8352 c2301be8 2018-04-30 stsp optreset = 1;
8353 9f7d7167 2018-04-29 stsp
8354 53ccebc2 2019-07-30 stsp if (Vflag) {
8355 53ccebc2 2019-07-30 stsp got_version_print_str();
8356 6879ba42 2020-10-01 naddy return 0;
8357 53ccebc2 2019-07-30 stsp }
8358 4010e238 2020-12-04 stsp
8359 4010e238 2020-12-04 stsp #ifndef PROFILE
8360 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8361 4010e238 2020-12-04 stsp NULL) == -1)
8362 4010e238 2020-12-04 stsp err(1, "pledge");
8363 4010e238 2020-12-04 stsp #endif
8364 53ccebc2 2019-07-30 stsp
8365 c2301be8 2018-04-30 stsp if (argc == 0) {
8366 f29d3e89 2018-06-23 stsp if (hflag)
8367 6879ba42 2020-10-01 naddy usage(hflag, 0);
8368 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8369 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8370 c2301be8 2018-04-30 stsp argc = 1;
8371 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8372 c2301be8 2018-04-30 stsp } else {
8373 6059809a 2020-12-17 stsp size_t i;
8374 9f7d7167 2018-04-29 stsp
8375 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8376 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8377 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8378 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8379 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8380 9f7d7167 2018-04-29 stsp break;
8381 9f7d7167 2018-04-29 stsp }
8382 9f7d7167 2018-04-29 stsp }
8383 ee85c5e8 2020-02-29 stsp }
8384 3642c4c6 2019-07-09 stsp
8385 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8386 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8387 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8388 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8389 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8390 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8391 adf4c9e0 2022-07-03 thomas }
8392 adf4c9e0 2022-07-03 thomas
8393 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8394 ee85c5e8 2020-02-29 stsp if (argc != 1)
8395 6879ba42 2020-10-01 naddy usage(0, 1);
8396 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8397 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8398 ee85c5e8 2020-02-29 stsp } else {
8399 ee85c5e8 2020-02-29 stsp if (hflag)
8400 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8401 ee85c5e8 2020-02-29 stsp else
8402 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8403 9f7d7167 2018-04-29 stsp }
8404 9f7d7167 2018-04-29 stsp
8405 9f7d7167 2018-04-29 stsp endwin();
8406 b46c1e04 2020-09-20 naddy putchar('\n');
8407 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8408 a2f4a359 2020-02-28 stsp int i;
8409 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8410 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8411 a2f4a359 2020-02-28 stsp free(cmd_argv);
8412 a2f4a359 2020-02-28 stsp }
8413 a2f4a359 2020-02-28 stsp
8414 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8415 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8416 9f7d7167 2018-04-29 stsp return 0;
8417 9f7d7167 2018-04-29 stsp }