Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 def2f970 2021-09-28 thomas #endif
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
44 9f7d7167 2018-04-29 stsp
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
46 dd038bc6 2021-09-21 thomas.ad
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 9f7d7167 2018-04-29 stsp
62 881b2d3e 2018-04-30 stsp #ifndef MIN
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 881b2d3e 2018-04-30 stsp #endif
65 881b2d3e 2018-04-30 stsp
66 2bd27830 2018-10-22 stsp #ifndef MAX
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #endif
69 2bd27830 2018-10-22 stsp
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
72 acf52a76 2021-09-21 thomas.ad #endif
73 2bd27830 2018-10-22 stsp
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 9f7d7167 2018-04-29 stsp #endif
77 9f7d7167 2018-04-29 stsp
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
82 9f7d7167 2018-04-29 stsp };
83 9f7d7167 2018-04-29 stsp
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 9f7d7167 2018-04-29 stsp
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 9f7d7167 2018-04-29 stsp
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
103 9f7d7167 2018-04-29 stsp };
104 9f7d7167 2018-04-29 stsp
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 d6b05b5b 2018-08-04 stsp };
112 c3e9aa98 2019-05-13 jcs
113 a5d43cac 2022-07-01 thomas enum tog_view_mode {
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
117 a5d43cac 2022-07-01 thomas };
118 a5d43cac 2022-07-01 thomas
119 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 a5d43cac 2022-07-01 thomas
121 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
122 d6b05b5b 2018-08-04 stsp
123 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
124 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
125 ba4f502b 2018-08-04 stsp struct got_object_id *id;
126 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
127 1a76625f 2018-10-22 stsp int idx;
128 ba4f502b 2018-08-04 stsp };
129 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 ba4f502b 2018-08-04 stsp struct commit_queue {
131 ba4f502b 2018-08-04 stsp int ncommits;
132 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
133 6d17833f 2019-11-08 stsp };
134 6d17833f 2019-11-08 stsp
135 f26dddb7 2019-11-08 stsp struct tog_color {
136 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
137 6d17833f 2019-11-08 stsp regex_t regex;
138 6d17833f 2019-11-08 stsp short colorpair;
139 15a087fe 2019-02-21 stsp };
140 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
141 11b20872 2019-11-08 stsp
142 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
144 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 2183bbf6 2022-01-23 thomas
146 2183bbf6 2022-01-23 thomas static const struct got_error *
147 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 2183bbf6 2022-01-23 thomas struct got_reference* re2)
149 2183bbf6 2022-01-23 thomas {
150 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
151 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
152 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
153 2183bbf6 2022-01-23 thomas
154 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
155 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
158 2183bbf6 2022-01-23 thomas *cmp = -1;
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
161 2183bbf6 2022-01-23 thomas *cmp = 1;
162 2183bbf6 2022-01-23 thomas return NULL;
163 2183bbf6 2022-01-23 thomas }
164 2183bbf6 2022-01-23 thomas
165 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 2183bbf6 2022-01-23 thomas return NULL;
167 2183bbf6 2022-01-23 thomas }
168 51a10b52 2020-12-26 stsp
169 11b20872 2019-11-08 stsp static const struct got_error *
170 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
171 51a10b52 2020-12-26 stsp {
172 51a10b52 2020-12-26 stsp const struct got_error *err;
173 51a10b52 2020-12-26 stsp
174 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 3bfadbd4 2021-11-20 thomas repo);
177 51a10b52 2020-12-26 stsp if (err)
178 51a10b52 2020-12-26 stsp return err;
179 51a10b52 2020-12-26 stsp
180 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 51a10b52 2020-12-26 stsp repo);
182 51a10b52 2020-12-26 stsp }
183 51a10b52 2020-12-26 stsp
184 51a10b52 2020-12-26 stsp static void
185 51a10b52 2020-12-26 stsp tog_free_refs(void)
186 51a10b52 2020-12-26 stsp {
187 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
188 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
189 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
190 51a10b52 2020-12-26 stsp }
191 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
192 51a10b52 2020-12-26 stsp }
193 51a10b52 2020-12-26 stsp
194 51a10b52 2020-12-26 stsp static const struct got_error *
195 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
196 11b20872 2019-11-08 stsp int idx, short color)
197 11b20872 2019-11-08 stsp {
198 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
199 11b20872 2019-11-08 stsp struct tog_color *tc;
200 11b20872 2019-11-08 stsp int regerr = 0;
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
203 11b20872 2019-11-08 stsp return NULL;
204 11b20872 2019-11-08 stsp
205 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
206 11b20872 2019-11-08 stsp
207 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
208 11b20872 2019-11-08 stsp if (tc == NULL)
209 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
210 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
211 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 11b20872 2019-11-08 stsp if (regerr) {
213 11b20872 2019-11-08 stsp static char regerr_msg[512];
214 11b20872 2019-11-08 stsp static char err_msg[512];
215 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
216 11b20872 2019-11-08 stsp sizeof(regerr_msg));
217 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 11b20872 2019-11-08 stsp regerr_msg);
219 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 11b20872 2019-11-08 stsp free(tc);
221 11b20872 2019-11-08 stsp return err;
222 11b20872 2019-11-08 stsp }
223 11b20872 2019-11-08 stsp tc->colorpair = idx;
224 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
225 11b20872 2019-11-08 stsp return NULL;
226 11b20872 2019-11-08 stsp }
227 11b20872 2019-11-08 stsp
228 11b20872 2019-11-08 stsp static void
229 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
230 11b20872 2019-11-08 stsp {
231 11b20872 2019-11-08 stsp struct tog_color *tc;
232 11b20872 2019-11-08 stsp
233 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
234 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
235 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
236 11b20872 2019-11-08 stsp regfree(&tc->regex);
237 11b20872 2019-11-08 stsp free(tc);
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 ef20f542 2022-06-26 thomas static struct tog_color *
242 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
247 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
248 11b20872 2019-11-08 stsp return tc;
249 11b20872 2019-11-08 stsp }
250 11b20872 2019-11-08 stsp
251 11b20872 2019-11-08 stsp return NULL;
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 11b20872 2019-11-08 stsp static int
255 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_CYAN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 11b20872 2019-11-08 stsp return COLOR_YELLOW;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 11b20872 2019-11-08 stsp return COLOR_CYAN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 11b20872 2019-11-08 stsp return COLOR_YELLOW;
279 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 6458efa5 2020-11-24 stsp return COLOR_GREEN;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
285 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
287 11b20872 2019-11-08 stsp
288 11b20872 2019-11-08 stsp return -1;
289 11b20872 2019-11-08 stsp }
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp static int
292 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
293 11b20872 2019-11-08 stsp {
294 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
295 11b20872 2019-11-08 stsp
296 11b20872 2019-11-08 stsp if (val == NULL)
297 11b20872 2019-11-08 stsp return default_color_value(envvar);
298 15a087fe 2019-02-21 stsp
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
300 11b20872 2019-11-08 stsp return COLOR_BLACK;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
302 11b20872 2019-11-08 stsp return COLOR_RED;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
304 11b20872 2019-11-08 stsp return COLOR_GREEN;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
306 11b20872 2019-11-08 stsp return COLOR_YELLOW;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLUE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
310 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
312 11b20872 2019-11-08 stsp return COLOR_CYAN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
314 11b20872 2019-11-08 stsp return COLOR_WHITE;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
316 11b20872 2019-11-08 stsp return -1;
317 11b20872 2019-11-08 stsp
318 11b20872 2019-11-08 stsp return default_color_value(envvar);
319 11b20872 2019-11-08 stsp }
320 11b20872 2019-11-08 stsp
321 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
322 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
323 3dbaef42 2020-11-24 stsp const char *label1, *label2;
324 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
325 19a6a6b5 2022-07-01 thomas int fd1, fd2;
326 82c78e96 2022-08-06 thomas int lineno;
327 15a087fe 2019-02-21 stsp int first_displayed_line;
328 15a087fe 2019-02-21 stsp int last_displayed_line;
329 15a087fe 2019-02-21 stsp int eof;
330 15a087fe 2019-02-21 stsp int diff_context;
331 3dbaef42 2020-11-24 stsp int ignore_whitespace;
332 64453f7e 2020-11-21 stsp int force_text_diff;
333 15a087fe 2019-02-21 stsp struct got_repository *repo;
334 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey int matched_line;
337 f44b1f58 2020-02-02 tracey int selected_line;
338 15a087fe 2019-02-21 stsp
339 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
340 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
341 b01e7d3b 2018-08-04 stsp };
342 b01e7d3b 2018-08-04 stsp
343 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
345 1a76625f 2018-10-22 stsp
346 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
347 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
348 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
349 1a76625f 2018-10-22 stsp int commits_needed;
350 fb280deb 2021-08-30 stsp int load_all;
351 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
352 1a76625f 2018-10-22 stsp struct commit_queue *commits;
353 1a76625f 2018-10-22 stsp const char *in_repo_path;
354 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
355 1a76625f 2018-10-22 stsp struct got_repository *repo;
356 1af5eddf 2022-06-23 thomas int *pack_fds;
357 1a76625f 2018-10-22 stsp int log_complete;
358 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
359 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
361 13add988 2019-10-15 stsp int *searching;
362 13add988 2019-10-15 stsp int *search_next_done;
363 13add988 2019-10-15 stsp regex_t *regex;
364 1a76625f 2018-10-22 stsp };
365 1a76625f 2018-10-22 stsp
366 1a76625f 2018-10-22 stsp struct tog_log_view_state {
367 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
368 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
371 b01e7d3b 2018-08-04 stsp int selected;
372 b01e7d3b 2018-08-04 stsp char *in_repo_path;
373 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
374 b672a97a 2020-01-27 stsp int log_branches;
375 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
376 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
377 1a76625f 2018-10-22 stsp sig_atomic_t quit;
378 1a76625f 2018-10-22 stsp pthread_t thread;
379 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
380 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
381 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
382 11b20872 2019-11-08 stsp struct tog_colors colors;
383 f69c5a46 2022-07-19 thomas int use_committer;
384 ba4f502b 2018-08-04 stsp };
385 11b20872 2019-11-08 stsp
386 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
390 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
394 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
395 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
396 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
397 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
400 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
401 ba4f502b 2018-08-04 stsp
402 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
403 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
404 e9424729 2018-08-04 stsp int nlines;
405 e9424729 2018-08-04 stsp
406 e9424729 2018-08-04 stsp struct tog_view *view;
407 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
408 e9424729 2018-08-04 stsp int *quit;
409 e9424729 2018-08-04 stsp };
410 e9424729 2018-08-04 stsp
411 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
412 e9424729 2018-08-04 stsp const char *path;
413 e9424729 2018-08-04 stsp struct got_repository *repo;
414 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
415 e9424729 2018-08-04 stsp int *complete;
416 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
417 fc06ba56 2019-08-22 stsp void *cancel_arg;
418 e9424729 2018-08-04 stsp };
419 e9424729 2018-08-04 stsp
420 e9424729 2018-08-04 stsp struct tog_blame {
421 e9424729 2018-08-04 stsp FILE *f;
422 be659d10 2020-11-18 stsp off_t filesize;
423 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
424 6fcac457 2018-11-19 stsp int nlines;
425 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
426 e9424729 2018-08-04 stsp pthread_t thread;
427 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
428 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
429 e9424729 2018-08-04 stsp const char *path;
430 7cd52833 2022-06-23 thomas int *pack_fds;
431 e9424729 2018-08-04 stsp };
432 e9424729 2018-08-04 stsp
433 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
434 7cbe629d 2018-08-04 stsp int first_displayed_line;
435 7cbe629d 2018-08-04 stsp int last_displayed_line;
436 7cbe629d 2018-08-04 stsp int selected_line;
437 4fc71f3b 2022-07-12 thomas int last_diffed_line;
438 7cbe629d 2018-08-04 stsp int blame_complete;
439 e5a0f69f 2018-08-18 stsp int eof;
440 e5a0f69f 2018-08-18 stsp int done;
441 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
442 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
443 e5a0f69f 2018-08-18 stsp char *path;
444 7cbe629d 2018-08-04 stsp struct got_repository *repo;
445 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
446 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
447 e9424729 2018-08-04 stsp struct tog_blame blame;
448 6c4c42e0 2019-06-24 stsp int matched_line;
449 11b20872 2019-11-08 stsp struct tog_colors colors;
450 ad80ab7b 2018-08-04 stsp };
451 ad80ab7b 2018-08-04 stsp
452 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
453 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
454 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 ad80ab7b 2018-08-04 stsp int selected;
458 ad80ab7b 2018-08-04 stsp };
459 ad80ab7b 2018-08-04 stsp
460 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
461 ad80ab7b 2018-08-04 stsp
462 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
463 ad80ab7b 2018-08-04 stsp char *tree_label;
464 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
465 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
466 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
469 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
470 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
471 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
473 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
474 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
475 6458efa5 2020-11-24 stsp struct tog_colors colors;
476 6458efa5 2020-11-24 stsp };
477 6458efa5 2020-11-24 stsp
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
479 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
480 6458efa5 2020-11-24 stsp struct got_reference *ref;
481 6458efa5 2020-11-24 stsp int idx;
482 6458efa5 2020-11-24 stsp };
483 6458efa5 2020-11-24 stsp
484 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
485 6458efa5 2020-11-24 stsp
486 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
487 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
490 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
491 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 6458efa5 2020-11-24 stsp struct got_repository *repo;
493 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
494 bddb1296 2019-11-08 stsp struct tog_colors colors;
495 7cbe629d 2018-08-04 stsp };
496 7cbe629d 2018-08-04 stsp
497 669b5ffa 2018-10-07 stsp /*
498 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
499 669b5ffa 2018-10-07 stsp *
500 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
501 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
502 669b5ffa 2018-10-07 stsp * there is enough screen estate.
503 669b5ffa 2018-10-07 stsp *
504 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
505 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
506 669b5ffa 2018-10-07 stsp *
507 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
508 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
509 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
510 669b5ffa 2018-10-07 stsp *
511 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
512 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
513 669b5ffa 2018-10-07 stsp */
514 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
515 669b5ffa 2018-10-07 stsp
516 cc3c9aac 2018-08-01 stsp struct tog_view {
517 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
518 26ed57b2 2018-05-19 stsp WINDOW *window;
519 26ed57b2 2018-05-19 stsp PANEL *panel;
520 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
522 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
523 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
524 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
526 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
527 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
528 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
529 9970f7fc 2020-12-03 stsp int dying;
530 669b5ffa 2018-10-07 stsp struct tog_view *parent;
531 669b5ffa 2018-10-07 stsp struct tog_view *child;
532 5dc9f4bc 2018-08-04 stsp
533 e78dc838 2020-12-04 stsp /*
534 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
535 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
536 e78dc838 2020-12-04 stsp * between parent and child.
537 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
538 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
539 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
540 e78dc838 2020-12-04 stsp * situations.
541 e78dc838 2020-12-04 stsp */
542 e78dc838 2020-12-04 stsp int focus_child;
543 e78dc838 2020-12-04 stsp
544 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
545 5dc9f4bc 2018-08-04 stsp /* type-specific state */
546 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
547 5dc9f4bc 2018-08-04 stsp union {
548 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
549 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
550 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
551 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
552 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
553 5dc9f4bc 2018-08-04 stsp } state;
554 e5a0f69f 2018-08-18 stsp
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
556 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
557 e78dc838 2020-12-04 stsp struct tog_view *, int);
558 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
559 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
560 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
561 60493ae3 2019-06-20 stsp
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
563 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
564 c0c4acc8 2021-01-24 stsp int search_started;
565 60493ae3 2019-06-20 stsp int searching;
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
567 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
568 60493ae3 2019-06-20 stsp int search_next_done;
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
570 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
571 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
572 1803e47f 2019-06-22 stsp regex_t regex;
573 41605754 2020-11-12 stsp regmatch_t regmatch;
574 cc3c9aac 2018-08-01 stsp };
575 cd0acaa7 2018-05-20 stsp
576 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
577 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
578 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
579 78756c87 2020-11-24 stsp struct got_repository *);
580 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
581 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
582 e78dc838 2020-12-04 stsp struct tog_view *, int);
583 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
584 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
586 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp
588 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
589 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
590 78756c87 2020-11-24 stsp const char *, const char *, int);
591 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
593 e78dc838 2020-12-04 stsp struct tog_view *, int);
594 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
597 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
598 e5a0f69f 2018-08-18 stsp
599 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
600 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
601 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
602 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
603 e78dc838 2020-12-04 stsp struct tog_view *, int);
604 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
605 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
607 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
608 e5a0f69f 2018-08-18 stsp
609 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
610 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
611 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
612 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
613 e78dc838 2020-12-04 stsp struct tog_view *, int);
614 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
616 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
617 6458efa5 2020-11-24 stsp
618 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
619 6458efa5 2020-11-24 stsp struct got_repository *);
620 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
621 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
622 e78dc838 2020-12-04 stsp struct tog_view *, int);
623 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
625 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
626 25791caa 2018-10-24 stsp
627 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
628 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
629 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
631 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
632 25791caa 2018-10-24 stsp
633 25791caa 2018-10-24 stsp static void
634 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
635 25791caa 2018-10-24 stsp {
636 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
637 83baff54 2019-08-12 stsp }
638 83baff54 2019-08-12 stsp
639 83baff54 2019-08-12 stsp static void
640 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
641 83baff54 2019-08-12 stsp {
642 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
643 25791caa 2018-10-24 stsp }
644 26ed57b2 2018-05-19 stsp
645 61266923 2020-01-14 stsp static void
646 61266923 2020-01-14 stsp tog_sigcont(int signo)
647 61266923 2020-01-14 stsp {
648 61266923 2020-01-14 stsp tog_sigcont_received = 1;
649 61266923 2020-01-14 stsp }
650 61266923 2020-01-14 stsp
651 296152d1 2022-05-31 thomas static void
652 296152d1 2022-05-31 thomas tog_sigint(int signo)
653 296152d1 2022-05-31 thomas {
654 296152d1 2022-05-31 thomas tog_sigint_received = 1;
655 296152d1 2022-05-31 thomas }
656 296152d1 2022-05-31 thomas
657 296152d1 2022-05-31 thomas static void
658 296152d1 2022-05-31 thomas tog_sigterm(int signo)
659 296152d1 2022-05-31 thomas {
660 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
661 296152d1 2022-05-31 thomas }
662 296152d1 2022-05-31 thomas
663 296152d1 2022-05-31 thomas static int
664 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
665 296152d1 2022-05-31 thomas {
666 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
667 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
668 296152d1 2022-05-31 thomas }
669 296152d1 2022-05-31 thomas
670 e5a0f69f 2018-08-18 stsp static const struct got_error *
671 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
672 ea5e7bb5 2018-08-01 stsp {
673 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
674 e5a0f69f 2018-08-18 stsp
675 669b5ffa 2018-10-07 stsp if (view->child) {
676 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
677 669b5ffa 2018-10-07 stsp view->child = NULL;
678 669b5ffa 2018-10-07 stsp }
679 e5a0f69f 2018-08-18 stsp if (view->close)
680 e5a0f69f 2018-08-18 stsp err = view->close(view);
681 ea5e7bb5 2018-08-01 stsp if (view->panel)
682 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
683 ea5e7bb5 2018-08-01 stsp if (view->window)
684 ea5e7bb5 2018-08-01 stsp delwin(view->window);
685 ea5e7bb5 2018-08-01 stsp free(view);
686 f2d749db 2022-07-12 thomas return err ? err : child_err;
687 ea5e7bb5 2018-08-01 stsp }
688 ea5e7bb5 2018-08-01 stsp
689 ea5e7bb5 2018-08-01 stsp static struct tog_view *
690 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
691 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
692 ea5e7bb5 2018-08-01 stsp {
693 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
694 ea5e7bb5 2018-08-01 stsp
695 ea5e7bb5 2018-08-01 stsp if (view == NULL)
696 ea5e7bb5 2018-08-01 stsp return NULL;
697 ea5e7bb5 2018-08-01 stsp
698 d6b05b5b 2018-08-04 stsp view->type = type;
699 f7d12f7e 2018-08-01 stsp view->lines = LINES;
700 f7d12f7e 2018-08-01 stsp view->cols = COLS;
701 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
702 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
703 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
704 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
705 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
706 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
709 ea5e7bb5 2018-08-01 stsp }
710 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
711 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
712 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
713 96a765a8 2018-08-04 stsp view_close(view);
714 ea5e7bb5 2018-08-01 stsp return NULL;
715 ea5e7bb5 2018-08-01 stsp }
716 ea5e7bb5 2018-08-01 stsp
717 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
718 ea5e7bb5 2018-08-01 stsp return view;
719 cdf1ee82 2018-08-01 stsp }
720 cdf1ee82 2018-08-01 stsp
721 0cf4efb1 2018-09-29 stsp static int
722 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
723 0cf4efb1 2018-09-29 stsp {
724 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
725 0cf4efb1 2018-09-29 stsp return 0;
726 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
727 5c60c32a 2018-10-18 stsp }
728 5c60c32a 2018-10-18 stsp
729 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
730 a5d43cac 2022-07-01 thomas static int
731 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
732 a5d43cac 2022-07-01 thomas {
733 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
734 a5d43cac 2022-07-01 thomas }
735 a5d43cac 2022-07-01 thomas
736 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
737 5c60c32a 2018-10-18 stsp
738 5c60c32a 2018-10-18 stsp static const struct got_error *
739 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
740 5c60c32a 2018-10-18 stsp {
741 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
742 5c60c32a 2018-10-18 stsp
743 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
744 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
745 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
746 53d2bdd3 2022-07-10 thomas else
747 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
748 a5d43cac 2022-07-01 thomas view->begin_x = 0;
749 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
750 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
751 53d2bdd3 2022-07-10 thomas view->cols > 119)
752 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
753 53d2bdd3 2022-07-10 thomas else
754 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
755 a5d43cac 2022-07-01 thomas view->begin_y = 0;
756 a5d43cac 2022-07-01 thomas }
757 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
758 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
759 5c60c32a 2018-10-18 stsp view->lines = LINES;
760 5c60c32a 2018-10-18 stsp view->cols = COLS;
761 5c60c32a 2018-10-18 stsp err = view_resize(view);
762 5c60c32a 2018-10-18 stsp if (err)
763 5c60c32a 2018-10-18 stsp return err;
764 5c60c32a 2018-10-18 stsp
765 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
766 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
767 a5d43cac 2022-07-01 thomas
768 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
769 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
770 5c60c32a 2018-10-18 stsp
771 5c60c32a 2018-10-18 stsp return NULL;
772 5c60c32a 2018-10-18 stsp }
773 5c60c32a 2018-10-18 stsp
774 5c60c32a 2018-10-18 stsp static const struct got_error *
775 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
776 5c60c32a 2018-10-18 stsp {
777 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
778 5c60c32a 2018-10-18 stsp
779 5c60c32a 2018-10-18 stsp view->begin_x = 0;
780 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
781 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
782 5c60c32a 2018-10-18 stsp view->ncols = COLS;
783 5c60c32a 2018-10-18 stsp view->lines = LINES;
784 5c60c32a 2018-10-18 stsp view->cols = COLS;
785 5c60c32a 2018-10-18 stsp err = view_resize(view);
786 5c60c32a 2018-10-18 stsp if (err)
787 5c60c32a 2018-10-18 stsp return err;
788 5c60c32a 2018-10-18 stsp
789 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
790 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
791 5c60c32a 2018-10-18 stsp
792 5c60c32a 2018-10-18 stsp return NULL;
793 0cf4efb1 2018-09-29 stsp }
794 0cf4efb1 2018-09-29 stsp
795 5c60c32a 2018-10-18 stsp static int
796 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
797 5c60c32a 2018-10-18 stsp {
798 5c60c32a 2018-10-18 stsp return view->parent == NULL;
799 5c60c32a 2018-10-18 stsp }
800 5c60c32a 2018-10-18 stsp
801 b65b3ea0 2022-06-23 thomas static int
802 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
803 b65b3ea0 2022-06-23 thomas {
804 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
805 e44940c3 2022-07-01 thomas }
806 e44940c3 2022-07-01 thomas
807 e44940c3 2022-07-01 thomas static int
808 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
809 e44940c3 2022-07-01 thomas {
810 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
811 b65b3ea0 2022-06-23 thomas }
812 b65b3ea0 2022-06-23 thomas
813 444d5325 2022-07-03 thomas static int
814 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
815 444d5325 2022-07-03 thomas {
816 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
817 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
818 444d5325 2022-07-03 thomas }
819 444d5325 2022-07-03 thomas
820 a5d43cac 2022-07-01 thomas static void
821 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
822 a5d43cac 2022-07-01 thomas {
823 a5d43cac 2022-07-01 thomas PANEL *panel;
824 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
825 b65b3ea0 2022-06-23 thomas
826 a5d43cac 2022-07-01 thomas if (view->parent)
827 a5d43cac 2022-07-01 thomas return view_border(view->parent);
828 a5d43cac 2022-07-01 thomas
829 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
830 a5d43cac 2022-07-01 thomas if (panel == NULL)
831 a5d43cac 2022-07-01 thomas return;
832 a5d43cac 2022-07-01 thomas
833 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
834 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
835 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
836 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
837 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
838 a5d43cac 2022-07-01 thomas else
839 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
840 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
841 a5d43cac 2022-07-01 thomas }
842 a5d43cac 2022-07-01 thomas
843 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
844 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
846 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
847 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
848 a5d43cac 2022-07-01 thomas
849 4d8c2215 2018-08-19 stsp static const struct got_error *
850 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
851 f7d12f7e 2018-08-01 stsp {
852 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
853 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
854 f7d12f7e 2018-08-01 stsp
855 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
856 a5d43cac 2022-07-01 thomas
857 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
858 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
859 0cf4efb1 2018-09-29 stsp else
860 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
861 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
862 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
863 0cf4efb1 2018-09-29 stsp else
864 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
865 6d0fee91 2018-08-01 stsp
866 4918811f 2022-07-01 thomas if (view->child) {
867 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
868 a5d43cac 2022-07-01 thomas
869 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
870 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
871 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
872 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
873 40236d76 2022-06-23 thomas ncols = COLS;
874 40236d76 2022-06-23 thomas
875 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
876 5c60c32a 2018-10-18 stsp if (view->child->focussed)
877 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
878 5c60c32a 2018-10-18 stsp else
879 5c60c32a 2018-10-18 stsp show_panel(view->panel);
880 5c60c32a 2018-10-18 stsp } else {
881 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
882 40236d76 2022-06-23 thomas
883 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
884 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
885 a5d43cac 2022-07-01 thomas }
886 a5d43cac 2022-07-01 thomas /*
887 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
888 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
889 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
890 a5d43cac 2022-07-01 thomas */
891 a5d43cac 2022-07-01 thomas if (hs) {
892 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
893 a5d43cac 2022-07-01 thomas if (err)
894 a5d43cac 2022-07-01 thomas return err;
895 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
896 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
897 a5d43cac 2022-07-01 thomas if (err)
898 a5d43cac 2022-07-01 thomas return err;
899 a5d43cac 2022-07-01 thomas }
900 a5d43cac 2022-07-01 thomas view_border(view);
901 a5d43cac 2022-07-01 thomas update_panels();
902 a5d43cac 2022-07-01 thomas doupdate();
903 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
904 a5d43cac 2022-07-01 thomas nlines = view->nlines;
905 a5d43cac 2022-07-01 thomas }
906 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
907 40236d76 2022-06-23 thomas ncols = COLS;
908 669b5ffa 2018-10-07 stsp
909 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
910 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
911 ea0bff04 2022-07-19 thomas if (err)
912 ea0bff04 2022-07-19 thomas return err;
913 ea0bff04 2022-07-19 thomas }
914 ea0bff04 2022-07-19 thomas
915 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
916 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
917 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
918 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
919 40236d76 2022-06-23 thomas wclear(view->window);
920 40236d76 2022-06-23 thomas
921 40236d76 2022-06-23 thomas view->nlines = nlines;
922 40236d76 2022-06-23 thomas view->ncols = ncols;
923 40236d76 2022-06-23 thomas view->lines = LINES;
924 40236d76 2022-06-23 thomas view->cols = COLS;
925 40236d76 2022-06-23 thomas
926 5c60c32a 2018-10-18 stsp return NULL;
927 ea0bff04 2022-07-19 thomas }
928 ea0bff04 2022-07-19 thomas
929 ea0bff04 2022-07-19 thomas static const struct got_error *
930 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
931 ea0bff04 2022-07-19 thomas {
932 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
933 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
934 3a0139e8 2022-07-22 thomas int n = 0;
935 ea0bff04 2022-07-19 thomas
936 3a0139e8 2022-07-22 thomas if (s->selected_entry)
937 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
938 3a0139e8 2022-07-22 thomas
939 ea0bff04 2022-07-19 thomas /*
940 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
941 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
942 ea0bff04 2022-07-19 thomas */
943 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
944 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
945 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
946 ea0bff04 2022-07-19 thomas }
947 ea0bff04 2022-07-19 thomas
948 ea0bff04 2022-07-19 thomas return err;
949 97cb21cd 2022-07-12 thomas }
950 97cb21cd 2022-07-12 thomas
951 97cb21cd 2022-07-12 thomas static void
952 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
953 97cb21cd 2022-07-12 thomas {
954 97cb21cd 2022-07-12 thomas if (n == 0)
955 97cb21cd 2022-07-12 thomas return;
956 97cb21cd 2022-07-12 thomas
957 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
958 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
959 97cb21cd 2022-07-12 thomas view->parent->offset += n;
960 97cb21cd 2022-07-12 thomas else
961 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
962 97cb21cd 2022-07-12 thomas } else if (view->offset) {
963 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
964 97cb21cd 2022-07-12 thomas view->offset -= n;
965 97cb21cd 2022-07-12 thomas else
966 97cb21cd 2022-07-12 thomas view->offset = 0;
967 97cb21cd 2022-07-12 thomas }
968 53d2bdd3 2022-07-10 thomas }
969 53d2bdd3 2022-07-10 thomas
970 53d2bdd3 2022-07-10 thomas static const struct got_error *
971 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
972 53d2bdd3 2022-07-10 thomas {
973 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
974 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
975 53d2bdd3 2022-07-10 thomas
976 53d2bdd3 2022-07-10 thomas if (view->parent)
977 53d2bdd3 2022-07-10 thomas v = view->parent;
978 53d2bdd3 2022-07-10 thomas else
979 53d2bdd3 2022-07-10 thomas v = view;
980 53d2bdd3 2022-07-10 thomas
981 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
982 53d2bdd3 2022-07-10 thomas return NULL;
983 53d2bdd3 2022-07-10 thomas
984 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
985 53d2bdd3 2022-07-10 thomas
986 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
987 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
988 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
989 53d2bdd3 2022-07-10 thomas if (view->parent)
990 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
991 53d2bdd3 2022-07-10 thomas else
992 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
993 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
994 53d2bdd3 2022-07-10 thomas view->count = 0;
995 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
996 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
997 53d2bdd3 2022-07-10 thomas view->count = 0;
998 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
999 53d2bdd3 2022-07-10 thomas }
1000 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1001 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1002 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1003 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1004 53d2bdd3 2022-07-10 thomas if (err)
1005 53d2bdd3 2022-07-10 thomas return err;
1006 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1007 53d2bdd3 2022-07-10 thomas } else {
1008 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1009 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1010 53d2bdd3 2022-07-10 thomas if (view->parent)
1011 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1012 53d2bdd3 2022-07-10 thomas else
1013 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1014 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1015 53d2bdd3 2022-07-10 thomas view->count = 0;
1016 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1017 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1018 53d2bdd3 2022-07-10 thomas view->count = 0;
1019 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1020 53d2bdd3 2022-07-10 thomas }
1021 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1022 53d2bdd3 2022-07-10 thomas }
1023 53d2bdd3 2022-07-10 thomas
1024 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1025 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1026 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1027 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1028 53d2bdd3 2022-07-10 thomas
1029 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1030 53d2bdd3 2022-07-10 thomas if (err)
1031 53d2bdd3 2022-07-10 thomas return err;
1032 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1033 53d2bdd3 2022-07-10 thomas if (err)
1034 53d2bdd3 2022-07-10 thomas return err;
1035 53d2bdd3 2022-07-10 thomas
1036 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1037 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1038 53d2bdd3 2022-07-10 thomas if (err)
1039 53d2bdd3 2022-07-10 thomas return err;
1040 53d2bdd3 2022-07-10 thomas }
1041 53d2bdd3 2022-07-10 thomas
1042 3a0139e8 2022-07-22 thomas if (v->resize)
1043 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1044 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1045 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1046 53d2bdd3 2022-07-10 thomas
1047 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1048 53d2bdd3 2022-07-10 thomas
1049 53d2bdd3 2022-07-10 thomas return err;
1050 53d2bdd3 2022-07-10 thomas }
1051 53d2bdd3 2022-07-10 thomas
1052 53d2bdd3 2022-07-10 thomas static void
1053 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1054 53d2bdd3 2022-07-10 thomas {
1055 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1056 53d2bdd3 2022-07-10 thomas
1057 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1058 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1059 669b5ffa 2018-10-07 stsp }
1060 669b5ffa 2018-10-07 stsp
1061 669b5ffa 2018-10-07 stsp static const struct got_error *
1062 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1063 669b5ffa 2018-10-07 stsp {
1064 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1065 669b5ffa 2018-10-07 stsp
1066 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1067 669b5ffa 2018-10-07 stsp return NULL;
1068 669b5ffa 2018-10-07 stsp
1069 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1070 669b5ffa 2018-10-07 stsp view->child = NULL;
1071 669b5ffa 2018-10-07 stsp return err;
1072 669b5ffa 2018-10-07 stsp }
1073 669b5ffa 2018-10-07 stsp
1074 40236d76 2022-06-23 thomas static const struct got_error *
1075 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1076 669b5ffa 2018-10-07 stsp {
1077 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1078 53d2bdd3 2022-07-10 thomas
1079 669b5ffa 2018-10-07 stsp view->child = child;
1080 669b5ffa 2018-10-07 stsp child->parent = view;
1081 40236d76 2022-06-23 thomas
1082 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1083 53d2bdd3 2022-07-10 thomas if (err)
1084 53d2bdd3 2022-07-10 thomas return err;
1085 53d2bdd3 2022-07-10 thomas
1086 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1087 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1088 53d2bdd3 2022-07-10 thomas
1089 53d2bdd3 2022-07-10 thomas return err;
1090 bfddd0d9 2018-09-29 stsp }
1091 2a31b33b 2022-07-23 thomas
1092 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1093 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1094 2a31b33b 2022-07-23 thomas
1095 2a31b33b 2022-07-23 thomas static const struct got_error *
1096 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1097 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1098 2a31b33b 2022-07-23 thomas {
1099 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1100 2a31b33b 2022-07-23 thomas const struct got_error *err;
1101 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1102 2a31b33b 2022-07-23 thomas
1103 2a31b33b 2022-07-23 thomas *requested = NULL;
1104 2a31b33b 2022-07-23 thomas
1105 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view))
1106 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1107 bfddd0d9 2018-09-29 stsp
1108 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1109 2a31b33b 2022-07-23 thomas if (err)
1110 2a31b33b 2022-07-23 thomas return err;
1111 2a31b33b 2022-07-23 thomas
1112 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1113 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1114 2a31b33b 2022-07-23 thomas if (err)
1115 2a31b33b 2022-07-23 thomas return err;
1116 2a31b33b 2022-07-23 thomas }
1117 2a31b33b 2022-07-23 thomas
1118 2a31b33b 2022-07-23 thomas view->focussed = 0;
1119 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1120 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1121 2a31b33b 2022-07-23 thomas new_view->nlines = view->lines - y;
1122 2a31b33b 2022-07-23 thomas
1123 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view)) {
1124 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1125 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1126 2a31b33b 2022-07-23 thomas if (err)
1127 2a31b33b 2022-07-23 thomas return err;
1128 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1129 2a31b33b 2022-07-23 thomas if (err)
1130 2a31b33b 2022-07-23 thomas return err;
1131 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1132 2a31b33b 2022-07-23 thomas } else
1133 2a31b33b 2022-07-23 thomas *requested = new_view;
1134 2a31b33b 2022-07-23 thomas
1135 2a31b33b 2022-07-23 thomas return NULL;
1136 2a31b33b 2022-07-23 thomas }
1137 2a31b33b 2022-07-23 thomas
1138 34bc9ec9 2019-02-22 stsp static void
1139 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1140 25791caa 2018-10-24 stsp {
1141 25791caa 2018-10-24 stsp int cols, lines;
1142 25791caa 2018-10-24 stsp struct winsize size;
1143 25791caa 2018-10-24 stsp
1144 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1145 25791caa 2018-10-24 stsp cols = 80; /* Default */
1146 25791caa 2018-10-24 stsp lines = 24;
1147 25791caa 2018-10-24 stsp } else {
1148 25791caa 2018-10-24 stsp cols = size.ws_col;
1149 25791caa 2018-10-24 stsp lines = size.ws_row;
1150 25791caa 2018-10-24 stsp }
1151 25791caa 2018-10-24 stsp resize_term(lines, cols);
1152 2b49a8ae 2019-06-22 stsp }
1153 2b49a8ae 2019-06-22 stsp
1154 2b49a8ae 2019-06-22 stsp static const struct got_error *
1155 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1156 2b49a8ae 2019-06-22 stsp {
1157 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1158 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1159 2b49a8ae 2019-06-22 stsp char pattern[1024];
1160 2b49a8ae 2019-06-22 stsp int ret;
1161 c0c4acc8 2021-01-24 stsp
1162 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1163 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1164 c0c4acc8 2021-01-24 stsp view->searching = 0;
1165 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1166 c0c4acc8 2021-01-24 stsp }
1167 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1168 2b49a8ae 2019-06-22 stsp
1169 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1170 2b49a8ae 2019-06-22 stsp return NULL;
1171 2b49a8ae 2019-06-22 stsp
1172 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1173 a5d43cac 2022-07-01 thomas v = view->child;
1174 2b49a8ae 2019-06-22 stsp
1175 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1176 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1177 a5d43cac 2022-07-01 thomas
1178 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1179 2b49a8ae 2019-06-22 stsp nocbreak();
1180 2b49a8ae 2019-06-22 stsp echo();
1181 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1182 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1183 2b49a8ae 2019-06-22 stsp cbreak();
1184 2b49a8ae 2019-06-22 stsp noecho();
1185 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1186 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1187 2b49a8ae 2019-06-22 stsp return NULL;
1188 2b49a8ae 2019-06-22 stsp
1189 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1190 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1191 7c32bd05 2019-06-22 stsp if (err) {
1192 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1193 7c32bd05 2019-06-22 stsp return err;
1194 7c32bd05 2019-06-22 stsp }
1195 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1196 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1197 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1198 2b49a8ae 2019-06-22 stsp view->search_next(view);
1199 2b49a8ae 2019-06-22 stsp }
1200 2b49a8ae 2019-06-22 stsp
1201 2b49a8ae 2019-06-22 stsp return NULL;
1202 64486692 2022-07-07 thomas }
1203 64486692 2022-07-07 thomas
1204 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1205 64486692 2022-07-07 thomas static const struct got_error *
1206 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1207 64486692 2022-07-07 thomas {
1208 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1209 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1210 64486692 2022-07-07 thomas
1211 64486692 2022-07-07 thomas if (view->parent)
1212 64486692 2022-07-07 thomas v = view->parent;
1213 64486692 2022-07-07 thomas else
1214 64486692 2022-07-07 thomas v = view;
1215 64486692 2022-07-07 thomas
1216 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1217 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1218 ddbc4d37 2022-07-12 thomas else
1219 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1220 64486692 2022-07-07 thomas
1221 ddbc4d37 2022-07-12 thomas if (!v->child)
1222 ddbc4d37 2022-07-12 thomas return NULL;
1223 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1224 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1225 ddbc4d37 2022-07-12 thomas
1226 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1227 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1228 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1229 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1230 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1231 64486692 2022-07-07 thomas
1232 ddbc4d37 2022-07-12 thomas
1233 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1234 64486692 2022-07-07 thomas v->ncols = COLS;
1235 64486692 2022-07-07 thomas v->child->ncols = COLS;
1236 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1237 64486692 2022-07-07 thomas
1238 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1239 64486692 2022-07-07 thomas if (err)
1240 64486692 2022-07-07 thomas return err;
1241 64486692 2022-07-07 thomas }
1242 64486692 2022-07-07 thomas v->child->mode = v->mode;
1243 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1244 64486692 2022-07-07 thomas v->focus_child = 1;
1245 64486692 2022-07-07 thomas
1246 64486692 2022-07-07 thomas err = view_fullscreen(v);
1247 64486692 2022-07-07 thomas if (err)
1248 64486692 2022-07-07 thomas return err;
1249 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1250 64486692 2022-07-07 thomas if (err)
1251 64486692 2022-07-07 thomas return err;
1252 64486692 2022-07-07 thomas
1253 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1254 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1255 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1256 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1257 cf1fe301 2022-07-29 thomas if (err)
1258 cf1fe301 2022-07-29 thomas return err;
1259 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1260 cf1fe301 2022-07-29 thomas if (err)
1261 cf1fe301 2022-07-29 thomas return err;
1262 ddbc4d37 2022-07-12 thomas } else {
1263 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1264 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1265 64486692 2022-07-07 thomas }
1266 f4e6231a 2022-07-22 thomas if (v->resize)
1267 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1268 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1269 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1270 64486692 2022-07-07 thomas
1271 64486692 2022-07-07 thomas return err;
1272 0cf4efb1 2018-09-29 stsp }
1273 6d0fee91 2018-08-01 stsp
1274 07b0611c 2022-06-23 thomas /*
1275 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1276 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1277 07b0611c 2022-06-23 thomas */
1278 07b0611c 2022-06-23 thomas static int
1279 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1280 07b0611c 2022-06-23 thomas {
1281 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1282 a5d43cac 2022-07-01 thomas int x, n = 0;
1283 07b0611c 2022-06-23 thomas
1284 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1285 a5d43cac 2022-07-01 thomas v = view->child;
1286 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1287 a5d43cac 2022-07-01 thomas v = view->parent;
1288 a5d43cac 2022-07-01 thomas
1289 07b0611c 2022-06-23 thomas view->count = 0;
1290 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1291 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1292 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1293 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1294 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1295 07b0611c 2022-06-23 thomas
1296 07b0611c 2022-06-23 thomas do {
1297 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1298 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1299 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1300 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1301 a5d43cac 2022-07-01 thomas }
1302 a5d43cac 2022-07-01 thomas
1303 07b0611c 2022-06-23 thomas /*
1304 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1305 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1306 07b0611c 2022-06-23 thomas */
1307 07b0611c 2022-06-23 thomas if (n >= 9999999)
1308 07b0611c 2022-06-23 thomas n = 9999999;
1309 07b0611c 2022-06-23 thomas else
1310 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1311 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1312 07b0611c 2022-06-23 thomas
1313 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1314 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1315 07dd3ed3 2022-08-06 thomas n = 0;
1316 07dd3ed3 2022-08-06 thomas c = 0;
1317 07dd3ed3 2022-08-06 thomas }
1318 07dd3ed3 2022-08-06 thomas
1319 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1320 07b0611c 2022-06-23 thomas view->count = n;
1321 07b0611c 2022-06-23 thomas
1322 07b0611c 2022-06-23 thomas return c;
1323 07b0611c 2022-06-23 thomas }
1324 07b0611c 2022-06-23 thomas
1325 0cf4efb1 2018-09-29 stsp static const struct got_error *
1326 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1327 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1328 e5a0f69f 2018-08-18 stsp {
1329 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1330 669b5ffa 2018-10-07 stsp struct tog_view *v;
1331 1a76625f 2018-10-22 stsp int ch, errcode;
1332 e5a0f69f 2018-08-18 stsp
1333 e5a0f69f 2018-08-18 stsp *new = NULL;
1334 8f4ed634 2020-03-26 stsp
1335 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1336 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1337 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1338 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1339 07b0611c 2022-06-23 thomas view->count = 0;
1340 07b0611c 2022-06-23 thomas }
1341 e5a0f69f 2018-08-18 stsp
1342 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1343 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1344 82954512 2020-02-03 stsp if (errcode)
1345 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1346 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1347 3da8ef85 2021-09-21 stsp sched_yield();
1348 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1349 82954512 2020-02-03 stsp if (errcode)
1350 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1351 82954512 2020-02-03 stsp "pthread_mutex_lock");
1352 60493ae3 2019-06-20 stsp view->search_next(view);
1353 60493ae3 2019-06-20 stsp return NULL;
1354 60493ae3 2019-06-20 stsp }
1355 60493ae3 2019-06-20 stsp
1356 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1357 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1358 1a76625f 2018-10-22 stsp if (errcode)
1359 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1360 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1361 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1362 634cb454 2022-07-03 thomas cbreak();
1363 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1364 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1365 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1366 634cb454 2022-07-03 thomas view->count = 0;
1367 634cb454 2022-07-03 thomas else
1368 634cb454 2022-07-03 thomas ch = view->ch;
1369 634cb454 2022-07-03 thomas } else {
1370 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1371 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1372 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1373 07b0611c 2022-06-23 thomas }
1374 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1375 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1376 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1377 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1378 1a76625f 2018-10-22 stsp if (errcode)
1379 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1380 25791caa 2018-10-24 stsp
1381 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1382 25791caa 2018-10-24 stsp tog_resizeterm();
1383 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1384 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1385 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1386 25791caa 2018-10-24 stsp err = view_resize(v);
1387 25791caa 2018-10-24 stsp if (err)
1388 25791caa 2018-10-24 stsp return err;
1389 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1390 25791caa 2018-10-24 stsp if (err)
1391 25791caa 2018-10-24 stsp return err;
1392 cdfcfb03 2020-12-06 stsp if (v->child) {
1393 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1394 cdfcfb03 2020-12-06 stsp if (err)
1395 cdfcfb03 2020-12-06 stsp return err;
1396 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1397 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1398 cdfcfb03 2020-12-06 stsp if (err)
1399 cdfcfb03 2020-12-06 stsp return err;
1400 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1401 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1402 53d2bdd3 2022-07-10 thomas if (err)
1403 53d2bdd3 2022-07-10 thomas return err;
1404 53d2bdd3 2022-07-10 thomas }
1405 cdfcfb03 2020-12-06 stsp }
1406 25791caa 2018-10-24 stsp }
1407 25791caa 2018-10-24 stsp }
1408 25791caa 2018-10-24 stsp
1409 e5a0f69f 2018-08-18 stsp switch (ch) {
1410 1e37a5c2 2019-05-12 jcs case '\t':
1411 07b0611c 2022-06-23 thomas view->count = 0;
1412 1e37a5c2 2019-05-12 jcs if (view->child) {
1413 e78dc838 2020-12-04 stsp view->focussed = 0;
1414 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1415 e78dc838 2020-12-04 stsp view->focus_child = 1;
1416 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1417 e78dc838 2020-12-04 stsp view->focussed = 0;
1418 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1419 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1420 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1421 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1422 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1423 3a0139e8 2022-07-22 thomas 0);
1424 a5d43cac 2022-07-01 thomas if (err)
1425 a5d43cac 2022-07-01 thomas return err;
1426 a5d43cac 2022-07-01 thomas }
1427 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1428 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1429 a5d43cac 2022-07-01 thomas if (err)
1430 a5d43cac 2022-07-01 thomas return err;
1431 a5d43cac 2022-07-01 thomas }
1432 1e37a5c2 2019-05-12 jcs }
1433 1e37a5c2 2019-05-12 jcs break;
1434 1e37a5c2 2019-05-12 jcs case 'q':
1435 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1436 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1437 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1438 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1439 a5d43cac 2022-07-01 thomas if (err)
1440 a5d43cac 2022-07-01 thomas break;
1441 a5d43cac 2022-07-01 thomas }
1442 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1443 a5d43cac 2022-07-01 thomas }
1444 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1445 9970f7fc 2020-12-03 stsp view->dying = 1;
1446 1e37a5c2 2019-05-12 jcs break;
1447 1e37a5c2 2019-05-12 jcs case 'Q':
1448 1e37a5c2 2019-05-12 jcs *done = 1;
1449 1e37a5c2 2019-05-12 jcs break;
1450 1c5e5faa 2022-06-23 thomas case 'F':
1451 07b0611c 2022-06-23 thomas view->count = 0;
1452 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1453 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1454 1e37a5c2 2019-05-12 jcs break;
1455 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1456 e78dc838 2020-12-04 stsp view->focussed = 0;
1457 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1458 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1459 53d2bdd3 2022-07-10 thomas } else {
1460 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1461 53d2bdd3 2022-07-10 thomas if (!err)
1462 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1463 53d2bdd3 2022-07-10 thomas }
1464 1e37a5c2 2019-05-12 jcs if (err)
1465 1e37a5c2 2019-05-12 jcs break;
1466 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1467 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1468 1e37a5c2 2019-05-12 jcs } else {
1469 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1470 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1471 e78dc838 2020-12-04 stsp view->focussed = 1;
1472 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1473 1e37a5c2 2019-05-12 jcs } else {
1474 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1475 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1476 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1477 53d2bdd3 2022-07-10 thomas if (!err)
1478 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1479 669b5ffa 2018-10-07 stsp }
1480 1e37a5c2 2019-05-12 jcs if (err)
1481 1e37a5c2 2019-05-12 jcs break;
1482 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1483 a5d43cac 2022-07-01 thomas }
1484 a5d43cac 2022-07-01 thomas if (err)
1485 a5d43cac 2022-07-01 thomas break;
1486 3a0139e8 2022-07-22 thomas if (view->resize) {
1487 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1488 a5d43cac 2022-07-01 thomas if (err)
1489 a5d43cac 2022-07-01 thomas break;
1490 1e37a5c2 2019-05-12 jcs }
1491 a5d43cac 2022-07-01 thomas if (view->parent)
1492 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1493 a5d43cac 2022-07-01 thomas if (!err)
1494 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1495 1e37a5c2 2019-05-12 jcs break;
1496 64486692 2022-07-07 thomas case 'S':
1497 53d2bdd3 2022-07-10 thomas view->count = 0;
1498 64486692 2022-07-07 thomas err = switch_split(view);
1499 53d2bdd3 2022-07-10 thomas break;
1500 53d2bdd3 2022-07-10 thomas case '-':
1501 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1502 64486692 2022-07-07 thomas break;
1503 53d2bdd3 2022-07-10 thomas case '+':
1504 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1505 53d2bdd3 2022-07-10 thomas break;
1506 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1507 60493ae3 2019-06-20 stsp break;
1508 60493ae3 2019-06-20 stsp case '/':
1509 07b0611c 2022-06-23 thomas view->count = 0;
1510 60493ae3 2019-06-20 stsp if (view->search_start)
1511 2b49a8ae 2019-06-22 stsp view_search_start(view);
1512 60493ae3 2019-06-20 stsp else
1513 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1514 1e37a5c2 2019-05-12 jcs break;
1515 b1bf1435 2019-06-21 stsp case 'N':
1516 60493ae3 2019-06-20 stsp case 'n':
1517 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1518 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1519 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1520 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1521 60493ae3 2019-06-20 stsp view->search_next(view);
1522 60493ae3 2019-06-20 stsp } else
1523 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1524 adf4c9e0 2022-07-03 thomas break;
1525 adf4c9e0 2022-07-03 thomas case 'A':
1526 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1527 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1528 adf4c9e0 2022-07-03 thomas else
1529 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1530 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1531 adf4c9e0 2022-07-03 thomas if (v->reset) {
1532 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1533 adf4c9e0 2022-07-03 thomas if (err)
1534 adf4c9e0 2022-07-03 thomas return err;
1535 adf4c9e0 2022-07-03 thomas }
1536 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1537 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1538 adf4c9e0 2022-07-03 thomas if (err)
1539 adf4c9e0 2022-07-03 thomas return err;
1540 adf4c9e0 2022-07-03 thomas }
1541 adf4c9e0 2022-07-03 thomas }
1542 60493ae3 2019-06-20 stsp break;
1543 1e37a5c2 2019-05-12 jcs default:
1544 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1545 1e37a5c2 2019-05-12 jcs break;
1546 e5a0f69f 2018-08-18 stsp }
1547 e5a0f69f 2018-08-18 stsp
1548 e5a0f69f 2018-08-18 stsp return err;
1549 bcbd79e2 2018-08-19 stsp }
1550 bcbd79e2 2018-08-19 stsp
1551 ef20f542 2022-06-26 thomas static int
1552 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1553 a3404814 2018-09-02 stsp {
1554 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1555 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1556 669b5ffa 2018-10-07 stsp return 0;
1557 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1558 669b5ffa 2018-10-07 stsp return 0;
1559 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1560 a3404814 2018-09-02 stsp return 0;
1561 a3404814 2018-09-02 stsp
1562 669b5ffa 2018-10-07 stsp return view->focussed;
1563 a3404814 2018-09-02 stsp }
1564 a3404814 2018-09-02 stsp
1565 bcbd79e2 2018-08-19 stsp static const struct got_error *
1566 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1567 e5a0f69f 2018-08-18 stsp {
1568 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1569 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1570 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1571 64486692 2022-07-07 thomas char *mode;
1572 fd823528 2018-10-22 stsp int fast_refresh = 10;
1573 1a76625f 2018-10-22 stsp int done = 0, errcode;
1574 e5a0f69f 2018-08-18 stsp
1575 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1576 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1577 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1578 64486692 2022-07-07 thomas else
1579 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1580 64486692 2022-07-07 thomas
1581 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1582 1a76625f 2018-10-22 stsp if (errcode)
1583 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1584 1a76625f 2018-10-22 stsp
1585 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1586 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1587 e5a0f69f 2018-08-18 stsp
1588 1004088d 2018-09-29 stsp view->focussed = 1;
1589 878940b7 2018-09-29 stsp err = view->show(view);
1590 0cf4efb1 2018-09-29 stsp if (err)
1591 0cf4efb1 2018-09-29 stsp return err;
1592 0cf4efb1 2018-09-29 stsp update_panels();
1593 0cf4efb1 2018-09-29 stsp doupdate();
1594 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1595 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1596 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1597 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1598 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1599 fd823528 2018-10-22 stsp
1600 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1601 e5a0f69f 2018-08-18 stsp if (err)
1602 e5a0f69f 2018-08-18 stsp break;
1603 9970f7fc 2020-12-03 stsp if (view->dying) {
1604 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1605 669b5ffa 2018-10-07 stsp
1606 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1607 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1608 9970f7fc 2020-12-03 stsp entry);
1609 e78dc838 2020-12-04 stsp else if (view->parent)
1610 669b5ffa 2018-10-07 stsp prev = view->parent;
1611 669b5ffa 2018-10-07 stsp
1612 e78dc838 2020-12-04 stsp if (view->parent) {
1613 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1614 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1615 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1616 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1617 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1618 40236d76 2022-06-23 thomas if (err)
1619 40236d76 2022-06-23 thomas break;
1620 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1621 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1622 e78dc838 2020-12-04 stsp } else
1623 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1624 669b5ffa 2018-10-07 stsp
1625 9970f7fc 2020-12-03 stsp err = view_close(view);
1626 fb59748f 2020-12-05 stsp if (err)
1627 e5a0f69f 2018-08-18 stsp goto done;
1628 669b5ffa 2018-10-07 stsp
1629 e78dc838 2020-12-04 stsp view = NULL;
1630 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1631 e78dc838 2020-12-04 stsp if (v->focussed)
1632 e78dc838 2020-12-04 stsp break;
1633 0cf4efb1 2018-09-29 stsp }
1634 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1635 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1636 e78dc838 2020-12-04 stsp if (prev)
1637 e78dc838 2020-12-04 stsp view = prev;
1638 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1639 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1640 e78dc838 2020-12-04 stsp tog_view_list_head);
1641 e78dc838 2020-12-04 stsp }
1642 e78dc838 2020-12-04 stsp if (view) {
1643 e78dc838 2020-12-04 stsp if (view->focus_child) {
1644 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1645 e78dc838 2020-12-04 stsp view = view->child;
1646 e78dc838 2020-12-04 stsp } else
1647 e78dc838 2020-12-04 stsp view->focussed = 1;
1648 e78dc838 2020-12-04 stsp }
1649 e78dc838 2020-12-04 stsp }
1650 e5a0f69f 2018-08-18 stsp }
1651 bcbd79e2 2018-08-19 stsp if (new_view) {
1652 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1653 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1654 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1655 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1656 86c66b02 2018-10-18 stsp continue;
1657 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1658 86c66b02 2018-10-18 stsp err = view_close(v);
1659 86c66b02 2018-10-18 stsp if (err)
1660 86c66b02 2018-10-18 stsp goto done;
1661 86c66b02 2018-10-18 stsp break;
1662 86c66b02 2018-10-18 stsp }
1663 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1664 fed7eaa8 2018-10-24 stsp view = new_view;
1665 7cd52833 2022-06-23 thomas }
1666 669b5ffa 2018-10-07 stsp if (view) {
1667 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1668 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1669 e78dc838 2020-12-04 stsp view = view->child;
1670 e78dc838 2020-12-04 stsp } else {
1671 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1672 e78dc838 2020-12-04 stsp view = view->parent;
1673 1a76625f 2018-10-22 stsp }
1674 e78dc838 2020-12-04 stsp show_panel(view->panel);
1675 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1676 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1677 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1678 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1679 669b5ffa 2018-10-07 stsp if (err)
1680 1a76625f 2018-10-22 stsp goto done;
1681 669b5ffa 2018-10-07 stsp }
1682 669b5ffa 2018-10-07 stsp err = view->show(view);
1683 0cf4efb1 2018-09-29 stsp if (err)
1684 1a76625f 2018-10-22 stsp goto done;
1685 669b5ffa 2018-10-07 stsp if (view->child) {
1686 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1687 669b5ffa 2018-10-07 stsp if (err)
1688 1a76625f 2018-10-22 stsp goto done;
1689 669b5ffa 2018-10-07 stsp }
1690 1a76625f 2018-10-22 stsp update_panels();
1691 1a76625f 2018-10-22 stsp doupdate();
1692 0cf4efb1 2018-09-29 stsp }
1693 e5a0f69f 2018-08-18 stsp }
1694 e5a0f69f 2018-08-18 stsp done:
1695 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1696 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1697 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1698 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1699 f2d749db 2022-07-12 thomas close_err = view_close(view);
1700 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1701 f2d749db 2022-07-12 thomas err = close_err;
1702 e5a0f69f 2018-08-18 stsp }
1703 1a76625f 2018-10-22 stsp
1704 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1705 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1706 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1707 1a76625f 2018-10-22 stsp
1708 e5a0f69f 2018-08-18 stsp return err;
1709 ea5e7bb5 2018-08-01 stsp }
1710 ea5e7bb5 2018-08-01 stsp
1711 4ed7e80c 2018-05-20 stsp __dead static void
1712 9f7d7167 2018-04-29 stsp usage_log(void)
1713 9f7d7167 2018-04-29 stsp {
1714 80ddbec8 2018-04-29 stsp endwin();
1715 c70c5802 2018-08-01 stsp fprintf(stderr,
1716 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1717 9f7d7167 2018-04-29 stsp getprogname());
1718 9f7d7167 2018-04-29 stsp exit(1);
1719 80ddbec8 2018-04-29 stsp }
1720 80ddbec8 2018-04-29 stsp
1721 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1722 80ddbec8 2018-04-29 stsp static const struct got_error *
1723 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1724 963b370f 2018-05-20 stsp {
1725 00dfcb92 2018-06-11 stsp char *vis = NULL;
1726 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1727 963b370f 2018-05-20 stsp
1728 963b370f 2018-05-20 stsp *ws = NULL;
1729 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1730 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1731 00dfcb92 2018-06-11 stsp int vislen;
1732 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1733 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1734 00dfcb92 2018-06-11 stsp
1735 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1736 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1737 00dfcb92 2018-06-11 stsp if (err)
1738 00dfcb92 2018-06-11 stsp return err;
1739 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1740 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1741 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1742 a7f50699 2018-06-11 stsp goto done;
1743 a7f50699 2018-06-11 stsp }
1744 00dfcb92 2018-06-11 stsp }
1745 963b370f 2018-05-20 stsp
1746 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1747 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1748 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1749 a7f50699 2018-06-11 stsp goto done;
1750 a7f50699 2018-06-11 stsp }
1751 963b370f 2018-05-20 stsp
1752 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1753 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1754 a7f50699 2018-06-11 stsp done:
1755 00dfcb92 2018-06-11 stsp free(vis);
1756 963b370f 2018-05-20 stsp if (err) {
1757 963b370f 2018-05-20 stsp free(*ws);
1758 963b370f 2018-05-20 stsp *ws = NULL;
1759 963b370f 2018-05-20 stsp *wlen = 0;
1760 963b370f 2018-05-20 stsp }
1761 963b370f 2018-05-20 stsp return err;
1762 05171be4 2022-06-23 thomas }
1763 05171be4 2022-06-23 thomas
1764 05171be4 2022-06-23 thomas static const struct got_error *
1765 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1766 05171be4 2022-06-23 thomas {
1767 05171be4 2022-06-23 thomas char *dst;
1768 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1769 05171be4 2022-06-23 thomas
1770 05171be4 2022-06-23 thomas *ptr = NULL;
1771 05171be4 2022-06-23 thomas n = len = strlen(src);
1772 83316834 2022-06-23 thomas dst = malloc(n + 1);
1773 05171be4 2022-06-23 thomas if (dst == NULL)
1774 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1775 05171be4 2022-06-23 thomas
1776 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1777 05171be4 2022-06-23 thomas const char c = src[idx];
1778 05171be4 2022-06-23 thomas
1779 05171be4 2022-06-23 thomas if (c == '\t') {
1780 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1781 b235ad5d 2022-06-23 thomas char *p;
1782 b235ad5d 2022-06-23 thomas
1783 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1784 83316834 2022-06-23 thomas if (p == NULL) {
1785 83316834 2022-06-23 thomas free(dst);
1786 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1787 83316834 2022-06-23 thomas
1788 83316834 2022-06-23 thomas }
1789 83316834 2022-06-23 thomas dst = p;
1790 05171be4 2022-06-23 thomas n += nb;
1791 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1792 05171be4 2022-06-23 thomas sz += nb;
1793 05171be4 2022-06-23 thomas } else
1794 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1795 05171be4 2022-06-23 thomas ++idx;
1796 05171be4 2022-06-23 thomas }
1797 05171be4 2022-06-23 thomas
1798 05171be4 2022-06-23 thomas dst[sz] = '\0';
1799 05171be4 2022-06-23 thomas *ptr = dst;
1800 05171be4 2022-06-23 thomas return NULL;
1801 963b370f 2018-05-20 stsp }
1802 963b370f 2018-05-20 stsp
1803 8d208d34 2022-06-23 thomas /*
1804 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1805 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1806 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1807 8d208d34 2022-06-23 thomas * *rcol.
1808 f91a2b48 2022-06-23 thomas */
1809 8d208d34 2022-06-23 thomas static int
1810 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1811 8d208d34 2022-06-23 thomas {
1812 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1813 f91a2b48 2022-06-23 thomas
1814 8d208d34 2022-06-23 thomas if (n == 0) {
1815 8d208d34 2022-06-23 thomas *rcol = cols;
1816 8d208d34 2022-06-23 thomas return off;
1817 8d208d34 2022-06-23 thomas }
1818 f91a2b48 2022-06-23 thomas
1819 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1820 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1821 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1822 8d208d34 2022-06-23 thomas else
1823 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1824 f91a2b48 2022-06-23 thomas
1825 8d208d34 2022-06-23 thomas if (width == -1) {
1826 8d208d34 2022-06-23 thomas width = 1;
1827 8d208d34 2022-06-23 thomas wline[i] = L'.';
1828 f91a2b48 2022-06-23 thomas }
1829 f91a2b48 2022-06-23 thomas
1830 8d208d34 2022-06-23 thomas if (cols + width > n)
1831 8d208d34 2022-06-23 thomas break;
1832 8d208d34 2022-06-23 thomas cols += width;
1833 f91a2b48 2022-06-23 thomas }
1834 f91a2b48 2022-06-23 thomas
1835 8d208d34 2022-06-23 thomas *rcol = cols;
1836 8d208d34 2022-06-23 thomas return i;
1837 f91a2b48 2022-06-23 thomas }
1838 f91a2b48 2022-06-23 thomas
1839 f91a2b48 2022-06-23 thomas /*
1840 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1841 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1842 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1843 f91a2b48 2022-06-23 thomas */
1844 f91a2b48 2022-06-23 thomas static const struct got_error *
1845 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1846 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1847 f91a2b48 2022-06-23 thomas {
1848 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1849 8d208d34 2022-06-23 thomas int cols;
1850 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1851 05171be4 2022-06-23 thomas char *exstr = NULL;
1852 963b370f 2018-05-20 stsp size_t wlen;
1853 8d208d34 2022-06-23 thomas int i, scrollx;
1854 963b370f 2018-05-20 stsp
1855 963b370f 2018-05-20 stsp *wlinep = NULL;
1856 b700b5d6 2018-07-10 stsp *widthp = 0;
1857 963b370f 2018-05-20 stsp
1858 05171be4 2022-06-23 thomas if (expand) {
1859 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1860 05171be4 2022-06-23 thomas if (err)
1861 05171be4 2022-06-23 thomas return err;
1862 05171be4 2022-06-23 thomas }
1863 05171be4 2022-06-23 thomas
1864 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1865 05171be4 2022-06-23 thomas free(exstr);
1866 963b370f 2018-05-20 stsp if (err)
1867 963b370f 2018-05-20 stsp return err;
1868 963b370f 2018-05-20 stsp
1869 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1870 f91a2b48 2022-06-23 thomas
1871 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1872 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1873 3f670bfb 2020-12-10 stsp wlen--;
1874 3f670bfb 2020-12-10 stsp }
1875 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1876 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1877 3f670bfb 2020-12-10 stsp wlen--;
1878 3f670bfb 2020-12-10 stsp }
1879 3f670bfb 2020-12-10 stsp
1880 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1881 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1882 27a741e5 2019-09-11 stsp
1883 b700b5d6 2018-07-10 stsp if (widthp)
1884 b700b5d6 2018-07-10 stsp *widthp = cols;
1885 f91a2b48 2022-06-23 thomas if (scrollxp)
1886 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1887 963b370f 2018-05-20 stsp if (err)
1888 963b370f 2018-05-20 stsp free(wline);
1889 963b370f 2018-05-20 stsp else
1890 963b370f 2018-05-20 stsp *wlinep = wline;
1891 963b370f 2018-05-20 stsp return err;
1892 963b370f 2018-05-20 stsp }
1893 963b370f 2018-05-20 stsp
1894 8b473291 2019-02-21 stsp static const struct got_error*
1895 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1896 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1897 8b473291 2019-02-21 stsp {
1898 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1899 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1900 8b473291 2019-02-21 stsp char *s;
1901 8b473291 2019-02-21 stsp const char *name;
1902 8b473291 2019-02-21 stsp
1903 8b473291 2019-02-21 stsp *refs_str = NULL;
1904 8b473291 2019-02-21 stsp
1905 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1906 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1907 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1908 52b5abe1 2019-08-13 stsp int cmp;
1909 52b5abe1 2019-08-13 stsp
1910 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1911 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1912 8b473291 2019-02-21 stsp continue;
1913 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1914 8b473291 2019-02-21 stsp name += 5;
1915 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1916 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1917 7143d404 2019-03-12 stsp continue;
1918 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1919 8b473291 2019-02-21 stsp name += 6;
1920 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1921 8b473291 2019-02-21 stsp name += 8;
1922 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1923 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1924 79cc719f 2020-04-24 stsp continue;
1925 79cc719f 2020-04-24 stsp }
1926 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1927 48cae60d 2020-09-22 stsp if (err)
1928 48cae60d 2020-09-22 stsp break;
1929 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1930 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1931 5d844a1e 2019-08-13 stsp if (err) {
1932 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1933 48cae60d 2020-09-22 stsp free(ref_id);
1934 5d844a1e 2019-08-13 stsp break;
1935 48cae60d 2020-09-22 stsp }
1936 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1937 5d844a1e 2019-08-13 stsp err = NULL;
1938 5d844a1e 2019-08-13 stsp tag = NULL;
1939 5d844a1e 2019-08-13 stsp }
1940 52b5abe1 2019-08-13 stsp }
1941 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1942 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1943 48cae60d 2020-09-22 stsp free(ref_id);
1944 52b5abe1 2019-08-13 stsp if (tag)
1945 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1946 52b5abe1 2019-08-13 stsp if (cmp != 0)
1947 52b5abe1 2019-08-13 stsp continue;
1948 8b473291 2019-02-21 stsp s = *refs_str;
1949 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1950 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1951 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1952 8b473291 2019-02-21 stsp free(s);
1953 8b473291 2019-02-21 stsp *refs_str = NULL;
1954 8b473291 2019-02-21 stsp break;
1955 8b473291 2019-02-21 stsp }
1956 8b473291 2019-02-21 stsp free(s);
1957 8b473291 2019-02-21 stsp }
1958 8b473291 2019-02-21 stsp
1959 8b473291 2019-02-21 stsp return err;
1960 8b473291 2019-02-21 stsp }
1961 8b473291 2019-02-21 stsp
1962 963b370f 2018-05-20 stsp static const struct got_error *
1963 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1964 27a741e5 2019-09-11 stsp int col_tab_align)
1965 5813d178 2019-03-09 stsp {
1966 e6b8b890 2020-12-29 naddy char *smallerthan;
1967 5813d178 2019-03-09 stsp
1968 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1969 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1970 5813d178 2019-03-09 stsp author = smallerthan + 1;
1971 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1972 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1973 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1974 5813d178 2019-03-09 stsp }
1975 5813d178 2019-03-09 stsp
1976 5813d178 2019-03-09 stsp static const struct got_error *
1977 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1978 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1979 8fdc79fe 2020-12-01 naddy int author_display_cols)
1980 80ddbec8 2018-04-29 stsp {
1981 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1982 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1983 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1984 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1985 5813d178 2019-03-09 stsp char *author = NULL;
1986 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1987 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1988 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1989 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1990 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1991 ccb26ccd 2018-11-05 stsp struct tm tm;
1992 45d799e2 2018-12-23 stsp time_t committer_time;
1993 11b20872 2019-11-08 stsp struct tog_color *tc;
1994 80ddbec8 2018-04-29 stsp
1995 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1996 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1997 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1998 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1999 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2000 b39d25c7 2018-07-10 stsp
2001 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2002 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2003 b39d25c7 2018-07-10 stsp else
2004 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2005 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2006 11b20872 2019-11-08 stsp if (tc)
2007 11b20872 2019-11-08 stsp wattr_on(view->window,
2008 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2009 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2010 11b20872 2019-11-08 stsp if (tc)
2011 11b20872 2019-11-08 stsp wattr_off(view->window,
2012 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2013 27a741e5 2019-09-11 stsp col = limit;
2014 b39d25c7 2018-07-10 stsp if (col > avail)
2015 b39d25c7 2018-07-10 stsp goto done;
2016 6570a66d 2019-11-08 stsp
2017 6570a66d 2019-11-08 stsp if (avail >= 120) {
2018 6570a66d 2019-11-08 stsp char *id_str;
2019 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2020 6570a66d 2019-11-08 stsp if (err)
2021 6570a66d 2019-11-08 stsp goto done;
2022 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2023 11b20872 2019-11-08 stsp if (tc)
2024 11b20872 2019-11-08 stsp wattr_on(view->window,
2025 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2026 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2027 11b20872 2019-11-08 stsp if (tc)
2028 11b20872 2019-11-08 stsp wattr_off(view->window,
2029 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2030 6570a66d 2019-11-08 stsp free(id_str);
2031 6570a66d 2019-11-08 stsp col += 9;
2032 6570a66d 2019-11-08 stsp if (col > avail)
2033 6570a66d 2019-11-08 stsp goto done;
2034 6570a66d 2019-11-08 stsp }
2035 b39d25c7 2018-07-10 stsp
2036 f69c5a46 2022-07-19 thomas if (s->use_committer)
2037 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2038 f69c5a46 2022-07-19 thomas else
2039 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2040 5813d178 2019-03-09 stsp if (author == NULL) {
2041 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2042 80ddbec8 2018-04-29 stsp goto done;
2043 80ddbec8 2018-04-29 stsp }
2044 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2045 bb737323 2018-05-20 stsp if (err)
2046 bb737323 2018-05-20 stsp goto done;
2047 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2048 11b20872 2019-11-08 stsp if (tc)
2049 11b20872 2019-11-08 stsp wattr_on(view->window,
2050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2051 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2052 11b20872 2019-11-08 stsp if (tc)
2053 11b20872 2019-11-08 stsp wattr_off(view->window,
2054 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2055 bb737323 2018-05-20 stsp col += author_width;
2056 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2057 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2058 bb737323 2018-05-20 stsp col++;
2059 bb737323 2018-05-20 stsp author_width++;
2060 bb737323 2018-05-20 stsp }
2061 9c2eaf34 2018-05-20 stsp if (col > avail)
2062 9c2eaf34 2018-05-20 stsp goto done;
2063 80ddbec8 2018-04-29 stsp
2064 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2065 5943eee2 2019-08-13 stsp if (err)
2066 6d9fbc00 2018-04-29 stsp goto done;
2067 bb737323 2018-05-20 stsp logmsg = logmsg0;
2068 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2069 bb737323 2018-05-20 stsp logmsg++;
2070 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2071 bb737323 2018-05-20 stsp if (newline)
2072 bb737323 2018-05-20 stsp *newline = '\0';
2073 f91a2b48 2022-06-23 thomas limit = avail - col;
2074 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2075 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2076 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2077 f91a2b48 2022-06-23 thomas limit, col, 1);
2078 331b1a16 2022-06-23 thomas if (err)
2079 331b1a16 2022-06-23 thomas goto done;
2080 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2081 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2082 27a741e5 2019-09-11 stsp while (col < avail) {
2083 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2084 bb737323 2018-05-20 stsp col++;
2085 881b2d3e 2018-04-30 stsp }
2086 80ddbec8 2018-04-29 stsp done:
2087 80ddbec8 2018-04-29 stsp free(logmsg0);
2088 bb737323 2018-05-20 stsp free(wlogmsg);
2089 5813d178 2019-03-09 stsp free(author);
2090 bb737323 2018-05-20 stsp free(wauthor);
2091 80ddbec8 2018-04-29 stsp free(line);
2092 80ddbec8 2018-04-29 stsp return err;
2093 80ddbec8 2018-04-29 stsp }
2094 26ed57b2 2018-05-19 stsp
2095 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2096 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2097 899d86c2 2018-05-10 stsp struct got_object_id *id)
2098 80ddbec8 2018-04-29 stsp {
2099 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2100 80ddbec8 2018-04-29 stsp
2101 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2102 80ddbec8 2018-04-29 stsp if (entry == NULL)
2103 899d86c2 2018-05-10 stsp return NULL;
2104 99db9666 2018-05-07 stsp
2105 899d86c2 2018-05-10 stsp entry->id = id;
2106 99db9666 2018-05-07 stsp entry->commit = commit;
2107 899d86c2 2018-05-10 stsp return entry;
2108 99db9666 2018-05-07 stsp }
2109 80ddbec8 2018-04-29 stsp
2110 99db9666 2018-05-07 stsp static void
2111 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2112 99db9666 2018-05-07 stsp {
2113 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2114 99db9666 2018-05-07 stsp
2115 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2116 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2117 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2118 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2119 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2120 99db9666 2018-05-07 stsp free(entry);
2121 99db9666 2018-05-07 stsp }
2122 99db9666 2018-05-07 stsp
2123 99db9666 2018-05-07 stsp static void
2124 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2125 99db9666 2018-05-07 stsp {
2126 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2127 99db9666 2018-05-07 stsp pop_commit(commits);
2128 c4972b91 2018-05-07 stsp }
2129 c4972b91 2018-05-07 stsp
2130 c4972b91 2018-05-07 stsp static const struct got_error *
2131 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2132 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2133 13add988 2019-10-15 stsp {
2134 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2135 13add988 2019-10-15 stsp regmatch_t regmatch;
2136 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2137 13add988 2019-10-15 stsp
2138 13add988 2019-10-15 stsp *have_match = 0;
2139 13add988 2019-10-15 stsp
2140 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2141 13add988 2019-10-15 stsp if (err)
2142 13add988 2019-10-15 stsp return err;
2143 13add988 2019-10-15 stsp
2144 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2145 13add988 2019-10-15 stsp if (err)
2146 13add988 2019-10-15 stsp goto done;
2147 13add988 2019-10-15 stsp
2148 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2149 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2150 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2151 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2152 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2153 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2154 13add988 2019-10-15 stsp *have_match = 1;
2155 13add988 2019-10-15 stsp done:
2156 13add988 2019-10-15 stsp free(id_str);
2157 13add988 2019-10-15 stsp free(logmsg);
2158 13add988 2019-10-15 stsp return err;
2159 13add988 2019-10-15 stsp }
2160 13add988 2019-10-15 stsp
2161 13add988 2019-10-15 stsp static const struct got_error *
2162 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2163 c4972b91 2018-05-07 stsp {
2164 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2165 9ba79e04 2018-06-11 stsp
2166 1a76625f 2018-10-22 stsp /*
2167 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2168 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2169 1a76625f 2018-10-22 stsp * while updating the display.
2170 1a76625f 2018-10-22 stsp */
2171 4e0d2870 2020-12-07 naddy do {
2172 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2173 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2174 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2175 1a76625f 2018-10-22 stsp int errcode;
2176 899d86c2 2018-05-10 stsp
2177 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2178 4e0d2870 2020-12-07 naddy NULL, NULL);
2179 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2180 ecb28ae0 2018-07-16 stsp break;
2181 899d86c2 2018-05-10 stsp
2182 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2183 9ba79e04 2018-06-11 stsp if (err)
2184 9ba79e04 2018-06-11 stsp break;
2185 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2186 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2187 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2188 9ba79e04 2018-06-11 stsp break;
2189 9ba79e04 2018-06-11 stsp }
2190 93e45b7c 2018-09-24 stsp
2191 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2192 1a76625f 2018-10-22 stsp if (errcode) {
2193 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2194 13add988 2019-10-15 stsp "pthread_mutex_lock");
2195 1a76625f 2018-10-22 stsp break;
2196 1a76625f 2018-10-22 stsp }
2197 1a76625f 2018-10-22 stsp
2198 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2199 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2200 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2201 1a76625f 2018-10-22 stsp
2202 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2203 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2204 7c1452c1 2020-03-26 stsp int have_match;
2205 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2206 7c1452c1 2020-03-26 stsp if (err)
2207 7c1452c1 2020-03-26 stsp break;
2208 7c1452c1 2020-03-26 stsp if (have_match)
2209 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2210 13add988 2019-10-15 stsp }
2211 13add988 2019-10-15 stsp
2212 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2213 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2214 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2215 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2216 7c1452c1 2020-03-26 stsp if (err)
2217 13add988 2019-10-15 stsp break;
2218 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2219 899d86c2 2018-05-10 stsp
2220 9ba79e04 2018-06-11 stsp return err;
2221 0553a4e3 2018-04-30 stsp }
2222 0553a4e3 2018-04-30 stsp
2223 2b779855 2020-12-05 naddy static void
2224 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2225 2b779855 2020-12-05 naddy {
2226 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2227 2b779855 2020-12-05 naddy int ncommits = 0;
2228 2b779855 2020-12-05 naddy
2229 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2230 2b779855 2020-12-05 naddy while (entry) {
2231 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2232 2b779855 2020-12-05 naddy s->selected_entry = entry;
2233 2b779855 2020-12-05 naddy break;
2234 2b779855 2020-12-05 naddy }
2235 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2236 2b779855 2020-12-05 naddy ncommits++;
2237 2b779855 2020-12-05 naddy }
2238 2b779855 2020-12-05 naddy }
2239 2b779855 2020-12-05 naddy
2240 0553a4e3 2018-04-30 stsp static const struct got_error *
2241 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2242 0553a4e3 2018-04-30 stsp {
2243 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2244 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2245 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2246 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2247 60493ae3 2019-06-20 stsp int width;
2248 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2249 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2250 8b473291 2019-02-21 stsp char *refs_str = NULL;
2251 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2252 11b20872 2019-11-08 stsp struct tog_color *tc;
2253 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2254 bd3f8225 2022-08-12 thomas
2255 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2256 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2257 0553a4e3 2018-04-30 stsp
2258 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2259 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2260 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2261 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2262 1a76625f 2018-10-22 stsp if (err)
2263 ecb28ae0 2018-07-16 stsp return err;
2264 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2265 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2266 d2075bf3 2020-12-25 stsp if (refs) {
2267 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2268 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2269 d2075bf3 2020-12-25 stsp if (err)
2270 d2075bf3 2020-12-25 stsp goto done;
2271 d2075bf3 2020-12-25 stsp }
2272 867c6645 2018-07-10 stsp }
2273 359bfafd 2019-02-22 stsp
2274 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2275 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2276 1a76625f 2018-10-22 stsp
2277 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2278 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2279 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2280 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2281 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2282 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2283 8f4ed634 2020-03-26 stsp goto done;
2284 8f4ed634 2020-03-26 stsp }
2285 8f4ed634 2020-03-26 stsp } else {
2286 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2287 f9686aa5 2020-03-27 stsp
2288 f9686aa5 2020-03-27 stsp if (view->searching) {
2289 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2290 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2291 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2292 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2293 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2294 f9686aa5 2020-03-27 stsp search_str = "searching...";
2295 f9686aa5 2020-03-27 stsp }
2296 f9686aa5 2020-03-27 stsp
2297 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2298 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2299 f9686aa5 2020-03-27 stsp search_str ? search_str :
2300 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2301 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2302 8f4ed634 2020-03-26 stsp goto done;
2303 8f4ed634 2020-03-26 stsp }
2304 8b473291 2019-02-21 stsp }
2305 1a76625f 2018-10-22 stsp
2306 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2307 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2308 91198554 2022-06-23 thomas "........................................",
2309 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2310 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2311 1a76625f 2018-10-22 stsp header = NULL;
2312 1a76625f 2018-10-22 stsp goto done;
2313 1a76625f 2018-10-22 stsp }
2314 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2315 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2316 1a76625f 2018-10-22 stsp 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 ecb28ae0 2018-07-16 stsp }
2321 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2322 1a76625f 2018-10-22 stsp if (err)
2323 1a76625f 2018-10-22 stsp goto done;
2324 867c6645 2018-07-10 stsp
2325 2814baeb 2018-08-01 stsp werase(view->window);
2326 867c6645 2018-07-10 stsp
2327 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2328 a3404814 2018-09-02 stsp wstandout(view->window);
2329 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2330 11b20872 2019-11-08 stsp if (tc)
2331 11b20872 2019-11-08 stsp wattr_on(view->window,
2332 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2333 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2334 11b20872 2019-11-08 stsp if (tc)
2335 11b20872 2019-11-08 stsp wattr_off(view->window,
2336 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2337 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2338 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2339 1a76625f 2018-10-22 stsp width++;
2340 1a76625f 2018-10-22 stsp }
2341 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2342 a3404814 2018-09-02 stsp wstandend(view->window);
2343 ecb28ae0 2018-07-16 stsp free(wline);
2344 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2345 1a76625f 2018-10-22 stsp goto done;
2346 0553a4e3 2018-04-30 stsp
2347 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2348 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2349 5813d178 2019-03-09 stsp ncommits = 0;
2350 05171be4 2022-06-23 thomas view->maxx = 0;
2351 5813d178 2019-03-09 stsp while (entry) {
2352 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2353 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2354 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2355 5813d178 2019-03-09 stsp int width;
2356 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2357 5813d178 2019-03-09 stsp break;
2358 f69c5a46 2022-07-19 thomas if (s->use_committer)
2359 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2360 f69c5a46 2022-07-19 thomas else
2361 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2362 5813d178 2019-03-09 stsp if (author == NULL) {
2363 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2364 5813d178 2019-03-09 stsp goto done;
2365 5813d178 2019-03-09 stsp }
2366 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2367 27a741e5 2019-09-11 stsp date_display_cols);
2368 5813d178 2019-03-09 stsp if (author_cols < width)
2369 5813d178 2019-03-09 stsp author_cols = width;
2370 5813d178 2019-03-09 stsp free(wauthor);
2371 5813d178 2019-03-09 stsp free(author);
2372 ef944b8b 2022-07-21 thomas if (err)
2373 ef944b8b 2022-07-21 thomas goto done;
2374 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2375 05171be4 2022-06-23 thomas if (err)
2376 05171be4 2022-06-23 thomas goto done;
2377 05171be4 2022-06-23 thomas msg = msg0;
2378 05171be4 2022-06-23 thomas while (*msg == '\n')
2379 05171be4 2022-06-23 thomas ++msg;
2380 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2381 331b1a16 2022-06-23 thomas *eol = '\0';
2382 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2383 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2384 331b1a16 2022-06-23 thomas if (err)
2385 331b1a16 2022-06-23 thomas goto done;
2386 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2387 05171be4 2022-06-23 thomas free(msg0);
2388 331b1a16 2022-06-23 thomas free(wmsg);
2389 7ca04879 2019-10-19 stsp ncommits++;
2390 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2391 5813d178 2019-03-09 stsp }
2392 5813d178 2019-03-09 stsp
2393 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2394 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2395 867c6645 2018-07-10 stsp ncommits = 0;
2396 899d86c2 2018-05-10 stsp while (entry) {
2397 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2398 899d86c2 2018-05-10 stsp break;
2399 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2400 2814baeb 2018-08-01 stsp wstandout(view->window);
2401 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2402 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2403 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2404 2814baeb 2018-08-01 stsp wstandend(view->window);
2405 0553a4e3 2018-04-30 stsp if (err)
2406 60493ae3 2019-06-20 stsp goto done;
2407 0553a4e3 2018-04-30 stsp ncommits++;
2408 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2409 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2410 80ddbec8 2018-04-29 stsp }
2411 80ddbec8 2018-04-29 stsp
2412 a5d43cac 2022-07-01 thomas view_border(view);
2413 1a76625f 2018-10-22 stsp done:
2414 1a76625f 2018-10-22 stsp free(id_str);
2415 8b473291 2019-02-21 stsp free(refs_str);
2416 1a76625f 2018-10-22 stsp free(ncommits_str);
2417 1a76625f 2018-10-22 stsp free(header);
2418 80ddbec8 2018-04-29 stsp return err;
2419 9f7d7167 2018-04-29 stsp }
2420 07b55e75 2018-05-10 stsp
2421 07b55e75 2018-05-10 stsp static void
2422 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2423 07b55e75 2018-05-10 stsp {
2424 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2425 07b55e75 2018-05-10 stsp int nscrolled = 0;
2426 07b55e75 2018-05-10 stsp
2427 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2428 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2429 07b55e75 2018-05-10 stsp return;
2430 9f7d7167 2018-04-29 stsp
2431 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2432 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2433 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2434 07b55e75 2018-05-10 stsp if (entry) {
2435 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2436 07b55e75 2018-05-10 stsp nscrolled++;
2437 07b55e75 2018-05-10 stsp }
2438 07b55e75 2018-05-10 stsp }
2439 aa075928 2018-05-10 stsp }
2440 aa075928 2018-05-10 stsp
2441 aa075928 2018-05-10 stsp static const struct got_error *
2442 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2443 aa075928 2018-05-10 stsp {
2444 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2445 5e224a3e 2019-02-22 stsp int errcode;
2446 8a42fca8 2019-02-22 stsp
2447 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2448 aa075928 2018-05-10 stsp
2449 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2450 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2451 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2452 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2453 7aafa0d1 2019-02-22 stsp if (errcode)
2454 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2455 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2456 7c1452c1 2020-03-26 stsp
2457 7c1452c1 2020-03-26 stsp /*
2458 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2459 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2460 7c1452c1 2020-03-26 stsp */
2461 7c1452c1 2020-03-26 stsp if (!wait)
2462 7c1452c1 2020-03-26 stsp break;
2463 7c1452c1 2020-03-26 stsp
2464 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2465 ffe38506 2020-12-01 naddy show_log_view(view);
2466 7c1452c1 2020-03-26 stsp update_panels();
2467 7c1452c1 2020-03-26 stsp doupdate();
2468 7c1452c1 2020-03-26 stsp
2469 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2470 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2471 82954512 2020-02-03 stsp if (errcode)
2472 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2473 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2474 82954512 2020-02-03 stsp
2475 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2476 ffe38506 2020-12-01 naddy show_log_view(view);
2477 7c1452c1 2020-03-26 stsp update_panels();
2478 7c1452c1 2020-03-26 stsp doupdate();
2479 5e224a3e 2019-02-22 stsp }
2480 5e224a3e 2019-02-22 stsp
2481 5e224a3e 2019-02-22 stsp return NULL;
2482 a5d43cac 2022-07-01 thomas }
2483 a5d43cac 2022-07-01 thomas
2484 a5d43cac 2022-07-01 thomas static const struct got_error *
2485 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2486 a5d43cac 2022-07-01 thomas {
2487 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2488 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2489 fe731b51 2022-07-14 thomas
2490 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2491 fe731b51 2022-07-14 thomas return NULL;
2492 a5d43cac 2022-07-01 thomas
2493 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2494 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2495 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2496 a5d43cac 2022-07-01 thomas
2497 a5d43cac 2022-07-01 thomas return err;
2498 5e224a3e 2019-02-22 stsp }
2499 5e224a3e 2019-02-22 stsp
2500 5e224a3e 2019-02-22 stsp static const struct got_error *
2501 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2502 5e224a3e 2019-02-22 stsp {
2503 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2504 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2505 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2506 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2507 5e224a3e 2019-02-22 stsp
2508 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2509 5e224a3e 2019-02-22 stsp return NULL;
2510 5e224a3e 2019-02-22 stsp
2511 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2512 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2513 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2514 08ebd0a9 2019-02-22 stsp /*
2515 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2516 08ebd0a9 2019-02-22 stsp */
2517 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2518 07dd3ed3 2022-08-06 thomas ncommits_needed - s->commits.ncommits;
2519 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2520 5e224a3e 2019-02-22 stsp if (err)
2521 5e224a3e 2019-02-22 stsp return err;
2522 7aafa0d1 2019-02-22 stsp }
2523 b295e71b 2019-02-22 stsp
2524 7aafa0d1 2019-02-22 stsp do {
2525 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2526 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2527 88048b54 2019-02-21 stsp break;
2528 88048b54 2019-02-21 stsp
2529 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2530 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2531 aa075928 2018-05-10 stsp
2532 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2533 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2534 dd0a52c1 2018-05-20 stsp break;
2535 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2536 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2537 aa075928 2018-05-10 stsp
2538 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2539 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2540 a5d43cac 2022-07-01 thomas else
2541 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2542 a5d43cac 2022-07-01 thomas
2543 dd0a52c1 2018-05-20 stsp return err;
2544 07b55e75 2018-05-10 stsp }
2545 4a7f7875 2018-05-10 stsp
2546 cd0acaa7 2018-05-20 stsp static const struct got_error *
2547 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2548 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2549 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2550 cd0acaa7 2018-05-20 stsp {
2551 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2552 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2553 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2554 cd0acaa7 2018-05-20 stsp
2555 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2556 15a94983 2018-12-23 stsp if (diff_view == NULL)
2557 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2558 ea5e7bb5 2018-08-01 stsp
2559 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2560 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2561 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2562 e5a0f69f 2018-08-18 stsp if (err == NULL)
2563 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2564 cd0acaa7 2018-05-20 stsp return err;
2565 4a7f7875 2018-05-10 stsp }
2566 4a7f7875 2018-05-10 stsp
2567 80ddbec8 2018-04-29 stsp static const struct got_error *
2568 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2569 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2570 9343a5fb 2018-06-23 stsp {
2571 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2572 941e9f74 2019-05-21 stsp
2573 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2574 941e9f74 2019-05-21 stsp if (parent == NULL)
2575 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2576 941e9f74 2019-05-21 stsp
2577 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2578 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2579 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2580 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2581 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2582 941e9f74 2019-05-21 stsp s->tree = subtree;
2583 941e9f74 2019-05-21 stsp s->selected = 0;
2584 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2585 941e9f74 2019-05-21 stsp return NULL;
2586 941e9f74 2019-05-21 stsp }
2587 941e9f74 2019-05-21 stsp
2588 941e9f74 2019-05-21 stsp static const struct got_error *
2589 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2590 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2591 941e9f74 2019-05-21 stsp {
2592 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2593 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2594 941e9f74 2019-05-21 stsp const char *p;
2595 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2596 9343a5fb 2018-06-23 stsp
2597 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2598 941e9f74 2019-05-21 stsp p = path;
2599 941e9f74 2019-05-21 stsp while (*p) {
2600 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2601 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2602 56e0773d 2019-11-28 stsp char *te_name;
2603 33cbf02b 2020-01-12 stsp
2604 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2605 33cbf02b 2020-01-12 stsp p++;
2606 941e9f74 2019-05-21 stsp
2607 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2608 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2609 941e9f74 2019-05-21 stsp if (slash == NULL)
2610 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2611 33cbf02b 2020-01-12 stsp else
2612 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2613 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2614 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2615 56e0773d 2019-11-28 stsp break;
2616 941e9f74 2019-05-21 stsp }
2617 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2618 56e0773d 2019-11-28 stsp if (te == NULL) {
2619 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2620 56e0773d 2019-11-28 stsp free(te_name);
2621 941e9f74 2019-05-21 stsp break;
2622 941e9f74 2019-05-21 stsp }
2623 56e0773d 2019-11-28 stsp free(te_name);
2624 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2625 941e9f74 2019-05-21 stsp
2626 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2627 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2628 b03c880f 2019-05-21 stsp
2629 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2630 941e9f74 2019-05-21 stsp if (slash)
2631 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2632 941e9f74 2019-05-21 stsp else
2633 941e9f74 2019-05-21 stsp subpath = strdup(path);
2634 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2635 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2636 941e9f74 2019-05-21 stsp break;
2637 941e9f74 2019-05-21 stsp }
2638 941e9f74 2019-05-21 stsp
2639 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2640 941e9f74 2019-05-21 stsp subpath);
2641 941e9f74 2019-05-21 stsp if (err)
2642 941e9f74 2019-05-21 stsp break;
2643 941e9f74 2019-05-21 stsp
2644 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2645 941e9f74 2019-05-21 stsp free(tree_id);
2646 941e9f74 2019-05-21 stsp if (err)
2647 941e9f74 2019-05-21 stsp break;
2648 941e9f74 2019-05-21 stsp
2649 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2650 941e9f74 2019-05-21 stsp if (err) {
2651 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2652 941e9f74 2019-05-21 stsp break;
2653 941e9f74 2019-05-21 stsp }
2654 941e9f74 2019-05-21 stsp if (slash == NULL)
2655 941e9f74 2019-05-21 stsp break;
2656 941e9f74 2019-05-21 stsp free(subpath);
2657 941e9f74 2019-05-21 stsp subpath = NULL;
2658 941e9f74 2019-05-21 stsp p = slash;
2659 941e9f74 2019-05-21 stsp }
2660 941e9f74 2019-05-21 stsp
2661 941e9f74 2019-05-21 stsp free(subpath);
2662 1a76625f 2018-10-22 stsp return err;
2663 61266923 2020-01-14 stsp }
2664 61266923 2020-01-14 stsp
2665 61266923 2020-01-14 stsp static const struct got_error *
2666 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2667 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2668 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2669 55cccc34 2020-02-20 stsp {
2670 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2671 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2672 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2673 55cccc34 2020-02-20 stsp
2674 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2675 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2676 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2677 55cccc34 2020-02-20 stsp
2678 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2679 bc573f3b 2021-07-10 stsp if (err)
2680 55cccc34 2020-02-20 stsp return err;
2681 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2682 55cccc34 2020-02-20 stsp
2683 55cccc34 2020-02-20 stsp *new_view = tree_view;
2684 55cccc34 2020-02-20 stsp
2685 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2686 55cccc34 2020-02-20 stsp return NULL;
2687 55cccc34 2020-02-20 stsp
2688 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2689 55cccc34 2020-02-20 stsp }
2690 55cccc34 2020-02-20 stsp
2691 55cccc34 2020-02-20 stsp static const struct got_error *
2692 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2693 61266923 2020-01-14 stsp {
2694 61266923 2020-01-14 stsp sigset_t sigset;
2695 61266923 2020-01-14 stsp int errcode;
2696 61266923 2020-01-14 stsp
2697 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2698 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2699 61266923 2020-01-14 stsp
2700 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2701 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2702 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2703 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2704 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2705 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2706 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2707 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2708 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2709 61266923 2020-01-14 stsp
2710 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2711 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2712 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2713 61266923 2020-01-14 stsp
2714 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2715 61266923 2020-01-14 stsp if (errcode)
2716 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2717 61266923 2020-01-14 stsp
2718 61266923 2020-01-14 stsp return NULL;
2719 1a76625f 2018-10-22 stsp }
2720 1a76625f 2018-10-22 stsp
2721 1a76625f 2018-10-22 stsp static void *
2722 1a76625f 2018-10-22 stsp log_thread(void *arg)
2723 1a76625f 2018-10-22 stsp {
2724 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2725 1a76625f 2018-10-22 stsp int errcode = 0;
2726 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2727 1a76625f 2018-10-22 stsp int done = 0;
2728 61266923 2020-01-14 stsp
2729 f2d749db 2022-07-12 thomas /*
2730 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2731 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2732 f2d749db 2022-07-12 thomas */
2733 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2734 f2d749db 2022-07-12 thomas if (errcode) {
2735 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2736 61266923 2020-01-14 stsp return (void *)err;
2737 f2d749db 2022-07-12 thomas }
2738 1a76625f 2018-10-22 stsp
2739 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2740 f2d749db 2022-07-12 thomas if (err) {
2741 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2742 f2d749db 2022-07-12 thomas goto done;
2743 f2d749db 2022-07-12 thomas }
2744 f2d749db 2022-07-12 thomas
2745 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2746 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2747 f2d749db 2022-07-12 thomas if (errcode) {
2748 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2749 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2750 f2d749db 2022-07-12 thomas goto done;
2751 f2d749db 2022-07-12 thomas }
2752 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2753 1a76625f 2018-10-22 stsp if (err) {
2754 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2755 f2d749db 2022-07-12 thomas goto done;
2756 1a76625f 2018-10-22 stsp err = NULL;
2757 1a76625f 2018-10-22 stsp done = 1;
2758 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2759 1a76625f 2018-10-22 stsp a->commits_needed--;
2760 1a76625f 2018-10-22 stsp
2761 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2762 3abe8080 2019-04-10 stsp if (errcode) {
2763 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2764 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2765 f2d749db 2022-07-12 thomas goto done;
2766 3abe8080 2019-04-10 stsp } else if (*a->quit)
2767 1a76625f 2018-10-22 stsp done = 1;
2768 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2769 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2770 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2771 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2772 1a76625f 2018-10-22 stsp }
2773 1a76625f 2018-10-22 stsp
2774 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2775 7c1452c1 2020-03-26 stsp if (errcode) {
2776 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2777 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2778 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2779 f2d749db 2022-07-12 thomas goto done;
2780 7c1452c1 2020-03-26 stsp }
2781 7c1452c1 2020-03-26 stsp
2782 1a76625f 2018-10-22 stsp if (done)
2783 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2784 7c1452c1 2020-03-26 stsp else {
2785 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2786 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2787 7c1452c1 2020-03-26 stsp &tog_mutex);
2788 f2d749db 2022-07-12 thomas if (errcode) {
2789 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2790 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2791 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2792 f2d749db 2022-07-12 thomas goto done;
2793 f2d749db 2022-07-12 thomas }
2794 21355643 2020-12-06 stsp if (*a->quit)
2795 21355643 2020-12-06 stsp done = 1;
2796 7c1452c1 2020-03-26 stsp }
2797 1a76625f 2018-10-22 stsp }
2798 1a76625f 2018-10-22 stsp }
2799 3abe8080 2019-04-10 stsp a->log_complete = 1;
2800 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2801 f2d749db 2022-07-12 thomas if (errcode)
2802 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2803 f2d749db 2022-07-12 thomas done:
2804 f2d749db 2022-07-12 thomas if (err) {
2805 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2806 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2807 f2d749db 2022-07-12 thomas }
2808 1a76625f 2018-10-22 stsp return (void *)err;
2809 1a76625f 2018-10-22 stsp }
2810 1a76625f 2018-10-22 stsp
2811 1a76625f 2018-10-22 stsp static const struct got_error *
2812 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2813 1a76625f 2018-10-22 stsp {
2814 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2815 1a76625f 2018-10-22 stsp int errcode;
2816 1a76625f 2018-10-22 stsp
2817 1a76625f 2018-10-22 stsp if (s->thread) {
2818 1a76625f 2018-10-22 stsp s->quit = 1;
2819 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2820 1a76625f 2018-10-22 stsp if (errcode)
2821 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2822 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2823 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2824 1a76625f 2018-10-22 stsp if (errcode)
2825 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2826 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2827 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2828 1a76625f 2018-10-22 stsp if (errcode)
2829 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2830 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2831 1a76625f 2018-10-22 stsp if (errcode)
2832 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2833 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2834 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2835 1a76625f 2018-10-22 stsp }
2836 1a76625f 2018-10-22 stsp
2837 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2838 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2839 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2840 1af5eddf 2022-06-23 thomas }
2841 1af5eddf 2022-06-23 thomas
2842 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2843 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2844 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2845 1af5eddf 2022-06-23 thomas if (err == NULL)
2846 1af5eddf 2022-06-23 thomas err = pack_err;
2847 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2848 1a76625f 2018-10-22 stsp }
2849 1a76625f 2018-10-22 stsp
2850 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2851 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2852 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2853 1a76625f 2018-10-22 stsp }
2854 1a76625f 2018-10-22 stsp
2855 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2856 9343a5fb 2018-06-23 stsp }
2857 9343a5fb 2018-06-23 stsp
2858 9343a5fb 2018-06-23 stsp static const struct got_error *
2859 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2860 1a76625f 2018-10-22 stsp {
2861 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2862 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2863 276b94a1 2020-11-13 naddy int errcode;
2864 1a76625f 2018-10-22 stsp
2865 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2866 276b94a1 2020-11-13 naddy
2867 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2868 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2869 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2870 276b94a1 2020-11-13 naddy
2871 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2872 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2873 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2874 276b94a1 2020-11-13 naddy
2875 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2876 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2877 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2878 1a76625f 2018-10-22 stsp free(s->start_id);
2879 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2880 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2881 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2882 1a76625f 2018-10-22 stsp return err;
2883 1a76625f 2018-10-22 stsp }
2884 1a76625f 2018-10-22 stsp
2885 1a76625f 2018-10-22 stsp static const struct got_error *
2886 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2887 60493ae3 2019-06-20 stsp {
2888 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2889 60493ae3 2019-06-20 stsp
2890 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2891 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2892 60493ae3 2019-06-20 stsp return NULL;
2893 60493ae3 2019-06-20 stsp }
2894 60493ae3 2019-06-20 stsp
2895 60493ae3 2019-06-20 stsp static const struct got_error *
2896 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2897 60493ae3 2019-06-20 stsp {
2898 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2899 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2900 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2901 60493ae3 2019-06-20 stsp
2902 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2903 f9686aa5 2020-03-27 stsp show_log_view(view);
2904 f9686aa5 2020-03-27 stsp update_panels();
2905 f9686aa5 2020-03-27 stsp doupdate();
2906 f9686aa5 2020-03-27 stsp
2907 96e2b566 2019-07-08 stsp if (s->search_entry) {
2908 13add988 2019-10-15 stsp int errcode, ch;
2909 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2910 13add988 2019-10-15 stsp if (errcode)
2911 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2912 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2913 13add988 2019-10-15 stsp ch = wgetch(view->window);
2914 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&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_lock");
2918 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2919 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2920 678cbce5 2019-07-28 stsp return NULL;
2921 678cbce5 2019-07-28 stsp }
2922 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2923 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2924 96e2b566 2019-07-08 stsp else
2925 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2926 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2927 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2928 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2929 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2930 df5e841d 2022-06-23 thomas
2931 df5e841d 2022-06-23 thomas /*
2932 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2933 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2934 1f4d5162 2022-06-23 thomas * might have changed.
2935 df5e841d 2022-06-23 thomas */
2936 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2937 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2938 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2939 df5e841d 2022-06-23 thomas else
2940 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2941 b74feda6 2022-06-23 thomas } else {
2942 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2943 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2944 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2945 df5e841d 2022-06-23 thomas else
2946 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2947 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2948 b74feda6 2022-06-23 thomas }
2949 20be8d96 2019-06-21 stsp } else {
2950 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2951 20be8d96 2019-06-21 stsp }
2952 60493ae3 2019-06-20 stsp
2953 60493ae3 2019-06-20 stsp while (1) {
2954 13add988 2019-10-15 stsp int have_match = 0;
2955 13add988 2019-10-15 stsp
2956 60493ae3 2019-06-20 stsp if (entry == NULL) {
2957 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2958 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2959 f9967bca 2020-03-27 stsp view->search_next_done =
2960 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2961 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2962 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2963 f801134a 2019-06-25 stsp return NULL;
2964 60493ae3 2019-06-20 stsp }
2965 96e2b566 2019-07-08 stsp /*
2966 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2967 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2968 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2969 96e2b566 2019-07-08 stsp */
2970 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2971 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2972 60493ae3 2019-06-20 stsp }
2973 60493ae3 2019-06-20 stsp
2974 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2975 13add988 2019-10-15 stsp &view->regex);
2976 5943eee2 2019-08-13 stsp if (err)
2977 13add988 2019-10-15 stsp break;
2978 13add988 2019-10-15 stsp if (have_match) {
2979 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2980 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2981 60493ae3 2019-06-20 stsp break;
2982 60493ae3 2019-06-20 stsp }
2983 13add988 2019-10-15 stsp
2984 96e2b566 2019-07-08 stsp s->search_entry = entry;
2985 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2986 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2987 b1bf1435 2019-06-21 stsp else
2988 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2989 60493ae3 2019-06-20 stsp }
2990 60493ae3 2019-06-20 stsp
2991 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2992 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2993 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2994 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2995 60493ae3 2019-06-20 stsp if (err)
2996 60493ae3 2019-06-20 stsp return err;
2997 ead14cbe 2019-06-21 stsp cur++;
2998 ead14cbe 2019-06-21 stsp }
2999 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3000 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3001 60493ae3 2019-06-20 stsp if (err)
3002 60493ae3 2019-06-20 stsp return err;
3003 ead14cbe 2019-06-21 stsp cur--;
3004 60493ae3 2019-06-20 stsp }
3005 60493ae3 2019-06-20 stsp }
3006 60493ae3 2019-06-20 stsp
3007 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3008 96e2b566 2019-07-08 stsp
3009 60493ae3 2019-06-20 stsp return NULL;
3010 60493ae3 2019-06-20 stsp }
3011 60493ae3 2019-06-20 stsp
3012 60493ae3 2019-06-20 stsp static const struct got_error *
3013 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3014 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3015 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3016 80ddbec8 2018-04-29 stsp {
3017 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3018 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3019 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3020 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3021 1a76625f 2018-10-22 stsp int errcode;
3022 80ddbec8 2018-04-29 stsp
3023 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3024 f135c941 2020-02-20 stsp free(s->in_repo_path);
3025 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3026 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3027 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3028 f135c941 2020-02-20 stsp }
3029 ecb28ae0 2018-07-16 stsp
3030 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3031 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3032 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3033 78756c87 2020-11-24 stsp
3034 fb2756b9 2018-08-04 stsp s->repo = repo;
3035 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3036 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3037 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3038 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3039 9cd7cbd1 2020-12-07 stsp goto done;
3040 9cd7cbd1 2020-12-07 stsp }
3041 9cd7cbd1 2020-12-07 stsp }
3042 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3043 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3044 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3045 5036bf37 2018-09-24 stsp goto done;
3046 5036bf37 2018-09-24 stsp }
3047 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3048 e5a0f69f 2018-08-18 stsp
3049 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3050 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3051 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3052 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3053 11b20872 2019-11-08 stsp if (err)
3054 11b20872 2019-11-08 stsp goto done;
3055 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3056 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3057 11b20872 2019-11-08 stsp if (err) {
3058 11b20872 2019-11-08 stsp free_colors(&s->colors);
3059 11b20872 2019-11-08 stsp goto done;
3060 11b20872 2019-11-08 stsp }
3061 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3062 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3063 11b20872 2019-11-08 stsp if (err) {
3064 11b20872 2019-11-08 stsp free_colors(&s->colors);
3065 11b20872 2019-11-08 stsp goto done;
3066 11b20872 2019-11-08 stsp }
3067 11b20872 2019-11-08 stsp }
3068 11b20872 2019-11-08 stsp
3069 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3070 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3071 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3072 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3073 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3074 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3075 1a76625f 2018-10-22 stsp
3076 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3077 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3078 1af5eddf 2022-06-23 thomas if (err)
3079 1af5eddf 2022-06-23 thomas goto done;
3080 1af5eddf 2022-06-23 thomas }
3081 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3082 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3083 7cd52833 2022-06-23 thomas if (err)
3084 7cd52833 2022-06-23 thomas goto done;
3085 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3086 b672a97a 2020-01-27 stsp !s->log_branches);
3087 1a76625f 2018-10-22 stsp if (err)
3088 1a76625f 2018-10-22 stsp goto done;
3089 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3090 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3091 c5b78334 2020-01-12 stsp if (err)
3092 c5b78334 2020-01-12 stsp goto done;
3093 1a76625f 2018-10-22 stsp
3094 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3095 1a76625f 2018-10-22 stsp if (errcode) {
3096 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3097 1a76625f 2018-10-22 stsp goto done;
3098 1a76625f 2018-10-22 stsp }
3099 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3100 7c1452c1 2020-03-26 stsp if (errcode) {
3101 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3102 7c1452c1 2020-03-26 stsp goto done;
3103 7c1452c1 2020-03-26 stsp }
3104 1a76625f 2018-10-22 stsp
3105 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3106 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3107 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3108 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3109 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3110 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3111 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3112 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3113 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3114 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3115 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3116 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3117 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3118 ba4f502b 2018-08-04 stsp done:
3119 1a76625f 2018-10-22 stsp if (err)
3120 1a76625f 2018-10-22 stsp close_log_view(view);
3121 ba4f502b 2018-08-04 stsp return err;
3122 ba4f502b 2018-08-04 stsp }
3123 ba4f502b 2018-08-04 stsp
3124 e5a0f69f 2018-08-18 stsp static const struct got_error *
3125 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3126 ba4f502b 2018-08-04 stsp {
3127 f2f6d207 2020-11-24 stsp const struct got_error *err;
3128 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3129 ba4f502b 2018-08-04 stsp
3130 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3131 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3132 2b380cc8 2018-10-24 stsp &s->thread_args);
3133 2b380cc8 2018-10-24 stsp if (errcode)
3134 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3135 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3136 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3137 f2f6d207 2020-11-24 stsp if (err)
3138 f2f6d207 2020-11-24 stsp return err;
3139 f2f6d207 2020-11-24 stsp }
3140 2b380cc8 2018-10-24 stsp }
3141 2b380cc8 2018-10-24 stsp
3142 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3143 b31f89ff 2022-07-01 thomas }
3144 b31f89ff 2022-07-01 thomas
3145 b31f89ff 2022-07-01 thomas static void
3146 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3147 b31f89ff 2022-07-01 thomas {
3148 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3149 b31f89ff 2022-07-01 thomas
3150 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3151 b31f89ff 2022-07-01 thomas view->count = 0;
3152 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3153 b31f89ff 2022-07-01 thomas return;
3154 b31f89ff 2022-07-01 thomas
3155 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3156 b31f89ff 2022-07-01 thomas || home)
3157 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3158 b31f89ff 2022-07-01 thomas
3159 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3160 b31f89ff 2022-07-01 thomas --s->selected;
3161 b31f89ff 2022-07-01 thomas else
3162 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3163 b31f89ff 2022-07-01 thomas
3164 b31f89ff 2022-07-01 thomas select_commit(s);
3165 b31f89ff 2022-07-01 thomas return;
3166 b31f89ff 2022-07-01 thomas }
3167 b31f89ff 2022-07-01 thomas
3168 b31f89ff 2022-07-01 thomas static const struct got_error *
3169 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3170 b31f89ff 2022-07-01 thomas {
3171 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3172 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3173 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3174 b31f89ff 2022-07-01 thomas
3175 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3176 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3177 b31f89ff 2022-07-01 thomas return NULL;
3178 b31f89ff 2022-07-01 thomas
3179 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3180 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3181 b31f89ff 2022-07-01 thomas
3182 7ed048bd 2022-08-12 thomas if (!page) {
3183 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3184 b31f89ff 2022-07-01 thomas ++s->selected;
3185 b31f89ff 2022-07-01 thomas else
3186 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3187 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3188 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3189 7ed048bd 2022-08-12 thomas int n;
3190 7ed048bd 2022-08-12 thomas
3191 7ed048bd 2022-08-12 thomas s->selected = 0;
3192 7ed048bd 2022-08-12 thomas entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3193 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3194 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3195 7ed048bd 2022-08-12 thomas if (entry == NULL)
3196 7ed048bd 2022-08-12 thomas break;
3197 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3198 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3199 7ed048bd 2022-08-12 thomas }
3200 7ed048bd 2022-08-12 thomas if (n > 0)
3201 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3202 b31f89ff 2022-07-01 thomas } else {
3203 bd3f8225 2022-08-12 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1 &&
3204 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3205 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3206 bd3f8225 2022-08-12 thomas s->commits.ncommits - s->selected_entry->idx - 1);
3207 bd3f8225 2022-08-12 thomas else
3208 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3209 b31f89ff 2022-07-01 thomas }
3210 b31f89ff 2022-07-01 thomas if (err)
3211 b31f89ff 2022-07-01 thomas return err;
3212 b31f89ff 2022-07-01 thomas
3213 a5d43cac 2022-07-01 thomas /*
3214 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3215 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3216 a5d43cac 2022-07-01 thomas */
3217 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3218 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3219 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3220 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3221 25100026 2022-08-13 thomas }
3222 a5d43cac 2022-07-01 thomas
3223 b31f89ff 2022-07-01 thomas select_commit(s);
3224 b31f89ff 2022-07-01 thomas
3225 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3226 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3227 b31f89ff 2022-07-01 thomas view->count = 0;
3228 b31f89ff 2022-07-01 thomas
3229 b31f89ff 2022-07-01 thomas return NULL;
3230 e5a0f69f 2018-08-18 stsp }
3231 04cc582a 2018-08-01 stsp
3232 a5d43cac 2022-07-01 thomas static void
3233 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3234 a5d43cac 2022-07-01 thomas {
3235 24415785 2022-07-03 thomas *x = 0;
3236 24415785 2022-07-03 thomas *y = 0;
3237 24415785 2022-07-03 thomas
3238 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3239 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3240 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3241 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3242 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3243 53d2bdd3 2022-07-10 thomas else
3244 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3245 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3246 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3247 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3248 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3249 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3250 53d2bdd3 2022-07-10 thomas else
3251 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3252 53d2bdd3 2022-07-10 thomas }
3253 a5d43cac 2022-07-01 thomas }
3254 a5d43cac 2022-07-01 thomas
3255 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3256 e5a0f69f 2018-08-18 stsp static const struct got_error *
3257 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3258 a5d43cac 2022-07-01 thomas {
3259 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3260 a5d43cac 2022-07-01 thomas
3261 a5d43cac 2022-07-01 thomas view->nlines = y;
3262 64486692 2022-07-07 thomas view->ncols = COLS;
3263 a5d43cac 2022-07-01 thomas err = view_resize(view);
3264 a5d43cac 2022-07-01 thomas if (err)
3265 a5d43cac 2022-07-01 thomas return err;
3266 a5d43cac 2022-07-01 thomas
3267 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3268 a5d43cac 2022-07-01 thomas
3269 a5d43cac 2022-07-01 thomas return err;
3270 07dd3ed3 2022-08-06 thomas }
3271 07dd3ed3 2022-08-06 thomas
3272 07dd3ed3 2022-08-06 thomas static const struct got_error *
3273 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3274 07dd3ed3 2022-08-06 thomas {
3275 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3276 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3277 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3278 25100026 2022-08-13 thomas
3279 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3280 25100026 2022-08-13 thomas return NULL;
3281 07dd3ed3 2022-08-06 thomas
3282 07dd3ed3 2022-08-06 thomas g = view->gline;
3283 07dd3ed3 2022-08-06 thomas view->gline = 0;
3284 07dd3ed3 2022-08-06 thomas
3285 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3286 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3287 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3288 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3289 07dd3ed3 2022-08-06 thomas select_commit(s);
3290 07dd3ed3 2022-08-06 thomas return NULL;
3291 07dd3ed3 2022-08-06 thomas }
3292 07dd3ed3 2022-08-06 thomas
3293 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3294 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3295 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3296 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3297 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3298 07dd3ed3 2022-08-06 thomas if (err)
3299 07dd3ed3 2022-08-06 thomas return err;
3300 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3301 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3302 07dd3ed3 2022-08-06 thomas
3303 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3304 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3305 07dd3ed3 2022-08-06 thomas
3306 07dd3ed3 2022-08-06 thomas select_commit(s);
3307 07dd3ed3 2022-08-06 thomas return NULL;
3308 07dd3ed3 2022-08-06 thomas
3309 a5d43cac 2022-07-01 thomas }
3310 a5d43cac 2022-07-01 thomas
3311 a5d43cac 2022-07-01 thomas static const struct got_error *
3312 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3313 e5a0f69f 2018-08-18 stsp {
3314 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3315 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3316 7ed048bd 2022-08-12 thomas int eos, nscroll;
3317 80ddbec8 2018-04-29 stsp
3318 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3319 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3320 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3321 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3322 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3323 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3324 fb280deb 2021-08-30 stsp }
3325 c0be8933 2022-08-12 thomas if (err)
3326 c0be8933 2022-08-12 thomas return err;
3327 528dedf3 2021-08-30 stsp }
3328 068ab281 2022-07-01 thomas
3329 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3330 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3331 068ab281 2022-07-01 thomas --eos; /* border */
3332 07dd3ed3 2022-08-06 thomas
3333 07dd3ed3 2022-08-06 thomas if (view->gline)
3334 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3335 068ab281 2022-07-01 thomas
3336 528dedf3 2021-08-30 stsp switch (ch) {
3337 1e37a5c2 2019-05-12 jcs case 'q':
3338 1e37a5c2 2019-05-12 jcs s->quit = 1;
3339 05171be4 2022-06-23 thomas break;
3340 05171be4 2022-06-23 thomas case '0':
3341 05171be4 2022-06-23 thomas view->x = 0;
3342 05171be4 2022-06-23 thomas break;
3343 05171be4 2022-06-23 thomas case '$':
3344 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3345 07b0611c 2022-06-23 thomas view->count = 0;
3346 05171be4 2022-06-23 thomas break;
3347 05171be4 2022-06-23 thomas case KEY_RIGHT:
3348 05171be4 2022-06-23 thomas case 'l':
3349 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3350 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3351 07b0611c 2022-06-23 thomas else
3352 07b0611c 2022-06-23 thomas view->count = 0;
3353 1e37a5c2 2019-05-12 jcs break;
3354 05171be4 2022-06-23 thomas case KEY_LEFT:
3355 05171be4 2022-06-23 thomas case 'h':
3356 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3357 07b0611c 2022-06-23 thomas if (view->x <= 0)
3358 07b0611c 2022-06-23 thomas view->count = 0;
3359 05171be4 2022-06-23 thomas break;
3360 1e37a5c2 2019-05-12 jcs case 'k':
3361 1e37a5c2 2019-05-12 jcs case KEY_UP:
3362 1e37a5c2 2019-05-12 jcs case '<':
3363 1e37a5c2 2019-05-12 jcs case ',':
3364 f7140bf5 2021-10-17 thomas case CTRL('p'):
3365 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3366 912a3f79 2021-08-30 j break;
3367 912a3f79 2021-08-30 j case 'g':
3368 912a3f79 2021-08-30 j case KEY_HOME:
3369 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3370 07b0611c 2022-06-23 thomas view->count = 0;
3371 1e37a5c2 2019-05-12 jcs break;
3372 70f17a53 2022-06-13 thomas case CTRL('u'):
3373 23427b14 2022-06-23 thomas case 'u':
3374 70f17a53 2022-06-13 thomas nscroll /= 2;
3375 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3376 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3377 a4292ac5 2019-05-12 jcs case CTRL('b'):
3378 1c5e5faa 2022-06-23 thomas case 'b':
3379 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3380 1e37a5c2 2019-05-12 jcs break;
3381 1e37a5c2 2019-05-12 jcs case 'j':
3382 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3383 1e37a5c2 2019-05-12 jcs case '>':
3384 1e37a5c2 2019-05-12 jcs case '.':
3385 f7140bf5 2021-10-17 thomas case CTRL('n'):
3386 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3387 912a3f79 2021-08-30 j break;
3388 f69c5a46 2022-07-19 thomas case '@':
3389 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3390 f69c5a46 2022-07-19 thomas break;
3391 912a3f79 2021-08-30 j case 'G':
3392 912a3f79 2021-08-30 j case KEY_END: {
3393 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3394 912a3f79 2021-08-30 j * traverse them all. */
3395 07b0611c 2022-06-23 thomas view->count = 0;
3396 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3397 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3398 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3399 7ed048bd 2022-08-12 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3400 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3401 1e37a5c2 2019-05-12 jcs break;
3402 912a3f79 2021-08-30 j }
3403 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3404 23427b14 2022-06-23 thomas case 'd':
3405 70f17a53 2022-06-13 thomas nscroll /= 2;
3406 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3407 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3408 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3409 4c2d69cb 2022-06-23 thomas case 'f':
3410 b31f89ff 2022-07-01 thomas case ' ':
3411 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3412 1e37a5c2 2019-05-12 jcs break;
3413 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3414 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3415 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3416 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3417 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3418 2b779855 2020-12-05 naddy select_commit(s);
3419 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3420 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3421 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3422 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3423 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3424 0bf7f153 2020-12-02 naddy }
3425 1e37a5c2 2019-05-12 jcs break;
3426 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3427 444d5325 2022-07-03 thomas case '\r':
3428 07b0611c 2022-06-23 thomas view->count = 0;
3429 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3430 e5a0f69f 2018-08-18 stsp break;
3431 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3432 1e37a5c2 2019-05-12 jcs break;
3433 1be4947a 2022-07-22 thomas case 'T':
3434 07b0611c 2022-06-23 thomas view->count = 0;
3435 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3436 5036bf37 2018-09-24 stsp break;
3437 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3438 1e37a5c2 2019-05-12 jcs break;
3439 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3440 21355643 2020-12-06 stsp case CTRL('l'):
3441 21355643 2020-12-06 stsp case 'B':
3442 07b0611c 2022-06-23 thomas view->count = 0;
3443 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3444 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3445 1e37a5c2 2019-05-12 jcs break;
3446 21355643 2020-12-06 stsp err = stop_log_thread(s);
3447 74cfe85e 2020-10-20 stsp if (err)
3448 74cfe85e 2020-10-20 stsp return err;
3449 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3450 21355643 2020-12-06 stsp char *parent_path;
3451 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3452 21355643 2020-12-06 stsp if (err)
3453 21355643 2020-12-06 stsp return err;
3454 21355643 2020-12-06 stsp free(s->in_repo_path);
3455 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3456 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3457 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3458 21355643 2020-12-06 stsp struct got_object_id *start_id;
3459 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3460 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3461 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3462 21355643 2020-12-06 stsp if (err)
3463 21355643 2020-12-06 stsp return err;
3464 21355643 2020-12-06 stsp free(s->start_id);
3465 21355643 2020-12-06 stsp s->start_id = start_id;
3466 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3467 21355643 2020-12-06 stsp } else /* 'B' */
3468 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3469 21355643 2020-12-06 stsp
3470 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3471 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3472 bf7e79b3 2022-06-23 thomas if (err)
3473 bf7e79b3 2022-06-23 thomas return err;
3474 bf7e79b3 2022-06-23 thomas }
3475 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3476 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3477 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3478 74cfe85e 2020-10-20 stsp if (err)
3479 21355643 2020-12-06 stsp return err;
3480 51a10b52 2020-12-26 stsp tog_free_refs();
3481 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3482 ca51c541 2020-12-07 stsp if (err)
3483 ca51c541 2020-12-07 stsp return err;
3484 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3485 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3486 d01904d4 2019-06-25 stsp if (err)
3487 d01904d4 2019-06-25 stsp return err;
3488 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3489 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3490 b672a97a 2020-01-27 stsp if (err)
3491 b672a97a 2020-01-27 stsp return err;
3492 21355643 2020-12-06 stsp free_commits(&s->commits);
3493 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3494 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3495 21355643 2020-12-06 stsp s->selected_entry = NULL;
3496 21355643 2020-12-06 stsp s->selected = 0;
3497 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3498 21355643 2020-12-06 stsp s->quit = 0;
3499 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3500 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3501 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3502 94ecf40d 2022-07-22 thomas view->offset = 0;
3503 d01904d4 2019-06-25 stsp break;
3504 1be4947a 2022-07-22 thomas case 'R':
3505 07b0611c 2022-06-23 thomas view->count = 0;
3506 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3507 6458efa5 2020-11-24 stsp break;
3508 1e37a5c2 2019-05-12 jcs default:
3509 07b0611c 2022-06-23 thomas view->count = 0;
3510 1e37a5c2 2019-05-12 jcs break;
3511 899d86c2 2018-05-10 stsp }
3512 e5a0f69f 2018-08-18 stsp
3513 80ddbec8 2018-04-29 stsp return err;
3514 80ddbec8 2018-04-29 stsp }
3515 80ddbec8 2018-04-29 stsp
3516 4ed7e80c 2018-05-20 stsp static const struct got_error *
3517 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3518 c2db6724 2019-01-04 stsp {
3519 c2db6724 2019-01-04 stsp const struct got_error *error;
3520 c2db6724 2019-01-04 stsp
3521 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3522 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3523 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3524 37c06ea4 2019-07-15 stsp #endif
3525 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3526 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3527 c2db6724 2019-01-04 stsp
3528 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3529 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3530 c2db6724 2019-01-04 stsp
3531 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3532 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3533 c2db6724 2019-01-04 stsp
3534 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3535 c2db6724 2019-01-04 stsp if (error != NULL)
3536 c2db6724 2019-01-04 stsp return error;
3537 c2db6724 2019-01-04 stsp
3538 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3539 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3540 c2db6724 2019-01-04 stsp
3541 c2db6724 2019-01-04 stsp return NULL;
3542 c2db6724 2019-01-04 stsp }
3543 c2db6724 2019-01-04 stsp
3544 a915003a 2019-02-05 stsp static void
3545 a915003a 2019-02-05 stsp init_curses(void)
3546 a915003a 2019-02-05 stsp {
3547 296152d1 2022-05-31 thomas /*
3548 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3549 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3550 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3551 296152d1 2022-05-31 thomas */
3552 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3553 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3554 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3555 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3556 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3557 296152d1 2022-05-31 thomas
3558 a915003a 2019-02-05 stsp initscr();
3559 a915003a 2019-02-05 stsp cbreak();
3560 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3561 a915003a 2019-02-05 stsp noecho();
3562 a915003a 2019-02-05 stsp nonl();
3563 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3564 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3565 a915003a 2019-02-05 stsp curs_set(0);
3566 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3567 6d17833f 2019-11-08 stsp start_color();
3568 6d17833f 2019-11-08 stsp use_default_colors();
3569 6d17833f 2019-11-08 stsp }
3570 a915003a 2019-02-05 stsp }
3571 a915003a 2019-02-05 stsp
3572 c2db6724 2019-01-04 stsp static const struct got_error *
3573 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3574 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3575 f135c941 2020-02-20 stsp {
3576 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3577 f135c941 2020-02-20 stsp
3578 f135c941 2020-02-20 stsp if (argc == 0) {
3579 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3580 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3581 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3582 f135c941 2020-02-20 stsp return NULL;
3583 f135c941 2020-02-20 stsp }
3584 f135c941 2020-02-20 stsp
3585 f135c941 2020-02-20 stsp if (worktree) {
3586 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3587 bfd61697 2020-11-14 stsp char *p;
3588 f135c941 2020-02-20 stsp
3589 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3590 f135c941 2020-02-20 stsp if (err)
3591 f135c941 2020-02-20 stsp return err;
3592 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3593 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3594 bfd61697 2020-11-14 stsp p) == -1) {
3595 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3596 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3597 f135c941 2020-02-20 stsp }
3598 f135c941 2020-02-20 stsp free(p);
3599 f135c941 2020-02-20 stsp } else
3600 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3601 f135c941 2020-02-20 stsp
3602 f135c941 2020-02-20 stsp return err;
3603 f135c941 2020-02-20 stsp }
3604 f135c941 2020-02-20 stsp
3605 f135c941 2020-02-20 stsp static const struct got_error *
3606 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3607 9f7d7167 2018-04-29 stsp {
3608 80ddbec8 2018-04-29 stsp const struct got_error *error;
3609 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3610 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3611 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3612 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3613 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3614 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3615 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3616 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3617 04cc582a 2018-08-01 stsp struct tog_view *view;
3618 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3619 80ddbec8 2018-04-29 stsp
3620 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3621 80ddbec8 2018-04-29 stsp switch (ch) {
3622 b672a97a 2020-01-27 stsp case 'b':
3623 b672a97a 2020-01-27 stsp log_branches = 1;
3624 b672a97a 2020-01-27 stsp break;
3625 80ddbec8 2018-04-29 stsp case 'c':
3626 80ddbec8 2018-04-29 stsp start_commit = optarg;
3627 80ddbec8 2018-04-29 stsp break;
3628 ecb28ae0 2018-07-16 stsp case 'r':
3629 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3630 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3631 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3632 9ba1d308 2019-10-21 stsp optarg);
3633 ecb28ae0 2018-07-16 stsp break;
3634 80ddbec8 2018-04-29 stsp default:
3635 17020d27 2019-03-07 stsp usage_log();
3636 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3637 80ddbec8 2018-04-29 stsp }
3638 80ddbec8 2018-04-29 stsp }
3639 80ddbec8 2018-04-29 stsp
3640 80ddbec8 2018-04-29 stsp argc -= optind;
3641 80ddbec8 2018-04-29 stsp argv += optind;
3642 80ddbec8 2018-04-29 stsp
3643 f135c941 2020-02-20 stsp if (argc > 1)
3644 f135c941 2020-02-20 stsp usage_log();
3645 963f97a1 2019-03-18 stsp
3646 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3647 7cd52833 2022-06-23 thomas if (error != NULL)
3648 7cd52833 2022-06-23 thomas goto done;
3649 7cd52833 2022-06-23 thomas
3650 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3651 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3652 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3653 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3654 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3655 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3656 c156c7a4 2020-12-18 stsp goto done;
3657 a1fbf39a 2019-08-11 stsp if (worktree)
3658 6962eb72 2020-02-20 stsp repo_path =
3659 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3660 a1fbf39a 2019-08-11 stsp else
3661 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3662 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3663 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3664 c156c7a4 2020-12-18 stsp goto done;
3665 c156c7a4 2020-12-18 stsp }
3666 ecb28ae0 2018-07-16 stsp }
3667 ecb28ae0 2018-07-16 stsp
3668 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3669 80ddbec8 2018-04-29 stsp if (error != NULL)
3670 ecb28ae0 2018-07-16 stsp goto done;
3671 80ddbec8 2018-04-29 stsp
3672 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3673 f135c941 2020-02-20 stsp repo, worktree);
3674 f135c941 2020-02-20 stsp if (error)
3675 f135c941 2020-02-20 stsp goto done;
3676 f135c941 2020-02-20 stsp
3677 f135c941 2020-02-20 stsp init_curses();
3678 f135c941 2020-02-20 stsp
3679 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3680 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3681 c02c541e 2019-03-29 stsp if (error)
3682 c02c541e 2019-03-29 stsp goto done;
3683 c02c541e 2019-03-29 stsp
3684 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3685 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3686 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3687 87670572 2020-12-26 naddy if (error)
3688 87670572 2020-12-26 naddy goto done;
3689 87670572 2020-12-26 naddy }
3690 51a10b52 2020-12-26 stsp
3691 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3692 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3693 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3694 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3695 d8f38dc4 2020-12-05 stsp if (error)
3696 d8f38dc4 2020-12-05 stsp goto done;
3697 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3698 d8f38dc4 2020-12-05 stsp } else {
3699 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3700 d8f38dc4 2020-12-05 stsp if (error == NULL)
3701 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3702 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3703 d8f38dc4 2020-12-05 stsp goto done;
3704 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3705 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3706 d8f38dc4 2020-12-05 stsp if (error)
3707 d8f38dc4 2020-12-05 stsp goto done;
3708 d8f38dc4 2020-12-05 stsp }
3709 8b473291 2019-02-21 stsp
3710 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3711 04cc582a 2018-08-01 stsp if (view == NULL) {
3712 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3713 04cc582a 2018-08-01 stsp goto done;
3714 04cc582a 2018-08-01 stsp }
3715 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3716 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3717 ba4f502b 2018-08-04 stsp if (error)
3718 ba4f502b 2018-08-04 stsp goto done;
3719 2fc00ff4 2019-08-31 stsp if (worktree) {
3720 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3721 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3722 2fc00ff4 2019-08-31 stsp worktree = NULL;
3723 2fc00ff4 2019-08-31 stsp }
3724 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3725 ecb28ae0 2018-07-16 stsp done:
3726 f135c941 2020-02-20 stsp free(in_repo_path);
3727 ecb28ae0 2018-07-16 stsp free(repo_path);
3728 ecb28ae0 2018-07-16 stsp free(cwd);
3729 899d86c2 2018-05-10 stsp free(start_id);
3730 d8f38dc4 2020-12-05 stsp free(label);
3731 d8f38dc4 2020-12-05 stsp if (ref)
3732 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3733 1d0f4054 2021-06-17 stsp if (repo) {
3734 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3735 1d0f4054 2021-06-17 stsp if (error == NULL)
3736 1d0f4054 2021-06-17 stsp error = close_err;
3737 1d0f4054 2021-06-17 stsp }
3738 ec142235 2019-03-07 stsp if (worktree)
3739 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3740 7cd52833 2022-06-23 thomas if (pack_fds) {
3741 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3742 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3743 7cd52833 2022-06-23 thomas if (error == NULL)
3744 7cd52833 2022-06-23 thomas error = pack_err;
3745 7cd52833 2022-06-23 thomas }
3746 51a10b52 2020-12-26 stsp tog_free_refs();
3747 80ddbec8 2018-04-29 stsp return error;
3748 9f7d7167 2018-04-29 stsp }
3749 9f7d7167 2018-04-29 stsp
3750 4ed7e80c 2018-05-20 stsp __dead static void
3751 9f7d7167 2018-04-29 stsp usage_diff(void)
3752 9f7d7167 2018-04-29 stsp {
3753 80ddbec8 2018-04-29 stsp endwin();
3754 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3755 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
3756 9f7d7167 2018-04-29 stsp exit(1);
3757 b304db33 2018-05-20 stsp }
3758 b304db33 2018-05-20 stsp
3759 6d17833f 2019-11-08 stsp static int
3760 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3761 41605754 2020-11-12 stsp regmatch_t *regmatch)
3762 6d17833f 2019-11-08 stsp {
3763 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3764 6d17833f 2019-11-08 stsp }
3765 6d17833f 2019-11-08 stsp
3766 ef20f542 2022-06-26 thomas static struct tog_color *
3767 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3768 6d17833f 2019-11-08 stsp {
3769 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3770 6d17833f 2019-11-08 stsp
3771 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3772 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3773 6d17833f 2019-11-08 stsp return tc;
3774 6d17833f 2019-11-08 stsp }
3775 6d17833f 2019-11-08 stsp
3776 6d17833f 2019-11-08 stsp return NULL;
3777 6d17833f 2019-11-08 stsp }
3778 6d17833f 2019-11-08 stsp
3779 4ed7e80c 2018-05-20 stsp static const struct got_error *
3780 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3781 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3782 41605754 2020-11-12 stsp {
3783 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3784 666f7b10 2022-06-23 thomas char *exstr = NULL;
3785 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3786 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3787 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3788 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3789 41605754 2020-11-12 stsp
3790 41605754 2020-11-12 stsp *wtotal = 0;
3791 cbea3800 2022-06-23 thomas
3792 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3793 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3794 41605754 2020-11-12 stsp
3795 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3796 666f7b10 2022-06-23 thomas if (err)
3797 666f7b10 2022-06-23 thomas return err;
3798 666f7b10 2022-06-23 thomas
3799 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3800 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3801 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3802 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3803 666f7b10 2022-06-23 thomas goto done;
3804 666f7b10 2022-06-23 thomas }
3805 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3806 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3807 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3808 cbea3800 2022-06-23 thomas goto done;
3809 cbea3800 2022-06-23 thomas }
3810 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3811 1065461d 2022-06-23 thomas if (seg2 == 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 05171be4 2022-06-23 thomas
3816 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3817 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3818 cbea3800 2022-06-23 thomas col_tab_align, 1);
3819 cbea3800 2022-06-23 thomas if (err)
3820 cbea3800 2022-06-23 thomas goto done;
3821 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3822 05171be4 2022-06-23 thomas if (n) {
3823 cbea3800 2022-06-23 thomas free(wline);
3824 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3825 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3826 cbea3800 2022-06-23 thomas if (err)
3827 cbea3800 2022-06-23 thomas goto done;
3828 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3829 cbea3800 2022-06-23 thomas wlimit -= width;
3830 cbea3800 2022-06-23 thomas *wtotal += width;
3831 41605754 2020-11-12 stsp }
3832 41605754 2020-11-12 stsp
3833 41605754 2020-11-12 stsp if (wlimit > 0) {
3834 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3835 cbea3800 2022-06-23 thomas size_t wlen;
3836 cbea3800 2022-06-23 thomas
3837 cbea3800 2022-06-23 thomas free(wline);
3838 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3839 cbea3800 2022-06-23 thomas col_tab_align, 1);
3840 cbea3800 2022-06-23 thomas if (err)
3841 cbea3800 2022-06-23 thomas goto done;
3842 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3843 cbea3800 2022-06-23 thomas while (i < wlen) {
3844 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3845 cbea3800 2022-06-23 thomas if (width == -1) {
3846 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3847 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3848 cbea3800 2022-06-23 thomas goto done;
3849 cbea3800 2022-06-23 thomas }
3850 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3851 cbea3800 2022-06-23 thomas break;
3852 1c5e5faa 2022-06-23 thomas w += width;
3853 cbea3800 2022-06-23 thomas i++;
3854 41605754 2020-11-12 stsp }
3855 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3856 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3857 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3858 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3859 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3860 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3861 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3862 41605754 2020-11-12 stsp }
3863 41605754 2020-11-12 stsp }
3864 41605754 2020-11-12 stsp
3865 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3866 cbea3800 2022-06-23 thomas free(wline);
3867 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3868 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3869 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3870 cbea3800 2022-06-23 thomas col_tab_align, 1);
3871 cbea3800 2022-06-23 thomas if (err)
3872 cbea3800 2022-06-23 thomas goto done;
3873 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3874 cbea3800 2022-06-23 thomas } else {
3875 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3876 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3877 cbea3800 2022-06-23 thomas if (err)
3878 cbea3800 2022-06-23 thomas goto done;
3879 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3880 cbea3800 2022-06-23 thomas }
3881 cbea3800 2022-06-23 thomas *wtotal += width2;
3882 41605754 2020-11-12 stsp }
3883 cbea3800 2022-06-23 thomas done:
3884 05171be4 2022-06-23 thomas free(wline);
3885 666f7b10 2022-06-23 thomas free(exstr);
3886 cbea3800 2022-06-23 thomas free(seg0);
3887 cbea3800 2022-06-23 thomas free(seg1);
3888 cbea3800 2022-06-23 thomas free(seg2);
3889 cbea3800 2022-06-23 thomas return err;
3890 41605754 2020-11-12 stsp }
3891 07dd3ed3 2022-08-06 thomas
3892 07dd3ed3 2022-08-06 thomas static int
3893 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
3894 07dd3ed3 2022-08-06 thomas {
3895 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
3896 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
3897 07dd3ed3 2022-08-06 thomas
3898 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
3899 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
3900 41605754 2020-11-12 stsp
3901 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3902 07dd3ed3 2022-08-06 thomas selected = first;
3903 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3904 07dd3ed3 2022-08-06 thomas f = s->f;
3905 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
3906 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
3907 07dd3ed3 2022-08-06 thomas
3908 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3909 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
3910 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3911 07dd3ed3 2022-08-06 thomas f = s->blame.f;
3912 07dd3ed3 2022-08-06 thomas } else
3913 07dd3ed3 2022-08-06 thomas return 0;
3914 07dd3ed3 2022-08-06 thomas
3915 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
3916 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
3917 07dd3ed3 2022-08-06 thomas return 0;
3918 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3919 07dd3ed3 2022-08-06 thomas rewind(f);
3920 07dd3ed3 2022-08-06 thomas *eof = 0;
3921 07dd3ed3 2022-08-06 thomas *first = 1;
3922 07dd3ed3 2022-08-06 thomas *lineno = 0;
3923 07dd3ed3 2022-08-06 thomas *nprinted = 0;
3924 07dd3ed3 2022-08-06 thomas return 0;
3925 07dd3ed3 2022-08-06 thomas }
3926 07dd3ed3 2022-08-06 thomas
3927 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
3928 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
3929 07dd3ed3 2022-08-06 thomas view->gline = 0;
3930 07dd3ed3 2022-08-06 thomas
3931 07dd3ed3 2022-08-06 thomas return 1;
3932 07dd3ed3 2022-08-06 thomas }
3933 07dd3ed3 2022-08-06 thomas
3934 41605754 2020-11-12 stsp static const struct got_error *
3935 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3936 26ed57b2 2018-05-19 stsp {
3937 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3938 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3939 61e69b96 2018-05-20 stsp const struct got_error *err;
3940 82c78e96 2022-08-06 thomas int nprinted = 0;
3941 b304db33 2018-05-20 stsp char *line;
3942 826082fe 2020-12-10 stsp size_t linesize = 0;
3943 826082fe 2020-12-10 stsp ssize_t linelen;
3944 61e69b96 2018-05-20 stsp wchar_t *wline;
3945 e0b650dd 2018-05-20 stsp int width;
3946 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3947 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3948 fe621944 2020-11-10 stsp off_t line_offset;
3949 26ed57b2 2018-05-19 stsp
3950 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
3951 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
3952 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3953 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3954 fe621944 2020-11-10 stsp
3955 f7d12f7e 2018-08-01 stsp werase(view->window);
3956 a3404814 2018-09-02 stsp
3957 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
3958 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
3959 07dd3ed3 2022-08-06 thomas
3960 a3404814 2018-09-02 stsp if (header) {
3961 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3962 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
3963 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
3964 07dd3ed3 2022-08-06 thomas
3965 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3966 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3967 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3968 f91a2b48 2022-06-23 thomas 0, 0);
3969 135a2da0 2020-11-11 stsp free(line);
3970 135a2da0 2020-11-11 stsp if (err)
3971 a3404814 2018-09-02 stsp return err;
3972 a3404814 2018-09-02 stsp
3973 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3974 a3404814 2018-09-02 stsp wstandout(view->window);
3975 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3976 e54cc94a 2020-11-11 stsp free(wline);
3977 e54cc94a 2020-11-11 stsp wline = NULL;
3978 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3979 a3404814 2018-09-02 stsp wstandend(view->window);
3980 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3981 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3982 26ed57b2 2018-05-19 stsp
3983 a3404814 2018-09-02 stsp if (max_lines <= 1)
3984 a3404814 2018-09-02 stsp return NULL;
3985 a3404814 2018-09-02 stsp max_lines--;
3986 a3404814 2018-09-02 stsp }
3987 a3404814 2018-09-02 stsp
3988 89f1a395 2020-12-01 naddy s->eof = 0;
3989 05171be4 2022-06-23 thomas view->maxx = 0;
3990 826082fe 2020-12-10 stsp line = NULL;
3991 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3992 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
3993 6568f0aa 2022-08-06 thomas attr_t attr = 0;
3994 6568f0aa 2022-08-06 thomas
3995 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3996 826082fe 2020-12-10 stsp if (linelen == -1) {
3997 826082fe 2020-12-10 stsp if (feof(s->f)) {
3998 826082fe 2020-12-10 stsp s->eof = 1;
3999 826082fe 2020-12-10 stsp break;
4000 826082fe 2020-12-10 stsp }
4001 826082fe 2020-12-10 stsp free(line);
4002 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4003 61e69b96 2018-05-20 stsp }
4004 05171be4 2022-06-23 thomas
4005 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4006 07dd3ed3 2022-08-06 thomas continue;
4007 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4008 07dd3ed3 2022-08-06 thomas continue;
4009 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4010 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4011 07dd3ed3 2022-08-06 thomas
4012 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4013 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4014 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4015 cbea3800 2022-06-23 thomas if (err) {
4016 cbea3800 2022-06-23 thomas free(line);
4017 cbea3800 2022-06-23 thomas return err;
4018 cbea3800 2022-06-23 thomas }
4019 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4020 cbea3800 2022-06-23 thomas free(wline);
4021 cbea3800 2022-06-23 thomas wline = NULL;
4022 cbea3800 2022-06-23 thomas
4023 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4024 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4025 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4026 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4027 07dd3ed3 2022-08-06 thomas if (attr)
4028 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4029 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4030 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4031 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4032 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4033 41605754 2020-11-12 stsp if (err) {
4034 41605754 2020-11-12 stsp free(line);
4035 41605754 2020-11-12 stsp return err;
4036 41605754 2020-11-12 stsp }
4037 41605754 2020-11-12 stsp } else {
4038 f1c0ec19 2022-06-23 thomas int skip;
4039 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4040 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4041 f1c0ec19 2022-06-23 thomas if (err) {
4042 f1c0ec19 2022-06-23 thomas free(line);
4043 f1c0ec19 2022-06-23 thomas return err;
4044 f1c0ec19 2022-06-23 thomas }
4045 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4046 f1c0ec19 2022-06-23 thomas free(wline);
4047 41605754 2020-11-12 stsp wline = NULL;
4048 41605754 2020-11-12 stsp }
4049 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4050 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4051 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4052 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4053 07dd3ed3 2022-08-06 thomas } else {
4054 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4055 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4056 07dd3ed3 2022-08-06 thomas }
4057 07dd3ed3 2022-08-06 thomas if (attr)
4058 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4059 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4060 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4061 826082fe 2020-12-10 stsp }
4062 826082fe 2020-12-10 stsp free(line);
4063 fe621944 2020-11-10 stsp if (nprinted >= 1)
4064 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4065 89f1a395 2020-12-01 naddy (nprinted - 1);
4066 fe621944 2020-11-10 stsp else
4067 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4068 26ed57b2 2018-05-19 stsp
4069 a5d43cac 2022-07-01 thomas view_border(view);
4070 c3e9aa98 2019-05-13 jcs
4071 89f1a395 2020-12-01 naddy if (s->eof) {
4072 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4073 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4074 c3e9aa98 2019-05-13 jcs nprinted++;
4075 c3e9aa98 2019-05-13 jcs }
4076 c3e9aa98 2019-05-13 jcs
4077 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4078 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4079 c3e9aa98 2019-05-13 jcs if (err) {
4080 c3e9aa98 2019-05-13 jcs return err;
4081 c3e9aa98 2019-05-13 jcs }
4082 26ed57b2 2018-05-19 stsp
4083 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4084 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4085 e54cc94a 2020-11-11 stsp free(wline);
4086 e54cc94a 2020-11-11 stsp wline = NULL;
4087 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4088 c3e9aa98 2019-05-13 jcs }
4089 c3e9aa98 2019-05-13 jcs
4090 26ed57b2 2018-05-19 stsp return NULL;
4091 abd2672a 2018-12-23 stsp }
4092 abd2672a 2018-12-23 stsp
4093 abd2672a 2018-12-23 stsp static char *
4094 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4095 abd2672a 2018-12-23 stsp {
4096 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4097 09867e48 2019-08-13 stsp char *p, *s;
4098 09867e48 2019-08-13 stsp
4099 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4100 09867e48 2019-08-13 stsp if (tm == NULL)
4101 09867e48 2019-08-13 stsp return NULL;
4102 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4103 09867e48 2019-08-13 stsp if (s == NULL)
4104 09867e48 2019-08-13 stsp return NULL;
4105 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4106 abd2672a 2018-12-23 stsp if (p)
4107 abd2672a 2018-12-23 stsp *p = '\0';
4108 abd2672a 2018-12-23 stsp return s;
4109 9f7d7167 2018-04-29 stsp }
4110 9f7d7167 2018-04-29 stsp
4111 4ed7e80c 2018-05-20 stsp static const struct got_error *
4112 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4113 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4114 0208f208 2020-05-05 stsp {
4115 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4116 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4117 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4118 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4119 0208f208 2020-05-05 stsp
4120 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4121 0208f208 2020-05-05 stsp if (qid != NULL) {
4122 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4123 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4124 ec242592 2022-04-22 thomas &qid->id);
4125 0208f208 2020-05-05 stsp if (err)
4126 0208f208 2020-05-05 stsp return err;
4127 0208f208 2020-05-05 stsp
4128 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4129 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4130 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4131 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4132 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4133 aa8b5dd0 2021-08-01 stsp }
4134 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4135 0208f208 2020-05-05 stsp
4136 0208f208 2020-05-05 stsp }
4137 0208f208 2020-05-05 stsp
4138 0208f208 2020-05-05 stsp if (tree_id1) {
4139 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4140 0208f208 2020-05-05 stsp if (err)
4141 0208f208 2020-05-05 stsp goto done;
4142 0208f208 2020-05-05 stsp }
4143 0208f208 2020-05-05 stsp
4144 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4145 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4146 0208f208 2020-05-05 stsp if (err)
4147 0208f208 2020-05-05 stsp goto done;
4148 0208f208 2020-05-05 stsp
4149 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4150 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4151 0208f208 2020-05-05 stsp done:
4152 0208f208 2020-05-05 stsp if (tree1)
4153 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4154 0208f208 2020-05-05 stsp if (tree2)
4155 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4156 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4157 0208f208 2020-05-05 stsp return err;
4158 0208f208 2020-05-05 stsp }
4159 0208f208 2020-05-05 stsp
4160 0208f208 2020-05-05 stsp static const struct got_error *
4161 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4162 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4163 abd2672a 2018-12-23 stsp {
4164 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4165 fe621944 2020-11-10 stsp
4166 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4167 fe621944 2020-11-10 stsp if (p == NULL)
4168 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4169 82c78e96 2022-08-06 thomas *lines = p;
4170 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4171 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4172 fe621944 2020-11-10 stsp (*nlines)++;
4173 82c78e96 2022-08-06 thomas
4174 fe621944 2020-11-10 stsp return NULL;
4175 fe621944 2020-11-10 stsp }
4176 fe621944 2020-11-10 stsp
4177 fe621944 2020-11-10 stsp static const struct got_error *
4178 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4179 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4180 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4181 fe621944 2020-11-10 stsp {
4182 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4183 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4184 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4185 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4186 45d799e2 2018-12-23 stsp time_t committer_time;
4187 45d799e2 2018-12-23 stsp const char *author, *committer;
4188 8b473291 2019-02-21 stsp char *refs_str = NULL;
4189 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4190 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4191 fe621944 2020-11-10 stsp off_t outoff = 0;
4192 fe621944 2020-11-10 stsp int n;
4193 abd2672a 2018-12-23 stsp
4194 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4195 0208f208 2020-05-05 stsp
4196 8b473291 2019-02-21 stsp if (refs) {
4197 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4198 8b473291 2019-02-21 stsp if (err)
4199 8b473291 2019-02-21 stsp return err;
4200 8b473291 2019-02-21 stsp }
4201 8b473291 2019-02-21 stsp
4202 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4203 abd2672a 2018-12-23 stsp if (err)
4204 abd2672a 2018-12-23 stsp return err;
4205 abd2672a 2018-12-23 stsp
4206 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4207 15a94983 2018-12-23 stsp if (err) {
4208 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4209 15a94983 2018-12-23 stsp goto done;
4210 15a94983 2018-12-23 stsp }
4211 abd2672a 2018-12-23 stsp
4212 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4213 fe621944 2020-11-10 stsp if (err)
4214 fe621944 2020-11-10 stsp goto done;
4215 fe621944 2020-11-10 stsp
4216 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4217 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4218 fe621944 2020-11-10 stsp if (n < 0) {
4219 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4220 abd2672a 2018-12-23 stsp goto done;
4221 abd2672a 2018-12-23 stsp }
4222 fe621944 2020-11-10 stsp outoff += n;
4223 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4224 fe621944 2020-11-10 stsp if (err)
4225 fe621944 2020-11-10 stsp goto done;
4226 fe621944 2020-11-10 stsp
4227 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4228 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4229 fe621944 2020-11-10 stsp if (n < 0) {
4230 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4231 abd2672a 2018-12-23 stsp goto done;
4232 abd2672a 2018-12-23 stsp }
4233 fe621944 2020-11-10 stsp outoff += n;
4234 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4235 fe621944 2020-11-10 stsp if (err)
4236 fe621944 2020-11-10 stsp goto done;
4237 fe621944 2020-11-10 stsp
4238 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4239 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4240 fe621944 2020-11-10 stsp if (datestr) {
4241 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4242 fe621944 2020-11-10 stsp if (n < 0) {
4243 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4244 fe621944 2020-11-10 stsp goto done;
4245 fe621944 2020-11-10 stsp }
4246 fe621944 2020-11-10 stsp outoff += n;
4247 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4248 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4249 fe621944 2020-11-10 stsp if (err)
4250 fe621944 2020-11-10 stsp goto done;
4251 abd2672a 2018-12-23 stsp }
4252 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4253 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4254 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4255 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4256 fe621944 2020-11-10 stsp if (n < 0) {
4257 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4258 fe621944 2020-11-10 stsp goto done;
4259 fe621944 2020-11-10 stsp }
4260 fe621944 2020-11-10 stsp outoff += n;
4261 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4262 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_AUTHOR);
4263 fe621944 2020-11-10 stsp if (err)
4264 fe621944 2020-11-10 stsp goto done;
4265 abd2672a 2018-12-23 stsp }
4266 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4267 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4268 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4269 ac4dc263 2021-09-24 thomas int pn = 1;
4270 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4271 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4272 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4273 ac4dc263 2021-09-24 thomas if (err)
4274 ac4dc263 2021-09-24 thomas goto done;
4275 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4276 ac4dc263 2021-09-24 thomas if (n < 0) {
4277 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4278 ac4dc263 2021-09-24 thomas goto done;
4279 ac4dc263 2021-09-24 thomas }
4280 ac4dc263 2021-09-24 thomas outoff += n;
4281 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4282 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4283 ac4dc263 2021-09-24 thomas if (err)
4284 ac4dc263 2021-09-24 thomas goto done;
4285 ac4dc263 2021-09-24 thomas free(id_str);
4286 ac4dc263 2021-09-24 thomas id_str = NULL;
4287 ac4dc263 2021-09-24 thomas }
4288 ac4dc263 2021-09-24 thomas }
4289 ac4dc263 2021-09-24 thomas
4290 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4291 5943eee2 2019-08-13 stsp if (err)
4292 5943eee2 2019-08-13 stsp goto done;
4293 fe621944 2020-11-10 stsp s = logmsg;
4294 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4295 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4296 fe621944 2020-11-10 stsp if (n < 0) {
4297 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4298 fe621944 2020-11-10 stsp goto done;
4299 fe621944 2020-11-10 stsp }
4300 fe621944 2020-11-10 stsp outoff += n;
4301 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4302 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4303 fe621944 2020-11-10 stsp if (err)
4304 fe621944 2020-11-10 stsp goto done;
4305 abd2672a 2018-12-23 stsp }
4306 fe621944 2020-11-10 stsp
4307 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4308 0208f208 2020-05-05 stsp if (err)
4309 0208f208 2020-05-05 stsp goto done;
4310 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4311 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4312 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4313 fe621944 2020-11-10 stsp if (n < 0) {
4314 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4315 fe621944 2020-11-10 stsp goto done;
4316 fe621944 2020-11-10 stsp }
4317 fe621944 2020-11-10 stsp outoff += n;
4318 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4319 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4320 fe621944 2020-11-10 stsp if (err)
4321 fe621944 2020-11-10 stsp goto done;
4322 0208f208 2020-05-05 stsp free((char *)pe->path);
4323 0208f208 2020-05-05 stsp free(pe->data);
4324 0208f208 2020-05-05 stsp }
4325 fe621944 2020-11-10 stsp
4326 0208f208 2020-05-05 stsp fputc('\n', outfile);
4327 fe621944 2020-11-10 stsp outoff++;
4328 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4329 abd2672a 2018-12-23 stsp done:
4330 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4331 abd2672a 2018-12-23 stsp free(id_str);
4332 5943eee2 2019-08-13 stsp free(logmsg);
4333 8b473291 2019-02-21 stsp free(refs_str);
4334 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4335 7510f233 2020-08-09 stsp if (err) {
4336 82c78e96 2022-08-06 thomas free(*lines);
4337 82c78e96 2022-08-06 thomas *lines = NULL;
4338 ae6a6978 2020-08-09 stsp *nlines = 0;
4339 7510f233 2020-08-09 stsp }
4340 fe621944 2020-11-10 stsp return err;
4341 abd2672a 2018-12-23 stsp }
4342 abd2672a 2018-12-23 stsp
4343 abd2672a 2018-12-23 stsp static const struct got_error *
4344 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4345 26ed57b2 2018-05-19 stsp {
4346 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4347 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4348 15a94983 2018-12-23 stsp int obj_type;
4349 fe621944 2020-11-10 stsp
4350 82c78e96 2022-08-06 thomas free(s->lines);
4351 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4352 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4353 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4354 fe621944 2020-11-10 stsp s->nlines = 0;
4355 26ed57b2 2018-05-19 stsp
4356 511a516b 2018-05-19 stsp f = got_opentemp();
4357 48ae06ee 2018-10-18 stsp if (f == NULL) {
4358 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4359 48ae06ee 2018-10-18 stsp goto done;
4360 48ae06ee 2018-10-18 stsp }
4361 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4362 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4363 fb43ecf1 2019-02-11 stsp goto done;
4364 fb43ecf1 2019-02-11 stsp }
4365 48ae06ee 2018-10-18 stsp s->f = f;
4366 26ed57b2 2018-05-19 stsp
4367 15a94983 2018-12-23 stsp if (s->id1)
4368 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4369 15a94983 2018-12-23 stsp else
4370 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4371 15a94983 2018-12-23 stsp if (err)
4372 15a94983 2018-12-23 stsp goto done;
4373 15a94983 2018-12-23 stsp
4374 15a94983 2018-12-23 stsp switch (obj_type) {
4375 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4376 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4377 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4378 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4379 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4380 26ed57b2 2018-05-19 stsp break;
4381 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4382 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4383 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4384 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4385 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4386 26ed57b2 2018-05-19 stsp break;
4387 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4388 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4389 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4390 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4391 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4392 abd2672a 2018-12-23 stsp
4393 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4394 abd2672a 2018-12-23 stsp if (err)
4395 3ffacbe1 2020-02-02 tracey goto done;
4396 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4397 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4398 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4399 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4400 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4401 f44b1f58 2020-02-02 tracey if (err)
4402 f44b1f58 2020-02-02 tracey goto done;
4403 f44b1f58 2020-02-02 tracey } else {
4404 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4405 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4406 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4407 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4408 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4409 82c78e96 2022-08-06 thomas s->f);
4410 f44b1f58 2020-02-02 tracey if (err)
4411 f44b1f58 2020-02-02 tracey goto done;
4412 f5404e4e 2020-02-02 tracey break;
4413 15a087fe 2019-02-21 stsp }
4414 abd2672a 2018-12-23 stsp }
4415 abd2672a 2018-12-23 stsp }
4416 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4417 abd2672a 2018-12-23 stsp
4418 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4419 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4420 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4421 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4422 26ed57b2 2018-05-19 stsp break;
4423 abd2672a 2018-12-23 stsp }
4424 26ed57b2 2018-05-19 stsp default:
4425 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4426 48ae06ee 2018-10-18 stsp break;
4427 26ed57b2 2018-05-19 stsp }
4428 48ae06ee 2018-10-18 stsp done:
4429 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4430 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4431 48ae06ee 2018-10-18 stsp return err;
4432 48ae06ee 2018-10-18 stsp }
4433 26ed57b2 2018-05-19 stsp
4434 f5215bb9 2019-02-22 stsp static void
4435 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4436 f5215bb9 2019-02-22 stsp {
4437 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4438 f5215bb9 2019-02-22 stsp update_panels();
4439 f5215bb9 2019-02-22 stsp doupdate();
4440 f44b1f58 2020-02-02 tracey }
4441 f44b1f58 2020-02-02 tracey
4442 f44b1f58 2020-02-02 tracey static const struct got_error *
4443 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4444 f44b1f58 2020-02-02 tracey {
4445 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4446 f44b1f58 2020-02-02 tracey
4447 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4448 f44b1f58 2020-02-02 tracey return NULL;
4449 f44b1f58 2020-02-02 tracey }
4450 f44b1f58 2020-02-02 tracey
4451 f44b1f58 2020-02-02 tracey static const struct got_error *
4452 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4453 f44b1f58 2020-02-02 tracey {
4454 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4455 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4456 f44b1f58 2020-02-02 tracey int lineno;
4457 78eecffc 2022-06-23 thomas char *line = NULL;
4458 826082fe 2020-12-10 stsp size_t linesize = 0;
4459 826082fe 2020-12-10 stsp ssize_t linelen;
4460 f44b1f58 2020-02-02 tracey
4461 f44b1f58 2020-02-02 tracey if (!view->searching) {
4462 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4463 f44b1f58 2020-02-02 tracey return NULL;
4464 f44b1f58 2020-02-02 tracey }
4465 f44b1f58 2020-02-02 tracey
4466 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4467 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4468 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4469 f44b1f58 2020-02-02 tracey else
4470 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4471 4b3f9dac 2021-12-17 thomas } else
4472 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4473 f44b1f58 2020-02-02 tracey
4474 f44b1f58 2020-02-02 tracey while (1) {
4475 f44b1f58 2020-02-02 tracey off_t offset;
4476 f44b1f58 2020-02-02 tracey
4477 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4478 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4479 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4480 f44b1f58 2020-02-02 tracey break;
4481 f44b1f58 2020-02-02 tracey }
4482 f44b1f58 2020-02-02 tracey
4483 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4484 f44b1f58 2020-02-02 tracey lineno = 1;
4485 f44b1f58 2020-02-02 tracey else
4486 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4487 f44b1f58 2020-02-02 tracey }
4488 f44b1f58 2020-02-02 tracey
4489 82c78e96 2022-08-06 thomas offset = s->lines[lineno - 1].offset;
4490 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4491 f44b1f58 2020-02-02 tracey free(line);
4492 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4493 f44b1f58 2020-02-02 tracey }
4494 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4495 78eecffc 2022-06-23 thomas if (linelen != -1) {
4496 78eecffc 2022-06-23 thomas char *exstr;
4497 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4498 78eecffc 2022-06-23 thomas if (err)
4499 78eecffc 2022-06-23 thomas break;
4500 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4501 78eecffc 2022-06-23 thomas &view->regmatch)) {
4502 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4503 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4504 78eecffc 2022-06-23 thomas free(exstr);
4505 78eecffc 2022-06-23 thomas break;
4506 78eecffc 2022-06-23 thomas }
4507 78eecffc 2022-06-23 thomas free(exstr);
4508 f44b1f58 2020-02-02 tracey }
4509 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4510 f44b1f58 2020-02-02 tracey lineno++;
4511 f44b1f58 2020-02-02 tracey else
4512 f44b1f58 2020-02-02 tracey lineno--;
4513 f44b1f58 2020-02-02 tracey }
4514 826082fe 2020-12-10 stsp free(line);
4515 f44b1f58 2020-02-02 tracey
4516 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4517 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4518 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4519 f44b1f58 2020-02-02 tracey }
4520 f44b1f58 2020-02-02 tracey
4521 05171be4 2022-06-23 thomas return err;
4522 f5215bb9 2019-02-22 stsp }
4523 f5215bb9 2019-02-22 stsp
4524 48ae06ee 2018-10-18 stsp static const struct got_error *
4525 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4526 a0f32f33 2022-06-13 thomas {
4527 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4528 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4529 a0f32f33 2022-06-13 thomas
4530 a0f32f33 2022-06-13 thomas free(s->id1);
4531 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4532 a0f32f33 2022-06-13 thomas free(s->id2);
4533 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4534 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4535 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4536 a0f32f33 2022-06-13 thomas s->f = NULL;
4537 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4538 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4539 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4540 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4541 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4542 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4543 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4544 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4545 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4546 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4547 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4548 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4549 82c78e96 2022-08-06 thomas free(s->lines);
4550 82c78e96 2022-08-06 thomas s->lines = NULL;
4551 a0f32f33 2022-06-13 thomas s->nlines = 0;
4552 a0f32f33 2022-06-13 thomas return err;
4553 a0f32f33 2022-06-13 thomas }
4554 a0f32f33 2022-06-13 thomas
4555 a0f32f33 2022-06-13 thomas static const struct got_error *
4556 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4557 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4558 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4559 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4560 48ae06ee 2018-10-18 stsp {
4561 48ae06ee 2018-10-18 stsp const struct got_error *err;
4562 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4563 5dc9f4bc 2018-08-04 stsp
4564 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4565 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4566 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4567 a0f32f33 2022-06-13 thomas
4568 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4569 15a94983 2018-12-23 stsp int type1, type2;
4570 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4571 15a94983 2018-12-23 stsp if (err)
4572 15a94983 2018-12-23 stsp return err;
4573 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4574 15a94983 2018-12-23 stsp if (err)
4575 15a94983 2018-12-23 stsp return err;
4576 15a94983 2018-12-23 stsp
4577 15a94983 2018-12-23 stsp if (type1 != type2)
4578 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4579 15a94983 2018-12-23 stsp }
4580 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4581 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4582 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4583 f44b1f58 2020-02-02 tracey s->repo = repo;
4584 f44b1f58 2020-02-02 tracey s->id1 = id1;
4585 f44b1f58 2020-02-02 tracey s->id2 = id2;
4586 3dbaef42 2020-11-24 stsp s->label1 = label1;
4587 3dbaef42 2020-11-24 stsp s->label2 = label2;
4588 48ae06ee 2018-10-18 stsp
4589 15a94983 2018-12-23 stsp if (id1) {
4590 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4591 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4592 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4593 48ae06ee 2018-10-18 stsp } else
4594 5465d566 2020-02-01 tracey s->id1 = NULL;
4595 48ae06ee 2018-10-18 stsp
4596 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4597 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4598 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4599 a0f32f33 2022-06-13 thomas goto done;
4600 48ae06ee 2018-10-18 stsp }
4601 a0f32f33 2022-06-13 thomas
4602 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4603 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4604 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4605 9a1d7435 2022-06-23 thomas goto done;
4606 9a1d7435 2022-06-23 thomas }
4607 9a1d7435 2022-06-23 thomas
4608 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4609 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4610 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4611 a0f32f33 2022-06-13 thomas goto done;
4612 a0f32f33 2022-06-13 thomas }
4613 a0f32f33 2022-06-13 thomas
4614 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4615 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4616 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4617 19a6a6b5 2022-07-01 thomas goto done;
4618 19a6a6b5 2022-07-01 thomas }
4619 19a6a6b5 2022-07-01 thomas
4620 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4621 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4622 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4623 19a6a6b5 2022-07-01 thomas goto done;
4624 19a6a6b5 2022-07-01 thomas }
4625 19a6a6b5 2022-07-01 thomas
4626 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4627 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4628 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4629 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4630 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4631 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4632 5465d566 2020-02-01 tracey s->repo = repo;
4633 6d17833f 2019-11-08 stsp
4634 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4635 6568f0aa 2022-08-06 thomas int rc;
4636 6d17833f 2019-11-08 stsp
4637 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
4638 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4639 6568f0aa 2022-08-06 thomas if (rc != ERR)
4640 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
4641 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4642 6568f0aa 2022-08-06 thomas if (rc != ERR)
4643 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
4644 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4645 6568f0aa 2022-08-06 thomas if (rc != ERR)
4646 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
4647 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4648 6568f0aa 2022-08-06 thomas if (rc != ERR)
4649 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
4650 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4651 6568f0aa 2022-08-06 thomas if (rc != ERR)
4652 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4653 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4654 6568f0aa 2022-08-06 thomas if (rc != ERR)
4655 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4656 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4657 6568f0aa 2022-08-06 thomas if (rc != ERR)
4658 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4659 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
4660 6568f0aa 2022-08-06 thomas if (rc != ERR)
4661 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
4662 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
4663 6568f0aa 2022-08-06 thomas if (rc == ERR) {
4664 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
4665 a0f32f33 2022-06-13 thomas goto done;
4666 6568f0aa 2022-08-06 thomas }
4667 6d17833f 2019-11-08 stsp }
4668 5dc9f4bc 2018-08-04 stsp
4669 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4670 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4671 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4672 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4673 f5215bb9 2019-02-22 stsp
4674 5465d566 2020-02-01 tracey err = create_diff(s);
4675 48ae06ee 2018-10-18 stsp
4676 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4677 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4678 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4679 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4680 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4681 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4682 a0f32f33 2022-06-13 thomas done:
4683 a0f32f33 2022-06-13 thomas if (err)
4684 a0f32f33 2022-06-13 thomas close_diff_view(view);
4685 e5a0f69f 2018-08-18 stsp return err;
4686 5dc9f4bc 2018-08-04 stsp }
4687 5dc9f4bc 2018-08-04 stsp
4688 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4689 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4690 5dc9f4bc 2018-08-04 stsp {
4691 a3404814 2018-09-02 stsp const struct got_error *err;
4692 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4693 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4694 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4695 a3404814 2018-09-02 stsp
4696 a3404814 2018-09-02 stsp if (s->id1) {
4697 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4698 a3404814 2018-09-02 stsp if (err)
4699 a3404814 2018-09-02 stsp return err;
4700 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
4701 3dbaef42 2020-11-24 stsp } else
4702 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4703 3dbaef42 2020-11-24 stsp
4704 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4705 a3404814 2018-09-02 stsp if (err)
4706 a3404814 2018-09-02 stsp return err;
4707 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
4708 26ed57b2 2018-05-19 stsp
4709 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4710 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4711 a3404814 2018-09-02 stsp free(id_str1);
4712 a3404814 2018-09-02 stsp free(id_str2);
4713 a3404814 2018-09-02 stsp return err;
4714 a3404814 2018-09-02 stsp }
4715 a3404814 2018-09-02 stsp free(id_str1);
4716 a3404814 2018-09-02 stsp free(id_str2);
4717 a3404814 2018-09-02 stsp
4718 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4719 267bb3b8 2021-08-01 stsp free(header);
4720 267bb3b8 2021-08-01 stsp return err;
4721 15a087fe 2019-02-21 stsp }
4722 15a087fe 2019-02-21 stsp
4723 15a087fe 2019-02-21 stsp static const struct got_error *
4724 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4725 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4726 15a087fe 2019-02-21 stsp {
4727 d7a04538 2019-02-21 stsp const struct got_error *err;
4728 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4729 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4730 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4731 15a087fe 2019-02-21 stsp
4732 15a087fe 2019-02-21 stsp free(s->id2);
4733 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4734 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4735 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4736 15a087fe 2019-02-21 stsp
4737 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4738 d7a04538 2019-02-21 stsp if (err)
4739 d7a04538 2019-02-21 stsp return err;
4740 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4741 15a087fe 2019-02-21 stsp free(s->id1);
4742 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4743 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4744 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4745 15a087fe 2019-02-21 stsp return NULL;
4746 0cf4efb1 2018-09-29 stsp }
4747 0cf4efb1 2018-09-29 stsp
4748 0cf4efb1 2018-09-29 stsp static const struct got_error *
4749 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4750 adf4c9e0 2022-07-03 thomas {
4751 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4752 adf4c9e0 2022-07-03 thomas
4753 adf4c9e0 2022-07-03 thomas view->count = 0;
4754 adf4c9e0 2022-07-03 thomas wclear(view->window);
4755 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4756 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4757 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4758 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4759 adf4c9e0 2022-07-03 thomas return create_diff(s);
4760 82c78e96 2022-08-06 thomas }
4761 82c78e96 2022-08-06 thomas
4762 82c78e96 2022-08-06 thomas static void
4763 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4764 82c78e96 2022-08-06 thomas {
4765 82c78e96 2022-08-06 thomas int start, i;
4766 82c78e96 2022-08-06 thomas
4767 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
4768 82c78e96 2022-08-06 thomas
4769 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4770 82c78e96 2022-08-06 thomas if (i == 0)
4771 82c78e96 2022-08-06 thomas i = s->nlines - 1;
4772 82c78e96 2022-08-06 thomas if (--i == start)
4773 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4774 82c78e96 2022-08-06 thomas }
4775 82c78e96 2022-08-06 thomas
4776 82c78e96 2022-08-06 thomas s->selected_line = 1;
4777 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4778 adf4c9e0 2022-07-03 thomas }
4779 adf4c9e0 2022-07-03 thomas
4780 82c78e96 2022-08-06 thomas static void
4781 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4782 82c78e96 2022-08-06 thomas {
4783 82c78e96 2022-08-06 thomas int start, i;
4784 82c78e96 2022-08-06 thomas
4785 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
4786 82c78e96 2022-08-06 thomas
4787 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4788 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
4789 82c78e96 2022-08-06 thomas i = 0;
4790 82c78e96 2022-08-06 thomas if (++i == start)
4791 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4792 82c78e96 2022-08-06 thomas }
4793 82c78e96 2022-08-06 thomas
4794 82c78e96 2022-08-06 thomas s->selected_line = 1;
4795 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4796 82c78e96 2022-08-06 thomas }
4797 82c78e96 2022-08-06 thomas
4798 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4799 4fc71f3b 2022-07-12 thomas int, int, int);
4800 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4801 4fc71f3b 2022-07-12 thomas int, int);
4802 4fc71f3b 2022-07-12 thomas
4803 adf4c9e0 2022-07-03 thomas static const struct got_error *
4804 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4805 e5a0f69f 2018-08-18 stsp {
4806 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4807 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4808 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4809 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4810 826082fe 2020-12-10 stsp char *line = NULL;
4811 826082fe 2020-12-10 stsp size_t linesize = 0;
4812 826082fe 2020-12-10 stsp ssize_t linelen;
4813 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4814 e5a0f69f 2018-08-18 stsp
4815 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
4816 82c78e96 2022-08-06 thomas
4817 e5a0f69f 2018-08-18 stsp switch (ch) {
4818 05171be4 2022-06-23 thomas case '0':
4819 05171be4 2022-06-23 thomas view->x = 0;
4820 05171be4 2022-06-23 thomas break;
4821 05171be4 2022-06-23 thomas case '$':
4822 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4823 07b0611c 2022-06-23 thomas view->count = 0;
4824 05171be4 2022-06-23 thomas break;
4825 05171be4 2022-06-23 thomas case KEY_RIGHT:
4826 05171be4 2022-06-23 thomas case 'l':
4827 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4828 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4829 07b0611c 2022-06-23 thomas else
4830 07b0611c 2022-06-23 thomas view->count = 0;
4831 05171be4 2022-06-23 thomas break;
4832 05171be4 2022-06-23 thomas case KEY_LEFT:
4833 05171be4 2022-06-23 thomas case 'h':
4834 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4835 07b0611c 2022-06-23 thomas if (view->x <= 0)
4836 07b0611c 2022-06-23 thomas view->count = 0;
4837 05171be4 2022-06-23 thomas break;
4838 64453f7e 2020-11-21 stsp case 'a':
4839 3dbaef42 2020-11-24 stsp case 'w':
4840 3dbaef42 2020-11-24 stsp if (ch == 'a')
4841 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4842 3dbaef42 2020-11-24 stsp if (ch == 'w')
4843 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4844 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4845 912a3f79 2021-08-30 j break;
4846 912a3f79 2021-08-30 j case 'g':
4847 912a3f79 2021-08-30 j case KEY_HOME:
4848 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4849 07b0611c 2022-06-23 thomas view->count = 0;
4850 64453f7e 2020-11-21 stsp break;
4851 912a3f79 2021-08-30 j case 'G':
4852 912a3f79 2021-08-30 j case KEY_END:
4853 07b0611c 2022-06-23 thomas view->count = 0;
4854 912a3f79 2021-08-30 j if (s->eof)
4855 912a3f79 2021-08-30 j break;
4856 912a3f79 2021-08-30 j
4857 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4858 912a3f79 2021-08-30 j s->eof = 1;
4859 912a3f79 2021-08-30 j break;
4860 1e37a5c2 2019-05-12 jcs case 'k':
4861 1e37a5c2 2019-05-12 jcs case KEY_UP:
4862 f7140bf5 2021-10-17 thomas case CTRL('p'):
4863 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4864 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4865 07b0611c 2022-06-23 thomas else
4866 07b0611c 2022-06-23 thomas view->count = 0;
4867 1e37a5c2 2019-05-12 jcs break;
4868 70f17a53 2022-06-13 thomas case CTRL('u'):
4869 23427b14 2022-06-23 thomas case 'u':
4870 70f17a53 2022-06-13 thomas nscroll /= 2;
4871 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4872 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4873 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4874 1c5e5faa 2022-06-23 thomas case 'b':
4875 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4876 07b0611c 2022-06-23 thomas view->count = 0;
4877 26ed57b2 2018-05-19 stsp break;
4878 07b0611c 2022-06-23 thomas }
4879 1e37a5c2 2019-05-12 jcs i = 0;
4880 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4881 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4882 1e37a5c2 2019-05-12 jcs break;
4883 1e37a5c2 2019-05-12 jcs case 'j':
4884 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4885 f7140bf5 2021-10-17 thomas case CTRL('n'):
4886 1e37a5c2 2019-05-12 jcs if (!s->eof)
4887 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4888 07b0611c 2022-06-23 thomas else
4889 07b0611c 2022-06-23 thomas view->count = 0;
4890 1e37a5c2 2019-05-12 jcs break;
4891 70f17a53 2022-06-13 thomas case CTRL('d'):
4892 23427b14 2022-06-23 thomas case 'd':
4893 70f17a53 2022-06-13 thomas nscroll /= 2;
4894 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4895 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4896 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4897 1c5e5faa 2022-06-23 thomas case 'f':
4898 1e37a5c2 2019-05-12 jcs case ' ':
4899 07b0611c 2022-06-23 thomas if (s->eof) {
4900 07b0611c 2022-06-23 thomas view->count = 0;
4901 1e37a5c2 2019-05-12 jcs break;
4902 07b0611c 2022-06-23 thomas }
4903 1e37a5c2 2019-05-12 jcs i = 0;
4904 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4905 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4906 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4907 826082fe 2020-12-10 stsp if (linelen == -1) {
4908 826082fe 2020-12-10 stsp if (feof(s->f)) {
4909 826082fe 2020-12-10 stsp s->eof = 1;
4910 826082fe 2020-12-10 stsp } else
4911 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4912 34bc9ec9 2019-02-22 stsp break;
4913 826082fe 2020-12-10 stsp }
4914 1e37a5c2 2019-05-12 jcs }
4915 826082fe 2020-12-10 stsp free(line);
4916 1e37a5c2 2019-05-12 jcs break;
4917 82c78e96 2022-08-06 thomas case '(':
4918 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
4919 82c78e96 2022-08-06 thomas break;
4920 82c78e96 2022-08-06 thomas case ')':
4921 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
4922 82c78e96 2022-08-06 thomas break;
4923 82c78e96 2022-08-06 thomas case '{':
4924 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
4925 82c78e96 2022-08-06 thomas break;
4926 82c78e96 2022-08-06 thomas case '}':
4927 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
4928 82c78e96 2022-08-06 thomas break;
4929 1e37a5c2 2019-05-12 jcs case '[':
4930 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4931 1e37a5c2 2019-05-12 jcs s->diff_context--;
4932 aa61903a 2021-12-31 thomas s->matched_line = 0;
4933 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4934 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4935 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4936 27829c9e 2020-11-21 stsp s->nlines) {
4937 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4938 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4939 27829c9e 2020-11-21 stsp }
4940 07b0611c 2022-06-23 thomas } else
4941 07b0611c 2022-06-23 thomas view->count = 0;
4942 1e37a5c2 2019-05-12 jcs break;
4943 1e37a5c2 2019-05-12 jcs case ']':
4944 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4945 1e37a5c2 2019-05-12 jcs s->diff_context++;
4946 aa61903a 2021-12-31 thomas s->matched_line = 0;
4947 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4948 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4949 07b0611c 2022-06-23 thomas } else
4950 07b0611c 2022-06-23 thomas view->count = 0;
4951 1e37a5c2 2019-05-12 jcs break;
4952 1e37a5c2 2019-05-12 jcs case '<':
4953 1e37a5c2 2019-05-12 jcs case ',':
4954 777aae21 2022-07-20 thomas case 'K':
4955 4fc71f3b 2022-07-12 thomas up = 1;
4956 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4957 4fc71f3b 2022-07-12 thomas case '>':
4958 4fc71f3b 2022-07-12 thomas case '.':
4959 777aae21 2022-07-20 thomas case 'J':
4960 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4961 07b0611c 2022-06-23 thomas view->count = 0;
4962 48ae06ee 2018-10-18 stsp break;
4963 07b0611c 2022-06-23 thomas }
4964 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4965 6524637e 2019-02-21 stsp
4966 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4967 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4968 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4969 15a087fe 2019-02-21 stsp
4970 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4971 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4972 4fc71f3b 2022-07-12 thomas if (err)
4973 4fc71f3b 2022-07-12 thomas break;
4974 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4975 15a087fe 2019-02-21 stsp
4976 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4977 4fc71f3b 2022-07-12 thomas break;
4978 15a087fe 2019-02-21 stsp
4979 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4980 4fc71f3b 2022-07-12 thomas if (err)
4981 4fc71f3b 2022-07-12 thomas break;
4982 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4983 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4984 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4985 5e224a3e 2019-02-22 stsp
4986 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4987 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4988 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4989 4fc71f3b 2022-07-12 thomas
4990 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4991 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4992 4fc71f3b 2022-07-12 thomas if (err)
4993 4fc71f3b 2022-07-12 thomas break;
4994 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4995 5a8b5076 2020-12-05 stsp
4996 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4997 4fc71f3b 2022-07-12 thomas break;
4998 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4999 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5000 4fc71f3b 2022-07-12 thomas bs->selected_line);
5001 4fc71f3b 2022-07-12 thomas if (id == NULL)
5002 4fc71f3b 2022-07-12 thomas break;
5003 15a087fe 2019-02-21 stsp
5004 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5005 4fc71f3b 2022-07-12 thomas break;
5006 15a087fe 2019-02-21 stsp
5007 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5008 4fc71f3b 2022-07-12 thomas if (err)
5009 4fc71f3b 2022-07-12 thomas break;
5010 4fc71f3b 2022-07-12 thomas }
5011 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5012 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5013 aa61903a 2021-12-31 thomas s->matched_line = 0;
5014 05171be4 2022-06-23 thomas view->x = 0;
5015 1e37a5c2 2019-05-12 jcs
5016 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5017 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5018 1e37a5c2 2019-05-12 jcs break;
5019 1e37a5c2 2019-05-12 jcs default:
5020 07b0611c 2022-06-23 thomas view->count = 0;
5021 1e37a5c2 2019-05-12 jcs break;
5022 26ed57b2 2018-05-19 stsp }
5023 e5a0f69f 2018-08-18 stsp
5024 bcbd79e2 2018-08-19 stsp return err;
5025 26ed57b2 2018-05-19 stsp }
5026 26ed57b2 2018-05-19 stsp
5027 4ed7e80c 2018-05-20 stsp static const struct got_error *
5028 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5029 9f7d7167 2018-04-29 stsp {
5030 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5031 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5032 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5033 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5034 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5035 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5036 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5037 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5038 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5039 3dbaef42 2020-11-24 stsp const char *errstr;
5040 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5041 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5042 70ac5f84 2019-03-28 stsp
5043 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5044 26ed57b2 2018-05-19 stsp switch (ch) {
5045 64453f7e 2020-11-21 stsp case 'a':
5046 64453f7e 2020-11-21 stsp force_text_diff = 1;
5047 3dbaef42 2020-11-24 stsp break;
5048 3dbaef42 2020-11-24 stsp case 'C':
5049 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5050 3dbaef42 2020-11-24 stsp &errstr);
5051 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5052 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5053 8f666e67 2022-02-12 thomas errstr, errstr);
5054 64453f7e 2020-11-21 stsp break;
5055 09b5bff8 2020-02-23 naddy case 'r':
5056 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5057 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5058 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5059 09b5bff8 2020-02-23 naddy optarg);
5060 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5061 09b5bff8 2020-02-23 naddy break;
5062 3dbaef42 2020-11-24 stsp case 'w':
5063 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5064 3dbaef42 2020-11-24 stsp break;
5065 26ed57b2 2018-05-19 stsp default:
5066 17020d27 2019-03-07 stsp usage_diff();
5067 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5068 26ed57b2 2018-05-19 stsp }
5069 26ed57b2 2018-05-19 stsp }
5070 26ed57b2 2018-05-19 stsp
5071 26ed57b2 2018-05-19 stsp argc -= optind;
5072 26ed57b2 2018-05-19 stsp argv += optind;
5073 26ed57b2 2018-05-19 stsp
5074 26ed57b2 2018-05-19 stsp if (argc == 0) {
5075 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5076 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5077 15a94983 2018-12-23 stsp id_str1 = argv[0];
5078 15a94983 2018-12-23 stsp id_str2 = argv[1];
5079 26ed57b2 2018-05-19 stsp } else
5080 26ed57b2 2018-05-19 stsp usage_diff();
5081 eb6600df 2019-01-04 stsp
5082 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5083 7cd52833 2022-06-23 thomas if (error)
5084 7cd52833 2022-06-23 thomas goto done;
5085 7cd52833 2022-06-23 thomas
5086 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5087 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5088 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5089 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5090 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5091 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5092 c156c7a4 2020-12-18 stsp goto done;
5093 a273ac94 2020-02-23 naddy if (worktree)
5094 a273ac94 2020-02-23 naddy repo_path =
5095 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5096 a273ac94 2020-02-23 naddy else
5097 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5098 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5099 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5100 c156c7a4 2020-12-18 stsp goto done;
5101 c156c7a4 2020-12-18 stsp }
5102 a273ac94 2020-02-23 naddy }
5103 a273ac94 2020-02-23 naddy
5104 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5105 eb6600df 2019-01-04 stsp if (error)
5106 eb6600df 2019-01-04 stsp goto done;
5107 26ed57b2 2018-05-19 stsp
5108 a273ac94 2020-02-23 naddy init_curses();
5109 a273ac94 2020-02-23 naddy
5110 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5111 51a10b52 2020-12-26 stsp if (error)
5112 51a10b52 2020-12-26 stsp goto done;
5113 51a10b52 2020-12-26 stsp
5114 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5115 26ed57b2 2018-05-19 stsp if (error)
5116 26ed57b2 2018-05-19 stsp goto done;
5117 26ed57b2 2018-05-19 stsp
5118 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5119 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
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(&id2, &label2, id_str2,
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 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5129 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5130 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5131 ea5e7bb5 2018-08-01 stsp goto done;
5132 ea5e7bb5 2018-08-01 stsp }
5133 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5134 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5135 5dc9f4bc 2018-08-04 stsp if (error)
5136 5dc9f4bc 2018-08-04 stsp goto done;
5137 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5138 26ed57b2 2018-05-19 stsp done:
5139 3dbaef42 2020-11-24 stsp free(label1);
5140 3dbaef42 2020-11-24 stsp free(label2);
5141 c02c541e 2019-03-29 stsp free(repo_path);
5142 a273ac94 2020-02-23 naddy free(cwd);
5143 1d0f4054 2021-06-17 stsp if (repo) {
5144 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5145 1d0f4054 2021-06-17 stsp if (error == NULL)
5146 1d0f4054 2021-06-17 stsp error = close_err;
5147 1d0f4054 2021-06-17 stsp }
5148 a273ac94 2020-02-23 naddy if (worktree)
5149 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5150 7cd52833 2022-06-23 thomas if (pack_fds) {
5151 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5152 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5153 7cd52833 2022-06-23 thomas if (error == NULL)
5154 7cd52833 2022-06-23 thomas error = pack_err;
5155 7cd52833 2022-06-23 thomas }
5156 51a10b52 2020-12-26 stsp tog_free_refs();
5157 26ed57b2 2018-05-19 stsp return error;
5158 9f7d7167 2018-04-29 stsp }
5159 9f7d7167 2018-04-29 stsp
5160 4ed7e80c 2018-05-20 stsp __dead static void
5161 9f7d7167 2018-04-29 stsp usage_blame(void)
5162 9f7d7167 2018-04-29 stsp {
5163 80ddbec8 2018-04-29 stsp endwin();
5164 91198554 2022-06-23 thomas fprintf(stderr,
5165 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5166 9f7d7167 2018-04-29 stsp getprogname());
5167 9f7d7167 2018-04-29 stsp exit(1);
5168 9f7d7167 2018-04-29 stsp }
5169 84451b3e 2018-07-10 stsp
5170 84451b3e 2018-07-10 stsp struct tog_blame_line {
5171 84451b3e 2018-07-10 stsp int annotated;
5172 84451b3e 2018-07-10 stsp struct got_object_id *id;
5173 84451b3e 2018-07-10 stsp };
5174 9f7d7167 2018-04-29 stsp
5175 4ed7e80c 2018-05-20 stsp static const struct got_error *
5176 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5177 84451b3e 2018-07-10 stsp {
5178 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5179 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5180 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5181 84451b3e 2018-07-10 stsp const struct got_error *err;
5182 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5183 826082fe 2020-12-10 stsp char *line = NULL;
5184 826082fe 2020-12-10 stsp size_t linesize = 0;
5185 826082fe 2020-12-10 stsp ssize_t linelen;
5186 84451b3e 2018-07-10 stsp wchar_t *wline;
5187 27a741e5 2019-09-11 stsp int width;
5188 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5189 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5190 ab089a2a 2018-07-12 stsp char *id_str;
5191 11b20872 2019-11-08 stsp struct tog_color *tc;
5192 ab089a2a 2018-07-12 stsp
5193 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5194 ab089a2a 2018-07-12 stsp if (err)
5195 ab089a2a 2018-07-12 stsp return err;
5196 84451b3e 2018-07-10 stsp
5197 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5198 f7d12f7e 2018-08-01 stsp werase(view->window);
5199 84451b3e 2018-07-10 stsp
5200 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5201 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5202 ab089a2a 2018-07-12 stsp free(id_str);
5203 ab089a2a 2018-07-12 stsp return err;
5204 ab089a2a 2018-07-12 stsp }
5205 ab089a2a 2018-07-12 stsp
5206 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5207 ab089a2a 2018-07-12 stsp free(line);
5208 2550e4c3 2018-07-13 stsp line = NULL;
5209 1cae65b4 2019-09-22 stsp if (err)
5210 1cae65b4 2019-09-22 stsp return err;
5211 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5212 a3404814 2018-09-02 stsp wstandout(view->window);
5213 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5214 11b20872 2019-11-08 stsp if (tc)
5215 11b20872 2019-11-08 stsp wattr_on(view->window,
5216 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5217 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5218 11b20872 2019-11-08 stsp if (tc)
5219 11b20872 2019-11-08 stsp wattr_off(view->window,
5220 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5221 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5222 a3404814 2018-09-02 stsp wstandend(view->window);
5223 2550e4c3 2018-07-13 stsp free(wline);
5224 2550e4c3 2018-07-13 stsp wline = NULL;
5225 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5226 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5227 ab089a2a 2018-07-12 stsp
5228 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5229 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5230 07dd3ed3 2022-08-06 thomas
5231 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5232 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5233 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5234 ab089a2a 2018-07-12 stsp free(id_str);
5235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5236 ab089a2a 2018-07-12 stsp }
5237 ab089a2a 2018-07-12 stsp free(id_str);
5238 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5239 3f60a8ef 2018-07-10 stsp free(line);
5240 2550e4c3 2018-07-13 stsp line = NULL;
5241 3f60a8ef 2018-07-10 stsp if (err)
5242 3f60a8ef 2018-07-10 stsp return err;
5243 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5244 2550e4c3 2018-07-13 stsp free(wline);
5245 2550e4c3 2018-07-13 stsp wline = NULL;
5246 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5247 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5248 3f60a8ef 2018-07-10 stsp
5249 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5250 05171be4 2022-06-23 thomas view->maxx = 0;
5251 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5252 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5253 826082fe 2020-12-10 stsp if (linelen == -1) {
5254 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5255 826082fe 2020-12-10 stsp s->eof = 1;
5256 826082fe 2020-12-10 stsp break;
5257 826082fe 2020-12-10 stsp }
5258 84451b3e 2018-07-10 stsp free(line);
5259 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5260 84451b3e 2018-07-10 stsp }
5261 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5262 07dd3ed3 2022-08-06 thomas continue;
5263 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5264 826082fe 2020-12-10 stsp continue;
5265 cbea3800 2022-06-23 thomas
5266 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5267 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5268 cbea3800 2022-06-23 thomas if (err) {
5269 cbea3800 2022-06-23 thomas free(line);
5270 cbea3800 2022-06-23 thomas return err;
5271 cbea3800 2022-06-23 thomas }
5272 cbea3800 2022-06-23 thomas free(wline);
5273 cbea3800 2022-06-23 thomas wline = NULL;
5274 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5275 84451b3e 2018-07-10 stsp
5276 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5277 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5278 b700b5d6 2018-07-10 stsp
5279 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5280 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5281 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5282 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5283 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5284 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5285 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5286 8d0fe45a 2019-08-12 stsp char *id_str;
5287 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5288 91198554 2022-06-23 thomas blame_line->id);
5289 8d0fe45a 2019-08-12 stsp if (err) {
5290 8d0fe45a 2019-08-12 stsp free(line);
5291 8d0fe45a 2019-08-12 stsp return err;
5292 8d0fe45a 2019-08-12 stsp }
5293 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5294 11b20872 2019-11-08 stsp if (tc)
5295 11b20872 2019-11-08 stsp wattr_on(view->window,
5296 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5297 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5298 11b20872 2019-11-08 stsp if (tc)
5299 11b20872 2019-11-08 stsp wattr_off(view->window,
5300 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5301 8d0fe45a 2019-08-12 stsp free(id_str);
5302 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5303 8d0fe45a 2019-08-12 stsp } else {
5304 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5305 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5306 84451b3e 2018-07-10 stsp }
5307 ee41ec32 2018-07-10 stsp } else {
5308 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5309 ee41ec32 2018-07-10 stsp prev_id = NULL;
5310 ee41ec32 2018-07-10 stsp }
5311 84451b3e 2018-07-10 stsp
5312 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5313 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5314 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5315 27a741e5 2019-09-11 stsp
5316 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5317 41605754 2020-11-12 stsp width = 9;
5318 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5319 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5320 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5321 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5322 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5323 41605754 2020-11-12 stsp if (err) {
5324 41605754 2020-11-12 stsp free(line);
5325 41605754 2020-11-12 stsp return err;
5326 41605754 2020-11-12 stsp }
5327 41605754 2020-11-12 stsp width += 9;
5328 41605754 2020-11-12 stsp } else {
5329 923086fd 2022-06-23 thomas int skip;
5330 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5331 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5332 923086fd 2022-06-23 thomas if (err) {
5333 923086fd 2022-06-23 thomas free(line);
5334 923086fd 2022-06-23 thomas return err;
5335 05171be4 2022-06-23 thomas }
5336 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5337 02bce7e2 2022-06-23 thomas width += 9;
5338 41605754 2020-11-12 stsp free(wline);
5339 41605754 2020-11-12 stsp wline = NULL;
5340 41605754 2020-11-12 stsp }
5341 41605754 2020-11-12 stsp
5342 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5343 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5344 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5345 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5346 84451b3e 2018-07-10 stsp }
5347 826082fe 2020-12-10 stsp free(line);
5348 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5349 84451b3e 2018-07-10 stsp
5350 a5d43cac 2022-07-01 thomas view_border(view);
5351 84451b3e 2018-07-10 stsp
5352 84451b3e 2018-07-10 stsp return NULL;
5353 84451b3e 2018-07-10 stsp }
5354 84451b3e 2018-07-10 stsp
5355 84451b3e 2018-07-10 stsp static const struct got_error *
5356 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5357 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5358 84451b3e 2018-07-10 stsp {
5359 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5360 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5361 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5362 1a76625f 2018-10-22 stsp int errcode;
5363 84451b3e 2018-07-10 stsp
5364 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5365 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5366 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5367 84451b3e 2018-07-10 stsp
5368 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5369 1a76625f 2018-10-22 stsp if (errcode)
5370 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5371 84451b3e 2018-07-10 stsp
5372 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5373 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5374 d68a0a7d 2018-07-10 stsp goto done;
5375 d68a0a7d 2018-07-10 stsp }
5376 d68a0a7d 2018-07-10 stsp
5377 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5378 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5379 d68a0a7d 2018-07-10 stsp
5380 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5381 d68a0a7d 2018-07-10 stsp if (line->annotated)
5382 d68a0a7d 2018-07-10 stsp goto done;
5383 d68a0a7d 2018-07-10 stsp
5384 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5385 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5386 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5387 84451b3e 2018-07-10 stsp goto done;
5388 84451b3e 2018-07-10 stsp }
5389 84451b3e 2018-07-10 stsp line->annotated = 1;
5390 84451b3e 2018-07-10 stsp done:
5391 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5392 1a76625f 2018-10-22 stsp if (errcode)
5393 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5394 84451b3e 2018-07-10 stsp return err;
5395 84451b3e 2018-07-10 stsp }
5396 84451b3e 2018-07-10 stsp
5397 84451b3e 2018-07-10 stsp static void *
5398 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5399 84451b3e 2018-07-10 stsp {
5400 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5401 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5402 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5403 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5404 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5405 00a8878e 2022-07-01 thomas
5406 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5407 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5408 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5409 9117a7b7 2022-07-01 thomas
5410 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5411 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5412 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5413 9117a7b7 2022-07-01 thomas goto done;
5414 9117a7b7 2022-07-01 thomas }
5415 18430de3 2018-07-10 stsp
5416 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5417 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5418 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5419 9117a7b7 2022-07-01 thomas goto done;
5420 9117a7b7 2022-07-01 thomas }
5421 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5422 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5423 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5424 9117a7b7 2022-07-01 thomas goto done;
5425 9117a7b7 2022-07-01 thomas }
5426 9117a7b7 2022-07-01 thomas
5427 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5428 61266923 2020-01-14 stsp if (err)
5429 9117a7b7 2022-07-01 thomas goto done;
5430 61266923 2020-01-14 stsp
5431 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5432 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5433 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5434 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5435 fc06ba56 2019-08-22 stsp err = NULL;
5436 18430de3 2018-07-10 stsp
5437 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5438 9117a7b7 2022-07-01 thomas if (errcode) {
5439 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5440 9117a7b7 2022-07-01 thomas goto done;
5441 9117a7b7 2022-07-01 thomas }
5442 18430de3 2018-07-10 stsp
5443 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5444 1d0f4054 2021-06-17 stsp if (err == NULL)
5445 1d0f4054 2021-06-17 stsp err = close_err;
5446 c9beca56 2018-07-22 stsp ta->repo = NULL;
5447 c9beca56 2018-07-22 stsp *ta->complete = 1;
5448 18430de3 2018-07-10 stsp
5449 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5450 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5451 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5452 18430de3 2018-07-10 stsp
5453 9117a7b7 2022-07-01 thomas done:
5454 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5455 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5456 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5457 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5458 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5459 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5460 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5461 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5462 00a8878e 2022-07-01 thomas
5463 18430de3 2018-07-10 stsp return (void *)err;
5464 84451b3e 2018-07-10 stsp }
5465 84451b3e 2018-07-10 stsp
5466 245d91c1 2018-07-12 stsp static struct got_object_id *
5467 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5468 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5469 245d91c1 2018-07-12 stsp {
5470 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5471 8d0fe45a 2019-08-12 stsp
5472 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5473 8d0fe45a 2019-08-12 stsp return NULL;
5474 b880a918 2018-07-10 stsp
5475 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5476 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5477 4fc71f3b 2022-07-12 thomas return NULL;
5478 4fc71f3b 2022-07-12 thomas
5479 4fc71f3b 2022-07-12 thomas return line->id;
5480 4fc71f3b 2022-07-12 thomas }
5481 4fc71f3b 2022-07-12 thomas
5482 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5483 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5484 4fc71f3b 2022-07-12 thomas int lineno)
5485 4fc71f3b 2022-07-12 thomas {
5486 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5487 4fc71f3b 2022-07-12 thomas
5488 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5489 4fc71f3b 2022-07-12 thomas return NULL;
5490 4fc71f3b 2022-07-12 thomas
5491 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5492 245d91c1 2018-07-12 stsp if (!line->annotated)
5493 245d91c1 2018-07-12 stsp return NULL;
5494 245d91c1 2018-07-12 stsp
5495 245d91c1 2018-07-12 stsp return line->id;
5496 b880a918 2018-07-10 stsp }
5497 245d91c1 2018-07-12 stsp
5498 b880a918 2018-07-10 stsp static const struct got_error *
5499 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5500 a70480e0 2018-06-23 stsp {
5501 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5502 245d91c1 2018-07-12 stsp int i;
5503 245d91c1 2018-07-12 stsp
5504 245d91c1 2018-07-12 stsp if (blame->thread) {
5505 1a76625f 2018-10-22 stsp int errcode;
5506 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5507 1a76625f 2018-10-22 stsp if (errcode)
5508 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5509 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5510 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5511 1a76625f 2018-10-22 stsp if (errcode)
5512 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5513 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5514 1a76625f 2018-10-22 stsp if (errcode)
5515 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5516 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5517 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5518 245d91c1 2018-07-12 stsp err = NULL;
5519 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5520 245d91c1 2018-07-12 stsp }
5521 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5522 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5523 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5524 1d0f4054 2021-06-17 stsp if (err == NULL)
5525 1d0f4054 2021-06-17 stsp err = close_err;
5526 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5527 245d91c1 2018-07-12 stsp }
5528 245d91c1 2018-07-12 stsp if (blame->f) {
5529 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5530 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5531 245d91c1 2018-07-12 stsp blame->f = NULL;
5532 245d91c1 2018-07-12 stsp }
5533 57670559 2018-12-23 stsp if (blame->lines) {
5534 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5535 57670559 2018-12-23 stsp free(blame->lines[i].id);
5536 57670559 2018-12-23 stsp free(blame->lines);
5537 57670559 2018-12-23 stsp blame->lines = NULL;
5538 57670559 2018-12-23 stsp }
5539 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5540 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5541 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5542 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5543 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5544 7cd52833 2022-06-23 thomas if (err == NULL)
5545 7cd52833 2022-06-23 thomas err = pack_err;
5546 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5547 7cd52833 2022-06-23 thomas }
5548 245d91c1 2018-07-12 stsp return err;
5549 245d91c1 2018-07-12 stsp }
5550 245d91c1 2018-07-12 stsp
5551 245d91c1 2018-07-12 stsp static const struct got_error *
5552 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5553 fc06ba56 2019-08-22 stsp {
5554 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5555 fc06ba56 2019-08-22 stsp int *done = arg;
5556 fc06ba56 2019-08-22 stsp int errcode;
5557 fc06ba56 2019-08-22 stsp
5558 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5559 fc06ba56 2019-08-22 stsp if (errcode)
5560 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5561 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5562 fc06ba56 2019-08-22 stsp
5563 fc06ba56 2019-08-22 stsp if (*done)
5564 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5565 fc06ba56 2019-08-22 stsp
5566 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5567 fc06ba56 2019-08-22 stsp if (errcode)
5568 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5569 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5570 fc06ba56 2019-08-22 stsp
5571 fc06ba56 2019-08-22 stsp return err;
5572 fc06ba56 2019-08-22 stsp }
5573 fc06ba56 2019-08-22 stsp
5574 fc06ba56 2019-08-22 stsp static const struct got_error *
5575 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5576 245d91c1 2018-07-12 stsp {
5577 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5578 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5579 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5580 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5581 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5582 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5583 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5584 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5585 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5586 a70480e0 2018-06-23 stsp
5587 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5588 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5589 27d434c2 2018-09-15 stsp if (err)
5590 15a94983 2018-12-23 stsp return err;
5591 f4ae6ddb 2022-07-01 thomas
5592 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5593 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5594 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5595 f4ae6ddb 2022-07-01 thomas goto done;
5596 f4ae6ddb 2022-07-01 thomas }
5597 945f9229 2022-04-16 thomas
5598 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5599 945f9229 2022-04-16 thomas if (err)
5600 945f9229 2022-04-16 thomas goto done;
5601 27d434c2 2018-09-15 stsp
5602 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5603 84451b3e 2018-07-10 stsp if (err)
5604 84451b3e 2018-07-10 stsp goto done;
5605 27d434c2 2018-09-15 stsp
5606 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5607 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5608 84451b3e 2018-07-10 stsp goto done;
5609 84451b3e 2018-07-10 stsp }
5610 a70480e0 2018-06-23 stsp
5611 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5612 a70480e0 2018-06-23 stsp if (err)
5613 a70480e0 2018-06-23 stsp goto done;
5614 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5615 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5616 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5617 84451b3e 2018-07-10 stsp goto done;
5618 84451b3e 2018-07-10 stsp }
5619 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5620 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5621 1fddf795 2021-01-20 stsp if (err)
5622 1fddf795 2021-01-20 stsp goto done;
5623 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5624 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5625 84451b3e 2018-07-10 stsp goto done;
5626 1fddf795 2021-01-20 stsp }
5627 b02560ec 2019-08-19 stsp
5628 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5629 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5630 b02560ec 2019-08-19 stsp blame->nlines--;
5631 a70480e0 2018-06-23 stsp
5632 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5633 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5634 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5635 84451b3e 2018-07-10 stsp goto done;
5636 84451b3e 2018-07-10 stsp }
5637 a70480e0 2018-06-23 stsp
5638 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5639 bd24772e 2018-07-11 stsp if (err)
5640 bd24772e 2018-07-11 stsp goto done;
5641 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5642 7cd52833 2022-06-23 thomas pack_fds);
5643 7cd52833 2022-06-23 thomas if (err)
5644 7cd52833 2022-06-23 thomas goto done;
5645 bd24772e 2018-07-11 stsp
5646 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5647 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5648 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5649 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5650 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5651 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5652 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5653 245d91c1 2018-07-12 stsp goto done;
5654 245d91c1 2018-07-12 stsp }
5655 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5656 245d91c1 2018-07-12 stsp
5657 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5658 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5659 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5660 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5661 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5662 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5663 a5388363 2020-12-01 naddy s->blame_complete = 0;
5664 f5a09613 2020-12-13 naddy
5665 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5666 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5667 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5668 f5a09613 2020-12-13 naddy s->selected_line = 1;
5669 f5a09613 2020-12-13 naddy }
5670 aa61903a 2021-12-31 thomas s->matched_line = 0;
5671 245d91c1 2018-07-12 stsp
5672 245d91c1 2018-07-12 stsp done:
5673 945f9229 2022-04-16 thomas if (commit)
5674 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5675 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5676 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5677 245d91c1 2018-07-12 stsp if (blob)
5678 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5679 27d434c2 2018-09-15 stsp free(obj_id);
5680 245d91c1 2018-07-12 stsp if (err)
5681 245d91c1 2018-07-12 stsp stop_blame(blame);
5682 245d91c1 2018-07-12 stsp return err;
5683 245d91c1 2018-07-12 stsp }
5684 245d91c1 2018-07-12 stsp
5685 245d91c1 2018-07-12 stsp static const struct got_error *
5686 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5687 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5688 245d91c1 2018-07-12 stsp {
5689 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5690 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5691 dbc6a6b6 2018-07-12 stsp
5692 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5693 245d91c1 2018-07-12 stsp
5694 c4843652 2019-08-12 stsp s->path = strdup(path);
5695 c4843652 2019-08-12 stsp if (s->path == NULL)
5696 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5697 c4843652 2019-08-12 stsp
5698 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5699 c4843652 2019-08-12 stsp if (err) {
5700 c4843652 2019-08-12 stsp free(s->path);
5701 7cbe629d 2018-08-04 stsp return err;
5702 c4843652 2019-08-12 stsp }
5703 245d91c1 2018-07-12 stsp
5704 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5705 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5706 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5707 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5708 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5709 fb2756b9 2018-08-04 stsp s->repo = repo;
5710 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5711 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5712 7cbe629d 2018-08-04 stsp
5713 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5714 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5715 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5716 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5717 11b20872 2019-11-08 stsp if (err)
5718 11b20872 2019-11-08 stsp return err;
5719 11b20872 2019-11-08 stsp }
5720 11b20872 2019-11-08 stsp
5721 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5722 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5723 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5724 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5725 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5726 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5727 e5a0f69f 2018-08-18 stsp
5728 a5388363 2020-12-01 naddy return run_blame(view);
5729 7cbe629d 2018-08-04 stsp }
5730 7cbe629d 2018-08-04 stsp
5731 e5a0f69f 2018-08-18 stsp static const struct got_error *
5732 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5733 7cbe629d 2018-08-04 stsp {
5734 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5735 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5736 7cbe629d 2018-08-04 stsp
5737 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5738 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5739 e5a0f69f 2018-08-18 stsp
5740 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5741 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5742 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5743 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5744 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5745 7cbe629d 2018-08-04 stsp }
5746 e5a0f69f 2018-08-18 stsp
5747 e5a0f69f 2018-08-18 stsp free(s->path);
5748 11b20872 2019-11-08 stsp free_colors(&s->colors);
5749 e5a0f69f 2018-08-18 stsp return err;
5750 7cbe629d 2018-08-04 stsp }
5751 7cbe629d 2018-08-04 stsp
5752 7cbe629d 2018-08-04 stsp static const struct got_error *
5753 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5754 6c4c42e0 2019-06-24 stsp {
5755 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5756 6c4c42e0 2019-06-24 stsp
5757 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5758 6c4c42e0 2019-06-24 stsp return NULL;
5759 6c4c42e0 2019-06-24 stsp }
5760 6c4c42e0 2019-06-24 stsp
5761 6c4c42e0 2019-06-24 stsp static const struct got_error *
5762 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5763 6c4c42e0 2019-06-24 stsp {
5764 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5765 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5766 6c4c42e0 2019-06-24 stsp int lineno;
5767 78eecffc 2022-06-23 thomas char *line = NULL;
5768 826082fe 2020-12-10 stsp size_t linesize = 0;
5769 826082fe 2020-12-10 stsp ssize_t linelen;
5770 6c4c42e0 2019-06-24 stsp
5771 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5772 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5773 6c4c42e0 2019-06-24 stsp return NULL;
5774 6c4c42e0 2019-06-24 stsp }
5775 6c4c42e0 2019-06-24 stsp
5776 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5777 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5778 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5779 6c4c42e0 2019-06-24 stsp else
5780 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5781 4b3f9dac 2021-12-17 thomas } else
5782 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5783 6c4c42e0 2019-06-24 stsp
5784 6c4c42e0 2019-06-24 stsp while (1) {
5785 6c4c42e0 2019-06-24 stsp off_t offset;
5786 6c4c42e0 2019-06-24 stsp
5787 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5788 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5789 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5790 6c4c42e0 2019-06-24 stsp break;
5791 6c4c42e0 2019-06-24 stsp }
5792 2246482e 2019-06-25 stsp
5793 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5794 6c4c42e0 2019-06-24 stsp lineno = 1;
5795 6c4c42e0 2019-06-24 stsp else
5796 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5797 6c4c42e0 2019-06-24 stsp }
5798 6c4c42e0 2019-06-24 stsp
5799 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5800 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5801 6c4c42e0 2019-06-24 stsp free(line);
5802 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5803 6c4c42e0 2019-06-24 stsp }
5804 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5805 78eecffc 2022-06-23 thomas if (linelen != -1) {
5806 78eecffc 2022-06-23 thomas char *exstr;
5807 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5808 78eecffc 2022-06-23 thomas if (err)
5809 78eecffc 2022-06-23 thomas break;
5810 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5811 78eecffc 2022-06-23 thomas &view->regmatch)) {
5812 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5813 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5814 78eecffc 2022-06-23 thomas free(exstr);
5815 78eecffc 2022-06-23 thomas break;
5816 78eecffc 2022-06-23 thomas }
5817 78eecffc 2022-06-23 thomas free(exstr);
5818 6c4c42e0 2019-06-24 stsp }
5819 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5820 6c4c42e0 2019-06-24 stsp lineno++;
5821 6c4c42e0 2019-06-24 stsp else
5822 6c4c42e0 2019-06-24 stsp lineno--;
5823 6c4c42e0 2019-06-24 stsp }
5824 826082fe 2020-12-10 stsp free(line);
5825 6c4c42e0 2019-06-24 stsp
5826 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5827 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5828 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5829 6c4c42e0 2019-06-24 stsp }
5830 6c4c42e0 2019-06-24 stsp
5831 05171be4 2022-06-23 thomas return err;
5832 6c4c42e0 2019-06-24 stsp }
5833 6c4c42e0 2019-06-24 stsp
5834 6c4c42e0 2019-06-24 stsp static const struct got_error *
5835 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5836 7cbe629d 2018-08-04 stsp {
5837 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5838 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5839 2b380cc8 2018-10-24 stsp int errcode;
5840 2b380cc8 2018-10-24 stsp
5841 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5842 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5843 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5844 2b380cc8 2018-10-24 stsp if (errcode)
5845 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5846 51fe7530 2019-08-19 stsp
5847 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5848 2b380cc8 2018-10-24 stsp }
5849 e5a0f69f 2018-08-18 stsp
5850 51fe7530 2019-08-19 stsp if (s->blame_complete)
5851 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5852 51fe7530 2019-08-19 stsp
5853 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5854 e5a0f69f 2018-08-18 stsp
5855 a5d43cac 2022-07-01 thomas view_border(view);
5856 e5a0f69f 2018-08-18 stsp return err;
5857 e5a0f69f 2018-08-18 stsp }
5858 e5a0f69f 2018-08-18 stsp
5859 e5a0f69f 2018-08-18 stsp static const struct got_error *
5860 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5861 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5862 eaeaa612 2022-07-20 thomas {
5863 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5864 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5865 eaeaa612 2022-07-20 thomas
5866 eaeaa612 2022-07-20 thomas *new_view = NULL;
5867 eaeaa612 2022-07-20 thomas
5868 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5869 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5870 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5871 eaeaa612 2022-07-20 thomas
5872 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5873 eaeaa612 2022-07-20 thomas if (err)
5874 eaeaa612 2022-07-20 thomas view_close(log_view);
5875 eaeaa612 2022-07-20 thomas else
5876 eaeaa612 2022-07-20 thomas *new_view = log_view;
5877 eaeaa612 2022-07-20 thomas
5878 eaeaa612 2022-07-20 thomas return err;
5879 eaeaa612 2022-07-20 thomas }
5880 eaeaa612 2022-07-20 thomas
5881 eaeaa612 2022-07-20 thomas static const struct got_error *
5882 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5883 e5a0f69f 2018-08-18 stsp {
5884 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5885 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
5886 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5887 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5888 a5d43cac 2022-07-01 thomas
5889 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5890 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5891 a5d43cac 2022-07-01 thomas --eos; /* border */
5892 7cbe629d 2018-08-04 stsp
5893 e5a0f69f 2018-08-18 stsp switch (ch) {
5894 05171be4 2022-06-23 thomas case '0':
5895 05171be4 2022-06-23 thomas view->x = 0;
5896 05171be4 2022-06-23 thomas break;
5897 05171be4 2022-06-23 thomas case '$':
5898 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5899 07b0611c 2022-06-23 thomas view->count = 0;
5900 05171be4 2022-06-23 thomas break;
5901 05171be4 2022-06-23 thomas case KEY_RIGHT:
5902 05171be4 2022-06-23 thomas case 'l':
5903 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5904 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5905 07b0611c 2022-06-23 thomas else
5906 07b0611c 2022-06-23 thomas view->count = 0;
5907 05171be4 2022-06-23 thomas break;
5908 05171be4 2022-06-23 thomas case KEY_LEFT:
5909 05171be4 2022-06-23 thomas case 'h':
5910 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5911 07b0611c 2022-06-23 thomas if (view->x <= 0)
5912 07b0611c 2022-06-23 thomas view->count = 0;
5913 05171be4 2022-06-23 thomas break;
5914 1e37a5c2 2019-05-12 jcs case 'q':
5915 1e37a5c2 2019-05-12 jcs s->done = 1;
5916 4deef56f 2021-09-02 naddy break;
5917 4deef56f 2021-09-02 naddy case 'g':
5918 4deef56f 2021-09-02 naddy case KEY_HOME:
5919 4deef56f 2021-09-02 naddy s->selected_line = 1;
5920 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5921 07b0611c 2022-06-23 thomas view->count = 0;
5922 4deef56f 2021-09-02 naddy break;
5923 4deef56f 2021-09-02 naddy case 'G':
5924 4deef56f 2021-09-02 naddy case KEY_END:
5925 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5926 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5927 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5928 4deef56f 2021-09-02 naddy } else {
5929 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5930 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5931 4deef56f 2021-09-02 naddy }
5932 07b0611c 2022-06-23 thomas view->count = 0;
5933 1e37a5c2 2019-05-12 jcs break;
5934 1e37a5c2 2019-05-12 jcs case 'k':
5935 1e37a5c2 2019-05-12 jcs case KEY_UP:
5936 f7140bf5 2021-10-17 thomas case CTRL('p'):
5937 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5938 1e37a5c2 2019-05-12 jcs s->selected_line--;
5939 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5940 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5941 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5942 07b0611c 2022-06-23 thomas else
5943 07b0611c 2022-06-23 thomas view->count = 0;
5944 1e37a5c2 2019-05-12 jcs break;
5945 70f17a53 2022-06-13 thomas case CTRL('u'):
5946 23427b14 2022-06-23 thomas case 'u':
5947 70f17a53 2022-06-13 thomas nscroll /= 2;
5948 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5949 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5950 ea025d1d 2020-02-22 naddy case CTRL('b'):
5951 1c5e5faa 2022-06-23 thomas case 'b':
5952 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5953 07b0611c 2022-06-23 thomas if (view->count > 1)
5954 07b0611c 2022-06-23 thomas nscroll += nscroll;
5955 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5956 07b0611c 2022-06-23 thomas view->count = 0;
5957 e5a0f69f 2018-08-18 stsp break;
5958 1e37a5c2 2019-05-12 jcs }
5959 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5960 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5961 1e37a5c2 2019-05-12 jcs else
5962 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5963 1e37a5c2 2019-05-12 jcs break;
5964 1e37a5c2 2019-05-12 jcs case 'j':
5965 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5966 f7140bf5 2021-10-17 thomas case CTRL('n'):
5967 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5968 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5969 1e37a5c2 2019-05-12 jcs s->selected_line++;
5970 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5971 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5972 07b0611c 2022-06-23 thomas else
5973 07b0611c 2022-06-23 thomas view->count = 0;
5974 1e37a5c2 2019-05-12 jcs break;
5975 1c5e5faa 2022-06-23 thomas case 'c':
5976 1e37a5c2 2019-05-12 jcs case 'p': {
5977 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5978 07b0611c 2022-06-23 thomas
5979 07b0611c 2022-06-23 thomas view->count = 0;
5980 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5981 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5982 1e37a5c2 2019-05-12 jcs if (id == NULL)
5983 e5a0f69f 2018-08-18 stsp break;
5984 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5985 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5986 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5987 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5988 1e37a5c2 2019-05-12 jcs int obj_type;
5989 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5990 1e37a5c2 2019-05-12 jcs s->repo, id);
5991 e5a0f69f 2018-08-18 stsp if (err)
5992 e5a0f69f 2018-08-18 stsp break;
5993 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5994 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5995 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5996 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5997 e5a0f69f 2018-08-18 stsp break;
5998 e5a0f69f 2018-08-18 stsp }
5999 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6000 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6001 ec242592 2022-04-22 thomas s->repo, &pid->id);
6002 945f9229 2022-04-16 thomas if (err)
6003 945f9229 2022-04-16 thomas break;
6004 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6005 945f9229 2022-04-16 thomas pcommit, s->path);
6006 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6007 e5a0f69f 2018-08-18 stsp if (err) {
6008 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6009 1e37a5c2 2019-05-12 jcs err = NULL;
6010 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6011 e5a0f69f 2018-08-18 stsp break;
6012 e5a0f69f 2018-08-18 stsp }
6013 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6014 1e37a5c2 2019-05-12 jcs blob_id);
6015 1e37a5c2 2019-05-12 jcs free(blob_id);
6016 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6017 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6018 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6019 e5a0f69f 2018-08-18 stsp break;
6020 1e37a5c2 2019-05-12 jcs }
6021 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6022 ec242592 2022-04-22 thomas &pid->id);
6023 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6024 1e37a5c2 2019-05-12 jcs } else {
6025 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6026 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6027 1e37a5c2 2019-05-12 jcs break;
6028 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6029 1e37a5c2 2019-05-12 jcs id);
6030 1e37a5c2 2019-05-12 jcs }
6031 1e37a5c2 2019-05-12 jcs if (err)
6032 e5a0f69f 2018-08-18 stsp break;
6033 1e37a5c2 2019-05-12 jcs s->done = 1;
6034 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6035 1e37a5c2 2019-05-12 jcs s->done = 0;
6036 1e37a5c2 2019-05-12 jcs if (thread_err)
6037 1e37a5c2 2019-05-12 jcs break;
6038 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6039 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6040 a5388363 2020-12-01 naddy err = run_blame(view);
6041 1e37a5c2 2019-05-12 jcs if (err)
6042 1e37a5c2 2019-05-12 jcs break;
6043 1e37a5c2 2019-05-12 jcs break;
6044 1e37a5c2 2019-05-12 jcs }
6045 1c5e5faa 2022-06-23 thomas case 'C': {
6046 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6047 07b0611c 2022-06-23 thomas
6048 07b0611c 2022-06-23 thomas view->count = 0;
6049 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6050 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6051 1e37a5c2 2019-05-12 jcs break;
6052 1e37a5c2 2019-05-12 jcs s->done = 1;
6053 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6054 1e37a5c2 2019-05-12 jcs s->done = 0;
6055 1e37a5c2 2019-05-12 jcs if (thread_err)
6056 1e37a5c2 2019-05-12 jcs break;
6057 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6058 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6059 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6060 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6061 a5388363 2020-12-01 naddy err = run_blame(view);
6062 1e37a5c2 2019-05-12 jcs if (err)
6063 1e37a5c2 2019-05-12 jcs break;
6064 1e37a5c2 2019-05-12 jcs break;
6065 1e37a5c2 2019-05-12 jcs }
6066 2a31b33b 2022-07-23 thomas case 'L':
6067 eaeaa612 2022-07-20 thomas view->count = 0;
6068 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6069 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6070 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6071 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6072 eaeaa612 2022-07-20 thomas break;
6073 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6074 1e37a5c2 2019-05-12 jcs case '\r': {
6075 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6076 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6077 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6078 07b0611c 2022-06-23 thomas
6079 07b0611c 2022-06-23 thomas view->count = 0;
6080 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6081 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6082 1e37a5c2 2019-05-12 jcs if (id == NULL)
6083 1e37a5c2 2019-05-12 jcs break;
6084 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6085 1e37a5c2 2019-05-12 jcs if (err)
6086 1e37a5c2 2019-05-12 jcs break;
6087 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6088 4fc71f3b 2022-07-12 thomas if (*new_view) {
6089 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6090 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6091 4fc71f3b 2022-07-12 thomas if (err)
6092 4fc71f3b 2022-07-12 thomas break;
6093 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6094 4fc71f3b 2022-07-12 thomas } else {
6095 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6096 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6097 a5d43cac 2022-07-01 thomas
6098 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6099 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6100 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6101 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6102 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6103 4fc71f3b 2022-07-12 thomas break;
6104 4fc71f3b 2022-07-12 thomas }
6105 15a94983 2018-12-23 stsp }
6106 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6107 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6108 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6109 1e37a5c2 2019-05-12 jcs if (err) {
6110 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6111 1e37a5c2 2019-05-12 jcs break;
6112 1e37a5c2 2019-05-12 jcs }
6113 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6114 4fc71f3b 2022-07-12 thomas s->selected_line;
6115 4fc71f3b 2022-07-12 thomas if (*new_view)
6116 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6117 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6118 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6119 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6120 a5d43cac 2022-07-01 thomas if (err)
6121 a5d43cac 2022-07-01 thomas break;
6122 a5d43cac 2022-07-01 thomas }
6123 a5d43cac 2022-07-01 thomas
6124 e78dc838 2020-12-04 stsp view->focussed = 0;
6125 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6126 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6127 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6128 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6129 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6130 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6131 1e37a5c2 2019-05-12 jcs if (err)
6132 34bc9ec9 2019-02-22 stsp break;
6133 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6134 40236d76 2022-06-23 thomas if (err)
6135 40236d76 2022-06-23 thomas break;
6136 e78dc838 2020-12-04 stsp view->focus_child = 1;
6137 1e37a5c2 2019-05-12 jcs } else
6138 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6139 1e37a5c2 2019-05-12 jcs if (err)
6140 e5a0f69f 2018-08-18 stsp break;
6141 1e37a5c2 2019-05-12 jcs break;
6142 1e37a5c2 2019-05-12 jcs }
6143 70f17a53 2022-06-13 thomas case CTRL('d'):
6144 23427b14 2022-06-23 thomas case 'd':
6145 70f17a53 2022-06-13 thomas nscroll /= 2;
6146 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6147 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6148 ea025d1d 2020-02-22 naddy case CTRL('f'):
6149 1c5e5faa 2022-06-23 thomas case 'f':
6150 1e37a5c2 2019-05-12 jcs case ' ':
6151 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6152 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6153 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6154 07b0611c 2022-06-23 thomas view->count = 0;
6155 e5a0f69f 2018-08-18 stsp break;
6156 1e37a5c2 2019-05-12 jcs }
6157 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6158 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6159 70f17a53 2022-06-13 thomas s->selected_line +=
6160 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6161 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6162 1e37a5c2 2019-05-12 jcs }
6163 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6164 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6165 1e37a5c2 2019-05-12 jcs else
6166 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6167 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6168 1e37a5c2 2019-05-12 jcs break;
6169 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6170 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6171 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6172 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6173 1e37a5c2 2019-05-12 jcs }
6174 1e37a5c2 2019-05-12 jcs break;
6175 1e37a5c2 2019-05-12 jcs default:
6176 07b0611c 2022-06-23 thomas view->count = 0;
6177 1e37a5c2 2019-05-12 jcs break;
6178 a70480e0 2018-06-23 stsp }
6179 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6180 adf4c9e0 2022-07-03 thomas }
6181 adf4c9e0 2022-07-03 thomas
6182 adf4c9e0 2022-07-03 thomas static const struct got_error *
6183 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6184 adf4c9e0 2022-07-03 thomas {
6185 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6186 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6187 adf4c9e0 2022-07-03 thomas
6188 adf4c9e0 2022-07-03 thomas view->count = 0;
6189 adf4c9e0 2022-07-03 thomas s->done = 1;
6190 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6191 adf4c9e0 2022-07-03 thomas s->done = 0;
6192 adf4c9e0 2022-07-03 thomas if (err)
6193 adf4c9e0 2022-07-03 thomas return err;
6194 adf4c9e0 2022-07-03 thomas return run_blame(view);
6195 a70480e0 2018-06-23 stsp }
6196 a70480e0 2018-06-23 stsp
6197 a70480e0 2018-06-23 stsp static const struct got_error *
6198 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6199 9f7d7167 2018-04-29 stsp {
6200 a70480e0 2018-06-23 stsp const struct got_error *error;
6201 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6202 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6203 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6204 0587e10c 2020-07-23 stsp char *link_target = NULL;
6205 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6206 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6207 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6208 a70480e0 2018-06-23 stsp int ch;
6209 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6210 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6211 a70480e0 2018-06-23 stsp
6212 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6213 a70480e0 2018-06-23 stsp switch (ch) {
6214 a70480e0 2018-06-23 stsp case 'c':
6215 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6216 a70480e0 2018-06-23 stsp break;
6217 69069811 2018-08-02 stsp case 'r':
6218 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6219 69069811 2018-08-02 stsp if (repo_path == NULL)
6220 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6221 9ba1d308 2019-10-21 stsp optarg);
6222 69069811 2018-08-02 stsp break;
6223 a70480e0 2018-06-23 stsp default:
6224 17020d27 2019-03-07 stsp usage_blame();
6225 a70480e0 2018-06-23 stsp /* NOTREACHED */
6226 a70480e0 2018-06-23 stsp }
6227 a70480e0 2018-06-23 stsp }
6228 a70480e0 2018-06-23 stsp
6229 a70480e0 2018-06-23 stsp argc -= optind;
6230 a70480e0 2018-06-23 stsp argv += optind;
6231 a70480e0 2018-06-23 stsp
6232 f135c941 2020-02-20 stsp if (argc != 1)
6233 a70480e0 2018-06-23 stsp usage_blame();
6234 6962eb72 2020-02-20 stsp
6235 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6236 7cd52833 2022-06-23 thomas if (error != NULL)
6237 7cd52833 2022-06-23 thomas goto done;
6238 7cd52833 2022-06-23 thomas
6239 69069811 2018-08-02 stsp if (repo_path == NULL) {
6240 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6241 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6242 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6243 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6244 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6245 c156c7a4 2020-12-18 stsp goto done;
6246 f135c941 2020-02-20 stsp if (worktree)
6247 eb41ed75 2019-02-05 stsp repo_path =
6248 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6249 f135c941 2020-02-20 stsp else
6250 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6251 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6252 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6253 c156c7a4 2020-12-18 stsp goto done;
6254 c156c7a4 2020-12-18 stsp }
6255 f135c941 2020-02-20 stsp }
6256 a915003a 2019-02-05 stsp
6257 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6258 c02c541e 2019-03-29 stsp if (error != NULL)
6259 8e94dd5b 2019-01-04 stsp goto done;
6260 69069811 2018-08-02 stsp
6261 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6262 f135c941 2020-02-20 stsp worktree);
6263 c02c541e 2019-03-29 stsp if (error)
6264 92205607 2019-01-04 stsp goto done;
6265 69069811 2018-08-02 stsp
6266 f135c941 2020-02-20 stsp init_curses();
6267 f135c941 2020-02-20 stsp
6268 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6269 51a10b52 2020-12-26 stsp if (error)
6270 51a10b52 2020-12-26 stsp goto done;
6271 51a10b52 2020-12-26 stsp
6272 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6273 eb41ed75 2019-02-05 stsp if (error)
6274 69069811 2018-08-02 stsp goto done;
6275 a70480e0 2018-06-23 stsp
6276 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6277 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6278 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6279 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6280 a70480e0 2018-06-23 stsp if (error != NULL)
6281 66b4983c 2018-06-23 stsp goto done;
6282 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6283 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6284 a70480e0 2018-06-23 stsp } else {
6285 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6286 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6287 a70480e0 2018-06-23 stsp }
6288 a19e88aa 2018-06-23 stsp if (error != NULL)
6289 8b473291 2019-02-21 stsp goto done;
6290 8b473291 2019-02-21 stsp
6291 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6292 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6293 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6294 e1cd8fed 2018-08-01 stsp goto done;
6295 e1cd8fed 2018-08-01 stsp }
6296 0587e10c 2020-07-23 stsp
6297 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6298 945f9229 2022-04-16 thomas if (error)
6299 945f9229 2022-04-16 thomas goto done;
6300 945f9229 2022-04-16 thomas
6301 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6302 945f9229 2022-04-16 thomas commit, repo);
6303 7cbe629d 2018-08-04 stsp if (error)
6304 7cbe629d 2018-08-04 stsp goto done;
6305 0587e10c 2020-07-23 stsp
6306 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6307 78756c87 2020-11-24 stsp commit_id, repo);
6308 0587e10c 2020-07-23 stsp if (error)
6309 0587e10c 2020-07-23 stsp goto done;
6310 12314ad4 2019-08-31 stsp if (worktree) {
6311 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6312 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6313 12314ad4 2019-08-31 stsp worktree = NULL;
6314 12314ad4 2019-08-31 stsp }
6315 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6316 a70480e0 2018-06-23 stsp done:
6317 69069811 2018-08-02 stsp free(repo_path);
6318 f135c941 2020-02-20 stsp free(in_repo_path);
6319 0587e10c 2020-07-23 stsp free(link_target);
6320 69069811 2018-08-02 stsp free(cwd);
6321 a70480e0 2018-06-23 stsp free(commit_id);
6322 945f9229 2022-04-16 thomas if (commit)
6323 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6324 eb41ed75 2019-02-05 stsp if (worktree)
6325 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6326 1d0f4054 2021-06-17 stsp if (repo) {
6327 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6328 1d0f4054 2021-06-17 stsp if (error == NULL)
6329 1d0f4054 2021-06-17 stsp error = close_err;
6330 1d0f4054 2021-06-17 stsp }
6331 7cd52833 2022-06-23 thomas if (pack_fds) {
6332 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6333 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6334 7cd52833 2022-06-23 thomas if (error == NULL)
6335 7cd52833 2022-06-23 thomas error = pack_err;
6336 7cd52833 2022-06-23 thomas }
6337 51a10b52 2020-12-26 stsp tog_free_refs();
6338 a70480e0 2018-06-23 stsp return error;
6339 ffd1d5e5 2018-06-23 stsp }
6340 ffd1d5e5 2018-06-23 stsp
6341 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6342 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6343 ffd1d5e5 2018-06-23 stsp {
6344 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6345 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6346 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6347 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6348 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6349 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6350 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6351 ffd1d5e5 2018-06-23 stsp
6352 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6353 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6354 a5d43cac 2022-07-01 thomas --limit; /* border */
6355 ffd1d5e5 2018-06-23 stsp
6356 f7d12f7e 2018-08-01 stsp werase(view->window);
6357 ffd1d5e5 2018-06-23 stsp
6358 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6359 ffd1d5e5 2018-06-23 stsp return NULL;
6360 ffd1d5e5 2018-06-23 stsp
6361 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6362 f91a2b48 2022-06-23 thomas 0, 0);
6363 ffd1d5e5 2018-06-23 stsp if (err)
6364 ffd1d5e5 2018-06-23 stsp return err;
6365 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6366 a3404814 2018-09-02 stsp wstandout(view->window);
6367 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6368 11b20872 2019-11-08 stsp if (tc)
6369 11b20872 2019-11-08 stsp wattr_on(view->window,
6370 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6371 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6372 11b20872 2019-11-08 stsp if (tc)
6373 11b20872 2019-11-08 stsp wattr_off(view->window,
6374 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6375 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6376 a3404814 2018-09-02 stsp wstandend(view->window);
6377 2550e4c3 2018-07-13 stsp free(wline);
6378 2550e4c3 2018-07-13 stsp wline = NULL;
6379 07dd3ed3 2022-08-06 thomas
6380 6f5f393a 2022-08-12 thomas i += s->selected;
6381 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6382 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6383 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6384 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6385 07dd3ed3 2022-08-06 thomas }
6386 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6387 07dd3ed3 2022-08-06 thomas wprintw(view->window, " [%d/%d]", i,
6388 07dd3ed3 2022-08-06 thomas nentries + (s->tree == s->root ? 0 : 1)); /* ".." in !root tree */
6389 07dd3ed3 2022-08-06 thomas
6390 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6391 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6392 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6393 ffd1d5e5 2018-06-23 stsp return NULL;
6394 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6395 f91a2b48 2022-06-23 thomas 0, 0);
6396 ce52c690 2018-06-23 stsp if (err)
6397 ce52c690 2018-06-23 stsp return err;
6398 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6399 2550e4c3 2018-07-13 stsp free(wline);
6400 2550e4c3 2018-07-13 stsp wline = NULL;
6401 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6402 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6403 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6404 ffd1d5e5 2018-06-23 stsp return NULL;
6405 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6406 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6407 a1eca9bb 2018-06-23 stsp return NULL;
6408 ffd1d5e5 2018-06-23 stsp
6409 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6410 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6411 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6412 0cf4efb1 2018-09-29 stsp if (view->focussed)
6413 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6414 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6415 ffd1d5e5 2018-06-23 stsp }
6416 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6417 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6418 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6419 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6420 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6421 ffd1d5e5 2018-06-23 stsp return NULL;
6422 ffd1d5e5 2018-06-23 stsp n = 1;
6423 ffd1d5e5 2018-06-23 stsp } else {
6424 ffd1d5e5 2018-06-23 stsp n = 0;
6425 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6426 ffd1d5e5 2018-06-23 stsp }
6427 ffd1d5e5 2018-06-23 stsp
6428 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6429 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6430 848d6979 2019-08-12 stsp const char *modestr = "";
6431 56e0773d 2019-11-28 stsp mode_t mode;
6432 1d13200f 2018-07-12 stsp
6433 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6434 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6435 56e0773d 2019-11-28 stsp
6436 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6437 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6438 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6439 1d13200f 2018-07-12 stsp if (err)
6440 638f9024 2019-05-13 stsp return got_error_from_errno(
6441 230a42bd 2019-05-11 jcs "got_object_id_str");
6442 1d13200f 2018-07-12 stsp }
6443 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6444 63c5ca5d 2019-08-24 stsp modestr = "$";
6445 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6446 0d6c6ee3 2020-05-20 stsp int i;
6447 0d6c6ee3 2020-05-20 stsp
6448 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6449 d86d3b18 2020-12-01 naddy te, s->repo);
6450 0d6c6ee3 2020-05-20 stsp if (err) {
6451 0d6c6ee3 2020-05-20 stsp free(id_str);
6452 0d6c6ee3 2020-05-20 stsp return err;
6453 0d6c6ee3 2020-05-20 stsp }
6454 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6455 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6456 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6457 0d6c6ee3 2020-05-20 stsp }
6458 848d6979 2019-08-12 stsp modestr = "@";
6459 0d6c6ee3 2020-05-20 stsp }
6460 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6461 848d6979 2019-08-12 stsp modestr = "/";
6462 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6463 848d6979 2019-08-12 stsp modestr = "*";
6464 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6465 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6466 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6467 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6468 1d13200f 2018-07-12 stsp free(id_str);
6469 0d6c6ee3 2020-05-20 stsp free(link_target);
6470 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6471 1d13200f 2018-07-12 stsp }
6472 1d13200f 2018-07-12 stsp free(id_str);
6473 0d6c6ee3 2020-05-20 stsp free(link_target);
6474 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6475 f91a2b48 2022-06-23 thomas 0, 0);
6476 ffd1d5e5 2018-06-23 stsp if (err) {
6477 ffd1d5e5 2018-06-23 stsp free(line);
6478 ffd1d5e5 2018-06-23 stsp break;
6479 ffd1d5e5 2018-06-23 stsp }
6480 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6481 0cf4efb1 2018-09-29 stsp if (view->focussed)
6482 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6483 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6484 ffd1d5e5 2018-06-23 stsp }
6485 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6486 f26dddb7 2019-11-08 stsp if (tc)
6487 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6488 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6489 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6490 f26dddb7 2019-11-08 stsp if (tc)
6491 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6492 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6493 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6494 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6495 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6496 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6497 ffd1d5e5 2018-06-23 stsp free(line);
6498 2550e4c3 2018-07-13 stsp free(wline);
6499 2550e4c3 2018-07-13 stsp wline = NULL;
6500 ffd1d5e5 2018-06-23 stsp n++;
6501 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6502 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6503 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6504 ffd1d5e5 2018-06-23 stsp break;
6505 ffd1d5e5 2018-06-23 stsp }
6506 ffd1d5e5 2018-06-23 stsp
6507 ffd1d5e5 2018-06-23 stsp return err;
6508 ffd1d5e5 2018-06-23 stsp }
6509 ffd1d5e5 2018-06-23 stsp
6510 ffd1d5e5 2018-06-23 stsp static void
6511 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6512 ffd1d5e5 2018-06-23 stsp {
6513 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6514 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6515 fa86c4bf 2020-11-29 stsp int i = 0;
6516 ffd1d5e5 2018-06-23 stsp
6517 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6518 ffd1d5e5 2018-06-23 stsp return;
6519 ffd1d5e5 2018-06-23 stsp
6520 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6521 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6522 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6523 fa86c4bf 2020-11-29 stsp if (!isroot)
6524 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6525 fa86c4bf 2020-11-29 stsp break;
6526 fa86c4bf 2020-11-29 stsp }
6527 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6528 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6529 ffd1d5e5 2018-06-23 stsp }
6530 ffd1d5e5 2018-06-23 stsp }
6531 ffd1d5e5 2018-06-23 stsp
6532 a5d43cac 2022-07-01 thomas static const struct got_error *
6533 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6534 ffd1d5e5 2018-06-23 stsp {
6535 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6536 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6537 ffd1d5e5 2018-06-23 stsp int n = 0;
6538 ffd1d5e5 2018-06-23 stsp
6539 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6540 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6541 694d3271 2020-12-01 naddy s->first_displayed_entry);
6542 694d3271 2020-12-01 naddy else
6543 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6544 56e0773d 2019-11-28 stsp
6545 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6546 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6547 07dd3ed3 2022-08-06 thomas if (last) {
6548 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6549 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6550 07dd3ed3 2022-08-06 thomas }
6551 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6552 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6553 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6554 768394f3 2019-01-24 stsp }
6555 ffd1d5e5 2018-06-23 stsp }
6556 a5d43cac 2022-07-01 thomas
6557 a5d43cac 2022-07-01 thomas return NULL;
6558 ffd1d5e5 2018-06-23 stsp }
6559 ffd1d5e5 2018-06-23 stsp
6560 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6561 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6562 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6563 ffd1d5e5 2018-06-23 stsp {
6564 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6565 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6566 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6567 ffd1d5e5 2018-06-23 stsp
6568 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6569 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6570 56e0773d 2019-11-28 stsp + 1 /* slash */;
6571 ce52c690 2018-06-23 stsp if (te)
6572 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6573 ce52c690 2018-06-23 stsp
6574 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6575 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6576 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6577 ffd1d5e5 2018-06-23 stsp
6578 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6579 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6580 d9765a41 2018-06-23 stsp while (pt) {
6581 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6582 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6583 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6584 cb2ebc8a 2018-06-23 stsp goto done;
6585 cb2ebc8a 2018-06-23 stsp }
6586 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6587 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6588 cb2ebc8a 2018-06-23 stsp goto done;
6589 cb2ebc8a 2018-06-23 stsp }
6590 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6591 ffd1d5e5 2018-06-23 stsp }
6592 ce52c690 2018-06-23 stsp if (te) {
6593 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6594 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6595 ce52c690 2018-06-23 stsp goto done;
6596 ce52c690 2018-06-23 stsp }
6597 cb2ebc8a 2018-06-23 stsp }
6598 ce52c690 2018-06-23 stsp done:
6599 ce52c690 2018-06-23 stsp if (err) {
6600 ce52c690 2018-06-23 stsp free(*path);
6601 ce52c690 2018-06-23 stsp *path = NULL;
6602 ce52c690 2018-06-23 stsp }
6603 ce52c690 2018-06-23 stsp return err;
6604 ce52c690 2018-06-23 stsp }
6605 ce52c690 2018-06-23 stsp
6606 ce52c690 2018-06-23 stsp static const struct got_error *
6607 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6608 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6609 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6610 ce52c690 2018-06-23 stsp {
6611 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6612 ce52c690 2018-06-23 stsp char *path;
6613 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6614 a0de39f3 2019-08-09 stsp
6615 a0de39f3 2019-08-09 stsp *new_view = NULL;
6616 69efd4c4 2018-07-18 stsp
6617 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6618 ce52c690 2018-06-23 stsp if (err)
6619 ce52c690 2018-06-23 stsp return err;
6620 ffd1d5e5 2018-06-23 stsp
6621 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6622 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6623 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6624 83ce39e3 2019-08-12 stsp goto done;
6625 83ce39e3 2019-08-12 stsp }
6626 cdf1ee82 2018-08-01 stsp
6627 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6628 e5a0f69f 2018-08-18 stsp if (err) {
6629 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6630 fc06ba56 2019-08-22 stsp err = NULL;
6631 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6632 e5a0f69f 2018-08-18 stsp } else
6633 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6634 83ce39e3 2019-08-12 stsp done:
6635 83ce39e3 2019-08-12 stsp free(path);
6636 69efd4c4 2018-07-18 stsp return err;
6637 69efd4c4 2018-07-18 stsp }
6638 69efd4c4 2018-07-18 stsp
6639 69efd4c4 2018-07-18 stsp static const struct got_error *
6640 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6641 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6642 69efd4c4 2018-07-18 stsp {
6643 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6644 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6645 69efd4c4 2018-07-18 stsp char *path;
6646 a0de39f3 2019-08-09 stsp
6647 a0de39f3 2019-08-09 stsp *new_view = NULL;
6648 69efd4c4 2018-07-18 stsp
6649 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6650 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6651 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6652 e5a0f69f 2018-08-18 stsp
6653 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6654 69efd4c4 2018-07-18 stsp if (err)
6655 69efd4c4 2018-07-18 stsp return err;
6656 69efd4c4 2018-07-18 stsp
6657 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6658 4e97c21c 2020-12-06 stsp path, 0);
6659 ba4f502b 2018-08-04 stsp if (err)
6660 e5a0f69f 2018-08-18 stsp view_close(log_view);
6661 e5a0f69f 2018-08-18 stsp else
6662 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6663 cb2ebc8a 2018-06-23 stsp free(path);
6664 cb2ebc8a 2018-06-23 stsp return err;
6665 ffd1d5e5 2018-06-23 stsp }
6666 ffd1d5e5 2018-06-23 stsp
6667 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6668 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6669 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6670 ffd1d5e5 2018-06-23 stsp {
6671 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6672 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6673 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6674 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6675 ffd1d5e5 2018-06-23 stsp
6676 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6677 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6678 bc573f3b 2021-07-10 stsp
6679 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6680 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6681 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6682 ffd1d5e5 2018-06-23 stsp
6683 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6684 bc573f3b 2021-07-10 stsp if (err)
6685 bc573f3b 2021-07-10 stsp goto done;
6686 bc573f3b 2021-07-10 stsp
6687 bc573f3b 2021-07-10 stsp /*
6688 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6689 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6690 bc573f3b 2021-07-10 stsp * closed on demand.
6691 bc573f3b 2021-07-10 stsp */
6692 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6693 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6694 bc573f3b 2021-07-10 stsp if (err)
6695 bc573f3b 2021-07-10 stsp goto done;
6696 bc573f3b 2021-07-10 stsp s->tree = s->root;
6697 bc573f3b 2021-07-10 stsp
6698 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6699 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6700 ffd1d5e5 2018-06-23 stsp goto done;
6701 ffd1d5e5 2018-06-23 stsp
6702 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6703 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6704 ffd1d5e5 2018-06-23 stsp goto done;
6705 ffd1d5e5 2018-06-23 stsp }
6706 ffd1d5e5 2018-06-23 stsp
6707 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6708 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6709 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6710 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6711 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6712 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6713 9cd7cbd1 2020-12-07 stsp goto done;
6714 9cd7cbd1 2020-12-07 stsp }
6715 9cd7cbd1 2020-12-07 stsp }
6716 fb2756b9 2018-08-04 stsp s->repo = repo;
6717 c0b01bdb 2019-11-08 stsp
6718 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6719 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6720 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6721 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6722 c0b01bdb 2019-11-08 stsp if (err)
6723 c0b01bdb 2019-11-08 stsp goto done;
6724 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6725 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6726 bc573f3b 2021-07-10 stsp if (err)
6727 c0b01bdb 2019-11-08 stsp goto done;
6728 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6729 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6730 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6731 bc573f3b 2021-07-10 stsp if (err)
6732 c0b01bdb 2019-11-08 stsp goto done;
6733 e5a0f69f 2018-08-18 stsp
6734 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6735 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6736 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6737 bc573f3b 2021-07-10 stsp if (err)
6738 11b20872 2019-11-08 stsp goto done;
6739 11b20872 2019-11-08 stsp
6740 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6741 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6742 bc573f3b 2021-07-10 stsp if (err)
6743 c0b01bdb 2019-11-08 stsp goto done;
6744 c0b01bdb 2019-11-08 stsp }
6745 c0b01bdb 2019-11-08 stsp
6746 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6747 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6748 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6749 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6750 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6751 ad80ab7b 2018-08-04 stsp done:
6752 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6753 bc573f3b 2021-07-10 stsp if (commit)
6754 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6755 bc573f3b 2021-07-10 stsp if (err)
6756 bc573f3b 2021-07-10 stsp close_tree_view(view);
6757 ad80ab7b 2018-08-04 stsp return err;
6758 ad80ab7b 2018-08-04 stsp }
6759 ad80ab7b 2018-08-04 stsp
6760 e5a0f69f 2018-08-18 stsp static const struct got_error *
6761 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6762 ad80ab7b 2018-08-04 stsp {
6763 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6764 ad80ab7b 2018-08-04 stsp
6765 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6766 fb2756b9 2018-08-04 stsp free(s->tree_label);
6767 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6768 6484ec90 2018-09-29 stsp free(s->commit_id);
6769 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6770 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6771 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6772 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6773 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6774 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6775 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6776 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6777 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6778 ad80ab7b 2018-08-04 stsp free(parent);
6779 ad80ab7b 2018-08-04 stsp
6780 ad80ab7b 2018-08-04 stsp }
6781 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6782 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6783 bc573f3b 2021-07-10 stsp if (s->root)
6784 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6785 7c32bd05 2019-06-22 stsp return NULL;
6786 7c32bd05 2019-06-22 stsp }
6787 7c32bd05 2019-06-22 stsp
6788 7c32bd05 2019-06-22 stsp static const struct got_error *
6789 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6790 7c32bd05 2019-06-22 stsp {
6791 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6792 7c32bd05 2019-06-22 stsp
6793 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6794 7c32bd05 2019-06-22 stsp return NULL;
6795 7c32bd05 2019-06-22 stsp }
6796 7c32bd05 2019-06-22 stsp
6797 7c32bd05 2019-06-22 stsp static int
6798 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6799 7c32bd05 2019-06-22 stsp {
6800 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6801 7c32bd05 2019-06-22 stsp
6802 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6803 56e0773d 2019-11-28 stsp 0) == 0;
6804 7c32bd05 2019-06-22 stsp }
6805 7c32bd05 2019-06-22 stsp
6806 7c32bd05 2019-06-22 stsp static const struct got_error *
6807 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6808 7c32bd05 2019-06-22 stsp {
6809 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6810 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6811 7c32bd05 2019-06-22 stsp
6812 7c32bd05 2019-06-22 stsp if (!view->searching) {
6813 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6814 7c32bd05 2019-06-22 stsp return NULL;
6815 7c32bd05 2019-06-22 stsp }
6816 7c32bd05 2019-06-22 stsp
6817 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6818 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6819 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6820 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6821 56e0773d 2019-11-28 stsp s->selected_entry);
6822 7c32bd05 2019-06-22 stsp else
6823 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6824 56e0773d 2019-11-28 stsp } else {
6825 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6826 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6827 56e0773d 2019-11-28 stsp else
6828 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6829 56e0773d 2019-11-28 stsp s->selected_entry);
6830 7c32bd05 2019-06-22 stsp }
6831 7c32bd05 2019-06-22 stsp } else {
6832 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6833 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6834 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6835 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6836 56e0773d 2019-11-28 stsp else
6837 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6838 7c32bd05 2019-06-22 stsp }
6839 7c32bd05 2019-06-22 stsp
6840 7c32bd05 2019-06-22 stsp while (1) {
6841 56e0773d 2019-11-28 stsp if (te == NULL) {
6842 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6843 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6844 ac66afb8 2019-06-24 stsp return NULL;
6845 ac66afb8 2019-06-24 stsp }
6846 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6847 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6848 56e0773d 2019-11-28 stsp else
6849 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6850 7c32bd05 2019-06-22 stsp }
6851 7c32bd05 2019-06-22 stsp
6852 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6853 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6854 56e0773d 2019-11-28 stsp s->matched_entry = te;
6855 7c32bd05 2019-06-22 stsp break;
6856 7c32bd05 2019-06-22 stsp }
6857 7c32bd05 2019-06-22 stsp
6858 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6859 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6860 56e0773d 2019-11-28 stsp else
6861 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6862 7c32bd05 2019-06-22 stsp }
6863 e5a0f69f 2018-08-18 stsp
6864 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6865 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6866 7c32bd05 2019-06-22 stsp s->selected = 0;
6867 7c32bd05 2019-06-22 stsp }
6868 7c32bd05 2019-06-22 stsp
6869 e5a0f69f 2018-08-18 stsp return NULL;
6870 ad80ab7b 2018-08-04 stsp }
6871 ad80ab7b 2018-08-04 stsp
6872 ad80ab7b 2018-08-04 stsp static const struct got_error *
6873 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6874 ad80ab7b 2018-08-04 stsp {
6875 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6876 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6877 e5a0f69f 2018-08-18 stsp char *parent_path;
6878 ad80ab7b 2018-08-04 stsp
6879 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6880 e5a0f69f 2018-08-18 stsp if (err)
6881 e5a0f69f 2018-08-18 stsp return err;
6882 ffd1d5e5 2018-06-23 stsp
6883 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6884 e5a0f69f 2018-08-18 stsp free(parent_path);
6885 669b5ffa 2018-10-07 stsp
6886 a5d43cac 2022-07-01 thomas view_border(view);
6887 e5a0f69f 2018-08-18 stsp return err;
6888 07dd3ed3 2022-08-06 thomas }
6889 07dd3ed3 2022-08-06 thomas
6890 07dd3ed3 2022-08-06 thomas static const struct got_error *
6891 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
6892 07dd3ed3 2022-08-06 thomas {
6893 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
6894 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
6895 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
6896 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
6897 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
6898 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
6899 07dd3ed3 2022-08-06 thomas
6900 07dd3ed3 2022-08-06 thomas g = view->gline;
6901 07dd3ed3 2022-08-06 thomas view->gline = 0;
6902 07dd3ed3 2022-08-06 thomas
6903 07dd3ed3 2022-08-06 thomas if (g == 0)
6904 07dd3ed3 2022-08-06 thomas g = 1;
6905 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
6906 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6907 07dd3ed3 2022-08-06 thomas
6908 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
6909 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
6910 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
6911 07dd3ed3 2022-08-06 thomas
6912 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
6913 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6914 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
6915 07dd3ed3 2022-08-06 thomas }
6916 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
6917 07dd3ed3 2022-08-06 thomas last += off;
6918 07dd3ed3 2022-08-06 thomas
6919 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
6920 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6921 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
6922 07dd3ed3 2022-08-06 thomas }
6923 07dd3ed3 2022-08-06 thomas
6924 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
6925 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
6926 07dd3ed3 2022-08-06 thomas i += off;
6927 07dd3ed3 2022-08-06 thomas }
6928 07dd3ed3 2022-08-06 thomas
6929 07dd3ed3 2022-08-06 thomas if (i < g) {
6930 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
6931 07dd3ed3 2022-08-06 thomas if (err)
6932 07dd3ed3 2022-08-06 thomas return err;
6933 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
6934 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
6935 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
6936 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
6937 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6938 07dd3ed3 2022-08-06 thomas first += off;
6939 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6940 07dd3ed3 2022-08-06 thomas }
6941 07dd3ed3 2022-08-06 thomas } else if (i > g)
6942 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
6943 07dd3ed3 2022-08-06 thomas
6944 07dd3ed3 2022-08-06 thomas if (g < nlines &&
6945 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6946 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
6947 07dd3ed3 2022-08-06 thomas
6948 07dd3ed3 2022-08-06 thomas return NULL;
6949 e5a0f69f 2018-08-18 stsp }
6950 ce52c690 2018-06-23 stsp
6951 e5a0f69f 2018-08-18 stsp static const struct got_error *
6952 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6953 e5a0f69f 2018-08-18 stsp {
6954 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6955 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6956 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6957 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
6958 ffd1d5e5 2018-06-23 stsp
6959 07dd3ed3 2022-08-06 thomas if (view->gline)
6960 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
6961 07dd3ed3 2022-08-06 thomas
6962 e5a0f69f 2018-08-18 stsp switch (ch) {
6963 1e37a5c2 2019-05-12 jcs case 'i':
6964 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6965 07b0611c 2022-06-23 thomas view->count = 0;
6966 1e37a5c2 2019-05-12 jcs break;
6967 1be4947a 2022-07-22 thomas case 'L':
6968 07b0611c 2022-06-23 thomas view->count = 0;
6969 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6970 ffd1d5e5 2018-06-23 stsp break;
6971 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6972 152c1c93 2020-11-29 stsp break;
6973 1be4947a 2022-07-22 thomas case 'R':
6974 07b0611c 2022-06-23 thomas view->count = 0;
6975 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
6976 e4526bf5 2021-09-03 naddy break;
6977 e4526bf5 2021-09-03 naddy case 'g':
6978 e4526bf5 2021-09-03 naddy case KEY_HOME:
6979 e4526bf5 2021-09-03 naddy s->selected = 0;
6980 07b0611c 2022-06-23 thomas view->count = 0;
6981 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6982 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6983 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6984 e4526bf5 2021-09-03 naddy else
6985 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6986 1e37a5c2 2019-05-12 jcs break;
6987 e4526bf5 2021-09-03 naddy case 'G':
6988 a5d43cac 2022-07-01 thomas case KEY_END: {
6989 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6990 a5d43cac 2022-07-01 thomas
6991 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6992 a5d43cac 2022-07-01 thomas --eos; /* border */
6993 e4526bf5 2021-09-03 naddy s->selected = 0;
6994 07b0611c 2022-06-23 thomas view->count = 0;
6995 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6996 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6997 e4526bf5 2021-09-03 naddy if (te == NULL) {
6998 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6999 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7000 e4526bf5 2021-09-03 naddy n++;
7001 e4526bf5 2021-09-03 naddy }
7002 e4526bf5 2021-09-03 naddy break;
7003 e4526bf5 2021-09-03 naddy }
7004 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7005 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7006 e4526bf5 2021-09-03 naddy }
7007 e4526bf5 2021-09-03 naddy if (n > 0)
7008 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7009 e4526bf5 2021-09-03 naddy break;
7010 a5d43cac 2022-07-01 thomas }
7011 1e37a5c2 2019-05-12 jcs case 'k':
7012 1e37a5c2 2019-05-12 jcs case KEY_UP:
7013 f7140bf5 2021-10-17 thomas case CTRL('p'):
7014 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7015 1e37a5c2 2019-05-12 jcs s->selected--;
7016 fa86c4bf 2020-11-29 stsp break;
7017 1e37a5c2 2019-05-12 jcs }
7018 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7019 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7020 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7021 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7022 07b0611c 2022-06-23 thomas view->count = 0;
7023 1e37a5c2 2019-05-12 jcs break;
7024 70f17a53 2022-06-13 thomas case CTRL('u'):
7025 23427b14 2022-06-23 thomas case 'u':
7026 70f17a53 2022-06-13 thomas nscroll /= 2;
7027 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7028 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7029 ea025d1d 2020-02-22 naddy case CTRL('b'):
7030 1c5e5faa 2022-06-23 thomas case 'b':
7031 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7032 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7033 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7034 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7035 fa86c4bf 2020-11-29 stsp } else {
7036 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7037 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7038 fa86c4bf 2020-11-29 stsp }
7039 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7040 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7041 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7042 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7043 07b0611c 2022-06-23 thomas view->count = 0;
7044 1e37a5c2 2019-05-12 jcs break;
7045 1e37a5c2 2019-05-12 jcs case 'j':
7046 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7047 f7140bf5 2021-10-17 thomas case CTRL('n'):
7048 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7049 1e37a5c2 2019-05-12 jcs s->selected++;
7050 1e37a5c2 2019-05-12 jcs break;
7051 1e37a5c2 2019-05-12 jcs }
7052 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7053 07b0611c 2022-06-23 thomas == NULL) {
7054 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7055 07b0611c 2022-06-23 thomas view->count = 0;
7056 1e37a5c2 2019-05-12 jcs break;
7057 07b0611c 2022-06-23 thomas }
7058 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7059 1e37a5c2 2019-05-12 jcs break;
7060 70f17a53 2022-06-13 thomas case CTRL('d'):
7061 23427b14 2022-06-23 thomas case 'd':
7062 70f17a53 2022-06-13 thomas nscroll /= 2;
7063 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7064 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7065 ea025d1d 2020-02-22 naddy case CTRL('f'):
7066 1c5e5faa 2022-06-23 thomas case 'f':
7067 4c2d69cb 2022-06-23 thomas case ' ':
7068 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7069 1e37a5c2 2019-05-12 jcs == NULL) {
7070 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7071 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7072 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7073 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7074 07b0611c 2022-06-23 thomas else
7075 07b0611c 2022-06-23 thomas view->count = 0;
7076 1e37a5c2 2019-05-12 jcs break;
7077 1e37a5c2 2019-05-12 jcs }
7078 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7079 1e37a5c2 2019-05-12 jcs break;
7080 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7081 1e37a5c2 2019-05-12 jcs case '\r':
7082 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7083 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7084 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7085 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7086 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7087 07b0611c 2022-06-23 thomas view->count = 0;
7088 1e37a5c2 2019-05-12 jcs break;
7089 07b0611c 2022-06-23 thomas }
7090 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7091 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7092 1e37a5c2 2019-05-12 jcs entry);
7093 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7094 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7095 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7096 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7097 1e37a5c2 2019-05-12 jcs s->selected_entry =
7098 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7099 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7100 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7101 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7102 a5d43cac 2022-07-01 thomas if (err)
7103 a5d43cac 2022-07-01 thomas break;
7104 a5d43cac 2022-07-01 thomas }
7105 1e37a5c2 2019-05-12 jcs free(parent);
7106 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7107 56e0773d 2019-11-28 stsp s->selected_entry))) {
7108 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7109 07b0611c 2022-06-23 thomas view->count = 0;
7110 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7111 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7112 1e37a5c2 2019-05-12 jcs if (err)
7113 1e37a5c2 2019-05-12 jcs break;
7114 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7115 941e9f74 2019-05-21 stsp if (err) {
7116 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7117 1e37a5c2 2019-05-12 jcs break;
7118 1e37a5c2 2019-05-12 jcs }
7119 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7120 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7121 1e37a5c2 2019-05-12 jcs break;
7122 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7123 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7124 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7125 07b0611c 2022-06-23 thomas view->count = 0;
7126 1e37a5c2 2019-05-12 jcs break;
7127 1e37a5c2 2019-05-12 jcs default:
7128 07b0611c 2022-06-23 thomas view->count = 0;
7129 1e37a5c2 2019-05-12 jcs break;
7130 ffd1d5e5 2018-06-23 stsp }
7131 e5a0f69f 2018-08-18 stsp
7132 ffd1d5e5 2018-06-23 stsp return err;
7133 9f7d7167 2018-04-29 stsp }
7134 9f7d7167 2018-04-29 stsp
7135 ffd1d5e5 2018-06-23 stsp __dead static void
7136 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7137 ffd1d5e5 2018-06-23 stsp {
7138 ffd1d5e5 2018-06-23 stsp endwin();
7139 91198554 2022-06-23 thomas fprintf(stderr,
7140 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7141 ffd1d5e5 2018-06-23 stsp getprogname());
7142 ffd1d5e5 2018-06-23 stsp exit(1);
7143 ffd1d5e5 2018-06-23 stsp }
7144 ffd1d5e5 2018-06-23 stsp
7145 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7146 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7147 ffd1d5e5 2018-06-23 stsp {
7148 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7149 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7150 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7151 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7152 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7153 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7154 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7155 4e97c21c 2020-12-06 stsp char *label = NULL;
7156 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7157 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7158 ffd1d5e5 2018-06-23 stsp int ch;
7159 5221c383 2018-08-01 stsp struct tog_view *view;
7160 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7161 70ac5f84 2019-03-28 stsp
7162 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7163 ffd1d5e5 2018-06-23 stsp switch (ch) {
7164 ffd1d5e5 2018-06-23 stsp case 'c':
7165 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7166 74283ab8 2020-02-07 stsp break;
7167 74283ab8 2020-02-07 stsp case 'r':
7168 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7169 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7170 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7171 74283ab8 2020-02-07 stsp optarg);
7172 ffd1d5e5 2018-06-23 stsp break;
7173 ffd1d5e5 2018-06-23 stsp default:
7174 e99e2d15 2020-11-24 naddy usage_tree();
7175 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7176 ffd1d5e5 2018-06-23 stsp }
7177 ffd1d5e5 2018-06-23 stsp }
7178 ffd1d5e5 2018-06-23 stsp
7179 ffd1d5e5 2018-06-23 stsp argc -= optind;
7180 ffd1d5e5 2018-06-23 stsp argv += optind;
7181 ffd1d5e5 2018-06-23 stsp
7182 55cccc34 2020-02-20 stsp if (argc > 1)
7183 e99e2d15 2020-11-24 naddy usage_tree();
7184 7cd52833 2022-06-23 thomas
7185 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7186 7cd52833 2022-06-23 thomas if (error != NULL)
7187 7cd52833 2022-06-23 thomas goto done;
7188 74283ab8 2020-02-07 stsp
7189 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7190 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7191 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7192 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7193 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7194 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7195 c156c7a4 2020-12-18 stsp goto done;
7196 55cccc34 2020-02-20 stsp if (worktree)
7197 52185f70 2019-02-05 stsp repo_path =
7198 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7199 55cccc34 2020-02-20 stsp else
7200 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7201 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7202 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7203 c156c7a4 2020-12-18 stsp goto done;
7204 c156c7a4 2020-12-18 stsp }
7205 55cccc34 2020-02-20 stsp }
7206 a915003a 2019-02-05 stsp
7207 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7208 c02c541e 2019-03-29 stsp if (error != NULL)
7209 52185f70 2019-02-05 stsp goto done;
7210 d188b9a6 2019-01-04 stsp
7211 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7212 55cccc34 2020-02-20 stsp repo, worktree);
7213 55cccc34 2020-02-20 stsp if (error)
7214 55cccc34 2020-02-20 stsp goto done;
7215 55cccc34 2020-02-20 stsp
7216 55cccc34 2020-02-20 stsp init_curses();
7217 55cccc34 2020-02-20 stsp
7218 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7219 c02c541e 2019-03-29 stsp if (error)
7220 52185f70 2019-02-05 stsp goto done;
7221 ffd1d5e5 2018-06-23 stsp
7222 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7223 51a10b52 2020-12-26 stsp if (error)
7224 51a10b52 2020-12-26 stsp goto done;
7225 51a10b52 2020-12-26 stsp
7226 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7227 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7228 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7229 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7230 4e97c21c 2020-12-06 stsp if (error)
7231 4e97c21c 2020-12-06 stsp goto done;
7232 4e97c21c 2020-12-06 stsp head_ref_name = label;
7233 4e97c21c 2020-12-06 stsp } else {
7234 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7235 4e97c21c 2020-12-06 stsp if (error == NULL)
7236 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7237 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7238 4e97c21c 2020-12-06 stsp goto done;
7239 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7240 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7241 4e97c21c 2020-12-06 stsp if (error)
7242 4e97c21c 2020-12-06 stsp goto done;
7243 4e97c21c 2020-12-06 stsp }
7244 ffd1d5e5 2018-06-23 stsp
7245 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7246 945f9229 2022-04-16 thomas if (error)
7247 945f9229 2022-04-16 thomas goto done;
7248 945f9229 2022-04-16 thomas
7249 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7250 5221c383 2018-08-01 stsp if (view == NULL) {
7251 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7252 5221c383 2018-08-01 stsp goto done;
7253 5221c383 2018-08-01 stsp }
7254 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7255 ad80ab7b 2018-08-04 stsp if (error)
7256 ad80ab7b 2018-08-04 stsp goto done;
7257 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7258 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7259 d91faf3b 2020-12-01 naddy in_repo_path);
7260 55cccc34 2020-02-20 stsp if (error)
7261 55cccc34 2020-02-20 stsp goto done;
7262 55cccc34 2020-02-20 stsp }
7263 55cccc34 2020-02-20 stsp
7264 55cccc34 2020-02-20 stsp if (worktree) {
7265 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7266 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7267 55cccc34 2020-02-20 stsp worktree = NULL;
7268 55cccc34 2020-02-20 stsp }
7269 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7270 ffd1d5e5 2018-06-23 stsp done:
7271 52185f70 2019-02-05 stsp free(repo_path);
7272 e4a0e26d 2020-02-20 stsp free(cwd);
7273 ffd1d5e5 2018-06-23 stsp free(commit_id);
7274 4e97c21c 2020-12-06 stsp free(label);
7275 486cd271 2020-12-06 stsp if (ref)
7276 486cd271 2020-12-06 stsp got_ref_close(ref);
7277 1d0f4054 2021-06-17 stsp if (repo) {
7278 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7279 1d0f4054 2021-06-17 stsp if (error == NULL)
7280 1d0f4054 2021-06-17 stsp error = close_err;
7281 1d0f4054 2021-06-17 stsp }
7282 7cd52833 2022-06-23 thomas if (pack_fds) {
7283 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7284 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7285 7cd52833 2022-06-23 thomas if (error == NULL)
7286 7cd52833 2022-06-23 thomas error = pack_err;
7287 7cd52833 2022-06-23 thomas }
7288 51a10b52 2020-12-26 stsp tog_free_refs();
7289 ffd1d5e5 2018-06-23 stsp return error;
7290 6458efa5 2020-11-24 stsp }
7291 6458efa5 2020-11-24 stsp
7292 6458efa5 2020-11-24 stsp static const struct got_error *
7293 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7294 6458efa5 2020-11-24 stsp {
7295 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7296 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7297 6458efa5 2020-11-24 stsp
7298 6458efa5 2020-11-24 stsp s->nrefs = 0;
7299 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7300 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7301 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7302 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7303 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7304 6458efa5 2020-11-24 stsp continue;
7305 6458efa5 2020-11-24 stsp
7306 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7307 6458efa5 2020-11-24 stsp if (re == NULL)
7308 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7309 6458efa5 2020-11-24 stsp
7310 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7311 8924d611 2020-12-26 stsp if (re->ref == NULL)
7312 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7313 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7314 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7315 6458efa5 2020-11-24 stsp }
7316 6458efa5 2020-11-24 stsp
7317 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7318 6458efa5 2020-11-24 stsp return NULL;
7319 6458efa5 2020-11-24 stsp }
7320 6458efa5 2020-11-24 stsp
7321 ef20f542 2022-06-26 thomas static void
7322 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7323 6458efa5 2020-11-24 stsp {
7324 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7325 6458efa5 2020-11-24 stsp
7326 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7327 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7328 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7329 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7330 6458efa5 2020-11-24 stsp free(re);
7331 6458efa5 2020-11-24 stsp }
7332 6458efa5 2020-11-24 stsp }
7333 6458efa5 2020-11-24 stsp
7334 6458efa5 2020-11-24 stsp static const struct got_error *
7335 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7336 6458efa5 2020-11-24 stsp {
7337 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7338 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7339 6458efa5 2020-11-24 stsp
7340 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7341 6458efa5 2020-11-24 stsp s->repo = repo;
7342 6458efa5 2020-11-24 stsp
7343 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7344 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7345 6458efa5 2020-11-24 stsp
7346 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7347 6458efa5 2020-11-24 stsp if (err)
7348 6458efa5 2020-11-24 stsp return err;
7349 34ba6917 2020-11-30 stsp
7350 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7351 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7352 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7353 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7354 6458efa5 2020-11-24 stsp if (err)
7355 6458efa5 2020-11-24 stsp goto done;
7356 6458efa5 2020-11-24 stsp
7357 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7358 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7359 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7360 6458efa5 2020-11-24 stsp if (err)
7361 6458efa5 2020-11-24 stsp goto done;
7362 6458efa5 2020-11-24 stsp
7363 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7364 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7365 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7366 2183bbf6 2022-01-23 thomas if (err)
7367 2183bbf6 2022-01-23 thomas goto done;
7368 2183bbf6 2022-01-23 thomas
7369 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7370 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7371 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7372 6458efa5 2020-11-24 stsp if (err)
7373 6458efa5 2020-11-24 stsp goto done;
7374 6458efa5 2020-11-24 stsp }
7375 6458efa5 2020-11-24 stsp
7376 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7377 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7378 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7379 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7380 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7381 6458efa5 2020-11-24 stsp done:
7382 6458efa5 2020-11-24 stsp if (err)
7383 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7384 6458efa5 2020-11-24 stsp return err;
7385 6458efa5 2020-11-24 stsp }
7386 6458efa5 2020-11-24 stsp
7387 6458efa5 2020-11-24 stsp static const struct got_error *
7388 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7389 6458efa5 2020-11-24 stsp {
7390 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7391 6458efa5 2020-11-24 stsp
7392 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7393 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7394 6458efa5 2020-11-24 stsp
7395 6458efa5 2020-11-24 stsp return NULL;
7396 9f7d7167 2018-04-29 stsp }
7397 ce5b7c56 2019-07-09 stsp
7398 6458efa5 2020-11-24 stsp static const struct got_error *
7399 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7400 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7401 6458efa5 2020-11-24 stsp {
7402 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7403 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7404 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7405 6458efa5 2020-11-24 stsp int obj_type;
7406 6458efa5 2020-11-24 stsp
7407 c42c9805 2020-11-24 stsp *commit_id = NULL;
7408 6458efa5 2020-11-24 stsp
7409 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7410 6458efa5 2020-11-24 stsp if (err)
7411 6458efa5 2020-11-24 stsp return err;
7412 6458efa5 2020-11-24 stsp
7413 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7414 6458efa5 2020-11-24 stsp if (err)
7415 6458efa5 2020-11-24 stsp goto done;
7416 6458efa5 2020-11-24 stsp
7417 6458efa5 2020-11-24 stsp switch (obj_type) {
7418 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7419 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7420 6458efa5 2020-11-24 stsp break;
7421 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7422 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7423 6458efa5 2020-11-24 stsp if (err)
7424 6458efa5 2020-11-24 stsp goto done;
7425 c42c9805 2020-11-24 stsp free(obj_id);
7426 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7427 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7428 c42c9805 2020-11-24 stsp if (err)
7429 6458efa5 2020-11-24 stsp goto done;
7430 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7431 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7432 c42c9805 2020-11-24 stsp goto done;
7433 c42c9805 2020-11-24 stsp }
7434 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7435 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7436 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7437 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7438 c42c9805 2020-11-24 stsp goto done;
7439 c42c9805 2020-11-24 stsp }
7440 6458efa5 2020-11-24 stsp break;
7441 6458efa5 2020-11-24 stsp default:
7442 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7443 c42c9805 2020-11-24 stsp break;
7444 6458efa5 2020-11-24 stsp }
7445 6458efa5 2020-11-24 stsp
7446 c42c9805 2020-11-24 stsp done:
7447 c42c9805 2020-11-24 stsp if (tag)
7448 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7449 c42c9805 2020-11-24 stsp if (err) {
7450 c42c9805 2020-11-24 stsp free(*commit_id);
7451 c42c9805 2020-11-24 stsp *commit_id = NULL;
7452 c42c9805 2020-11-24 stsp }
7453 c42c9805 2020-11-24 stsp return err;
7454 c42c9805 2020-11-24 stsp }
7455 c42c9805 2020-11-24 stsp
7456 c42c9805 2020-11-24 stsp static const struct got_error *
7457 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7458 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7459 c42c9805 2020-11-24 stsp {
7460 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7461 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7462 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7463 c42c9805 2020-11-24 stsp
7464 c42c9805 2020-11-24 stsp *new_view = NULL;
7465 c42c9805 2020-11-24 stsp
7466 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7467 c42c9805 2020-11-24 stsp if (err) {
7468 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7469 c42c9805 2020-11-24 stsp return err;
7470 c42c9805 2020-11-24 stsp else
7471 c42c9805 2020-11-24 stsp return NULL;
7472 c42c9805 2020-11-24 stsp }
7473 c42c9805 2020-11-24 stsp
7474 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7475 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7476 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7477 6458efa5 2020-11-24 stsp goto done;
7478 6458efa5 2020-11-24 stsp }
7479 6458efa5 2020-11-24 stsp
7480 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7481 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7482 6458efa5 2020-11-24 stsp done:
7483 6458efa5 2020-11-24 stsp if (err)
7484 6458efa5 2020-11-24 stsp view_close(log_view);
7485 6458efa5 2020-11-24 stsp else
7486 6458efa5 2020-11-24 stsp *new_view = log_view;
7487 c42c9805 2020-11-24 stsp free(commit_id);
7488 6458efa5 2020-11-24 stsp return err;
7489 6458efa5 2020-11-24 stsp }
7490 6458efa5 2020-11-24 stsp
7491 ce5b7c56 2019-07-09 stsp static void
7492 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7493 6458efa5 2020-11-24 stsp {
7494 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7495 34ba6917 2020-11-30 stsp int i = 0;
7496 6458efa5 2020-11-24 stsp
7497 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7498 6458efa5 2020-11-24 stsp return;
7499 6458efa5 2020-11-24 stsp
7500 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7501 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7502 34ba6917 2020-11-30 stsp if (re == NULL)
7503 34ba6917 2020-11-30 stsp break;
7504 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7505 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7506 6458efa5 2020-11-24 stsp }
7507 6458efa5 2020-11-24 stsp }
7508 6458efa5 2020-11-24 stsp
7509 a5d43cac 2022-07-01 thomas static const struct got_error *
7510 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7511 6458efa5 2020-11-24 stsp {
7512 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7513 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7514 6458efa5 2020-11-24 stsp int n = 0;
7515 6458efa5 2020-11-24 stsp
7516 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7517 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7518 6458efa5 2020-11-24 stsp else
7519 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7520 6458efa5 2020-11-24 stsp
7521 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7522 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7523 07dd3ed3 2022-08-06 thomas if (last) {
7524 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7525 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7526 07dd3ed3 2022-08-06 thomas }
7527 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7528 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7529 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7530 6458efa5 2020-11-24 stsp }
7531 6458efa5 2020-11-24 stsp }
7532 a5d43cac 2022-07-01 thomas
7533 a5d43cac 2022-07-01 thomas return NULL;
7534 6458efa5 2020-11-24 stsp }
7535 6458efa5 2020-11-24 stsp
7536 6458efa5 2020-11-24 stsp static const struct got_error *
7537 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7538 6458efa5 2020-11-24 stsp {
7539 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7540 6458efa5 2020-11-24 stsp
7541 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7542 6458efa5 2020-11-24 stsp return NULL;
7543 6458efa5 2020-11-24 stsp }
7544 6458efa5 2020-11-24 stsp
7545 6458efa5 2020-11-24 stsp static int
7546 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7547 6458efa5 2020-11-24 stsp {
7548 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7549 6458efa5 2020-11-24 stsp
7550 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7551 6458efa5 2020-11-24 stsp 0) == 0;
7552 6458efa5 2020-11-24 stsp }
7553 6458efa5 2020-11-24 stsp
7554 6458efa5 2020-11-24 stsp static const struct got_error *
7555 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7556 6458efa5 2020-11-24 stsp {
7557 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7558 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7559 6458efa5 2020-11-24 stsp
7560 6458efa5 2020-11-24 stsp if (!view->searching) {
7561 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7562 6458efa5 2020-11-24 stsp return NULL;
7563 6458efa5 2020-11-24 stsp }
7564 6458efa5 2020-11-24 stsp
7565 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7566 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7567 6458efa5 2020-11-24 stsp if (s->selected_entry)
7568 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7569 6458efa5 2020-11-24 stsp else
7570 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7571 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7572 6458efa5 2020-11-24 stsp } else {
7573 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7574 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7575 6458efa5 2020-11-24 stsp else
7576 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7577 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7578 6458efa5 2020-11-24 stsp }
7579 6458efa5 2020-11-24 stsp } else {
7580 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7581 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7582 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7583 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7584 6458efa5 2020-11-24 stsp else
7585 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7586 6458efa5 2020-11-24 stsp }
7587 6458efa5 2020-11-24 stsp
7588 6458efa5 2020-11-24 stsp while (1) {
7589 6458efa5 2020-11-24 stsp if (re == NULL) {
7590 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7591 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7592 6458efa5 2020-11-24 stsp return NULL;
7593 6458efa5 2020-11-24 stsp }
7594 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7595 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7596 6458efa5 2020-11-24 stsp else
7597 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7598 6458efa5 2020-11-24 stsp }
7599 6458efa5 2020-11-24 stsp
7600 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7601 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7602 6458efa5 2020-11-24 stsp s->matched_entry = re;
7603 6458efa5 2020-11-24 stsp break;
7604 6458efa5 2020-11-24 stsp }
7605 6458efa5 2020-11-24 stsp
7606 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7607 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7608 6458efa5 2020-11-24 stsp else
7609 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7610 6458efa5 2020-11-24 stsp }
7611 6458efa5 2020-11-24 stsp
7612 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7613 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7614 6458efa5 2020-11-24 stsp s->selected = 0;
7615 6458efa5 2020-11-24 stsp }
7616 6458efa5 2020-11-24 stsp
7617 6458efa5 2020-11-24 stsp return NULL;
7618 6458efa5 2020-11-24 stsp }
7619 6458efa5 2020-11-24 stsp
7620 6458efa5 2020-11-24 stsp static const struct got_error *
7621 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7622 6458efa5 2020-11-24 stsp {
7623 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7624 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7625 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7626 6458efa5 2020-11-24 stsp char *line = NULL;
7627 6458efa5 2020-11-24 stsp wchar_t *wline;
7628 6458efa5 2020-11-24 stsp struct tog_color *tc;
7629 6458efa5 2020-11-24 stsp int width, n;
7630 6458efa5 2020-11-24 stsp int limit = view->nlines;
7631 6458efa5 2020-11-24 stsp
7632 6458efa5 2020-11-24 stsp werase(view->window);
7633 6458efa5 2020-11-24 stsp
7634 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7635 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7636 a5d43cac 2022-07-01 thomas --limit; /* border */
7637 6458efa5 2020-11-24 stsp
7638 6458efa5 2020-11-24 stsp if (limit == 0)
7639 6458efa5 2020-11-24 stsp return NULL;
7640 6458efa5 2020-11-24 stsp
7641 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7642 6458efa5 2020-11-24 stsp
7643 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7644 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7645 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7646 6458efa5 2020-11-24 stsp
7647 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7648 6458efa5 2020-11-24 stsp if (err) {
7649 6458efa5 2020-11-24 stsp free(line);
7650 6458efa5 2020-11-24 stsp return err;
7651 6458efa5 2020-11-24 stsp }
7652 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7653 6458efa5 2020-11-24 stsp wstandout(view->window);
7654 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7655 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7656 6458efa5 2020-11-24 stsp wstandend(view->window);
7657 6458efa5 2020-11-24 stsp free(wline);
7658 6458efa5 2020-11-24 stsp wline = NULL;
7659 6458efa5 2020-11-24 stsp free(line);
7660 6458efa5 2020-11-24 stsp line = NULL;
7661 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7662 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7663 6458efa5 2020-11-24 stsp if (--limit <= 0)
7664 6458efa5 2020-11-24 stsp return NULL;
7665 6458efa5 2020-11-24 stsp
7666 6458efa5 2020-11-24 stsp n = 0;
7667 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7668 6458efa5 2020-11-24 stsp char *line = NULL;
7669 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7670 6458efa5 2020-11-24 stsp
7671 84227eb1 2022-06-23 thomas if (s->show_date) {
7672 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7673 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7674 84227eb1 2022-06-23 thomas struct got_object_id *id;
7675 84227eb1 2022-06-23 thomas struct tm tm;
7676 84227eb1 2022-06-23 thomas time_t t;
7677 84227eb1 2022-06-23 thomas
7678 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7679 84227eb1 2022-06-23 thomas if (err)
7680 84227eb1 2022-06-23 thomas return err;
7681 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7682 84227eb1 2022-06-23 thomas if (err) {
7683 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7684 84227eb1 2022-06-23 thomas free(id);
7685 84227eb1 2022-06-23 thomas return err;
7686 84227eb1 2022-06-23 thomas }
7687 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7688 84227eb1 2022-06-23 thomas id);
7689 84227eb1 2022-06-23 thomas if (err) {
7690 84227eb1 2022-06-23 thomas free(id);
7691 84227eb1 2022-06-23 thomas return err;
7692 84227eb1 2022-06-23 thomas }
7693 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7694 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7695 84227eb1 2022-06-23 thomas } else {
7696 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7697 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7698 84227eb1 2022-06-23 thomas }
7699 84227eb1 2022-06-23 thomas free(id);
7700 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7701 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7702 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7703 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7704 84227eb1 2022-06-23 thomas }
7705 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7706 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7707 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7708 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7709 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7710 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7711 6458efa5 2020-11-24 stsp struct got_object_id *id;
7712 6458efa5 2020-11-24 stsp char *id_str;
7713 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7714 6458efa5 2020-11-24 stsp if (err)
7715 6458efa5 2020-11-24 stsp return err;
7716 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7717 6458efa5 2020-11-24 stsp if (err) {
7718 6458efa5 2020-11-24 stsp free(id);
7719 6458efa5 2020-11-24 stsp return err;
7720 6458efa5 2020-11-24 stsp }
7721 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7722 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7723 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7724 6458efa5 2020-11-24 stsp free(id);
7725 6458efa5 2020-11-24 stsp free(id_str);
7726 6458efa5 2020-11-24 stsp return err;
7727 6458efa5 2020-11-24 stsp }
7728 6458efa5 2020-11-24 stsp free(id);
7729 6458efa5 2020-11-24 stsp free(id_str);
7730 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7731 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7732 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7733 6458efa5 2020-11-24 stsp
7734 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7735 f91a2b48 2022-06-23 thomas 0, 0);
7736 6458efa5 2020-11-24 stsp if (err) {
7737 6458efa5 2020-11-24 stsp free(line);
7738 6458efa5 2020-11-24 stsp return err;
7739 6458efa5 2020-11-24 stsp }
7740 6458efa5 2020-11-24 stsp if (n == s->selected) {
7741 6458efa5 2020-11-24 stsp if (view->focussed)
7742 6458efa5 2020-11-24 stsp wstandout(view->window);
7743 6458efa5 2020-11-24 stsp s->selected_entry = re;
7744 6458efa5 2020-11-24 stsp }
7745 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7746 6458efa5 2020-11-24 stsp if (tc)
7747 6458efa5 2020-11-24 stsp wattr_on(view->window,
7748 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7749 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7750 6458efa5 2020-11-24 stsp if (tc)
7751 6458efa5 2020-11-24 stsp wattr_off(view->window,
7752 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7753 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7754 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7755 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7756 6458efa5 2020-11-24 stsp wstandend(view->window);
7757 6458efa5 2020-11-24 stsp free(line);
7758 6458efa5 2020-11-24 stsp free(wline);
7759 6458efa5 2020-11-24 stsp wline = NULL;
7760 6458efa5 2020-11-24 stsp n++;
7761 6458efa5 2020-11-24 stsp s->ndisplayed++;
7762 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7763 6458efa5 2020-11-24 stsp
7764 6458efa5 2020-11-24 stsp limit--;
7765 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7766 6458efa5 2020-11-24 stsp }
7767 6458efa5 2020-11-24 stsp
7768 a5d43cac 2022-07-01 thomas view_border(view);
7769 6458efa5 2020-11-24 stsp return err;
7770 6458efa5 2020-11-24 stsp }
7771 6458efa5 2020-11-24 stsp
7772 6458efa5 2020-11-24 stsp static const struct got_error *
7773 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7774 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7775 c42c9805 2020-11-24 stsp {
7776 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7777 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7778 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7779 c42c9805 2020-11-24 stsp
7780 c42c9805 2020-11-24 stsp *new_view = NULL;
7781 c42c9805 2020-11-24 stsp
7782 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7783 c42c9805 2020-11-24 stsp if (err) {
7784 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7785 c42c9805 2020-11-24 stsp return err;
7786 c42c9805 2020-11-24 stsp else
7787 c42c9805 2020-11-24 stsp return NULL;
7788 c42c9805 2020-11-24 stsp }
7789 c42c9805 2020-11-24 stsp
7790 c42c9805 2020-11-24 stsp
7791 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7792 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7793 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7794 c42c9805 2020-11-24 stsp goto done;
7795 c42c9805 2020-11-24 stsp }
7796 c42c9805 2020-11-24 stsp
7797 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7798 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7799 c42c9805 2020-11-24 stsp if (err)
7800 c42c9805 2020-11-24 stsp goto done;
7801 c42c9805 2020-11-24 stsp
7802 c42c9805 2020-11-24 stsp *new_view = tree_view;
7803 c42c9805 2020-11-24 stsp done:
7804 c42c9805 2020-11-24 stsp free(commit_id);
7805 c42c9805 2020-11-24 stsp return err;
7806 07dd3ed3 2022-08-06 thomas }
7807 07dd3ed3 2022-08-06 thomas
7808 07dd3ed3 2022-08-06 thomas static const struct got_error *
7809 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
7810 07dd3ed3 2022-08-06 thomas {
7811 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7812 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
7813 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
7814 07dd3ed3 2022-08-06 thomas
7815 07dd3ed3 2022-08-06 thomas g = view->gline;
7816 07dd3ed3 2022-08-06 thomas view->gline = 0;
7817 07dd3ed3 2022-08-06 thomas
7818 07dd3ed3 2022-08-06 thomas if (g == 0)
7819 07dd3ed3 2022-08-06 thomas g = 1;
7820 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
7821 07dd3ed3 2022-08-06 thomas g = s->nrefs;
7822 07dd3ed3 2022-08-06 thomas
7823 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
7824 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
7825 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
7826 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7827 07dd3ed3 2022-08-06 thomas return NULL;
7828 07dd3ed3 2022-08-06 thomas }
7829 07dd3ed3 2022-08-06 thomas
7830 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
7831 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
7832 07dd3ed3 2022-08-06 thomas if (err)
7833 07dd3ed3 2022-08-06 thomas return err;
7834 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7835 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
7836 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
7837 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7838 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
7839 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
7840 07dd3ed3 2022-08-06 thomas
7841 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
7842 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7843 07dd3ed3 2022-08-06 thomas
7844 07dd3ed3 2022-08-06 thomas return NULL;
7845 07dd3ed3 2022-08-06 thomas
7846 c42c9805 2020-11-24 stsp }
7847 07dd3ed3 2022-08-06 thomas
7848 c42c9805 2020-11-24 stsp static const struct got_error *
7849 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7850 6458efa5 2020-11-24 stsp {
7851 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7852 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7853 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7854 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
7855 6458efa5 2020-11-24 stsp
7856 07dd3ed3 2022-08-06 thomas if (view->gline)
7857 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
7858 07dd3ed3 2022-08-06 thomas
7859 6458efa5 2020-11-24 stsp switch (ch) {
7860 6458efa5 2020-11-24 stsp case 'i':
7861 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7862 07b0611c 2022-06-23 thomas view->count = 0;
7863 3bfadbd4 2021-11-20 thomas break;
7864 84227eb1 2022-06-23 thomas case 'm':
7865 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7866 07b0611c 2022-06-23 thomas view->count = 0;
7867 84227eb1 2022-06-23 thomas break;
7868 98182bd0 2021-11-20 thomas case 'o':
7869 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7870 07b0611c 2022-06-23 thomas view->count = 0;
7871 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7872 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7873 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7874 c591440f 2021-11-20 thomas if (err)
7875 c591440f 2021-11-20 thomas break;
7876 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7877 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7878 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7879 3bfadbd4 2021-11-20 thomas if (err)
7880 3bfadbd4 2021-11-20 thomas break;
7881 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7882 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7883 6458efa5 2020-11-24 stsp break;
7884 6458efa5 2020-11-24 stsp case KEY_ENTER:
7885 6458efa5 2020-11-24 stsp case '\r':
7886 07b0611c 2022-06-23 thomas view->count = 0;
7887 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7888 a5d43cac 2022-07-01 thomas break;
7889 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7890 6458efa5 2020-11-24 stsp break;
7891 1be4947a 2022-07-22 thomas case 'T':
7892 07b0611c 2022-06-23 thomas view->count = 0;
7893 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7894 c42c9805 2020-11-24 stsp break;
7895 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
7896 c42c9805 2020-11-24 stsp break;
7897 e4526bf5 2021-09-03 naddy case 'g':
7898 e4526bf5 2021-09-03 naddy case KEY_HOME:
7899 e4526bf5 2021-09-03 naddy s->selected = 0;
7900 07b0611c 2022-06-23 thomas view->count = 0;
7901 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7902 e4526bf5 2021-09-03 naddy break;
7903 e4526bf5 2021-09-03 naddy case 'G':
7904 a5d43cac 2022-07-01 thomas case KEY_END: {
7905 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7906 a5d43cac 2022-07-01 thomas
7907 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7908 a5d43cac 2022-07-01 thomas --eos; /* border */
7909 e4526bf5 2021-09-03 naddy s->selected = 0;
7910 07b0611c 2022-06-23 thomas view->count = 0;
7911 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7912 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7913 e4526bf5 2021-09-03 naddy if (re == NULL)
7914 e4526bf5 2021-09-03 naddy break;
7915 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7916 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7917 e4526bf5 2021-09-03 naddy }
7918 e4526bf5 2021-09-03 naddy if (n > 0)
7919 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7920 e4526bf5 2021-09-03 naddy break;
7921 a5d43cac 2022-07-01 thomas }
7922 6458efa5 2020-11-24 stsp case 'k':
7923 6458efa5 2020-11-24 stsp case KEY_UP:
7924 f7140bf5 2021-10-17 thomas case CTRL('p'):
7925 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7926 6458efa5 2020-11-24 stsp s->selected--;
7927 6458efa5 2020-11-24 stsp break;
7928 34ba6917 2020-11-30 stsp }
7929 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7930 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7931 07b0611c 2022-06-23 thomas view->count = 0;
7932 6458efa5 2020-11-24 stsp break;
7933 70f17a53 2022-06-13 thomas case CTRL('u'):
7934 23427b14 2022-06-23 thomas case 'u':
7935 70f17a53 2022-06-13 thomas nscroll /= 2;
7936 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7937 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7938 6458efa5 2020-11-24 stsp case CTRL('b'):
7939 1c5e5faa 2022-06-23 thomas case 'b':
7940 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7941 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7942 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7943 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7944 07b0611c 2022-06-23 thomas view->count = 0;
7945 6458efa5 2020-11-24 stsp break;
7946 6458efa5 2020-11-24 stsp case 'j':
7947 6458efa5 2020-11-24 stsp case KEY_DOWN:
7948 f7140bf5 2021-10-17 thomas case CTRL('n'):
7949 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7950 6458efa5 2020-11-24 stsp s->selected++;
7951 6458efa5 2020-11-24 stsp break;
7952 6458efa5 2020-11-24 stsp }
7953 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7954 6458efa5 2020-11-24 stsp /* can't scroll any further */
7955 07b0611c 2022-06-23 thomas view->count = 0;
7956 6458efa5 2020-11-24 stsp break;
7957 07b0611c 2022-06-23 thomas }
7958 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7959 6458efa5 2020-11-24 stsp break;
7960 70f17a53 2022-06-13 thomas case CTRL('d'):
7961 23427b14 2022-06-23 thomas case 'd':
7962 70f17a53 2022-06-13 thomas nscroll /= 2;
7963 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7964 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7965 6458efa5 2020-11-24 stsp case CTRL('f'):
7966 1c5e5faa 2022-06-23 thomas case 'f':
7967 4c2d69cb 2022-06-23 thomas case ' ':
7968 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7969 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7970 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7971 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7972 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7973 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7974 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7975 07b0611c 2022-06-23 thomas view->count = 0;
7976 6458efa5 2020-11-24 stsp break;
7977 6458efa5 2020-11-24 stsp }
7978 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7979 6458efa5 2020-11-24 stsp break;
7980 6458efa5 2020-11-24 stsp case CTRL('l'):
7981 07b0611c 2022-06-23 thomas view->count = 0;
7982 8924d611 2020-12-26 stsp tog_free_refs();
7983 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7984 8924d611 2020-12-26 stsp if (err)
7985 8924d611 2020-12-26 stsp break;
7986 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7987 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7988 6458efa5 2020-11-24 stsp break;
7989 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7990 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7991 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7992 6458efa5 2020-11-24 stsp break;
7993 6458efa5 2020-11-24 stsp default:
7994 07b0611c 2022-06-23 thomas view->count = 0;
7995 6458efa5 2020-11-24 stsp break;
7996 6458efa5 2020-11-24 stsp }
7997 6458efa5 2020-11-24 stsp
7998 6458efa5 2020-11-24 stsp return err;
7999 6458efa5 2020-11-24 stsp }
8000 6458efa5 2020-11-24 stsp
8001 6458efa5 2020-11-24 stsp __dead static void
8002 6458efa5 2020-11-24 stsp usage_ref(void)
8003 6458efa5 2020-11-24 stsp {
8004 6458efa5 2020-11-24 stsp endwin();
8005 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8006 6458efa5 2020-11-24 stsp getprogname());
8007 6458efa5 2020-11-24 stsp exit(1);
8008 6458efa5 2020-11-24 stsp }
8009 6458efa5 2020-11-24 stsp
8010 6458efa5 2020-11-24 stsp static const struct got_error *
8011 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8012 6458efa5 2020-11-24 stsp {
8013 6458efa5 2020-11-24 stsp const struct got_error *error;
8014 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8015 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8016 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8017 6458efa5 2020-11-24 stsp int ch;
8018 6458efa5 2020-11-24 stsp struct tog_view *view;
8019 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8020 6458efa5 2020-11-24 stsp
8021 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8022 6458efa5 2020-11-24 stsp switch (ch) {
8023 6458efa5 2020-11-24 stsp case 'r':
8024 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8025 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8026 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8027 6458efa5 2020-11-24 stsp optarg);
8028 6458efa5 2020-11-24 stsp break;
8029 6458efa5 2020-11-24 stsp default:
8030 e99e2d15 2020-11-24 naddy usage_ref();
8031 6458efa5 2020-11-24 stsp /* NOTREACHED */
8032 6458efa5 2020-11-24 stsp }
8033 6458efa5 2020-11-24 stsp }
8034 6458efa5 2020-11-24 stsp
8035 6458efa5 2020-11-24 stsp argc -= optind;
8036 6458efa5 2020-11-24 stsp argv += optind;
8037 6458efa5 2020-11-24 stsp
8038 6458efa5 2020-11-24 stsp if (argc > 1)
8039 e99e2d15 2020-11-24 naddy usage_ref();
8040 7cd52833 2022-06-23 thomas
8041 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8042 7cd52833 2022-06-23 thomas if (error != NULL)
8043 7cd52833 2022-06-23 thomas goto done;
8044 6458efa5 2020-11-24 stsp
8045 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8046 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8047 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8048 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8049 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8050 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8051 c156c7a4 2020-12-18 stsp goto done;
8052 6458efa5 2020-11-24 stsp if (worktree)
8053 6458efa5 2020-11-24 stsp repo_path =
8054 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8055 6458efa5 2020-11-24 stsp else
8056 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8057 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8058 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8059 c156c7a4 2020-12-18 stsp goto done;
8060 c156c7a4 2020-12-18 stsp }
8061 6458efa5 2020-11-24 stsp }
8062 6458efa5 2020-11-24 stsp
8063 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8064 6458efa5 2020-11-24 stsp if (error != NULL)
8065 6458efa5 2020-11-24 stsp goto done;
8066 6458efa5 2020-11-24 stsp
8067 6458efa5 2020-11-24 stsp init_curses();
8068 6458efa5 2020-11-24 stsp
8069 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8070 51a10b52 2020-12-26 stsp if (error)
8071 51a10b52 2020-12-26 stsp goto done;
8072 51a10b52 2020-12-26 stsp
8073 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8074 6458efa5 2020-11-24 stsp if (error)
8075 6458efa5 2020-11-24 stsp goto done;
8076 6458efa5 2020-11-24 stsp
8077 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8078 6458efa5 2020-11-24 stsp if (view == NULL) {
8079 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8080 6458efa5 2020-11-24 stsp goto done;
8081 6458efa5 2020-11-24 stsp }
8082 6458efa5 2020-11-24 stsp
8083 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8084 6458efa5 2020-11-24 stsp if (error)
8085 6458efa5 2020-11-24 stsp goto done;
8086 6458efa5 2020-11-24 stsp
8087 6458efa5 2020-11-24 stsp if (worktree) {
8088 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8089 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8090 6458efa5 2020-11-24 stsp worktree = NULL;
8091 6458efa5 2020-11-24 stsp }
8092 6458efa5 2020-11-24 stsp error = view_loop(view);
8093 6458efa5 2020-11-24 stsp done:
8094 6458efa5 2020-11-24 stsp free(repo_path);
8095 6458efa5 2020-11-24 stsp free(cwd);
8096 1d0f4054 2021-06-17 stsp if (repo) {
8097 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8098 1d0f4054 2021-06-17 stsp if (close_err)
8099 1d0f4054 2021-06-17 stsp error = close_err;
8100 1d0f4054 2021-06-17 stsp }
8101 7cd52833 2022-06-23 thomas if (pack_fds) {
8102 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8103 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8104 7cd52833 2022-06-23 thomas if (error == NULL)
8105 7cd52833 2022-06-23 thomas error = pack_err;
8106 7cd52833 2022-06-23 thomas }
8107 51a10b52 2020-12-26 stsp tog_free_refs();
8108 6458efa5 2020-11-24 stsp return error;
8109 6458efa5 2020-11-24 stsp }
8110 6458efa5 2020-11-24 stsp
8111 2a31b33b 2022-07-23 thomas static const struct got_error *
8112 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8113 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8114 2a31b33b 2022-07-23 thomas {
8115 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8116 2a31b33b 2022-07-23 thomas
8117 2a31b33b 2022-07-23 thomas *new_view = NULL;
8118 2a31b33b 2022-07-23 thomas
8119 2a31b33b 2022-07-23 thomas switch (request) {
8120 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8121 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8122 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8123 2a31b33b 2022-07-23 thomas
8124 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8125 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8126 2a31b33b 2022-07-23 thomas view, s->repo);
8127 2a31b33b 2022-07-23 thomas } else
8128 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8129 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8130 2a31b33b 2022-07-23 thomas break;
8131 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8132 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8133 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8134 2a31b33b 2022-07-23 thomas
8135 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8136 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8137 2a31b33b 2022-07-23 thomas s->repo);
8138 2a31b33b 2022-07-23 thomas } else
8139 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8140 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8141 2a31b33b 2022-07-23 thomas break;
8142 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8143 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8144 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8145 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8146 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8147 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8148 2a31b33b 2022-07-23 thomas &view->state.tree);
8149 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8150 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8151 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8152 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8153 2a31b33b 2022-07-23 thomas else
8154 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8155 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8156 2a31b33b 2022-07-23 thomas break;
8157 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8158 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8159 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8160 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8161 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8162 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8163 2a31b33b 2022-07-23 thomas view->state.log.repo);
8164 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8165 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8166 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8167 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8168 2a31b33b 2022-07-23 thomas else
8169 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8170 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8171 2a31b33b 2022-07-23 thomas break;
8172 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
8173 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8174 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
8175 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
8176 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8177 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
8178 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8179 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
8180 2a31b33b 2022-07-23 thomas else
8181 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
8182 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8183 2a31b33b 2022-07-23 thomas if (err)
8184 2a31b33b 2022-07-23 thomas view_close(*new_view);
8185 2a31b33b 2022-07-23 thomas break;
8186 2a31b33b 2022-07-23 thomas default:
8187 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8188 2a31b33b 2022-07-23 thomas }
8189 2a31b33b 2022-07-23 thomas
8190 2a31b33b 2022-07-23 thomas return err;
8191 2a31b33b 2022-07-23 thomas }
8192 2a31b33b 2022-07-23 thomas
8193 a5d43cac 2022-07-01 thomas /*
8194 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8195 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8196 a5d43cac 2022-07-01 thomas */
8197 6458efa5 2020-11-24 stsp static void
8198 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8199 a5d43cac 2022-07-01 thomas {
8200 a5d43cac 2022-07-01 thomas switch (view->type) {
8201 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8202 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8203 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8204 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8205 a5d43cac 2022-07-01 thomas 1);
8206 a5d43cac 2022-07-01 thomas break;
8207 a5d43cac 2022-07-01 thomas }
8208 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8209 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8210 a5d43cac 2022-07-01 thomas else
8211 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8212 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8213 a5d43cac 2022-07-01 thomas break;
8214 a5d43cac 2022-07-01 thomas }
8215 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8216 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8217 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8218 a5d43cac 2022-07-01 thomas break;
8219 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8220 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8221 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8222 a5d43cac 2022-07-01 thomas break;
8223 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8224 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8225 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8226 a5d43cac 2022-07-01 thomas break;
8227 a5d43cac 2022-07-01 thomas default:
8228 a5d43cac 2022-07-01 thomas break;
8229 a5d43cac 2022-07-01 thomas }
8230 a5d43cac 2022-07-01 thomas
8231 a5d43cac 2022-07-01 thomas view->offset = 0;
8232 a5d43cac 2022-07-01 thomas }
8233 a5d43cac 2022-07-01 thomas
8234 a5d43cac 2022-07-01 thomas /*
8235 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8236 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8237 a5d43cac 2022-07-01 thomas */
8238 a5d43cac 2022-07-01 thomas static const struct got_error *
8239 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8240 a5d43cac 2022-07-01 thomas {
8241 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8242 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8243 a5d43cac 2022-07-01 thomas int *selected = NULL;
8244 a5d43cac 2022-07-01 thomas int header, offset;
8245 a5d43cac 2022-07-01 thomas
8246 a5d43cac 2022-07-01 thomas switch (view->type) {
8247 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8248 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8249 a5d43cac 2022-07-01 thomas header = 3;
8250 a5d43cac 2022-07-01 thomas scrolld = NULL;
8251 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8252 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8253 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8254 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8255 a5d43cac 2022-07-01 thomas view->offset = offset;
8256 a5d43cac 2022-07-01 thomas }
8257 a5d43cac 2022-07-01 thomas break;
8258 a5d43cac 2022-07-01 thomas }
8259 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8260 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8261 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8262 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8263 a5d43cac 2022-07-01 thomas selected = &s->selected;
8264 a5d43cac 2022-07-01 thomas break;
8265 a5d43cac 2022-07-01 thomas }
8266 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8267 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8268 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8269 a5d43cac 2022-07-01 thomas header = 3;
8270 a5d43cac 2022-07-01 thomas selected = &s->selected;
8271 a5d43cac 2022-07-01 thomas break;
8272 a5d43cac 2022-07-01 thomas }
8273 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8274 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8275 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8276 a5d43cac 2022-07-01 thomas header = 5;
8277 a5d43cac 2022-07-01 thomas selected = &s->selected;
8278 a5d43cac 2022-07-01 thomas break;
8279 a5d43cac 2022-07-01 thomas }
8280 a5d43cac 2022-07-01 thomas default:
8281 a5d43cac 2022-07-01 thomas selected = NULL;
8282 a5d43cac 2022-07-01 thomas scrolld = NULL;
8283 a5d43cac 2022-07-01 thomas header = 0;
8284 a5d43cac 2022-07-01 thomas break;
8285 a5d43cac 2022-07-01 thomas }
8286 a5d43cac 2022-07-01 thomas
8287 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8288 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8289 a5d43cac 2022-07-01 thomas view->offset = offset;
8290 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8291 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8292 a5d43cac 2022-07-01 thomas *selected -= offset;
8293 a5d43cac 2022-07-01 thomas }
8294 a5d43cac 2022-07-01 thomas }
8295 a5d43cac 2022-07-01 thomas
8296 a5d43cac 2022-07-01 thomas return err;
8297 a5d43cac 2022-07-01 thomas }
8298 a5d43cac 2022-07-01 thomas
8299 a5d43cac 2022-07-01 thomas static void
8300 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8301 ce5b7c56 2019-07-09 stsp {
8302 6059809a 2020-12-17 stsp size_t i;
8303 9f7d7167 2018-04-29 stsp
8304 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8305 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8306 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8307 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8308 ce5b7c56 2019-07-09 stsp }
8309 6879ba42 2020-10-01 naddy fputc('\n', fp);
8310 ce5b7c56 2019-07-09 stsp }
8311 ce5b7c56 2019-07-09 stsp
8312 4ed7e80c 2018-05-20 stsp __dead static void
8313 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8314 9f7d7167 2018-04-29 stsp {
8315 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8316 6879ba42 2020-10-01 naddy
8317 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8318 6879ba42 2020-10-01 naddy getprogname());
8319 6879ba42 2020-10-01 naddy if (hflag) {
8320 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8321 6879ba42 2020-10-01 naddy list_commands(fp);
8322 ee85c5e8 2020-02-29 stsp }
8323 6879ba42 2020-10-01 naddy exit(status);
8324 9f7d7167 2018-04-29 stsp }
8325 9f7d7167 2018-04-29 stsp
8326 c2301be8 2018-04-30 stsp static char **
8327 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8328 c2301be8 2018-04-30 stsp {
8329 ee85c5e8 2020-02-29 stsp va_list ap;
8330 c2301be8 2018-04-30 stsp char **argv;
8331 ee85c5e8 2020-02-29 stsp int i;
8332 c2301be8 2018-04-30 stsp
8333 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8334 ee85c5e8 2020-02-29 stsp
8335 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8336 c2301be8 2018-04-30 stsp if (argv == NULL)
8337 c2301be8 2018-04-30 stsp err(1, "calloc");
8338 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8339 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8340 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8341 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8342 c2301be8 2018-04-30 stsp }
8343 c2301be8 2018-04-30 stsp
8344 ee85c5e8 2020-02-29 stsp va_end(ap);
8345 c2301be8 2018-04-30 stsp return argv;
8346 ee85c5e8 2020-02-29 stsp }
8347 ee85c5e8 2020-02-29 stsp
8348 ee85c5e8 2020-02-29 stsp /*
8349 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8350 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8351 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8352 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8353 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8354 ee85c5e8 2020-02-29 stsp */
8355 ee85c5e8 2020-02-29 stsp static const struct got_error *
8356 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8357 ee85c5e8 2020-02-29 stsp {
8358 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8359 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8360 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8361 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8362 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8363 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8364 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8365 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8366 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8367 84de9106 2020-12-26 stsp
8368 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8369 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8370 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8371 ee85c5e8 2020-02-29 stsp
8372 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8373 7cd52833 2022-06-23 thomas if (error != NULL)
8374 7cd52833 2022-06-23 thomas goto done;
8375 7cd52833 2022-06-23 thomas
8376 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8377 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8378 ee85c5e8 2020-02-29 stsp goto done;
8379 ee85c5e8 2020-02-29 stsp
8380 ee85c5e8 2020-02-29 stsp if (worktree)
8381 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8382 ee85c5e8 2020-02-29 stsp else
8383 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8384 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8385 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8386 ee85c5e8 2020-02-29 stsp goto done;
8387 ee85c5e8 2020-02-29 stsp }
8388 ee85c5e8 2020-02-29 stsp
8389 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8390 ee85c5e8 2020-02-29 stsp if (error != NULL)
8391 ee85c5e8 2020-02-29 stsp goto done;
8392 ee85c5e8 2020-02-29 stsp
8393 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8394 ee85c5e8 2020-02-29 stsp repo, worktree);
8395 ee85c5e8 2020-02-29 stsp if (error)
8396 ee85c5e8 2020-02-29 stsp goto done;
8397 ee85c5e8 2020-02-29 stsp
8398 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8399 84de9106 2020-12-26 stsp if (error)
8400 84de9106 2020-12-26 stsp goto done;
8401 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8402 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8403 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8404 ee85c5e8 2020-02-29 stsp if (error)
8405 ee85c5e8 2020-02-29 stsp goto done;
8406 ee85c5e8 2020-02-29 stsp
8407 ee85c5e8 2020-02-29 stsp if (worktree) {
8408 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8409 ee85c5e8 2020-02-29 stsp worktree = NULL;
8410 ee85c5e8 2020-02-29 stsp }
8411 ee85c5e8 2020-02-29 stsp
8412 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8413 945f9229 2022-04-16 thomas if (error)
8414 945f9229 2022-04-16 thomas goto done;
8415 945f9229 2022-04-16 thomas
8416 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8417 ee85c5e8 2020-02-29 stsp if (error) {
8418 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8419 ee85c5e8 2020-02-29 stsp goto done;
8420 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8421 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8422 6879ba42 2020-10-01 naddy usage(1, 1);
8423 ee85c5e8 2020-02-29 stsp /* not reached */
8424 ee85c5e8 2020-02-29 stsp }
8425 ee85c5e8 2020-02-29 stsp
8426 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8427 ee85c5e8 2020-02-29 stsp if (error)
8428 ee85c5e8 2020-02-29 stsp goto done;
8429 ee85c5e8 2020-02-29 stsp
8430 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8431 ee85c5e8 2020-02-29 stsp argc = 4;
8432 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8433 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8434 ee85c5e8 2020-02-29 stsp done:
8435 1d0f4054 2021-06-17 stsp if (repo) {
8436 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8437 1d0f4054 2021-06-17 stsp if (error == NULL)
8438 1d0f4054 2021-06-17 stsp error = close_err;
8439 1d0f4054 2021-06-17 stsp }
8440 945f9229 2022-04-16 thomas if (commit)
8441 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8442 ee85c5e8 2020-02-29 stsp if (worktree)
8443 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8444 7cd52833 2022-06-23 thomas if (pack_fds) {
8445 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8446 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8447 7cd52833 2022-06-23 thomas if (error == NULL)
8448 7cd52833 2022-06-23 thomas error = pack_err;
8449 7cd52833 2022-06-23 thomas }
8450 ee85c5e8 2020-02-29 stsp free(id);
8451 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8452 ee85c5e8 2020-02-29 stsp free(commit_id);
8453 ee85c5e8 2020-02-29 stsp free(cwd);
8454 ee85c5e8 2020-02-29 stsp free(repo_path);
8455 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8456 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8457 ee85c5e8 2020-02-29 stsp int i;
8458 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8459 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8460 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8461 ee85c5e8 2020-02-29 stsp }
8462 87670572 2020-12-26 naddy tog_free_refs();
8463 ee85c5e8 2020-02-29 stsp return error;
8464 c2301be8 2018-04-30 stsp }
8465 c2301be8 2018-04-30 stsp
8466 9f7d7167 2018-04-29 stsp int
8467 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8468 9f7d7167 2018-04-29 stsp {
8469 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8470 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8471 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8472 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8473 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8474 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8475 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8476 83cd27f8 2020-01-13 stsp };
8477 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8478 9f7d7167 2018-04-29 stsp
8479 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8480 9f7d7167 2018-04-29 stsp
8481 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8482 9f7d7167 2018-04-29 stsp switch (ch) {
8483 9f7d7167 2018-04-29 stsp case 'h':
8484 9f7d7167 2018-04-29 stsp hflag = 1;
8485 9f7d7167 2018-04-29 stsp break;
8486 53ccebc2 2019-07-30 stsp case 'V':
8487 53ccebc2 2019-07-30 stsp Vflag = 1;
8488 53ccebc2 2019-07-30 stsp break;
8489 9f7d7167 2018-04-29 stsp default:
8490 6879ba42 2020-10-01 naddy usage(hflag, 1);
8491 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8492 9f7d7167 2018-04-29 stsp }
8493 9f7d7167 2018-04-29 stsp }
8494 9f7d7167 2018-04-29 stsp
8495 9f7d7167 2018-04-29 stsp argc -= optind;
8496 9f7d7167 2018-04-29 stsp argv += optind;
8497 9814e6a3 2020-09-27 naddy optind = 1;
8498 c2301be8 2018-04-30 stsp optreset = 1;
8499 9f7d7167 2018-04-29 stsp
8500 53ccebc2 2019-07-30 stsp if (Vflag) {
8501 53ccebc2 2019-07-30 stsp got_version_print_str();
8502 6879ba42 2020-10-01 naddy return 0;
8503 53ccebc2 2019-07-30 stsp }
8504 4010e238 2020-12-04 stsp
8505 4010e238 2020-12-04 stsp #ifndef PROFILE
8506 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8507 4010e238 2020-12-04 stsp NULL) == -1)
8508 4010e238 2020-12-04 stsp err(1, "pledge");
8509 4010e238 2020-12-04 stsp #endif
8510 53ccebc2 2019-07-30 stsp
8511 c2301be8 2018-04-30 stsp if (argc == 0) {
8512 f29d3e89 2018-06-23 stsp if (hflag)
8513 6879ba42 2020-10-01 naddy usage(hflag, 0);
8514 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8515 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8516 c2301be8 2018-04-30 stsp argc = 1;
8517 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8518 c2301be8 2018-04-30 stsp } else {
8519 6059809a 2020-12-17 stsp size_t i;
8520 9f7d7167 2018-04-29 stsp
8521 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8522 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8523 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8524 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8525 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8526 9f7d7167 2018-04-29 stsp break;
8527 9f7d7167 2018-04-29 stsp }
8528 9f7d7167 2018-04-29 stsp }
8529 ee85c5e8 2020-02-29 stsp }
8530 3642c4c6 2019-07-09 stsp
8531 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8532 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8533 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8534 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8535 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8536 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8537 adf4c9e0 2022-07-03 thomas }
8538 adf4c9e0 2022-07-03 thomas
8539 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8540 ee85c5e8 2020-02-29 stsp if (argc != 1)
8541 6879ba42 2020-10-01 naddy usage(0, 1);
8542 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8543 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8544 ee85c5e8 2020-02-29 stsp } else {
8545 ee85c5e8 2020-02-29 stsp if (hflag)
8546 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8547 ee85c5e8 2020-02-29 stsp else
8548 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8549 9f7d7167 2018-04-29 stsp }
8550 9f7d7167 2018-04-29 stsp
8551 9f7d7167 2018-04-29 stsp endwin();
8552 b46c1e04 2020-09-20 naddy putchar('\n');
8553 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8554 a2f4a359 2020-02-28 stsp int i;
8555 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8556 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8557 a2f4a359 2020-02-28 stsp free(cmd_argv);
8558 a2f4a359 2020-02-28 stsp }
8559 a2f4a359 2020-02-28 stsp
8560 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8561 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8562 9f7d7167 2018-04-29 stsp return 0;
8563 9f7d7167 2018-04-29 stsp }