Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 def2f970 2021-09-28 thomas #endif
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
44 9f7d7167 2018-04-29 stsp
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
46 dd038bc6 2021-09-21 thomas.ad
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 9f7d7167 2018-04-29 stsp
62 881b2d3e 2018-04-30 stsp #ifndef MIN
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 881b2d3e 2018-04-30 stsp #endif
65 881b2d3e 2018-04-30 stsp
66 2bd27830 2018-10-22 stsp #ifndef MAX
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #endif
69 2bd27830 2018-10-22 stsp
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
72 acf52a76 2021-09-21 thomas.ad #endif
73 2bd27830 2018-10-22 stsp
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 9f7d7167 2018-04-29 stsp #endif
77 9f7d7167 2018-04-29 stsp
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
82 9f7d7167 2018-04-29 stsp };
83 9f7d7167 2018-04-29 stsp
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 9f7d7167 2018-04-29 stsp
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 9f7d7167 2018-04-29 stsp
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
103 9f7d7167 2018-04-29 stsp };
104 9f7d7167 2018-04-29 stsp
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 d6b05b5b 2018-08-04 stsp };
112 c3e9aa98 2019-05-13 jcs
113 a5d43cac 2022-07-01 thomas enum tog_view_mode {
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
117 a5d43cac 2022-07-01 thomas };
118 a5d43cac 2022-07-01 thomas
119 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 a5d43cac 2022-07-01 thomas
121 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
122 d6b05b5b 2018-08-04 stsp
123 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
124 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
125 ba4f502b 2018-08-04 stsp struct got_object_id *id;
126 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
127 1a76625f 2018-10-22 stsp int idx;
128 ba4f502b 2018-08-04 stsp };
129 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 ba4f502b 2018-08-04 stsp struct commit_queue {
131 ba4f502b 2018-08-04 stsp int ncommits;
132 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
133 6d17833f 2019-11-08 stsp };
134 6d17833f 2019-11-08 stsp
135 f26dddb7 2019-11-08 stsp struct tog_color {
136 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
137 6d17833f 2019-11-08 stsp regex_t regex;
138 6d17833f 2019-11-08 stsp short colorpair;
139 15a087fe 2019-02-21 stsp };
140 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
141 11b20872 2019-11-08 stsp
142 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
144 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 2183bbf6 2022-01-23 thomas
146 2183bbf6 2022-01-23 thomas static const struct got_error *
147 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 2183bbf6 2022-01-23 thomas struct got_reference* re2)
149 2183bbf6 2022-01-23 thomas {
150 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
151 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
152 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
153 2183bbf6 2022-01-23 thomas
154 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
155 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
158 2183bbf6 2022-01-23 thomas *cmp = -1;
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
161 2183bbf6 2022-01-23 thomas *cmp = 1;
162 2183bbf6 2022-01-23 thomas return NULL;
163 2183bbf6 2022-01-23 thomas }
164 2183bbf6 2022-01-23 thomas
165 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 2183bbf6 2022-01-23 thomas return NULL;
167 2183bbf6 2022-01-23 thomas }
168 51a10b52 2020-12-26 stsp
169 11b20872 2019-11-08 stsp static const struct got_error *
170 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
171 51a10b52 2020-12-26 stsp {
172 51a10b52 2020-12-26 stsp const struct got_error *err;
173 51a10b52 2020-12-26 stsp
174 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 3bfadbd4 2021-11-20 thomas repo);
177 51a10b52 2020-12-26 stsp if (err)
178 51a10b52 2020-12-26 stsp return err;
179 51a10b52 2020-12-26 stsp
180 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 51a10b52 2020-12-26 stsp repo);
182 51a10b52 2020-12-26 stsp }
183 51a10b52 2020-12-26 stsp
184 51a10b52 2020-12-26 stsp static void
185 51a10b52 2020-12-26 stsp tog_free_refs(void)
186 51a10b52 2020-12-26 stsp {
187 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
188 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
189 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
190 51a10b52 2020-12-26 stsp }
191 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
192 51a10b52 2020-12-26 stsp }
193 51a10b52 2020-12-26 stsp
194 51a10b52 2020-12-26 stsp static const struct got_error *
195 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
196 11b20872 2019-11-08 stsp int idx, short color)
197 11b20872 2019-11-08 stsp {
198 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
199 11b20872 2019-11-08 stsp struct tog_color *tc;
200 11b20872 2019-11-08 stsp int regerr = 0;
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
203 11b20872 2019-11-08 stsp return NULL;
204 11b20872 2019-11-08 stsp
205 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
206 11b20872 2019-11-08 stsp
207 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
208 11b20872 2019-11-08 stsp if (tc == NULL)
209 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
210 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
211 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 11b20872 2019-11-08 stsp if (regerr) {
213 11b20872 2019-11-08 stsp static char regerr_msg[512];
214 11b20872 2019-11-08 stsp static char err_msg[512];
215 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
216 11b20872 2019-11-08 stsp sizeof(regerr_msg));
217 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 11b20872 2019-11-08 stsp regerr_msg);
219 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 11b20872 2019-11-08 stsp free(tc);
221 11b20872 2019-11-08 stsp return err;
222 11b20872 2019-11-08 stsp }
223 11b20872 2019-11-08 stsp tc->colorpair = idx;
224 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
225 11b20872 2019-11-08 stsp return NULL;
226 11b20872 2019-11-08 stsp }
227 11b20872 2019-11-08 stsp
228 11b20872 2019-11-08 stsp static void
229 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
230 11b20872 2019-11-08 stsp {
231 11b20872 2019-11-08 stsp struct tog_color *tc;
232 11b20872 2019-11-08 stsp
233 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
234 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
235 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
236 11b20872 2019-11-08 stsp regfree(&tc->regex);
237 11b20872 2019-11-08 stsp free(tc);
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 ef20f542 2022-06-26 thomas static struct tog_color *
242 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
247 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
248 11b20872 2019-11-08 stsp return tc;
249 11b20872 2019-11-08 stsp }
250 11b20872 2019-11-08 stsp
251 11b20872 2019-11-08 stsp return NULL;
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 11b20872 2019-11-08 stsp static int
255 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_CYAN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 11b20872 2019-11-08 stsp return COLOR_YELLOW;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 11b20872 2019-11-08 stsp return COLOR_CYAN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 11b20872 2019-11-08 stsp return COLOR_YELLOW;
279 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 6458efa5 2020-11-24 stsp return COLOR_GREEN;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
285 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
287 11b20872 2019-11-08 stsp
288 11b20872 2019-11-08 stsp return -1;
289 11b20872 2019-11-08 stsp }
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp static int
292 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
293 11b20872 2019-11-08 stsp {
294 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
295 11b20872 2019-11-08 stsp
296 11b20872 2019-11-08 stsp if (val == NULL)
297 11b20872 2019-11-08 stsp return default_color_value(envvar);
298 15a087fe 2019-02-21 stsp
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
300 11b20872 2019-11-08 stsp return COLOR_BLACK;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
302 11b20872 2019-11-08 stsp return COLOR_RED;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
304 11b20872 2019-11-08 stsp return COLOR_GREEN;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
306 11b20872 2019-11-08 stsp return COLOR_YELLOW;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLUE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
310 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
312 11b20872 2019-11-08 stsp return COLOR_CYAN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
314 11b20872 2019-11-08 stsp return COLOR_WHITE;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
316 11b20872 2019-11-08 stsp return -1;
317 11b20872 2019-11-08 stsp
318 11b20872 2019-11-08 stsp return default_color_value(envvar);
319 11b20872 2019-11-08 stsp }
320 11b20872 2019-11-08 stsp
321 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
322 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
323 3dbaef42 2020-11-24 stsp const char *label1, *label2;
324 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
325 19a6a6b5 2022-07-01 thomas int fd1, fd2;
326 82c78e96 2022-08-06 thomas int lineno;
327 15a087fe 2019-02-21 stsp int first_displayed_line;
328 15a087fe 2019-02-21 stsp int last_displayed_line;
329 15a087fe 2019-02-21 stsp int eof;
330 15a087fe 2019-02-21 stsp int diff_context;
331 3dbaef42 2020-11-24 stsp int ignore_whitespace;
332 64453f7e 2020-11-21 stsp int force_text_diff;
333 15a087fe 2019-02-21 stsp struct got_repository *repo;
334 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey int matched_line;
337 f44b1f58 2020-02-02 tracey int selected_line;
338 15a087fe 2019-02-21 stsp
339 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
340 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
341 b01e7d3b 2018-08-04 stsp };
342 b01e7d3b 2018-08-04 stsp
343 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
345 1a76625f 2018-10-22 stsp
346 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
347 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
348 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
349 1a76625f 2018-10-22 stsp int commits_needed;
350 fb280deb 2021-08-30 stsp int load_all;
351 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
352 1a76625f 2018-10-22 stsp struct commit_queue *commits;
353 1a76625f 2018-10-22 stsp const char *in_repo_path;
354 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
355 1a76625f 2018-10-22 stsp struct got_repository *repo;
356 1af5eddf 2022-06-23 thomas int *pack_fds;
357 1a76625f 2018-10-22 stsp int log_complete;
358 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
359 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
361 13add988 2019-10-15 stsp int *searching;
362 13add988 2019-10-15 stsp int *search_next_done;
363 13add988 2019-10-15 stsp regex_t *regex;
364 1a76625f 2018-10-22 stsp };
365 1a76625f 2018-10-22 stsp
366 1a76625f 2018-10-22 stsp struct tog_log_view_state {
367 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
368 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
371 b01e7d3b 2018-08-04 stsp int selected;
372 b01e7d3b 2018-08-04 stsp char *in_repo_path;
373 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
374 b672a97a 2020-01-27 stsp int log_branches;
375 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
376 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
377 1a76625f 2018-10-22 stsp sig_atomic_t quit;
378 1a76625f 2018-10-22 stsp pthread_t thread;
379 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
380 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
381 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
382 11b20872 2019-11-08 stsp struct tog_colors colors;
383 f69c5a46 2022-07-19 thomas int use_committer;
384 ba4f502b 2018-08-04 stsp };
385 11b20872 2019-11-08 stsp
386 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
390 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
394 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
395 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
396 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
397 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
400 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
401 ba4f502b 2018-08-04 stsp
402 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
403 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
404 e9424729 2018-08-04 stsp int nlines;
405 e9424729 2018-08-04 stsp
406 e9424729 2018-08-04 stsp struct tog_view *view;
407 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
408 e9424729 2018-08-04 stsp int *quit;
409 e9424729 2018-08-04 stsp };
410 e9424729 2018-08-04 stsp
411 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
412 e9424729 2018-08-04 stsp const char *path;
413 e9424729 2018-08-04 stsp struct got_repository *repo;
414 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
415 e9424729 2018-08-04 stsp int *complete;
416 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
417 fc06ba56 2019-08-22 stsp void *cancel_arg;
418 e9424729 2018-08-04 stsp };
419 e9424729 2018-08-04 stsp
420 e9424729 2018-08-04 stsp struct tog_blame {
421 e9424729 2018-08-04 stsp FILE *f;
422 be659d10 2020-11-18 stsp off_t filesize;
423 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
424 6fcac457 2018-11-19 stsp int nlines;
425 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
426 e9424729 2018-08-04 stsp pthread_t thread;
427 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
428 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
429 e9424729 2018-08-04 stsp const char *path;
430 7cd52833 2022-06-23 thomas int *pack_fds;
431 e9424729 2018-08-04 stsp };
432 e9424729 2018-08-04 stsp
433 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
434 7cbe629d 2018-08-04 stsp int first_displayed_line;
435 7cbe629d 2018-08-04 stsp int last_displayed_line;
436 7cbe629d 2018-08-04 stsp int selected_line;
437 4fc71f3b 2022-07-12 thomas int last_diffed_line;
438 7cbe629d 2018-08-04 stsp int blame_complete;
439 e5a0f69f 2018-08-18 stsp int eof;
440 e5a0f69f 2018-08-18 stsp int done;
441 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
442 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
443 e5a0f69f 2018-08-18 stsp char *path;
444 7cbe629d 2018-08-04 stsp struct got_repository *repo;
445 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
446 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
447 e9424729 2018-08-04 stsp struct tog_blame blame;
448 6c4c42e0 2019-06-24 stsp int matched_line;
449 11b20872 2019-11-08 stsp struct tog_colors colors;
450 ad80ab7b 2018-08-04 stsp };
451 ad80ab7b 2018-08-04 stsp
452 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
453 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
454 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 ad80ab7b 2018-08-04 stsp int selected;
458 ad80ab7b 2018-08-04 stsp };
459 ad80ab7b 2018-08-04 stsp
460 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
461 ad80ab7b 2018-08-04 stsp
462 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
463 ad80ab7b 2018-08-04 stsp char *tree_label;
464 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
465 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
466 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
469 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
470 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
471 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
473 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
474 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
475 6458efa5 2020-11-24 stsp struct tog_colors colors;
476 6458efa5 2020-11-24 stsp };
477 6458efa5 2020-11-24 stsp
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
479 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
480 6458efa5 2020-11-24 stsp struct got_reference *ref;
481 6458efa5 2020-11-24 stsp int idx;
482 6458efa5 2020-11-24 stsp };
483 6458efa5 2020-11-24 stsp
484 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
485 6458efa5 2020-11-24 stsp
486 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
487 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
490 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
491 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 6458efa5 2020-11-24 stsp struct got_repository *repo;
493 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
494 bddb1296 2019-11-08 stsp struct tog_colors colors;
495 7cbe629d 2018-08-04 stsp };
496 7cbe629d 2018-08-04 stsp
497 669b5ffa 2018-10-07 stsp /*
498 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
499 669b5ffa 2018-10-07 stsp *
500 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
501 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
502 669b5ffa 2018-10-07 stsp * there is enough screen estate.
503 669b5ffa 2018-10-07 stsp *
504 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
505 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
506 669b5ffa 2018-10-07 stsp *
507 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
508 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
509 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
510 669b5ffa 2018-10-07 stsp *
511 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
512 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
513 669b5ffa 2018-10-07 stsp */
514 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
515 669b5ffa 2018-10-07 stsp
516 cc3c9aac 2018-08-01 stsp struct tog_view {
517 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
518 26ed57b2 2018-05-19 stsp WINDOW *window;
519 26ed57b2 2018-05-19 stsp PANEL *panel;
520 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
522 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
523 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
524 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
526 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
527 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
528 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
529 9970f7fc 2020-12-03 stsp int dying;
530 669b5ffa 2018-10-07 stsp struct tog_view *parent;
531 669b5ffa 2018-10-07 stsp struct tog_view *child;
532 5dc9f4bc 2018-08-04 stsp
533 e78dc838 2020-12-04 stsp /*
534 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
535 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
536 e78dc838 2020-12-04 stsp * between parent and child.
537 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
538 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
539 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
540 e78dc838 2020-12-04 stsp * situations.
541 e78dc838 2020-12-04 stsp */
542 e78dc838 2020-12-04 stsp int focus_child;
543 e78dc838 2020-12-04 stsp
544 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
545 5dc9f4bc 2018-08-04 stsp /* type-specific state */
546 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
547 5dc9f4bc 2018-08-04 stsp union {
548 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
549 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
550 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
551 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
552 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
553 5dc9f4bc 2018-08-04 stsp } state;
554 e5a0f69f 2018-08-18 stsp
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
556 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
557 e78dc838 2020-12-04 stsp struct tog_view *, int);
558 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
559 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
560 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
561 60493ae3 2019-06-20 stsp
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
563 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
564 c0c4acc8 2021-01-24 stsp int search_started;
565 60493ae3 2019-06-20 stsp int searching;
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
567 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
568 60493ae3 2019-06-20 stsp int search_next_done;
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
570 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
571 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
572 1803e47f 2019-06-22 stsp regex_t regex;
573 41605754 2020-11-12 stsp regmatch_t regmatch;
574 cc3c9aac 2018-08-01 stsp };
575 cd0acaa7 2018-05-20 stsp
576 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
577 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
578 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
579 78756c87 2020-11-24 stsp struct got_repository *);
580 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
581 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
582 e78dc838 2020-12-04 stsp struct tog_view *, int);
583 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
584 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
586 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp
588 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
589 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
590 78756c87 2020-11-24 stsp const char *, const char *, int);
591 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
593 e78dc838 2020-12-04 stsp struct tog_view *, int);
594 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
597 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
598 e5a0f69f 2018-08-18 stsp
599 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
600 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
601 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
602 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
603 e78dc838 2020-12-04 stsp struct tog_view *, int);
604 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
605 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
607 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
608 e5a0f69f 2018-08-18 stsp
609 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
610 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
611 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
612 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
613 e78dc838 2020-12-04 stsp struct tog_view *, int);
614 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
616 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
617 6458efa5 2020-11-24 stsp
618 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
619 6458efa5 2020-11-24 stsp struct got_repository *);
620 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
621 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
622 e78dc838 2020-12-04 stsp struct tog_view *, int);
623 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
625 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
626 25791caa 2018-10-24 stsp
627 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
628 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
629 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
631 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
632 25791caa 2018-10-24 stsp
633 25791caa 2018-10-24 stsp static void
634 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
635 25791caa 2018-10-24 stsp {
636 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
637 83baff54 2019-08-12 stsp }
638 83baff54 2019-08-12 stsp
639 83baff54 2019-08-12 stsp static void
640 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
641 83baff54 2019-08-12 stsp {
642 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
643 25791caa 2018-10-24 stsp }
644 26ed57b2 2018-05-19 stsp
645 61266923 2020-01-14 stsp static void
646 61266923 2020-01-14 stsp tog_sigcont(int signo)
647 61266923 2020-01-14 stsp {
648 61266923 2020-01-14 stsp tog_sigcont_received = 1;
649 61266923 2020-01-14 stsp }
650 61266923 2020-01-14 stsp
651 296152d1 2022-05-31 thomas static void
652 296152d1 2022-05-31 thomas tog_sigint(int signo)
653 296152d1 2022-05-31 thomas {
654 296152d1 2022-05-31 thomas tog_sigint_received = 1;
655 296152d1 2022-05-31 thomas }
656 296152d1 2022-05-31 thomas
657 296152d1 2022-05-31 thomas static void
658 296152d1 2022-05-31 thomas tog_sigterm(int signo)
659 296152d1 2022-05-31 thomas {
660 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
661 296152d1 2022-05-31 thomas }
662 296152d1 2022-05-31 thomas
663 296152d1 2022-05-31 thomas static int
664 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
665 296152d1 2022-05-31 thomas {
666 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
667 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
668 296152d1 2022-05-31 thomas }
669 296152d1 2022-05-31 thomas
670 e5a0f69f 2018-08-18 stsp static const struct got_error *
671 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
672 ea5e7bb5 2018-08-01 stsp {
673 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
674 e5a0f69f 2018-08-18 stsp
675 669b5ffa 2018-10-07 stsp if (view->child) {
676 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
677 669b5ffa 2018-10-07 stsp view->child = NULL;
678 669b5ffa 2018-10-07 stsp }
679 e5a0f69f 2018-08-18 stsp if (view->close)
680 e5a0f69f 2018-08-18 stsp err = view->close(view);
681 ea5e7bb5 2018-08-01 stsp if (view->panel)
682 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
683 ea5e7bb5 2018-08-01 stsp if (view->window)
684 ea5e7bb5 2018-08-01 stsp delwin(view->window);
685 ea5e7bb5 2018-08-01 stsp free(view);
686 f2d749db 2022-07-12 thomas return err ? err : child_err;
687 ea5e7bb5 2018-08-01 stsp }
688 ea5e7bb5 2018-08-01 stsp
689 ea5e7bb5 2018-08-01 stsp static struct tog_view *
690 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
691 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
692 ea5e7bb5 2018-08-01 stsp {
693 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
694 ea5e7bb5 2018-08-01 stsp
695 ea5e7bb5 2018-08-01 stsp if (view == NULL)
696 ea5e7bb5 2018-08-01 stsp return NULL;
697 ea5e7bb5 2018-08-01 stsp
698 d6b05b5b 2018-08-04 stsp view->type = type;
699 f7d12f7e 2018-08-01 stsp view->lines = LINES;
700 f7d12f7e 2018-08-01 stsp view->cols = COLS;
701 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
702 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
703 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
704 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
705 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
706 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
709 ea5e7bb5 2018-08-01 stsp }
710 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
711 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
712 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
713 96a765a8 2018-08-04 stsp view_close(view);
714 ea5e7bb5 2018-08-01 stsp return NULL;
715 ea5e7bb5 2018-08-01 stsp }
716 ea5e7bb5 2018-08-01 stsp
717 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
718 ea5e7bb5 2018-08-01 stsp return view;
719 cdf1ee82 2018-08-01 stsp }
720 cdf1ee82 2018-08-01 stsp
721 0cf4efb1 2018-09-29 stsp static int
722 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
723 0cf4efb1 2018-09-29 stsp {
724 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
725 0cf4efb1 2018-09-29 stsp return 0;
726 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
727 5c60c32a 2018-10-18 stsp }
728 5c60c32a 2018-10-18 stsp
729 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
730 a5d43cac 2022-07-01 thomas static int
731 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
732 a5d43cac 2022-07-01 thomas {
733 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
734 a5d43cac 2022-07-01 thomas }
735 a5d43cac 2022-07-01 thomas
736 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
737 5c60c32a 2018-10-18 stsp
738 5c60c32a 2018-10-18 stsp static const struct got_error *
739 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
740 5c60c32a 2018-10-18 stsp {
741 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
742 5c60c32a 2018-10-18 stsp
743 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
744 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
745 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
746 53d2bdd3 2022-07-10 thomas else
747 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
748 a5d43cac 2022-07-01 thomas view->begin_x = 0;
749 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
750 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
751 53d2bdd3 2022-07-10 thomas view->cols > 119)
752 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
753 53d2bdd3 2022-07-10 thomas else
754 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
755 a5d43cac 2022-07-01 thomas view->begin_y = 0;
756 a5d43cac 2022-07-01 thomas }
757 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
758 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
759 5c60c32a 2018-10-18 stsp view->lines = LINES;
760 5c60c32a 2018-10-18 stsp view->cols = COLS;
761 5c60c32a 2018-10-18 stsp err = view_resize(view);
762 5c60c32a 2018-10-18 stsp if (err)
763 5c60c32a 2018-10-18 stsp return err;
764 5c60c32a 2018-10-18 stsp
765 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
766 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
767 a5d43cac 2022-07-01 thomas
768 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
769 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
770 5c60c32a 2018-10-18 stsp
771 5c60c32a 2018-10-18 stsp return NULL;
772 5c60c32a 2018-10-18 stsp }
773 5c60c32a 2018-10-18 stsp
774 5c60c32a 2018-10-18 stsp static const struct got_error *
775 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
776 5c60c32a 2018-10-18 stsp {
777 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
778 5c60c32a 2018-10-18 stsp
779 5c60c32a 2018-10-18 stsp view->begin_x = 0;
780 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
781 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
782 5c60c32a 2018-10-18 stsp view->ncols = COLS;
783 5c60c32a 2018-10-18 stsp view->lines = LINES;
784 5c60c32a 2018-10-18 stsp view->cols = COLS;
785 5c60c32a 2018-10-18 stsp err = view_resize(view);
786 5c60c32a 2018-10-18 stsp if (err)
787 5c60c32a 2018-10-18 stsp return err;
788 5c60c32a 2018-10-18 stsp
789 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
790 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
791 5c60c32a 2018-10-18 stsp
792 5c60c32a 2018-10-18 stsp return NULL;
793 0cf4efb1 2018-09-29 stsp }
794 0cf4efb1 2018-09-29 stsp
795 5c60c32a 2018-10-18 stsp static int
796 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
797 5c60c32a 2018-10-18 stsp {
798 5c60c32a 2018-10-18 stsp return view->parent == NULL;
799 5c60c32a 2018-10-18 stsp }
800 5c60c32a 2018-10-18 stsp
801 b65b3ea0 2022-06-23 thomas static int
802 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
803 b65b3ea0 2022-06-23 thomas {
804 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
805 e44940c3 2022-07-01 thomas }
806 e44940c3 2022-07-01 thomas
807 e44940c3 2022-07-01 thomas static int
808 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
809 e44940c3 2022-07-01 thomas {
810 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
811 b65b3ea0 2022-06-23 thomas }
812 b65b3ea0 2022-06-23 thomas
813 444d5325 2022-07-03 thomas static int
814 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
815 444d5325 2022-07-03 thomas {
816 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
817 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
818 444d5325 2022-07-03 thomas }
819 444d5325 2022-07-03 thomas
820 a5d43cac 2022-07-01 thomas static void
821 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
822 a5d43cac 2022-07-01 thomas {
823 a5d43cac 2022-07-01 thomas PANEL *panel;
824 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
825 b65b3ea0 2022-06-23 thomas
826 a5d43cac 2022-07-01 thomas if (view->parent)
827 a5d43cac 2022-07-01 thomas return view_border(view->parent);
828 a5d43cac 2022-07-01 thomas
829 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
830 a5d43cac 2022-07-01 thomas if (panel == NULL)
831 a5d43cac 2022-07-01 thomas return;
832 a5d43cac 2022-07-01 thomas
833 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
834 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
835 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
836 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
837 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
838 a5d43cac 2022-07-01 thomas else
839 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
840 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
841 a5d43cac 2022-07-01 thomas }
842 a5d43cac 2022-07-01 thomas
843 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
844 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
846 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
847 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
848 a5d43cac 2022-07-01 thomas
849 4d8c2215 2018-08-19 stsp static const struct got_error *
850 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
851 f7d12f7e 2018-08-01 stsp {
852 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
853 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
854 f7d12f7e 2018-08-01 stsp
855 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
856 a5d43cac 2022-07-01 thomas
857 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
858 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
859 0cf4efb1 2018-09-29 stsp else
860 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
861 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
862 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
863 0cf4efb1 2018-09-29 stsp else
864 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
865 6d0fee91 2018-08-01 stsp
866 4918811f 2022-07-01 thomas if (view->child) {
867 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
868 a5d43cac 2022-07-01 thomas
869 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
870 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
871 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
872 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
873 40236d76 2022-06-23 thomas ncols = COLS;
874 40236d76 2022-06-23 thomas
875 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
876 5c60c32a 2018-10-18 stsp if (view->child->focussed)
877 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
878 5c60c32a 2018-10-18 stsp else
879 5c60c32a 2018-10-18 stsp show_panel(view->panel);
880 5c60c32a 2018-10-18 stsp } else {
881 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
882 40236d76 2022-06-23 thomas
883 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
884 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
885 a5d43cac 2022-07-01 thomas }
886 a5d43cac 2022-07-01 thomas /*
887 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
888 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
889 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
890 a5d43cac 2022-07-01 thomas */
891 a5d43cac 2022-07-01 thomas if (hs) {
892 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
893 a5d43cac 2022-07-01 thomas if (err)
894 a5d43cac 2022-07-01 thomas return err;
895 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
896 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
897 a5d43cac 2022-07-01 thomas if (err)
898 a5d43cac 2022-07-01 thomas return err;
899 a5d43cac 2022-07-01 thomas }
900 a5d43cac 2022-07-01 thomas view_border(view);
901 a5d43cac 2022-07-01 thomas update_panels();
902 a5d43cac 2022-07-01 thomas doupdate();
903 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
904 a5d43cac 2022-07-01 thomas nlines = view->nlines;
905 a5d43cac 2022-07-01 thomas }
906 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
907 40236d76 2022-06-23 thomas ncols = COLS;
908 669b5ffa 2018-10-07 stsp
909 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
910 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
911 ea0bff04 2022-07-19 thomas if (err)
912 ea0bff04 2022-07-19 thomas return err;
913 ea0bff04 2022-07-19 thomas }
914 ea0bff04 2022-07-19 thomas
915 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
916 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
917 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
918 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
919 40236d76 2022-06-23 thomas wclear(view->window);
920 40236d76 2022-06-23 thomas
921 40236d76 2022-06-23 thomas view->nlines = nlines;
922 40236d76 2022-06-23 thomas view->ncols = ncols;
923 40236d76 2022-06-23 thomas view->lines = LINES;
924 40236d76 2022-06-23 thomas view->cols = COLS;
925 40236d76 2022-06-23 thomas
926 5c60c32a 2018-10-18 stsp return NULL;
927 ea0bff04 2022-07-19 thomas }
928 ea0bff04 2022-07-19 thomas
929 ea0bff04 2022-07-19 thomas static const struct got_error *
930 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
931 ea0bff04 2022-07-19 thomas {
932 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
933 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
934 3a0139e8 2022-07-22 thomas int n = 0;
935 ea0bff04 2022-07-19 thomas
936 3a0139e8 2022-07-22 thomas if (s->selected_entry)
937 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
938 3a0139e8 2022-07-22 thomas
939 ea0bff04 2022-07-19 thomas /*
940 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
941 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
942 ea0bff04 2022-07-19 thomas */
943 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
944 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
945 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
946 ea0bff04 2022-07-19 thomas }
947 ea0bff04 2022-07-19 thomas
948 ea0bff04 2022-07-19 thomas return err;
949 97cb21cd 2022-07-12 thomas }
950 97cb21cd 2022-07-12 thomas
951 97cb21cd 2022-07-12 thomas static void
952 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
953 97cb21cd 2022-07-12 thomas {
954 97cb21cd 2022-07-12 thomas if (n == 0)
955 97cb21cd 2022-07-12 thomas return;
956 97cb21cd 2022-07-12 thomas
957 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
958 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
959 97cb21cd 2022-07-12 thomas view->parent->offset += n;
960 97cb21cd 2022-07-12 thomas else
961 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
962 97cb21cd 2022-07-12 thomas } else if (view->offset) {
963 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
964 97cb21cd 2022-07-12 thomas view->offset -= n;
965 97cb21cd 2022-07-12 thomas else
966 97cb21cd 2022-07-12 thomas view->offset = 0;
967 97cb21cd 2022-07-12 thomas }
968 53d2bdd3 2022-07-10 thomas }
969 53d2bdd3 2022-07-10 thomas
970 53d2bdd3 2022-07-10 thomas static const struct got_error *
971 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
972 53d2bdd3 2022-07-10 thomas {
973 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
974 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
975 53d2bdd3 2022-07-10 thomas
976 53d2bdd3 2022-07-10 thomas if (view->parent)
977 53d2bdd3 2022-07-10 thomas v = view->parent;
978 53d2bdd3 2022-07-10 thomas else
979 53d2bdd3 2022-07-10 thomas v = view;
980 53d2bdd3 2022-07-10 thomas
981 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
982 53d2bdd3 2022-07-10 thomas return NULL;
983 53d2bdd3 2022-07-10 thomas
984 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
985 53d2bdd3 2022-07-10 thomas
986 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
987 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
988 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
989 53d2bdd3 2022-07-10 thomas if (view->parent)
990 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
991 53d2bdd3 2022-07-10 thomas else
992 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
993 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
994 53d2bdd3 2022-07-10 thomas view->count = 0;
995 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
996 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
997 53d2bdd3 2022-07-10 thomas view->count = 0;
998 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
999 53d2bdd3 2022-07-10 thomas }
1000 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1001 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1002 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1003 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1004 53d2bdd3 2022-07-10 thomas if (err)
1005 53d2bdd3 2022-07-10 thomas return err;
1006 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1007 53d2bdd3 2022-07-10 thomas } else {
1008 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1009 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1010 53d2bdd3 2022-07-10 thomas if (view->parent)
1011 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1012 53d2bdd3 2022-07-10 thomas else
1013 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1014 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1015 53d2bdd3 2022-07-10 thomas view->count = 0;
1016 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1017 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1018 53d2bdd3 2022-07-10 thomas view->count = 0;
1019 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1020 53d2bdd3 2022-07-10 thomas }
1021 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1022 53d2bdd3 2022-07-10 thomas }
1023 53d2bdd3 2022-07-10 thomas
1024 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1025 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1026 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1027 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1028 53d2bdd3 2022-07-10 thomas
1029 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1030 53d2bdd3 2022-07-10 thomas if (err)
1031 53d2bdd3 2022-07-10 thomas return err;
1032 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1033 53d2bdd3 2022-07-10 thomas if (err)
1034 53d2bdd3 2022-07-10 thomas return err;
1035 53d2bdd3 2022-07-10 thomas
1036 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1037 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1038 53d2bdd3 2022-07-10 thomas if (err)
1039 53d2bdd3 2022-07-10 thomas return err;
1040 53d2bdd3 2022-07-10 thomas }
1041 53d2bdd3 2022-07-10 thomas
1042 3a0139e8 2022-07-22 thomas if (v->resize)
1043 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1044 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1045 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1046 53d2bdd3 2022-07-10 thomas
1047 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1048 53d2bdd3 2022-07-10 thomas
1049 53d2bdd3 2022-07-10 thomas return err;
1050 53d2bdd3 2022-07-10 thomas }
1051 53d2bdd3 2022-07-10 thomas
1052 53d2bdd3 2022-07-10 thomas static void
1053 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1054 53d2bdd3 2022-07-10 thomas {
1055 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1056 53d2bdd3 2022-07-10 thomas
1057 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1058 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1059 669b5ffa 2018-10-07 stsp }
1060 669b5ffa 2018-10-07 stsp
1061 669b5ffa 2018-10-07 stsp static const struct got_error *
1062 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1063 669b5ffa 2018-10-07 stsp {
1064 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1065 669b5ffa 2018-10-07 stsp
1066 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1067 669b5ffa 2018-10-07 stsp return NULL;
1068 669b5ffa 2018-10-07 stsp
1069 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1070 669b5ffa 2018-10-07 stsp view->child = NULL;
1071 669b5ffa 2018-10-07 stsp return err;
1072 669b5ffa 2018-10-07 stsp }
1073 669b5ffa 2018-10-07 stsp
1074 40236d76 2022-06-23 thomas static const struct got_error *
1075 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1076 669b5ffa 2018-10-07 stsp {
1077 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1078 53d2bdd3 2022-07-10 thomas
1079 669b5ffa 2018-10-07 stsp view->child = child;
1080 669b5ffa 2018-10-07 stsp child->parent = view;
1081 40236d76 2022-06-23 thomas
1082 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1083 53d2bdd3 2022-07-10 thomas if (err)
1084 53d2bdd3 2022-07-10 thomas return err;
1085 53d2bdd3 2022-07-10 thomas
1086 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1087 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1088 53d2bdd3 2022-07-10 thomas
1089 53d2bdd3 2022-07-10 thomas return err;
1090 bfddd0d9 2018-09-29 stsp }
1091 2a31b33b 2022-07-23 thomas
1092 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1093 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1094 2a31b33b 2022-07-23 thomas
1095 2a31b33b 2022-07-23 thomas static const struct got_error *
1096 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1097 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1098 2a31b33b 2022-07-23 thomas {
1099 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1100 2a31b33b 2022-07-23 thomas const struct got_error *err;
1101 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1102 2a31b33b 2022-07-23 thomas
1103 2a31b33b 2022-07-23 thomas *requested = NULL;
1104 2a31b33b 2022-07-23 thomas
1105 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view))
1106 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1107 bfddd0d9 2018-09-29 stsp
1108 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1109 2a31b33b 2022-07-23 thomas if (err)
1110 2a31b33b 2022-07-23 thomas return err;
1111 2a31b33b 2022-07-23 thomas
1112 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1113 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1114 2a31b33b 2022-07-23 thomas if (err)
1115 2a31b33b 2022-07-23 thomas return err;
1116 2a31b33b 2022-07-23 thomas }
1117 2a31b33b 2022-07-23 thomas
1118 2a31b33b 2022-07-23 thomas view->focussed = 0;
1119 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1120 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1121 2a31b33b 2022-07-23 thomas new_view->nlines = view->lines - y;
1122 2a31b33b 2022-07-23 thomas
1123 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view)) {
1124 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1125 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1126 2a31b33b 2022-07-23 thomas if (err)
1127 2a31b33b 2022-07-23 thomas return err;
1128 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1129 2a31b33b 2022-07-23 thomas if (err)
1130 2a31b33b 2022-07-23 thomas return err;
1131 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1132 2a31b33b 2022-07-23 thomas } else
1133 2a31b33b 2022-07-23 thomas *requested = new_view;
1134 2a31b33b 2022-07-23 thomas
1135 2a31b33b 2022-07-23 thomas return NULL;
1136 2a31b33b 2022-07-23 thomas }
1137 2a31b33b 2022-07-23 thomas
1138 34bc9ec9 2019-02-22 stsp static void
1139 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1140 25791caa 2018-10-24 stsp {
1141 25791caa 2018-10-24 stsp int cols, lines;
1142 25791caa 2018-10-24 stsp struct winsize size;
1143 25791caa 2018-10-24 stsp
1144 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1145 25791caa 2018-10-24 stsp cols = 80; /* Default */
1146 25791caa 2018-10-24 stsp lines = 24;
1147 25791caa 2018-10-24 stsp } else {
1148 25791caa 2018-10-24 stsp cols = size.ws_col;
1149 25791caa 2018-10-24 stsp lines = size.ws_row;
1150 25791caa 2018-10-24 stsp }
1151 25791caa 2018-10-24 stsp resize_term(lines, cols);
1152 2b49a8ae 2019-06-22 stsp }
1153 2b49a8ae 2019-06-22 stsp
1154 2b49a8ae 2019-06-22 stsp static const struct got_error *
1155 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1156 2b49a8ae 2019-06-22 stsp {
1157 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1158 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1159 2b49a8ae 2019-06-22 stsp char pattern[1024];
1160 2b49a8ae 2019-06-22 stsp int ret;
1161 c0c4acc8 2021-01-24 stsp
1162 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1163 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1164 c0c4acc8 2021-01-24 stsp view->searching = 0;
1165 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1166 c0c4acc8 2021-01-24 stsp }
1167 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1168 2b49a8ae 2019-06-22 stsp
1169 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1170 2b49a8ae 2019-06-22 stsp return NULL;
1171 2b49a8ae 2019-06-22 stsp
1172 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1173 a5d43cac 2022-07-01 thomas v = view->child;
1174 2b49a8ae 2019-06-22 stsp
1175 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1176 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1177 a5d43cac 2022-07-01 thomas
1178 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1179 2b49a8ae 2019-06-22 stsp nocbreak();
1180 2b49a8ae 2019-06-22 stsp echo();
1181 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1182 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1183 2b49a8ae 2019-06-22 stsp cbreak();
1184 2b49a8ae 2019-06-22 stsp noecho();
1185 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1186 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1187 2b49a8ae 2019-06-22 stsp return NULL;
1188 2b49a8ae 2019-06-22 stsp
1189 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1190 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1191 7c32bd05 2019-06-22 stsp if (err) {
1192 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1193 7c32bd05 2019-06-22 stsp return err;
1194 7c32bd05 2019-06-22 stsp }
1195 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1196 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1197 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1198 2b49a8ae 2019-06-22 stsp view->search_next(view);
1199 2b49a8ae 2019-06-22 stsp }
1200 2b49a8ae 2019-06-22 stsp
1201 2b49a8ae 2019-06-22 stsp return NULL;
1202 64486692 2022-07-07 thomas }
1203 64486692 2022-07-07 thomas
1204 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1205 64486692 2022-07-07 thomas static const struct got_error *
1206 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1207 64486692 2022-07-07 thomas {
1208 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1209 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1210 64486692 2022-07-07 thomas
1211 64486692 2022-07-07 thomas if (view->parent)
1212 64486692 2022-07-07 thomas v = view->parent;
1213 64486692 2022-07-07 thomas else
1214 64486692 2022-07-07 thomas v = view;
1215 64486692 2022-07-07 thomas
1216 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1217 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1218 ddbc4d37 2022-07-12 thomas else
1219 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1220 64486692 2022-07-07 thomas
1221 ddbc4d37 2022-07-12 thomas if (!v->child)
1222 ddbc4d37 2022-07-12 thomas return NULL;
1223 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1224 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1225 ddbc4d37 2022-07-12 thomas
1226 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1227 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1228 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1229 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1230 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1231 64486692 2022-07-07 thomas
1232 ddbc4d37 2022-07-12 thomas
1233 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1234 64486692 2022-07-07 thomas v->ncols = COLS;
1235 64486692 2022-07-07 thomas v->child->ncols = COLS;
1236 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1237 64486692 2022-07-07 thomas
1238 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1239 64486692 2022-07-07 thomas if (err)
1240 64486692 2022-07-07 thomas return err;
1241 64486692 2022-07-07 thomas }
1242 64486692 2022-07-07 thomas v->child->mode = v->mode;
1243 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1244 64486692 2022-07-07 thomas v->focus_child = 1;
1245 64486692 2022-07-07 thomas
1246 64486692 2022-07-07 thomas err = view_fullscreen(v);
1247 64486692 2022-07-07 thomas if (err)
1248 64486692 2022-07-07 thomas return err;
1249 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1250 64486692 2022-07-07 thomas if (err)
1251 64486692 2022-07-07 thomas return err;
1252 64486692 2022-07-07 thomas
1253 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1254 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1255 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1256 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1257 cf1fe301 2022-07-29 thomas if (err)
1258 cf1fe301 2022-07-29 thomas return err;
1259 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1260 cf1fe301 2022-07-29 thomas if (err)
1261 cf1fe301 2022-07-29 thomas return err;
1262 ddbc4d37 2022-07-12 thomas } else {
1263 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1264 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1265 64486692 2022-07-07 thomas }
1266 f4e6231a 2022-07-22 thomas if (v->resize)
1267 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1268 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1269 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1270 64486692 2022-07-07 thomas
1271 64486692 2022-07-07 thomas return err;
1272 0cf4efb1 2018-09-29 stsp }
1273 6d0fee91 2018-08-01 stsp
1274 07b0611c 2022-06-23 thomas /*
1275 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1276 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1277 07b0611c 2022-06-23 thomas */
1278 07b0611c 2022-06-23 thomas static int
1279 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1280 07b0611c 2022-06-23 thomas {
1281 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1282 a5d43cac 2022-07-01 thomas int x, n = 0;
1283 07b0611c 2022-06-23 thomas
1284 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1285 a5d43cac 2022-07-01 thomas v = view->child;
1286 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1287 a5d43cac 2022-07-01 thomas v = view->parent;
1288 a5d43cac 2022-07-01 thomas
1289 07b0611c 2022-06-23 thomas view->count = 0;
1290 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1291 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1292 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1293 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1294 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1295 07b0611c 2022-06-23 thomas
1296 07b0611c 2022-06-23 thomas do {
1297 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1298 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1299 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1300 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1301 a5d43cac 2022-07-01 thomas }
1302 a5d43cac 2022-07-01 thomas
1303 07b0611c 2022-06-23 thomas /*
1304 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1305 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1306 07b0611c 2022-06-23 thomas */
1307 07b0611c 2022-06-23 thomas if (n >= 9999999)
1308 07b0611c 2022-06-23 thomas n = 9999999;
1309 07b0611c 2022-06-23 thomas else
1310 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1311 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1312 07b0611c 2022-06-23 thomas
1313 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1314 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1315 07dd3ed3 2022-08-06 thomas n = 0;
1316 07dd3ed3 2022-08-06 thomas c = 0;
1317 07dd3ed3 2022-08-06 thomas }
1318 07dd3ed3 2022-08-06 thomas
1319 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1320 07b0611c 2022-06-23 thomas view->count = n;
1321 07b0611c 2022-06-23 thomas
1322 07b0611c 2022-06-23 thomas return c;
1323 07b0611c 2022-06-23 thomas }
1324 07b0611c 2022-06-23 thomas
1325 0cf4efb1 2018-09-29 stsp static const struct got_error *
1326 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1327 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1328 e5a0f69f 2018-08-18 stsp {
1329 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1330 669b5ffa 2018-10-07 stsp struct tog_view *v;
1331 1a76625f 2018-10-22 stsp int ch, errcode;
1332 e5a0f69f 2018-08-18 stsp
1333 e5a0f69f 2018-08-18 stsp *new = NULL;
1334 8f4ed634 2020-03-26 stsp
1335 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1336 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1337 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1338 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1339 07b0611c 2022-06-23 thomas view->count = 0;
1340 07b0611c 2022-06-23 thomas }
1341 e5a0f69f 2018-08-18 stsp
1342 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1343 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1344 82954512 2020-02-03 stsp if (errcode)
1345 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1346 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1347 3da8ef85 2021-09-21 stsp sched_yield();
1348 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1349 82954512 2020-02-03 stsp if (errcode)
1350 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1351 82954512 2020-02-03 stsp "pthread_mutex_lock");
1352 60493ae3 2019-06-20 stsp view->search_next(view);
1353 60493ae3 2019-06-20 stsp return NULL;
1354 60493ae3 2019-06-20 stsp }
1355 60493ae3 2019-06-20 stsp
1356 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1357 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1358 1a76625f 2018-10-22 stsp if (errcode)
1359 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1360 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1361 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1362 634cb454 2022-07-03 thomas cbreak();
1363 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1364 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1365 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1366 634cb454 2022-07-03 thomas view->count = 0;
1367 634cb454 2022-07-03 thomas else
1368 634cb454 2022-07-03 thomas ch = view->ch;
1369 634cb454 2022-07-03 thomas } else {
1370 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1371 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1372 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1373 07b0611c 2022-06-23 thomas }
1374 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1375 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1376 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1377 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1378 1a76625f 2018-10-22 stsp if (errcode)
1379 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1380 25791caa 2018-10-24 stsp
1381 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1382 25791caa 2018-10-24 stsp tog_resizeterm();
1383 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1384 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1385 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1386 25791caa 2018-10-24 stsp err = view_resize(v);
1387 25791caa 2018-10-24 stsp if (err)
1388 25791caa 2018-10-24 stsp return err;
1389 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1390 25791caa 2018-10-24 stsp if (err)
1391 25791caa 2018-10-24 stsp return err;
1392 cdfcfb03 2020-12-06 stsp if (v->child) {
1393 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1394 cdfcfb03 2020-12-06 stsp if (err)
1395 cdfcfb03 2020-12-06 stsp return err;
1396 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1397 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1398 cdfcfb03 2020-12-06 stsp if (err)
1399 cdfcfb03 2020-12-06 stsp return err;
1400 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1401 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1402 53d2bdd3 2022-07-10 thomas if (err)
1403 53d2bdd3 2022-07-10 thomas return err;
1404 53d2bdd3 2022-07-10 thomas }
1405 cdfcfb03 2020-12-06 stsp }
1406 25791caa 2018-10-24 stsp }
1407 25791caa 2018-10-24 stsp }
1408 25791caa 2018-10-24 stsp
1409 e5a0f69f 2018-08-18 stsp switch (ch) {
1410 1e37a5c2 2019-05-12 jcs case '\t':
1411 07b0611c 2022-06-23 thomas view->count = 0;
1412 1e37a5c2 2019-05-12 jcs if (view->child) {
1413 e78dc838 2020-12-04 stsp view->focussed = 0;
1414 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1415 e78dc838 2020-12-04 stsp view->focus_child = 1;
1416 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1417 e78dc838 2020-12-04 stsp view->focussed = 0;
1418 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1419 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1420 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1421 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1422 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1423 3a0139e8 2022-07-22 thomas 0);
1424 a5d43cac 2022-07-01 thomas if (err)
1425 a5d43cac 2022-07-01 thomas return err;
1426 a5d43cac 2022-07-01 thomas }
1427 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1428 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1429 a5d43cac 2022-07-01 thomas if (err)
1430 a5d43cac 2022-07-01 thomas return err;
1431 a5d43cac 2022-07-01 thomas }
1432 1e37a5c2 2019-05-12 jcs }
1433 1e37a5c2 2019-05-12 jcs break;
1434 1e37a5c2 2019-05-12 jcs case 'q':
1435 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1436 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1437 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1438 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1439 a5d43cac 2022-07-01 thomas if (err)
1440 a5d43cac 2022-07-01 thomas break;
1441 a5d43cac 2022-07-01 thomas }
1442 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1443 a5d43cac 2022-07-01 thomas }
1444 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1445 9970f7fc 2020-12-03 stsp view->dying = 1;
1446 1e37a5c2 2019-05-12 jcs break;
1447 1e37a5c2 2019-05-12 jcs case 'Q':
1448 1e37a5c2 2019-05-12 jcs *done = 1;
1449 1e37a5c2 2019-05-12 jcs break;
1450 1c5e5faa 2022-06-23 thomas case 'F':
1451 07b0611c 2022-06-23 thomas view->count = 0;
1452 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1453 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1454 1e37a5c2 2019-05-12 jcs break;
1455 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1456 e78dc838 2020-12-04 stsp view->focussed = 0;
1457 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1458 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1459 53d2bdd3 2022-07-10 thomas } else {
1460 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1461 53d2bdd3 2022-07-10 thomas if (!err)
1462 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1463 53d2bdd3 2022-07-10 thomas }
1464 1e37a5c2 2019-05-12 jcs if (err)
1465 1e37a5c2 2019-05-12 jcs break;
1466 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1467 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1468 1e37a5c2 2019-05-12 jcs } else {
1469 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1470 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1471 e78dc838 2020-12-04 stsp view->focussed = 1;
1472 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1473 1e37a5c2 2019-05-12 jcs } else {
1474 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1475 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1476 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1477 53d2bdd3 2022-07-10 thomas if (!err)
1478 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1479 669b5ffa 2018-10-07 stsp }
1480 1e37a5c2 2019-05-12 jcs if (err)
1481 1e37a5c2 2019-05-12 jcs break;
1482 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1483 a5d43cac 2022-07-01 thomas }
1484 a5d43cac 2022-07-01 thomas if (err)
1485 a5d43cac 2022-07-01 thomas break;
1486 3a0139e8 2022-07-22 thomas if (view->resize) {
1487 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1488 a5d43cac 2022-07-01 thomas if (err)
1489 a5d43cac 2022-07-01 thomas break;
1490 1e37a5c2 2019-05-12 jcs }
1491 a5d43cac 2022-07-01 thomas if (view->parent)
1492 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1493 a5d43cac 2022-07-01 thomas if (!err)
1494 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1495 1e37a5c2 2019-05-12 jcs break;
1496 64486692 2022-07-07 thomas case 'S':
1497 53d2bdd3 2022-07-10 thomas view->count = 0;
1498 64486692 2022-07-07 thomas err = switch_split(view);
1499 53d2bdd3 2022-07-10 thomas break;
1500 53d2bdd3 2022-07-10 thomas case '-':
1501 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1502 64486692 2022-07-07 thomas break;
1503 53d2bdd3 2022-07-10 thomas case '+':
1504 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1505 53d2bdd3 2022-07-10 thomas break;
1506 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1507 60493ae3 2019-06-20 stsp break;
1508 60493ae3 2019-06-20 stsp case '/':
1509 07b0611c 2022-06-23 thomas view->count = 0;
1510 60493ae3 2019-06-20 stsp if (view->search_start)
1511 2b49a8ae 2019-06-22 stsp view_search_start(view);
1512 60493ae3 2019-06-20 stsp else
1513 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1514 1e37a5c2 2019-05-12 jcs break;
1515 b1bf1435 2019-06-21 stsp case 'N':
1516 60493ae3 2019-06-20 stsp case 'n':
1517 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1518 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1519 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1520 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1521 60493ae3 2019-06-20 stsp view->search_next(view);
1522 60493ae3 2019-06-20 stsp } else
1523 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1524 adf4c9e0 2022-07-03 thomas break;
1525 adf4c9e0 2022-07-03 thomas case 'A':
1526 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1527 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1528 adf4c9e0 2022-07-03 thomas else
1529 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1530 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1531 adf4c9e0 2022-07-03 thomas if (v->reset) {
1532 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1533 adf4c9e0 2022-07-03 thomas if (err)
1534 adf4c9e0 2022-07-03 thomas return err;
1535 adf4c9e0 2022-07-03 thomas }
1536 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1537 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1538 adf4c9e0 2022-07-03 thomas if (err)
1539 adf4c9e0 2022-07-03 thomas return err;
1540 adf4c9e0 2022-07-03 thomas }
1541 adf4c9e0 2022-07-03 thomas }
1542 60493ae3 2019-06-20 stsp break;
1543 1e37a5c2 2019-05-12 jcs default:
1544 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1545 1e37a5c2 2019-05-12 jcs break;
1546 e5a0f69f 2018-08-18 stsp }
1547 e5a0f69f 2018-08-18 stsp
1548 e5a0f69f 2018-08-18 stsp return err;
1549 bcbd79e2 2018-08-19 stsp }
1550 bcbd79e2 2018-08-19 stsp
1551 ef20f542 2022-06-26 thomas static int
1552 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1553 a3404814 2018-09-02 stsp {
1554 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1555 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1556 669b5ffa 2018-10-07 stsp return 0;
1557 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1558 669b5ffa 2018-10-07 stsp return 0;
1559 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1560 a3404814 2018-09-02 stsp return 0;
1561 a3404814 2018-09-02 stsp
1562 669b5ffa 2018-10-07 stsp return view->focussed;
1563 a3404814 2018-09-02 stsp }
1564 a3404814 2018-09-02 stsp
1565 bcbd79e2 2018-08-19 stsp static const struct got_error *
1566 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1567 e5a0f69f 2018-08-18 stsp {
1568 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1569 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1570 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1571 64486692 2022-07-07 thomas char *mode;
1572 fd823528 2018-10-22 stsp int fast_refresh = 10;
1573 1a76625f 2018-10-22 stsp int done = 0, errcode;
1574 e5a0f69f 2018-08-18 stsp
1575 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1576 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1577 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1578 64486692 2022-07-07 thomas else
1579 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1580 64486692 2022-07-07 thomas
1581 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1582 1a76625f 2018-10-22 stsp if (errcode)
1583 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1584 1a76625f 2018-10-22 stsp
1585 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1586 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1587 e5a0f69f 2018-08-18 stsp
1588 1004088d 2018-09-29 stsp view->focussed = 1;
1589 878940b7 2018-09-29 stsp err = view->show(view);
1590 0cf4efb1 2018-09-29 stsp if (err)
1591 0cf4efb1 2018-09-29 stsp return err;
1592 0cf4efb1 2018-09-29 stsp update_panels();
1593 0cf4efb1 2018-09-29 stsp doupdate();
1594 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1595 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1596 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1597 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1598 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1599 fd823528 2018-10-22 stsp
1600 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1601 e5a0f69f 2018-08-18 stsp if (err)
1602 e5a0f69f 2018-08-18 stsp break;
1603 9970f7fc 2020-12-03 stsp if (view->dying) {
1604 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1605 669b5ffa 2018-10-07 stsp
1606 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1607 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1608 9970f7fc 2020-12-03 stsp entry);
1609 e78dc838 2020-12-04 stsp else if (view->parent)
1610 669b5ffa 2018-10-07 stsp prev = view->parent;
1611 669b5ffa 2018-10-07 stsp
1612 e78dc838 2020-12-04 stsp if (view->parent) {
1613 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1614 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1615 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1616 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1617 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1618 40236d76 2022-06-23 thomas if (err)
1619 40236d76 2022-06-23 thomas break;
1620 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1621 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1622 e78dc838 2020-12-04 stsp } else
1623 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1624 669b5ffa 2018-10-07 stsp
1625 9970f7fc 2020-12-03 stsp err = view_close(view);
1626 fb59748f 2020-12-05 stsp if (err)
1627 e5a0f69f 2018-08-18 stsp goto done;
1628 669b5ffa 2018-10-07 stsp
1629 e78dc838 2020-12-04 stsp view = NULL;
1630 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1631 e78dc838 2020-12-04 stsp if (v->focussed)
1632 e78dc838 2020-12-04 stsp break;
1633 0cf4efb1 2018-09-29 stsp }
1634 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1635 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1636 e78dc838 2020-12-04 stsp if (prev)
1637 e78dc838 2020-12-04 stsp view = prev;
1638 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1639 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1640 e78dc838 2020-12-04 stsp tog_view_list_head);
1641 e78dc838 2020-12-04 stsp }
1642 e78dc838 2020-12-04 stsp if (view) {
1643 e78dc838 2020-12-04 stsp if (view->focus_child) {
1644 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1645 e78dc838 2020-12-04 stsp view = view->child;
1646 e78dc838 2020-12-04 stsp } else
1647 e78dc838 2020-12-04 stsp view->focussed = 1;
1648 e78dc838 2020-12-04 stsp }
1649 e78dc838 2020-12-04 stsp }
1650 e5a0f69f 2018-08-18 stsp }
1651 bcbd79e2 2018-08-19 stsp if (new_view) {
1652 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1653 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1654 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1655 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1656 86c66b02 2018-10-18 stsp continue;
1657 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1658 86c66b02 2018-10-18 stsp err = view_close(v);
1659 86c66b02 2018-10-18 stsp if (err)
1660 86c66b02 2018-10-18 stsp goto done;
1661 86c66b02 2018-10-18 stsp break;
1662 86c66b02 2018-10-18 stsp }
1663 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1664 fed7eaa8 2018-10-24 stsp view = new_view;
1665 7cd52833 2022-06-23 thomas }
1666 669b5ffa 2018-10-07 stsp if (view) {
1667 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1668 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1669 e78dc838 2020-12-04 stsp view = view->child;
1670 e78dc838 2020-12-04 stsp } else {
1671 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1672 e78dc838 2020-12-04 stsp view = view->parent;
1673 1a76625f 2018-10-22 stsp }
1674 e78dc838 2020-12-04 stsp show_panel(view->panel);
1675 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1676 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1677 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1678 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1679 669b5ffa 2018-10-07 stsp if (err)
1680 1a76625f 2018-10-22 stsp goto done;
1681 669b5ffa 2018-10-07 stsp }
1682 669b5ffa 2018-10-07 stsp err = view->show(view);
1683 0cf4efb1 2018-09-29 stsp if (err)
1684 1a76625f 2018-10-22 stsp goto done;
1685 669b5ffa 2018-10-07 stsp if (view->child) {
1686 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1687 669b5ffa 2018-10-07 stsp if (err)
1688 1a76625f 2018-10-22 stsp goto done;
1689 669b5ffa 2018-10-07 stsp }
1690 1a76625f 2018-10-22 stsp update_panels();
1691 1a76625f 2018-10-22 stsp doupdate();
1692 0cf4efb1 2018-09-29 stsp }
1693 e5a0f69f 2018-08-18 stsp }
1694 e5a0f69f 2018-08-18 stsp done:
1695 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1696 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1697 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1698 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1699 f2d749db 2022-07-12 thomas close_err = view_close(view);
1700 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1701 f2d749db 2022-07-12 thomas err = close_err;
1702 e5a0f69f 2018-08-18 stsp }
1703 1a76625f 2018-10-22 stsp
1704 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1705 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1706 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1707 1a76625f 2018-10-22 stsp
1708 e5a0f69f 2018-08-18 stsp return err;
1709 ea5e7bb5 2018-08-01 stsp }
1710 ea5e7bb5 2018-08-01 stsp
1711 4ed7e80c 2018-05-20 stsp __dead static void
1712 9f7d7167 2018-04-29 stsp usage_log(void)
1713 9f7d7167 2018-04-29 stsp {
1714 80ddbec8 2018-04-29 stsp endwin();
1715 c70c5802 2018-08-01 stsp fprintf(stderr,
1716 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1717 9f7d7167 2018-04-29 stsp getprogname());
1718 9f7d7167 2018-04-29 stsp exit(1);
1719 80ddbec8 2018-04-29 stsp }
1720 80ddbec8 2018-04-29 stsp
1721 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1722 80ddbec8 2018-04-29 stsp static const struct got_error *
1723 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1724 963b370f 2018-05-20 stsp {
1725 00dfcb92 2018-06-11 stsp char *vis = NULL;
1726 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1727 963b370f 2018-05-20 stsp
1728 963b370f 2018-05-20 stsp *ws = NULL;
1729 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1730 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1731 00dfcb92 2018-06-11 stsp int vislen;
1732 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1733 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1734 00dfcb92 2018-06-11 stsp
1735 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1736 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1737 00dfcb92 2018-06-11 stsp if (err)
1738 00dfcb92 2018-06-11 stsp return err;
1739 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1740 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1741 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1742 a7f50699 2018-06-11 stsp goto done;
1743 a7f50699 2018-06-11 stsp }
1744 00dfcb92 2018-06-11 stsp }
1745 963b370f 2018-05-20 stsp
1746 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1747 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1748 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1749 a7f50699 2018-06-11 stsp goto done;
1750 a7f50699 2018-06-11 stsp }
1751 963b370f 2018-05-20 stsp
1752 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1753 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1754 a7f50699 2018-06-11 stsp done:
1755 00dfcb92 2018-06-11 stsp free(vis);
1756 963b370f 2018-05-20 stsp if (err) {
1757 963b370f 2018-05-20 stsp free(*ws);
1758 963b370f 2018-05-20 stsp *ws = NULL;
1759 963b370f 2018-05-20 stsp *wlen = 0;
1760 963b370f 2018-05-20 stsp }
1761 963b370f 2018-05-20 stsp return err;
1762 05171be4 2022-06-23 thomas }
1763 05171be4 2022-06-23 thomas
1764 05171be4 2022-06-23 thomas static const struct got_error *
1765 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1766 05171be4 2022-06-23 thomas {
1767 05171be4 2022-06-23 thomas char *dst;
1768 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1769 05171be4 2022-06-23 thomas
1770 05171be4 2022-06-23 thomas *ptr = NULL;
1771 05171be4 2022-06-23 thomas n = len = strlen(src);
1772 83316834 2022-06-23 thomas dst = malloc(n + 1);
1773 05171be4 2022-06-23 thomas if (dst == NULL)
1774 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1775 05171be4 2022-06-23 thomas
1776 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1777 05171be4 2022-06-23 thomas const char c = src[idx];
1778 05171be4 2022-06-23 thomas
1779 05171be4 2022-06-23 thomas if (c == '\t') {
1780 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1781 b235ad5d 2022-06-23 thomas char *p;
1782 b235ad5d 2022-06-23 thomas
1783 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1784 83316834 2022-06-23 thomas if (p == NULL) {
1785 83316834 2022-06-23 thomas free(dst);
1786 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1787 83316834 2022-06-23 thomas
1788 83316834 2022-06-23 thomas }
1789 83316834 2022-06-23 thomas dst = p;
1790 05171be4 2022-06-23 thomas n += nb;
1791 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1792 05171be4 2022-06-23 thomas sz += nb;
1793 05171be4 2022-06-23 thomas } else
1794 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1795 05171be4 2022-06-23 thomas ++idx;
1796 05171be4 2022-06-23 thomas }
1797 05171be4 2022-06-23 thomas
1798 05171be4 2022-06-23 thomas dst[sz] = '\0';
1799 05171be4 2022-06-23 thomas *ptr = dst;
1800 05171be4 2022-06-23 thomas return NULL;
1801 963b370f 2018-05-20 stsp }
1802 963b370f 2018-05-20 stsp
1803 8d208d34 2022-06-23 thomas /*
1804 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1805 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1806 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1807 8d208d34 2022-06-23 thomas * *rcol.
1808 f91a2b48 2022-06-23 thomas */
1809 8d208d34 2022-06-23 thomas static int
1810 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1811 8d208d34 2022-06-23 thomas {
1812 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1813 f91a2b48 2022-06-23 thomas
1814 8d208d34 2022-06-23 thomas if (n == 0) {
1815 8d208d34 2022-06-23 thomas *rcol = cols;
1816 8d208d34 2022-06-23 thomas return off;
1817 8d208d34 2022-06-23 thomas }
1818 f91a2b48 2022-06-23 thomas
1819 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1820 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1821 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1822 8d208d34 2022-06-23 thomas else
1823 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1824 f91a2b48 2022-06-23 thomas
1825 8d208d34 2022-06-23 thomas if (width == -1) {
1826 8d208d34 2022-06-23 thomas width = 1;
1827 8d208d34 2022-06-23 thomas wline[i] = L'.';
1828 f91a2b48 2022-06-23 thomas }
1829 f91a2b48 2022-06-23 thomas
1830 8d208d34 2022-06-23 thomas if (cols + width > n)
1831 8d208d34 2022-06-23 thomas break;
1832 8d208d34 2022-06-23 thomas cols += width;
1833 f91a2b48 2022-06-23 thomas }
1834 f91a2b48 2022-06-23 thomas
1835 8d208d34 2022-06-23 thomas *rcol = cols;
1836 8d208d34 2022-06-23 thomas return i;
1837 f91a2b48 2022-06-23 thomas }
1838 f91a2b48 2022-06-23 thomas
1839 f91a2b48 2022-06-23 thomas /*
1840 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1841 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1842 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1843 f91a2b48 2022-06-23 thomas */
1844 f91a2b48 2022-06-23 thomas static const struct got_error *
1845 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1846 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1847 f91a2b48 2022-06-23 thomas {
1848 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1849 8d208d34 2022-06-23 thomas int cols;
1850 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1851 05171be4 2022-06-23 thomas char *exstr = NULL;
1852 963b370f 2018-05-20 stsp size_t wlen;
1853 8d208d34 2022-06-23 thomas int i, scrollx;
1854 963b370f 2018-05-20 stsp
1855 963b370f 2018-05-20 stsp *wlinep = NULL;
1856 b700b5d6 2018-07-10 stsp *widthp = 0;
1857 963b370f 2018-05-20 stsp
1858 05171be4 2022-06-23 thomas if (expand) {
1859 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1860 05171be4 2022-06-23 thomas if (err)
1861 05171be4 2022-06-23 thomas return err;
1862 05171be4 2022-06-23 thomas }
1863 05171be4 2022-06-23 thomas
1864 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1865 05171be4 2022-06-23 thomas free(exstr);
1866 963b370f 2018-05-20 stsp if (err)
1867 963b370f 2018-05-20 stsp return err;
1868 963b370f 2018-05-20 stsp
1869 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1870 f91a2b48 2022-06-23 thomas
1871 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1872 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1873 3f670bfb 2020-12-10 stsp wlen--;
1874 3f670bfb 2020-12-10 stsp }
1875 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1876 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1877 3f670bfb 2020-12-10 stsp wlen--;
1878 3f670bfb 2020-12-10 stsp }
1879 3f670bfb 2020-12-10 stsp
1880 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1881 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1882 27a741e5 2019-09-11 stsp
1883 b700b5d6 2018-07-10 stsp if (widthp)
1884 b700b5d6 2018-07-10 stsp *widthp = cols;
1885 f91a2b48 2022-06-23 thomas if (scrollxp)
1886 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1887 963b370f 2018-05-20 stsp if (err)
1888 963b370f 2018-05-20 stsp free(wline);
1889 963b370f 2018-05-20 stsp else
1890 963b370f 2018-05-20 stsp *wlinep = wline;
1891 963b370f 2018-05-20 stsp return err;
1892 963b370f 2018-05-20 stsp }
1893 963b370f 2018-05-20 stsp
1894 8b473291 2019-02-21 stsp static const struct got_error*
1895 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1896 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1897 8b473291 2019-02-21 stsp {
1898 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1899 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1900 8b473291 2019-02-21 stsp char *s;
1901 8b473291 2019-02-21 stsp const char *name;
1902 8b473291 2019-02-21 stsp
1903 8b473291 2019-02-21 stsp *refs_str = NULL;
1904 8b473291 2019-02-21 stsp
1905 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1906 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1907 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1908 52b5abe1 2019-08-13 stsp int cmp;
1909 52b5abe1 2019-08-13 stsp
1910 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1911 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1912 8b473291 2019-02-21 stsp continue;
1913 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1914 8b473291 2019-02-21 stsp name += 5;
1915 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1916 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1917 7143d404 2019-03-12 stsp continue;
1918 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1919 8b473291 2019-02-21 stsp name += 6;
1920 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1921 8b473291 2019-02-21 stsp name += 8;
1922 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1923 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1924 79cc719f 2020-04-24 stsp continue;
1925 79cc719f 2020-04-24 stsp }
1926 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1927 48cae60d 2020-09-22 stsp if (err)
1928 48cae60d 2020-09-22 stsp break;
1929 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1930 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1931 5d844a1e 2019-08-13 stsp if (err) {
1932 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1933 48cae60d 2020-09-22 stsp free(ref_id);
1934 5d844a1e 2019-08-13 stsp break;
1935 48cae60d 2020-09-22 stsp }
1936 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1937 5d844a1e 2019-08-13 stsp err = NULL;
1938 5d844a1e 2019-08-13 stsp tag = NULL;
1939 5d844a1e 2019-08-13 stsp }
1940 52b5abe1 2019-08-13 stsp }
1941 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1942 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1943 48cae60d 2020-09-22 stsp free(ref_id);
1944 52b5abe1 2019-08-13 stsp if (tag)
1945 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1946 52b5abe1 2019-08-13 stsp if (cmp != 0)
1947 52b5abe1 2019-08-13 stsp continue;
1948 8b473291 2019-02-21 stsp s = *refs_str;
1949 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1950 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1951 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1952 8b473291 2019-02-21 stsp free(s);
1953 8b473291 2019-02-21 stsp *refs_str = NULL;
1954 8b473291 2019-02-21 stsp break;
1955 8b473291 2019-02-21 stsp }
1956 8b473291 2019-02-21 stsp free(s);
1957 8b473291 2019-02-21 stsp }
1958 8b473291 2019-02-21 stsp
1959 8b473291 2019-02-21 stsp return err;
1960 8b473291 2019-02-21 stsp }
1961 8b473291 2019-02-21 stsp
1962 963b370f 2018-05-20 stsp static const struct got_error *
1963 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1964 27a741e5 2019-09-11 stsp int col_tab_align)
1965 5813d178 2019-03-09 stsp {
1966 e6b8b890 2020-12-29 naddy char *smallerthan;
1967 5813d178 2019-03-09 stsp
1968 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1969 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1970 5813d178 2019-03-09 stsp author = smallerthan + 1;
1971 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1972 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1973 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1974 5813d178 2019-03-09 stsp }
1975 5813d178 2019-03-09 stsp
1976 5813d178 2019-03-09 stsp static const struct got_error *
1977 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1978 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1979 8fdc79fe 2020-12-01 naddy int author_display_cols)
1980 80ddbec8 2018-04-29 stsp {
1981 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1982 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1983 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1984 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1985 5813d178 2019-03-09 stsp char *author = NULL;
1986 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1987 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1988 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1989 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1990 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1991 ccb26ccd 2018-11-05 stsp struct tm tm;
1992 45d799e2 2018-12-23 stsp time_t committer_time;
1993 11b20872 2019-11-08 stsp struct tog_color *tc;
1994 80ddbec8 2018-04-29 stsp
1995 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1996 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1997 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1998 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1999 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2000 b39d25c7 2018-07-10 stsp
2001 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2002 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2003 b39d25c7 2018-07-10 stsp else
2004 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2005 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2006 11b20872 2019-11-08 stsp if (tc)
2007 11b20872 2019-11-08 stsp wattr_on(view->window,
2008 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2009 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2010 11b20872 2019-11-08 stsp if (tc)
2011 11b20872 2019-11-08 stsp wattr_off(view->window,
2012 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2013 27a741e5 2019-09-11 stsp col = limit;
2014 b39d25c7 2018-07-10 stsp if (col > avail)
2015 b39d25c7 2018-07-10 stsp goto done;
2016 6570a66d 2019-11-08 stsp
2017 6570a66d 2019-11-08 stsp if (avail >= 120) {
2018 6570a66d 2019-11-08 stsp char *id_str;
2019 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2020 6570a66d 2019-11-08 stsp if (err)
2021 6570a66d 2019-11-08 stsp goto done;
2022 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2023 11b20872 2019-11-08 stsp if (tc)
2024 11b20872 2019-11-08 stsp wattr_on(view->window,
2025 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2026 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2027 11b20872 2019-11-08 stsp if (tc)
2028 11b20872 2019-11-08 stsp wattr_off(view->window,
2029 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2030 6570a66d 2019-11-08 stsp free(id_str);
2031 6570a66d 2019-11-08 stsp col += 9;
2032 6570a66d 2019-11-08 stsp if (col > avail)
2033 6570a66d 2019-11-08 stsp goto done;
2034 6570a66d 2019-11-08 stsp }
2035 b39d25c7 2018-07-10 stsp
2036 f69c5a46 2022-07-19 thomas if (s->use_committer)
2037 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2038 f69c5a46 2022-07-19 thomas else
2039 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2040 5813d178 2019-03-09 stsp if (author == NULL) {
2041 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2042 80ddbec8 2018-04-29 stsp goto done;
2043 80ddbec8 2018-04-29 stsp }
2044 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2045 bb737323 2018-05-20 stsp if (err)
2046 bb737323 2018-05-20 stsp goto done;
2047 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2048 11b20872 2019-11-08 stsp if (tc)
2049 11b20872 2019-11-08 stsp wattr_on(view->window,
2050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2051 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2052 bb737323 2018-05-20 stsp col += author_width;
2053 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2054 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2055 bb737323 2018-05-20 stsp col++;
2056 bb737323 2018-05-20 stsp author_width++;
2057 bb737323 2018-05-20 stsp }
2058 f31d6c3b 2022-09-09 thomas if (tc)
2059 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2060 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2061 9c2eaf34 2018-05-20 stsp if (col > avail)
2062 9c2eaf34 2018-05-20 stsp goto done;
2063 80ddbec8 2018-04-29 stsp
2064 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2065 5943eee2 2019-08-13 stsp if (err)
2066 6d9fbc00 2018-04-29 stsp goto done;
2067 bb737323 2018-05-20 stsp logmsg = logmsg0;
2068 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2069 bb737323 2018-05-20 stsp logmsg++;
2070 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2071 bb737323 2018-05-20 stsp if (newline)
2072 bb737323 2018-05-20 stsp *newline = '\0';
2073 f91a2b48 2022-06-23 thomas limit = avail - col;
2074 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2075 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2076 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2077 f91a2b48 2022-06-23 thomas limit, col, 1);
2078 331b1a16 2022-06-23 thomas if (err)
2079 331b1a16 2022-06-23 thomas goto done;
2080 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2081 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2082 27a741e5 2019-09-11 stsp while (col < avail) {
2083 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2084 bb737323 2018-05-20 stsp col++;
2085 881b2d3e 2018-04-30 stsp }
2086 80ddbec8 2018-04-29 stsp done:
2087 80ddbec8 2018-04-29 stsp free(logmsg0);
2088 bb737323 2018-05-20 stsp free(wlogmsg);
2089 5813d178 2019-03-09 stsp free(author);
2090 bb737323 2018-05-20 stsp free(wauthor);
2091 80ddbec8 2018-04-29 stsp free(line);
2092 80ddbec8 2018-04-29 stsp return err;
2093 80ddbec8 2018-04-29 stsp }
2094 26ed57b2 2018-05-19 stsp
2095 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2096 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2097 899d86c2 2018-05-10 stsp struct got_object_id *id)
2098 80ddbec8 2018-04-29 stsp {
2099 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2100 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2101 80ddbec8 2018-04-29 stsp
2102 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2103 80ddbec8 2018-04-29 stsp if (entry == NULL)
2104 899d86c2 2018-05-10 stsp return NULL;
2105 99db9666 2018-05-07 stsp
2106 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2107 d68b9737 2022-09-05 thomas if (dup == NULL) {
2108 d68b9737 2022-09-05 thomas free(entry);
2109 d68b9737 2022-09-05 thomas return NULL;
2110 d68b9737 2022-09-05 thomas }
2111 d68b9737 2022-09-05 thomas
2112 d68b9737 2022-09-05 thomas entry->id = dup;
2113 99db9666 2018-05-07 stsp entry->commit = commit;
2114 899d86c2 2018-05-10 stsp return entry;
2115 99db9666 2018-05-07 stsp }
2116 80ddbec8 2018-04-29 stsp
2117 99db9666 2018-05-07 stsp static void
2118 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2119 99db9666 2018-05-07 stsp {
2120 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2121 99db9666 2018-05-07 stsp
2122 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2123 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2124 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2125 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2126 d68b9737 2022-09-05 thomas free(entry->id);
2127 99db9666 2018-05-07 stsp free(entry);
2128 99db9666 2018-05-07 stsp }
2129 99db9666 2018-05-07 stsp
2130 99db9666 2018-05-07 stsp static void
2131 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2132 99db9666 2018-05-07 stsp {
2133 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2134 99db9666 2018-05-07 stsp pop_commit(commits);
2135 c4972b91 2018-05-07 stsp }
2136 c4972b91 2018-05-07 stsp
2137 c4972b91 2018-05-07 stsp static const struct got_error *
2138 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2139 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2140 13add988 2019-10-15 stsp {
2141 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2142 13add988 2019-10-15 stsp regmatch_t regmatch;
2143 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2144 13add988 2019-10-15 stsp
2145 13add988 2019-10-15 stsp *have_match = 0;
2146 13add988 2019-10-15 stsp
2147 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2148 13add988 2019-10-15 stsp if (err)
2149 13add988 2019-10-15 stsp return err;
2150 13add988 2019-10-15 stsp
2151 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2152 13add988 2019-10-15 stsp if (err)
2153 13add988 2019-10-15 stsp goto done;
2154 13add988 2019-10-15 stsp
2155 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2156 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2157 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2158 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2159 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2160 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2161 13add988 2019-10-15 stsp *have_match = 1;
2162 13add988 2019-10-15 stsp done:
2163 13add988 2019-10-15 stsp free(id_str);
2164 13add988 2019-10-15 stsp free(logmsg);
2165 13add988 2019-10-15 stsp return err;
2166 13add988 2019-10-15 stsp }
2167 13add988 2019-10-15 stsp
2168 13add988 2019-10-15 stsp static const struct got_error *
2169 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2170 c4972b91 2018-05-07 stsp {
2171 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2172 9ba79e04 2018-06-11 stsp
2173 1a76625f 2018-10-22 stsp /*
2174 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2175 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2176 1a76625f 2018-10-22 stsp * while updating the display.
2177 1a76625f 2018-10-22 stsp */
2178 4e0d2870 2020-12-07 naddy do {
2179 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2180 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2181 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2182 1a76625f 2018-10-22 stsp int errcode;
2183 899d86c2 2018-05-10 stsp
2184 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2185 4e0d2870 2020-12-07 naddy NULL, NULL);
2186 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2187 ecb28ae0 2018-07-16 stsp break;
2188 899d86c2 2018-05-10 stsp
2189 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2190 9ba79e04 2018-06-11 stsp if (err)
2191 9ba79e04 2018-06-11 stsp break;
2192 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2193 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2194 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2195 9ba79e04 2018-06-11 stsp break;
2196 9ba79e04 2018-06-11 stsp }
2197 93e45b7c 2018-09-24 stsp
2198 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2199 1a76625f 2018-10-22 stsp if (errcode) {
2200 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2201 13add988 2019-10-15 stsp "pthread_mutex_lock");
2202 1a76625f 2018-10-22 stsp break;
2203 1a76625f 2018-10-22 stsp }
2204 1a76625f 2018-10-22 stsp
2205 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2206 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2207 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2208 1a76625f 2018-10-22 stsp
2209 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2210 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2211 7c1452c1 2020-03-26 stsp int have_match;
2212 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2213 7c1452c1 2020-03-26 stsp if (err)
2214 7c1452c1 2020-03-26 stsp break;
2215 7c1452c1 2020-03-26 stsp if (have_match)
2216 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2217 13add988 2019-10-15 stsp }
2218 13add988 2019-10-15 stsp
2219 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2220 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2221 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2222 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2223 7c1452c1 2020-03-26 stsp if (err)
2224 13add988 2019-10-15 stsp break;
2225 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2226 899d86c2 2018-05-10 stsp
2227 9ba79e04 2018-06-11 stsp return err;
2228 0553a4e3 2018-04-30 stsp }
2229 0553a4e3 2018-04-30 stsp
2230 2b779855 2020-12-05 naddy static void
2231 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2232 2b779855 2020-12-05 naddy {
2233 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2234 2b779855 2020-12-05 naddy int ncommits = 0;
2235 2b779855 2020-12-05 naddy
2236 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2237 2b779855 2020-12-05 naddy while (entry) {
2238 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2239 2b779855 2020-12-05 naddy s->selected_entry = entry;
2240 2b779855 2020-12-05 naddy break;
2241 2b779855 2020-12-05 naddy }
2242 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2243 2b779855 2020-12-05 naddy ncommits++;
2244 2b779855 2020-12-05 naddy }
2245 2b779855 2020-12-05 naddy }
2246 2b779855 2020-12-05 naddy
2247 0553a4e3 2018-04-30 stsp static const struct got_error *
2248 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2249 0553a4e3 2018-04-30 stsp {
2250 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2251 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2252 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2253 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2254 60493ae3 2019-06-20 stsp int width;
2255 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2256 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2257 8b473291 2019-02-21 stsp char *refs_str = NULL;
2258 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2259 11b20872 2019-11-08 stsp struct tog_color *tc;
2260 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2261 bd3f8225 2022-08-12 thomas
2262 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2263 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2264 0553a4e3 2018-04-30 stsp
2265 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2266 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2267 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2268 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2269 1a76625f 2018-10-22 stsp if (err)
2270 ecb28ae0 2018-07-16 stsp return err;
2271 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2272 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2273 d2075bf3 2020-12-25 stsp if (refs) {
2274 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2275 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2276 d2075bf3 2020-12-25 stsp if (err)
2277 d2075bf3 2020-12-25 stsp goto done;
2278 d2075bf3 2020-12-25 stsp }
2279 867c6645 2018-07-10 stsp }
2280 359bfafd 2019-02-22 stsp
2281 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2282 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2283 1a76625f 2018-10-22 stsp
2284 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2285 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2286 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2287 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2288 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2289 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2290 8f4ed634 2020-03-26 stsp goto done;
2291 8f4ed634 2020-03-26 stsp }
2292 8f4ed634 2020-03-26 stsp } else {
2293 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2294 f9686aa5 2020-03-27 stsp
2295 f9686aa5 2020-03-27 stsp if (view->searching) {
2296 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2297 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2298 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2299 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2300 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2301 f9686aa5 2020-03-27 stsp search_str = "searching...";
2302 f9686aa5 2020-03-27 stsp }
2303 f9686aa5 2020-03-27 stsp
2304 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2305 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2306 f9686aa5 2020-03-27 stsp search_str ? search_str :
2307 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2308 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2309 8f4ed634 2020-03-26 stsp goto done;
2310 8f4ed634 2020-03-26 stsp }
2311 8b473291 2019-02-21 stsp }
2312 1a76625f 2018-10-22 stsp
2313 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2314 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2315 91198554 2022-06-23 thomas "........................................",
2316 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2317 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2318 1a76625f 2018-10-22 stsp header = NULL;
2319 1a76625f 2018-10-22 stsp goto done;
2320 1a76625f 2018-10-22 stsp }
2321 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2322 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2323 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2324 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2325 1a76625f 2018-10-22 stsp header = NULL;
2326 1a76625f 2018-10-22 stsp goto done;
2327 ecb28ae0 2018-07-16 stsp }
2328 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2329 1a76625f 2018-10-22 stsp if (err)
2330 1a76625f 2018-10-22 stsp goto done;
2331 867c6645 2018-07-10 stsp
2332 2814baeb 2018-08-01 stsp werase(view->window);
2333 867c6645 2018-07-10 stsp
2334 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2335 a3404814 2018-09-02 stsp wstandout(view->window);
2336 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2337 11b20872 2019-11-08 stsp if (tc)
2338 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2339 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2340 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2341 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2342 1a76625f 2018-10-22 stsp width++;
2343 1a76625f 2018-10-22 stsp }
2344 86f4aab9 2022-09-09 thomas if (tc)
2345 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2346 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2347 a3404814 2018-09-02 stsp wstandend(view->window);
2348 ecb28ae0 2018-07-16 stsp free(wline);
2349 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2350 1a76625f 2018-10-22 stsp goto done;
2351 0553a4e3 2018-04-30 stsp
2352 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2353 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2354 5813d178 2019-03-09 stsp ncommits = 0;
2355 05171be4 2022-06-23 thomas view->maxx = 0;
2356 5813d178 2019-03-09 stsp while (entry) {
2357 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2358 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2359 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2360 5813d178 2019-03-09 stsp int width;
2361 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2362 5813d178 2019-03-09 stsp break;
2363 f69c5a46 2022-07-19 thomas if (s->use_committer)
2364 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2365 f69c5a46 2022-07-19 thomas else
2366 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2367 5813d178 2019-03-09 stsp if (author == NULL) {
2368 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2369 5813d178 2019-03-09 stsp goto done;
2370 5813d178 2019-03-09 stsp }
2371 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2372 27a741e5 2019-09-11 stsp date_display_cols);
2373 5813d178 2019-03-09 stsp if (author_cols < width)
2374 5813d178 2019-03-09 stsp author_cols = width;
2375 5813d178 2019-03-09 stsp free(wauthor);
2376 5813d178 2019-03-09 stsp free(author);
2377 ef944b8b 2022-07-21 thomas if (err)
2378 ef944b8b 2022-07-21 thomas goto done;
2379 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2380 05171be4 2022-06-23 thomas if (err)
2381 05171be4 2022-06-23 thomas goto done;
2382 05171be4 2022-06-23 thomas msg = msg0;
2383 05171be4 2022-06-23 thomas while (*msg == '\n')
2384 05171be4 2022-06-23 thomas ++msg;
2385 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2386 331b1a16 2022-06-23 thomas *eol = '\0';
2387 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2388 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2389 331b1a16 2022-06-23 thomas if (err)
2390 331b1a16 2022-06-23 thomas goto done;
2391 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2392 05171be4 2022-06-23 thomas free(msg0);
2393 331b1a16 2022-06-23 thomas free(wmsg);
2394 7ca04879 2019-10-19 stsp ncommits++;
2395 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2396 5813d178 2019-03-09 stsp }
2397 5813d178 2019-03-09 stsp
2398 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2399 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2400 867c6645 2018-07-10 stsp ncommits = 0;
2401 899d86c2 2018-05-10 stsp while (entry) {
2402 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2403 899d86c2 2018-05-10 stsp break;
2404 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2405 2814baeb 2018-08-01 stsp wstandout(view->window);
2406 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2407 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2408 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2409 2814baeb 2018-08-01 stsp wstandend(view->window);
2410 0553a4e3 2018-04-30 stsp if (err)
2411 60493ae3 2019-06-20 stsp goto done;
2412 0553a4e3 2018-04-30 stsp ncommits++;
2413 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2414 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2415 80ddbec8 2018-04-29 stsp }
2416 80ddbec8 2018-04-29 stsp
2417 a5d43cac 2022-07-01 thomas view_border(view);
2418 1a76625f 2018-10-22 stsp done:
2419 1a76625f 2018-10-22 stsp free(id_str);
2420 8b473291 2019-02-21 stsp free(refs_str);
2421 1a76625f 2018-10-22 stsp free(ncommits_str);
2422 1a76625f 2018-10-22 stsp free(header);
2423 80ddbec8 2018-04-29 stsp return err;
2424 9f7d7167 2018-04-29 stsp }
2425 07b55e75 2018-05-10 stsp
2426 07b55e75 2018-05-10 stsp static void
2427 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2428 07b55e75 2018-05-10 stsp {
2429 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2430 07b55e75 2018-05-10 stsp int nscrolled = 0;
2431 07b55e75 2018-05-10 stsp
2432 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2433 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2434 07b55e75 2018-05-10 stsp return;
2435 9f7d7167 2018-04-29 stsp
2436 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2437 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2438 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2439 07b55e75 2018-05-10 stsp if (entry) {
2440 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2441 07b55e75 2018-05-10 stsp nscrolled++;
2442 07b55e75 2018-05-10 stsp }
2443 07b55e75 2018-05-10 stsp }
2444 aa075928 2018-05-10 stsp }
2445 aa075928 2018-05-10 stsp
2446 aa075928 2018-05-10 stsp static const struct got_error *
2447 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2448 aa075928 2018-05-10 stsp {
2449 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2450 5e224a3e 2019-02-22 stsp int errcode;
2451 8a42fca8 2019-02-22 stsp
2452 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2453 aa075928 2018-05-10 stsp
2454 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2455 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2456 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2457 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2458 7aafa0d1 2019-02-22 stsp if (errcode)
2459 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2460 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2461 7c1452c1 2020-03-26 stsp
2462 7c1452c1 2020-03-26 stsp /*
2463 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2464 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2465 7c1452c1 2020-03-26 stsp */
2466 7c1452c1 2020-03-26 stsp if (!wait)
2467 7c1452c1 2020-03-26 stsp break;
2468 7c1452c1 2020-03-26 stsp
2469 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2470 ffe38506 2020-12-01 naddy show_log_view(view);
2471 7c1452c1 2020-03-26 stsp update_panels();
2472 7c1452c1 2020-03-26 stsp doupdate();
2473 7c1452c1 2020-03-26 stsp
2474 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2475 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2476 82954512 2020-02-03 stsp if (errcode)
2477 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2478 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2479 82954512 2020-02-03 stsp
2480 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2481 ffe38506 2020-12-01 naddy show_log_view(view);
2482 7c1452c1 2020-03-26 stsp update_panels();
2483 7c1452c1 2020-03-26 stsp doupdate();
2484 5e224a3e 2019-02-22 stsp }
2485 5e224a3e 2019-02-22 stsp
2486 5e224a3e 2019-02-22 stsp return NULL;
2487 a5d43cac 2022-07-01 thomas }
2488 a5d43cac 2022-07-01 thomas
2489 a5d43cac 2022-07-01 thomas static const struct got_error *
2490 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2491 a5d43cac 2022-07-01 thomas {
2492 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2493 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2494 fe731b51 2022-07-14 thomas
2495 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2496 fe731b51 2022-07-14 thomas return NULL;
2497 a5d43cac 2022-07-01 thomas
2498 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2499 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2500 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2501 a5d43cac 2022-07-01 thomas
2502 a5d43cac 2022-07-01 thomas return err;
2503 5e224a3e 2019-02-22 stsp }
2504 5e224a3e 2019-02-22 stsp
2505 5e224a3e 2019-02-22 stsp static const struct got_error *
2506 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2507 5e224a3e 2019-02-22 stsp {
2508 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2509 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2510 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2511 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2512 5e224a3e 2019-02-22 stsp
2513 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2514 5e224a3e 2019-02-22 stsp return NULL;
2515 5e224a3e 2019-02-22 stsp
2516 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2517 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2518 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2519 08ebd0a9 2019-02-22 stsp /*
2520 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2521 08ebd0a9 2019-02-22 stsp */
2522 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2523 07dd3ed3 2022-08-06 thomas ncommits_needed - s->commits.ncommits;
2524 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2525 5e224a3e 2019-02-22 stsp if (err)
2526 5e224a3e 2019-02-22 stsp return err;
2527 7aafa0d1 2019-02-22 stsp }
2528 b295e71b 2019-02-22 stsp
2529 7aafa0d1 2019-02-22 stsp do {
2530 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2531 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2532 88048b54 2019-02-21 stsp break;
2533 88048b54 2019-02-21 stsp
2534 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2535 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2536 aa075928 2018-05-10 stsp
2537 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2538 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2539 dd0a52c1 2018-05-20 stsp break;
2540 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2541 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2542 aa075928 2018-05-10 stsp
2543 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2544 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2545 a5d43cac 2022-07-01 thomas else
2546 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2547 a5d43cac 2022-07-01 thomas
2548 dd0a52c1 2018-05-20 stsp return err;
2549 07b55e75 2018-05-10 stsp }
2550 4a7f7875 2018-05-10 stsp
2551 cd0acaa7 2018-05-20 stsp static const struct got_error *
2552 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2553 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2554 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2555 cd0acaa7 2018-05-20 stsp {
2556 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2557 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2558 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2559 cd0acaa7 2018-05-20 stsp
2560 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2561 15a94983 2018-12-23 stsp if (diff_view == NULL)
2562 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2563 ea5e7bb5 2018-08-01 stsp
2564 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2565 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2566 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2567 e5a0f69f 2018-08-18 stsp if (err == NULL)
2568 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2569 cd0acaa7 2018-05-20 stsp return err;
2570 4a7f7875 2018-05-10 stsp }
2571 4a7f7875 2018-05-10 stsp
2572 80ddbec8 2018-04-29 stsp static const struct got_error *
2573 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2574 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2575 9343a5fb 2018-06-23 stsp {
2576 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2577 941e9f74 2019-05-21 stsp
2578 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2579 941e9f74 2019-05-21 stsp if (parent == NULL)
2580 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2581 941e9f74 2019-05-21 stsp
2582 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2583 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2584 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2585 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2586 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2587 941e9f74 2019-05-21 stsp s->tree = subtree;
2588 941e9f74 2019-05-21 stsp s->selected = 0;
2589 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2590 941e9f74 2019-05-21 stsp return NULL;
2591 941e9f74 2019-05-21 stsp }
2592 941e9f74 2019-05-21 stsp
2593 941e9f74 2019-05-21 stsp static const struct got_error *
2594 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2595 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2596 941e9f74 2019-05-21 stsp {
2597 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2598 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2599 941e9f74 2019-05-21 stsp const char *p;
2600 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2601 9343a5fb 2018-06-23 stsp
2602 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2603 941e9f74 2019-05-21 stsp p = path;
2604 941e9f74 2019-05-21 stsp while (*p) {
2605 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2606 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2607 56e0773d 2019-11-28 stsp char *te_name;
2608 33cbf02b 2020-01-12 stsp
2609 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2610 33cbf02b 2020-01-12 stsp p++;
2611 941e9f74 2019-05-21 stsp
2612 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2613 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2614 941e9f74 2019-05-21 stsp if (slash == NULL)
2615 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2616 33cbf02b 2020-01-12 stsp else
2617 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2618 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2619 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2620 56e0773d 2019-11-28 stsp break;
2621 941e9f74 2019-05-21 stsp }
2622 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2623 56e0773d 2019-11-28 stsp if (te == NULL) {
2624 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2625 56e0773d 2019-11-28 stsp free(te_name);
2626 941e9f74 2019-05-21 stsp break;
2627 941e9f74 2019-05-21 stsp }
2628 56e0773d 2019-11-28 stsp free(te_name);
2629 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2630 941e9f74 2019-05-21 stsp
2631 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2632 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2633 b03c880f 2019-05-21 stsp
2634 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2635 941e9f74 2019-05-21 stsp if (slash)
2636 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2637 941e9f74 2019-05-21 stsp else
2638 941e9f74 2019-05-21 stsp subpath = strdup(path);
2639 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2640 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2641 941e9f74 2019-05-21 stsp break;
2642 941e9f74 2019-05-21 stsp }
2643 941e9f74 2019-05-21 stsp
2644 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2645 941e9f74 2019-05-21 stsp subpath);
2646 941e9f74 2019-05-21 stsp if (err)
2647 941e9f74 2019-05-21 stsp break;
2648 941e9f74 2019-05-21 stsp
2649 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2650 941e9f74 2019-05-21 stsp free(tree_id);
2651 941e9f74 2019-05-21 stsp if (err)
2652 941e9f74 2019-05-21 stsp break;
2653 941e9f74 2019-05-21 stsp
2654 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2655 941e9f74 2019-05-21 stsp if (err) {
2656 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2657 941e9f74 2019-05-21 stsp break;
2658 941e9f74 2019-05-21 stsp }
2659 941e9f74 2019-05-21 stsp if (slash == NULL)
2660 941e9f74 2019-05-21 stsp break;
2661 941e9f74 2019-05-21 stsp free(subpath);
2662 941e9f74 2019-05-21 stsp subpath = NULL;
2663 941e9f74 2019-05-21 stsp p = slash;
2664 941e9f74 2019-05-21 stsp }
2665 941e9f74 2019-05-21 stsp
2666 941e9f74 2019-05-21 stsp free(subpath);
2667 1a76625f 2018-10-22 stsp return err;
2668 61266923 2020-01-14 stsp }
2669 61266923 2020-01-14 stsp
2670 61266923 2020-01-14 stsp static const struct got_error *
2671 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2672 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2673 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2674 55cccc34 2020-02-20 stsp {
2675 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2676 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2677 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2678 55cccc34 2020-02-20 stsp
2679 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2680 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2681 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2682 55cccc34 2020-02-20 stsp
2683 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2684 bc573f3b 2021-07-10 stsp if (err)
2685 55cccc34 2020-02-20 stsp return err;
2686 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2687 55cccc34 2020-02-20 stsp
2688 55cccc34 2020-02-20 stsp *new_view = tree_view;
2689 55cccc34 2020-02-20 stsp
2690 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2691 55cccc34 2020-02-20 stsp return NULL;
2692 55cccc34 2020-02-20 stsp
2693 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2694 55cccc34 2020-02-20 stsp }
2695 55cccc34 2020-02-20 stsp
2696 55cccc34 2020-02-20 stsp static const struct got_error *
2697 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2698 61266923 2020-01-14 stsp {
2699 61266923 2020-01-14 stsp sigset_t sigset;
2700 61266923 2020-01-14 stsp int errcode;
2701 61266923 2020-01-14 stsp
2702 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2703 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2704 61266923 2020-01-14 stsp
2705 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2706 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2707 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2708 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2709 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2710 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2711 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2712 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2713 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2714 61266923 2020-01-14 stsp
2715 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2716 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2717 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2718 61266923 2020-01-14 stsp
2719 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2720 61266923 2020-01-14 stsp if (errcode)
2721 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2722 61266923 2020-01-14 stsp
2723 61266923 2020-01-14 stsp return NULL;
2724 1a76625f 2018-10-22 stsp }
2725 1a76625f 2018-10-22 stsp
2726 1a76625f 2018-10-22 stsp static void *
2727 1a76625f 2018-10-22 stsp log_thread(void *arg)
2728 1a76625f 2018-10-22 stsp {
2729 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2730 1a76625f 2018-10-22 stsp int errcode = 0;
2731 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2732 1a76625f 2018-10-22 stsp int done = 0;
2733 61266923 2020-01-14 stsp
2734 f2d749db 2022-07-12 thomas /*
2735 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2736 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2737 f2d749db 2022-07-12 thomas */
2738 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2739 f2d749db 2022-07-12 thomas if (errcode) {
2740 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2741 61266923 2020-01-14 stsp return (void *)err;
2742 f2d749db 2022-07-12 thomas }
2743 1a76625f 2018-10-22 stsp
2744 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2745 f2d749db 2022-07-12 thomas if (err) {
2746 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2747 f2d749db 2022-07-12 thomas goto done;
2748 f2d749db 2022-07-12 thomas }
2749 f2d749db 2022-07-12 thomas
2750 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2751 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2752 f2d749db 2022-07-12 thomas if (errcode) {
2753 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2754 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2755 f2d749db 2022-07-12 thomas goto done;
2756 f2d749db 2022-07-12 thomas }
2757 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2758 1a76625f 2018-10-22 stsp if (err) {
2759 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2760 f2d749db 2022-07-12 thomas goto done;
2761 1a76625f 2018-10-22 stsp err = NULL;
2762 1a76625f 2018-10-22 stsp done = 1;
2763 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2764 1a76625f 2018-10-22 stsp a->commits_needed--;
2765 1a76625f 2018-10-22 stsp
2766 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2767 3abe8080 2019-04-10 stsp if (errcode) {
2768 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2769 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2770 f2d749db 2022-07-12 thomas goto done;
2771 3abe8080 2019-04-10 stsp } else if (*a->quit)
2772 1a76625f 2018-10-22 stsp done = 1;
2773 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2774 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2775 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2776 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2777 1a76625f 2018-10-22 stsp }
2778 1a76625f 2018-10-22 stsp
2779 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2780 7c1452c1 2020-03-26 stsp if (errcode) {
2781 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2782 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2783 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2784 f2d749db 2022-07-12 thomas goto done;
2785 7c1452c1 2020-03-26 stsp }
2786 7c1452c1 2020-03-26 stsp
2787 1a76625f 2018-10-22 stsp if (done)
2788 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2789 7c1452c1 2020-03-26 stsp else {
2790 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2791 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2792 7c1452c1 2020-03-26 stsp &tog_mutex);
2793 f2d749db 2022-07-12 thomas if (errcode) {
2794 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2795 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2796 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2797 f2d749db 2022-07-12 thomas goto done;
2798 f2d749db 2022-07-12 thomas }
2799 21355643 2020-12-06 stsp if (*a->quit)
2800 21355643 2020-12-06 stsp done = 1;
2801 7c1452c1 2020-03-26 stsp }
2802 1a76625f 2018-10-22 stsp }
2803 1a76625f 2018-10-22 stsp }
2804 3abe8080 2019-04-10 stsp a->log_complete = 1;
2805 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2806 f2d749db 2022-07-12 thomas if (errcode)
2807 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2808 f2d749db 2022-07-12 thomas done:
2809 f2d749db 2022-07-12 thomas if (err) {
2810 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2811 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2812 f2d749db 2022-07-12 thomas }
2813 1a76625f 2018-10-22 stsp return (void *)err;
2814 1a76625f 2018-10-22 stsp }
2815 1a76625f 2018-10-22 stsp
2816 1a76625f 2018-10-22 stsp static const struct got_error *
2817 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2818 1a76625f 2018-10-22 stsp {
2819 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2820 1a76625f 2018-10-22 stsp int errcode;
2821 1a76625f 2018-10-22 stsp
2822 1a76625f 2018-10-22 stsp if (s->thread) {
2823 1a76625f 2018-10-22 stsp s->quit = 1;
2824 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2825 1a76625f 2018-10-22 stsp if (errcode)
2826 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2827 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2828 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2829 1a76625f 2018-10-22 stsp if (errcode)
2830 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2831 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2832 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2833 1a76625f 2018-10-22 stsp if (errcode)
2834 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2835 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2836 1a76625f 2018-10-22 stsp if (errcode)
2837 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2838 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2839 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2840 1a76625f 2018-10-22 stsp }
2841 1a76625f 2018-10-22 stsp
2842 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2843 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2844 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2845 1af5eddf 2022-06-23 thomas }
2846 1af5eddf 2022-06-23 thomas
2847 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2848 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2849 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2850 1af5eddf 2022-06-23 thomas if (err == NULL)
2851 1af5eddf 2022-06-23 thomas err = pack_err;
2852 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2853 1a76625f 2018-10-22 stsp }
2854 1a76625f 2018-10-22 stsp
2855 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2856 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2857 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2858 1a76625f 2018-10-22 stsp }
2859 1a76625f 2018-10-22 stsp
2860 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2861 9343a5fb 2018-06-23 stsp }
2862 9343a5fb 2018-06-23 stsp
2863 9343a5fb 2018-06-23 stsp static const struct got_error *
2864 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2865 1a76625f 2018-10-22 stsp {
2866 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2867 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2868 276b94a1 2020-11-13 naddy int errcode;
2869 1a76625f 2018-10-22 stsp
2870 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2871 276b94a1 2020-11-13 naddy
2872 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2873 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2874 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2875 276b94a1 2020-11-13 naddy
2876 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2877 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2878 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2879 276b94a1 2020-11-13 naddy
2880 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2881 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2882 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2883 1a76625f 2018-10-22 stsp free(s->start_id);
2884 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2885 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2886 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2887 1a76625f 2018-10-22 stsp return err;
2888 1a76625f 2018-10-22 stsp }
2889 1a76625f 2018-10-22 stsp
2890 1a76625f 2018-10-22 stsp static const struct got_error *
2891 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2892 60493ae3 2019-06-20 stsp {
2893 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2894 60493ae3 2019-06-20 stsp
2895 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2896 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2897 60493ae3 2019-06-20 stsp return NULL;
2898 60493ae3 2019-06-20 stsp }
2899 60493ae3 2019-06-20 stsp
2900 60493ae3 2019-06-20 stsp static const struct got_error *
2901 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2902 60493ae3 2019-06-20 stsp {
2903 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2904 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2905 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2906 60493ae3 2019-06-20 stsp
2907 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2908 f9686aa5 2020-03-27 stsp show_log_view(view);
2909 f9686aa5 2020-03-27 stsp update_panels();
2910 f9686aa5 2020-03-27 stsp doupdate();
2911 f9686aa5 2020-03-27 stsp
2912 96e2b566 2019-07-08 stsp if (s->search_entry) {
2913 13add988 2019-10-15 stsp int errcode, ch;
2914 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2915 13add988 2019-10-15 stsp if (errcode)
2916 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2917 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2918 13add988 2019-10-15 stsp ch = wgetch(view->window);
2919 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2920 13add988 2019-10-15 stsp if (errcode)
2921 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2922 13add988 2019-10-15 stsp "pthread_mutex_lock");
2923 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2924 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2925 678cbce5 2019-07-28 stsp return NULL;
2926 678cbce5 2019-07-28 stsp }
2927 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2928 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2929 96e2b566 2019-07-08 stsp else
2930 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2931 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2932 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2933 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2934 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2935 df5e841d 2022-06-23 thomas
2936 df5e841d 2022-06-23 thomas /*
2937 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2938 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2939 1f4d5162 2022-06-23 thomas * might have changed.
2940 df5e841d 2022-06-23 thomas */
2941 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2942 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2943 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2944 df5e841d 2022-06-23 thomas else
2945 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2946 b74feda6 2022-06-23 thomas } else {
2947 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2948 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2949 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2950 df5e841d 2022-06-23 thomas else
2951 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2952 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2953 b74feda6 2022-06-23 thomas }
2954 20be8d96 2019-06-21 stsp } else {
2955 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2956 20be8d96 2019-06-21 stsp }
2957 60493ae3 2019-06-20 stsp
2958 60493ae3 2019-06-20 stsp while (1) {
2959 13add988 2019-10-15 stsp int have_match = 0;
2960 13add988 2019-10-15 stsp
2961 60493ae3 2019-06-20 stsp if (entry == NULL) {
2962 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2963 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2964 f9967bca 2020-03-27 stsp view->search_next_done =
2965 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2966 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2967 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2968 f801134a 2019-06-25 stsp return NULL;
2969 60493ae3 2019-06-20 stsp }
2970 96e2b566 2019-07-08 stsp /*
2971 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2972 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2973 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2974 96e2b566 2019-07-08 stsp */
2975 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2976 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2977 60493ae3 2019-06-20 stsp }
2978 60493ae3 2019-06-20 stsp
2979 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2980 13add988 2019-10-15 stsp &view->regex);
2981 5943eee2 2019-08-13 stsp if (err)
2982 13add988 2019-10-15 stsp break;
2983 13add988 2019-10-15 stsp if (have_match) {
2984 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2985 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2986 60493ae3 2019-06-20 stsp break;
2987 60493ae3 2019-06-20 stsp }
2988 13add988 2019-10-15 stsp
2989 96e2b566 2019-07-08 stsp s->search_entry = entry;
2990 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2991 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2992 b1bf1435 2019-06-21 stsp else
2993 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2994 60493ae3 2019-06-20 stsp }
2995 60493ae3 2019-06-20 stsp
2996 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2997 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2998 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2999 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3000 60493ae3 2019-06-20 stsp if (err)
3001 60493ae3 2019-06-20 stsp return err;
3002 ead14cbe 2019-06-21 stsp cur++;
3003 ead14cbe 2019-06-21 stsp }
3004 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3005 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3006 60493ae3 2019-06-20 stsp if (err)
3007 60493ae3 2019-06-20 stsp return err;
3008 ead14cbe 2019-06-21 stsp cur--;
3009 60493ae3 2019-06-20 stsp }
3010 60493ae3 2019-06-20 stsp }
3011 60493ae3 2019-06-20 stsp
3012 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3013 96e2b566 2019-07-08 stsp
3014 60493ae3 2019-06-20 stsp return NULL;
3015 60493ae3 2019-06-20 stsp }
3016 60493ae3 2019-06-20 stsp
3017 60493ae3 2019-06-20 stsp static const struct got_error *
3018 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3019 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3020 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3021 80ddbec8 2018-04-29 stsp {
3022 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3023 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3024 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3025 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3026 1a76625f 2018-10-22 stsp int errcode;
3027 80ddbec8 2018-04-29 stsp
3028 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3029 f135c941 2020-02-20 stsp free(s->in_repo_path);
3030 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3031 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3032 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3033 f135c941 2020-02-20 stsp }
3034 ecb28ae0 2018-07-16 stsp
3035 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3036 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3037 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3038 78756c87 2020-11-24 stsp
3039 fb2756b9 2018-08-04 stsp s->repo = repo;
3040 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3041 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3042 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3043 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3044 9cd7cbd1 2020-12-07 stsp goto done;
3045 9cd7cbd1 2020-12-07 stsp }
3046 9cd7cbd1 2020-12-07 stsp }
3047 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3048 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3049 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3050 5036bf37 2018-09-24 stsp goto done;
3051 5036bf37 2018-09-24 stsp }
3052 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3053 e5a0f69f 2018-08-18 stsp
3054 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3055 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3056 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3057 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3058 11b20872 2019-11-08 stsp if (err)
3059 11b20872 2019-11-08 stsp goto done;
3060 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3061 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3062 11b20872 2019-11-08 stsp if (err) {
3063 11b20872 2019-11-08 stsp free_colors(&s->colors);
3064 11b20872 2019-11-08 stsp goto done;
3065 11b20872 2019-11-08 stsp }
3066 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3067 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3068 11b20872 2019-11-08 stsp if (err) {
3069 11b20872 2019-11-08 stsp free_colors(&s->colors);
3070 11b20872 2019-11-08 stsp goto done;
3071 11b20872 2019-11-08 stsp }
3072 11b20872 2019-11-08 stsp }
3073 11b20872 2019-11-08 stsp
3074 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3075 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3076 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3077 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3078 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3079 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3080 1a76625f 2018-10-22 stsp
3081 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3082 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3083 1af5eddf 2022-06-23 thomas if (err)
3084 1af5eddf 2022-06-23 thomas goto done;
3085 1af5eddf 2022-06-23 thomas }
3086 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3087 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3088 7cd52833 2022-06-23 thomas if (err)
3089 7cd52833 2022-06-23 thomas goto done;
3090 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3091 b672a97a 2020-01-27 stsp !s->log_branches);
3092 1a76625f 2018-10-22 stsp if (err)
3093 1a76625f 2018-10-22 stsp goto done;
3094 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3095 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3096 c5b78334 2020-01-12 stsp if (err)
3097 c5b78334 2020-01-12 stsp goto done;
3098 1a76625f 2018-10-22 stsp
3099 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3100 1a76625f 2018-10-22 stsp if (errcode) {
3101 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3102 1a76625f 2018-10-22 stsp goto done;
3103 1a76625f 2018-10-22 stsp }
3104 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3105 7c1452c1 2020-03-26 stsp if (errcode) {
3106 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3107 7c1452c1 2020-03-26 stsp goto done;
3108 7c1452c1 2020-03-26 stsp }
3109 1a76625f 2018-10-22 stsp
3110 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3111 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3112 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3113 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3114 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3115 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3116 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3117 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3118 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3119 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3120 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3121 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3122 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3123 ba4f502b 2018-08-04 stsp done:
3124 1a76625f 2018-10-22 stsp if (err)
3125 1a76625f 2018-10-22 stsp close_log_view(view);
3126 ba4f502b 2018-08-04 stsp return err;
3127 ba4f502b 2018-08-04 stsp }
3128 ba4f502b 2018-08-04 stsp
3129 e5a0f69f 2018-08-18 stsp static const struct got_error *
3130 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3131 ba4f502b 2018-08-04 stsp {
3132 f2f6d207 2020-11-24 stsp const struct got_error *err;
3133 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3134 ba4f502b 2018-08-04 stsp
3135 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3136 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3137 2b380cc8 2018-10-24 stsp &s->thread_args);
3138 2b380cc8 2018-10-24 stsp if (errcode)
3139 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3140 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3141 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3142 f2f6d207 2020-11-24 stsp if (err)
3143 f2f6d207 2020-11-24 stsp return err;
3144 f2f6d207 2020-11-24 stsp }
3145 2b380cc8 2018-10-24 stsp }
3146 2b380cc8 2018-10-24 stsp
3147 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3148 b31f89ff 2022-07-01 thomas }
3149 b31f89ff 2022-07-01 thomas
3150 b31f89ff 2022-07-01 thomas static void
3151 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3152 b31f89ff 2022-07-01 thomas {
3153 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3154 b31f89ff 2022-07-01 thomas
3155 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3156 b31f89ff 2022-07-01 thomas view->count = 0;
3157 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3158 b31f89ff 2022-07-01 thomas return;
3159 b31f89ff 2022-07-01 thomas
3160 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3161 b31f89ff 2022-07-01 thomas || home)
3162 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3163 b31f89ff 2022-07-01 thomas
3164 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3165 b31f89ff 2022-07-01 thomas --s->selected;
3166 b31f89ff 2022-07-01 thomas else
3167 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3168 b31f89ff 2022-07-01 thomas
3169 b31f89ff 2022-07-01 thomas select_commit(s);
3170 b31f89ff 2022-07-01 thomas return;
3171 b31f89ff 2022-07-01 thomas }
3172 b31f89ff 2022-07-01 thomas
3173 b31f89ff 2022-07-01 thomas static const struct got_error *
3174 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3175 b31f89ff 2022-07-01 thomas {
3176 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3177 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3178 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3179 b31f89ff 2022-07-01 thomas
3180 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3181 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3182 b31f89ff 2022-07-01 thomas return NULL;
3183 b31f89ff 2022-07-01 thomas
3184 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3185 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3186 b31f89ff 2022-07-01 thomas
3187 7ed048bd 2022-08-12 thomas if (!page) {
3188 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3189 b31f89ff 2022-07-01 thomas ++s->selected;
3190 b31f89ff 2022-07-01 thomas else
3191 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3192 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3193 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3194 7ed048bd 2022-08-12 thomas int n;
3195 7ed048bd 2022-08-12 thomas
3196 7ed048bd 2022-08-12 thomas s->selected = 0;
3197 7ed048bd 2022-08-12 thomas entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3198 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3199 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3200 7ed048bd 2022-08-12 thomas if (entry == NULL)
3201 7ed048bd 2022-08-12 thomas break;
3202 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3203 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3204 7ed048bd 2022-08-12 thomas }
3205 7ed048bd 2022-08-12 thomas if (n > 0)
3206 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3207 b31f89ff 2022-07-01 thomas } else {
3208 bd3f8225 2022-08-12 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1 &&
3209 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3210 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3211 bd3f8225 2022-08-12 thomas s->commits.ncommits - s->selected_entry->idx - 1);
3212 bd3f8225 2022-08-12 thomas else
3213 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3214 b31f89ff 2022-07-01 thomas }
3215 b31f89ff 2022-07-01 thomas if (err)
3216 b31f89ff 2022-07-01 thomas return err;
3217 b31f89ff 2022-07-01 thomas
3218 a5d43cac 2022-07-01 thomas /*
3219 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3220 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3221 a5d43cac 2022-07-01 thomas */
3222 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3223 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3224 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3225 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3226 25100026 2022-08-13 thomas }
3227 a5d43cac 2022-07-01 thomas
3228 b31f89ff 2022-07-01 thomas select_commit(s);
3229 b31f89ff 2022-07-01 thomas
3230 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3231 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3232 b31f89ff 2022-07-01 thomas view->count = 0;
3233 b31f89ff 2022-07-01 thomas
3234 b31f89ff 2022-07-01 thomas return NULL;
3235 e5a0f69f 2018-08-18 stsp }
3236 04cc582a 2018-08-01 stsp
3237 a5d43cac 2022-07-01 thomas static void
3238 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3239 a5d43cac 2022-07-01 thomas {
3240 24415785 2022-07-03 thomas *x = 0;
3241 24415785 2022-07-03 thomas *y = 0;
3242 24415785 2022-07-03 thomas
3243 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3244 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3245 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3246 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3247 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3248 53d2bdd3 2022-07-10 thomas else
3249 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3250 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3251 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3252 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3253 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3254 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3255 53d2bdd3 2022-07-10 thomas else
3256 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3257 53d2bdd3 2022-07-10 thomas }
3258 a5d43cac 2022-07-01 thomas }
3259 a5d43cac 2022-07-01 thomas
3260 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3261 e5a0f69f 2018-08-18 stsp static const struct got_error *
3262 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3263 a5d43cac 2022-07-01 thomas {
3264 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3265 a5d43cac 2022-07-01 thomas
3266 a5d43cac 2022-07-01 thomas view->nlines = y;
3267 64486692 2022-07-07 thomas view->ncols = COLS;
3268 a5d43cac 2022-07-01 thomas err = view_resize(view);
3269 a5d43cac 2022-07-01 thomas if (err)
3270 a5d43cac 2022-07-01 thomas return err;
3271 a5d43cac 2022-07-01 thomas
3272 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3273 a5d43cac 2022-07-01 thomas
3274 a5d43cac 2022-07-01 thomas return err;
3275 07dd3ed3 2022-08-06 thomas }
3276 07dd3ed3 2022-08-06 thomas
3277 07dd3ed3 2022-08-06 thomas static const struct got_error *
3278 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3279 07dd3ed3 2022-08-06 thomas {
3280 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3281 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3282 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3283 25100026 2022-08-13 thomas
3284 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3285 25100026 2022-08-13 thomas return NULL;
3286 07dd3ed3 2022-08-06 thomas
3287 07dd3ed3 2022-08-06 thomas g = view->gline;
3288 07dd3ed3 2022-08-06 thomas view->gline = 0;
3289 07dd3ed3 2022-08-06 thomas
3290 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3291 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3292 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3293 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3294 07dd3ed3 2022-08-06 thomas select_commit(s);
3295 07dd3ed3 2022-08-06 thomas return NULL;
3296 07dd3ed3 2022-08-06 thomas }
3297 07dd3ed3 2022-08-06 thomas
3298 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3299 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3300 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3301 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3302 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3303 07dd3ed3 2022-08-06 thomas if (err)
3304 07dd3ed3 2022-08-06 thomas return err;
3305 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3306 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3307 07dd3ed3 2022-08-06 thomas
3308 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3309 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3310 07dd3ed3 2022-08-06 thomas
3311 07dd3ed3 2022-08-06 thomas select_commit(s);
3312 07dd3ed3 2022-08-06 thomas return NULL;
3313 07dd3ed3 2022-08-06 thomas
3314 a5d43cac 2022-07-01 thomas }
3315 a5d43cac 2022-07-01 thomas
3316 a5d43cac 2022-07-01 thomas static const struct got_error *
3317 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3318 e5a0f69f 2018-08-18 stsp {
3319 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3320 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3321 7ed048bd 2022-08-12 thomas int eos, nscroll;
3322 80ddbec8 2018-04-29 stsp
3323 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3324 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3325 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3326 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3327 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3328 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3329 fb280deb 2021-08-30 stsp }
3330 c0be8933 2022-08-12 thomas if (err)
3331 c0be8933 2022-08-12 thomas return err;
3332 528dedf3 2021-08-30 stsp }
3333 068ab281 2022-07-01 thomas
3334 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3335 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3336 068ab281 2022-07-01 thomas --eos; /* border */
3337 07dd3ed3 2022-08-06 thomas
3338 07dd3ed3 2022-08-06 thomas if (view->gline)
3339 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3340 068ab281 2022-07-01 thomas
3341 528dedf3 2021-08-30 stsp switch (ch) {
3342 1e37a5c2 2019-05-12 jcs case 'q':
3343 1e37a5c2 2019-05-12 jcs s->quit = 1;
3344 05171be4 2022-06-23 thomas break;
3345 05171be4 2022-06-23 thomas case '0':
3346 05171be4 2022-06-23 thomas view->x = 0;
3347 05171be4 2022-06-23 thomas break;
3348 05171be4 2022-06-23 thomas case '$':
3349 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3350 07b0611c 2022-06-23 thomas view->count = 0;
3351 05171be4 2022-06-23 thomas break;
3352 05171be4 2022-06-23 thomas case KEY_RIGHT:
3353 05171be4 2022-06-23 thomas case 'l':
3354 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3355 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3356 07b0611c 2022-06-23 thomas else
3357 07b0611c 2022-06-23 thomas view->count = 0;
3358 1e37a5c2 2019-05-12 jcs break;
3359 05171be4 2022-06-23 thomas case KEY_LEFT:
3360 05171be4 2022-06-23 thomas case 'h':
3361 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3362 07b0611c 2022-06-23 thomas if (view->x <= 0)
3363 07b0611c 2022-06-23 thomas view->count = 0;
3364 05171be4 2022-06-23 thomas break;
3365 1e37a5c2 2019-05-12 jcs case 'k':
3366 1e37a5c2 2019-05-12 jcs case KEY_UP:
3367 1e37a5c2 2019-05-12 jcs case '<':
3368 1e37a5c2 2019-05-12 jcs case ',':
3369 f7140bf5 2021-10-17 thomas case CTRL('p'):
3370 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3371 912a3f79 2021-08-30 j break;
3372 912a3f79 2021-08-30 j case 'g':
3373 912a3f79 2021-08-30 j case KEY_HOME:
3374 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3375 07b0611c 2022-06-23 thomas view->count = 0;
3376 1e37a5c2 2019-05-12 jcs break;
3377 70f17a53 2022-06-13 thomas case CTRL('u'):
3378 23427b14 2022-06-23 thomas case 'u':
3379 70f17a53 2022-06-13 thomas nscroll /= 2;
3380 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3381 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3382 a4292ac5 2019-05-12 jcs case CTRL('b'):
3383 1c5e5faa 2022-06-23 thomas case 'b':
3384 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3385 1e37a5c2 2019-05-12 jcs break;
3386 1e37a5c2 2019-05-12 jcs case 'j':
3387 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3388 1e37a5c2 2019-05-12 jcs case '>':
3389 1e37a5c2 2019-05-12 jcs case '.':
3390 f7140bf5 2021-10-17 thomas case CTRL('n'):
3391 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3392 912a3f79 2021-08-30 j break;
3393 f69c5a46 2022-07-19 thomas case '@':
3394 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3395 f69c5a46 2022-07-19 thomas break;
3396 912a3f79 2021-08-30 j case 'G':
3397 912a3f79 2021-08-30 j case KEY_END: {
3398 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3399 912a3f79 2021-08-30 j * traverse them all. */
3400 07b0611c 2022-06-23 thomas view->count = 0;
3401 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3402 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3403 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3404 7ed048bd 2022-08-12 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3405 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3406 1e37a5c2 2019-05-12 jcs break;
3407 912a3f79 2021-08-30 j }
3408 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3409 23427b14 2022-06-23 thomas case 'd':
3410 70f17a53 2022-06-13 thomas nscroll /= 2;
3411 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3412 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3413 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3414 4c2d69cb 2022-06-23 thomas case 'f':
3415 b31f89ff 2022-07-01 thomas case ' ':
3416 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3417 1e37a5c2 2019-05-12 jcs break;
3418 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3419 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3420 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3421 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3422 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3423 2b779855 2020-12-05 naddy select_commit(s);
3424 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3425 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3426 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3427 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3428 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3429 0bf7f153 2020-12-02 naddy }
3430 1e37a5c2 2019-05-12 jcs break;
3431 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3432 444d5325 2022-07-03 thomas case '\r':
3433 07b0611c 2022-06-23 thomas view->count = 0;
3434 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3435 e5a0f69f 2018-08-18 stsp break;
3436 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3437 1e37a5c2 2019-05-12 jcs break;
3438 1be4947a 2022-07-22 thomas case 'T':
3439 07b0611c 2022-06-23 thomas view->count = 0;
3440 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3441 5036bf37 2018-09-24 stsp break;
3442 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3443 1e37a5c2 2019-05-12 jcs break;
3444 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3445 21355643 2020-12-06 stsp case CTRL('l'):
3446 21355643 2020-12-06 stsp case 'B':
3447 07b0611c 2022-06-23 thomas view->count = 0;
3448 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3449 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3450 1e37a5c2 2019-05-12 jcs break;
3451 21355643 2020-12-06 stsp err = stop_log_thread(s);
3452 74cfe85e 2020-10-20 stsp if (err)
3453 74cfe85e 2020-10-20 stsp return err;
3454 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3455 21355643 2020-12-06 stsp char *parent_path;
3456 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3457 21355643 2020-12-06 stsp if (err)
3458 21355643 2020-12-06 stsp return err;
3459 21355643 2020-12-06 stsp free(s->in_repo_path);
3460 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3461 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3462 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3463 21355643 2020-12-06 stsp struct got_object_id *start_id;
3464 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3465 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3466 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3467 21355643 2020-12-06 stsp if (err)
3468 21355643 2020-12-06 stsp return err;
3469 21355643 2020-12-06 stsp free(s->start_id);
3470 21355643 2020-12-06 stsp s->start_id = start_id;
3471 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3472 21355643 2020-12-06 stsp } else /* 'B' */
3473 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3474 21355643 2020-12-06 stsp
3475 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3476 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3477 bf7e79b3 2022-06-23 thomas if (err)
3478 bf7e79b3 2022-06-23 thomas return err;
3479 bf7e79b3 2022-06-23 thomas }
3480 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3481 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3482 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3483 74cfe85e 2020-10-20 stsp if (err)
3484 21355643 2020-12-06 stsp return err;
3485 51a10b52 2020-12-26 stsp tog_free_refs();
3486 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3487 ca51c541 2020-12-07 stsp if (err)
3488 ca51c541 2020-12-07 stsp return err;
3489 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3490 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3491 d01904d4 2019-06-25 stsp if (err)
3492 d01904d4 2019-06-25 stsp return err;
3493 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3494 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3495 b672a97a 2020-01-27 stsp if (err)
3496 b672a97a 2020-01-27 stsp return err;
3497 21355643 2020-12-06 stsp free_commits(&s->commits);
3498 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3499 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3500 21355643 2020-12-06 stsp s->selected_entry = NULL;
3501 21355643 2020-12-06 stsp s->selected = 0;
3502 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3503 21355643 2020-12-06 stsp s->quit = 0;
3504 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3505 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3506 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3507 94ecf40d 2022-07-22 thomas view->offset = 0;
3508 d01904d4 2019-06-25 stsp break;
3509 1be4947a 2022-07-22 thomas case 'R':
3510 07b0611c 2022-06-23 thomas view->count = 0;
3511 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3512 6458efa5 2020-11-24 stsp break;
3513 1e37a5c2 2019-05-12 jcs default:
3514 07b0611c 2022-06-23 thomas view->count = 0;
3515 1e37a5c2 2019-05-12 jcs break;
3516 899d86c2 2018-05-10 stsp }
3517 e5a0f69f 2018-08-18 stsp
3518 80ddbec8 2018-04-29 stsp return err;
3519 80ddbec8 2018-04-29 stsp }
3520 80ddbec8 2018-04-29 stsp
3521 4ed7e80c 2018-05-20 stsp static const struct got_error *
3522 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3523 c2db6724 2019-01-04 stsp {
3524 c2db6724 2019-01-04 stsp const struct got_error *error;
3525 c2db6724 2019-01-04 stsp
3526 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3527 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3528 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3529 37c06ea4 2019-07-15 stsp #endif
3530 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3531 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3532 c2db6724 2019-01-04 stsp
3533 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3534 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3535 c2db6724 2019-01-04 stsp
3536 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3537 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3538 c2db6724 2019-01-04 stsp
3539 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3540 c2db6724 2019-01-04 stsp if (error != NULL)
3541 c2db6724 2019-01-04 stsp return error;
3542 c2db6724 2019-01-04 stsp
3543 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3544 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3545 c2db6724 2019-01-04 stsp
3546 c2db6724 2019-01-04 stsp return NULL;
3547 c2db6724 2019-01-04 stsp }
3548 c2db6724 2019-01-04 stsp
3549 a915003a 2019-02-05 stsp static void
3550 a915003a 2019-02-05 stsp init_curses(void)
3551 a915003a 2019-02-05 stsp {
3552 296152d1 2022-05-31 thomas /*
3553 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3554 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3555 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3556 296152d1 2022-05-31 thomas */
3557 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3558 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3559 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3560 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3561 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3562 296152d1 2022-05-31 thomas
3563 a915003a 2019-02-05 stsp initscr();
3564 a915003a 2019-02-05 stsp cbreak();
3565 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3566 a915003a 2019-02-05 stsp noecho();
3567 a915003a 2019-02-05 stsp nonl();
3568 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3569 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3570 a915003a 2019-02-05 stsp curs_set(0);
3571 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3572 6d17833f 2019-11-08 stsp start_color();
3573 6d17833f 2019-11-08 stsp use_default_colors();
3574 6d17833f 2019-11-08 stsp }
3575 a915003a 2019-02-05 stsp }
3576 a915003a 2019-02-05 stsp
3577 c2db6724 2019-01-04 stsp static const struct got_error *
3578 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3579 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3580 f135c941 2020-02-20 stsp {
3581 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3582 f135c941 2020-02-20 stsp
3583 f135c941 2020-02-20 stsp if (argc == 0) {
3584 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3585 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3586 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3587 f135c941 2020-02-20 stsp return NULL;
3588 f135c941 2020-02-20 stsp }
3589 f135c941 2020-02-20 stsp
3590 f135c941 2020-02-20 stsp if (worktree) {
3591 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3592 bfd61697 2020-11-14 stsp char *p;
3593 f135c941 2020-02-20 stsp
3594 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3595 f135c941 2020-02-20 stsp if (err)
3596 f135c941 2020-02-20 stsp return err;
3597 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3598 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3599 bfd61697 2020-11-14 stsp p) == -1) {
3600 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3601 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3602 f135c941 2020-02-20 stsp }
3603 f135c941 2020-02-20 stsp free(p);
3604 f135c941 2020-02-20 stsp } else
3605 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3606 f135c941 2020-02-20 stsp
3607 f135c941 2020-02-20 stsp return err;
3608 f135c941 2020-02-20 stsp }
3609 f135c941 2020-02-20 stsp
3610 f135c941 2020-02-20 stsp static const struct got_error *
3611 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3612 9f7d7167 2018-04-29 stsp {
3613 80ddbec8 2018-04-29 stsp const struct got_error *error;
3614 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3615 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3616 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3617 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3618 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3619 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3620 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3621 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3622 04cc582a 2018-08-01 stsp struct tog_view *view;
3623 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3624 80ddbec8 2018-04-29 stsp
3625 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3626 80ddbec8 2018-04-29 stsp switch (ch) {
3627 b672a97a 2020-01-27 stsp case 'b':
3628 b672a97a 2020-01-27 stsp log_branches = 1;
3629 b672a97a 2020-01-27 stsp break;
3630 80ddbec8 2018-04-29 stsp case 'c':
3631 80ddbec8 2018-04-29 stsp start_commit = optarg;
3632 80ddbec8 2018-04-29 stsp break;
3633 ecb28ae0 2018-07-16 stsp case 'r':
3634 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3635 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3636 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3637 9ba1d308 2019-10-21 stsp optarg);
3638 ecb28ae0 2018-07-16 stsp break;
3639 80ddbec8 2018-04-29 stsp default:
3640 17020d27 2019-03-07 stsp usage_log();
3641 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3642 80ddbec8 2018-04-29 stsp }
3643 80ddbec8 2018-04-29 stsp }
3644 80ddbec8 2018-04-29 stsp
3645 80ddbec8 2018-04-29 stsp argc -= optind;
3646 80ddbec8 2018-04-29 stsp argv += optind;
3647 80ddbec8 2018-04-29 stsp
3648 f135c941 2020-02-20 stsp if (argc > 1)
3649 f135c941 2020-02-20 stsp usage_log();
3650 963f97a1 2019-03-18 stsp
3651 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3652 7cd52833 2022-06-23 thomas if (error != NULL)
3653 7cd52833 2022-06-23 thomas goto done;
3654 7cd52833 2022-06-23 thomas
3655 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3656 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3657 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3658 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3659 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3660 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3661 c156c7a4 2020-12-18 stsp goto done;
3662 a1fbf39a 2019-08-11 stsp if (worktree)
3663 6962eb72 2020-02-20 stsp repo_path =
3664 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3665 a1fbf39a 2019-08-11 stsp else
3666 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3667 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3668 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3669 c156c7a4 2020-12-18 stsp goto done;
3670 c156c7a4 2020-12-18 stsp }
3671 ecb28ae0 2018-07-16 stsp }
3672 ecb28ae0 2018-07-16 stsp
3673 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3674 80ddbec8 2018-04-29 stsp if (error != NULL)
3675 ecb28ae0 2018-07-16 stsp goto done;
3676 80ddbec8 2018-04-29 stsp
3677 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3678 f135c941 2020-02-20 stsp repo, worktree);
3679 f135c941 2020-02-20 stsp if (error)
3680 f135c941 2020-02-20 stsp goto done;
3681 f135c941 2020-02-20 stsp
3682 f135c941 2020-02-20 stsp init_curses();
3683 f135c941 2020-02-20 stsp
3684 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3685 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3686 c02c541e 2019-03-29 stsp if (error)
3687 c02c541e 2019-03-29 stsp goto done;
3688 c02c541e 2019-03-29 stsp
3689 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3690 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3691 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3692 87670572 2020-12-26 naddy if (error)
3693 87670572 2020-12-26 naddy goto done;
3694 87670572 2020-12-26 naddy }
3695 51a10b52 2020-12-26 stsp
3696 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3697 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3698 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3699 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3700 d8f38dc4 2020-12-05 stsp if (error)
3701 d8f38dc4 2020-12-05 stsp goto done;
3702 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3703 d8f38dc4 2020-12-05 stsp } else {
3704 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3705 d8f38dc4 2020-12-05 stsp if (error == NULL)
3706 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3707 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3708 d8f38dc4 2020-12-05 stsp goto done;
3709 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3710 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3711 d8f38dc4 2020-12-05 stsp if (error)
3712 d8f38dc4 2020-12-05 stsp goto done;
3713 d8f38dc4 2020-12-05 stsp }
3714 8b473291 2019-02-21 stsp
3715 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3716 04cc582a 2018-08-01 stsp if (view == NULL) {
3717 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3718 04cc582a 2018-08-01 stsp goto done;
3719 04cc582a 2018-08-01 stsp }
3720 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3721 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3722 ba4f502b 2018-08-04 stsp if (error)
3723 ba4f502b 2018-08-04 stsp goto done;
3724 2fc00ff4 2019-08-31 stsp if (worktree) {
3725 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3726 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3727 2fc00ff4 2019-08-31 stsp worktree = NULL;
3728 2fc00ff4 2019-08-31 stsp }
3729 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3730 ecb28ae0 2018-07-16 stsp done:
3731 f135c941 2020-02-20 stsp free(in_repo_path);
3732 ecb28ae0 2018-07-16 stsp free(repo_path);
3733 ecb28ae0 2018-07-16 stsp free(cwd);
3734 899d86c2 2018-05-10 stsp free(start_id);
3735 d8f38dc4 2020-12-05 stsp free(label);
3736 d8f38dc4 2020-12-05 stsp if (ref)
3737 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3738 1d0f4054 2021-06-17 stsp if (repo) {
3739 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3740 1d0f4054 2021-06-17 stsp if (error == NULL)
3741 1d0f4054 2021-06-17 stsp error = close_err;
3742 1d0f4054 2021-06-17 stsp }
3743 ec142235 2019-03-07 stsp if (worktree)
3744 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3745 7cd52833 2022-06-23 thomas if (pack_fds) {
3746 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3747 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3748 7cd52833 2022-06-23 thomas if (error == NULL)
3749 7cd52833 2022-06-23 thomas error = pack_err;
3750 7cd52833 2022-06-23 thomas }
3751 51a10b52 2020-12-26 stsp tog_free_refs();
3752 80ddbec8 2018-04-29 stsp return error;
3753 9f7d7167 2018-04-29 stsp }
3754 9f7d7167 2018-04-29 stsp
3755 4ed7e80c 2018-05-20 stsp __dead static void
3756 9f7d7167 2018-04-29 stsp usage_diff(void)
3757 9f7d7167 2018-04-29 stsp {
3758 80ddbec8 2018-04-29 stsp endwin();
3759 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3760 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
3761 9f7d7167 2018-04-29 stsp exit(1);
3762 b304db33 2018-05-20 stsp }
3763 b304db33 2018-05-20 stsp
3764 6d17833f 2019-11-08 stsp static int
3765 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3766 41605754 2020-11-12 stsp regmatch_t *regmatch)
3767 6d17833f 2019-11-08 stsp {
3768 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3769 6d17833f 2019-11-08 stsp }
3770 6d17833f 2019-11-08 stsp
3771 ef20f542 2022-06-26 thomas static struct tog_color *
3772 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3773 6d17833f 2019-11-08 stsp {
3774 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3775 6d17833f 2019-11-08 stsp
3776 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3777 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3778 6d17833f 2019-11-08 stsp return tc;
3779 6d17833f 2019-11-08 stsp }
3780 6d17833f 2019-11-08 stsp
3781 6d17833f 2019-11-08 stsp return NULL;
3782 6d17833f 2019-11-08 stsp }
3783 6d17833f 2019-11-08 stsp
3784 4ed7e80c 2018-05-20 stsp static const struct got_error *
3785 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3786 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3787 41605754 2020-11-12 stsp {
3788 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3789 666f7b10 2022-06-23 thomas char *exstr = NULL;
3790 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3791 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3792 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3793 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3794 41605754 2020-11-12 stsp
3795 41605754 2020-11-12 stsp *wtotal = 0;
3796 cbea3800 2022-06-23 thomas
3797 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3798 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3799 41605754 2020-11-12 stsp
3800 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3801 666f7b10 2022-06-23 thomas if (err)
3802 666f7b10 2022-06-23 thomas return err;
3803 666f7b10 2022-06-23 thomas
3804 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3805 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3806 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3807 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3808 666f7b10 2022-06-23 thomas goto done;
3809 666f7b10 2022-06-23 thomas }
3810 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3811 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3812 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3813 cbea3800 2022-06-23 thomas goto done;
3814 cbea3800 2022-06-23 thomas }
3815 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3816 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3817 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3818 cbea3800 2022-06-23 thomas goto done;
3819 cbea3800 2022-06-23 thomas }
3820 05171be4 2022-06-23 thomas
3821 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3822 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3823 cbea3800 2022-06-23 thomas col_tab_align, 1);
3824 cbea3800 2022-06-23 thomas if (err)
3825 cbea3800 2022-06-23 thomas goto done;
3826 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3827 05171be4 2022-06-23 thomas if (n) {
3828 cbea3800 2022-06-23 thomas free(wline);
3829 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3830 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3831 cbea3800 2022-06-23 thomas if (err)
3832 cbea3800 2022-06-23 thomas goto done;
3833 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3834 cbea3800 2022-06-23 thomas wlimit -= width;
3835 cbea3800 2022-06-23 thomas *wtotal += width;
3836 41605754 2020-11-12 stsp }
3837 41605754 2020-11-12 stsp
3838 41605754 2020-11-12 stsp if (wlimit > 0) {
3839 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3840 cbea3800 2022-06-23 thomas size_t wlen;
3841 cbea3800 2022-06-23 thomas
3842 cbea3800 2022-06-23 thomas free(wline);
3843 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3844 cbea3800 2022-06-23 thomas col_tab_align, 1);
3845 cbea3800 2022-06-23 thomas if (err)
3846 cbea3800 2022-06-23 thomas goto done;
3847 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3848 cbea3800 2022-06-23 thomas while (i < wlen) {
3849 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3850 cbea3800 2022-06-23 thomas if (width == -1) {
3851 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3852 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3853 cbea3800 2022-06-23 thomas goto done;
3854 cbea3800 2022-06-23 thomas }
3855 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3856 cbea3800 2022-06-23 thomas break;
3857 1c5e5faa 2022-06-23 thomas w += width;
3858 cbea3800 2022-06-23 thomas i++;
3859 41605754 2020-11-12 stsp }
3860 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3861 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3862 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3863 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3864 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3865 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3866 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3867 41605754 2020-11-12 stsp }
3868 41605754 2020-11-12 stsp }
3869 41605754 2020-11-12 stsp
3870 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3871 cbea3800 2022-06-23 thomas free(wline);
3872 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3873 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3874 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3875 cbea3800 2022-06-23 thomas col_tab_align, 1);
3876 cbea3800 2022-06-23 thomas if (err)
3877 cbea3800 2022-06-23 thomas goto done;
3878 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3879 cbea3800 2022-06-23 thomas } else {
3880 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3881 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3882 cbea3800 2022-06-23 thomas if (err)
3883 cbea3800 2022-06-23 thomas goto done;
3884 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3885 cbea3800 2022-06-23 thomas }
3886 cbea3800 2022-06-23 thomas *wtotal += width2;
3887 41605754 2020-11-12 stsp }
3888 cbea3800 2022-06-23 thomas done:
3889 05171be4 2022-06-23 thomas free(wline);
3890 666f7b10 2022-06-23 thomas free(exstr);
3891 cbea3800 2022-06-23 thomas free(seg0);
3892 cbea3800 2022-06-23 thomas free(seg1);
3893 cbea3800 2022-06-23 thomas free(seg2);
3894 cbea3800 2022-06-23 thomas return err;
3895 41605754 2020-11-12 stsp }
3896 07dd3ed3 2022-08-06 thomas
3897 07dd3ed3 2022-08-06 thomas static int
3898 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
3899 07dd3ed3 2022-08-06 thomas {
3900 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
3901 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
3902 07dd3ed3 2022-08-06 thomas
3903 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
3904 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
3905 41605754 2020-11-12 stsp
3906 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3907 07dd3ed3 2022-08-06 thomas selected = first;
3908 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3909 07dd3ed3 2022-08-06 thomas f = s->f;
3910 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
3911 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
3912 07dd3ed3 2022-08-06 thomas
3913 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3914 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
3915 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3916 07dd3ed3 2022-08-06 thomas f = s->blame.f;
3917 07dd3ed3 2022-08-06 thomas } else
3918 07dd3ed3 2022-08-06 thomas return 0;
3919 07dd3ed3 2022-08-06 thomas
3920 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
3921 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
3922 07dd3ed3 2022-08-06 thomas return 0;
3923 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3924 07dd3ed3 2022-08-06 thomas rewind(f);
3925 07dd3ed3 2022-08-06 thomas *eof = 0;
3926 07dd3ed3 2022-08-06 thomas *first = 1;
3927 07dd3ed3 2022-08-06 thomas *lineno = 0;
3928 07dd3ed3 2022-08-06 thomas *nprinted = 0;
3929 07dd3ed3 2022-08-06 thomas return 0;
3930 07dd3ed3 2022-08-06 thomas }
3931 07dd3ed3 2022-08-06 thomas
3932 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
3933 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
3934 07dd3ed3 2022-08-06 thomas view->gline = 0;
3935 07dd3ed3 2022-08-06 thomas
3936 07dd3ed3 2022-08-06 thomas return 1;
3937 07dd3ed3 2022-08-06 thomas }
3938 07dd3ed3 2022-08-06 thomas
3939 41605754 2020-11-12 stsp static const struct got_error *
3940 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3941 26ed57b2 2018-05-19 stsp {
3942 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3943 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3944 61e69b96 2018-05-20 stsp const struct got_error *err;
3945 82c78e96 2022-08-06 thomas int nprinted = 0;
3946 b304db33 2018-05-20 stsp char *line;
3947 826082fe 2020-12-10 stsp size_t linesize = 0;
3948 826082fe 2020-12-10 stsp ssize_t linelen;
3949 61e69b96 2018-05-20 stsp wchar_t *wline;
3950 e0b650dd 2018-05-20 stsp int width;
3951 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3952 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3953 fe621944 2020-11-10 stsp off_t line_offset;
3954 26ed57b2 2018-05-19 stsp
3955 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
3956 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
3957 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3958 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3959 fe621944 2020-11-10 stsp
3960 f7d12f7e 2018-08-01 stsp werase(view->window);
3961 a3404814 2018-09-02 stsp
3962 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
3963 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
3964 07dd3ed3 2022-08-06 thomas
3965 a3404814 2018-09-02 stsp if (header) {
3966 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3967 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
3968 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
3969 07dd3ed3 2022-08-06 thomas
3970 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3971 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3972 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3973 f91a2b48 2022-06-23 thomas 0, 0);
3974 135a2da0 2020-11-11 stsp free(line);
3975 135a2da0 2020-11-11 stsp if (err)
3976 a3404814 2018-09-02 stsp return err;
3977 a3404814 2018-09-02 stsp
3978 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3979 a3404814 2018-09-02 stsp wstandout(view->window);
3980 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3981 e54cc94a 2020-11-11 stsp free(wline);
3982 e54cc94a 2020-11-11 stsp wline = NULL;
3983 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
3984 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
3985 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3986 a3404814 2018-09-02 stsp wstandend(view->window);
3987 26ed57b2 2018-05-19 stsp
3988 a3404814 2018-09-02 stsp if (max_lines <= 1)
3989 a3404814 2018-09-02 stsp return NULL;
3990 a3404814 2018-09-02 stsp max_lines--;
3991 a3404814 2018-09-02 stsp }
3992 a3404814 2018-09-02 stsp
3993 89f1a395 2020-12-01 naddy s->eof = 0;
3994 05171be4 2022-06-23 thomas view->maxx = 0;
3995 826082fe 2020-12-10 stsp line = NULL;
3996 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3997 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
3998 6568f0aa 2022-08-06 thomas attr_t attr = 0;
3999 6568f0aa 2022-08-06 thomas
4000 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4001 826082fe 2020-12-10 stsp if (linelen == -1) {
4002 826082fe 2020-12-10 stsp if (feof(s->f)) {
4003 826082fe 2020-12-10 stsp s->eof = 1;
4004 826082fe 2020-12-10 stsp break;
4005 826082fe 2020-12-10 stsp }
4006 826082fe 2020-12-10 stsp free(line);
4007 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4008 61e69b96 2018-05-20 stsp }
4009 05171be4 2022-06-23 thomas
4010 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4011 07dd3ed3 2022-08-06 thomas continue;
4012 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4013 07dd3ed3 2022-08-06 thomas continue;
4014 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4015 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4016 07dd3ed3 2022-08-06 thomas
4017 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4018 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4019 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4020 cbea3800 2022-06-23 thomas if (err) {
4021 cbea3800 2022-06-23 thomas free(line);
4022 cbea3800 2022-06-23 thomas return err;
4023 cbea3800 2022-06-23 thomas }
4024 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4025 cbea3800 2022-06-23 thomas free(wline);
4026 cbea3800 2022-06-23 thomas wline = NULL;
4027 cbea3800 2022-06-23 thomas
4028 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4029 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4030 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4031 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4032 07dd3ed3 2022-08-06 thomas if (attr)
4033 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4034 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4035 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4036 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4037 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4038 41605754 2020-11-12 stsp if (err) {
4039 41605754 2020-11-12 stsp free(line);
4040 41605754 2020-11-12 stsp return err;
4041 41605754 2020-11-12 stsp }
4042 41605754 2020-11-12 stsp } else {
4043 f1c0ec19 2022-06-23 thomas int skip;
4044 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4045 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4046 f1c0ec19 2022-06-23 thomas if (err) {
4047 f1c0ec19 2022-06-23 thomas free(line);
4048 f1c0ec19 2022-06-23 thomas return err;
4049 f1c0ec19 2022-06-23 thomas }
4050 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4051 f1c0ec19 2022-06-23 thomas free(wline);
4052 41605754 2020-11-12 stsp wline = NULL;
4053 41605754 2020-11-12 stsp }
4054 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4055 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4056 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4057 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4058 07dd3ed3 2022-08-06 thomas } else {
4059 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4060 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4061 07dd3ed3 2022-08-06 thomas }
4062 07dd3ed3 2022-08-06 thomas if (attr)
4063 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4064 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4065 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4066 826082fe 2020-12-10 stsp }
4067 826082fe 2020-12-10 stsp free(line);
4068 fe621944 2020-11-10 stsp if (nprinted >= 1)
4069 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4070 89f1a395 2020-12-01 naddy (nprinted - 1);
4071 fe621944 2020-11-10 stsp else
4072 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4073 26ed57b2 2018-05-19 stsp
4074 a5d43cac 2022-07-01 thomas view_border(view);
4075 c3e9aa98 2019-05-13 jcs
4076 89f1a395 2020-12-01 naddy if (s->eof) {
4077 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4078 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4079 c3e9aa98 2019-05-13 jcs nprinted++;
4080 c3e9aa98 2019-05-13 jcs }
4081 c3e9aa98 2019-05-13 jcs
4082 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4083 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4084 c3e9aa98 2019-05-13 jcs if (err) {
4085 c3e9aa98 2019-05-13 jcs return err;
4086 c3e9aa98 2019-05-13 jcs }
4087 26ed57b2 2018-05-19 stsp
4088 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4089 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4090 e54cc94a 2020-11-11 stsp free(wline);
4091 e54cc94a 2020-11-11 stsp wline = NULL;
4092 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4093 c3e9aa98 2019-05-13 jcs }
4094 c3e9aa98 2019-05-13 jcs
4095 26ed57b2 2018-05-19 stsp return NULL;
4096 abd2672a 2018-12-23 stsp }
4097 abd2672a 2018-12-23 stsp
4098 abd2672a 2018-12-23 stsp static char *
4099 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4100 abd2672a 2018-12-23 stsp {
4101 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4102 09867e48 2019-08-13 stsp char *p, *s;
4103 09867e48 2019-08-13 stsp
4104 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4105 09867e48 2019-08-13 stsp if (tm == NULL)
4106 09867e48 2019-08-13 stsp return NULL;
4107 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4108 09867e48 2019-08-13 stsp if (s == NULL)
4109 09867e48 2019-08-13 stsp return NULL;
4110 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4111 abd2672a 2018-12-23 stsp if (p)
4112 abd2672a 2018-12-23 stsp *p = '\0';
4113 abd2672a 2018-12-23 stsp return s;
4114 9f7d7167 2018-04-29 stsp }
4115 9f7d7167 2018-04-29 stsp
4116 4ed7e80c 2018-05-20 stsp static const struct got_error *
4117 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4118 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4119 0208f208 2020-05-05 stsp {
4120 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4121 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4122 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4123 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4124 0208f208 2020-05-05 stsp
4125 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4126 0208f208 2020-05-05 stsp if (qid != NULL) {
4127 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4128 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4129 ec242592 2022-04-22 thomas &qid->id);
4130 0208f208 2020-05-05 stsp if (err)
4131 0208f208 2020-05-05 stsp return err;
4132 0208f208 2020-05-05 stsp
4133 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4134 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4135 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4136 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4137 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4138 aa8b5dd0 2021-08-01 stsp }
4139 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4140 0208f208 2020-05-05 stsp
4141 0208f208 2020-05-05 stsp }
4142 0208f208 2020-05-05 stsp
4143 0208f208 2020-05-05 stsp if (tree_id1) {
4144 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4145 0208f208 2020-05-05 stsp if (err)
4146 0208f208 2020-05-05 stsp goto done;
4147 0208f208 2020-05-05 stsp }
4148 0208f208 2020-05-05 stsp
4149 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4150 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4151 0208f208 2020-05-05 stsp if (err)
4152 0208f208 2020-05-05 stsp goto done;
4153 0208f208 2020-05-05 stsp
4154 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4155 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4156 0208f208 2020-05-05 stsp done:
4157 0208f208 2020-05-05 stsp if (tree1)
4158 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4159 0208f208 2020-05-05 stsp if (tree2)
4160 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4161 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4162 0208f208 2020-05-05 stsp return err;
4163 0208f208 2020-05-05 stsp }
4164 0208f208 2020-05-05 stsp
4165 0208f208 2020-05-05 stsp static const struct got_error *
4166 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4167 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4168 abd2672a 2018-12-23 stsp {
4169 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4170 fe621944 2020-11-10 stsp
4171 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4172 fe621944 2020-11-10 stsp if (p == NULL)
4173 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4174 82c78e96 2022-08-06 thomas *lines = p;
4175 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4176 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4177 fe621944 2020-11-10 stsp (*nlines)++;
4178 82c78e96 2022-08-06 thomas
4179 fe621944 2020-11-10 stsp return NULL;
4180 fe621944 2020-11-10 stsp }
4181 fe621944 2020-11-10 stsp
4182 fe621944 2020-11-10 stsp static const struct got_error *
4183 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4184 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4185 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4186 fe621944 2020-11-10 stsp {
4187 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4188 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4189 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4190 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4191 45d799e2 2018-12-23 stsp time_t committer_time;
4192 45d799e2 2018-12-23 stsp const char *author, *committer;
4193 8b473291 2019-02-21 stsp char *refs_str = NULL;
4194 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4195 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4196 fe621944 2020-11-10 stsp off_t outoff = 0;
4197 fe621944 2020-11-10 stsp int n;
4198 abd2672a 2018-12-23 stsp
4199 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4200 0208f208 2020-05-05 stsp
4201 8b473291 2019-02-21 stsp if (refs) {
4202 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4203 8b473291 2019-02-21 stsp if (err)
4204 8b473291 2019-02-21 stsp return err;
4205 8b473291 2019-02-21 stsp }
4206 8b473291 2019-02-21 stsp
4207 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4208 abd2672a 2018-12-23 stsp if (err)
4209 abd2672a 2018-12-23 stsp return err;
4210 abd2672a 2018-12-23 stsp
4211 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4212 15a94983 2018-12-23 stsp if (err) {
4213 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4214 15a94983 2018-12-23 stsp goto done;
4215 15a94983 2018-12-23 stsp }
4216 abd2672a 2018-12-23 stsp
4217 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4218 fe621944 2020-11-10 stsp if (err)
4219 fe621944 2020-11-10 stsp goto done;
4220 fe621944 2020-11-10 stsp
4221 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4222 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4223 fe621944 2020-11-10 stsp if (n < 0) {
4224 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4225 abd2672a 2018-12-23 stsp goto done;
4226 abd2672a 2018-12-23 stsp }
4227 fe621944 2020-11-10 stsp outoff += n;
4228 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4229 fe621944 2020-11-10 stsp if (err)
4230 fe621944 2020-11-10 stsp goto done;
4231 fe621944 2020-11-10 stsp
4232 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4233 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4234 fe621944 2020-11-10 stsp if (n < 0) {
4235 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4236 abd2672a 2018-12-23 stsp goto done;
4237 abd2672a 2018-12-23 stsp }
4238 fe621944 2020-11-10 stsp outoff += n;
4239 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4240 fe621944 2020-11-10 stsp if (err)
4241 fe621944 2020-11-10 stsp goto done;
4242 fe621944 2020-11-10 stsp
4243 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4244 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4245 fe621944 2020-11-10 stsp if (datestr) {
4246 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4247 fe621944 2020-11-10 stsp if (n < 0) {
4248 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4249 fe621944 2020-11-10 stsp goto done;
4250 fe621944 2020-11-10 stsp }
4251 fe621944 2020-11-10 stsp outoff += n;
4252 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4253 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4254 fe621944 2020-11-10 stsp if (err)
4255 fe621944 2020-11-10 stsp goto done;
4256 abd2672a 2018-12-23 stsp }
4257 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4258 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4259 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4260 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4261 fe621944 2020-11-10 stsp if (n < 0) {
4262 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4263 fe621944 2020-11-10 stsp goto done;
4264 fe621944 2020-11-10 stsp }
4265 fe621944 2020-11-10 stsp outoff += n;
4266 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4267 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_AUTHOR);
4268 fe621944 2020-11-10 stsp if (err)
4269 fe621944 2020-11-10 stsp goto done;
4270 abd2672a 2018-12-23 stsp }
4271 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4272 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4273 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4274 ac4dc263 2021-09-24 thomas int pn = 1;
4275 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4276 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4277 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4278 ac4dc263 2021-09-24 thomas if (err)
4279 ac4dc263 2021-09-24 thomas goto done;
4280 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4281 ac4dc263 2021-09-24 thomas if (n < 0) {
4282 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4283 ac4dc263 2021-09-24 thomas goto done;
4284 ac4dc263 2021-09-24 thomas }
4285 ac4dc263 2021-09-24 thomas outoff += n;
4286 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4287 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4288 ac4dc263 2021-09-24 thomas if (err)
4289 ac4dc263 2021-09-24 thomas goto done;
4290 ac4dc263 2021-09-24 thomas free(id_str);
4291 ac4dc263 2021-09-24 thomas id_str = NULL;
4292 ac4dc263 2021-09-24 thomas }
4293 ac4dc263 2021-09-24 thomas }
4294 ac4dc263 2021-09-24 thomas
4295 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4296 5943eee2 2019-08-13 stsp if (err)
4297 5943eee2 2019-08-13 stsp goto done;
4298 fe621944 2020-11-10 stsp s = logmsg;
4299 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4300 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4301 fe621944 2020-11-10 stsp if (n < 0) {
4302 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4303 fe621944 2020-11-10 stsp goto done;
4304 fe621944 2020-11-10 stsp }
4305 fe621944 2020-11-10 stsp outoff += n;
4306 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4307 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4308 fe621944 2020-11-10 stsp if (err)
4309 fe621944 2020-11-10 stsp goto done;
4310 abd2672a 2018-12-23 stsp }
4311 fe621944 2020-11-10 stsp
4312 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4313 0208f208 2020-05-05 stsp if (err)
4314 0208f208 2020-05-05 stsp goto done;
4315 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4316 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4317 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4318 fe621944 2020-11-10 stsp if (n < 0) {
4319 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4320 fe621944 2020-11-10 stsp goto done;
4321 fe621944 2020-11-10 stsp }
4322 fe621944 2020-11-10 stsp outoff += n;
4323 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4324 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4325 fe621944 2020-11-10 stsp if (err)
4326 fe621944 2020-11-10 stsp goto done;
4327 0208f208 2020-05-05 stsp free((char *)pe->path);
4328 0208f208 2020-05-05 stsp free(pe->data);
4329 0208f208 2020-05-05 stsp }
4330 fe621944 2020-11-10 stsp
4331 0208f208 2020-05-05 stsp fputc('\n', outfile);
4332 fe621944 2020-11-10 stsp outoff++;
4333 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4334 abd2672a 2018-12-23 stsp done:
4335 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4336 abd2672a 2018-12-23 stsp free(id_str);
4337 5943eee2 2019-08-13 stsp free(logmsg);
4338 8b473291 2019-02-21 stsp free(refs_str);
4339 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4340 7510f233 2020-08-09 stsp if (err) {
4341 82c78e96 2022-08-06 thomas free(*lines);
4342 82c78e96 2022-08-06 thomas *lines = NULL;
4343 ae6a6978 2020-08-09 stsp *nlines = 0;
4344 7510f233 2020-08-09 stsp }
4345 fe621944 2020-11-10 stsp return err;
4346 abd2672a 2018-12-23 stsp }
4347 abd2672a 2018-12-23 stsp
4348 abd2672a 2018-12-23 stsp static const struct got_error *
4349 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4350 26ed57b2 2018-05-19 stsp {
4351 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4352 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4353 15a94983 2018-12-23 stsp int obj_type;
4354 fe621944 2020-11-10 stsp
4355 82c78e96 2022-08-06 thomas free(s->lines);
4356 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4357 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4358 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4359 fe621944 2020-11-10 stsp s->nlines = 0;
4360 26ed57b2 2018-05-19 stsp
4361 511a516b 2018-05-19 stsp f = got_opentemp();
4362 48ae06ee 2018-10-18 stsp if (f == NULL) {
4363 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4364 48ae06ee 2018-10-18 stsp goto done;
4365 48ae06ee 2018-10-18 stsp }
4366 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4367 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4368 fb43ecf1 2019-02-11 stsp goto done;
4369 fb43ecf1 2019-02-11 stsp }
4370 48ae06ee 2018-10-18 stsp s->f = f;
4371 26ed57b2 2018-05-19 stsp
4372 15a94983 2018-12-23 stsp if (s->id1)
4373 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4374 15a94983 2018-12-23 stsp else
4375 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4376 15a94983 2018-12-23 stsp if (err)
4377 15a94983 2018-12-23 stsp goto done;
4378 15a94983 2018-12-23 stsp
4379 15a94983 2018-12-23 stsp switch (obj_type) {
4380 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4381 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4382 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4383 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4384 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4385 26ed57b2 2018-05-19 stsp break;
4386 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4387 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4388 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4389 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4390 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4391 26ed57b2 2018-05-19 stsp break;
4392 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4393 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4394 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4395 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4396 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4397 abd2672a 2018-12-23 stsp
4398 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4399 abd2672a 2018-12-23 stsp if (err)
4400 3ffacbe1 2020-02-02 tracey goto done;
4401 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4402 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4403 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4404 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4405 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4406 f44b1f58 2020-02-02 tracey if (err)
4407 f44b1f58 2020-02-02 tracey goto done;
4408 f44b1f58 2020-02-02 tracey } else {
4409 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4410 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4411 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4412 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4413 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4414 82c78e96 2022-08-06 thomas s->f);
4415 f44b1f58 2020-02-02 tracey if (err)
4416 f44b1f58 2020-02-02 tracey goto done;
4417 f5404e4e 2020-02-02 tracey break;
4418 15a087fe 2019-02-21 stsp }
4419 abd2672a 2018-12-23 stsp }
4420 abd2672a 2018-12-23 stsp }
4421 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4422 abd2672a 2018-12-23 stsp
4423 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4424 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4425 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4426 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4427 26ed57b2 2018-05-19 stsp break;
4428 abd2672a 2018-12-23 stsp }
4429 26ed57b2 2018-05-19 stsp default:
4430 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4431 48ae06ee 2018-10-18 stsp break;
4432 26ed57b2 2018-05-19 stsp }
4433 48ae06ee 2018-10-18 stsp done:
4434 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4435 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4436 48ae06ee 2018-10-18 stsp return err;
4437 48ae06ee 2018-10-18 stsp }
4438 26ed57b2 2018-05-19 stsp
4439 f5215bb9 2019-02-22 stsp static void
4440 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4441 f5215bb9 2019-02-22 stsp {
4442 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4443 f5215bb9 2019-02-22 stsp update_panels();
4444 f5215bb9 2019-02-22 stsp doupdate();
4445 f44b1f58 2020-02-02 tracey }
4446 f44b1f58 2020-02-02 tracey
4447 f44b1f58 2020-02-02 tracey static const struct got_error *
4448 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4449 f44b1f58 2020-02-02 tracey {
4450 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4451 f44b1f58 2020-02-02 tracey
4452 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4453 f44b1f58 2020-02-02 tracey return NULL;
4454 f44b1f58 2020-02-02 tracey }
4455 f44b1f58 2020-02-02 tracey
4456 f44b1f58 2020-02-02 tracey static const struct got_error *
4457 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4458 f44b1f58 2020-02-02 tracey {
4459 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4460 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4461 f44b1f58 2020-02-02 tracey int lineno;
4462 78eecffc 2022-06-23 thomas char *line = NULL;
4463 826082fe 2020-12-10 stsp size_t linesize = 0;
4464 826082fe 2020-12-10 stsp ssize_t linelen;
4465 f44b1f58 2020-02-02 tracey
4466 f44b1f58 2020-02-02 tracey if (!view->searching) {
4467 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4468 f44b1f58 2020-02-02 tracey return NULL;
4469 f44b1f58 2020-02-02 tracey }
4470 f44b1f58 2020-02-02 tracey
4471 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4472 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4473 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4474 f44b1f58 2020-02-02 tracey else
4475 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4476 4b3f9dac 2021-12-17 thomas } else
4477 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4478 f44b1f58 2020-02-02 tracey
4479 f44b1f58 2020-02-02 tracey while (1) {
4480 f44b1f58 2020-02-02 tracey off_t offset;
4481 f44b1f58 2020-02-02 tracey
4482 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4483 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4484 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4485 f44b1f58 2020-02-02 tracey break;
4486 f44b1f58 2020-02-02 tracey }
4487 f44b1f58 2020-02-02 tracey
4488 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4489 f44b1f58 2020-02-02 tracey lineno = 1;
4490 f44b1f58 2020-02-02 tracey else
4491 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4492 f44b1f58 2020-02-02 tracey }
4493 f44b1f58 2020-02-02 tracey
4494 82c78e96 2022-08-06 thomas offset = s->lines[lineno - 1].offset;
4495 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4496 f44b1f58 2020-02-02 tracey free(line);
4497 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4498 f44b1f58 2020-02-02 tracey }
4499 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4500 78eecffc 2022-06-23 thomas if (linelen != -1) {
4501 78eecffc 2022-06-23 thomas char *exstr;
4502 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4503 78eecffc 2022-06-23 thomas if (err)
4504 78eecffc 2022-06-23 thomas break;
4505 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4506 78eecffc 2022-06-23 thomas &view->regmatch)) {
4507 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4508 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4509 78eecffc 2022-06-23 thomas free(exstr);
4510 78eecffc 2022-06-23 thomas break;
4511 78eecffc 2022-06-23 thomas }
4512 78eecffc 2022-06-23 thomas free(exstr);
4513 f44b1f58 2020-02-02 tracey }
4514 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4515 f44b1f58 2020-02-02 tracey lineno++;
4516 f44b1f58 2020-02-02 tracey else
4517 f44b1f58 2020-02-02 tracey lineno--;
4518 f44b1f58 2020-02-02 tracey }
4519 826082fe 2020-12-10 stsp free(line);
4520 f44b1f58 2020-02-02 tracey
4521 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4522 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4523 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4524 f44b1f58 2020-02-02 tracey }
4525 f44b1f58 2020-02-02 tracey
4526 05171be4 2022-06-23 thomas return err;
4527 f5215bb9 2019-02-22 stsp }
4528 f5215bb9 2019-02-22 stsp
4529 48ae06ee 2018-10-18 stsp static const struct got_error *
4530 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4531 a0f32f33 2022-06-13 thomas {
4532 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4533 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4534 a0f32f33 2022-06-13 thomas
4535 a0f32f33 2022-06-13 thomas free(s->id1);
4536 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4537 a0f32f33 2022-06-13 thomas free(s->id2);
4538 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4539 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4540 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4541 a0f32f33 2022-06-13 thomas s->f = NULL;
4542 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4543 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4544 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4545 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4546 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4547 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4548 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4549 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4550 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4551 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4552 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4553 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4554 82c78e96 2022-08-06 thomas free(s->lines);
4555 82c78e96 2022-08-06 thomas s->lines = NULL;
4556 a0f32f33 2022-06-13 thomas s->nlines = 0;
4557 a0f32f33 2022-06-13 thomas return err;
4558 a0f32f33 2022-06-13 thomas }
4559 a0f32f33 2022-06-13 thomas
4560 a0f32f33 2022-06-13 thomas static const struct got_error *
4561 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4562 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4563 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4564 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4565 48ae06ee 2018-10-18 stsp {
4566 48ae06ee 2018-10-18 stsp const struct got_error *err;
4567 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4568 5dc9f4bc 2018-08-04 stsp
4569 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4570 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4571 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4572 a0f32f33 2022-06-13 thomas
4573 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4574 15a94983 2018-12-23 stsp int type1, type2;
4575 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4576 15a94983 2018-12-23 stsp if (err)
4577 15a94983 2018-12-23 stsp return err;
4578 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4579 15a94983 2018-12-23 stsp if (err)
4580 15a94983 2018-12-23 stsp return err;
4581 15a94983 2018-12-23 stsp
4582 15a94983 2018-12-23 stsp if (type1 != type2)
4583 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4584 15a94983 2018-12-23 stsp }
4585 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4586 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4587 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4588 f44b1f58 2020-02-02 tracey s->repo = repo;
4589 f44b1f58 2020-02-02 tracey s->id1 = id1;
4590 f44b1f58 2020-02-02 tracey s->id2 = id2;
4591 3dbaef42 2020-11-24 stsp s->label1 = label1;
4592 3dbaef42 2020-11-24 stsp s->label2 = label2;
4593 48ae06ee 2018-10-18 stsp
4594 15a94983 2018-12-23 stsp if (id1) {
4595 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4596 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4597 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4598 48ae06ee 2018-10-18 stsp } else
4599 5465d566 2020-02-01 tracey s->id1 = NULL;
4600 48ae06ee 2018-10-18 stsp
4601 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4602 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4603 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4604 a0f32f33 2022-06-13 thomas goto done;
4605 48ae06ee 2018-10-18 stsp }
4606 a0f32f33 2022-06-13 thomas
4607 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4608 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4609 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4610 9a1d7435 2022-06-23 thomas goto done;
4611 9a1d7435 2022-06-23 thomas }
4612 9a1d7435 2022-06-23 thomas
4613 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4614 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4615 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4616 a0f32f33 2022-06-13 thomas goto done;
4617 a0f32f33 2022-06-13 thomas }
4618 a0f32f33 2022-06-13 thomas
4619 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4620 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4621 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4622 19a6a6b5 2022-07-01 thomas goto done;
4623 19a6a6b5 2022-07-01 thomas }
4624 19a6a6b5 2022-07-01 thomas
4625 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4626 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4627 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4628 19a6a6b5 2022-07-01 thomas goto done;
4629 19a6a6b5 2022-07-01 thomas }
4630 19a6a6b5 2022-07-01 thomas
4631 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4632 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4633 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4634 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4635 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4636 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4637 5465d566 2020-02-01 tracey s->repo = repo;
4638 6d17833f 2019-11-08 stsp
4639 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4640 6568f0aa 2022-08-06 thomas int rc;
4641 6d17833f 2019-11-08 stsp
4642 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
4643 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4644 6568f0aa 2022-08-06 thomas if (rc != ERR)
4645 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
4646 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4647 6568f0aa 2022-08-06 thomas if (rc != ERR)
4648 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
4649 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4650 6568f0aa 2022-08-06 thomas if (rc != ERR)
4651 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
4652 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4653 6568f0aa 2022-08-06 thomas if (rc != ERR)
4654 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
4655 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4656 6568f0aa 2022-08-06 thomas if (rc != ERR)
4657 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4658 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4659 6568f0aa 2022-08-06 thomas if (rc != ERR)
4660 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4661 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4662 6568f0aa 2022-08-06 thomas if (rc != ERR)
4663 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4664 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
4665 6568f0aa 2022-08-06 thomas if (rc != ERR)
4666 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
4667 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
4668 6568f0aa 2022-08-06 thomas if (rc == ERR) {
4669 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
4670 a0f32f33 2022-06-13 thomas goto done;
4671 6568f0aa 2022-08-06 thomas }
4672 6d17833f 2019-11-08 stsp }
4673 5dc9f4bc 2018-08-04 stsp
4674 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4675 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4676 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4677 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4678 f5215bb9 2019-02-22 stsp
4679 5465d566 2020-02-01 tracey err = create_diff(s);
4680 48ae06ee 2018-10-18 stsp
4681 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4682 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4683 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4684 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4685 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4686 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4687 a0f32f33 2022-06-13 thomas done:
4688 a0f32f33 2022-06-13 thomas if (err)
4689 a0f32f33 2022-06-13 thomas close_diff_view(view);
4690 e5a0f69f 2018-08-18 stsp return err;
4691 5dc9f4bc 2018-08-04 stsp }
4692 5dc9f4bc 2018-08-04 stsp
4693 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4694 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4695 5dc9f4bc 2018-08-04 stsp {
4696 a3404814 2018-09-02 stsp const struct got_error *err;
4697 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4698 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4699 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4700 a3404814 2018-09-02 stsp
4701 a3404814 2018-09-02 stsp if (s->id1) {
4702 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4703 a3404814 2018-09-02 stsp if (err)
4704 a3404814 2018-09-02 stsp return err;
4705 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
4706 3dbaef42 2020-11-24 stsp } else
4707 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4708 3dbaef42 2020-11-24 stsp
4709 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4710 a3404814 2018-09-02 stsp if (err)
4711 a3404814 2018-09-02 stsp return err;
4712 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
4713 26ed57b2 2018-05-19 stsp
4714 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4715 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4716 a3404814 2018-09-02 stsp free(id_str1);
4717 a3404814 2018-09-02 stsp free(id_str2);
4718 a3404814 2018-09-02 stsp return err;
4719 a3404814 2018-09-02 stsp }
4720 a3404814 2018-09-02 stsp free(id_str1);
4721 a3404814 2018-09-02 stsp free(id_str2);
4722 a3404814 2018-09-02 stsp
4723 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4724 267bb3b8 2021-08-01 stsp free(header);
4725 267bb3b8 2021-08-01 stsp return err;
4726 15a087fe 2019-02-21 stsp }
4727 15a087fe 2019-02-21 stsp
4728 15a087fe 2019-02-21 stsp static const struct got_error *
4729 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4730 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4731 15a087fe 2019-02-21 stsp {
4732 d7a04538 2019-02-21 stsp const struct got_error *err;
4733 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4734 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4735 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4736 15a087fe 2019-02-21 stsp
4737 15a087fe 2019-02-21 stsp free(s->id2);
4738 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4739 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4740 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4741 15a087fe 2019-02-21 stsp
4742 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4743 d7a04538 2019-02-21 stsp if (err)
4744 d7a04538 2019-02-21 stsp return err;
4745 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4746 15a087fe 2019-02-21 stsp free(s->id1);
4747 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4748 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4749 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4750 15a087fe 2019-02-21 stsp return NULL;
4751 0cf4efb1 2018-09-29 stsp }
4752 0cf4efb1 2018-09-29 stsp
4753 0cf4efb1 2018-09-29 stsp static const struct got_error *
4754 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4755 adf4c9e0 2022-07-03 thomas {
4756 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4757 adf4c9e0 2022-07-03 thomas
4758 adf4c9e0 2022-07-03 thomas view->count = 0;
4759 adf4c9e0 2022-07-03 thomas wclear(view->window);
4760 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4761 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4762 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4763 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4764 adf4c9e0 2022-07-03 thomas return create_diff(s);
4765 82c78e96 2022-08-06 thomas }
4766 82c78e96 2022-08-06 thomas
4767 82c78e96 2022-08-06 thomas static void
4768 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4769 82c78e96 2022-08-06 thomas {
4770 82c78e96 2022-08-06 thomas int start, i;
4771 82c78e96 2022-08-06 thomas
4772 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
4773 82c78e96 2022-08-06 thomas
4774 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4775 82c78e96 2022-08-06 thomas if (i == 0)
4776 82c78e96 2022-08-06 thomas i = s->nlines - 1;
4777 82c78e96 2022-08-06 thomas if (--i == start)
4778 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4779 82c78e96 2022-08-06 thomas }
4780 82c78e96 2022-08-06 thomas
4781 82c78e96 2022-08-06 thomas s->selected_line = 1;
4782 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4783 adf4c9e0 2022-07-03 thomas }
4784 adf4c9e0 2022-07-03 thomas
4785 82c78e96 2022-08-06 thomas static void
4786 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4787 82c78e96 2022-08-06 thomas {
4788 82c78e96 2022-08-06 thomas int start, i;
4789 82c78e96 2022-08-06 thomas
4790 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
4791 82c78e96 2022-08-06 thomas
4792 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4793 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
4794 82c78e96 2022-08-06 thomas i = 0;
4795 82c78e96 2022-08-06 thomas if (++i == start)
4796 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4797 82c78e96 2022-08-06 thomas }
4798 82c78e96 2022-08-06 thomas
4799 82c78e96 2022-08-06 thomas s->selected_line = 1;
4800 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4801 82c78e96 2022-08-06 thomas }
4802 82c78e96 2022-08-06 thomas
4803 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4804 4fc71f3b 2022-07-12 thomas int, int, int);
4805 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4806 4fc71f3b 2022-07-12 thomas int, int);
4807 4fc71f3b 2022-07-12 thomas
4808 adf4c9e0 2022-07-03 thomas static const struct got_error *
4809 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4810 e5a0f69f 2018-08-18 stsp {
4811 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4812 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4813 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4814 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4815 826082fe 2020-12-10 stsp char *line = NULL;
4816 826082fe 2020-12-10 stsp size_t linesize = 0;
4817 826082fe 2020-12-10 stsp ssize_t linelen;
4818 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4819 e5a0f69f 2018-08-18 stsp
4820 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
4821 82c78e96 2022-08-06 thomas
4822 e5a0f69f 2018-08-18 stsp switch (ch) {
4823 05171be4 2022-06-23 thomas case '0':
4824 05171be4 2022-06-23 thomas view->x = 0;
4825 05171be4 2022-06-23 thomas break;
4826 05171be4 2022-06-23 thomas case '$':
4827 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4828 07b0611c 2022-06-23 thomas view->count = 0;
4829 05171be4 2022-06-23 thomas break;
4830 05171be4 2022-06-23 thomas case KEY_RIGHT:
4831 05171be4 2022-06-23 thomas case 'l':
4832 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4833 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4834 07b0611c 2022-06-23 thomas else
4835 07b0611c 2022-06-23 thomas view->count = 0;
4836 05171be4 2022-06-23 thomas break;
4837 05171be4 2022-06-23 thomas case KEY_LEFT:
4838 05171be4 2022-06-23 thomas case 'h':
4839 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4840 07b0611c 2022-06-23 thomas if (view->x <= 0)
4841 07b0611c 2022-06-23 thomas view->count = 0;
4842 05171be4 2022-06-23 thomas break;
4843 64453f7e 2020-11-21 stsp case 'a':
4844 3dbaef42 2020-11-24 stsp case 'w':
4845 3dbaef42 2020-11-24 stsp if (ch == 'a')
4846 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4847 3dbaef42 2020-11-24 stsp if (ch == 'w')
4848 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4849 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4850 912a3f79 2021-08-30 j break;
4851 912a3f79 2021-08-30 j case 'g':
4852 912a3f79 2021-08-30 j case KEY_HOME:
4853 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4854 07b0611c 2022-06-23 thomas view->count = 0;
4855 64453f7e 2020-11-21 stsp break;
4856 912a3f79 2021-08-30 j case 'G':
4857 912a3f79 2021-08-30 j case KEY_END:
4858 07b0611c 2022-06-23 thomas view->count = 0;
4859 912a3f79 2021-08-30 j if (s->eof)
4860 912a3f79 2021-08-30 j break;
4861 912a3f79 2021-08-30 j
4862 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4863 912a3f79 2021-08-30 j s->eof = 1;
4864 912a3f79 2021-08-30 j break;
4865 1e37a5c2 2019-05-12 jcs case 'k':
4866 1e37a5c2 2019-05-12 jcs case KEY_UP:
4867 f7140bf5 2021-10-17 thomas case CTRL('p'):
4868 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4869 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4870 07b0611c 2022-06-23 thomas else
4871 07b0611c 2022-06-23 thomas view->count = 0;
4872 1e37a5c2 2019-05-12 jcs break;
4873 70f17a53 2022-06-13 thomas case CTRL('u'):
4874 23427b14 2022-06-23 thomas case 'u':
4875 70f17a53 2022-06-13 thomas nscroll /= 2;
4876 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4877 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4878 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4879 1c5e5faa 2022-06-23 thomas case 'b':
4880 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4881 07b0611c 2022-06-23 thomas view->count = 0;
4882 26ed57b2 2018-05-19 stsp break;
4883 07b0611c 2022-06-23 thomas }
4884 1e37a5c2 2019-05-12 jcs i = 0;
4885 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4886 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4887 1e37a5c2 2019-05-12 jcs break;
4888 1e37a5c2 2019-05-12 jcs case 'j':
4889 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4890 f7140bf5 2021-10-17 thomas case CTRL('n'):
4891 1e37a5c2 2019-05-12 jcs if (!s->eof)
4892 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4893 07b0611c 2022-06-23 thomas else
4894 07b0611c 2022-06-23 thomas view->count = 0;
4895 1e37a5c2 2019-05-12 jcs break;
4896 70f17a53 2022-06-13 thomas case CTRL('d'):
4897 23427b14 2022-06-23 thomas case 'd':
4898 70f17a53 2022-06-13 thomas nscroll /= 2;
4899 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4900 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4901 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4902 1c5e5faa 2022-06-23 thomas case 'f':
4903 1e37a5c2 2019-05-12 jcs case ' ':
4904 07b0611c 2022-06-23 thomas if (s->eof) {
4905 07b0611c 2022-06-23 thomas view->count = 0;
4906 1e37a5c2 2019-05-12 jcs break;
4907 07b0611c 2022-06-23 thomas }
4908 1e37a5c2 2019-05-12 jcs i = 0;
4909 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4910 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4911 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4912 826082fe 2020-12-10 stsp if (linelen == -1) {
4913 826082fe 2020-12-10 stsp if (feof(s->f)) {
4914 826082fe 2020-12-10 stsp s->eof = 1;
4915 826082fe 2020-12-10 stsp } else
4916 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4917 34bc9ec9 2019-02-22 stsp break;
4918 826082fe 2020-12-10 stsp }
4919 1e37a5c2 2019-05-12 jcs }
4920 826082fe 2020-12-10 stsp free(line);
4921 1e37a5c2 2019-05-12 jcs break;
4922 82c78e96 2022-08-06 thomas case '(':
4923 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
4924 82c78e96 2022-08-06 thomas break;
4925 82c78e96 2022-08-06 thomas case ')':
4926 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
4927 82c78e96 2022-08-06 thomas break;
4928 82c78e96 2022-08-06 thomas case '{':
4929 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
4930 82c78e96 2022-08-06 thomas break;
4931 82c78e96 2022-08-06 thomas case '}':
4932 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
4933 82c78e96 2022-08-06 thomas break;
4934 1e37a5c2 2019-05-12 jcs case '[':
4935 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4936 1e37a5c2 2019-05-12 jcs s->diff_context--;
4937 aa61903a 2021-12-31 thomas s->matched_line = 0;
4938 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4939 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4940 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4941 27829c9e 2020-11-21 stsp s->nlines) {
4942 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4943 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4944 27829c9e 2020-11-21 stsp }
4945 07b0611c 2022-06-23 thomas } else
4946 07b0611c 2022-06-23 thomas view->count = 0;
4947 1e37a5c2 2019-05-12 jcs break;
4948 1e37a5c2 2019-05-12 jcs case ']':
4949 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4950 1e37a5c2 2019-05-12 jcs s->diff_context++;
4951 aa61903a 2021-12-31 thomas s->matched_line = 0;
4952 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4953 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4954 07b0611c 2022-06-23 thomas } else
4955 07b0611c 2022-06-23 thomas view->count = 0;
4956 1e37a5c2 2019-05-12 jcs break;
4957 1e37a5c2 2019-05-12 jcs case '<':
4958 1e37a5c2 2019-05-12 jcs case ',':
4959 777aae21 2022-07-20 thomas case 'K':
4960 4fc71f3b 2022-07-12 thomas up = 1;
4961 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4962 4fc71f3b 2022-07-12 thomas case '>':
4963 4fc71f3b 2022-07-12 thomas case '.':
4964 777aae21 2022-07-20 thomas case 'J':
4965 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4966 07b0611c 2022-06-23 thomas view->count = 0;
4967 48ae06ee 2018-10-18 stsp break;
4968 07b0611c 2022-06-23 thomas }
4969 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4970 6524637e 2019-02-21 stsp
4971 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4972 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4973 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4974 15a087fe 2019-02-21 stsp
4975 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4976 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4977 4fc71f3b 2022-07-12 thomas if (err)
4978 4fc71f3b 2022-07-12 thomas break;
4979 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4980 15a087fe 2019-02-21 stsp
4981 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4982 4fc71f3b 2022-07-12 thomas break;
4983 15a087fe 2019-02-21 stsp
4984 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4985 4fc71f3b 2022-07-12 thomas if (err)
4986 4fc71f3b 2022-07-12 thomas break;
4987 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4988 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4989 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4990 5e224a3e 2019-02-22 stsp
4991 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4992 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4993 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4994 4fc71f3b 2022-07-12 thomas
4995 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4996 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4997 4fc71f3b 2022-07-12 thomas if (err)
4998 4fc71f3b 2022-07-12 thomas break;
4999 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5000 5a8b5076 2020-12-05 stsp
5001 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5002 4fc71f3b 2022-07-12 thomas break;
5003 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5004 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5005 4fc71f3b 2022-07-12 thomas bs->selected_line);
5006 4fc71f3b 2022-07-12 thomas if (id == NULL)
5007 4fc71f3b 2022-07-12 thomas break;
5008 15a087fe 2019-02-21 stsp
5009 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5010 4fc71f3b 2022-07-12 thomas break;
5011 15a087fe 2019-02-21 stsp
5012 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5013 4fc71f3b 2022-07-12 thomas if (err)
5014 4fc71f3b 2022-07-12 thomas break;
5015 4fc71f3b 2022-07-12 thomas }
5016 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5017 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5018 aa61903a 2021-12-31 thomas s->matched_line = 0;
5019 05171be4 2022-06-23 thomas view->x = 0;
5020 1e37a5c2 2019-05-12 jcs
5021 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5022 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5023 1e37a5c2 2019-05-12 jcs break;
5024 1e37a5c2 2019-05-12 jcs default:
5025 07b0611c 2022-06-23 thomas view->count = 0;
5026 1e37a5c2 2019-05-12 jcs break;
5027 26ed57b2 2018-05-19 stsp }
5028 e5a0f69f 2018-08-18 stsp
5029 bcbd79e2 2018-08-19 stsp return err;
5030 26ed57b2 2018-05-19 stsp }
5031 26ed57b2 2018-05-19 stsp
5032 4ed7e80c 2018-05-20 stsp static const struct got_error *
5033 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5034 9f7d7167 2018-04-29 stsp {
5035 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5036 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5037 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5038 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5039 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5040 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5041 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5042 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5043 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5044 3dbaef42 2020-11-24 stsp const char *errstr;
5045 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5046 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5047 70ac5f84 2019-03-28 stsp
5048 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5049 26ed57b2 2018-05-19 stsp switch (ch) {
5050 64453f7e 2020-11-21 stsp case 'a':
5051 64453f7e 2020-11-21 stsp force_text_diff = 1;
5052 3dbaef42 2020-11-24 stsp break;
5053 3dbaef42 2020-11-24 stsp case 'C':
5054 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5055 3dbaef42 2020-11-24 stsp &errstr);
5056 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5057 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5058 8f666e67 2022-02-12 thomas errstr, errstr);
5059 64453f7e 2020-11-21 stsp break;
5060 09b5bff8 2020-02-23 naddy case 'r':
5061 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5062 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5063 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5064 09b5bff8 2020-02-23 naddy optarg);
5065 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5066 09b5bff8 2020-02-23 naddy break;
5067 3dbaef42 2020-11-24 stsp case 'w':
5068 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5069 3dbaef42 2020-11-24 stsp break;
5070 26ed57b2 2018-05-19 stsp default:
5071 17020d27 2019-03-07 stsp usage_diff();
5072 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5073 26ed57b2 2018-05-19 stsp }
5074 26ed57b2 2018-05-19 stsp }
5075 26ed57b2 2018-05-19 stsp
5076 26ed57b2 2018-05-19 stsp argc -= optind;
5077 26ed57b2 2018-05-19 stsp argv += optind;
5078 26ed57b2 2018-05-19 stsp
5079 26ed57b2 2018-05-19 stsp if (argc == 0) {
5080 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5081 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5082 15a94983 2018-12-23 stsp id_str1 = argv[0];
5083 15a94983 2018-12-23 stsp id_str2 = argv[1];
5084 26ed57b2 2018-05-19 stsp } else
5085 26ed57b2 2018-05-19 stsp usage_diff();
5086 eb6600df 2019-01-04 stsp
5087 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5088 7cd52833 2022-06-23 thomas if (error)
5089 7cd52833 2022-06-23 thomas goto done;
5090 7cd52833 2022-06-23 thomas
5091 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5092 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5093 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5094 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5095 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5096 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5097 c156c7a4 2020-12-18 stsp goto done;
5098 a273ac94 2020-02-23 naddy if (worktree)
5099 a273ac94 2020-02-23 naddy repo_path =
5100 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5101 a273ac94 2020-02-23 naddy else
5102 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5103 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5104 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5105 c156c7a4 2020-12-18 stsp goto done;
5106 c156c7a4 2020-12-18 stsp }
5107 a273ac94 2020-02-23 naddy }
5108 a273ac94 2020-02-23 naddy
5109 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5110 eb6600df 2019-01-04 stsp if (error)
5111 eb6600df 2019-01-04 stsp goto done;
5112 26ed57b2 2018-05-19 stsp
5113 a273ac94 2020-02-23 naddy init_curses();
5114 a273ac94 2020-02-23 naddy
5115 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5116 51a10b52 2020-12-26 stsp if (error)
5117 51a10b52 2020-12-26 stsp goto done;
5118 51a10b52 2020-12-26 stsp
5119 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5120 26ed57b2 2018-05-19 stsp if (error)
5121 26ed57b2 2018-05-19 stsp goto done;
5122 26ed57b2 2018-05-19 stsp
5123 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5124 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5125 26ed57b2 2018-05-19 stsp if (error)
5126 26ed57b2 2018-05-19 stsp goto done;
5127 26ed57b2 2018-05-19 stsp
5128 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5129 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5130 26ed57b2 2018-05-19 stsp if (error)
5131 26ed57b2 2018-05-19 stsp goto done;
5132 26ed57b2 2018-05-19 stsp
5133 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5134 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5135 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5136 ea5e7bb5 2018-08-01 stsp goto done;
5137 ea5e7bb5 2018-08-01 stsp }
5138 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5139 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5140 5dc9f4bc 2018-08-04 stsp if (error)
5141 5dc9f4bc 2018-08-04 stsp goto done;
5142 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5143 26ed57b2 2018-05-19 stsp done:
5144 3dbaef42 2020-11-24 stsp free(label1);
5145 3dbaef42 2020-11-24 stsp free(label2);
5146 c02c541e 2019-03-29 stsp free(repo_path);
5147 a273ac94 2020-02-23 naddy free(cwd);
5148 1d0f4054 2021-06-17 stsp if (repo) {
5149 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5150 1d0f4054 2021-06-17 stsp if (error == NULL)
5151 1d0f4054 2021-06-17 stsp error = close_err;
5152 1d0f4054 2021-06-17 stsp }
5153 a273ac94 2020-02-23 naddy if (worktree)
5154 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5155 7cd52833 2022-06-23 thomas if (pack_fds) {
5156 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5157 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5158 7cd52833 2022-06-23 thomas if (error == NULL)
5159 7cd52833 2022-06-23 thomas error = pack_err;
5160 7cd52833 2022-06-23 thomas }
5161 51a10b52 2020-12-26 stsp tog_free_refs();
5162 26ed57b2 2018-05-19 stsp return error;
5163 9f7d7167 2018-04-29 stsp }
5164 9f7d7167 2018-04-29 stsp
5165 4ed7e80c 2018-05-20 stsp __dead static void
5166 9f7d7167 2018-04-29 stsp usage_blame(void)
5167 9f7d7167 2018-04-29 stsp {
5168 80ddbec8 2018-04-29 stsp endwin();
5169 91198554 2022-06-23 thomas fprintf(stderr,
5170 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5171 9f7d7167 2018-04-29 stsp getprogname());
5172 9f7d7167 2018-04-29 stsp exit(1);
5173 9f7d7167 2018-04-29 stsp }
5174 84451b3e 2018-07-10 stsp
5175 84451b3e 2018-07-10 stsp struct tog_blame_line {
5176 84451b3e 2018-07-10 stsp int annotated;
5177 84451b3e 2018-07-10 stsp struct got_object_id *id;
5178 84451b3e 2018-07-10 stsp };
5179 9f7d7167 2018-04-29 stsp
5180 4ed7e80c 2018-05-20 stsp static const struct got_error *
5181 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5182 84451b3e 2018-07-10 stsp {
5183 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5184 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5185 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5186 84451b3e 2018-07-10 stsp const struct got_error *err;
5187 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5188 826082fe 2020-12-10 stsp char *line = NULL;
5189 826082fe 2020-12-10 stsp size_t linesize = 0;
5190 826082fe 2020-12-10 stsp ssize_t linelen;
5191 84451b3e 2018-07-10 stsp wchar_t *wline;
5192 27a741e5 2019-09-11 stsp int width;
5193 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5194 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5195 ab089a2a 2018-07-12 stsp char *id_str;
5196 11b20872 2019-11-08 stsp struct tog_color *tc;
5197 ab089a2a 2018-07-12 stsp
5198 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5199 ab089a2a 2018-07-12 stsp if (err)
5200 ab089a2a 2018-07-12 stsp return err;
5201 84451b3e 2018-07-10 stsp
5202 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5203 f7d12f7e 2018-08-01 stsp werase(view->window);
5204 84451b3e 2018-07-10 stsp
5205 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5206 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5207 ab089a2a 2018-07-12 stsp free(id_str);
5208 ab089a2a 2018-07-12 stsp return err;
5209 ab089a2a 2018-07-12 stsp }
5210 ab089a2a 2018-07-12 stsp
5211 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5212 ab089a2a 2018-07-12 stsp free(line);
5213 2550e4c3 2018-07-13 stsp line = NULL;
5214 1cae65b4 2019-09-22 stsp if (err)
5215 1cae65b4 2019-09-22 stsp return err;
5216 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5217 a3404814 2018-09-02 stsp wstandout(view->window);
5218 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5219 11b20872 2019-11-08 stsp if (tc)
5220 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5221 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5222 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5223 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5224 11b20872 2019-11-08 stsp if (tc)
5225 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5226 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5227 a3404814 2018-09-02 stsp wstandend(view->window);
5228 2550e4c3 2018-07-13 stsp free(wline);
5229 2550e4c3 2018-07-13 stsp wline = NULL;
5230 ab089a2a 2018-07-12 stsp
5231 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5232 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5233 07dd3ed3 2022-08-06 thomas
5234 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5235 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5236 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5237 ab089a2a 2018-07-12 stsp free(id_str);
5238 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5239 ab089a2a 2018-07-12 stsp }
5240 ab089a2a 2018-07-12 stsp free(id_str);
5241 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5242 3f60a8ef 2018-07-10 stsp free(line);
5243 2550e4c3 2018-07-13 stsp line = NULL;
5244 3f60a8ef 2018-07-10 stsp if (err)
5245 3f60a8ef 2018-07-10 stsp return err;
5246 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5247 2550e4c3 2018-07-13 stsp free(wline);
5248 2550e4c3 2018-07-13 stsp wline = NULL;
5249 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5250 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5251 3f60a8ef 2018-07-10 stsp
5252 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5253 05171be4 2022-06-23 thomas view->maxx = 0;
5254 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5255 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5256 826082fe 2020-12-10 stsp if (linelen == -1) {
5257 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5258 826082fe 2020-12-10 stsp s->eof = 1;
5259 826082fe 2020-12-10 stsp break;
5260 826082fe 2020-12-10 stsp }
5261 84451b3e 2018-07-10 stsp free(line);
5262 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5263 84451b3e 2018-07-10 stsp }
5264 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5265 07dd3ed3 2022-08-06 thomas continue;
5266 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5267 826082fe 2020-12-10 stsp continue;
5268 cbea3800 2022-06-23 thomas
5269 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5270 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5271 cbea3800 2022-06-23 thomas if (err) {
5272 cbea3800 2022-06-23 thomas free(line);
5273 cbea3800 2022-06-23 thomas return err;
5274 cbea3800 2022-06-23 thomas }
5275 cbea3800 2022-06-23 thomas free(wline);
5276 cbea3800 2022-06-23 thomas wline = NULL;
5277 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5278 84451b3e 2018-07-10 stsp
5279 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5280 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5281 b700b5d6 2018-07-10 stsp
5282 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5283 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5284 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5285 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5286 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5287 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5288 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5289 8d0fe45a 2019-08-12 stsp char *id_str;
5290 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5291 91198554 2022-06-23 thomas blame_line->id);
5292 8d0fe45a 2019-08-12 stsp if (err) {
5293 8d0fe45a 2019-08-12 stsp free(line);
5294 8d0fe45a 2019-08-12 stsp return err;
5295 8d0fe45a 2019-08-12 stsp }
5296 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5297 11b20872 2019-11-08 stsp if (tc)
5298 11b20872 2019-11-08 stsp wattr_on(view->window,
5299 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5300 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5301 11b20872 2019-11-08 stsp if (tc)
5302 11b20872 2019-11-08 stsp wattr_off(view->window,
5303 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5304 8d0fe45a 2019-08-12 stsp free(id_str);
5305 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5306 8d0fe45a 2019-08-12 stsp } else {
5307 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5308 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5309 84451b3e 2018-07-10 stsp }
5310 ee41ec32 2018-07-10 stsp } else {
5311 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5312 ee41ec32 2018-07-10 stsp prev_id = NULL;
5313 ee41ec32 2018-07-10 stsp }
5314 84451b3e 2018-07-10 stsp
5315 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5316 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5317 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5318 27a741e5 2019-09-11 stsp
5319 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5320 41605754 2020-11-12 stsp width = 9;
5321 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5322 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5323 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5324 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5325 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5326 41605754 2020-11-12 stsp if (err) {
5327 41605754 2020-11-12 stsp free(line);
5328 41605754 2020-11-12 stsp return err;
5329 41605754 2020-11-12 stsp }
5330 41605754 2020-11-12 stsp width += 9;
5331 41605754 2020-11-12 stsp } else {
5332 923086fd 2022-06-23 thomas int skip;
5333 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5334 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5335 923086fd 2022-06-23 thomas if (err) {
5336 923086fd 2022-06-23 thomas free(line);
5337 923086fd 2022-06-23 thomas return err;
5338 05171be4 2022-06-23 thomas }
5339 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5340 02bce7e2 2022-06-23 thomas width += 9;
5341 41605754 2020-11-12 stsp free(wline);
5342 41605754 2020-11-12 stsp wline = NULL;
5343 41605754 2020-11-12 stsp }
5344 41605754 2020-11-12 stsp
5345 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5346 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5347 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5348 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5349 84451b3e 2018-07-10 stsp }
5350 826082fe 2020-12-10 stsp free(line);
5351 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5352 84451b3e 2018-07-10 stsp
5353 a5d43cac 2022-07-01 thomas view_border(view);
5354 84451b3e 2018-07-10 stsp
5355 84451b3e 2018-07-10 stsp return NULL;
5356 84451b3e 2018-07-10 stsp }
5357 84451b3e 2018-07-10 stsp
5358 84451b3e 2018-07-10 stsp static const struct got_error *
5359 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5360 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5361 84451b3e 2018-07-10 stsp {
5362 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5363 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5364 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5365 1a76625f 2018-10-22 stsp int errcode;
5366 84451b3e 2018-07-10 stsp
5367 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5368 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5369 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5370 84451b3e 2018-07-10 stsp
5371 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5372 1a76625f 2018-10-22 stsp if (errcode)
5373 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5374 84451b3e 2018-07-10 stsp
5375 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5376 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5377 d68a0a7d 2018-07-10 stsp goto done;
5378 d68a0a7d 2018-07-10 stsp }
5379 d68a0a7d 2018-07-10 stsp
5380 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5381 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5382 d68a0a7d 2018-07-10 stsp
5383 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5384 d68a0a7d 2018-07-10 stsp if (line->annotated)
5385 d68a0a7d 2018-07-10 stsp goto done;
5386 d68a0a7d 2018-07-10 stsp
5387 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5388 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5389 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5390 84451b3e 2018-07-10 stsp goto done;
5391 84451b3e 2018-07-10 stsp }
5392 84451b3e 2018-07-10 stsp line->annotated = 1;
5393 84451b3e 2018-07-10 stsp done:
5394 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5395 1a76625f 2018-10-22 stsp if (errcode)
5396 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5397 84451b3e 2018-07-10 stsp return err;
5398 84451b3e 2018-07-10 stsp }
5399 84451b3e 2018-07-10 stsp
5400 84451b3e 2018-07-10 stsp static void *
5401 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5402 84451b3e 2018-07-10 stsp {
5403 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5404 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5405 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5406 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5407 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5408 00a8878e 2022-07-01 thomas
5409 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5410 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5411 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5412 9117a7b7 2022-07-01 thomas
5413 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5414 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5415 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5416 9117a7b7 2022-07-01 thomas goto done;
5417 9117a7b7 2022-07-01 thomas }
5418 18430de3 2018-07-10 stsp
5419 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5420 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5421 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5422 9117a7b7 2022-07-01 thomas goto done;
5423 9117a7b7 2022-07-01 thomas }
5424 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5425 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5426 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5427 9117a7b7 2022-07-01 thomas goto done;
5428 9117a7b7 2022-07-01 thomas }
5429 9117a7b7 2022-07-01 thomas
5430 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5431 61266923 2020-01-14 stsp if (err)
5432 9117a7b7 2022-07-01 thomas goto done;
5433 61266923 2020-01-14 stsp
5434 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5435 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5436 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5437 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5438 fc06ba56 2019-08-22 stsp err = NULL;
5439 18430de3 2018-07-10 stsp
5440 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5441 9117a7b7 2022-07-01 thomas if (errcode) {
5442 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5443 9117a7b7 2022-07-01 thomas goto done;
5444 9117a7b7 2022-07-01 thomas }
5445 18430de3 2018-07-10 stsp
5446 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5447 1d0f4054 2021-06-17 stsp if (err == NULL)
5448 1d0f4054 2021-06-17 stsp err = close_err;
5449 c9beca56 2018-07-22 stsp ta->repo = NULL;
5450 c9beca56 2018-07-22 stsp *ta->complete = 1;
5451 18430de3 2018-07-10 stsp
5452 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5453 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5454 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5455 18430de3 2018-07-10 stsp
5456 9117a7b7 2022-07-01 thomas done:
5457 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5458 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5459 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5460 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5461 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5462 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5463 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5464 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5465 00a8878e 2022-07-01 thomas
5466 18430de3 2018-07-10 stsp return (void *)err;
5467 84451b3e 2018-07-10 stsp }
5468 84451b3e 2018-07-10 stsp
5469 245d91c1 2018-07-12 stsp static struct got_object_id *
5470 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5471 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5472 245d91c1 2018-07-12 stsp {
5473 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5474 8d0fe45a 2019-08-12 stsp
5475 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5476 8d0fe45a 2019-08-12 stsp return NULL;
5477 b880a918 2018-07-10 stsp
5478 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5479 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5480 4fc71f3b 2022-07-12 thomas return NULL;
5481 4fc71f3b 2022-07-12 thomas
5482 4fc71f3b 2022-07-12 thomas return line->id;
5483 4fc71f3b 2022-07-12 thomas }
5484 4fc71f3b 2022-07-12 thomas
5485 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5486 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5487 4fc71f3b 2022-07-12 thomas int lineno)
5488 4fc71f3b 2022-07-12 thomas {
5489 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5490 4fc71f3b 2022-07-12 thomas
5491 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5492 4fc71f3b 2022-07-12 thomas return NULL;
5493 4fc71f3b 2022-07-12 thomas
5494 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5495 245d91c1 2018-07-12 stsp if (!line->annotated)
5496 245d91c1 2018-07-12 stsp return NULL;
5497 245d91c1 2018-07-12 stsp
5498 245d91c1 2018-07-12 stsp return line->id;
5499 b880a918 2018-07-10 stsp }
5500 245d91c1 2018-07-12 stsp
5501 b880a918 2018-07-10 stsp static const struct got_error *
5502 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5503 a70480e0 2018-06-23 stsp {
5504 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5505 245d91c1 2018-07-12 stsp int i;
5506 245d91c1 2018-07-12 stsp
5507 245d91c1 2018-07-12 stsp if (blame->thread) {
5508 1a76625f 2018-10-22 stsp int errcode;
5509 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5510 1a76625f 2018-10-22 stsp if (errcode)
5511 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5512 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5513 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5514 1a76625f 2018-10-22 stsp if (errcode)
5515 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5516 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5517 1a76625f 2018-10-22 stsp if (errcode)
5518 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5519 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5520 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5521 245d91c1 2018-07-12 stsp err = NULL;
5522 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5523 245d91c1 2018-07-12 stsp }
5524 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5525 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5526 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5527 1d0f4054 2021-06-17 stsp if (err == NULL)
5528 1d0f4054 2021-06-17 stsp err = close_err;
5529 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5530 245d91c1 2018-07-12 stsp }
5531 245d91c1 2018-07-12 stsp if (blame->f) {
5532 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5533 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5534 245d91c1 2018-07-12 stsp blame->f = NULL;
5535 245d91c1 2018-07-12 stsp }
5536 57670559 2018-12-23 stsp if (blame->lines) {
5537 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5538 57670559 2018-12-23 stsp free(blame->lines[i].id);
5539 57670559 2018-12-23 stsp free(blame->lines);
5540 57670559 2018-12-23 stsp blame->lines = NULL;
5541 57670559 2018-12-23 stsp }
5542 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5543 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5544 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5545 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5546 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5547 7cd52833 2022-06-23 thomas if (err == NULL)
5548 7cd52833 2022-06-23 thomas err = pack_err;
5549 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5550 7cd52833 2022-06-23 thomas }
5551 245d91c1 2018-07-12 stsp return err;
5552 245d91c1 2018-07-12 stsp }
5553 245d91c1 2018-07-12 stsp
5554 245d91c1 2018-07-12 stsp static const struct got_error *
5555 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5556 fc06ba56 2019-08-22 stsp {
5557 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5558 fc06ba56 2019-08-22 stsp int *done = arg;
5559 fc06ba56 2019-08-22 stsp int errcode;
5560 fc06ba56 2019-08-22 stsp
5561 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5562 fc06ba56 2019-08-22 stsp if (errcode)
5563 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5564 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5565 fc06ba56 2019-08-22 stsp
5566 fc06ba56 2019-08-22 stsp if (*done)
5567 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5568 fc06ba56 2019-08-22 stsp
5569 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5570 fc06ba56 2019-08-22 stsp if (errcode)
5571 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5572 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5573 fc06ba56 2019-08-22 stsp
5574 fc06ba56 2019-08-22 stsp return err;
5575 fc06ba56 2019-08-22 stsp }
5576 fc06ba56 2019-08-22 stsp
5577 fc06ba56 2019-08-22 stsp static const struct got_error *
5578 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5579 245d91c1 2018-07-12 stsp {
5580 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5581 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5582 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5583 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5584 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5585 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5586 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5587 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5588 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5589 a70480e0 2018-06-23 stsp
5590 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5591 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5592 27d434c2 2018-09-15 stsp if (err)
5593 15a94983 2018-12-23 stsp return err;
5594 f4ae6ddb 2022-07-01 thomas
5595 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5596 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5597 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5598 f4ae6ddb 2022-07-01 thomas goto done;
5599 f4ae6ddb 2022-07-01 thomas }
5600 945f9229 2022-04-16 thomas
5601 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5602 945f9229 2022-04-16 thomas if (err)
5603 945f9229 2022-04-16 thomas goto done;
5604 27d434c2 2018-09-15 stsp
5605 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5606 84451b3e 2018-07-10 stsp if (err)
5607 84451b3e 2018-07-10 stsp goto done;
5608 27d434c2 2018-09-15 stsp
5609 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5610 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5611 84451b3e 2018-07-10 stsp goto done;
5612 84451b3e 2018-07-10 stsp }
5613 a70480e0 2018-06-23 stsp
5614 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5615 a70480e0 2018-06-23 stsp if (err)
5616 a70480e0 2018-06-23 stsp goto done;
5617 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5618 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5619 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5620 84451b3e 2018-07-10 stsp goto done;
5621 84451b3e 2018-07-10 stsp }
5622 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5623 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5624 1fddf795 2021-01-20 stsp if (err)
5625 1fddf795 2021-01-20 stsp goto done;
5626 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5627 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5628 84451b3e 2018-07-10 stsp goto done;
5629 1fddf795 2021-01-20 stsp }
5630 b02560ec 2019-08-19 stsp
5631 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5632 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5633 b02560ec 2019-08-19 stsp blame->nlines--;
5634 a70480e0 2018-06-23 stsp
5635 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5636 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5637 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5638 84451b3e 2018-07-10 stsp goto done;
5639 84451b3e 2018-07-10 stsp }
5640 a70480e0 2018-06-23 stsp
5641 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5642 bd24772e 2018-07-11 stsp if (err)
5643 bd24772e 2018-07-11 stsp goto done;
5644 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5645 7cd52833 2022-06-23 thomas pack_fds);
5646 7cd52833 2022-06-23 thomas if (err)
5647 7cd52833 2022-06-23 thomas goto done;
5648 bd24772e 2018-07-11 stsp
5649 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5650 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5651 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5652 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5653 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5654 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5655 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5656 245d91c1 2018-07-12 stsp goto done;
5657 245d91c1 2018-07-12 stsp }
5658 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5659 245d91c1 2018-07-12 stsp
5660 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5661 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5662 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5663 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5664 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5665 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5666 a5388363 2020-12-01 naddy s->blame_complete = 0;
5667 f5a09613 2020-12-13 naddy
5668 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5669 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5670 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5671 f5a09613 2020-12-13 naddy s->selected_line = 1;
5672 f5a09613 2020-12-13 naddy }
5673 aa61903a 2021-12-31 thomas s->matched_line = 0;
5674 245d91c1 2018-07-12 stsp
5675 245d91c1 2018-07-12 stsp done:
5676 945f9229 2022-04-16 thomas if (commit)
5677 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5678 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5679 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5680 245d91c1 2018-07-12 stsp if (blob)
5681 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5682 27d434c2 2018-09-15 stsp free(obj_id);
5683 245d91c1 2018-07-12 stsp if (err)
5684 245d91c1 2018-07-12 stsp stop_blame(blame);
5685 245d91c1 2018-07-12 stsp return err;
5686 245d91c1 2018-07-12 stsp }
5687 245d91c1 2018-07-12 stsp
5688 245d91c1 2018-07-12 stsp static const struct got_error *
5689 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5690 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5691 245d91c1 2018-07-12 stsp {
5692 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5693 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5694 dbc6a6b6 2018-07-12 stsp
5695 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5696 245d91c1 2018-07-12 stsp
5697 c4843652 2019-08-12 stsp s->path = strdup(path);
5698 c4843652 2019-08-12 stsp if (s->path == NULL)
5699 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5700 c4843652 2019-08-12 stsp
5701 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5702 c4843652 2019-08-12 stsp if (err) {
5703 c4843652 2019-08-12 stsp free(s->path);
5704 7cbe629d 2018-08-04 stsp return err;
5705 c4843652 2019-08-12 stsp }
5706 245d91c1 2018-07-12 stsp
5707 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5708 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5709 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5710 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5711 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5712 fb2756b9 2018-08-04 stsp s->repo = repo;
5713 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5714 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5715 7cbe629d 2018-08-04 stsp
5716 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5717 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5718 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5719 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5720 11b20872 2019-11-08 stsp if (err)
5721 11b20872 2019-11-08 stsp return err;
5722 11b20872 2019-11-08 stsp }
5723 11b20872 2019-11-08 stsp
5724 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5725 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5726 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5727 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5728 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5729 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5730 e5a0f69f 2018-08-18 stsp
5731 a5388363 2020-12-01 naddy return run_blame(view);
5732 7cbe629d 2018-08-04 stsp }
5733 7cbe629d 2018-08-04 stsp
5734 e5a0f69f 2018-08-18 stsp static const struct got_error *
5735 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5736 7cbe629d 2018-08-04 stsp {
5737 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5738 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5739 7cbe629d 2018-08-04 stsp
5740 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5741 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5742 e5a0f69f 2018-08-18 stsp
5743 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5744 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5745 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5746 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5747 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5748 7cbe629d 2018-08-04 stsp }
5749 e5a0f69f 2018-08-18 stsp
5750 e5a0f69f 2018-08-18 stsp free(s->path);
5751 11b20872 2019-11-08 stsp free_colors(&s->colors);
5752 e5a0f69f 2018-08-18 stsp return err;
5753 7cbe629d 2018-08-04 stsp }
5754 7cbe629d 2018-08-04 stsp
5755 7cbe629d 2018-08-04 stsp static const struct got_error *
5756 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5757 6c4c42e0 2019-06-24 stsp {
5758 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5759 6c4c42e0 2019-06-24 stsp
5760 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5761 6c4c42e0 2019-06-24 stsp return NULL;
5762 6c4c42e0 2019-06-24 stsp }
5763 6c4c42e0 2019-06-24 stsp
5764 6c4c42e0 2019-06-24 stsp static const struct got_error *
5765 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5766 6c4c42e0 2019-06-24 stsp {
5767 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5768 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5769 6c4c42e0 2019-06-24 stsp int lineno;
5770 78eecffc 2022-06-23 thomas char *line = NULL;
5771 826082fe 2020-12-10 stsp size_t linesize = 0;
5772 826082fe 2020-12-10 stsp ssize_t linelen;
5773 6c4c42e0 2019-06-24 stsp
5774 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5775 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5776 6c4c42e0 2019-06-24 stsp return NULL;
5777 6c4c42e0 2019-06-24 stsp }
5778 6c4c42e0 2019-06-24 stsp
5779 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5780 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5781 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5782 6c4c42e0 2019-06-24 stsp else
5783 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5784 4b3f9dac 2021-12-17 thomas } else
5785 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5786 6c4c42e0 2019-06-24 stsp
5787 6c4c42e0 2019-06-24 stsp while (1) {
5788 6c4c42e0 2019-06-24 stsp off_t offset;
5789 6c4c42e0 2019-06-24 stsp
5790 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5791 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5792 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5793 6c4c42e0 2019-06-24 stsp break;
5794 6c4c42e0 2019-06-24 stsp }
5795 2246482e 2019-06-25 stsp
5796 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5797 6c4c42e0 2019-06-24 stsp lineno = 1;
5798 6c4c42e0 2019-06-24 stsp else
5799 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5800 6c4c42e0 2019-06-24 stsp }
5801 6c4c42e0 2019-06-24 stsp
5802 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5803 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5804 6c4c42e0 2019-06-24 stsp free(line);
5805 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5806 6c4c42e0 2019-06-24 stsp }
5807 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5808 78eecffc 2022-06-23 thomas if (linelen != -1) {
5809 78eecffc 2022-06-23 thomas char *exstr;
5810 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5811 78eecffc 2022-06-23 thomas if (err)
5812 78eecffc 2022-06-23 thomas break;
5813 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5814 78eecffc 2022-06-23 thomas &view->regmatch)) {
5815 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5816 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5817 78eecffc 2022-06-23 thomas free(exstr);
5818 78eecffc 2022-06-23 thomas break;
5819 78eecffc 2022-06-23 thomas }
5820 78eecffc 2022-06-23 thomas free(exstr);
5821 6c4c42e0 2019-06-24 stsp }
5822 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5823 6c4c42e0 2019-06-24 stsp lineno++;
5824 6c4c42e0 2019-06-24 stsp else
5825 6c4c42e0 2019-06-24 stsp lineno--;
5826 6c4c42e0 2019-06-24 stsp }
5827 826082fe 2020-12-10 stsp free(line);
5828 6c4c42e0 2019-06-24 stsp
5829 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5830 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5831 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5832 6c4c42e0 2019-06-24 stsp }
5833 6c4c42e0 2019-06-24 stsp
5834 05171be4 2022-06-23 thomas return err;
5835 6c4c42e0 2019-06-24 stsp }
5836 6c4c42e0 2019-06-24 stsp
5837 6c4c42e0 2019-06-24 stsp static const struct got_error *
5838 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5839 7cbe629d 2018-08-04 stsp {
5840 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5841 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5842 2b380cc8 2018-10-24 stsp int errcode;
5843 2b380cc8 2018-10-24 stsp
5844 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5845 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5846 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5847 2b380cc8 2018-10-24 stsp if (errcode)
5848 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5849 51fe7530 2019-08-19 stsp
5850 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5851 2b380cc8 2018-10-24 stsp }
5852 e5a0f69f 2018-08-18 stsp
5853 51fe7530 2019-08-19 stsp if (s->blame_complete)
5854 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5855 51fe7530 2019-08-19 stsp
5856 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5857 e5a0f69f 2018-08-18 stsp
5858 a5d43cac 2022-07-01 thomas view_border(view);
5859 e5a0f69f 2018-08-18 stsp return err;
5860 e5a0f69f 2018-08-18 stsp }
5861 e5a0f69f 2018-08-18 stsp
5862 e5a0f69f 2018-08-18 stsp static const struct got_error *
5863 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5864 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5865 eaeaa612 2022-07-20 thomas {
5866 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5867 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5868 eaeaa612 2022-07-20 thomas
5869 eaeaa612 2022-07-20 thomas *new_view = NULL;
5870 eaeaa612 2022-07-20 thomas
5871 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5872 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5873 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5874 eaeaa612 2022-07-20 thomas
5875 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5876 eaeaa612 2022-07-20 thomas if (err)
5877 eaeaa612 2022-07-20 thomas view_close(log_view);
5878 eaeaa612 2022-07-20 thomas else
5879 eaeaa612 2022-07-20 thomas *new_view = log_view;
5880 eaeaa612 2022-07-20 thomas
5881 eaeaa612 2022-07-20 thomas return err;
5882 eaeaa612 2022-07-20 thomas }
5883 eaeaa612 2022-07-20 thomas
5884 eaeaa612 2022-07-20 thomas static const struct got_error *
5885 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5886 e5a0f69f 2018-08-18 stsp {
5887 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5888 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
5889 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5890 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5891 a5d43cac 2022-07-01 thomas
5892 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5893 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5894 a5d43cac 2022-07-01 thomas --eos; /* border */
5895 7cbe629d 2018-08-04 stsp
5896 e5a0f69f 2018-08-18 stsp switch (ch) {
5897 05171be4 2022-06-23 thomas case '0':
5898 05171be4 2022-06-23 thomas view->x = 0;
5899 05171be4 2022-06-23 thomas break;
5900 05171be4 2022-06-23 thomas case '$':
5901 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5902 07b0611c 2022-06-23 thomas view->count = 0;
5903 05171be4 2022-06-23 thomas break;
5904 05171be4 2022-06-23 thomas case KEY_RIGHT:
5905 05171be4 2022-06-23 thomas case 'l':
5906 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5907 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5908 07b0611c 2022-06-23 thomas else
5909 07b0611c 2022-06-23 thomas view->count = 0;
5910 05171be4 2022-06-23 thomas break;
5911 05171be4 2022-06-23 thomas case KEY_LEFT:
5912 05171be4 2022-06-23 thomas case 'h':
5913 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5914 07b0611c 2022-06-23 thomas if (view->x <= 0)
5915 07b0611c 2022-06-23 thomas view->count = 0;
5916 05171be4 2022-06-23 thomas break;
5917 1e37a5c2 2019-05-12 jcs case 'q':
5918 1e37a5c2 2019-05-12 jcs s->done = 1;
5919 4deef56f 2021-09-02 naddy break;
5920 4deef56f 2021-09-02 naddy case 'g':
5921 4deef56f 2021-09-02 naddy case KEY_HOME:
5922 4deef56f 2021-09-02 naddy s->selected_line = 1;
5923 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5924 07b0611c 2022-06-23 thomas view->count = 0;
5925 4deef56f 2021-09-02 naddy break;
5926 4deef56f 2021-09-02 naddy case 'G':
5927 4deef56f 2021-09-02 naddy case KEY_END:
5928 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5929 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5930 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5931 4deef56f 2021-09-02 naddy } else {
5932 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5933 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5934 4deef56f 2021-09-02 naddy }
5935 07b0611c 2022-06-23 thomas view->count = 0;
5936 1e37a5c2 2019-05-12 jcs break;
5937 1e37a5c2 2019-05-12 jcs case 'k':
5938 1e37a5c2 2019-05-12 jcs case KEY_UP:
5939 f7140bf5 2021-10-17 thomas case CTRL('p'):
5940 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5941 1e37a5c2 2019-05-12 jcs s->selected_line--;
5942 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5943 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5944 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5945 07b0611c 2022-06-23 thomas else
5946 07b0611c 2022-06-23 thomas view->count = 0;
5947 1e37a5c2 2019-05-12 jcs break;
5948 70f17a53 2022-06-13 thomas case CTRL('u'):
5949 23427b14 2022-06-23 thomas case 'u':
5950 70f17a53 2022-06-13 thomas nscroll /= 2;
5951 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5952 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5953 ea025d1d 2020-02-22 naddy case CTRL('b'):
5954 1c5e5faa 2022-06-23 thomas case 'b':
5955 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5956 07b0611c 2022-06-23 thomas if (view->count > 1)
5957 07b0611c 2022-06-23 thomas nscroll += nscroll;
5958 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5959 07b0611c 2022-06-23 thomas view->count = 0;
5960 e5a0f69f 2018-08-18 stsp break;
5961 1e37a5c2 2019-05-12 jcs }
5962 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5963 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5964 1e37a5c2 2019-05-12 jcs else
5965 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5966 1e37a5c2 2019-05-12 jcs break;
5967 1e37a5c2 2019-05-12 jcs case 'j':
5968 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5969 f7140bf5 2021-10-17 thomas case CTRL('n'):
5970 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5971 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5972 1e37a5c2 2019-05-12 jcs s->selected_line++;
5973 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5974 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5975 07b0611c 2022-06-23 thomas else
5976 07b0611c 2022-06-23 thomas view->count = 0;
5977 1e37a5c2 2019-05-12 jcs break;
5978 1c5e5faa 2022-06-23 thomas case 'c':
5979 1e37a5c2 2019-05-12 jcs case 'p': {
5980 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5981 07b0611c 2022-06-23 thomas
5982 07b0611c 2022-06-23 thomas view->count = 0;
5983 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5984 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5985 1e37a5c2 2019-05-12 jcs if (id == NULL)
5986 e5a0f69f 2018-08-18 stsp break;
5987 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5988 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5989 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5990 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5991 1e37a5c2 2019-05-12 jcs int obj_type;
5992 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5993 1e37a5c2 2019-05-12 jcs s->repo, id);
5994 e5a0f69f 2018-08-18 stsp if (err)
5995 e5a0f69f 2018-08-18 stsp break;
5996 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5997 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5998 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5999 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6000 e5a0f69f 2018-08-18 stsp break;
6001 e5a0f69f 2018-08-18 stsp }
6002 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6003 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6004 ec242592 2022-04-22 thomas s->repo, &pid->id);
6005 945f9229 2022-04-16 thomas if (err)
6006 945f9229 2022-04-16 thomas break;
6007 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6008 945f9229 2022-04-16 thomas pcommit, s->path);
6009 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6010 e5a0f69f 2018-08-18 stsp if (err) {
6011 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6012 1e37a5c2 2019-05-12 jcs err = NULL;
6013 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6014 e5a0f69f 2018-08-18 stsp break;
6015 e5a0f69f 2018-08-18 stsp }
6016 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6017 1e37a5c2 2019-05-12 jcs blob_id);
6018 1e37a5c2 2019-05-12 jcs free(blob_id);
6019 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6020 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6021 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6022 e5a0f69f 2018-08-18 stsp break;
6023 1e37a5c2 2019-05-12 jcs }
6024 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6025 ec242592 2022-04-22 thomas &pid->id);
6026 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6027 1e37a5c2 2019-05-12 jcs } else {
6028 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6029 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6030 1e37a5c2 2019-05-12 jcs break;
6031 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6032 1e37a5c2 2019-05-12 jcs id);
6033 1e37a5c2 2019-05-12 jcs }
6034 1e37a5c2 2019-05-12 jcs if (err)
6035 e5a0f69f 2018-08-18 stsp break;
6036 1e37a5c2 2019-05-12 jcs s->done = 1;
6037 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6038 1e37a5c2 2019-05-12 jcs s->done = 0;
6039 1e37a5c2 2019-05-12 jcs if (thread_err)
6040 1e37a5c2 2019-05-12 jcs break;
6041 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6042 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6043 a5388363 2020-12-01 naddy err = run_blame(view);
6044 1e37a5c2 2019-05-12 jcs if (err)
6045 1e37a5c2 2019-05-12 jcs break;
6046 1e37a5c2 2019-05-12 jcs break;
6047 1e37a5c2 2019-05-12 jcs }
6048 1c5e5faa 2022-06-23 thomas case 'C': {
6049 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6050 07b0611c 2022-06-23 thomas
6051 07b0611c 2022-06-23 thomas view->count = 0;
6052 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6053 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6054 1e37a5c2 2019-05-12 jcs break;
6055 1e37a5c2 2019-05-12 jcs s->done = 1;
6056 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6057 1e37a5c2 2019-05-12 jcs s->done = 0;
6058 1e37a5c2 2019-05-12 jcs if (thread_err)
6059 1e37a5c2 2019-05-12 jcs break;
6060 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6061 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6062 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6063 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6064 a5388363 2020-12-01 naddy err = run_blame(view);
6065 1e37a5c2 2019-05-12 jcs if (err)
6066 1e37a5c2 2019-05-12 jcs break;
6067 1e37a5c2 2019-05-12 jcs break;
6068 1e37a5c2 2019-05-12 jcs }
6069 2a31b33b 2022-07-23 thomas case 'L':
6070 eaeaa612 2022-07-20 thomas view->count = 0;
6071 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6072 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6073 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6074 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6075 eaeaa612 2022-07-20 thomas break;
6076 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6077 1e37a5c2 2019-05-12 jcs case '\r': {
6078 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6079 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6080 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6081 07b0611c 2022-06-23 thomas
6082 07b0611c 2022-06-23 thomas view->count = 0;
6083 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6084 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6085 1e37a5c2 2019-05-12 jcs if (id == NULL)
6086 1e37a5c2 2019-05-12 jcs break;
6087 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6088 1e37a5c2 2019-05-12 jcs if (err)
6089 1e37a5c2 2019-05-12 jcs break;
6090 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6091 4fc71f3b 2022-07-12 thomas if (*new_view) {
6092 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6093 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6094 4fc71f3b 2022-07-12 thomas if (err)
6095 4fc71f3b 2022-07-12 thomas break;
6096 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6097 4fc71f3b 2022-07-12 thomas } else {
6098 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6099 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6100 a5d43cac 2022-07-01 thomas
6101 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6102 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6103 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6104 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6105 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6106 4fc71f3b 2022-07-12 thomas break;
6107 4fc71f3b 2022-07-12 thomas }
6108 15a94983 2018-12-23 stsp }
6109 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6110 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6111 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6112 1e37a5c2 2019-05-12 jcs if (err) {
6113 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6114 1e37a5c2 2019-05-12 jcs break;
6115 1e37a5c2 2019-05-12 jcs }
6116 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6117 4fc71f3b 2022-07-12 thomas s->selected_line;
6118 4fc71f3b 2022-07-12 thomas if (*new_view)
6119 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6120 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6121 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6122 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6123 a5d43cac 2022-07-01 thomas if (err)
6124 a5d43cac 2022-07-01 thomas break;
6125 a5d43cac 2022-07-01 thomas }
6126 a5d43cac 2022-07-01 thomas
6127 e78dc838 2020-12-04 stsp view->focussed = 0;
6128 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6129 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6130 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6131 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6132 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6133 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6134 1e37a5c2 2019-05-12 jcs if (err)
6135 34bc9ec9 2019-02-22 stsp break;
6136 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6137 40236d76 2022-06-23 thomas if (err)
6138 40236d76 2022-06-23 thomas break;
6139 e78dc838 2020-12-04 stsp view->focus_child = 1;
6140 1e37a5c2 2019-05-12 jcs } else
6141 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6142 1e37a5c2 2019-05-12 jcs if (err)
6143 e5a0f69f 2018-08-18 stsp break;
6144 1e37a5c2 2019-05-12 jcs break;
6145 1e37a5c2 2019-05-12 jcs }
6146 70f17a53 2022-06-13 thomas case CTRL('d'):
6147 23427b14 2022-06-23 thomas case 'd':
6148 70f17a53 2022-06-13 thomas nscroll /= 2;
6149 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6150 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6151 ea025d1d 2020-02-22 naddy case CTRL('f'):
6152 1c5e5faa 2022-06-23 thomas case 'f':
6153 1e37a5c2 2019-05-12 jcs case ' ':
6154 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6155 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6156 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6157 07b0611c 2022-06-23 thomas view->count = 0;
6158 e5a0f69f 2018-08-18 stsp break;
6159 1e37a5c2 2019-05-12 jcs }
6160 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6161 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6162 70f17a53 2022-06-13 thomas s->selected_line +=
6163 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6164 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6165 1e37a5c2 2019-05-12 jcs }
6166 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6167 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6168 1e37a5c2 2019-05-12 jcs else
6169 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6170 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6171 1e37a5c2 2019-05-12 jcs break;
6172 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6173 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6174 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6175 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6176 1e37a5c2 2019-05-12 jcs }
6177 1e37a5c2 2019-05-12 jcs break;
6178 1e37a5c2 2019-05-12 jcs default:
6179 07b0611c 2022-06-23 thomas view->count = 0;
6180 1e37a5c2 2019-05-12 jcs break;
6181 a70480e0 2018-06-23 stsp }
6182 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6183 adf4c9e0 2022-07-03 thomas }
6184 adf4c9e0 2022-07-03 thomas
6185 adf4c9e0 2022-07-03 thomas static const struct got_error *
6186 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6187 adf4c9e0 2022-07-03 thomas {
6188 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6189 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6190 adf4c9e0 2022-07-03 thomas
6191 adf4c9e0 2022-07-03 thomas view->count = 0;
6192 adf4c9e0 2022-07-03 thomas s->done = 1;
6193 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6194 adf4c9e0 2022-07-03 thomas s->done = 0;
6195 adf4c9e0 2022-07-03 thomas if (err)
6196 adf4c9e0 2022-07-03 thomas return err;
6197 adf4c9e0 2022-07-03 thomas return run_blame(view);
6198 a70480e0 2018-06-23 stsp }
6199 a70480e0 2018-06-23 stsp
6200 a70480e0 2018-06-23 stsp static const struct got_error *
6201 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6202 9f7d7167 2018-04-29 stsp {
6203 a70480e0 2018-06-23 stsp const struct got_error *error;
6204 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6205 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6206 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6207 0587e10c 2020-07-23 stsp char *link_target = NULL;
6208 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6209 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6210 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6211 a70480e0 2018-06-23 stsp int ch;
6212 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6213 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6214 a70480e0 2018-06-23 stsp
6215 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6216 a70480e0 2018-06-23 stsp switch (ch) {
6217 a70480e0 2018-06-23 stsp case 'c':
6218 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6219 a70480e0 2018-06-23 stsp break;
6220 69069811 2018-08-02 stsp case 'r':
6221 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6222 69069811 2018-08-02 stsp if (repo_path == NULL)
6223 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6224 9ba1d308 2019-10-21 stsp optarg);
6225 69069811 2018-08-02 stsp break;
6226 a70480e0 2018-06-23 stsp default:
6227 17020d27 2019-03-07 stsp usage_blame();
6228 a70480e0 2018-06-23 stsp /* NOTREACHED */
6229 a70480e0 2018-06-23 stsp }
6230 a70480e0 2018-06-23 stsp }
6231 a70480e0 2018-06-23 stsp
6232 a70480e0 2018-06-23 stsp argc -= optind;
6233 a70480e0 2018-06-23 stsp argv += optind;
6234 a70480e0 2018-06-23 stsp
6235 f135c941 2020-02-20 stsp if (argc != 1)
6236 a70480e0 2018-06-23 stsp usage_blame();
6237 6962eb72 2020-02-20 stsp
6238 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6239 7cd52833 2022-06-23 thomas if (error != NULL)
6240 7cd52833 2022-06-23 thomas goto done;
6241 7cd52833 2022-06-23 thomas
6242 69069811 2018-08-02 stsp if (repo_path == NULL) {
6243 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6244 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6245 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6246 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6247 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6248 c156c7a4 2020-12-18 stsp goto done;
6249 f135c941 2020-02-20 stsp if (worktree)
6250 eb41ed75 2019-02-05 stsp repo_path =
6251 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6252 f135c941 2020-02-20 stsp else
6253 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6254 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6255 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6256 c156c7a4 2020-12-18 stsp goto done;
6257 c156c7a4 2020-12-18 stsp }
6258 f135c941 2020-02-20 stsp }
6259 a915003a 2019-02-05 stsp
6260 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6261 c02c541e 2019-03-29 stsp if (error != NULL)
6262 8e94dd5b 2019-01-04 stsp goto done;
6263 69069811 2018-08-02 stsp
6264 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6265 f135c941 2020-02-20 stsp worktree);
6266 c02c541e 2019-03-29 stsp if (error)
6267 92205607 2019-01-04 stsp goto done;
6268 69069811 2018-08-02 stsp
6269 f135c941 2020-02-20 stsp init_curses();
6270 f135c941 2020-02-20 stsp
6271 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6272 51a10b52 2020-12-26 stsp if (error)
6273 51a10b52 2020-12-26 stsp goto done;
6274 51a10b52 2020-12-26 stsp
6275 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6276 eb41ed75 2019-02-05 stsp if (error)
6277 69069811 2018-08-02 stsp goto done;
6278 a70480e0 2018-06-23 stsp
6279 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6280 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6281 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6282 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6283 a70480e0 2018-06-23 stsp if (error != NULL)
6284 66b4983c 2018-06-23 stsp goto done;
6285 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6286 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6287 a70480e0 2018-06-23 stsp } else {
6288 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6289 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6290 a70480e0 2018-06-23 stsp }
6291 a19e88aa 2018-06-23 stsp if (error != NULL)
6292 8b473291 2019-02-21 stsp goto done;
6293 8b473291 2019-02-21 stsp
6294 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6295 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6296 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6297 e1cd8fed 2018-08-01 stsp goto done;
6298 e1cd8fed 2018-08-01 stsp }
6299 0587e10c 2020-07-23 stsp
6300 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6301 945f9229 2022-04-16 thomas if (error)
6302 945f9229 2022-04-16 thomas goto done;
6303 945f9229 2022-04-16 thomas
6304 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6305 945f9229 2022-04-16 thomas commit, repo);
6306 7cbe629d 2018-08-04 stsp if (error)
6307 7cbe629d 2018-08-04 stsp goto done;
6308 0587e10c 2020-07-23 stsp
6309 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6310 78756c87 2020-11-24 stsp commit_id, repo);
6311 0587e10c 2020-07-23 stsp if (error)
6312 0587e10c 2020-07-23 stsp goto done;
6313 12314ad4 2019-08-31 stsp if (worktree) {
6314 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6315 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6316 12314ad4 2019-08-31 stsp worktree = NULL;
6317 12314ad4 2019-08-31 stsp }
6318 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6319 a70480e0 2018-06-23 stsp done:
6320 69069811 2018-08-02 stsp free(repo_path);
6321 f135c941 2020-02-20 stsp free(in_repo_path);
6322 0587e10c 2020-07-23 stsp free(link_target);
6323 69069811 2018-08-02 stsp free(cwd);
6324 a70480e0 2018-06-23 stsp free(commit_id);
6325 945f9229 2022-04-16 thomas if (commit)
6326 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6327 eb41ed75 2019-02-05 stsp if (worktree)
6328 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6329 1d0f4054 2021-06-17 stsp if (repo) {
6330 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6331 1d0f4054 2021-06-17 stsp if (error == NULL)
6332 1d0f4054 2021-06-17 stsp error = close_err;
6333 1d0f4054 2021-06-17 stsp }
6334 7cd52833 2022-06-23 thomas if (pack_fds) {
6335 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6336 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6337 7cd52833 2022-06-23 thomas if (error == NULL)
6338 7cd52833 2022-06-23 thomas error = pack_err;
6339 7cd52833 2022-06-23 thomas }
6340 51a10b52 2020-12-26 stsp tog_free_refs();
6341 a70480e0 2018-06-23 stsp return error;
6342 ffd1d5e5 2018-06-23 stsp }
6343 ffd1d5e5 2018-06-23 stsp
6344 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6345 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6346 ffd1d5e5 2018-06-23 stsp {
6347 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6348 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6349 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6350 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6351 86f4aab9 2022-09-09 thomas char *index = NULL;
6352 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6353 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6354 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6355 ffd1d5e5 2018-06-23 stsp
6356 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6357 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6358 a5d43cac 2022-07-01 thomas --limit; /* border */
6359 ffd1d5e5 2018-06-23 stsp
6360 f7d12f7e 2018-08-01 stsp werase(view->window);
6361 ffd1d5e5 2018-06-23 stsp
6362 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6363 ffd1d5e5 2018-06-23 stsp return NULL;
6364 ffd1d5e5 2018-06-23 stsp
6365 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6366 f91a2b48 2022-06-23 thomas 0, 0);
6367 ffd1d5e5 2018-06-23 stsp if (err)
6368 ffd1d5e5 2018-06-23 stsp return err;
6369 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6370 a3404814 2018-09-02 stsp wstandout(view->window);
6371 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6372 11b20872 2019-11-08 stsp if (tc)
6373 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6374 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6375 86f4aab9 2022-09-09 thomas free(wline);
6376 86f4aab9 2022-09-09 thomas wline = NULL;
6377 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6378 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6379 11b20872 2019-11-08 stsp if (tc)
6380 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6381 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6382 a3404814 2018-09-02 stsp wstandend(view->window);
6383 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6384 86f4aab9 2022-09-09 thomas return NULL;
6385 07dd3ed3 2022-08-06 thomas
6386 6f5f393a 2022-08-12 thomas i += s->selected;
6387 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6388 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6389 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6390 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6391 07dd3ed3 2022-08-06 thomas }
6392 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6393 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6394 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6395 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6396 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6397 86f4aab9 2022-09-09 thomas free(index);
6398 ce52c690 2018-06-23 stsp if (err)
6399 ce52c690 2018-06-23 stsp return err;
6400 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6401 2550e4c3 2018-07-13 stsp free(wline);
6402 2550e4c3 2018-07-13 stsp wline = NULL;
6403 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6404 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6405 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6406 ffd1d5e5 2018-06-23 stsp return NULL;
6407 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6408 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6409 a1eca9bb 2018-06-23 stsp return NULL;
6410 ffd1d5e5 2018-06-23 stsp
6411 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6412 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6413 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6414 0cf4efb1 2018-09-29 stsp if (view->focussed)
6415 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6416 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6417 ffd1d5e5 2018-06-23 stsp }
6418 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6419 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6420 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6421 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6422 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6423 ffd1d5e5 2018-06-23 stsp return NULL;
6424 ffd1d5e5 2018-06-23 stsp n = 1;
6425 ffd1d5e5 2018-06-23 stsp } else {
6426 ffd1d5e5 2018-06-23 stsp n = 0;
6427 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6428 ffd1d5e5 2018-06-23 stsp }
6429 ffd1d5e5 2018-06-23 stsp
6430 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6431 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6432 848d6979 2019-08-12 stsp const char *modestr = "";
6433 56e0773d 2019-11-28 stsp mode_t mode;
6434 1d13200f 2018-07-12 stsp
6435 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6436 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6437 56e0773d 2019-11-28 stsp
6438 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6439 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6440 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6441 1d13200f 2018-07-12 stsp if (err)
6442 638f9024 2019-05-13 stsp return got_error_from_errno(
6443 230a42bd 2019-05-11 jcs "got_object_id_str");
6444 1d13200f 2018-07-12 stsp }
6445 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6446 63c5ca5d 2019-08-24 stsp modestr = "$";
6447 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6448 0d6c6ee3 2020-05-20 stsp int i;
6449 0d6c6ee3 2020-05-20 stsp
6450 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6451 d86d3b18 2020-12-01 naddy te, s->repo);
6452 0d6c6ee3 2020-05-20 stsp if (err) {
6453 0d6c6ee3 2020-05-20 stsp free(id_str);
6454 0d6c6ee3 2020-05-20 stsp return err;
6455 0d6c6ee3 2020-05-20 stsp }
6456 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6457 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6458 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6459 0d6c6ee3 2020-05-20 stsp }
6460 848d6979 2019-08-12 stsp modestr = "@";
6461 0d6c6ee3 2020-05-20 stsp }
6462 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6463 848d6979 2019-08-12 stsp modestr = "/";
6464 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6465 848d6979 2019-08-12 stsp modestr = "*";
6466 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6467 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6468 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6469 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6470 1d13200f 2018-07-12 stsp free(id_str);
6471 0d6c6ee3 2020-05-20 stsp free(link_target);
6472 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6473 1d13200f 2018-07-12 stsp }
6474 1d13200f 2018-07-12 stsp free(id_str);
6475 0d6c6ee3 2020-05-20 stsp free(link_target);
6476 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6477 f91a2b48 2022-06-23 thomas 0, 0);
6478 ffd1d5e5 2018-06-23 stsp if (err) {
6479 ffd1d5e5 2018-06-23 stsp free(line);
6480 ffd1d5e5 2018-06-23 stsp break;
6481 ffd1d5e5 2018-06-23 stsp }
6482 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6483 0cf4efb1 2018-09-29 stsp if (view->focussed)
6484 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6485 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6486 ffd1d5e5 2018-06-23 stsp }
6487 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6488 f26dddb7 2019-11-08 stsp if (tc)
6489 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6490 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6491 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6492 f26dddb7 2019-11-08 stsp if (tc)
6493 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6494 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6495 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6496 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6497 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6498 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6499 ffd1d5e5 2018-06-23 stsp free(line);
6500 2550e4c3 2018-07-13 stsp free(wline);
6501 2550e4c3 2018-07-13 stsp wline = NULL;
6502 ffd1d5e5 2018-06-23 stsp n++;
6503 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6504 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6505 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6506 ffd1d5e5 2018-06-23 stsp break;
6507 ffd1d5e5 2018-06-23 stsp }
6508 ffd1d5e5 2018-06-23 stsp
6509 ffd1d5e5 2018-06-23 stsp return err;
6510 ffd1d5e5 2018-06-23 stsp }
6511 ffd1d5e5 2018-06-23 stsp
6512 ffd1d5e5 2018-06-23 stsp static void
6513 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6514 ffd1d5e5 2018-06-23 stsp {
6515 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6516 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6517 fa86c4bf 2020-11-29 stsp int i = 0;
6518 ffd1d5e5 2018-06-23 stsp
6519 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6520 ffd1d5e5 2018-06-23 stsp return;
6521 ffd1d5e5 2018-06-23 stsp
6522 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6523 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6524 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6525 fa86c4bf 2020-11-29 stsp if (!isroot)
6526 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6527 fa86c4bf 2020-11-29 stsp break;
6528 fa86c4bf 2020-11-29 stsp }
6529 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6530 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6531 ffd1d5e5 2018-06-23 stsp }
6532 ffd1d5e5 2018-06-23 stsp }
6533 ffd1d5e5 2018-06-23 stsp
6534 a5d43cac 2022-07-01 thomas static const struct got_error *
6535 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6536 ffd1d5e5 2018-06-23 stsp {
6537 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6538 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6539 ffd1d5e5 2018-06-23 stsp int n = 0;
6540 ffd1d5e5 2018-06-23 stsp
6541 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6542 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6543 694d3271 2020-12-01 naddy s->first_displayed_entry);
6544 694d3271 2020-12-01 naddy else
6545 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6546 56e0773d 2019-11-28 stsp
6547 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6548 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6549 07dd3ed3 2022-08-06 thomas if (last) {
6550 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6551 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6552 07dd3ed3 2022-08-06 thomas }
6553 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6554 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6555 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6556 768394f3 2019-01-24 stsp }
6557 ffd1d5e5 2018-06-23 stsp }
6558 a5d43cac 2022-07-01 thomas
6559 a5d43cac 2022-07-01 thomas return NULL;
6560 ffd1d5e5 2018-06-23 stsp }
6561 ffd1d5e5 2018-06-23 stsp
6562 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6563 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6564 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6565 ffd1d5e5 2018-06-23 stsp {
6566 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6567 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6568 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6569 ffd1d5e5 2018-06-23 stsp
6570 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6571 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6572 56e0773d 2019-11-28 stsp + 1 /* slash */;
6573 ce52c690 2018-06-23 stsp if (te)
6574 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6575 ce52c690 2018-06-23 stsp
6576 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6577 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6578 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6579 ffd1d5e5 2018-06-23 stsp
6580 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6581 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6582 d9765a41 2018-06-23 stsp while (pt) {
6583 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6584 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6585 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6586 cb2ebc8a 2018-06-23 stsp goto done;
6587 cb2ebc8a 2018-06-23 stsp }
6588 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6589 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6590 cb2ebc8a 2018-06-23 stsp goto done;
6591 cb2ebc8a 2018-06-23 stsp }
6592 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6593 ffd1d5e5 2018-06-23 stsp }
6594 ce52c690 2018-06-23 stsp if (te) {
6595 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6596 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6597 ce52c690 2018-06-23 stsp goto done;
6598 ce52c690 2018-06-23 stsp }
6599 cb2ebc8a 2018-06-23 stsp }
6600 ce52c690 2018-06-23 stsp done:
6601 ce52c690 2018-06-23 stsp if (err) {
6602 ce52c690 2018-06-23 stsp free(*path);
6603 ce52c690 2018-06-23 stsp *path = NULL;
6604 ce52c690 2018-06-23 stsp }
6605 ce52c690 2018-06-23 stsp return err;
6606 ce52c690 2018-06-23 stsp }
6607 ce52c690 2018-06-23 stsp
6608 ce52c690 2018-06-23 stsp static const struct got_error *
6609 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6610 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6611 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6612 ce52c690 2018-06-23 stsp {
6613 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6614 ce52c690 2018-06-23 stsp char *path;
6615 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6616 a0de39f3 2019-08-09 stsp
6617 a0de39f3 2019-08-09 stsp *new_view = NULL;
6618 69efd4c4 2018-07-18 stsp
6619 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6620 ce52c690 2018-06-23 stsp if (err)
6621 ce52c690 2018-06-23 stsp return err;
6622 ffd1d5e5 2018-06-23 stsp
6623 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6624 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6625 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6626 83ce39e3 2019-08-12 stsp goto done;
6627 83ce39e3 2019-08-12 stsp }
6628 cdf1ee82 2018-08-01 stsp
6629 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6630 e5a0f69f 2018-08-18 stsp if (err) {
6631 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6632 fc06ba56 2019-08-22 stsp err = NULL;
6633 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6634 e5a0f69f 2018-08-18 stsp } else
6635 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6636 83ce39e3 2019-08-12 stsp done:
6637 83ce39e3 2019-08-12 stsp free(path);
6638 69efd4c4 2018-07-18 stsp return err;
6639 69efd4c4 2018-07-18 stsp }
6640 69efd4c4 2018-07-18 stsp
6641 69efd4c4 2018-07-18 stsp static const struct got_error *
6642 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6643 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6644 69efd4c4 2018-07-18 stsp {
6645 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6646 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6647 69efd4c4 2018-07-18 stsp char *path;
6648 a0de39f3 2019-08-09 stsp
6649 a0de39f3 2019-08-09 stsp *new_view = NULL;
6650 69efd4c4 2018-07-18 stsp
6651 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6652 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6653 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6654 e5a0f69f 2018-08-18 stsp
6655 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6656 69efd4c4 2018-07-18 stsp if (err)
6657 69efd4c4 2018-07-18 stsp return err;
6658 69efd4c4 2018-07-18 stsp
6659 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6660 4e97c21c 2020-12-06 stsp path, 0);
6661 ba4f502b 2018-08-04 stsp if (err)
6662 e5a0f69f 2018-08-18 stsp view_close(log_view);
6663 e5a0f69f 2018-08-18 stsp else
6664 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6665 cb2ebc8a 2018-06-23 stsp free(path);
6666 cb2ebc8a 2018-06-23 stsp return err;
6667 ffd1d5e5 2018-06-23 stsp }
6668 ffd1d5e5 2018-06-23 stsp
6669 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6670 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6671 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6672 ffd1d5e5 2018-06-23 stsp {
6673 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6674 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6675 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6676 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6677 ffd1d5e5 2018-06-23 stsp
6678 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6679 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6680 bc573f3b 2021-07-10 stsp
6681 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6682 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6683 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6684 ffd1d5e5 2018-06-23 stsp
6685 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6686 bc573f3b 2021-07-10 stsp if (err)
6687 bc573f3b 2021-07-10 stsp goto done;
6688 bc573f3b 2021-07-10 stsp
6689 bc573f3b 2021-07-10 stsp /*
6690 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6691 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6692 bc573f3b 2021-07-10 stsp * closed on demand.
6693 bc573f3b 2021-07-10 stsp */
6694 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6695 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6696 bc573f3b 2021-07-10 stsp if (err)
6697 bc573f3b 2021-07-10 stsp goto done;
6698 bc573f3b 2021-07-10 stsp s->tree = s->root;
6699 bc573f3b 2021-07-10 stsp
6700 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6701 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6702 ffd1d5e5 2018-06-23 stsp goto done;
6703 ffd1d5e5 2018-06-23 stsp
6704 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6705 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6706 ffd1d5e5 2018-06-23 stsp goto done;
6707 ffd1d5e5 2018-06-23 stsp }
6708 ffd1d5e5 2018-06-23 stsp
6709 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6710 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6711 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6712 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6713 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6714 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6715 9cd7cbd1 2020-12-07 stsp goto done;
6716 9cd7cbd1 2020-12-07 stsp }
6717 9cd7cbd1 2020-12-07 stsp }
6718 fb2756b9 2018-08-04 stsp s->repo = repo;
6719 c0b01bdb 2019-11-08 stsp
6720 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6721 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6722 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6723 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6724 c0b01bdb 2019-11-08 stsp if (err)
6725 c0b01bdb 2019-11-08 stsp goto done;
6726 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6727 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6728 bc573f3b 2021-07-10 stsp if (err)
6729 c0b01bdb 2019-11-08 stsp goto done;
6730 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6731 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6732 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6733 bc573f3b 2021-07-10 stsp if (err)
6734 c0b01bdb 2019-11-08 stsp goto done;
6735 e5a0f69f 2018-08-18 stsp
6736 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6737 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6738 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6739 bc573f3b 2021-07-10 stsp if (err)
6740 11b20872 2019-11-08 stsp goto done;
6741 11b20872 2019-11-08 stsp
6742 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6743 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6744 bc573f3b 2021-07-10 stsp if (err)
6745 c0b01bdb 2019-11-08 stsp goto done;
6746 c0b01bdb 2019-11-08 stsp }
6747 c0b01bdb 2019-11-08 stsp
6748 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6749 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6750 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6751 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6752 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6753 ad80ab7b 2018-08-04 stsp done:
6754 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6755 bc573f3b 2021-07-10 stsp if (commit)
6756 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6757 bc573f3b 2021-07-10 stsp if (err)
6758 bc573f3b 2021-07-10 stsp close_tree_view(view);
6759 ad80ab7b 2018-08-04 stsp return err;
6760 ad80ab7b 2018-08-04 stsp }
6761 ad80ab7b 2018-08-04 stsp
6762 e5a0f69f 2018-08-18 stsp static const struct got_error *
6763 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6764 ad80ab7b 2018-08-04 stsp {
6765 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6766 ad80ab7b 2018-08-04 stsp
6767 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6768 fb2756b9 2018-08-04 stsp free(s->tree_label);
6769 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6770 6484ec90 2018-09-29 stsp free(s->commit_id);
6771 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6772 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6773 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6774 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6775 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6776 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6777 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6778 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6779 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6780 ad80ab7b 2018-08-04 stsp free(parent);
6781 ad80ab7b 2018-08-04 stsp
6782 ad80ab7b 2018-08-04 stsp }
6783 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6784 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6785 bc573f3b 2021-07-10 stsp if (s->root)
6786 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6787 7c32bd05 2019-06-22 stsp return NULL;
6788 7c32bd05 2019-06-22 stsp }
6789 7c32bd05 2019-06-22 stsp
6790 7c32bd05 2019-06-22 stsp static const struct got_error *
6791 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6792 7c32bd05 2019-06-22 stsp {
6793 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6794 7c32bd05 2019-06-22 stsp
6795 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6796 7c32bd05 2019-06-22 stsp return NULL;
6797 7c32bd05 2019-06-22 stsp }
6798 7c32bd05 2019-06-22 stsp
6799 7c32bd05 2019-06-22 stsp static int
6800 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6801 7c32bd05 2019-06-22 stsp {
6802 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6803 7c32bd05 2019-06-22 stsp
6804 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6805 56e0773d 2019-11-28 stsp 0) == 0;
6806 7c32bd05 2019-06-22 stsp }
6807 7c32bd05 2019-06-22 stsp
6808 7c32bd05 2019-06-22 stsp static const struct got_error *
6809 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6810 7c32bd05 2019-06-22 stsp {
6811 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6812 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6813 7c32bd05 2019-06-22 stsp
6814 7c32bd05 2019-06-22 stsp if (!view->searching) {
6815 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6816 7c32bd05 2019-06-22 stsp return NULL;
6817 7c32bd05 2019-06-22 stsp }
6818 7c32bd05 2019-06-22 stsp
6819 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6820 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6821 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6822 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6823 56e0773d 2019-11-28 stsp s->selected_entry);
6824 7c32bd05 2019-06-22 stsp else
6825 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6826 56e0773d 2019-11-28 stsp } else {
6827 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6828 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6829 56e0773d 2019-11-28 stsp else
6830 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6831 56e0773d 2019-11-28 stsp s->selected_entry);
6832 7c32bd05 2019-06-22 stsp }
6833 7c32bd05 2019-06-22 stsp } else {
6834 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6835 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6836 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6837 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6838 56e0773d 2019-11-28 stsp else
6839 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6840 7c32bd05 2019-06-22 stsp }
6841 7c32bd05 2019-06-22 stsp
6842 7c32bd05 2019-06-22 stsp while (1) {
6843 56e0773d 2019-11-28 stsp if (te == NULL) {
6844 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6845 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6846 ac66afb8 2019-06-24 stsp return NULL;
6847 ac66afb8 2019-06-24 stsp }
6848 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6849 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6850 56e0773d 2019-11-28 stsp else
6851 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6852 7c32bd05 2019-06-22 stsp }
6853 7c32bd05 2019-06-22 stsp
6854 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6855 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6856 56e0773d 2019-11-28 stsp s->matched_entry = te;
6857 7c32bd05 2019-06-22 stsp break;
6858 7c32bd05 2019-06-22 stsp }
6859 7c32bd05 2019-06-22 stsp
6860 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6861 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6862 56e0773d 2019-11-28 stsp else
6863 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6864 7c32bd05 2019-06-22 stsp }
6865 e5a0f69f 2018-08-18 stsp
6866 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6867 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6868 7c32bd05 2019-06-22 stsp s->selected = 0;
6869 7c32bd05 2019-06-22 stsp }
6870 7c32bd05 2019-06-22 stsp
6871 e5a0f69f 2018-08-18 stsp return NULL;
6872 ad80ab7b 2018-08-04 stsp }
6873 ad80ab7b 2018-08-04 stsp
6874 ad80ab7b 2018-08-04 stsp static const struct got_error *
6875 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6876 ad80ab7b 2018-08-04 stsp {
6877 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6878 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6879 e5a0f69f 2018-08-18 stsp char *parent_path;
6880 ad80ab7b 2018-08-04 stsp
6881 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6882 e5a0f69f 2018-08-18 stsp if (err)
6883 e5a0f69f 2018-08-18 stsp return err;
6884 ffd1d5e5 2018-06-23 stsp
6885 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6886 e5a0f69f 2018-08-18 stsp free(parent_path);
6887 669b5ffa 2018-10-07 stsp
6888 a5d43cac 2022-07-01 thomas view_border(view);
6889 e5a0f69f 2018-08-18 stsp return err;
6890 07dd3ed3 2022-08-06 thomas }
6891 07dd3ed3 2022-08-06 thomas
6892 07dd3ed3 2022-08-06 thomas static const struct got_error *
6893 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
6894 07dd3ed3 2022-08-06 thomas {
6895 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
6896 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
6897 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
6898 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
6899 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
6900 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
6901 07dd3ed3 2022-08-06 thomas
6902 07dd3ed3 2022-08-06 thomas g = view->gline;
6903 07dd3ed3 2022-08-06 thomas view->gline = 0;
6904 07dd3ed3 2022-08-06 thomas
6905 07dd3ed3 2022-08-06 thomas if (g == 0)
6906 07dd3ed3 2022-08-06 thomas g = 1;
6907 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
6908 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6909 07dd3ed3 2022-08-06 thomas
6910 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
6911 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
6912 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
6913 07dd3ed3 2022-08-06 thomas
6914 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
6915 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6916 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
6917 07dd3ed3 2022-08-06 thomas }
6918 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
6919 07dd3ed3 2022-08-06 thomas last += off;
6920 07dd3ed3 2022-08-06 thomas
6921 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
6922 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6923 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
6924 07dd3ed3 2022-08-06 thomas }
6925 07dd3ed3 2022-08-06 thomas
6926 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
6927 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
6928 07dd3ed3 2022-08-06 thomas i += off;
6929 07dd3ed3 2022-08-06 thomas }
6930 07dd3ed3 2022-08-06 thomas
6931 07dd3ed3 2022-08-06 thomas if (i < g) {
6932 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
6933 07dd3ed3 2022-08-06 thomas if (err)
6934 07dd3ed3 2022-08-06 thomas return err;
6935 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
6936 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
6937 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
6938 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
6939 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6940 07dd3ed3 2022-08-06 thomas first += off;
6941 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6942 07dd3ed3 2022-08-06 thomas }
6943 07dd3ed3 2022-08-06 thomas } else if (i > g)
6944 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
6945 07dd3ed3 2022-08-06 thomas
6946 07dd3ed3 2022-08-06 thomas if (g < nlines &&
6947 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6948 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
6949 07dd3ed3 2022-08-06 thomas
6950 07dd3ed3 2022-08-06 thomas return NULL;
6951 e5a0f69f 2018-08-18 stsp }
6952 ce52c690 2018-06-23 stsp
6953 e5a0f69f 2018-08-18 stsp static const struct got_error *
6954 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6955 e5a0f69f 2018-08-18 stsp {
6956 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6957 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6958 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6959 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
6960 ffd1d5e5 2018-06-23 stsp
6961 07dd3ed3 2022-08-06 thomas if (view->gline)
6962 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
6963 07dd3ed3 2022-08-06 thomas
6964 e5a0f69f 2018-08-18 stsp switch (ch) {
6965 1e37a5c2 2019-05-12 jcs case 'i':
6966 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6967 07b0611c 2022-06-23 thomas view->count = 0;
6968 1e37a5c2 2019-05-12 jcs break;
6969 1be4947a 2022-07-22 thomas case 'L':
6970 07b0611c 2022-06-23 thomas view->count = 0;
6971 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6972 ffd1d5e5 2018-06-23 stsp break;
6973 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6974 152c1c93 2020-11-29 stsp break;
6975 1be4947a 2022-07-22 thomas case 'R':
6976 07b0611c 2022-06-23 thomas view->count = 0;
6977 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
6978 e4526bf5 2021-09-03 naddy break;
6979 e4526bf5 2021-09-03 naddy case 'g':
6980 e4526bf5 2021-09-03 naddy case KEY_HOME:
6981 e4526bf5 2021-09-03 naddy s->selected = 0;
6982 07b0611c 2022-06-23 thomas view->count = 0;
6983 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6984 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6985 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6986 e4526bf5 2021-09-03 naddy else
6987 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6988 1e37a5c2 2019-05-12 jcs break;
6989 e4526bf5 2021-09-03 naddy case 'G':
6990 a5d43cac 2022-07-01 thomas case KEY_END: {
6991 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6992 a5d43cac 2022-07-01 thomas
6993 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6994 a5d43cac 2022-07-01 thomas --eos; /* border */
6995 e4526bf5 2021-09-03 naddy s->selected = 0;
6996 07b0611c 2022-06-23 thomas view->count = 0;
6997 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6998 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6999 e4526bf5 2021-09-03 naddy if (te == NULL) {
7000 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7001 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7002 e4526bf5 2021-09-03 naddy n++;
7003 e4526bf5 2021-09-03 naddy }
7004 e4526bf5 2021-09-03 naddy break;
7005 e4526bf5 2021-09-03 naddy }
7006 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7007 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7008 e4526bf5 2021-09-03 naddy }
7009 e4526bf5 2021-09-03 naddy if (n > 0)
7010 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7011 e4526bf5 2021-09-03 naddy break;
7012 a5d43cac 2022-07-01 thomas }
7013 1e37a5c2 2019-05-12 jcs case 'k':
7014 1e37a5c2 2019-05-12 jcs case KEY_UP:
7015 f7140bf5 2021-10-17 thomas case CTRL('p'):
7016 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7017 1e37a5c2 2019-05-12 jcs s->selected--;
7018 fa86c4bf 2020-11-29 stsp break;
7019 1e37a5c2 2019-05-12 jcs }
7020 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7021 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7022 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7023 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7024 07b0611c 2022-06-23 thomas view->count = 0;
7025 1e37a5c2 2019-05-12 jcs break;
7026 70f17a53 2022-06-13 thomas case CTRL('u'):
7027 23427b14 2022-06-23 thomas case 'u':
7028 70f17a53 2022-06-13 thomas nscroll /= 2;
7029 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7030 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7031 ea025d1d 2020-02-22 naddy case CTRL('b'):
7032 1c5e5faa 2022-06-23 thomas case 'b':
7033 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7034 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7035 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7036 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7037 fa86c4bf 2020-11-29 stsp } else {
7038 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7039 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7040 fa86c4bf 2020-11-29 stsp }
7041 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7042 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7043 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7044 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7045 07b0611c 2022-06-23 thomas view->count = 0;
7046 1e37a5c2 2019-05-12 jcs break;
7047 1e37a5c2 2019-05-12 jcs case 'j':
7048 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7049 f7140bf5 2021-10-17 thomas case CTRL('n'):
7050 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7051 1e37a5c2 2019-05-12 jcs s->selected++;
7052 1e37a5c2 2019-05-12 jcs break;
7053 1e37a5c2 2019-05-12 jcs }
7054 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7055 07b0611c 2022-06-23 thomas == NULL) {
7056 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7057 07b0611c 2022-06-23 thomas view->count = 0;
7058 1e37a5c2 2019-05-12 jcs break;
7059 07b0611c 2022-06-23 thomas }
7060 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7061 1e37a5c2 2019-05-12 jcs break;
7062 70f17a53 2022-06-13 thomas case CTRL('d'):
7063 23427b14 2022-06-23 thomas case 'd':
7064 70f17a53 2022-06-13 thomas nscroll /= 2;
7065 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7066 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7067 ea025d1d 2020-02-22 naddy case CTRL('f'):
7068 1c5e5faa 2022-06-23 thomas case 'f':
7069 4c2d69cb 2022-06-23 thomas case ' ':
7070 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7071 1e37a5c2 2019-05-12 jcs == NULL) {
7072 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7073 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7074 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7075 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7076 07b0611c 2022-06-23 thomas else
7077 07b0611c 2022-06-23 thomas view->count = 0;
7078 1e37a5c2 2019-05-12 jcs break;
7079 1e37a5c2 2019-05-12 jcs }
7080 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7081 1e37a5c2 2019-05-12 jcs break;
7082 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7083 1e37a5c2 2019-05-12 jcs case '\r':
7084 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7085 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7086 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7087 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7088 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7089 07b0611c 2022-06-23 thomas view->count = 0;
7090 1e37a5c2 2019-05-12 jcs break;
7091 07b0611c 2022-06-23 thomas }
7092 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7093 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7094 1e37a5c2 2019-05-12 jcs entry);
7095 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7096 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7097 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7098 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7099 1e37a5c2 2019-05-12 jcs s->selected_entry =
7100 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7101 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7102 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7103 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7104 a5d43cac 2022-07-01 thomas if (err)
7105 a5d43cac 2022-07-01 thomas break;
7106 a5d43cac 2022-07-01 thomas }
7107 1e37a5c2 2019-05-12 jcs free(parent);
7108 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7109 56e0773d 2019-11-28 stsp s->selected_entry))) {
7110 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7111 07b0611c 2022-06-23 thomas view->count = 0;
7112 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7113 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7114 1e37a5c2 2019-05-12 jcs if (err)
7115 1e37a5c2 2019-05-12 jcs break;
7116 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7117 941e9f74 2019-05-21 stsp if (err) {
7118 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7119 1e37a5c2 2019-05-12 jcs break;
7120 1e37a5c2 2019-05-12 jcs }
7121 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7122 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7123 1e37a5c2 2019-05-12 jcs break;
7124 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7125 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7126 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7127 07b0611c 2022-06-23 thomas view->count = 0;
7128 1e37a5c2 2019-05-12 jcs break;
7129 1e37a5c2 2019-05-12 jcs default:
7130 07b0611c 2022-06-23 thomas view->count = 0;
7131 1e37a5c2 2019-05-12 jcs break;
7132 ffd1d5e5 2018-06-23 stsp }
7133 e5a0f69f 2018-08-18 stsp
7134 ffd1d5e5 2018-06-23 stsp return err;
7135 9f7d7167 2018-04-29 stsp }
7136 9f7d7167 2018-04-29 stsp
7137 ffd1d5e5 2018-06-23 stsp __dead static void
7138 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7139 ffd1d5e5 2018-06-23 stsp {
7140 ffd1d5e5 2018-06-23 stsp endwin();
7141 91198554 2022-06-23 thomas fprintf(stderr,
7142 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7143 ffd1d5e5 2018-06-23 stsp getprogname());
7144 ffd1d5e5 2018-06-23 stsp exit(1);
7145 ffd1d5e5 2018-06-23 stsp }
7146 ffd1d5e5 2018-06-23 stsp
7147 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7148 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7149 ffd1d5e5 2018-06-23 stsp {
7150 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7151 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7152 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7153 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7154 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7155 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7156 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7157 4e97c21c 2020-12-06 stsp char *label = NULL;
7158 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7159 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7160 ffd1d5e5 2018-06-23 stsp int ch;
7161 5221c383 2018-08-01 stsp struct tog_view *view;
7162 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7163 70ac5f84 2019-03-28 stsp
7164 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7165 ffd1d5e5 2018-06-23 stsp switch (ch) {
7166 ffd1d5e5 2018-06-23 stsp case 'c':
7167 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7168 74283ab8 2020-02-07 stsp break;
7169 74283ab8 2020-02-07 stsp case 'r':
7170 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7171 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7172 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7173 74283ab8 2020-02-07 stsp optarg);
7174 ffd1d5e5 2018-06-23 stsp break;
7175 ffd1d5e5 2018-06-23 stsp default:
7176 e99e2d15 2020-11-24 naddy usage_tree();
7177 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7178 ffd1d5e5 2018-06-23 stsp }
7179 ffd1d5e5 2018-06-23 stsp }
7180 ffd1d5e5 2018-06-23 stsp
7181 ffd1d5e5 2018-06-23 stsp argc -= optind;
7182 ffd1d5e5 2018-06-23 stsp argv += optind;
7183 ffd1d5e5 2018-06-23 stsp
7184 55cccc34 2020-02-20 stsp if (argc > 1)
7185 e99e2d15 2020-11-24 naddy usage_tree();
7186 7cd52833 2022-06-23 thomas
7187 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7188 7cd52833 2022-06-23 thomas if (error != NULL)
7189 7cd52833 2022-06-23 thomas goto done;
7190 74283ab8 2020-02-07 stsp
7191 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7192 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7193 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7194 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7195 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7196 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7197 c156c7a4 2020-12-18 stsp goto done;
7198 55cccc34 2020-02-20 stsp if (worktree)
7199 52185f70 2019-02-05 stsp repo_path =
7200 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7201 55cccc34 2020-02-20 stsp else
7202 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7203 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7204 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7205 c156c7a4 2020-12-18 stsp goto done;
7206 c156c7a4 2020-12-18 stsp }
7207 55cccc34 2020-02-20 stsp }
7208 a915003a 2019-02-05 stsp
7209 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7210 c02c541e 2019-03-29 stsp if (error != NULL)
7211 52185f70 2019-02-05 stsp goto done;
7212 d188b9a6 2019-01-04 stsp
7213 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7214 55cccc34 2020-02-20 stsp repo, worktree);
7215 55cccc34 2020-02-20 stsp if (error)
7216 55cccc34 2020-02-20 stsp goto done;
7217 55cccc34 2020-02-20 stsp
7218 55cccc34 2020-02-20 stsp init_curses();
7219 55cccc34 2020-02-20 stsp
7220 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7221 c02c541e 2019-03-29 stsp if (error)
7222 52185f70 2019-02-05 stsp goto done;
7223 ffd1d5e5 2018-06-23 stsp
7224 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7225 51a10b52 2020-12-26 stsp if (error)
7226 51a10b52 2020-12-26 stsp goto done;
7227 51a10b52 2020-12-26 stsp
7228 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7229 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7230 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7231 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7232 4e97c21c 2020-12-06 stsp if (error)
7233 4e97c21c 2020-12-06 stsp goto done;
7234 4e97c21c 2020-12-06 stsp head_ref_name = label;
7235 4e97c21c 2020-12-06 stsp } else {
7236 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7237 4e97c21c 2020-12-06 stsp if (error == NULL)
7238 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7239 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7240 4e97c21c 2020-12-06 stsp goto done;
7241 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7242 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7243 4e97c21c 2020-12-06 stsp if (error)
7244 4e97c21c 2020-12-06 stsp goto done;
7245 4e97c21c 2020-12-06 stsp }
7246 ffd1d5e5 2018-06-23 stsp
7247 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7248 945f9229 2022-04-16 thomas if (error)
7249 945f9229 2022-04-16 thomas goto done;
7250 945f9229 2022-04-16 thomas
7251 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7252 5221c383 2018-08-01 stsp if (view == NULL) {
7253 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7254 5221c383 2018-08-01 stsp goto done;
7255 5221c383 2018-08-01 stsp }
7256 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7257 ad80ab7b 2018-08-04 stsp if (error)
7258 ad80ab7b 2018-08-04 stsp goto done;
7259 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7260 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7261 d91faf3b 2020-12-01 naddy in_repo_path);
7262 55cccc34 2020-02-20 stsp if (error)
7263 55cccc34 2020-02-20 stsp goto done;
7264 55cccc34 2020-02-20 stsp }
7265 55cccc34 2020-02-20 stsp
7266 55cccc34 2020-02-20 stsp if (worktree) {
7267 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7268 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7269 55cccc34 2020-02-20 stsp worktree = NULL;
7270 55cccc34 2020-02-20 stsp }
7271 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7272 ffd1d5e5 2018-06-23 stsp done:
7273 52185f70 2019-02-05 stsp free(repo_path);
7274 e4a0e26d 2020-02-20 stsp free(cwd);
7275 ffd1d5e5 2018-06-23 stsp free(commit_id);
7276 4e97c21c 2020-12-06 stsp free(label);
7277 486cd271 2020-12-06 stsp if (ref)
7278 486cd271 2020-12-06 stsp got_ref_close(ref);
7279 1d0f4054 2021-06-17 stsp if (repo) {
7280 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7281 1d0f4054 2021-06-17 stsp if (error == NULL)
7282 1d0f4054 2021-06-17 stsp error = close_err;
7283 1d0f4054 2021-06-17 stsp }
7284 7cd52833 2022-06-23 thomas if (pack_fds) {
7285 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7286 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7287 7cd52833 2022-06-23 thomas if (error == NULL)
7288 7cd52833 2022-06-23 thomas error = pack_err;
7289 7cd52833 2022-06-23 thomas }
7290 51a10b52 2020-12-26 stsp tog_free_refs();
7291 ffd1d5e5 2018-06-23 stsp return error;
7292 6458efa5 2020-11-24 stsp }
7293 6458efa5 2020-11-24 stsp
7294 6458efa5 2020-11-24 stsp static const struct got_error *
7295 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7296 6458efa5 2020-11-24 stsp {
7297 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7298 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7299 6458efa5 2020-11-24 stsp
7300 6458efa5 2020-11-24 stsp s->nrefs = 0;
7301 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7302 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7303 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7304 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7305 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7306 6458efa5 2020-11-24 stsp continue;
7307 6458efa5 2020-11-24 stsp
7308 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7309 6458efa5 2020-11-24 stsp if (re == NULL)
7310 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7311 6458efa5 2020-11-24 stsp
7312 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7313 8924d611 2020-12-26 stsp if (re->ref == NULL)
7314 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7315 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7316 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7317 6458efa5 2020-11-24 stsp }
7318 6458efa5 2020-11-24 stsp
7319 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7320 6458efa5 2020-11-24 stsp return NULL;
7321 6458efa5 2020-11-24 stsp }
7322 6458efa5 2020-11-24 stsp
7323 ef20f542 2022-06-26 thomas static void
7324 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7325 6458efa5 2020-11-24 stsp {
7326 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7327 6458efa5 2020-11-24 stsp
7328 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7329 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7330 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7331 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7332 6458efa5 2020-11-24 stsp free(re);
7333 6458efa5 2020-11-24 stsp }
7334 6458efa5 2020-11-24 stsp }
7335 6458efa5 2020-11-24 stsp
7336 6458efa5 2020-11-24 stsp static const struct got_error *
7337 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7338 6458efa5 2020-11-24 stsp {
7339 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7340 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7341 6458efa5 2020-11-24 stsp
7342 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7343 6458efa5 2020-11-24 stsp s->repo = repo;
7344 6458efa5 2020-11-24 stsp
7345 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7346 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7347 6458efa5 2020-11-24 stsp
7348 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7349 6458efa5 2020-11-24 stsp if (err)
7350 6458efa5 2020-11-24 stsp return err;
7351 34ba6917 2020-11-30 stsp
7352 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7353 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7354 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7355 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7356 6458efa5 2020-11-24 stsp if (err)
7357 6458efa5 2020-11-24 stsp goto done;
7358 6458efa5 2020-11-24 stsp
7359 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7360 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7361 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7362 6458efa5 2020-11-24 stsp if (err)
7363 6458efa5 2020-11-24 stsp goto done;
7364 6458efa5 2020-11-24 stsp
7365 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7366 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7367 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7368 2183bbf6 2022-01-23 thomas if (err)
7369 2183bbf6 2022-01-23 thomas goto done;
7370 2183bbf6 2022-01-23 thomas
7371 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7372 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7373 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7374 6458efa5 2020-11-24 stsp if (err)
7375 6458efa5 2020-11-24 stsp goto done;
7376 6458efa5 2020-11-24 stsp }
7377 6458efa5 2020-11-24 stsp
7378 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7379 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7380 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7381 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7382 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7383 6458efa5 2020-11-24 stsp done:
7384 6458efa5 2020-11-24 stsp if (err)
7385 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7386 6458efa5 2020-11-24 stsp return err;
7387 6458efa5 2020-11-24 stsp }
7388 6458efa5 2020-11-24 stsp
7389 6458efa5 2020-11-24 stsp static const struct got_error *
7390 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7391 6458efa5 2020-11-24 stsp {
7392 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7393 6458efa5 2020-11-24 stsp
7394 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7395 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7396 6458efa5 2020-11-24 stsp
7397 6458efa5 2020-11-24 stsp return NULL;
7398 9f7d7167 2018-04-29 stsp }
7399 ce5b7c56 2019-07-09 stsp
7400 6458efa5 2020-11-24 stsp static const struct got_error *
7401 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7402 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7403 6458efa5 2020-11-24 stsp {
7404 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7405 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7406 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7407 6458efa5 2020-11-24 stsp int obj_type;
7408 6458efa5 2020-11-24 stsp
7409 c42c9805 2020-11-24 stsp *commit_id = NULL;
7410 6458efa5 2020-11-24 stsp
7411 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7412 6458efa5 2020-11-24 stsp if (err)
7413 6458efa5 2020-11-24 stsp return err;
7414 6458efa5 2020-11-24 stsp
7415 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7416 6458efa5 2020-11-24 stsp if (err)
7417 6458efa5 2020-11-24 stsp goto done;
7418 6458efa5 2020-11-24 stsp
7419 6458efa5 2020-11-24 stsp switch (obj_type) {
7420 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7421 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7422 6458efa5 2020-11-24 stsp break;
7423 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7424 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7425 6458efa5 2020-11-24 stsp if (err)
7426 6458efa5 2020-11-24 stsp goto done;
7427 c42c9805 2020-11-24 stsp free(obj_id);
7428 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7429 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7430 c42c9805 2020-11-24 stsp if (err)
7431 6458efa5 2020-11-24 stsp goto done;
7432 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7433 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7434 c42c9805 2020-11-24 stsp goto done;
7435 c42c9805 2020-11-24 stsp }
7436 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7437 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7438 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7439 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7440 c42c9805 2020-11-24 stsp goto done;
7441 c42c9805 2020-11-24 stsp }
7442 6458efa5 2020-11-24 stsp break;
7443 6458efa5 2020-11-24 stsp default:
7444 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7445 c42c9805 2020-11-24 stsp break;
7446 6458efa5 2020-11-24 stsp }
7447 6458efa5 2020-11-24 stsp
7448 c42c9805 2020-11-24 stsp done:
7449 c42c9805 2020-11-24 stsp if (tag)
7450 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7451 c42c9805 2020-11-24 stsp if (err) {
7452 c42c9805 2020-11-24 stsp free(*commit_id);
7453 c42c9805 2020-11-24 stsp *commit_id = NULL;
7454 c42c9805 2020-11-24 stsp }
7455 c42c9805 2020-11-24 stsp return err;
7456 c42c9805 2020-11-24 stsp }
7457 c42c9805 2020-11-24 stsp
7458 c42c9805 2020-11-24 stsp static const struct got_error *
7459 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7460 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7461 c42c9805 2020-11-24 stsp {
7462 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7463 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7464 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7465 c42c9805 2020-11-24 stsp
7466 c42c9805 2020-11-24 stsp *new_view = NULL;
7467 c42c9805 2020-11-24 stsp
7468 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7469 c42c9805 2020-11-24 stsp if (err) {
7470 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7471 c42c9805 2020-11-24 stsp return err;
7472 c42c9805 2020-11-24 stsp else
7473 c42c9805 2020-11-24 stsp return NULL;
7474 c42c9805 2020-11-24 stsp }
7475 c42c9805 2020-11-24 stsp
7476 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7477 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7478 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7479 6458efa5 2020-11-24 stsp goto done;
7480 6458efa5 2020-11-24 stsp }
7481 6458efa5 2020-11-24 stsp
7482 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7483 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7484 6458efa5 2020-11-24 stsp done:
7485 6458efa5 2020-11-24 stsp if (err)
7486 6458efa5 2020-11-24 stsp view_close(log_view);
7487 6458efa5 2020-11-24 stsp else
7488 6458efa5 2020-11-24 stsp *new_view = log_view;
7489 c42c9805 2020-11-24 stsp free(commit_id);
7490 6458efa5 2020-11-24 stsp return err;
7491 6458efa5 2020-11-24 stsp }
7492 6458efa5 2020-11-24 stsp
7493 ce5b7c56 2019-07-09 stsp static void
7494 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7495 6458efa5 2020-11-24 stsp {
7496 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7497 34ba6917 2020-11-30 stsp int i = 0;
7498 6458efa5 2020-11-24 stsp
7499 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7500 6458efa5 2020-11-24 stsp return;
7501 6458efa5 2020-11-24 stsp
7502 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7503 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7504 34ba6917 2020-11-30 stsp if (re == NULL)
7505 34ba6917 2020-11-30 stsp break;
7506 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7507 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7508 6458efa5 2020-11-24 stsp }
7509 6458efa5 2020-11-24 stsp }
7510 6458efa5 2020-11-24 stsp
7511 a5d43cac 2022-07-01 thomas static const struct got_error *
7512 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7513 6458efa5 2020-11-24 stsp {
7514 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7515 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7516 6458efa5 2020-11-24 stsp int n = 0;
7517 6458efa5 2020-11-24 stsp
7518 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7519 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7520 6458efa5 2020-11-24 stsp else
7521 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7522 6458efa5 2020-11-24 stsp
7523 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7524 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7525 07dd3ed3 2022-08-06 thomas if (last) {
7526 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7527 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7528 07dd3ed3 2022-08-06 thomas }
7529 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7530 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7531 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7532 6458efa5 2020-11-24 stsp }
7533 6458efa5 2020-11-24 stsp }
7534 a5d43cac 2022-07-01 thomas
7535 a5d43cac 2022-07-01 thomas return NULL;
7536 6458efa5 2020-11-24 stsp }
7537 6458efa5 2020-11-24 stsp
7538 6458efa5 2020-11-24 stsp static const struct got_error *
7539 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7540 6458efa5 2020-11-24 stsp {
7541 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7542 6458efa5 2020-11-24 stsp
7543 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7544 6458efa5 2020-11-24 stsp return NULL;
7545 6458efa5 2020-11-24 stsp }
7546 6458efa5 2020-11-24 stsp
7547 6458efa5 2020-11-24 stsp static int
7548 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7549 6458efa5 2020-11-24 stsp {
7550 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7551 6458efa5 2020-11-24 stsp
7552 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7553 6458efa5 2020-11-24 stsp 0) == 0;
7554 6458efa5 2020-11-24 stsp }
7555 6458efa5 2020-11-24 stsp
7556 6458efa5 2020-11-24 stsp static const struct got_error *
7557 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7558 6458efa5 2020-11-24 stsp {
7559 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7560 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7561 6458efa5 2020-11-24 stsp
7562 6458efa5 2020-11-24 stsp if (!view->searching) {
7563 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7564 6458efa5 2020-11-24 stsp return NULL;
7565 6458efa5 2020-11-24 stsp }
7566 6458efa5 2020-11-24 stsp
7567 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7568 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7569 6458efa5 2020-11-24 stsp if (s->selected_entry)
7570 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7571 6458efa5 2020-11-24 stsp else
7572 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7573 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7574 6458efa5 2020-11-24 stsp } else {
7575 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7576 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7577 6458efa5 2020-11-24 stsp else
7578 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7579 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7580 6458efa5 2020-11-24 stsp }
7581 6458efa5 2020-11-24 stsp } else {
7582 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7583 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7584 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7585 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7586 6458efa5 2020-11-24 stsp else
7587 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7588 6458efa5 2020-11-24 stsp }
7589 6458efa5 2020-11-24 stsp
7590 6458efa5 2020-11-24 stsp while (1) {
7591 6458efa5 2020-11-24 stsp if (re == NULL) {
7592 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7593 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7594 6458efa5 2020-11-24 stsp return NULL;
7595 6458efa5 2020-11-24 stsp }
7596 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7597 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7598 6458efa5 2020-11-24 stsp else
7599 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7600 6458efa5 2020-11-24 stsp }
7601 6458efa5 2020-11-24 stsp
7602 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7603 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7604 6458efa5 2020-11-24 stsp s->matched_entry = re;
7605 6458efa5 2020-11-24 stsp break;
7606 6458efa5 2020-11-24 stsp }
7607 6458efa5 2020-11-24 stsp
7608 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7609 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7610 6458efa5 2020-11-24 stsp else
7611 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7612 6458efa5 2020-11-24 stsp }
7613 6458efa5 2020-11-24 stsp
7614 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7615 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7616 6458efa5 2020-11-24 stsp s->selected = 0;
7617 6458efa5 2020-11-24 stsp }
7618 6458efa5 2020-11-24 stsp
7619 6458efa5 2020-11-24 stsp return NULL;
7620 6458efa5 2020-11-24 stsp }
7621 6458efa5 2020-11-24 stsp
7622 6458efa5 2020-11-24 stsp static const struct got_error *
7623 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7624 6458efa5 2020-11-24 stsp {
7625 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7626 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7627 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7628 6458efa5 2020-11-24 stsp char *line = NULL;
7629 6458efa5 2020-11-24 stsp wchar_t *wline;
7630 6458efa5 2020-11-24 stsp struct tog_color *tc;
7631 6458efa5 2020-11-24 stsp int width, n;
7632 6458efa5 2020-11-24 stsp int limit = view->nlines;
7633 6458efa5 2020-11-24 stsp
7634 6458efa5 2020-11-24 stsp werase(view->window);
7635 6458efa5 2020-11-24 stsp
7636 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7637 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7638 a5d43cac 2022-07-01 thomas --limit; /* border */
7639 6458efa5 2020-11-24 stsp
7640 6458efa5 2020-11-24 stsp if (limit == 0)
7641 6458efa5 2020-11-24 stsp return NULL;
7642 6458efa5 2020-11-24 stsp
7643 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7644 6458efa5 2020-11-24 stsp
7645 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7646 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7647 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7648 6458efa5 2020-11-24 stsp
7649 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7650 6458efa5 2020-11-24 stsp if (err) {
7651 6458efa5 2020-11-24 stsp free(line);
7652 6458efa5 2020-11-24 stsp return err;
7653 6458efa5 2020-11-24 stsp }
7654 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7655 6458efa5 2020-11-24 stsp wstandout(view->window);
7656 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7657 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7658 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7659 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7660 6458efa5 2020-11-24 stsp wstandend(view->window);
7661 6458efa5 2020-11-24 stsp free(wline);
7662 6458efa5 2020-11-24 stsp wline = NULL;
7663 6458efa5 2020-11-24 stsp free(line);
7664 6458efa5 2020-11-24 stsp line = NULL;
7665 6458efa5 2020-11-24 stsp if (--limit <= 0)
7666 6458efa5 2020-11-24 stsp return NULL;
7667 6458efa5 2020-11-24 stsp
7668 6458efa5 2020-11-24 stsp n = 0;
7669 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7670 6458efa5 2020-11-24 stsp char *line = NULL;
7671 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7672 6458efa5 2020-11-24 stsp
7673 84227eb1 2022-06-23 thomas if (s->show_date) {
7674 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7675 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7676 84227eb1 2022-06-23 thomas struct got_object_id *id;
7677 84227eb1 2022-06-23 thomas struct tm tm;
7678 84227eb1 2022-06-23 thomas time_t t;
7679 84227eb1 2022-06-23 thomas
7680 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7681 84227eb1 2022-06-23 thomas if (err)
7682 84227eb1 2022-06-23 thomas return err;
7683 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7684 84227eb1 2022-06-23 thomas if (err) {
7685 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7686 84227eb1 2022-06-23 thomas free(id);
7687 84227eb1 2022-06-23 thomas return err;
7688 84227eb1 2022-06-23 thomas }
7689 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7690 84227eb1 2022-06-23 thomas id);
7691 84227eb1 2022-06-23 thomas if (err) {
7692 84227eb1 2022-06-23 thomas free(id);
7693 84227eb1 2022-06-23 thomas return err;
7694 84227eb1 2022-06-23 thomas }
7695 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7696 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7697 84227eb1 2022-06-23 thomas } else {
7698 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7699 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7700 84227eb1 2022-06-23 thomas }
7701 84227eb1 2022-06-23 thomas free(id);
7702 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7703 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7704 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7705 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7706 84227eb1 2022-06-23 thomas }
7707 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7708 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7709 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7710 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7711 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7712 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7713 6458efa5 2020-11-24 stsp struct got_object_id *id;
7714 6458efa5 2020-11-24 stsp char *id_str;
7715 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7716 6458efa5 2020-11-24 stsp if (err)
7717 6458efa5 2020-11-24 stsp return err;
7718 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7719 6458efa5 2020-11-24 stsp if (err) {
7720 6458efa5 2020-11-24 stsp free(id);
7721 6458efa5 2020-11-24 stsp return err;
7722 6458efa5 2020-11-24 stsp }
7723 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7724 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7725 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7726 6458efa5 2020-11-24 stsp free(id);
7727 6458efa5 2020-11-24 stsp free(id_str);
7728 6458efa5 2020-11-24 stsp return err;
7729 6458efa5 2020-11-24 stsp }
7730 6458efa5 2020-11-24 stsp free(id);
7731 6458efa5 2020-11-24 stsp free(id_str);
7732 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7733 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7734 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7735 6458efa5 2020-11-24 stsp
7736 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7737 f91a2b48 2022-06-23 thomas 0, 0);
7738 6458efa5 2020-11-24 stsp if (err) {
7739 6458efa5 2020-11-24 stsp free(line);
7740 6458efa5 2020-11-24 stsp return err;
7741 6458efa5 2020-11-24 stsp }
7742 6458efa5 2020-11-24 stsp if (n == s->selected) {
7743 6458efa5 2020-11-24 stsp if (view->focussed)
7744 6458efa5 2020-11-24 stsp wstandout(view->window);
7745 6458efa5 2020-11-24 stsp s->selected_entry = re;
7746 6458efa5 2020-11-24 stsp }
7747 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7748 6458efa5 2020-11-24 stsp if (tc)
7749 6458efa5 2020-11-24 stsp wattr_on(view->window,
7750 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7751 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7752 6458efa5 2020-11-24 stsp if (tc)
7753 6458efa5 2020-11-24 stsp wattr_off(view->window,
7754 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7755 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7756 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7757 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7758 6458efa5 2020-11-24 stsp wstandend(view->window);
7759 6458efa5 2020-11-24 stsp free(line);
7760 6458efa5 2020-11-24 stsp free(wline);
7761 6458efa5 2020-11-24 stsp wline = NULL;
7762 6458efa5 2020-11-24 stsp n++;
7763 6458efa5 2020-11-24 stsp s->ndisplayed++;
7764 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7765 6458efa5 2020-11-24 stsp
7766 6458efa5 2020-11-24 stsp limit--;
7767 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7768 6458efa5 2020-11-24 stsp }
7769 6458efa5 2020-11-24 stsp
7770 a5d43cac 2022-07-01 thomas view_border(view);
7771 6458efa5 2020-11-24 stsp return err;
7772 6458efa5 2020-11-24 stsp }
7773 6458efa5 2020-11-24 stsp
7774 6458efa5 2020-11-24 stsp static const struct got_error *
7775 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7776 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7777 c42c9805 2020-11-24 stsp {
7778 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7779 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7780 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7781 c42c9805 2020-11-24 stsp
7782 c42c9805 2020-11-24 stsp *new_view = NULL;
7783 c42c9805 2020-11-24 stsp
7784 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7785 c42c9805 2020-11-24 stsp if (err) {
7786 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7787 c42c9805 2020-11-24 stsp return err;
7788 c42c9805 2020-11-24 stsp else
7789 c42c9805 2020-11-24 stsp return NULL;
7790 c42c9805 2020-11-24 stsp }
7791 c42c9805 2020-11-24 stsp
7792 c42c9805 2020-11-24 stsp
7793 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7794 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7795 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7796 c42c9805 2020-11-24 stsp goto done;
7797 c42c9805 2020-11-24 stsp }
7798 c42c9805 2020-11-24 stsp
7799 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7800 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7801 c42c9805 2020-11-24 stsp if (err)
7802 c42c9805 2020-11-24 stsp goto done;
7803 c42c9805 2020-11-24 stsp
7804 c42c9805 2020-11-24 stsp *new_view = tree_view;
7805 c42c9805 2020-11-24 stsp done:
7806 c42c9805 2020-11-24 stsp free(commit_id);
7807 c42c9805 2020-11-24 stsp return err;
7808 07dd3ed3 2022-08-06 thomas }
7809 07dd3ed3 2022-08-06 thomas
7810 07dd3ed3 2022-08-06 thomas static const struct got_error *
7811 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
7812 07dd3ed3 2022-08-06 thomas {
7813 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7814 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
7815 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
7816 07dd3ed3 2022-08-06 thomas
7817 07dd3ed3 2022-08-06 thomas g = view->gline;
7818 07dd3ed3 2022-08-06 thomas view->gline = 0;
7819 07dd3ed3 2022-08-06 thomas
7820 07dd3ed3 2022-08-06 thomas if (g == 0)
7821 07dd3ed3 2022-08-06 thomas g = 1;
7822 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
7823 07dd3ed3 2022-08-06 thomas g = s->nrefs;
7824 07dd3ed3 2022-08-06 thomas
7825 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
7826 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
7827 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
7828 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7829 07dd3ed3 2022-08-06 thomas return NULL;
7830 07dd3ed3 2022-08-06 thomas }
7831 07dd3ed3 2022-08-06 thomas
7832 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
7833 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
7834 07dd3ed3 2022-08-06 thomas if (err)
7835 07dd3ed3 2022-08-06 thomas return err;
7836 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7837 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
7838 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
7839 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7840 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
7841 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
7842 07dd3ed3 2022-08-06 thomas
7843 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
7844 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7845 07dd3ed3 2022-08-06 thomas
7846 07dd3ed3 2022-08-06 thomas return NULL;
7847 07dd3ed3 2022-08-06 thomas
7848 c42c9805 2020-11-24 stsp }
7849 07dd3ed3 2022-08-06 thomas
7850 c42c9805 2020-11-24 stsp static const struct got_error *
7851 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7852 6458efa5 2020-11-24 stsp {
7853 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7854 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7855 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7856 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
7857 6458efa5 2020-11-24 stsp
7858 07dd3ed3 2022-08-06 thomas if (view->gline)
7859 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
7860 07dd3ed3 2022-08-06 thomas
7861 6458efa5 2020-11-24 stsp switch (ch) {
7862 6458efa5 2020-11-24 stsp case 'i':
7863 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7864 07b0611c 2022-06-23 thomas view->count = 0;
7865 3bfadbd4 2021-11-20 thomas break;
7866 84227eb1 2022-06-23 thomas case 'm':
7867 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7868 07b0611c 2022-06-23 thomas view->count = 0;
7869 84227eb1 2022-06-23 thomas break;
7870 98182bd0 2021-11-20 thomas case 'o':
7871 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7872 07b0611c 2022-06-23 thomas view->count = 0;
7873 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7874 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7875 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7876 c591440f 2021-11-20 thomas if (err)
7877 c591440f 2021-11-20 thomas break;
7878 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7879 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7880 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7881 3bfadbd4 2021-11-20 thomas if (err)
7882 3bfadbd4 2021-11-20 thomas break;
7883 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7884 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7885 6458efa5 2020-11-24 stsp break;
7886 6458efa5 2020-11-24 stsp case KEY_ENTER:
7887 6458efa5 2020-11-24 stsp case '\r':
7888 07b0611c 2022-06-23 thomas view->count = 0;
7889 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7890 a5d43cac 2022-07-01 thomas break;
7891 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7892 6458efa5 2020-11-24 stsp break;
7893 1be4947a 2022-07-22 thomas case 'T':
7894 07b0611c 2022-06-23 thomas view->count = 0;
7895 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7896 c42c9805 2020-11-24 stsp break;
7897 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
7898 c42c9805 2020-11-24 stsp break;
7899 e4526bf5 2021-09-03 naddy case 'g':
7900 e4526bf5 2021-09-03 naddy case KEY_HOME:
7901 e4526bf5 2021-09-03 naddy s->selected = 0;
7902 07b0611c 2022-06-23 thomas view->count = 0;
7903 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7904 e4526bf5 2021-09-03 naddy break;
7905 e4526bf5 2021-09-03 naddy case 'G':
7906 a5d43cac 2022-07-01 thomas case KEY_END: {
7907 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7908 a5d43cac 2022-07-01 thomas
7909 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7910 a5d43cac 2022-07-01 thomas --eos; /* border */
7911 e4526bf5 2021-09-03 naddy s->selected = 0;
7912 07b0611c 2022-06-23 thomas view->count = 0;
7913 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7914 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7915 e4526bf5 2021-09-03 naddy if (re == NULL)
7916 e4526bf5 2021-09-03 naddy break;
7917 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7918 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7919 e4526bf5 2021-09-03 naddy }
7920 e4526bf5 2021-09-03 naddy if (n > 0)
7921 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7922 e4526bf5 2021-09-03 naddy break;
7923 a5d43cac 2022-07-01 thomas }
7924 6458efa5 2020-11-24 stsp case 'k':
7925 6458efa5 2020-11-24 stsp case KEY_UP:
7926 f7140bf5 2021-10-17 thomas case CTRL('p'):
7927 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7928 6458efa5 2020-11-24 stsp s->selected--;
7929 6458efa5 2020-11-24 stsp break;
7930 34ba6917 2020-11-30 stsp }
7931 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7932 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7933 07b0611c 2022-06-23 thomas view->count = 0;
7934 6458efa5 2020-11-24 stsp break;
7935 70f17a53 2022-06-13 thomas case CTRL('u'):
7936 23427b14 2022-06-23 thomas case 'u':
7937 70f17a53 2022-06-13 thomas nscroll /= 2;
7938 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7939 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7940 6458efa5 2020-11-24 stsp case CTRL('b'):
7941 1c5e5faa 2022-06-23 thomas case 'b':
7942 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7943 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7944 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7945 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7946 07b0611c 2022-06-23 thomas view->count = 0;
7947 6458efa5 2020-11-24 stsp break;
7948 6458efa5 2020-11-24 stsp case 'j':
7949 6458efa5 2020-11-24 stsp case KEY_DOWN:
7950 f7140bf5 2021-10-17 thomas case CTRL('n'):
7951 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7952 6458efa5 2020-11-24 stsp s->selected++;
7953 6458efa5 2020-11-24 stsp break;
7954 6458efa5 2020-11-24 stsp }
7955 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7956 6458efa5 2020-11-24 stsp /* can't scroll any further */
7957 07b0611c 2022-06-23 thomas view->count = 0;
7958 6458efa5 2020-11-24 stsp break;
7959 07b0611c 2022-06-23 thomas }
7960 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7961 6458efa5 2020-11-24 stsp break;
7962 70f17a53 2022-06-13 thomas case CTRL('d'):
7963 23427b14 2022-06-23 thomas case 'd':
7964 70f17a53 2022-06-13 thomas nscroll /= 2;
7965 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7966 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7967 6458efa5 2020-11-24 stsp case CTRL('f'):
7968 1c5e5faa 2022-06-23 thomas case 'f':
7969 4c2d69cb 2022-06-23 thomas case ' ':
7970 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7971 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7972 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7973 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7974 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7975 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7976 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7977 07b0611c 2022-06-23 thomas view->count = 0;
7978 6458efa5 2020-11-24 stsp break;
7979 6458efa5 2020-11-24 stsp }
7980 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7981 6458efa5 2020-11-24 stsp break;
7982 6458efa5 2020-11-24 stsp case CTRL('l'):
7983 07b0611c 2022-06-23 thomas view->count = 0;
7984 8924d611 2020-12-26 stsp tog_free_refs();
7985 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7986 8924d611 2020-12-26 stsp if (err)
7987 8924d611 2020-12-26 stsp break;
7988 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7989 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7990 6458efa5 2020-11-24 stsp break;
7991 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7992 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7993 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7994 6458efa5 2020-11-24 stsp break;
7995 6458efa5 2020-11-24 stsp default:
7996 07b0611c 2022-06-23 thomas view->count = 0;
7997 6458efa5 2020-11-24 stsp break;
7998 6458efa5 2020-11-24 stsp }
7999 6458efa5 2020-11-24 stsp
8000 6458efa5 2020-11-24 stsp return err;
8001 6458efa5 2020-11-24 stsp }
8002 6458efa5 2020-11-24 stsp
8003 6458efa5 2020-11-24 stsp __dead static void
8004 6458efa5 2020-11-24 stsp usage_ref(void)
8005 6458efa5 2020-11-24 stsp {
8006 6458efa5 2020-11-24 stsp endwin();
8007 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8008 6458efa5 2020-11-24 stsp getprogname());
8009 6458efa5 2020-11-24 stsp exit(1);
8010 6458efa5 2020-11-24 stsp }
8011 6458efa5 2020-11-24 stsp
8012 6458efa5 2020-11-24 stsp static const struct got_error *
8013 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8014 6458efa5 2020-11-24 stsp {
8015 6458efa5 2020-11-24 stsp const struct got_error *error;
8016 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8017 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8018 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8019 6458efa5 2020-11-24 stsp int ch;
8020 6458efa5 2020-11-24 stsp struct tog_view *view;
8021 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8022 6458efa5 2020-11-24 stsp
8023 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8024 6458efa5 2020-11-24 stsp switch (ch) {
8025 6458efa5 2020-11-24 stsp case 'r':
8026 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8027 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8028 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8029 6458efa5 2020-11-24 stsp optarg);
8030 6458efa5 2020-11-24 stsp break;
8031 6458efa5 2020-11-24 stsp default:
8032 e99e2d15 2020-11-24 naddy usage_ref();
8033 6458efa5 2020-11-24 stsp /* NOTREACHED */
8034 6458efa5 2020-11-24 stsp }
8035 6458efa5 2020-11-24 stsp }
8036 6458efa5 2020-11-24 stsp
8037 6458efa5 2020-11-24 stsp argc -= optind;
8038 6458efa5 2020-11-24 stsp argv += optind;
8039 6458efa5 2020-11-24 stsp
8040 6458efa5 2020-11-24 stsp if (argc > 1)
8041 e99e2d15 2020-11-24 naddy usage_ref();
8042 7cd52833 2022-06-23 thomas
8043 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8044 7cd52833 2022-06-23 thomas if (error != NULL)
8045 7cd52833 2022-06-23 thomas goto done;
8046 6458efa5 2020-11-24 stsp
8047 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8048 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8049 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8050 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8051 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8052 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8053 c156c7a4 2020-12-18 stsp goto done;
8054 6458efa5 2020-11-24 stsp if (worktree)
8055 6458efa5 2020-11-24 stsp repo_path =
8056 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8057 6458efa5 2020-11-24 stsp else
8058 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8059 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8060 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8061 c156c7a4 2020-12-18 stsp goto done;
8062 c156c7a4 2020-12-18 stsp }
8063 6458efa5 2020-11-24 stsp }
8064 6458efa5 2020-11-24 stsp
8065 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8066 6458efa5 2020-11-24 stsp if (error != NULL)
8067 6458efa5 2020-11-24 stsp goto done;
8068 6458efa5 2020-11-24 stsp
8069 6458efa5 2020-11-24 stsp init_curses();
8070 6458efa5 2020-11-24 stsp
8071 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8072 51a10b52 2020-12-26 stsp if (error)
8073 51a10b52 2020-12-26 stsp goto done;
8074 51a10b52 2020-12-26 stsp
8075 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8076 6458efa5 2020-11-24 stsp if (error)
8077 6458efa5 2020-11-24 stsp goto done;
8078 6458efa5 2020-11-24 stsp
8079 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8080 6458efa5 2020-11-24 stsp if (view == NULL) {
8081 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8082 6458efa5 2020-11-24 stsp goto done;
8083 6458efa5 2020-11-24 stsp }
8084 6458efa5 2020-11-24 stsp
8085 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8086 6458efa5 2020-11-24 stsp if (error)
8087 6458efa5 2020-11-24 stsp goto done;
8088 6458efa5 2020-11-24 stsp
8089 6458efa5 2020-11-24 stsp if (worktree) {
8090 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8091 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8092 6458efa5 2020-11-24 stsp worktree = NULL;
8093 6458efa5 2020-11-24 stsp }
8094 6458efa5 2020-11-24 stsp error = view_loop(view);
8095 6458efa5 2020-11-24 stsp done:
8096 6458efa5 2020-11-24 stsp free(repo_path);
8097 6458efa5 2020-11-24 stsp free(cwd);
8098 1d0f4054 2021-06-17 stsp if (repo) {
8099 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8100 1d0f4054 2021-06-17 stsp if (close_err)
8101 1d0f4054 2021-06-17 stsp error = close_err;
8102 1d0f4054 2021-06-17 stsp }
8103 7cd52833 2022-06-23 thomas if (pack_fds) {
8104 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8105 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8106 7cd52833 2022-06-23 thomas if (error == NULL)
8107 7cd52833 2022-06-23 thomas error = pack_err;
8108 7cd52833 2022-06-23 thomas }
8109 51a10b52 2020-12-26 stsp tog_free_refs();
8110 6458efa5 2020-11-24 stsp return error;
8111 6458efa5 2020-11-24 stsp }
8112 6458efa5 2020-11-24 stsp
8113 2a31b33b 2022-07-23 thomas static const struct got_error *
8114 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8115 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8116 2a31b33b 2022-07-23 thomas {
8117 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8118 2a31b33b 2022-07-23 thomas
8119 2a31b33b 2022-07-23 thomas *new_view = NULL;
8120 2a31b33b 2022-07-23 thomas
8121 2a31b33b 2022-07-23 thomas switch (request) {
8122 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8123 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8124 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8125 2a31b33b 2022-07-23 thomas
8126 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8127 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8128 2a31b33b 2022-07-23 thomas view, s->repo);
8129 2a31b33b 2022-07-23 thomas } else
8130 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8131 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8132 2a31b33b 2022-07-23 thomas break;
8133 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8134 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8135 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8136 2a31b33b 2022-07-23 thomas
8137 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8138 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8139 2a31b33b 2022-07-23 thomas s->repo);
8140 2a31b33b 2022-07-23 thomas } else
8141 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8142 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8143 2a31b33b 2022-07-23 thomas break;
8144 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8145 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8146 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8147 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8148 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8149 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8150 2a31b33b 2022-07-23 thomas &view->state.tree);
8151 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8152 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8153 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8154 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8155 2a31b33b 2022-07-23 thomas else
8156 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8157 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8158 2a31b33b 2022-07-23 thomas break;
8159 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8160 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8161 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8162 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8163 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8164 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8165 2a31b33b 2022-07-23 thomas view->state.log.repo);
8166 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8167 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8168 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8169 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8170 2a31b33b 2022-07-23 thomas else
8171 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8172 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8173 2a31b33b 2022-07-23 thomas break;
8174 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
8175 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8176 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
8177 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
8178 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8179 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
8180 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8181 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
8182 2a31b33b 2022-07-23 thomas else
8183 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
8184 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8185 2a31b33b 2022-07-23 thomas if (err)
8186 2a31b33b 2022-07-23 thomas view_close(*new_view);
8187 2a31b33b 2022-07-23 thomas break;
8188 2a31b33b 2022-07-23 thomas default:
8189 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8190 2a31b33b 2022-07-23 thomas }
8191 2a31b33b 2022-07-23 thomas
8192 2a31b33b 2022-07-23 thomas return err;
8193 2a31b33b 2022-07-23 thomas }
8194 2a31b33b 2022-07-23 thomas
8195 a5d43cac 2022-07-01 thomas /*
8196 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8197 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8198 a5d43cac 2022-07-01 thomas */
8199 6458efa5 2020-11-24 stsp static void
8200 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8201 a5d43cac 2022-07-01 thomas {
8202 a5d43cac 2022-07-01 thomas switch (view->type) {
8203 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8204 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8205 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8206 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8207 a5d43cac 2022-07-01 thomas 1);
8208 a5d43cac 2022-07-01 thomas break;
8209 a5d43cac 2022-07-01 thomas }
8210 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8211 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8212 a5d43cac 2022-07-01 thomas else
8213 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8214 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8215 a5d43cac 2022-07-01 thomas break;
8216 a5d43cac 2022-07-01 thomas }
8217 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8218 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8219 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8220 a5d43cac 2022-07-01 thomas break;
8221 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8222 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8223 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8224 a5d43cac 2022-07-01 thomas break;
8225 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8226 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8227 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8228 a5d43cac 2022-07-01 thomas break;
8229 a5d43cac 2022-07-01 thomas default:
8230 a5d43cac 2022-07-01 thomas break;
8231 a5d43cac 2022-07-01 thomas }
8232 a5d43cac 2022-07-01 thomas
8233 a5d43cac 2022-07-01 thomas view->offset = 0;
8234 a5d43cac 2022-07-01 thomas }
8235 a5d43cac 2022-07-01 thomas
8236 a5d43cac 2022-07-01 thomas /*
8237 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8238 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8239 a5d43cac 2022-07-01 thomas */
8240 a5d43cac 2022-07-01 thomas static const struct got_error *
8241 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8242 a5d43cac 2022-07-01 thomas {
8243 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8244 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8245 a5d43cac 2022-07-01 thomas int *selected = NULL;
8246 a5d43cac 2022-07-01 thomas int header, offset;
8247 a5d43cac 2022-07-01 thomas
8248 a5d43cac 2022-07-01 thomas switch (view->type) {
8249 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8250 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8251 a5d43cac 2022-07-01 thomas header = 3;
8252 a5d43cac 2022-07-01 thomas scrolld = NULL;
8253 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8254 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8255 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8256 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8257 a5d43cac 2022-07-01 thomas view->offset = offset;
8258 a5d43cac 2022-07-01 thomas }
8259 a5d43cac 2022-07-01 thomas break;
8260 a5d43cac 2022-07-01 thomas }
8261 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8262 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8263 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8264 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8265 a5d43cac 2022-07-01 thomas selected = &s->selected;
8266 a5d43cac 2022-07-01 thomas break;
8267 a5d43cac 2022-07-01 thomas }
8268 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8269 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8270 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8271 a5d43cac 2022-07-01 thomas header = 3;
8272 a5d43cac 2022-07-01 thomas selected = &s->selected;
8273 a5d43cac 2022-07-01 thomas break;
8274 a5d43cac 2022-07-01 thomas }
8275 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8276 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8277 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8278 a5d43cac 2022-07-01 thomas header = 5;
8279 a5d43cac 2022-07-01 thomas selected = &s->selected;
8280 a5d43cac 2022-07-01 thomas break;
8281 a5d43cac 2022-07-01 thomas }
8282 a5d43cac 2022-07-01 thomas default:
8283 a5d43cac 2022-07-01 thomas selected = NULL;
8284 a5d43cac 2022-07-01 thomas scrolld = NULL;
8285 a5d43cac 2022-07-01 thomas header = 0;
8286 a5d43cac 2022-07-01 thomas break;
8287 a5d43cac 2022-07-01 thomas }
8288 a5d43cac 2022-07-01 thomas
8289 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8290 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8291 a5d43cac 2022-07-01 thomas view->offset = offset;
8292 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8293 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8294 a5d43cac 2022-07-01 thomas *selected -= offset;
8295 a5d43cac 2022-07-01 thomas }
8296 a5d43cac 2022-07-01 thomas }
8297 a5d43cac 2022-07-01 thomas
8298 a5d43cac 2022-07-01 thomas return err;
8299 a5d43cac 2022-07-01 thomas }
8300 a5d43cac 2022-07-01 thomas
8301 a5d43cac 2022-07-01 thomas static void
8302 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8303 ce5b7c56 2019-07-09 stsp {
8304 6059809a 2020-12-17 stsp size_t i;
8305 9f7d7167 2018-04-29 stsp
8306 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8307 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8308 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8309 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8310 ce5b7c56 2019-07-09 stsp }
8311 6879ba42 2020-10-01 naddy fputc('\n', fp);
8312 ce5b7c56 2019-07-09 stsp }
8313 ce5b7c56 2019-07-09 stsp
8314 4ed7e80c 2018-05-20 stsp __dead static void
8315 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8316 9f7d7167 2018-04-29 stsp {
8317 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8318 6879ba42 2020-10-01 naddy
8319 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8320 6879ba42 2020-10-01 naddy getprogname());
8321 6879ba42 2020-10-01 naddy if (hflag) {
8322 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8323 6879ba42 2020-10-01 naddy list_commands(fp);
8324 ee85c5e8 2020-02-29 stsp }
8325 6879ba42 2020-10-01 naddy exit(status);
8326 9f7d7167 2018-04-29 stsp }
8327 9f7d7167 2018-04-29 stsp
8328 c2301be8 2018-04-30 stsp static char **
8329 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8330 c2301be8 2018-04-30 stsp {
8331 ee85c5e8 2020-02-29 stsp va_list ap;
8332 c2301be8 2018-04-30 stsp char **argv;
8333 ee85c5e8 2020-02-29 stsp int i;
8334 c2301be8 2018-04-30 stsp
8335 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8336 ee85c5e8 2020-02-29 stsp
8337 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8338 c2301be8 2018-04-30 stsp if (argv == NULL)
8339 c2301be8 2018-04-30 stsp err(1, "calloc");
8340 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8341 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8342 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8343 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8344 c2301be8 2018-04-30 stsp }
8345 c2301be8 2018-04-30 stsp
8346 ee85c5e8 2020-02-29 stsp va_end(ap);
8347 c2301be8 2018-04-30 stsp return argv;
8348 ee85c5e8 2020-02-29 stsp }
8349 ee85c5e8 2020-02-29 stsp
8350 ee85c5e8 2020-02-29 stsp /*
8351 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8352 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8353 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8354 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8355 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8356 ee85c5e8 2020-02-29 stsp */
8357 ee85c5e8 2020-02-29 stsp static const struct got_error *
8358 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8359 ee85c5e8 2020-02-29 stsp {
8360 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8361 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8362 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8363 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8364 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8365 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8366 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8367 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8368 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8369 84de9106 2020-12-26 stsp
8370 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8371 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8372 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8373 ee85c5e8 2020-02-29 stsp
8374 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8375 7cd52833 2022-06-23 thomas if (error != NULL)
8376 7cd52833 2022-06-23 thomas goto done;
8377 7cd52833 2022-06-23 thomas
8378 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8379 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8380 ee85c5e8 2020-02-29 stsp goto done;
8381 ee85c5e8 2020-02-29 stsp
8382 ee85c5e8 2020-02-29 stsp if (worktree)
8383 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8384 ee85c5e8 2020-02-29 stsp else
8385 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8386 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8387 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8388 ee85c5e8 2020-02-29 stsp goto done;
8389 ee85c5e8 2020-02-29 stsp }
8390 ee85c5e8 2020-02-29 stsp
8391 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8392 ee85c5e8 2020-02-29 stsp if (error != NULL)
8393 ee85c5e8 2020-02-29 stsp goto done;
8394 ee85c5e8 2020-02-29 stsp
8395 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8396 ee85c5e8 2020-02-29 stsp repo, worktree);
8397 ee85c5e8 2020-02-29 stsp if (error)
8398 ee85c5e8 2020-02-29 stsp goto done;
8399 ee85c5e8 2020-02-29 stsp
8400 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8401 84de9106 2020-12-26 stsp if (error)
8402 84de9106 2020-12-26 stsp goto done;
8403 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8404 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8405 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8406 ee85c5e8 2020-02-29 stsp if (error)
8407 ee85c5e8 2020-02-29 stsp goto done;
8408 ee85c5e8 2020-02-29 stsp
8409 ee85c5e8 2020-02-29 stsp if (worktree) {
8410 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8411 ee85c5e8 2020-02-29 stsp worktree = NULL;
8412 ee85c5e8 2020-02-29 stsp }
8413 ee85c5e8 2020-02-29 stsp
8414 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8415 945f9229 2022-04-16 thomas if (error)
8416 945f9229 2022-04-16 thomas goto done;
8417 945f9229 2022-04-16 thomas
8418 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8419 ee85c5e8 2020-02-29 stsp if (error) {
8420 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8421 ee85c5e8 2020-02-29 stsp goto done;
8422 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8423 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8424 6879ba42 2020-10-01 naddy usage(1, 1);
8425 ee85c5e8 2020-02-29 stsp /* not reached */
8426 ee85c5e8 2020-02-29 stsp }
8427 ee85c5e8 2020-02-29 stsp
8428 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8429 ee85c5e8 2020-02-29 stsp if (error)
8430 ee85c5e8 2020-02-29 stsp goto done;
8431 ee85c5e8 2020-02-29 stsp
8432 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8433 ee85c5e8 2020-02-29 stsp argc = 4;
8434 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8435 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8436 ee85c5e8 2020-02-29 stsp done:
8437 1d0f4054 2021-06-17 stsp if (repo) {
8438 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8439 1d0f4054 2021-06-17 stsp if (error == NULL)
8440 1d0f4054 2021-06-17 stsp error = close_err;
8441 1d0f4054 2021-06-17 stsp }
8442 945f9229 2022-04-16 thomas if (commit)
8443 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8444 ee85c5e8 2020-02-29 stsp if (worktree)
8445 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8446 7cd52833 2022-06-23 thomas if (pack_fds) {
8447 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8448 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8449 7cd52833 2022-06-23 thomas if (error == NULL)
8450 7cd52833 2022-06-23 thomas error = pack_err;
8451 7cd52833 2022-06-23 thomas }
8452 ee85c5e8 2020-02-29 stsp free(id);
8453 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8454 ee85c5e8 2020-02-29 stsp free(commit_id);
8455 ee85c5e8 2020-02-29 stsp free(cwd);
8456 ee85c5e8 2020-02-29 stsp free(repo_path);
8457 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8458 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8459 ee85c5e8 2020-02-29 stsp int i;
8460 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8461 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8462 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8463 ee85c5e8 2020-02-29 stsp }
8464 87670572 2020-12-26 naddy tog_free_refs();
8465 ee85c5e8 2020-02-29 stsp return error;
8466 c2301be8 2018-04-30 stsp }
8467 c2301be8 2018-04-30 stsp
8468 9f7d7167 2018-04-29 stsp int
8469 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8470 9f7d7167 2018-04-29 stsp {
8471 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8472 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8473 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8474 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8475 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8476 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8477 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8478 83cd27f8 2020-01-13 stsp };
8479 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8480 9f7d7167 2018-04-29 stsp
8481 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
8482 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
8483 cacf1690 2022-09-06 thomas
8484 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8485 9f7d7167 2018-04-29 stsp
8486 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8487 9f7d7167 2018-04-29 stsp switch (ch) {
8488 9f7d7167 2018-04-29 stsp case 'h':
8489 9f7d7167 2018-04-29 stsp hflag = 1;
8490 9f7d7167 2018-04-29 stsp break;
8491 53ccebc2 2019-07-30 stsp case 'V':
8492 53ccebc2 2019-07-30 stsp Vflag = 1;
8493 53ccebc2 2019-07-30 stsp break;
8494 9f7d7167 2018-04-29 stsp default:
8495 6879ba42 2020-10-01 naddy usage(hflag, 1);
8496 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8497 9f7d7167 2018-04-29 stsp }
8498 9f7d7167 2018-04-29 stsp }
8499 9f7d7167 2018-04-29 stsp
8500 9f7d7167 2018-04-29 stsp argc -= optind;
8501 9f7d7167 2018-04-29 stsp argv += optind;
8502 9814e6a3 2020-09-27 naddy optind = 1;
8503 c2301be8 2018-04-30 stsp optreset = 1;
8504 9f7d7167 2018-04-29 stsp
8505 53ccebc2 2019-07-30 stsp if (Vflag) {
8506 53ccebc2 2019-07-30 stsp got_version_print_str();
8507 6879ba42 2020-10-01 naddy return 0;
8508 53ccebc2 2019-07-30 stsp }
8509 4010e238 2020-12-04 stsp
8510 4010e238 2020-12-04 stsp #ifndef PROFILE
8511 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8512 4010e238 2020-12-04 stsp NULL) == -1)
8513 4010e238 2020-12-04 stsp err(1, "pledge");
8514 4010e238 2020-12-04 stsp #endif
8515 53ccebc2 2019-07-30 stsp
8516 c2301be8 2018-04-30 stsp if (argc == 0) {
8517 f29d3e89 2018-06-23 stsp if (hflag)
8518 6879ba42 2020-10-01 naddy usage(hflag, 0);
8519 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8520 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8521 c2301be8 2018-04-30 stsp argc = 1;
8522 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8523 c2301be8 2018-04-30 stsp } else {
8524 6059809a 2020-12-17 stsp size_t i;
8525 9f7d7167 2018-04-29 stsp
8526 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8527 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8528 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8529 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8530 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8531 9f7d7167 2018-04-29 stsp break;
8532 9f7d7167 2018-04-29 stsp }
8533 9f7d7167 2018-04-29 stsp }
8534 ee85c5e8 2020-02-29 stsp }
8535 3642c4c6 2019-07-09 stsp
8536 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8537 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8538 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8539 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8540 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8541 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8542 adf4c9e0 2022-07-03 thomas }
8543 adf4c9e0 2022-07-03 thomas
8544 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8545 ee85c5e8 2020-02-29 stsp if (argc != 1)
8546 6879ba42 2020-10-01 naddy usage(0, 1);
8547 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8548 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8549 ee85c5e8 2020-02-29 stsp } else {
8550 ee85c5e8 2020-02-29 stsp if (hflag)
8551 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8552 ee85c5e8 2020-02-29 stsp else
8553 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8554 9f7d7167 2018-04-29 stsp }
8555 9f7d7167 2018-04-29 stsp
8556 9f7d7167 2018-04-29 stsp endwin();
8557 b46c1e04 2020-09-20 naddy putchar('\n');
8558 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8559 a2f4a359 2020-02-28 stsp int i;
8560 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8561 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8562 a2f4a359 2020-02-28 stsp free(cmd_argv);
8563 a2f4a359 2020-02-28 stsp }
8564 a2f4a359 2020-02-28 stsp
8565 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8566 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8567 9f7d7167 2018-04-29 stsp return 0;
8568 9f7d7167 2018-04-29 stsp }