Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 def2f970 2021-09-28 thomas #endif
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
44 9f7d7167 2018-04-29 stsp
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
46 dd038bc6 2021-09-21 thomas.ad
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 9f7d7167 2018-04-29 stsp
62 881b2d3e 2018-04-30 stsp #ifndef MIN
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 881b2d3e 2018-04-30 stsp #endif
65 881b2d3e 2018-04-30 stsp
66 2bd27830 2018-10-22 stsp #ifndef MAX
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #endif
69 2bd27830 2018-10-22 stsp
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
72 acf52a76 2021-09-21 thomas.ad #endif
73 2bd27830 2018-10-22 stsp
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 9f7d7167 2018-04-29 stsp #endif
77 9f7d7167 2018-04-29 stsp
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
82 9f7d7167 2018-04-29 stsp };
83 9f7d7167 2018-04-29 stsp
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 9f7d7167 2018-04-29 stsp
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 9f7d7167 2018-04-29 stsp
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
103 9f7d7167 2018-04-29 stsp };
104 9f7d7167 2018-04-29 stsp
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 d6b05b5b 2018-08-04 stsp };
112 c3e9aa98 2019-05-13 jcs
113 a5d43cac 2022-07-01 thomas enum tog_view_mode {
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
117 a5d43cac 2022-07-01 thomas };
118 a5d43cac 2022-07-01 thomas
119 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 a5d43cac 2022-07-01 thomas
121 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
122 d6b05b5b 2018-08-04 stsp
123 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
124 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
125 ba4f502b 2018-08-04 stsp struct got_object_id *id;
126 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
127 1a76625f 2018-10-22 stsp int idx;
128 ba4f502b 2018-08-04 stsp };
129 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 ba4f502b 2018-08-04 stsp struct commit_queue {
131 ba4f502b 2018-08-04 stsp int ncommits;
132 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
133 6d17833f 2019-11-08 stsp };
134 6d17833f 2019-11-08 stsp
135 f26dddb7 2019-11-08 stsp struct tog_color {
136 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
137 6d17833f 2019-11-08 stsp regex_t regex;
138 6d17833f 2019-11-08 stsp short colorpair;
139 15a087fe 2019-02-21 stsp };
140 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
141 11b20872 2019-11-08 stsp
142 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
144 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 2183bbf6 2022-01-23 thomas
146 2183bbf6 2022-01-23 thomas static const struct got_error *
147 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 2183bbf6 2022-01-23 thomas struct got_reference* re2)
149 2183bbf6 2022-01-23 thomas {
150 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
151 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
152 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
153 2183bbf6 2022-01-23 thomas
154 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
155 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
158 2183bbf6 2022-01-23 thomas *cmp = -1;
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
161 2183bbf6 2022-01-23 thomas *cmp = 1;
162 2183bbf6 2022-01-23 thomas return NULL;
163 2183bbf6 2022-01-23 thomas }
164 2183bbf6 2022-01-23 thomas
165 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 2183bbf6 2022-01-23 thomas return NULL;
167 2183bbf6 2022-01-23 thomas }
168 51a10b52 2020-12-26 stsp
169 11b20872 2019-11-08 stsp static const struct got_error *
170 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
171 51a10b52 2020-12-26 stsp {
172 51a10b52 2020-12-26 stsp const struct got_error *err;
173 51a10b52 2020-12-26 stsp
174 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 3bfadbd4 2021-11-20 thomas repo);
177 51a10b52 2020-12-26 stsp if (err)
178 51a10b52 2020-12-26 stsp return err;
179 51a10b52 2020-12-26 stsp
180 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 51a10b52 2020-12-26 stsp repo);
182 51a10b52 2020-12-26 stsp }
183 51a10b52 2020-12-26 stsp
184 51a10b52 2020-12-26 stsp static void
185 51a10b52 2020-12-26 stsp tog_free_refs(void)
186 51a10b52 2020-12-26 stsp {
187 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
188 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
189 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
190 51a10b52 2020-12-26 stsp }
191 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
192 51a10b52 2020-12-26 stsp }
193 51a10b52 2020-12-26 stsp
194 51a10b52 2020-12-26 stsp static const struct got_error *
195 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
196 11b20872 2019-11-08 stsp int idx, short color)
197 11b20872 2019-11-08 stsp {
198 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
199 11b20872 2019-11-08 stsp struct tog_color *tc;
200 11b20872 2019-11-08 stsp int regerr = 0;
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
203 11b20872 2019-11-08 stsp return NULL;
204 11b20872 2019-11-08 stsp
205 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
206 11b20872 2019-11-08 stsp
207 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
208 11b20872 2019-11-08 stsp if (tc == NULL)
209 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
210 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
211 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 11b20872 2019-11-08 stsp if (regerr) {
213 11b20872 2019-11-08 stsp static char regerr_msg[512];
214 11b20872 2019-11-08 stsp static char err_msg[512];
215 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
216 11b20872 2019-11-08 stsp sizeof(regerr_msg));
217 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 11b20872 2019-11-08 stsp regerr_msg);
219 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 11b20872 2019-11-08 stsp free(tc);
221 11b20872 2019-11-08 stsp return err;
222 11b20872 2019-11-08 stsp }
223 11b20872 2019-11-08 stsp tc->colorpair = idx;
224 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
225 11b20872 2019-11-08 stsp return NULL;
226 11b20872 2019-11-08 stsp }
227 11b20872 2019-11-08 stsp
228 11b20872 2019-11-08 stsp static void
229 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
230 11b20872 2019-11-08 stsp {
231 11b20872 2019-11-08 stsp struct tog_color *tc;
232 11b20872 2019-11-08 stsp
233 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
234 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
235 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
236 11b20872 2019-11-08 stsp regfree(&tc->regex);
237 11b20872 2019-11-08 stsp free(tc);
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 ef20f542 2022-06-26 thomas static struct tog_color *
242 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
247 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
248 11b20872 2019-11-08 stsp return tc;
249 11b20872 2019-11-08 stsp }
250 11b20872 2019-11-08 stsp
251 11b20872 2019-11-08 stsp return NULL;
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 11b20872 2019-11-08 stsp static int
255 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_CYAN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 11b20872 2019-11-08 stsp return COLOR_YELLOW;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 11b20872 2019-11-08 stsp return COLOR_CYAN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 11b20872 2019-11-08 stsp return COLOR_YELLOW;
279 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 6458efa5 2020-11-24 stsp return COLOR_GREEN;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
285 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
287 11b20872 2019-11-08 stsp
288 11b20872 2019-11-08 stsp return -1;
289 11b20872 2019-11-08 stsp }
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp static int
292 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
293 11b20872 2019-11-08 stsp {
294 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
295 11b20872 2019-11-08 stsp
296 11b20872 2019-11-08 stsp if (val == NULL)
297 11b20872 2019-11-08 stsp return default_color_value(envvar);
298 15a087fe 2019-02-21 stsp
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
300 11b20872 2019-11-08 stsp return COLOR_BLACK;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
302 11b20872 2019-11-08 stsp return COLOR_RED;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
304 11b20872 2019-11-08 stsp return COLOR_GREEN;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
306 11b20872 2019-11-08 stsp return COLOR_YELLOW;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLUE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
310 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
312 11b20872 2019-11-08 stsp return COLOR_CYAN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
314 11b20872 2019-11-08 stsp return COLOR_WHITE;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
316 11b20872 2019-11-08 stsp return -1;
317 11b20872 2019-11-08 stsp
318 11b20872 2019-11-08 stsp return default_color_value(envvar);
319 11b20872 2019-11-08 stsp }
320 11b20872 2019-11-08 stsp
321 11b20872 2019-11-08 stsp
322 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
323 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
324 3dbaef42 2020-11-24 stsp const char *label1, *label2;
325 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
326 19a6a6b5 2022-07-01 thomas int fd1, fd2;
327 15a087fe 2019-02-21 stsp int first_displayed_line;
328 15a087fe 2019-02-21 stsp int last_displayed_line;
329 15a087fe 2019-02-21 stsp int eof;
330 15a087fe 2019-02-21 stsp int diff_context;
331 3dbaef42 2020-11-24 stsp int ignore_whitespace;
332 64453f7e 2020-11-21 stsp int force_text_diff;
333 15a087fe 2019-02-21 stsp struct got_repository *repo;
334 bddb1296 2019-11-08 stsp struct tog_colors colors;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey off_t *line_offsets;
337 f44b1f58 2020-02-02 tracey int matched_line;
338 f44b1f58 2020-02-02 tracey int selected_line;
339 15a087fe 2019-02-21 stsp
340 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
341 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
342 b01e7d3b 2018-08-04 stsp };
343 b01e7d3b 2018-08-04 stsp
344 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
346 1a76625f 2018-10-22 stsp
347 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
348 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
349 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
350 1a76625f 2018-10-22 stsp int commits_needed;
351 fb280deb 2021-08-30 stsp int load_all;
352 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
353 1a76625f 2018-10-22 stsp struct commit_queue *commits;
354 1a76625f 2018-10-22 stsp const char *in_repo_path;
355 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
356 1a76625f 2018-10-22 stsp struct got_repository *repo;
357 1af5eddf 2022-06-23 thomas int *pack_fds;
358 1a76625f 2018-10-22 stsp int log_complete;
359 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
361 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
362 13add988 2019-10-15 stsp int *searching;
363 13add988 2019-10-15 stsp int *search_next_done;
364 13add988 2019-10-15 stsp regex_t *regex;
365 1a76625f 2018-10-22 stsp };
366 1a76625f 2018-10-22 stsp
367 1a76625f 2018-10-22 stsp struct tog_log_view_state {
368 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
371 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
372 b01e7d3b 2018-08-04 stsp int selected;
373 b01e7d3b 2018-08-04 stsp char *in_repo_path;
374 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
375 b672a97a 2020-01-27 stsp int log_branches;
376 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
377 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
378 1a76625f 2018-10-22 stsp sig_atomic_t quit;
379 1a76625f 2018-10-22 stsp pthread_t thread;
380 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
381 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
382 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
383 11b20872 2019-11-08 stsp struct tog_colors colors;
384 f69c5a46 2022-07-19 thomas int use_committer;
385 ba4f502b 2018-08-04 stsp };
386 11b20872 2019-11-08 stsp
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
394 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
395 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
396 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
397 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
400 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
401 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
402 ba4f502b 2018-08-04 stsp
403 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
404 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
405 e9424729 2018-08-04 stsp int nlines;
406 e9424729 2018-08-04 stsp
407 e9424729 2018-08-04 stsp struct tog_view *view;
408 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
409 e9424729 2018-08-04 stsp int *quit;
410 e9424729 2018-08-04 stsp };
411 e9424729 2018-08-04 stsp
412 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
413 e9424729 2018-08-04 stsp const char *path;
414 e9424729 2018-08-04 stsp struct got_repository *repo;
415 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
416 e9424729 2018-08-04 stsp int *complete;
417 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
418 fc06ba56 2019-08-22 stsp void *cancel_arg;
419 e9424729 2018-08-04 stsp };
420 e9424729 2018-08-04 stsp
421 e9424729 2018-08-04 stsp struct tog_blame {
422 e9424729 2018-08-04 stsp FILE *f;
423 be659d10 2020-11-18 stsp off_t filesize;
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
425 6fcac457 2018-11-19 stsp int nlines;
426 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
427 e9424729 2018-08-04 stsp pthread_t thread;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
429 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
430 e9424729 2018-08-04 stsp const char *path;
431 7cd52833 2022-06-23 thomas int *pack_fds;
432 e9424729 2018-08-04 stsp };
433 e9424729 2018-08-04 stsp
434 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
435 7cbe629d 2018-08-04 stsp int first_displayed_line;
436 7cbe629d 2018-08-04 stsp int last_displayed_line;
437 7cbe629d 2018-08-04 stsp int selected_line;
438 4fc71f3b 2022-07-12 thomas int last_diffed_line;
439 7cbe629d 2018-08-04 stsp int blame_complete;
440 e5a0f69f 2018-08-18 stsp int eof;
441 e5a0f69f 2018-08-18 stsp int done;
442 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
443 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
444 e5a0f69f 2018-08-18 stsp char *path;
445 7cbe629d 2018-08-04 stsp struct got_repository *repo;
446 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
447 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
448 e9424729 2018-08-04 stsp struct tog_blame blame;
449 6c4c42e0 2019-06-24 stsp int matched_line;
450 11b20872 2019-11-08 stsp struct tog_colors colors;
451 ad80ab7b 2018-08-04 stsp };
452 ad80ab7b 2018-08-04 stsp
453 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
454 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
455 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
457 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
458 ad80ab7b 2018-08-04 stsp int selected;
459 ad80ab7b 2018-08-04 stsp };
460 ad80ab7b 2018-08-04 stsp
461 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
462 ad80ab7b 2018-08-04 stsp
463 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
464 ad80ab7b 2018-08-04 stsp char *tree_label;
465 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
466 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
467 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
469 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
470 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
471 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
472 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
473 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
474 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
475 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
476 6458efa5 2020-11-24 stsp struct tog_colors colors;
477 6458efa5 2020-11-24 stsp };
478 6458efa5 2020-11-24 stsp
479 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
480 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
481 6458efa5 2020-11-24 stsp struct got_reference *ref;
482 6458efa5 2020-11-24 stsp int idx;
483 6458efa5 2020-11-24 stsp };
484 6458efa5 2020-11-24 stsp
485 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
486 6458efa5 2020-11-24 stsp
487 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
488 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
490 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
491 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
492 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
493 6458efa5 2020-11-24 stsp struct got_repository *repo;
494 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
495 bddb1296 2019-11-08 stsp struct tog_colors colors;
496 7cbe629d 2018-08-04 stsp };
497 7cbe629d 2018-08-04 stsp
498 669b5ffa 2018-10-07 stsp /*
499 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
500 669b5ffa 2018-10-07 stsp *
501 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
502 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
503 669b5ffa 2018-10-07 stsp * there is enough screen estate.
504 669b5ffa 2018-10-07 stsp *
505 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
506 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
507 669b5ffa 2018-10-07 stsp *
508 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
509 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
510 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
511 669b5ffa 2018-10-07 stsp *
512 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
513 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
514 669b5ffa 2018-10-07 stsp */
515 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
516 669b5ffa 2018-10-07 stsp
517 cc3c9aac 2018-08-01 stsp struct tog_view {
518 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
519 26ed57b2 2018-05-19 stsp WINDOW *window;
520 26ed57b2 2018-05-19 stsp PANEL *panel;
521 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
522 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
523 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
524 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
525 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
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 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1258 ddbc4d37 2022-07-12 thomas } else {
1259 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1260 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1261 64486692 2022-07-07 thomas }
1262 f4e6231a 2022-07-22 thomas if (v->resize)
1263 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1264 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1265 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1266 64486692 2022-07-07 thomas
1267 64486692 2022-07-07 thomas return err;
1268 0cf4efb1 2018-09-29 stsp }
1269 6d0fee91 2018-08-01 stsp
1270 07b0611c 2022-06-23 thomas /*
1271 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1272 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1273 07b0611c 2022-06-23 thomas */
1274 07b0611c 2022-06-23 thomas static int
1275 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1276 07b0611c 2022-06-23 thomas {
1277 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1278 a5d43cac 2022-07-01 thomas int x, n = 0;
1279 07b0611c 2022-06-23 thomas
1280 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1281 a5d43cac 2022-07-01 thomas v = view->child;
1282 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1283 a5d43cac 2022-07-01 thomas v = view->parent;
1284 a5d43cac 2022-07-01 thomas
1285 07b0611c 2022-06-23 thomas view->count = 0;
1286 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1287 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1288 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1289 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1290 07b0611c 2022-06-23 thomas
1291 07b0611c 2022-06-23 thomas do {
1292 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1293 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1294 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1295 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1296 a5d43cac 2022-07-01 thomas }
1297 a5d43cac 2022-07-01 thomas
1298 07b0611c 2022-06-23 thomas /*
1299 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1300 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1301 07b0611c 2022-06-23 thomas */
1302 07b0611c 2022-06-23 thomas if (n >= 9999999)
1303 07b0611c 2022-06-23 thomas n = 9999999;
1304 07b0611c 2022-06-23 thomas else
1305 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1306 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1307 07b0611c 2022-06-23 thomas
1308 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1309 07b0611c 2022-06-23 thomas view->count = n;
1310 07b0611c 2022-06-23 thomas
1311 07b0611c 2022-06-23 thomas return c;
1312 07b0611c 2022-06-23 thomas }
1313 07b0611c 2022-06-23 thomas
1314 0cf4efb1 2018-09-29 stsp static const struct got_error *
1315 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1316 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1317 e5a0f69f 2018-08-18 stsp {
1318 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1319 669b5ffa 2018-10-07 stsp struct tog_view *v;
1320 1a76625f 2018-10-22 stsp int ch, errcode;
1321 e5a0f69f 2018-08-18 stsp
1322 e5a0f69f 2018-08-18 stsp *new = NULL;
1323 8f4ed634 2020-03-26 stsp
1324 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1325 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1326 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1327 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1328 07b0611c 2022-06-23 thomas view->count = 0;
1329 07b0611c 2022-06-23 thomas }
1330 e5a0f69f 2018-08-18 stsp
1331 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1332 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1333 82954512 2020-02-03 stsp if (errcode)
1334 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1335 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1336 3da8ef85 2021-09-21 stsp sched_yield();
1337 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1338 82954512 2020-02-03 stsp if (errcode)
1339 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1340 82954512 2020-02-03 stsp "pthread_mutex_lock");
1341 60493ae3 2019-06-20 stsp view->search_next(view);
1342 60493ae3 2019-06-20 stsp return NULL;
1343 60493ae3 2019-06-20 stsp }
1344 60493ae3 2019-06-20 stsp
1345 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1346 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1347 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1348 1a76625f 2018-10-22 stsp if (errcode)
1349 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1350 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1351 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1352 634cb454 2022-07-03 thomas cbreak();
1353 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1354 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1355 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1356 634cb454 2022-07-03 thomas view->count = 0;
1357 634cb454 2022-07-03 thomas else
1358 634cb454 2022-07-03 thomas ch = view->ch;
1359 634cb454 2022-07-03 thomas } else {
1360 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1361 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1362 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1363 07b0611c 2022-06-23 thomas }
1364 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1365 1a76625f 2018-10-22 stsp if (errcode)
1366 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1367 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1368 25791caa 2018-10-24 stsp
1369 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1370 25791caa 2018-10-24 stsp tog_resizeterm();
1371 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1372 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1373 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1374 25791caa 2018-10-24 stsp err = view_resize(v);
1375 25791caa 2018-10-24 stsp if (err)
1376 25791caa 2018-10-24 stsp return err;
1377 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1378 25791caa 2018-10-24 stsp if (err)
1379 25791caa 2018-10-24 stsp return err;
1380 cdfcfb03 2020-12-06 stsp if (v->child) {
1381 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1382 cdfcfb03 2020-12-06 stsp if (err)
1383 cdfcfb03 2020-12-06 stsp return err;
1384 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1385 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1386 cdfcfb03 2020-12-06 stsp if (err)
1387 cdfcfb03 2020-12-06 stsp return err;
1388 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1389 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1390 53d2bdd3 2022-07-10 thomas if (err)
1391 53d2bdd3 2022-07-10 thomas return err;
1392 53d2bdd3 2022-07-10 thomas }
1393 cdfcfb03 2020-12-06 stsp }
1394 25791caa 2018-10-24 stsp }
1395 25791caa 2018-10-24 stsp }
1396 25791caa 2018-10-24 stsp
1397 e5a0f69f 2018-08-18 stsp switch (ch) {
1398 1e37a5c2 2019-05-12 jcs case '\t':
1399 07b0611c 2022-06-23 thomas view->count = 0;
1400 1e37a5c2 2019-05-12 jcs if (view->child) {
1401 e78dc838 2020-12-04 stsp view->focussed = 0;
1402 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1403 e78dc838 2020-12-04 stsp view->focus_child = 1;
1404 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1405 e78dc838 2020-12-04 stsp view->focussed = 0;
1406 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1407 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1408 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1409 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1410 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1411 3a0139e8 2022-07-22 thomas 0);
1412 a5d43cac 2022-07-01 thomas if (err)
1413 a5d43cac 2022-07-01 thomas return err;
1414 a5d43cac 2022-07-01 thomas }
1415 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1416 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1417 a5d43cac 2022-07-01 thomas if (err)
1418 a5d43cac 2022-07-01 thomas return err;
1419 a5d43cac 2022-07-01 thomas }
1420 1e37a5c2 2019-05-12 jcs }
1421 1e37a5c2 2019-05-12 jcs break;
1422 1e37a5c2 2019-05-12 jcs case 'q':
1423 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1424 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1425 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1426 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1427 a5d43cac 2022-07-01 thomas if (err)
1428 a5d43cac 2022-07-01 thomas break;
1429 a5d43cac 2022-07-01 thomas }
1430 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1431 a5d43cac 2022-07-01 thomas }
1432 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1433 9970f7fc 2020-12-03 stsp view->dying = 1;
1434 1e37a5c2 2019-05-12 jcs break;
1435 1e37a5c2 2019-05-12 jcs case 'Q':
1436 1e37a5c2 2019-05-12 jcs *done = 1;
1437 1e37a5c2 2019-05-12 jcs break;
1438 1c5e5faa 2022-06-23 thomas case 'F':
1439 07b0611c 2022-06-23 thomas view->count = 0;
1440 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1441 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1442 1e37a5c2 2019-05-12 jcs break;
1443 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1444 e78dc838 2020-12-04 stsp view->focussed = 0;
1445 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1446 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1447 53d2bdd3 2022-07-10 thomas } else {
1448 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1449 53d2bdd3 2022-07-10 thomas if (!err)
1450 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1451 53d2bdd3 2022-07-10 thomas }
1452 1e37a5c2 2019-05-12 jcs if (err)
1453 1e37a5c2 2019-05-12 jcs break;
1454 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1455 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1456 1e37a5c2 2019-05-12 jcs } else {
1457 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1458 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1459 e78dc838 2020-12-04 stsp view->focussed = 1;
1460 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1461 1e37a5c2 2019-05-12 jcs } else {
1462 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1463 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1464 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1465 53d2bdd3 2022-07-10 thomas if (!err)
1466 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1467 669b5ffa 2018-10-07 stsp }
1468 1e37a5c2 2019-05-12 jcs if (err)
1469 1e37a5c2 2019-05-12 jcs break;
1470 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1471 a5d43cac 2022-07-01 thomas }
1472 a5d43cac 2022-07-01 thomas if (err)
1473 a5d43cac 2022-07-01 thomas break;
1474 3a0139e8 2022-07-22 thomas if (view->resize) {
1475 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1476 a5d43cac 2022-07-01 thomas if (err)
1477 a5d43cac 2022-07-01 thomas break;
1478 1e37a5c2 2019-05-12 jcs }
1479 a5d43cac 2022-07-01 thomas if (view->parent)
1480 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1481 a5d43cac 2022-07-01 thomas if (!err)
1482 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1483 1e37a5c2 2019-05-12 jcs break;
1484 64486692 2022-07-07 thomas case 'S':
1485 53d2bdd3 2022-07-10 thomas view->count = 0;
1486 64486692 2022-07-07 thomas err = switch_split(view);
1487 53d2bdd3 2022-07-10 thomas break;
1488 53d2bdd3 2022-07-10 thomas case '-':
1489 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1490 64486692 2022-07-07 thomas break;
1491 53d2bdd3 2022-07-10 thomas case '+':
1492 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1493 53d2bdd3 2022-07-10 thomas break;
1494 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1495 60493ae3 2019-06-20 stsp break;
1496 60493ae3 2019-06-20 stsp case '/':
1497 07b0611c 2022-06-23 thomas view->count = 0;
1498 60493ae3 2019-06-20 stsp if (view->search_start)
1499 2b49a8ae 2019-06-22 stsp view_search_start(view);
1500 60493ae3 2019-06-20 stsp else
1501 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1502 1e37a5c2 2019-05-12 jcs break;
1503 b1bf1435 2019-06-21 stsp case 'N':
1504 60493ae3 2019-06-20 stsp case 'n':
1505 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1506 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1507 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1508 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1509 60493ae3 2019-06-20 stsp view->search_next(view);
1510 60493ae3 2019-06-20 stsp } else
1511 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1512 adf4c9e0 2022-07-03 thomas break;
1513 adf4c9e0 2022-07-03 thomas case 'A':
1514 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1515 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1516 adf4c9e0 2022-07-03 thomas else
1517 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1518 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1519 adf4c9e0 2022-07-03 thomas if (v->reset) {
1520 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1521 adf4c9e0 2022-07-03 thomas if (err)
1522 adf4c9e0 2022-07-03 thomas return err;
1523 adf4c9e0 2022-07-03 thomas }
1524 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1525 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1526 adf4c9e0 2022-07-03 thomas if (err)
1527 adf4c9e0 2022-07-03 thomas return err;
1528 adf4c9e0 2022-07-03 thomas }
1529 adf4c9e0 2022-07-03 thomas }
1530 60493ae3 2019-06-20 stsp break;
1531 1e37a5c2 2019-05-12 jcs default:
1532 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1533 1e37a5c2 2019-05-12 jcs break;
1534 e5a0f69f 2018-08-18 stsp }
1535 e5a0f69f 2018-08-18 stsp
1536 e5a0f69f 2018-08-18 stsp return err;
1537 bcbd79e2 2018-08-19 stsp }
1538 bcbd79e2 2018-08-19 stsp
1539 ef20f542 2022-06-26 thomas static int
1540 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1541 a3404814 2018-09-02 stsp {
1542 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1543 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1544 669b5ffa 2018-10-07 stsp return 0;
1545 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1546 669b5ffa 2018-10-07 stsp return 0;
1547 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1548 a3404814 2018-09-02 stsp return 0;
1549 a3404814 2018-09-02 stsp
1550 669b5ffa 2018-10-07 stsp return view->focussed;
1551 a3404814 2018-09-02 stsp }
1552 a3404814 2018-09-02 stsp
1553 bcbd79e2 2018-08-19 stsp static const struct got_error *
1554 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1555 e5a0f69f 2018-08-18 stsp {
1556 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1557 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1558 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1559 64486692 2022-07-07 thomas char *mode;
1560 fd823528 2018-10-22 stsp int fast_refresh = 10;
1561 1a76625f 2018-10-22 stsp int done = 0, errcode;
1562 e5a0f69f 2018-08-18 stsp
1563 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1564 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1565 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1566 64486692 2022-07-07 thomas else
1567 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1568 64486692 2022-07-07 thomas
1569 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1570 1a76625f 2018-10-22 stsp if (errcode)
1571 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1572 1a76625f 2018-10-22 stsp
1573 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1574 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1575 e5a0f69f 2018-08-18 stsp
1576 1004088d 2018-09-29 stsp view->focussed = 1;
1577 878940b7 2018-09-29 stsp err = view->show(view);
1578 0cf4efb1 2018-09-29 stsp if (err)
1579 0cf4efb1 2018-09-29 stsp return err;
1580 0cf4efb1 2018-09-29 stsp update_panels();
1581 0cf4efb1 2018-09-29 stsp doupdate();
1582 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1583 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1584 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1585 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1586 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1587 fd823528 2018-10-22 stsp
1588 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1589 e5a0f69f 2018-08-18 stsp if (err)
1590 e5a0f69f 2018-08-18 stsp break;
1591 9970f7fc 2020-12-03 stsp if (view->dying) {
1592 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1593 669b5ffa 2018-10-07 stsp
1594 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1595 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1596 9970f7fc 2020-12-03 stsp entry);
1597 e78dc838 2020-12-04 stsp else if (view->parent)
1598 669b5ffa 2018-10-07 stsp prev = view->parent;
1599 669b5ffa 2018-10-07 stsp
1600 e78dc838 2020-12-04 stsp if (view->parent) {
1601 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1602 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1603 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1604 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1605 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1606 40236d76 2022-06-23 thomas if (err)
1607 40236d76 2022-06-23 thomas break;
1608 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1609 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1610 e78dc838 2020-12-04 stsp } else
1611 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1612 669b5ffa 2018-10-07 stsp
1613 9970f7fc 2020-12-03 stsp err = view_close(view);
1614 fb59748f 2020-12-05 stsp if (err)
1615 e5a0f69f 2018-08-18 stsp goto done;
1616 669b5ffa 2018-10-07 stsp
1617 e78dc838 2020-12-04 stsp view = NULL;
1618 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1619 e78dc838 2020-12-04 stsp if (v->focussed)
1620 e78dc838 2020-12-04 stsp break;
1621 0cf4efb1 2018-09-29 stsp }
1622 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1623 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1624 e78dc838 2020-12-04 stsp if (prev)
1625 e78dc838 2020-12-04 stsp view = prev;
1626 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1627 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1628 e78dc838 2020-12-04 stsp tog_view_list_head);
1629 e78dc838 2020-12-04 stsp }
1630 e78dc838 2020-12-04 stsp if (view) {
1631 e78dc838 2020-12-04 stsp if (view->focus_child) {
1632 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1633 e78dc838 2020-12-04 stsp view = view->child;
1634 e78dc838 2020-12-04 stsp } else
1635 e78dc838 2020-12-04 stsp view->focussed = 1;
1636 e78dc838 2020-12-04 stsp }
1637 e78dc838 2020-12-04 stsp }
1638 e5a0f69f 2018-08-18 stsp }
1639 bcbd79e2 2018-08-19 stsp if (new_view) {
1640 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1641 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1642 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1643 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1644 86c66b02 2018-10-18 stsp continue;
1645 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1646 86c66b02 2018-10-18 stsp err = view_close(v);
1647 86c66b02 2018-10-18 stsp if (err)
1648 86c66b02 2018-10-18 stsp goto done;
1649 86c66b02 2018-10-18 stsp break;
1650 86c66b02 2018-10-18 stsp }
1651 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1652 fed7eaa8 2018-10-24 stsp view = new_view;
1653 7cd52833 2022-06-23 thomas }
1654 669b5ffa 2018-10-07 stsp if (view) {
1655 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1656 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1657 e78dc838 2020-12-04 stsp view = view->child;
1658 e78dc838 2020-12-04 stsp } else {
1659 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1660 e78dc838 2020-12-04 stsp view = view->parent;
1661 1a76625f 2018-10-22 stsp }
1662 e78dc838 2020-12-04 stsp show_panel(view->panel);
1663 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1664 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1665 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1666 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1667 669b5ffa 2018-10-07 stsp if (err)
1668 1a76625f 2018-10-22 stsp goto done;
1669 669b5ffa 2018-10-07 stsp }
1670 669b5ffa 2018-10-07 stsp err = view->show(view);
1671 0cf4efb1 2018-09-29 stsp if (err)
1672 1a76625f 2018-10-22 stsp goto done;
1673 669b5ffa 2018-10-07 stsp if (view->child) {
1674 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1675 669b5ffa 2018-10-07 stsp if (err)
1676 1a76625f 2018-10-22 stsp goto done;
1677 669b5ffa 2018-10-07 stsp }
1678 1a76625f 2018-10-22 stsp update_panels();
1679 1a76625f 2018-10-22 stsp doupdate();
1680 0cf4efb1 2018-09-29 stsp }
1681 e5a0f69f 2018-08-18 stsp }
1682 e5a0f69f 2018-08-18 stsp done:
1683 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1684 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1685 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1686 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1687 f2d749db 2022-07-12 thomas close_err = view_close(view);
1688 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1689 f2d749db 2022-07-12 thomas err = close_err;
1690 e5a0f69f 2018-08-18 stsp }
1691 1a76625f 2018-10-22 stsp
1692 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1693 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1694 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1695 1a76625f 2018-10-22 stsp
1696 e5a0f69f 2018-08-18 stsp return err;
1697 ea5e7bb5 2018-08-01 stsp }
1698 ea5e7bb5 2018-08-01 stsp
1699 4ed7e80c 2018-05-20 stsp __dead static void
1700 9f7d7167 2018-04-29 stsp usage_log(void)
1701 9f7d7167 2018-04-29 stsp {
1702 80ddbec8 2018-04-29 stsp endwin();
1703 c70c5802 2018-08-01 stsp fprintf(stderr,
1704 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1705 9f7d7167 2018-04-29 stsp getprogname());
1706 9f7d7167 2018-04-29 stsp exit(1);
1707 80ddbec8 2018-04-29 stsp }
1708 80ddbec8 2018-04-29 stsp
1709 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1710 80ddbec8 2018-04-29 stsp static const struct got_error *
1711 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1712 963b370f 2018-05-20 stsp {
1713 00dfcb92 2018-06-11 stsp char *vis = NULL;
1714 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1715 963b370f 2018-05-20 stsp
1716 963b370f 2018-05-20 stsp *ws = NULL;
1717 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1718 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1719 00dfcb92 2018-06-11 stsp int vislen;
1720 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1721 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1722 00dfcb92 2018-06-11 stsp
1723 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1724 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1725 00dfcb92 2018-06-11 stsp if (err)
1726 00dfcb92 2018-06-11 stsp return err;
1727 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1728 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1729 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1730 a7f50699 2018-06-11 stsp goto done;
1731 a7f50699 2018-06-11 stsp }
1732 00dfcb92 2018-06-11 stsp }
1733 963b370f 2018-05-20 stsp
1734 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1735 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1736 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1737 a7f50699 2018-06-11 stsp goto done;
1738 a7f50699 2018-06-11 stsp }
1739 963b370f 2018-05-20 stsp
1740 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1741 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1742 a7f50699 2018-06-11 stsp done:
1743 00dfcb92 2018-06-11 stsp free(vis);
1744 963b370f 2018-05-20 stsp if (err) {
1745 963b370f 2018-05-20 stsp free(*ws);
1746 963b370f 2018-05-20 stsp *ws = NULL;
1747 963b370f 2018-05-20 stsp *wlen = 0;
1748 963b370f 2018-05-20 stsp }
1749 963b370f 2018-05-20 stsp return err;
1750 05171be4 2022-06-23 thomas }
1751 05171be4 2022-06-23 thomas
1752 05171be4 2022-06-23 thomas static const struct got_error *
1753 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1754 05171be4 2022-06-23 thomas {
1755 05171be4 2022-06-23 thomas char *dst;
1756 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1757 05171be4 2022-06-23 thomas
1758 05171be4 2022-06-23 thomas *ptr = NULL;
1759 05171be4 2022-06-23 thomas n = len = strlen(src);
1760 83316834 2022-06-23 thomas dst = malloc(n + 1);
1761 05171be4 2022-06-23 thomas if (dst == NULL)
1762 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1763 05171be4 2022-06-23 thomas
1764 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1765 05171be4 2022-06-23 thomas const char c = src[idx];
1766 05171be4 2022-06-23 thomas
1767 05171be4 2022-06-23 thomas if (c == '\t') {
1768 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1769 b235ad5d 2022-06-23 thomas char *p;
1770 b235ad5d 2022-06-23 thomas
1771 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1772 83316834 2022-06-23 thomas if (p == NULL) {
1773 83316834 2022-06-23 thomas free(dst);
1774 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1775 83316834 2022-06-23 thomas
1776 83316834 2022-06-23 thomas }
1777 83316834 2022-06-23 thomas dst = p;
1778 05171be4 2022-06-23 thomas n += nb;
1779 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1780 05171be4 2022-06-23 thomas sz += nb;
1781 05171be4 2022-06-23 thomas } else
1782 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1783 05171be4 2022-06-23 thomas ++idx;
1784 05171be4 2022-06-23 thomas }
1785 05171be4 2022-06-23 thomas
1786 05171be4 2022-06-23 thomas dst[sz] = '\0';
1787 05171be4 2022-06-23 thomas *ptr = dst;
1788 05171be4 2022-06-23 thomas return NULL;
1789 963b370f 2018-05-20 stsp }
1790 963b370f 2018-05-20 stsp
1791 8d208d34 2022-06-23 thomas /*
1792 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1793 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1794 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1795 8d208d34 2022-06-23 thomas * *rcol.
1796 f91a2b48 2022-06-23 thomas */
1797 8d208d34 2022-06-23 thomas static int
1798 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1799 8d208d34 2022-06-23 thomas {
1800 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1801 f91a2b48 2022-06-23 thomas
1802 8d208d34 2022-06-23 thomas if (n == 0) {
1803 8d208d34 2022-06-23 thomas *rcol = cols;
1804 8d208d34 2022-06-23 thomas return off;
1805 8d208d34 2022-06-23 thomas }
1806 f91a2b48 2022-06-23 thomas
1807 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1808 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1809 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1810 8d208d34 2022-06-23 thomas else
1811 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1812 f91a2b48 2022-06-23 thomas
1813 8d208d34 2022-06-23 thomas if (width == -1) {
1814 8d208d34 2022-06-23 thomas width = 1;
1815 8d208d34 2022-06-23 thomas wline[i] = L'.';
1816 f91a2b48 2022-06-23 thomas }
1817 f91a2b48 2022-06-23 thomas
1818 8d208d34 2022-06-23 thomas if (cols + width > n)
1819 8d208d34 2022-06-23 thomas break;
1820 8d208d34 2022-06-23 thomas cols += width;
1821 f91a2b48 2022-06-23 thomas }
1822 f91a2b48 2022-06-23 thomas
1823 8d208d34 2022-06-23 thomas *rcol = cols;
1824 8d208d34 2022-06-23 thomas return i;
1825 f91a2b48 2022-06-23 thomas }
1826 f91a2b48 2022-06-23 thomas
1827 f91a2b48 2022-06-23 thomas /*
1828 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1829 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1830 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1831 f91a2b48 2022-06-23 thomas */
1832 f91a2b48 2022-06-23 thomas static const struct got_error *
1833 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1834 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1835 f91a2b48 2022-06-23 thomas {
1836 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1837 8d208d34 2022-06-23 thomas int cols;
1838 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1839 05171be4 2022-06-23 thomas char *exstr = NULL;
1840 963b370f 2018-05-20 stsp size_t wlen;
1841 8d208d34 2022-06-23 thomas int i, scrollx;
1842 963b370f 2018-05-20 stsp
1843 963b370f 2018-05-20 stsp *wlinep = NULL;
1844 b700b5d6 2018-07-10 stsp *widthp = 0;
1845 963b370f 2018-05-20 stsp
1846 05171be4 2022-06-23 thomas if (expand) {
1847 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1848 05171be4 2022-06-23 thomas if (err)
1849 05171be4 2022-06-23 thomas return err;
1850 05171be4 2022-06-23 thomas }
1851 05171be4 2022-06-23 thomas
1852 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1853 05171be4 2022-06-23 thomas free(exstr);
1854 963b370f 2018-05-20 stsp if (err)
1855 963b370f 2018-05-20 stsp return err;
1856 963b370f 2018-05-20 stsp
1857 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1858 f91a2b48 2022-06-23 thomas
1859 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1860 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1861 3f670bfb 2020-12-10 stsp wlen--;
1862 3f670bfb 2020-12-10 stsp }
1863 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1864 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1865 3f670bfb 2020-12-10 stsp wlen--;
1866 3f670bfb 2020-12-10 stsp }
1867 3f670bfb 2020-12-10 stsp
1868 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1869 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1870 27a741e5 2019-09-11 stsp
1871 b700b5d6 2018-07-10 stsp if (widthp)
1872 b700b5d6 2018-07-10 stsp *widthp = cols;
1873 f91a2b48 2022-06-23 thomas if (scrollxp)
1874 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1875 963b370f 2018-05-20 stsp if (err)
1876 963b370f 2018-05-20 stsp free(wline);
1877 963b370f 2018-05-20 stsp else
1878 963b370f 2018-05-20 stsp *wlinep = wline;
1879 963b370f 2018-05-20 stsp return err;
1880 963b370f 2018-05-20 stsp }
1881 963b370f 2018-05-20 stsp
1882 8b473291 2019-02-21 stsp static const struct got_error*
1883 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1884 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1885 8b473291 2019-02-21 stsp {
1886 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1887 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1888 8b473291 2019-02-21 stsp char *s;
1889 8b473291 2019-02-21 stsp const char *name;
1890 8b473291 2019-02-21 stsp
1891 8b473291 2019-02-21 stsp *refs_str = NULL;
1892 8b473291 2019-02-21 stsp
1893 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1894 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1895 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1896 52b5abe1 2019-08-13 stsp int cmp;
1897 52b5abe1 2019-08-13 stsp
1898 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1899 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1900 8b473291 2019-02-21 stsp continue;
1901 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1902 8b473291 2019-02-21 stsp name += 5;
1903 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1904 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1905 7143d404 2019-03-12 stsp continue;
1906 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1907 8b473291 2019-02-21 stsp name += 6;
1908 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1909 8b473291 2019-02-21 stsp name += 8;
1910 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1911 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1912 79cc719f 2020-04-24 stsp continue;
1913 79cc719f 2020-04-24 stsp }
1914 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1915 48cae60d 2020-09-22 stsp if (err)
1916 48cae60d 2020-09-22 stsp break;
1917 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1918 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1919 5d844a1e 2019-08-13 stsp if (err) {
1920 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1921 48cae60d 2020-09-22 stsp free(ref_id);
1922 5d844a1e 2019-08-13 stsp break;
1923 48cae60d 2020-09-22 stsp }
1924 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1925 5d844a1e 2019-08-13 stsp err = NULL;
1926 5d844a1e 2019-08-13 stsp tag = NULL;
1927 5d844a1e 2019-08-13 stsp }
1928 52b5abe1 2019-08-13 stsp }
1929 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1930 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1931 48cae60d 2020-09-22 stsp free(ref_id);
1932 52b5abe1 2019-08-13 stsp if (tag)
1933 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1934 52b5abe1 2019-08-13 stsp if (cmp != 0)
1935 52b5abe1 2019-08-13 stsp continue;
1936 8b473291 2019-02-21 stsp s = *refs_str;
1937 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1938 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1939 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1940 8b473291 2019-02-21 stsp free(s);
1941 8b473291 2019-02-21 stsp *refs_str = NULL;
1942 8b473291 2019-02-21 stsp break;
1943 8b473291 2019-02-21 stsp }
1944 8b473291 2019-02-21 stsp free(s);
1945 8b473291 2019-02-21 stsp }
1946 8b473291 2019-02-21 stsp
1947 8b473291 2019-02-21 stsp return err;
1948 8b473291 2019-02-21 stsp }
1949 8b473291 2019-02-21 stsp
1950 963b370f 2018-05-20 stsp static const struct got_error *
1951 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1952 27a741e5 2019-09-11 stsp int col_tab_align)
1953 5813d178 2019-03-09 stsp {
1954 e6b8b890 2020-12-29 naddy char *smallerthan;
1955 5813d178 2019-03-09 stsp
1956 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1957 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1958 5813d178 2019-03-09 stsp author = smallerthan + 1;
1959 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1960 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1961 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1962 5813d178 2019-03-09 stsp }
1963 5813d178 2019-03-09 stsp
1964 5813d178 2019-03-09 stsp static const struct got_error *
1965 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1966 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1967 8fdc79fe 2020-12-01 naddy int author_display_cols)
1968 80ddbec8 2018-04-29 stsp {
1969 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1970 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1971 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1972 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1973 5813d178 2019-03-09 stsp char *author = NULL;
1974 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1975 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1976 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1977 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1978 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1979 ccb26ccd 2018-11-05 stsp struct tm tm;
1980 45d799e2 2018-12-23 stsp time_t committer_time;
1981 11b20872 2019-11-08 stsp struct tog_color *tc;
1982 80ddbec8 2018-04-29 stsp
1983 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1984 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1985 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1986 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1987 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1988 b39d25c7 2018-07-10 stsp
1989 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1990 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1991 b39d25c7 2018-07-10 stsp else
1992 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1993 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1994 11b20872 2019-11-08 stsp if (tc)
1995 11b20872 2019-11-08 stsp wattr_on(view->window,
1996 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1997 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1998 11b20872 2019-11-08 stsp if (tc)
1999 11b20872 2019-11-08 stsp wattr_off(view->window,
2000 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2001 27a741e5 2019-09-11 stsp col = limit;
2002 b39d25c7 2018-07-10 stsp if (col > avail)
2003 b39d25c7 2018-07-10 stsp goto done;
2004 6570a66d 2019-11-08 stsp
2005 6570a66d 2019-11-08 stsp if (avail >= 120) {
2006 6570a66d 2019-11-08 stsp char *id_str;
2007 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2008 6570a66d 2019-11-08 stsp if (err)
2009 6570a66d 2019-11-08 stsp goto done;
2010 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2011 11b20872 2019-11-08 stsp if (tc)
2012 11b20872 2019-11-08 stsp wattr_on(view->window,
2013 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2014 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2015 11b20872 2019-11-08 stsp if (tc)
2016 11b20872 2019-11-08 stsp wattr_off(view->window,
2017 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2018 6570a66d 2019-11-08 stsp free(id_str);
2019 6570a66d 2019-11-08 stsp col += 9;
2020 6570a66d 2019-11-08 stsp if (col > avail)
2021 6570a66d 2019-11-08 stsp goto done;
2022 6570a66d 2019-11-08 stsp }
2023 b39d25c7 2018-07-10 stsp
2024 f69c5a46 2022-07-19 thomas if (s->use_committer)
2025 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2026 f69c5a46 2022-07-19 thomas else
2027 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2028 5813d178 2019-03-09 stsp if (author == NULL) {
2029 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2030 80ddbec8 2018-04-29 stsp goto done;
2031 80ddbec8 2018-04-29 stsp }
2032 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2033 bb737323 2018-05-20 stsp if (err)
2034 bb737323 2018-05-20 stsp goto done;
2035 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2036 11b20872 2019-11-08 stsp if (tc)
2037 11b20872 2019-11-08 stsp wattr_on(view->window,
2038 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2039 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2040 11b20872 2019-11-08 stsp if (tc)
2041 11b20872 2019-11-08 stsp wattr_off(view->window,
2042 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2043 bb737323 2018-05-20 stsp col += author_width;
2044 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2045 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2046 bb737323 2018-05-20 stsp col++;
2047 bb737323 2018-05-20 stsp author_width++;
2048 bb737323 2018-05-20 stsp }
2049 9c2eaf34 2018-05-20 stsp if (col > avail)
2050 9c2eaf34 2018-05-20 stsp goto done;
2051 80ddbec8 2018-04-29 stsp
2052 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2053 5943eee2 2019-08-13 stsp if (err)
2054 6d9fbc00 2018-04-29 stsp goto done;
2055 bb737323 2018-05-20 stsp logmsg = logmsg0;
2056 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2057 bb737323 2018-05-20 stsp logmsg++;
2058 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2059 bb737323 2018-05-20 stsp if (newline)
2060 bb737323 2018-05-20 stsp *newline = '\0';
2061 f91a2b48 2022-06-23 thomas limit = avail - col;
2062 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2063 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2064 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2065 f91a2b48 2022-06-23 thomas limit, col, 1);
2066 331b1a16 2022-06-23 thomas if (err)
2067 331b1a16 2022-06-23 thomas goto done;
2068 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2069 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2070 27a741e5 2019-09-11 stsp while (col < avail) {
2071 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2072 bb737323 2018-05-20 stsp col++;
2073 881b2d3e 2018-04-30 stsp }
2074 80ddbec8 2018-04-29 stsp done:
2075 80ddbec8 2018-04-29 stsp free(logmsg0);
2076 bb737323 2018-05-20 stsp free(wlogmsg);
2077 5813d178 2019-03-09 stsp free(author);
2078 bb737323 2018-05-20 stsp free(wauthor);
2079 80ddbec8 2018-04-29 stsp free(line);
2080 80ddbec8 2018-04-29 stsp return err;
2081 80ddbec8 2018-04-29 stsp }
2082 26ed57b2 2018-05-19 stsp
2083 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2084 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2085 899d86c2 2018-05-10 stsp struct got_object_id *id)
2086 80ddbec8 2018-04-29 stsp {
2087 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2088 80ddbec8 2018-04-29 stsp
2089 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2090 80ddbec8 2018-04-29 stsp if (entry == NULL)
2091 899d86c2 2018-05-10 stsp return NULL;
2092 99db9666 2018-05-07 stsp
2093 899d86c2 2018-05-10 stsp entry->id = id;
2094 99db9666 2018-05-07 stsp entry->commit = commit;
2095 899d86c2 2018-05-10 stsp return entry;
2096 99db9666 2018-05-07 stsp }
2097 80ddbec8 2018-04-29 stsp
2098 99db9666 2018-05-07 stsp static void
2099 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2100 99db9666 2018-05-07 stsp {
2101 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2102 99db9666 2018-05-07 stsp
2103 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2104 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2105 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2106 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2107 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2108 99db9666 2018-05-07 stsp free(entry);
2109 99db9666 2018-05-07 stsp }
2110 99db9666 2018-05-07 stsp
2111 99db9666 2018-05-07 stsp static void
2112 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2113 99db9666 2018-05-07 stsp {
2114 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2115 99db9666 2018-05-07 stsp pop_commit(commits);
2116 c4972b91 2018-05-07 stsp }
2117 c4972b91 2018-05-07 stsp
2118 c4972b91 2018-05-07 stsp static const struct got_error *
2119 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2120 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2121 13add988 2019-10-15 stsp {
2122 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2123 13add988 2019-10-15 stsp regmatch_t regmatch;
2124 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2125 13add988 2019-10-15 stsp
2126 13add988 2019-10-15 stsp *have_match = 0;
2127 13add988 2019-10-15 stsp
2128 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2129 13add988 2019-10-15 stsp if (err)
2130 13add988 2019-10-15 stsp return err;
2131 13add988 2019-10-15 stsp
2132 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2133 13add988 2019-10-15 stsp if (err)
2134 13add988 2019-10-15 stsp goto done;
2135 13add988 2019-10-15 stsp
2136 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2137 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2138 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2139 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2140 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2141 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2142 13add988 2019-10-15 stsp *have_match = 1;
2143 13add988 2019-10-15 stsp done:
2144 13add988 2019-10-15 stsp free(id_str);
2145 13add988 2019-10-15 stsp free(logmsg);
2146 13add988 2019-10-15 stsp return err;
2147 13add988 2019-10-15 stsp }
2148 13add988 2019-10-15 stsp
2149 13add988 2019-10-15 stsp static const struct got_error *
2150 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2151 c4972b91 2018-05-07 stsp {
2152 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2153 9ba79e04 2018-06-11 stsp
2154 1a76625f 2018-10-22 stsp /*
2155 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2156 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2157 1a76625f 2018-10-22 stsp * while updating the display.
2158 1a76625f 2018-10-22 stsp */
2159 4e0d2870 2020-12-07 naddy do {
2160 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2161 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2162 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2163 1a76625f 2018-10-22 stsp int errcode;
2164 899d86c2 2018-05-10 stsp
2165 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2166 4e0d2870 2020-12-07 naddy NULL, NULL);
2167 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2168 ecb28ae0 2018-07-16 stsp break;
2169 899d86c2 2018-05-10 stsp
2170 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2171 9ba79e04 2018-06-11 stsp if (err)
2172 9ba79e04 2018-06-11 stsp break;
2173 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2174 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2175 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2176 9ba79e04 2018-06-11 stsp break;
2177 9ba79e04 2018-06-11 stsp }
2178 93e45b7c 2018-09-24 stsp
2179 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2180 1a76625f 2018-10-22 stsp if (errcode) {
2181 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2182 13add988 2019-10-15 stsp "pthread_mutex_lock");
2183 1a76625f 2018-10-22 stsp break;
2184 1a76625f 2018-10-22 stsp }
2185 1a76625f 2018-10-22 stsp
2186 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2187 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2188 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2189 1a76625f 2018-10-22 stsp
2190 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2191 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2192 7c1452c1 2020-03-26 stsp int have_match;
2193 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2194 7c1452c1 2020-03-26 stsp if (err)
2195 7c1452c1 2020-03-26 stsp break;
2196 7c1452c1 2020-03-26 stsp if (have_match)
2197 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2198 13add988 2019-10-15 stsp }
2199 13add988 2019-10-15 stsp
2200 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2201 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2202 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2203 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2204 7c1452c1 2020-03-26 stsp if (err)
2205 13add988 2019-10-15 stsp break;
2206 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2207 899d86c2 2018-05-10 stsp
2208 9ba79e04 2018-06-11 stsp return err;
2209 0553a4e3 2018-04-30 stsp }
2210 0553a4e3 2018-04-30 stsp
2211 2b779855 2020-12-05 naddy static void
2212 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2213 2b779855 2020-12-05 naddy {
2214 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2215 2b779855 2020-12-05 naddy int ncommits = 0;
2216 2b779855 2020-12-05 naddy
2217 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2218 2b779855 2020-12-05 naddy while (entry) {
2219 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2220 2b779855 2020-12-05 naddy s->selected_entry = entry;
2221 2b779855 2020-12-05 naddy break;
2222 2b779855 2020-12-05 naddy }
2223 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2224 2b779855 2020-12-05 naddy ncommits++;
2225 2b779855 2020-12-05 naddy }
2226 2b779855 2020-12-05 naddy }
2227 2b779855 2020-12-05 naddy
2228 0553a4e3 2018-04-30 stsp static const struct got_error *
2229 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2230 0553a4e3 2018-04-30 stsp {
2231 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2232 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2233 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2234 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2235 60493ae3 2019-06-20 stsp int width;
2236 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2237 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2238 8b473291 2019-02-21 stsp char *refs_str = NULL;
2239 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2240 11b20872 2019-11-08 stsp struct tog_color *tc;
2241 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2242 0553a4e3 2018-04-30 stsp
2243 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2244 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2245 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2246 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2247 1a76625f 2018-10-22 stsp if (err)
2248 ecb28ae0 2018-07-16 stsp return err;
2249 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2250 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2251 d2075bf3 2020-12-25 stsp if (refs) {
2252 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2253 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2254 d2075bf3 2020-12-25 stsp if (err)
2255 d2075bf3 2020-12-25 stsp goto done;
2256 d2075bf3 2020-12-25 stsp }
2257 867c6645 2018-07-10 stsp }
2258 359bfafd 2019-02-22 stsp
2259 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2260 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2261 1a76625f 2018-10-22 stsp
2262 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2263 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2264 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2265 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2266 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2267 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2268 8f4ed634 2020-03-26 stsp goto done;
2269 8f4ed634 2020-03-26 stsp }
2270 8f4ed634 2020-03-26 stsp } else {
2271 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2272 f9686aa5 2020-03-27 stsp
2273 f9686aa5 2020-03-27 stsp if (view->searching) {
2274 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2275 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2276 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2277 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2278 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2279 f9686aa5 2020-03-27 stsp search_str = "searching...";
2280 f9686aa5 2020-03-27 stsp }
2281 f9686aa5 2020-03-27 stsp
2282 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2283 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2284 f9686aa5 2020-03-27 stsp search_str ? search_str :
2285 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2286 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2287 8f4ed634 2020-03-26 stsp goto done;
2288 8f4ed634 2020-03-26 stsp }
2289 8b473291 2019-02-21 stsp }
2290 1a76625f 2018-10-22 stsp
2291 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2292 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2293 91198554 2022-06-23 thomas "........................................",
2294 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2295 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2296 1a76625f 2018-10-22 stsp header = NULL;
2297 1a76625f 2018-10-22 stsp goto done;
2298 1a76625f 2018-10-22 stsp }
2299 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2300 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2301 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2302 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2303 1a76625f 2018-10-22 stsp header = NULL;
2304 1a76625f 2018-10-22 stsp goto done;
2305 ecb28ae0 2018-07-16 stsp }
2306 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2307 1a76625f 2018-10-22 stsp if (err)
2308 1a76625f 2018-10-22 stsp goto done;
2309 867c6645 2018-07-10 stsp
2310 2814baeb 2018-08-01 stsp werase(view->window);
2311 867c6645 2018-07-10 stsp
2312 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2313 a3404814 2018-09-02 stsp wstandout(view->window);
2314 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2315 11b20872 2019-11-08 stsp if (tc)
2316 11b20872 2019-11-08 stsp wattr_on(view->window,
2317 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2318 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2319 11b20872 2019-11-08 stsp if (tc)
2320 11b20872 2019-11-08 stsp wattr_off(view->window,
2321 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2322 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2323 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2324 1a76625f 2018-10-22 stsp width++;
2325 1a76625f 2018-10-22 stsp }
2326 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2327 a3404814 2018-09-02 stsp wstandend(view->window);
2328 ecb28ae0 2018-07-16 stsp free(wline);
2329 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2330 1a76625f 2018-10-22 stsp goto done;
2331 0553a4e3 2018-04-30 stsp
2332 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2333 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2334 5813d178 2019-03-09 stsp ncommits = 0;
2335 05171be4 2022-06-23 thomas view->maxx = 0;
2336 5813d178 2019-03-09 stsp while (entry) {
2337 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2338 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2339 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2340 5813d178 2019-03-09 stsp int width;
2341 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2342 5813d178 2019-03-09 stsp break;
2343 f69c5a46 2022-07-19 thomas if (s->use_committer)
2344 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2345 f69c5a46 2022-07-19 thomas else
2346 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2347 5813d178 2019-03-09 stsp if (author == NULL) {
2348 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2349 5813d178 2019-03-09 stsp goto done;
2350 5813d178 2019-03-09 stsp }
2351 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2352 27a741e5 2019-09-11 stsp date_display_cols);
2353 5813d178 2019-03-09 stsp if (author_cols < width)
2354 5813d178 2019-03-09 stsp author_cols = width;
2355 5813d178 2019-03-09 stsp free(wauthor);
2356 5813d178 2019-03-09 stsp free(author);
2357 ef944b8b 2022-07-21 thomas if (err)
2358 ef944b8b 2022-07-21 thomas goto done;
2359 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2360 05171be4 2022-06-23 thomas if (err)
2361 05171be4 2022-06-23 thomas goto done;
2362 05171be4 2022-06-23 thomas msg = msg0;
2363 05171be4 2022-06-23 thomas while (*msg == '\n')
2364 05171be4 2022-06-23 thomas ++msg;
2365 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2366 331b1a16 2022-06-23 thomas *eol = '\0';
2367 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2368 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2369 331b1a16 2022-06-23 thomas if (err)
2370 331b1a16 2022-06-23 thomas goto done;
2371 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2372 05171be4 2022-06-23 thomas free(msg0);
2373 331b1a16 2022-06-23 thomas free(wmsg);
2374 7ca04879 2019-10-19 stsp ncommits++;
2375 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2376 5813d178 2019-03-09 stsp }
2377 5813d178 2019-03-09 stsp
2378 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2379 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2380 867c6645 2018-07-10 stsp ncommits = 0;
2381 899d86c2 2018-05-10 stsp while (entry) {
2382 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2383 899d86c2 2018-05-10 stsp break;
2384 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2385 2814baeb 2018-08-01 stsp wstandout(view->window);
2386 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2387 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2388 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2389 2814baeb 2018-08-01 stsp wstandend(view->window);
2390 0553a4e3 2018-04-30 stsp if (err)
2391 60493ae3 2019-06-20 stsp goto done;
2392 0553a4e3 2018-04-30 stsp ncommits++;
2393 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2394 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2395 80ddbec8 2018-04-29 stsp }
2396 80ddbec8 2018-04-29 stsp
2397 a5d43cac 2022-07-01 thomas view_border(view);
2398 1a76625f 2018-10-22 stsp done:
2399 1a76625f 2018-10-22 stsp free(id_str);
2400 8b473291 2019-02-21 stsp free(refs_str);
2401 1a76625f 2018-10-22 stsp free(ncommits_str);
2402 1a76625f 2018-10-22 stsp free(header);
2403 80ddbec8 2018-04-29 stsp return err;
2404 9f7d7167 2018-04-29 stsp }
2405 07b55e75 2018-05-10 stsp
2406 07b55e75 2018-05-10 stsp static void
2407 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2408 07b55e75 2018-05-10 stsp {
2409 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2410 07b55e75 2018-05-10 stsp int nscrolled = 0;
2411 07b55e75 2018-05-10 stsp
2412 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2413 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2414 07b55e75 2018-05-10 stsp return;
2415 9f7d7167 2018-04-29 stsp
2416 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2417 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2418 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2419 07b55e75 2018-05-10 stsp if (entry) {
2420 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2421 07b55e75 2018-05-10 stsp nscrolled++;
2422 07b55e75 2018-05-10 stsp }
2423 07b55e75 2018-05-10 stsp }
2424 aa075928 2018-05-10 stsp }
2425 aa075928 2018-05-10 stsp
2426 aa075928 2018-05-10 stsp static const struct got_error *
2427 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2428 aa075928 2018-05-10 stsp {
2429 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2430 5e224a3e 2019-02-22 stsp int errcode;
2431 8a42fca8 2019-02-22 stsp
2432 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2433 aa075928 2018-05-10 stsp
2434 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2435 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2436 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2437 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2438 7aafa0d1 2019-02-22 stsp if (errcode)
2439 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2440 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2441 7c1452c1 2020-03-26 stsp
2442 7c1452c1 2020-03-26 stsp /*
2443 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2444 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2445 7c1452c1 2020-03-26 stsp */
2446 7c1452c1 2020-03-26 stsp if (!wait)
2447 7c1452c1 2020-03-26 stsp break;
2448 7c1452c1 2020-03-26 stsp
2449 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2450 ffe38506 2020-12-01 naddy show_log_view(view);
2451 7c1452c1 2020-03-26 stsp update_panels();
2452 7c1452c1 2020-03-26 stsp doupdate();
2453 7c1452c1 2020-03-26 stsp
2454 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2455 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2456 82954512 2020-02-03 stsp if (errcode)
2457 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2458 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2459 82954512 2020-02-03 stsp
2460 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2461 ffe38506 2020-12-01 naddy show_log_view(view);
2462 7c1452c1 2020-03-26 stsp update_panels();
2463 7c1452c1 2020-03-26 stsp doupdate();
2464 5e224a3e 2019-02-22 stsp }
2465 5e224a3e 2019-02-22 stsp
2466 5e224a3e 2019-02-22 stsp return NULL;
2467 a5d43cac 2022-07-01 thomas }
2468 a5d43cac 2022-07-01 thomas
2469 a5d43cac 2022-07-01 thomas static const struct got_error *
2470 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2471 a5d43cac 2022-07-01 thomas {
2472 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2473 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2474 fe731b51 2022-07-14 thomas
2475 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2476 fe731b51 2022-07-14 thomas return NULL;
2477 a5d43cac 2022-07-01 thomas
2478 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2479 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2480 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2481 a5d43cac 2022-07-01 thomas
2482 a5d43cac 2022-07-01 thomas return err;
2483 5e224a3e 2019-02-22 stsp }
2484 5e224a3e 2019-02-22 stsp
2485 5e224a3e 2019-02-22 stsp static const struct got_error *
2486 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2487 5e224a3e 2019-02-22 stsp {
2488 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2489 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2490 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2491 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2492 5e224a3e 2019-02-22 stsp
2493 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2494 5e224a3e 2019-02-22 stsp return NULL;
2495 5e224a3e 2019-02-22 stsp
2496 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2497 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2498 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2499 08ebd0a9 2019-02-22 stsp /*
2500 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2501 08ebd0a9 2019-02-22 stsp */
2502 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2503 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2504 5e224a3e 2019-02-22 stsp if (err)
2505 5e224a3e 2019-02-22 stsp return err;
2506 7aafa0d1 2019-02-22 stsp }
2507 b295e71b 2019-02-22 stsp
2508 7aafa0d1 2019-02-22 stsp do {
2509 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2510 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2511 88048b54 2019-02-21 stsp break;
2512 88048b54 2019-02-21 stsp
2513 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2514 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2515 aa075928 2018-05-10 stsp
2516 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2517 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2518 dd0a52c1 2018-05-20 stsp break;
2519 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2520 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2521 aa075928 2018-05-10 stsp
2522 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2523 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2524 a5d43cac 2022-07-01 thomas else
2525 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2526 a5d43cac 2022-07-01 thomas
2527 dd0a52c1 2018-05-20 stsp return err;
2528 07b55e75 2018-05-10 stsp }
2529 4a7f7875 2018-05-10 stsp
2530 cd0acaa7 2018-05-20 stsp static const struct got_error *
2531 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2532 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2533 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2534 cd0acaa7 2018-05-20 stsp {
2535 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2536 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2537 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2538 cd0acaa7 2018-05-20 stsp
2539 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2540 15a94983 2018-12-23 stsp if (diff_view == NULL)
2541 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2542 ea5e7bb5 2018-08-01 stsp
2543 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2544 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2545 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2546 e5a0f69f 2018-08-18 stsp if (err == NULL)
2547 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2548 cd0acaa7 2018-05-20 stsp return err;
2549 4a7f7875 2018-05-10 stsp }
2550 4a7f7875 2018-05-10 stsp
2551 80ddbec8 2018-04-29 stsp static const struct got_error *
2552 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2553 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2554 9343a5fb 2018-06-23 stsp {
2555 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2556 941e9f74 2019-05-21 stsp
2557 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2558 941e9f74 2019-05-21 stsp if (parent == NULL)
2559 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2560 941e9f74 2019-05-21 stsp
2561 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2562 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2563 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2564 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2565 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2566 941e9f74 2019-05-21 stsp s->tree = subtree;
2567 941e9f74 2019-05-21 stsp s->selected = 0;
2568 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2569 941e9f74 2019-05-21 stsp return NULL;
2570 941e9f74 2019-05-21 stsp }
2571 941e9f74 2019-05-21 stsp
2572 941e9f74 2019-05-21 stsp static const struct got_error *
2573 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2574 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2575 941e9f74 2019-05-21 stsp {
2576 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2577 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2578 941e9f74 2019-05-21 stsp const char *p;
2579 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2580 9343a5fb 2018-06-23 stsp
2581 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2582 941e9f74 2019-05-21 stsp p = path;
2583 941e9f74 2019-05-21 stsp while (*p) {
2584 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2585 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2586 56e0773d 2019-11-28 stsp char *te_name;
2587 33cbf02b 2020-01-12 stsp
2588 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2589 33cbf02b 2020-01-12 stsp p++;
2590 941e9f74 2019-05-21 stsp
2591 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2592 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2593 941e9f74 2019-05-21 stsp if (slash == NULL)
2594 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2595 33cbf02b 2020-01-12 stsp else
2596 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2597 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2598 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2599 56e0773d 2019-11-28 stsp break;
2600 941e9f74 2019-05-21 stsp }
2601 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2602 56e0773d 2019-11-28 stsp if (te == NULL) {
2603 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2604 56e0773d 2019-11-28 stsp free(te_name);
2605 941e9f74 2019-05-21 stsp break;
2606 941e9f74 2019-05-21 stsp }
2607 56e0773d 2019-11-28 stsp free(te_name);
2608 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2609 941e9f74 2019-05-21 stsp
2610 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2611 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2612 b03c880f 2019-05-21 stsp
2613 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2614 941e9f74 2019-05-21 stsp if (slash)
2615 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2616 941e9f74 2019-05-21 stsp else
2617 941e9f74 2019-05-21 stsp subpath = strdup(path);
2618 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2619 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2620 941e9f74 2019-05-21 stsp break;
2621 941e9f74 2019-05-21 stsp }
2622 941e9f74 2019-05-21 stsp
2623 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2624 941e9f74 2019-05-21 stsp subpath);
2625 941e9f74 2019-05-21 stsp if (err)
2626 941e9f74 2019-05-21 stsp break;
2627 941e9f74 2019-05-21 stsp
2628 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2629 941e9f74 2019-05-21 stsp free(tree_id);
2630 941e9f74 2019-05-21 stsp if (err)
2631 941e9f74 2019-05-21 stsp break;
2632 941e9f74 2019-05-21 stsp
2633 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2634 941e9f74 2019-05-21 stsp if (err) {
2635 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2636 941e9f74 2019-05-21 stsp break;
2637 941e9f74 2019-05-21 stsp }
2638 941e9f74 2019-05-21 stsp if (slash == NULL)
2639 941e9f74 2019-05-21 stsp break;
2640 941e9f74 2019-05-21 stsp free(subpath);
2641 941e9f74 2019-05-21 stsp subpath = NULL;
2642 941e9f74 2019-05-21 stsp p = slash;
2643 941e9f74 2019-05-21 stsp }
2644 941e9f74 2019-05-21 stsp
2645 941e9f74 2019-05-21 stsp free(subpath);
2646 1a76625f 2018-10-22 stsp return err;
2647 61266923 2020-01-14 stsp }
2648 61266923 2020-01-14 stsp
2649 61266923 2020-01-14 stsp static const struct got_error *
2650 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2651 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2652 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2653 55cccc34 2020-02-20 stsp {
2654 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2655 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2656 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2657 55cccc34 2020-02-20 stsp
2658 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2659 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2660 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2661 55cccc34 2020-02-20 stsp
2662 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2663 bc573f3b 2021-07-10 stsp if (err)
2664 55cccc34 2020-02-20 stsp return err;
2665 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2666 55cccc34 2020-02-20 stsp
2667 55cccc34 2020-02-20 stsp *new_view = tree_view;
2668 55cccc34 2020-02-20 stsp
2669 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2670 55cccc34 2020-02-20 stsp return NULL;
2671 55cccc34 2020-02-20 stsp
2672 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2673 55cccc34 2020-02-20 stsp }
2674 55cccc34 2020-02-20 stsp
2675 55cccc34 2020-02-20 stsp static const struct got_error *
2676 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2677 61266923 2020-01-14 stsp {
2678 61266923 2020-01-14 stsp sigset_t sigset;
2679 61266923 2020-01-14 stsp int errcode;
2680 61266923 2020-01-14 stsp
2681 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2682 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2683 61266923 2020-01-14 stsp
2684 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2685 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2686 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2687 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2688 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2689 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2690 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2691 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2692 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2693 61266923 2020-01-14 stsp
2694 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2695 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2696 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2697 61266923 2020-01-14 stsp
2698 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2699 61266923 2020-01-14 stsp if (errcode)
2700 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2701 61266923 2020-01-14 stsp
2702 61266923 2020-01-14 stsp return NULL;
2703 1a76625f 2018-10-22 stsp }
2704 1a76625f 2018-10-22 stsp
2705 1a76625f 2018-10-22 stsp static void *
2706 1a76625f 2018-10-22 stsp log_thread(void *arg)
2707 1a76625f 2018-10-22 stsp {
2708 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2709 1a76625f 2018-10-22 stsp int errcode = 0;
2710 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2711 1a76625f 2018-10-22 stsp int done = 0;
2712 61266923 2020-01-14 stsp
2713 f2d749db 2022-07-12 thomas /*
2714 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2715 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2716 f2d749db 2022-07-12 thomas */
2717 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2718 f2d749db 2022-07-12 thomas if (errcode) {
2719 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2720 61266923 2020-01-14 stsp return (void *)err;
2721 f2d749db 2022-07-12 thomas }
2722 1a76625f 2018-10-22 stsp
2723 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2724 f2d749db 2022-07-12 thomas if (err) {
2725 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2726 f2d749db 2022-07-12 thomas goto done;
2727 f2d749db 2022-07-12 thomas }
2728 f2d749db 2022-07-12 thomas
2729 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2730 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2731 f2d749db 2022-07-12 thomas if (errcode) {
2732 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2733 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2734 f2d749db 2022-07-12 thomas goto done;
2735 f2d749db 2022-07-12 thomas }
2736 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2737 1a76625f 2018-10-22 stsp if (err) {
2738 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2739 f2d749db 2022-07-12 thomas goto done;
2740 1a76625f 2018-10-22 stsp err = NULL;
2741 1a76625f 2018-10-22 stsp done = 1;
2742 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2743 1a76625f 2018-10-22 stsp a->commits_needed--;
2744 1a76625f 2018-10-22 stsp
2745 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2746 3abe8080 2019-04-10 stsp if (errcode) {
2747 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2748 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2749 f2d749db 2022-07-12 thomas goto done;
2750 3abe8080 2019-04-10 stsp } else if (*a->quit)
2751 1a76625f 2018-10-22 stsp done = 1;
2752 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2753 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2754 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2755 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2756 1a76625f 2018-10-22 stsp }
2757 1a76625f 2018-10-22 stsp
2758 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2759 7c1452c1 2020-03-26 stsp if (errcode) {
2760 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2761 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2762 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2763 f2d749db 2022-07-12 thomas goto done;
2764 7c1452c1 2020-03-26 stsp }
2765 7c1452c1 2020-03-26 stsp
2766 1a76625f 2018-10-22 stsp if (done)
2767 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2768 7c1452c1 2020-03-26 stsp else {
2769 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2770 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2771 7c1452c1 2020-03-26 stsp &tog_mutex);
2772 f2d749db 2022-07-12 thomas if (errcode) {
2773 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2774 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2775 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2776 f2d749db 2022-07-12 thomas goto done;
2777 f2d749db 2022-07-12 thomas }
2778 21355643 2020-12-06 stsp if (*a->quit)
2779 21355643 2020-12-06 stsp done = 1;
2780 7c1452c1 2020-03-26 stsp }
2781 1a76625f 2018-10-22 stsp }
2782 1a76625f 2018-10-22 stsp }
2783 3abe8080 2019-04-10 stsp a->log_complete = 1;
2784 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2785 f2d749db 2022-07-12 thomas if (errcode)
2786 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2787 f2d749db 2022-07-12 thomas done:
2788 f2d749db 2022-07-12 thomas if (err) {
2789 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2790 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2791 f2d749db 2022-07-12 thomas }
2792 1a76625f 2018-10-22 stsp return (void *)err;
2793 1a76625f 2018-10-22 stsp }
2794 1a76625f 2018-10-22 stsp
2795 1a76625f 2018-10-22 stsp static const struct got_error *
2796 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2797 1a76625f 2018-10-22 stsp {
2798 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2799 1a76625f 2018-10-22 stsp int errcode;
2800 1a76625f 2018-10-22 stsp
2801 1a76625f 2018-10-22 stsp if (s->thread) {
2802 1a76625f 2018-10-22 stsp s->quit = 1;
2803 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2804 1a76625f 2018-10-22 stsp if (errcode)
2805 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2806 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2807 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2808 1a76625f 2018-10-22 stsp if (errcode)
2809 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2810 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2811 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2812 1a76625f 2018-10-22 stsp if (errcode)
2813 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2814 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2815 1a76625f 2018-10-22 stsp if (errcode)
2816 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2817 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2818 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2819 1a76625f 2018-10-22 stsp }
2820 1a76625f 2018-10-22 stsp
2821 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2822 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2823 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2824 1af5eddf 2022-06-23 thomas }
2825 1af5eddf 2022-06-23 thomas
2826 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2827 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2828 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2829 1af5eddf 2022-06-23 thomas if (err == NULL)
2830 1af5eddf 2022-06-23 thomas err = pack_err;
2831 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2832 1a76625f 2018-10-22 stsp }
2833 1a76625f 2018-10-22 stsp
2834 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2835 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2836 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2837 1a76625f 2018-10-22 stsp }
2838 1a76625f 2018-10-22 stsp
2839 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2840 9343a5fb 2018-06-23 stsp }
2841 9343a5fb 2018-06-23 stsp
2842 9343a5fb 2018-06-23 stsp static const struct got_error *
2843 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2844 1a76625f 2018-10-22 stsp {
2845 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2846 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2847 276b94a1 2020-11-13 naddy int errcode;
2848 1a76625f 2018-10-22 stsp
2849 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2850 276b94a1 2020-11-13 naddy
2851 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2852 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2853 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2854 276b94a1 2020-11-13 naddy
2855 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2856 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2857 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2858 276b94a1 2020-11-13 naddy
2859 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2860 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2861 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2862 1a76625f 2018-10-22 stsp free(s->start_id);
2863 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2864 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2865 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2866 1a76625f 2018-10-22 stsp return err;
2867 1a76625f 2018-10-22 stsp }
2868 1a76625f 2018-10-22 stsp
2869 1a76625f 2018-10-22 stsp static const struct got_error *
2870 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2871 60493ae3 2019-06-20 stsp {
2872 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2873 60493ae3 2019-06-20 stsp
2874 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2875 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2876 60493ae3 2019-06-20 stsp return NULL;
2877 60493ae3 2019-06-20 stsp }
2878 60493ae3 2019-06-20 stsp
2879 60493ae3 2019-06-20 stsp static const struct got_error *
2880 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2881 60493ae3 2019-06-20 stsp {
2882 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2883 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2884 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2885 60493ae3 2019-06-20 stsp
2886 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2887 f9686aa5 2020-03-27 stsp show_log_view(view);
2888 f9686aa5 2020-03-27 stsp update_panels();
2889 f9686aa5 2020-03-27 stsp doupdate();
2890 f9686aa5 2020-03-27 stsp
2891 96e2b566 2019-07-08 stsp if (s->search_entry) {
2892 13add988 2019-10-15 stsp int errcode, ch;
2893 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2894 13add988 2019-10-15 stsp if (errcode)
2895 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2896 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2897 13add988 2019-10-15 stsp ch = wgetch(view->window);
2898 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2899 13add988 2019-10-15 stsp if (errcode)
2900 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2901 13add988 2019-10-15 stsp "pthread_mutex_lock");
2902 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2903 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2904 678cbce5 2019-07-28 stsp return NULL;
2905 678cbce5 2019-07-28 stsp }
2906 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2907 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2908 96e2b566 2019-07-08 stsp else
2909 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2910 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2911 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2912 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2913 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2914 df5e841d 2022-06-23 thomas
2915 df5e841d 2022-06-23 thomas /*
2916 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2917 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2918 1f4d5162 2022-06-23 thomas * might have changed.
2919 df5e841d 2022-06-23 thomas */
2920 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2921 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2922 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2923 df5e841d 2022-06-23 thomas else
2924 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2925 b74feda6 2022-06-23 thomas } else {
2926 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2927 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2928 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2929 df5e841d 2022-06-23 thomas else
2930 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2931 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2932 b74feda6 2022-06-23 thomas }
2933 20be8d96 2019-06-21 stsp } else {
2934 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2935 20be8d96 2019-06-21 stsp }
2936 60493ae3 2019-06-20 stsp
2937 60493ae3 2019-06-20 stsp while (1) {
2938 13add988 2019-10-15 stsp int have_match = 0;
2939 13add988 2019-10-15 stsp
2940 60493ae3 2019-06-20 stsp if (entry == NULL) {
2941 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2942 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2943 f9967bca 2020-03-27 stsp view->search_next_done =
2944 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2945 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2946 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2947 f801134a 2019-06-25 stsp return NULL;
2948 60493ae3 2019-06-20 stsp }
2949 96e2b566 2019-07-08 stsp /*
2950 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2951 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2952 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2953 96e2b566 2019-07-08 stsp */
2954 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2955 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2956 60493ae3 2019-06-20 stsp }
2957 60493ae3 2019-06-20 stsp
2958 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2959 13add988 2019-10-15 stsp &view->regex);
2960 5943eee2 2019-08-13 stsp if (err)
2961 13add988 2019-10-15 stsp break;
2962 13add988 2019-10-15 stsp if (have_match) {
2963 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2964 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2965 60493ae3 2019-06-20 stsp break;
2966 60493ae3 2019-06-20 stsp }
2967 13add988 2019-10-15 stsp
2968 96e2b566 2019-07-08 stsp s->search_entry = entry;
2969 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2970 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2971 b1bf1435 2019-06-21 stsp else
2972 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2973 60493ae3 2019-06-20 stsp }
2974 60493ae3 2019-06-20 stsp
2975 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2976 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2977 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2978 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2979 60493ae3 2019-06-20 stsp if (err)
2980 60493ae3 2019-06-20 stsp return err;
2981 ead14cbe 2019-06-21 stsp cur++;
2982 ead14cbe 2019-06-21 stsp }
2983 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2984 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2985 60493ae3 2019-06-20 stsp if (err)
2986 60493ae3 2019-06-20 stsp return err;
2987 ead14cbe 2019-06-21 stsp cur--;
2988 60493ae3 2019-06-20 stsp }
2989 60493ae3 2019-06-20 stsp }
2990 60493ae3 2019-06-20 stsp
2991 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2992 96e2b566 2019-07-08 stsp
2993 60493ae3 2019-06-20 stsp return NULL;
2994 60493ae3 2019-06-20 stsp }
2995 60493ae3 2019-06-20 stsp
2996 60493ae3 2019-06-20 stsp static const struct got_error *
2997 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2998 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2999 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3000 80ddbec8 2018-04-29 stsp {
3001 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3002 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3003 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3004 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3005 1a76625f 2018-10-22 stsp int errcode;
3006 80ddbec8 2018-04-29 stsp
3007 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3008 f135c941 2020-02-20 stsp free(s->in_repo_path);
3009 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3010 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3011 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3012 f135c941 2020-02-20 stsp }
3013 ecb28ae0 2018-07-16 stsp
3014 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3015 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3016 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3017 78756c87 2020-11-24 stsp
3018 fb2756b9 2018-08-04 stsp s->repo = repo;
3019 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3020 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3021 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3022 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3023 9cd7cbd1 2020-12-07 stsp goto done;
3024 9cd7cbd1 2020-12-07 stsp }
3025 9cd7cbd1 2020-12-07 stsp }
3026 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3027 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3028 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3029 5036bf37 2018-09-24 stsp goto done;
3030 5036bf37 2018-09-24 stsp }
3031 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3032 e5a0f69f 2018-08-18 stsp
3033 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3034 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3035 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3036 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3037 11b20872 2019-11-08 stsp if (err)
3038 11b20872 2019-11-08 stsp goto done;
3039 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3040 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3041 11b20872 2019-11-08 stsp if (err) {
3042 11b20872 2019-11-08 stsp free_colors(&s->colors);
3043 11b20872 2019-11-08 stsp goto done;
3044 11b20872 2019-11-08 stsp }
3045 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3046 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3047 11b20872 2019-11-08 stsp if (err) {
3048 11b20872 2019-11-08 stsp free_colors(&s->colors);
3049 11b20872 2019-11-08 stsp goto done;
3050 11b20872 2019-11-08 stsp }
3051 11b20872 2019-11-08 stsp }
3052 11b20872 2019-11-08 stsp
3053 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3054 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3055 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3056 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3057 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3058 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3059 1a76625f 2018-10-22 stsp
3060 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3061 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3062 1af5eddf 2022-06-23 thomas if (err)
3063 1af5eddf 2022-06-23 thomas goto done;
3064 1af5eddf 2022-06-23 thomas }
3065 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3066 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3067 7cd52833 2022-06-23 thomas if (err)
3068 7cd52833 2022-06-23 thomas goto done;
3069 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3070 b672a97a 2020-01-27 stsp !s->log_branches);
3071 1a76625f 2018-10-22 stsp if (err)
3072 1a76625f 2018-10-22 stsp goto done;
3073 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3074 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3075 c5b78334 2020-01-12 stsp if (err)
3076 c5b78334 2020-01-12 stsp goto done;
3077 1a76625f 2018-10-22 stsp
3078 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3079 1a76625f 2018-10-22 stsp if (errcode) {
3080 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3081 1a76625f 2018-10-22 stsp goto done;
3082 1a76625f 2018-10-22 stsp }
3083 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3084 7c1452c1 2020-03-26 stsp if (errcode) {
3085 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3086 7c1452c1 2020-03-26 stsp goto done;
3087 7c1452c1 2020-03-26 stsp }
3088 1a76625f 2018-10-22 stsp
3089 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3090 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3091 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3092 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3093 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3094 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3095 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3096 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3097 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3098 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3099 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3100 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3101 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3102 ba4f502b 2018-08-04 stsp done:
3103 1a76625f 2018-10-22 stsp if (err)
3104 1a76625f 2018-10-22 stsp close_log_view(view);
3105 ba4f502b 2018-08-04 stsp return err;
3106 ba4f502b 2018-08-04 stsp }
3107 ba4f502b 2018-08-04 stsp
3108 e5a0f69f 2018-08-18 stsp static const struct got_error *
3109 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3110 ba4f502b 2018-08-04 stsp {
3111 f2f6d207 2020-11-24 stsp const struct got_error *err;
3112 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3113 ba4f502b 2018-08-04 stsp
3114 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3115 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3116 2b380cc8 2018-10-24 stsp &s->thread_args);
3117 2b380cc8 2018-10-24 stsp if (errcode)
3118 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3119 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3120 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3121 f2f6d207 2020-11-24 stsp if (err)
3122 f2f6d207 2020-11-24 stsp return err;
3123 f2f6d207 2020-11-24 stsp }
3124 2b380cc8 2018-10-24 stsp }
3125 2b380cc8 2018-10-24 stsp
3126 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3127 b31f89ff 2022-07-01 thomas }
3128 b31f89ff 2022-07-01 thomas
3129 b31f89ff 2022-07-01 thomas static void
3130 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3131 b31f89ff 2022-07-01 thomas {
3132 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3133 b31f89ff 2022-07-01 thomas
3134 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3135 b31f89ff 2022-07-01 thomas view->count = 0;
3136 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3137 b31f89ff 2022-07-01 thomas return;
3138 b31f89ff 2022-07-01 thomas
3139 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3140 b31f89ff 2022-07-01 thomas || home)
3141 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3142 b31f89ff 2022-07-01 thomas
3143 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3144 b31f89ff 2022-07-01 thomas --s->selected;
3145 b31f89ff 2022-07-01 thomas else
3146 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3147 b31f89ff 2022-07-01 thomas
3148 b31f89ff 2022-07-01 thomas select_commit(s);
3149 b31f89ff 2022-07-01 thomas return;
3150 b31f89ff 2022-07-01 thomas }
3151 b31f89ff 2022-07-01 thomas
3152 b31f89ff 2022-07-01 thomas static const struct got_error *
3153 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3154 b31f89ff 2022-07-01 thomas {
3155 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3156 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3157 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3158 b31f89ff 2022-07-01 thomas
3159 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3160 b31f89ff 2022-07-01 thomas if (first == NULL) {
3161 b31f89ff 2022-07-01 thomas view->count = 0;
3162 b31f89ff 2022-07-01 thomas return NULL;
3163 b31f89ff 2022-07-01 thomas }
3164 b31f89ff 2022-07-01 thomas
3165 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3166 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3167 b31f89ff 2022-07-01 thomas return NULL;
3168 b31f89ff 2022-07-01 thomas
3169 b31f89ff 2022-07-01 thomas if (!page) {
3170 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3171 b31f89ff 2022-07-01 thomas
3172 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3173 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3174 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3175 b31f89ff 2022-07-01 thomas ++s->selected;
3176 b31f89ff 2022-07-01 thomas else
3177 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3178 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3179 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3180 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3181 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3182 b31f89ff 2022-07-01 thomas else
3183 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3184 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3185 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3186 b31f89ff 2022-07-01 thomas } else {
3187 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3188 b31f89ff 2022-07-01 thomas if (err)
3189 b31f89ff 2022-07-01 thomas return err;
3190 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3191 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3192 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3193 b31f89ff 2022-07-01 thomas }
3194 b31f89ff 2022-07-01 thomas }
3195 b31f89ff 2022-07-01 thomas if (err)
3196 b31f89ff 2022-07-01 thomas return err;
3197 b31f89ff 2022-07-01 thomas
3198 a5d43cac 2022-07-01 thomas /*
3199 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3200 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3201 a5d43cac 2022-07-01 thomas */
3202 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3203 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3204 a5d43cac 2022-07-01 thomas
3205 b31f89ff 2022-07-01 thomas select_commit(s);
3206 b31f89ff 2022-07-01 thomas
3207 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3208 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3209 b31f89ff 2022-07-01 thomas view->count = 0;
3210 b31f89ff 2022-07-01 thomas
3211 b31f89ff 2022-07-01 thomas return NULL;
3212 e5a0f69f 2018-08-18 stsp }
3213 04cc582a 2018-08-01 stsp
3214 a5d43cac 2022-07-01 thomas static void
3215 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3216 a5d43cac 2022-07-01 thomas {
3217 24415785 2022-07-03 thomas *x = 0;
3218 24415785 2022-07-03 thomas *y = 0;
3219 24415785 2022-07-03 thomas
3220 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3221 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3222 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3223 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3224 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3225 53d2bdd3 2022-07-10 thomas else
3226 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3227 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3228 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3229 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3230 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3231 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3232 53d2bdd3 2022-07-10 thomas else
3233 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3234 53d2bdd3 2022-07-10 thomas }
3235 a5d43cac 2022-07-01 thomas }
3236 a5d43cac 2022-07-01 thomas
3237 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3238 e5a0f69f 2018-08-18 stsp static const struct got_error *
3239 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3240 a5d43cac 2022-07-01 thomas {
3241 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3242 a5d43cac 2022-07-01 thomas
3243 a5d43cac 2022-07-01 thomas view->nlines = y;
3244 64486692 2022-07-07 thomas view->ncols = COLS;
3245 a5d43cac 2022-07-01 thomas err = view_resize(view);
3246 a5d43cac 2022-07-01 thomas if (err)
3247 a5d43cac 2022-07-01 thomas return err;
3248 a5d43cac 2022-07-01 thomas
3249 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3250 a5d43cac 2022-07-01 thomas
3251 a5d43cac 2022-07-01 thomas return err;
3252 a5d43cac 2022-07-01 thomas }
3253 a5d43cac 2022-07-01 thomas
3254 a5d43cac 2022-07-01 thomas static const struct got_error *
3255 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3256 e5a0f69f 2018-08-18 stsp {
3257 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3258 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3259 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3260 2a31b33b 2022-07-23 thomas int eos, n, nscroll;
3261 80ddbec8 2018-04-29 stsp
3262 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3263 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3264 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3265 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3266 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3267 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3268 fb280deb 2021-08-30 stsp }
3269 b31f89ff 2022-07-01 thomas return err;
3270 528dedf3 2021-08-30 stsp }
3271 068ab281 2022-07-01 thomas
3272 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3273 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3274 068ab281 2022-07-01 thomas --eos; /* border */
3275 068ab281 2022-07-01 thomas
3276 528dedf3 2021-08-30 stsp switch (ch) {
3277 1e37a5c2 2019-05-12 jcs case 'q':
3278 1e37a5c2 2019-05-12 jcs s->quit = 1;
3279 05171be4 2022-06-23 thomas break;
3280 05171be4 2022-06-23 thomas case '0':
3281 05171be4 2022-06-23 thomas view->x = 0;
3282 05171be4 2022-06-23 thomas break;
3283 05171be4 2022-06-23 thomas case '$':
3284 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3285 07b0611c 2022-06-23 thomas view->count = 0;
3286 05171be4 2022-06-23 thomas break;
3287 05171be4 2022-06-23 thomas case KEY_RIGHT:
3288 05171be4 2022-06-23 thomas case 'l':
3289 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3290 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3291 07b0611c 2022-06-23 thomas else
3292 07b0611c 2022-06-23 thomas view->count = 0;
3293 1e37a5c2 2019-05-12 jcs break;
3294 05171be4 2022-06-23 thomas case KEY_LEFT:
3295 05171be4 2022-06-23 thomas case 'h':
3296 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3297 07b0611c 2022-06-23 thomas if (view->x <= 0)
3298 07b0611c 2022-06-23 thomas view->count = 0;
3299 05171be4 2022-06-23 thomas break;
3300 1e37a5c2 2019-05-12 jcs case 'k':
3301 1e37a5c2 2019-05-12 jcs case KEY_UP:
3302 1e37a5c2 2019-05-12 jcs case '<':
3303 1e37a5c2 2019-05-12 jcs case ',':
3304 f7140bf5 2021-10-17 thomas case CTRL('p'):
3305 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3306 912a3f79 2021-08-30 j break;
3307 912a3f79 2021-08-30 j case 'g':
3308 912a3f79 2021-08-30 j case KEY_HOME:
3309 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3310 07b0611c 2022-06-23 thomas view->count = 0;
3311 1e37a5c2 2019-05-12 jcs break;
3312 70f17a53 2022-06-13 thomas case CTRL('u'):
3313 23427b14 2022-06-23 thomas case 'u':
3314 70f17a53 2022-06-13 thomas nscroll /= 2;
3315 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3316 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3317 a4292ac5 2019-05-12 jcs case CTRL('b'):
3318 1c5e5faa 2022-06-23 thomas case 'b':
3319 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3320 1e37a5c2 2019-05-12 jcs break;
3321 1e37a5c2 2019-05-12 jcs case 'j':
3322 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3323 1e37a5c2 2019-05-12 jcs case '>':
3324 1e37a5c2 2019-05-12 jcs case '.':
3325 f7140bf5 2021-10-17 thomas case CTRL('n'):
3326 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3327 912a3f79 2021-08-30 j break;
3328 f69c5a46 2022-07-19 thomas case '@':
3329 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3330 f69c5a46 2022-07-19 thomas break;
3331 912a3f79 2021-08-30 j case 'G':
3332 912a3f79 2021-08-30 j case KEY_END: {
3333 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3334 912a3f79 2021-08-30 j * traverse them all. */
3335 07b0611c 2022-06-23 thomas view->count = 0;
3336 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3337 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3338 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3339 80ddbec8 2018-04-29 stsp }
3340 912a3f79 2021-08-30 j
3341 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3342 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3343 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3344 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3345 f3bc9f1d 2021-09-05 naddy break;
3346 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3347 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3348 f3bc9f1d 2021-09-05 naddy }
3349 f3bc9f1d 2021-09-05 naddy if (n > 0)
3350 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3351 2b779855 2020-12-05 naddy select_commit(s);
3352 1e37a5c2 2019-05-12 jcs break;
3353 912a3f79 2021-08-30 j }
3354 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3355 23427b14 2022-06-23 thomas case 'd':
3356 70f17a53 2022-06-13 thomas nscroll /= 2;
3357 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3358 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3359 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3360 4c2d69cb 2022-06-23 thomas case 'f':
3361 b31f89ff 2022-07-01 thomas case ' ':
3362 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3363 1e37a5c2 2019-05-12 jcs break;
3364 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3365 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3366 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3367 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3368 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3369 2b779855 2020-12-05 naddy select_commit(s);
3370 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3371 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3372 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3373 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3374 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3375 0bf7f153 2020-12-02 naddy }
3376 1e37a5c2 2019-05-12 jcs break;
3377 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3378 444d5325 2022-07-03 thomas case '\r':
3379 07b0611c 2022-06-23 thomas view->count = 0;
3380 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3381 e5a0f69f 2018-08-18 stsp break;
3382 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3383 1e37a5c2 2019-05-12 jcs break;
3384 1be4947a 2022-07-22 thomas case 'T':
3385 07b0611c 2022-06-23 thomas view->count = 0;
3386 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3387 5036bf37 2018-09-24 stsp break;
3388 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3389 1e37a5c2 2019-05-12 jcs break;
3390 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3391 21355643 2020-12-06 stsp case CTRL('l'):
3392 21355643 2020-12-06 stsp case 'B':
3393 07b0611c 2022-06-23 thomas view->count = 0;
3394 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3395 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3396 1e37a5c2 2019-05-12 jcs break;
3397 21355643 2020-12-06 stsp err = stop_log_thread(s);
3398 74cfe85e 2020-10-20 stsp if (err)
3399 74cfe85e 2020-10-20 stsp return err;
3400 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3401 21355643 2020-12-06 stsp char *parent_path;
3402 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3403 21355643 2020-12-06 stsp if (err)
3404 21355643 2020-12-06 stsp return err;
3405 21355643 2020-12-06 stsp free(s->in_repo_path);
3406 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3407 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3408 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3409 21355643 2020-12-06 stsp struct got_object_id *start_id;
3410 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3411 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3412 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3413 21355643 2020-12-06 stsp if (err)
3414 21355643 2020-12-06 stsp return err;
3415 21355643 2020-12-06 stsp free(s->start_id);
3416 21355643 2020-12-06 stsp s->start_id = start_id;
3417 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3418 21355643 2020-12-06 stsp } else /* 'B' */
3419 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3420 21355643 2020-12-06 stsp
3421 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3422 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3423 bf7e79b3 2022-06-23 thomas if (err)
3424 bf7e79b3 2022-06-23 thomas return err;
3425 bf7e79b3 2022-06-23 thomas }
3426 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3427 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3428 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3429 74cfe85e 2020-10-20 stsp if (err)
3430 21355643 2020-12-06 stsp return err;
3431 51a10b52 2020-12-26 stsp tog_free_refs();
3432 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3433 ca51c541 2020-12-07 stsp if (err)
3434 ca51c541 2020-12-07 stsp return err;
3435 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3436 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3437 d01904d4 2019-06-25 stsp if (err)
3438 d01904d4 2019-06-25 stsp return err;
3439 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3440 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3441 b672a97a 2020-01-27 stsp if (err)
3442 b672a97a 2020-01-27 stsp return err;
3443 21355643 2020-12-06 stsp free_commits(&s->commits);
3444 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3445 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3446 21355643 2020-12-06 stsp s->selected_entry = NULL;
3447 21355643 2020-12-06 stsp s->selected = 0;
3448 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3449 21355643 2020-12-06 stsp s->quit = 0;
3450 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3451 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3452 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3453 94ecf40d 2022-07-22 thomas view->offset = 0;
3454 d01904d4 2019-06-25 stsp break;
3455 1be4947a 2022-07-22 thomas case 'R':
3456 07b0611c 2022-06-23 thomas view->count = 0;
3457 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3458 6458efa5 2020-11-24 stsp break;
3459 1e37a5c2 2019-05-12 jcs default:
3460 07b0611c 2022-06-23 thomas view->count = 0;
3461 1e37a5c2 2019-05-12 jcs break;
3462 899d86c2 2018-05-10 stsp }
3463 e5a0f69f 2018-08-18 stsp
3464 80ddbec8 2018-04-29 stsp return err;
3465 80ddbec8 2018-04-29 stsp }
3466 80ddbec8 2018-04-29 stsp
3467 4ed7e80c 2018-05-20 stsp static const struct got_error *
3468 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3469 c2db6724 2019-01-04 stsp {
3470 c2db6724 2019-01-04 stsp const struct got_error *error;
3471 c2db6724 2019-01-04 stsp
3472 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3473 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3474 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3475 37c06ea4 2019-07-15 stsp #endif
3476 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3477 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3478 c2db6724 2019-01-04 stsp
3479 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3480 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3481 c2db6724 2019-01-04 stsp
3482 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3483 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3484 c2db6724 2019-01-04 stsp
3485 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3486 c2db6724 2019-01-04 stsp if (error != NULL)
3487 c2db6724 2019-01-04 stsp return error;
3488 c2db6724 2019-01-04 stsp
3489 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3490 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3491 c2db6724 2019-01-04 stsp
3492 c2db6724 2019-01-04 stsp return NULL;
3493 c2db6724 2019-01-04 stsp }
3494 c2db6724 2019-01-04 stsp
3495 a915003a 2019-02-05 stsp static void
3496 a915003a 2019-02-05 stsp init_curses(void)
3497 a915003a 2019-02-05 stsp {
3498 296152d1 2022-05-31 thomas /*
3499 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3500 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3501 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3502 296152d1 2022-05-31 thomas */
3503 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3504 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3505 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3506 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3507 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3508 296152d1 2022-05-31 thomas
3509 a915003a 2019-02-05 stsp initscr();
3510 a915003a 2019-02-05 stsp cbreak();
3511 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3512 a915003a 2019-02-05 stsp noecho();
3513 a915003a 2019-02-05 stsp nonl();
3514 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3515 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3516 a915003a 2019-02-05 stsp curs_set(0);
3517 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3518 6d17833f 2019-11-08 stsp start_color();
3519 6d17833f 2019-11-08 stsp use_default_colors();
3520 6d17833f 2019-11-08 stsp }
3521 a915003a 2019-02-05 stsp }
3522 a915003a 2019-02-05 stsp
3523 c2db6724 2019-01-04 stsp static const struct got_error *
3524 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3525 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3526 f135c941 2020-02-20 stsp {
3527 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3528 f135c941 2020-02-20 stsp
3529 f135c941 2020-02-20 stsp if (argc == 0) {
3530 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3531 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3532 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3533 f135c941 2020-02-20 stsp return NULL;
3534 f135c941 2020-02-20 stsp }
3535 f135c941 2020-02-20 stsp
3536 f135c941 2020-02-20 stsp if (worktree) {
3537 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3538 bfd61697 2020-11-14 stsp char *p;
3539 f135c941 2020-02-20 stsp
3540 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3541 f135c941 2020-02-20 stsp if (err)
3542 f135c941 2020-02-20 stsp return err;
3543 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3544 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3545 bfd61697 2020-11-14 stsp p) == -1) {
3546 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3547 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3548 f135c941 2020-02-20 stsp }
3549 f135c941 2020-02-20 stsp free(p);
3550 f135c941 2020-02-20 stsp } else
3551 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3552 f135c941 2020-02-20 stsp
3553 f135c941 2020-02-20 stsp return err;
3554 f135c941 2020-02-20 stsp }
3555 f135c941 2020-02-20 stsp
3556 f135c941 2020-02-20 stsp static const struct got_error *
3557 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3558 9f7d7167 2018-04-29 stsp {
3559 80ddbec8 2018-04-29 stsp const struct got_error *error;
3560 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3561 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3562 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3563 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3564 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3565 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3566 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3567 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3568 04cc582a 2018-08-01 stsp struct tog_view *view;
3569 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3570 80ddbec8 2018-04-29 stsp
3571 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3572 80ddbec8 2018-04-29 stsp switch (ch) {
3573 b672a97a 2020-01-27 stsp case 'b':
3574 b672a97a 2020-01-27 stsp log_branches = 1;
3575 b672a97a 2020-01-27 stsp break;
3576 80ddbec8 2018-04-29 stsp case 'c':
3577 80ddbec8 2018-04-29 stsp start_commit = optarg;
3578 80ddbec8 2018-04-29 stsp break;
3579 ecb28ae0 2018-07-16 stsp case 'r':
3580 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3581 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3582 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3583 9ba1d308 2019-10-21 stsp optarg);
3584 ecb28ae0 2018-07-16 stsp break;
3585 80ddbec8 2018-04-29 stsp default:
3586 17020d27 2019-03-07 stsp usage_log();
3587 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3588 80ddbec8 2018-04-29 stsp }
3589 80ddbec8 2018-04-29 stsp }
3590 80ddbec8 2018-04-29 stsp
3591 80ddbec8 2018-04-29 stsp argc -= optind;
3592 80ddbec8 2018-04-29 stsp argv += optind;
3593 80ddbec8 2018-04-29 stsp
3594 f135c941 2020-02-20 stsp if (argc > 1)
3595 f135c941 2020-02-20 stsp usage_log();
3596 963f97a1 2019-03-18 stsp
3597 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3598 7cd52833 2022-06-23 thomas if (error != NULL)
3599 7cd52833 2022-06-23 thomas goto done;
3600 7cd52833 2022-06-23 thomas
3601 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3602 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3603 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3604 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3605 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3606 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3607 c156c7a4 2020-12-18 stsp goto done;
3608 a1fbf39a 2019-08-11 stsp if (worktree)
3609 6962eb72 2020-02-20 stsp repo_path =
3610 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3611 a1fbf39a 2019-08-11 stsp else
3612 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3613 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3614 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3615 c156c7a4 2020-12-18 stsp goto done;
3616 c156c7a4 2020-12-18 stsp }
3617 ecb28ae0 2018-07-16 stsp }
3618 ecb28ae0 2018-07-16 stsp
3619 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3620 80ddbec8 2018-04-29 stsp if (error != NULL)
3621 ecb28ae0 2018-07-16 stsp goto done;
3622 80ddbec8 2018-04-29 stsp
3623 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3624 f135c941 2020-02-20 stsp repo, worktree);
3625 f135c941 2020-02-20 stsp if (error)
3626 f135c941 2020-02-20 stsp goto done;
3627 f135c941 2020-02-20 stsp
3628 f135c941 2020-02-20 stsp init_curses();
3629 f135c941 2020-02-20 stsp
3630 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3631 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3632 c02c541e 2019-03-29 stsp if (error)
3633 c02c541e 2019-03-29 stsp goto done;
3634 c02c541e 2019-03-29 stsp
3635 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3636 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3637 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3638 87670572 2020-12-26 naddy if (error)
3639 87670572 2020-12-26 naddy goto done;
3640 87670572 2020-12-26 naddy }
3641 51a10b52 2020-12-26 stsp
3642 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3643 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3644 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3645 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3646 d8f38dc4 2020-12-05 stsp if (error)
3647 d8f38dc4 2020-12-05 stsp goto done;
3648 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3649 d8f38dc4 2020-12-05 stsp } else {
3650 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3651 d8f38dc4 2020-12-05 stsp if (error == NULL)
3652 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3653 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3654 d8f38dc4 2020-12-05 stsp goto done;
3655 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3656 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3657 d8f38dc4 2020-12-05 stsp if (error)
3658 d8f38dc4 2020-12-05 stsp goto done;
3659 d8f38dc4 2020-12-05 stsp }
3660 8b473291 2019-02-21 stsp
3661 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3662 04cc582a 2018-08-01 stsp if (view == NULL) {
3663 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3664 04cc582a 2018-08-01 stsp goto done;
3665 04cc582a 2018-08-01 stsp }
3666 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3667 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3668 ba4f502b 2018-08-04 stsp if (error)
3669 ba4f502b 2018-08-04 stsp goto done;
3670 2fc00ff4 2019-08-31 stsp if (worktree) {
3671 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3672 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3673 2fc00ff4 2019-08-31 stsp worktree = NULL;
3674 2fc00ff4 2019-08-31 stsp }
3675 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3676 ecb28ae0 2018-07-16 stsp done:
3677 f135c941 2020-02-20 stsp free(in_repo_path);
3678 ecb28ae0 2018-07-16 stsp free(repo_path);
3679 ecb28ae0 2018-07-16 stsp free(cwd);
3680 899d86c2 2018-05-10 stsp free(start_id);
3681 d8f38dc4 2020-12-05 stsp free(label);
3682 d8f38dc4 2020-12-05 stsp if (ref)
3683 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3684 1d0f4054 2021-06-17 stsp if (repo) {
3685 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3686 1d0f4054 2021-06-17 stsp if (error == NULL)
3687 1d0f4054 2021-06-17 stsp error = close_err;
3688 1d0f4054 2021-06-17 stsp }
3689 ec142235 2019-03-07 stsp if (worktree)
3690 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3691 7cd52833 2022-06-23 thomas if (pack_fds) {
3692 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3693 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3694 7cd52833 2022-06-23 thomas if (error == NULL)
3695 7cd52833 2022-06-23 thomas error = pack_err;
3696 7cd52833 2022-06-23 thomas }
3697 51a10b52 2020-12-26 stsp tog_free_refs();
3698 80ddbec8 2018-04-29 stsp return error;
3699 9f7d7167 2018-04-29 stsp }
3700 9f7d7167 2018-04-29 stsp
3701 4ed7e80c 2018-05-20 stsp __dead static void
3702 9f7d7167 2018-04-29 stsp usage_diff(void)
3703 9f7d7167 2018-04-29 stsp {
3704 80ddbec8 2018-04-29 stsp endwin();
3705 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3706 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3707 9f7d7167 2018-04-29 stsp exit(1);
3708 b304db33 2018-05-20 stsp }
3709 b304db33 2018-05-20 stsp
3710 6d17833f 2019-11-08 stsp static int
3711 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3712 41605754 2020-11-12 stsp regmatch_t *regmatch)
3713 6d17833f 2019-11-08 stsp {
3714 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3715 6d17833f 2019-11-08 stsp }
3716 6d17833f 2019-11-08 stsp
3717 ef20f542 2022-06-26 thomas static struct tog_color *
3718 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3719 6d17833f 2019-11-08 stsp {
3720 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3721 6d17833f 2019-11-08 stsp
3722 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3723 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3724 6d17833f 2019-11-08 stsp return tc;
3725 6d17833f 2019-11-08 stsp }
3726 6d17833f 2019-11-08 stsp
3727 6d17833f 2019-11-08 stsp return NULL;
3728 6d17833f 2019-11-08 stsp }
3729 6d17833f 2019-11-08 stsp
3730 4ed7e80c 2018-05-20 stsp static const struct got_error *
3731 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3732 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3733 41605754 2020-11-12 stsp {
3734 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3735 666f7b10 2022-06-23 thomas char *exstr = NULL;
3736 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3737 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3738 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3739 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3740 41605754 2020-11-12 stsp
3741 41605754 2020-11-12 stsp *wtotal = 0;
3742 cbea3800 2022-06-23 thomas
3743 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3744 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3745 41605754 2020-11-12 stsp
3746 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3747 666f7b10 2022-06-23 thomas if (err)
3748 666f7b10 2022-06-23 thomas return err;
3749 666f7b10 2022-06-23 thomas
3750 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3751 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3752 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3753 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3754 666f7b10 2022-06-23 thomas goto done;
3755 666f7b10 2022-06-23 thomas }
3756 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3757 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3758 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3759 cbea3800 2022-06-23 thomas goto done;
3760 cbea3800 2022-06-23 thomas }
3761 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3762 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3763 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3764 cbea3800 2022-06-23 thomas goto done;
3765 cbea3800 2022-06-23 thomas }
3766 05171be4 2022-06-23 thomas
3767 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3768 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3769 cbea3800 2022-06-23 thomas col_tab_align, 1);
3770 cbea3800 2022-06-23 thomas if (err)
3771 cbea3800 2022-06-23 thomas goto done;
3772 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3773 05171be4 2022-06-23 thomas if (n) {
3774 cbea3800 2022-06-23 thomas free(wline);
3775 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3776 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3777 cbea3800 2022-06-23 thomas if (err)
3778 cbea3800 2022-06-23 thomas goto done;
3779 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3780 cbea3800 2022-06-23 thomas wlimit -= width;
3781 cbea3800 2022-06-23 thomas *wtotal += width;
3782 41605754 2020-11-12 stsp }
3783 41605754 2020-11-12 stsp
3784 41605754 2020-11-12 stsp if (wlimit > 0) {
3785 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3786 cbea3800 2022-06-23 thomas size_t wlen;
3787 cbea3800 2022-06-23 thomas
3788 cbea3800 2022-06-23 thomas free(wline);
3789 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3790 cbea3800 2022-06-23 thomas col_tab_align, 1);
3791 cbea3800 2022-06-23 thomas if (err)
3792 cbea3800 2022-06-23 thomas goto done;
3793 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3794 cbea3800 2022-06-23 thomas while (i < wlen) {
3795 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3796 cbea3800 2022-06-23 thomas if (width == -1) {
3797 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3798 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3799 cbea3800 2022-06-23 thomas goto done;
3800 cbea3800 2022-06-23 thomas }
3801 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3802 cbea3800 2022-06-23 thomas break;
3803 1c5e5faa 2022-06-23 thomas w += width;
3804 cbea3800 2022-06-23 thomas i++;
3805 41605754 2020-11-12 stsp }
3806 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3807 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3808 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3809 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3810 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3811 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3812 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3813 41605754 2020-11-12 stsp }
3814 41605754 2020-11-12 stsp }
3815 41605754 2020-11-12 stsp
3816 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3817 cbea3800 2022-06-23 thomas free(wline);
3818 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3819 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3820 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3821 cbea3800 2022-06-23 thomas col_tab_align, 1);
3822 cbea3800 2022-06-23 thomas if (err)
3823 cbea3800 2022-06-23 thomas goto done;
3824 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3825 cbea3800 2022-06-23 thomas } else {
3826 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3827 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3828 cbea3800 2022-06-23 thomas if (err)
3829 cbea3800 2022-06-23 thomas goto done;
3830 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3831 cbea3800 2022-06-23 thomas }
3832 cbea3800 2022-06-23 thomas *wtotal += width2;
3833 41605754 2020-11-12 stsp }
3834 cbea3800 2022-06-23 thomas done:
3835 05171be4 2022-06-23 thomas free(wline);
3836 666f7b10 2022-06-23 thomas free(exstr);
3837 cbea3800 2022-06-23 thomas free(seg0);
3838 cbea3800 2022-06-23 thomas free(seg1);
3839 cbea3800 2022-06-23 thomas free(seg2);
3840 cbea3800 2022-06-23 thomas return err;
3841 41605754 2020-11-12 stsp }
3842 41605754 2020-11-12 stsp
3843 41605754 2020-11-12 stsp static const struct got_error *
3844 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3845 26ed57b2 2018-05-19 stsp {
3846 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3847 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3848 61e69b96 2018-05-20 stsp const struct got_error *err;
3849 fe621944 2020-11-10 stsp int nprinted = 0;
3850 b304db33 2018-05-20 stsp char *line;
3851 826082fe 2020-12-10 stsp size_t linesize = 0;
3852 826082fe 2020-12-10 stsp ssize_t linelen;
3853 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3854 61e69b96 2018-05-20 stsp wchar_t *wline;
3855 e0b650dd 2018-05-20 stsp int width;
3856 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3857 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3858 fe621944 2020-11-10 stsp off_t line_offset;
3859 26ed57b2 2018-05-19 stsp
3860 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3861 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3862 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3863 fe621944 2020-11-10 stsp
3864 f7d12f7e 2018-08-01 stsp werase(view->window);
3865 a3404814 2018-09-02 stsp
3866 a3404814 2018-09-02 stsp if (header) {
3867 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3868 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3869 135a2da0 2020-11-11 stsp header) == -1)
3870 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3871 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3872 f91a2b48 2022-06-23 thomas 0, 0);
3873 135a2da0 2020-11-11 stsp free(line);
3874 135a2da0 2020-11-11 stsp if (err)
3875 a3404814 2018-09-02 stsp return err;
3876 a3404814 2018-09-02 stsp
3877 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3878 a3404814 2018-09-02 stsp wstandout(view->window);
3879 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3880 e54cc94a 2020-11-11 stsp free(wline);
3881 e54cc94a 2020-11-11 stsp wline = NULL;
3882 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3883 a3404814 2018-09-02 stsp wstandend(view->window);
3884 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3885 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3886 26ed57b2 2018-05-19 stsp
3887 a3404814 2018-09-02 stsp if (max_lines <= 1)
3888 a3404814 2018-09-02 stsp return NULL;
3889 a3404814 2018-09-02 stsp max_lines--;
3890 a3404814 2018-09-02 stsp }
3891 a3404814 2018-09-02 stsp
3892 89f1a395 2020-12-01 naddy s->eof = 0;
3893 05171be4 2022-06-23 thomas view->maxx = 0;
3894 826082fe 2020-12-10 stsp line = NULL;
3895 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3896 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3897 826082fe 2020-12-10 stsp if (linelen == -1) {
3898 826082fe 2020-12-10 stsp if (feof(s->f)) {
3899 826082fe 2020-12-10 stsp s->eof = 1;
3900 826082fe 2020-12-10 stsp break;
3901 826082fe 2020-12-10 stsp }
3902 826082fe 2020-12-10 stsp free(line);
3903 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3904 61e69b96 2018-05-20 stsp }
3905 05171be4 2022-06-23 thomas
3906 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3907 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3908 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3909 cbea3800 2022-06-23 thomas if (err) {
3910 cbea3800 2022-06-23 thomas free(line);
3911 cbea3800 2022-06-23 thomas return err;
3912 cbea3800 2022-06-23 thomas }
3913 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3914 cbea3800 2022-06-23 thomas free(wline);
3915 cbea3800 2022-06-23 thomas wline = NULL;
3916 cbea3800 2022-06-23 thomas
3917 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3918 f26dddb7 2019-11-08 stsp if (tc)
3919 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3920 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3921 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3922 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3923 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3924 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3925 41605754 2020-11-12 stsp if (err) {
3926 41605754 2020-11-12 stsp free(line);
3927 41605754 2020-11-12 stsp return err;
3928 41605754 2020-11-12 stsp }
3929 41605754 2020-11-12 stsp } else {
3930 f1c0ec19 2022-06-23 thomas int skip;
3931 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3932 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3933 f1c0ec19 2022-06-23 thomas if (err) {
3934 f1c0ec19 2022-06-23 thomas free(line);
3935 f1c0ec19 2022-06-23 thomas return err;
3936 f1c0ec19 2022-06-23 thomas }
3937 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3938 f1c0ec19 2022-06-23 thomas free(wline);
3939 41605754 2020-11-12 stsp wline = NULL;
3940 41605754 2020-11-12 stsp }
3941 f26dddb7 2019-11-08 stsp if (tc)
3942 6d17833f 2019-11-08 stsp wattr_off(view->window,
3943 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3944 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3945 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3946 fe621944 2020-11-10 stsp nprinted++;
3947 826082fe 2020-12-10 stsp }
3948 826082fe 2020-12-10 stsp free(line);
3949 fe621944 2020-11-10 stsp if (nprinted >= 1)
3950 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3951 89f1a395 2020-12-01 naddy (nprinted - 1);
3952 fe621944 2020-11-10 stsp else
3953 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3954 26ed57b2 2018-05-19 stsp
3955 a5d43cac 2022-07-01 thomas view_border(view);
3956 c3e9aa98 2019-05-13 jcs
3957 89f1a395 2020-12-01 naddy if (s->eof) {
3958 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3959 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3960 c3e9aa98 2019-05-13 jcs nprinted++;
3961 c3e9aa98 2019-05-13 jcs }
3962 c3e9aa98 2019-05-13 jcs
3963 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3964 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
3965 c3e9aa98 2019-05-13 jcs if (err) {
3966 c3e9aa98 2019-05-13 jcs return err;
3967 c3e9aa98 2019-05-13 jcs }
3968 26ed57b2 2018-05-19 stsp
3969 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3970 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3971 e54cc94a 2020-11-11 stsp free(wline);
3972 e54cc94a 2020-11-11 stsp wline = NULL;
3973 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3974 c3e9aa98 2019-05-13 jcs }
3975 c3e9aa98 2019-05-13 jcs
3976 26ed57b2 2018-05-19 stsp return NULL;
3977 abd2672a 2018-12-23 stsp }
3978 abd2672a 2018-12-23 stsp
3979 abd2672a 2018-12-23 stsp static char *
3980 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3981 abd2672a 2018-12-23 stsp {
3982 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3983 09867e48 2019-08-13 stsp char *p, *s;
3984 09867e48 2019-08-13 stsp
3985 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3986 09867e48 2019-08-13 stsp if (tm == NULL)
3987 09867e48 2019-08-13 stsp return NULL;
3988 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3989 09867e48 2019-08-13 stsp if (s == NULL)
3990 09867e48 2019-08-13 stsp return NULL;
3991 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3992 abd2672a 2018-12-23 stsp if (p)
3993 abd2672a 2018-12-23 stsp *p = '\0';
3994 abd2672a 2018-12-23 stsp return s;
3995 9f7d7167 2018-04-29 stsp }
3996 9f7d7167 2018-04-29 stsp
3997 4ed7e80c 2018-05-20 stsp static const struct got_error *
3998 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3999 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4000 0208f208 2020-05-05 stsp {
4001 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4002 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4003 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4004 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4005 0208f208 2020-05-05 stsp
4006 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4007 0208f208 2020-05-05 stsp if (qid != NULL) {
4008 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4009 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4010 ec242592 2022-04-22 thomas &qid->id);
4011 0208f208 2020-05-05 stsp if (err)
4012 0208f208 2020-05-05 stsp return err;
4013 0208f208 2020-05-05 stsp
4014 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4015 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4016 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4017 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4018 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4019 aa8b5dd0 2021-08-01 stsp }
4020 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4021 0208f208 2020-05-05 stsp
4022 0208f208 2020-05-05 stsp }
4023 0208f208 2020-05-05 stsp
4024 0208f208 2020-05-05 stsp if (tree_id1) {
4025 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4026 0208f208 2020-05-05 stsp if (err)
4027 0208f208 2020-05-05 stsp goto done;
4028 0208f208 2020-05-05 stsp }
4029 0208f208 2020-05-05 stsp
4030 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4031 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4032 0208f208 2020-05-05 stsp if (err)
4033 0208f208 2020-05-05 stsp goto done;
4034 0208f208 2020-05-05 stsp
4035 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4036 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4037 0208f208 2020-05-05 stsp done:
4038 0208f208 2020-05-05 stsp if (tree1)
4039 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4040 0208f208 2020-05-05 stsp if (tree2)
4041 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4042 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4043 0208f208 2020-05-05 stsp return err;
4044 0208f208 2020-05-05 stsp }
4045 0208f208 2020-05-05 stsp
4046 0208f208 2020-05-05 stsp static const struct got_error *
4047 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4048 abd2672a 2018-12-23 stsp {
4049 fe621944 2020-11-10 stsp off_t *p;
4050 fe621944 2020-11-10 stsp
4051 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4052 fe621944 2020-11-10 stsp if (p == NULL)
4053 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4054 fe621944 2020-11-10 stsp *line_offsets = p;
4055 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4056 fe621944 2020-11-10 stsp (*nlines)++;
4057 fe621944 2020-11-10 stsp return NULL;
4058 fe621944 2020-11-10 stsp }
4059 fe621944 2020-11-10 stsp
4060 fe621944 2020-11-10 stsp static const struct got_error *
4061 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4062 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4063 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4064 fe621944 2020-11-10 stsp {
4065 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4066 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4067 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4068 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4069 45d799e2 2018-12-23 stsp time_t committer_time;
4070 45d799e2 2018-12-23 stsp const char *author, *committer;
4071 8b473291 2019-02-21 stsp char *refs_str = NULL;
4072 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4073 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4074 fe621944 2020-11-10 stsp off_t outoff = 0;
4075 fe621944 2020-11-10 stsp int n;
4076 abd2672a 2018-12-23 stsp
4077 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4078 0208f208 2020-05-05 stsp
4079 8b473291 2019-02-21 stsp if (refs) {
4080 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4081 8b473291 2019-02-21 stsp if (err)
4082 8b473291 2019-02-21 stsp return err;
4083 8b473291 2019-02-21 stsp }
4084 8b473291 2019-02-21 stsp
4085 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4086 abd2672a 2018-12-23 stsp if (err)
4087 abd2672a 2018-12-23 stsp return err;
4088 abd2672a 2018-12-23 stsp
4089 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4090 15a94983 2018-12-23 stsp if (err) {
4091 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4092 15a94983 2018-12-23 stsp goto done;
4093 15a94983 2018-12-23 stsp }
4094 abd2672a 2018-12-23 stsp
4095 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4096 fe621944 2020-11-10 stsp if (err)
4097 fe621944 2020-11-10 stsp goto done;
4098 fe621944 2020-11-10 stsp
4099 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4100 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4101 fe621944 2020-11-10 stsp if (n < 0) {
4102 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4103 abd2672a 2018-12-23 stsp goto done;
4104 abd2672a 2018-12-23 stsp }
4105 fe621944 2020-11-10 stsp outoff += n;
4106 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4107 fe621944 2020-11-10 stsp if (err)
4108 fe621944 2020-11-10 stsp goto done;
4109 fe621944 2020-11-10 stsp
4110 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4111 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4112 fe621944 2020-11-10 stsp if (n < 0) {
4113 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4114 abd2672a 2018-12-23 stsp goto done;
4115 abd2672a 2018-12-23 stsp }
4116 fe621944 2020-11-10 stsp outoff += n;
4117 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4118 fe621944 2020-11-10 stsp if (err)
4119 fe621944 2020-11-10 stsp goto done;
4120 fe621944 2020-11-10 stsp
4121 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4122 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4123 fe621944 2020-11-10 stsp if (datestr) {
4124 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4125 fe621944 2020-11-10 stsp if (n < 0) {
4126 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4127 fe621944 2020-11-10 stsp goto done;
4128 fe621944 2020-11-10 stsp }
4129 fe621944 2020-11-10 stsp outoff += n;
4130 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4131 fe621944 2020-11-10 stsp if (err)
4132 fe621944 2020-11-10 stsp goto done;
4133 abd2672a 2018-12-23 stsp }
4134 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4135 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4136 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4137 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4138 fe621944 2020-11-10 stsp if (n < 0) {
4139 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4140 fe621944 2020-11-10 stsp goto done;
4141 fe621944 2020-11-10 stsp }
4142 fe621944 2020-11-10 stsp outoff += n;
4143 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4144 fe621944 2020-11-10 stsp if (err)
4145 fe621944 2020-11-10 stsp goto done;
4146 abd2672a 2018-12-23 stsp }
4147 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4148 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4149 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4150 ac4dc263 2021-09-24 thomas int pn = 1;
4151 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4152 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4153 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4154 ac4dc263 2021-09-24 thomas if (err)
4155 ac4dc263 2021-09-24 thomas goto done;
4156 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4157 ac4dc263 2021-09-24 thomas if (n < 0) {
4158 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4159 ac4dc263 2021-09-24 thomas goto done;
4160 ac4dc263 2021-09-24 thomas }
4161 ac4dc263 2021-09-24 thomas outoff += n;
4162 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4163 ac4dc263 2021-09-24 thomas if (err)
4164 ac4dc263 2021-09-24 thomas goto done;
4165 ac4dc263 2021-09-24 thomas free(id_str);
4166 ac4dc263 2021-09-24 thomas id_str = NULL;
4167 ac4dc263 2021-09-24 thomas }
4168 ac4dc263 2021-09-24 thomas }
4169 ac4dc263 2021-09-24 thomas
4170 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4171 5943eee2 2019-08-13 stsp if (err)
4172 5943eee2 2019-08-13 stsp goto done;
4173 fe621944 2020-11-10 stsp s = logmsg;
4174 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4175 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4176 fe621944 2020-11-10 stsp if (n < 0) {
4177 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4178 fe621944 2020-11-10 stsp goto done;
4179 fe621944 2020-11-10 stsp }
4180 fe621944 2020-11-10 stsp outoff += n;
4181 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4182 fe621944 2020-11-10 stsp if (err)
4183 fe621944 2020-11-10 stsp goto done;
4184 abd2672a 2018-12-23 stsp }
4185 fe621944 2020-11-10 stsp
4186 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4187 0208f208 2020-05-05 stsp if (err)
4188 0208f208 2020-05-05 stsp goto done;
4189 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4190 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4191 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4192 fe621944 2020-11-10 stsp if (n < 0) {
4193 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4194 fe621944 2020-11-10 stsp goto done;
4195 fe621944 2020-11-10 stsp }
4196 fe621944 2020-11-10 stsp outoff += n;
4197 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4198 fe621944 2020-11-10 stsp if (err)
4199 fe621944 2020-11-10 stsp goto done;
4200 0208f208 2020-05-05 stsp free((char *)pe->path);
4201 0208f208 2020-05-05 stsp free(pe->data);
4202 0208f208 2020-05-05 stsp }
4203 fe621944 2020-11-10 stsp
4204 0208f208 2020-05-05 stsp fputc('\n', outfile);
4205 fe621944 2020-11-10 stsp outoff++;
4206 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4207 abd2672a 2018-12-23 stsp done:
4208 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4209 abd2672a 2018-12-23 stsp free(id_str);
4210 5943eee2 2019-08-13 stsp free(logmsg);
4211 8b473291 2019-02-21 stsp free(refs_str);
4212 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4213 7510f233 2020-08-09 stsp if (err) {
4214 ae6a6978 2020-08-09 stsp free(*line_offsets);
4215 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4216 ae6a6978 2020-08-09 stsp *nlines = 0;
4217 7510f233 2020-08-09 stsp }
4218 fe621944 2020-11-10 stsp return err;
4219 abd2672a 2018-12-23 stsp }
4220 abd2672a 2018-12-23 stsp
4221 abd2672a 2018-12-23 stsp static const struct got_error *
4222 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4223 26ed57b2 2018-05-19 stsp {
4224 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4225 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4226 15a94983 2018-12-23 stsp int obj_type;
4227 fe621944 2020-11-10 stsp
4228 fe621944 2020-11-10 stsp free(s->line_offsets);
4229 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4230 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4231 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4232 fe621944 2020-11-10 stsp s->nlines = 0;
4233 26ed57b2 2018-05-19 stsp
4234 511a516b 2018-05-19 stsp f = got_opentemp();
4235 48ae06ee 2018-10-18 stsp if (f == NULL) {
4236 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4237 48ae06ee 2018-10-18 stsp goto done;
4238 48ae06ee 2018-10-18 stsp }
4239 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4240 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4241 fb43ecf1 2019-02-11 stsp goto done;
4242 fb43ecf1 2019-02-11 stsp }
4243 48ae06ee 2018-10-18 stsp s->f = f;
4244 26ed57b2 2018-05-19 stsp
4245 15a94983 2018-12-23 stsp if (s->id1)
4246 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4247 15a94983 2018-12-23 stsp else
4248 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4249 15a94983 2018-12-23 stsp if (err)
4250 15a94983 2018-12-23 stsp goto done;
4251 15a94983 2018-12-23 stsp
4252 15a94983 2018-12-23 stsp switch (obj_type) {
4253 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4254 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4255 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4256 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4257 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4258 26ed57b2 2018-05-19 stsp break;
4259 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4260 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4261 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4262 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4263 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4264 26ed57b2 2018-05-19 stsp break;
4265 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4266 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4267 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4268 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4269 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4270 abd2672a 2018-12-23 stsp
4271 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4272 abd2672a 2018-12-23 stsp if (err)
4273 3ffacbe1 2020-02-02 tracey goto done;
4274 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4275 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4276 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4277 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4278 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4279 f44b1f58 2020-02-02 tracey if (err)
4280 f44b1f58 2020-02-02 tracey goto done;
4281 f44b1f58 2020-02-02 tracey } else {
4282 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4283 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4284 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4285 fe621944 2020-11-10 stsp err = write_commit_info(
4286 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4287 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4288 f44b1f58 2020-02-02 tracey if (err)
4289 f44b1f58 2020-02-02 tracey goto done;
4290 f5404e4e 2020-02-02 tracey break;
4291 15a087fe 2019-02-21 stsp }
4292 abd2672a 2018-12-23 stsp }
4293 abd2672a 2018-12-23 stsp }
4294 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4295 abd2672a 2018-12-23 stsp
4296 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4297 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4298 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4299 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4300 26ed57b2 2018-05-19 stsp break;
4301 abd2672a 2018-12-23 stsp }
4302 26ed57b2 2018-05-19 stsp default:
4303 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4304 48ae06ee 2018-10-18 stsp break;
4305 26ed57b2 2018-05-19 stsp }
4306 f44b1f58 2020-02-02 tracey if (err)
4307 f44b1f58 2020-02-02 tracey goto done;
4308 48ae06ee 2018-10-18 stsp done:
4309 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4310 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4311 48ae06ee 2018-10-18 stsp return err;
4312 48ae06ee 2018-10-18 stsp }
4313 26ed57b2 2018-05-19 stsp
4314 f5215bb9 2019-02-22 stsp static void
4315 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4316 f5215bb9 2019-02-22 stsp {
4317 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4318 f5215bb9 2019-02-22 stsp update_panels();
4319 f5215bb9 2019-02-22 stsp doupdate();
4320 f44b1f58 2020-02-02 tracey }
4321 f44b1f58 2020-02-02 tracey
4322 f44b1f58 2020-02-02 tracey static const struct got_error *
4323 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4324 f44b1f58 2020-02-02 tracey {
4325 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4326 f44b1f58 2020-02-02 tracey
4327 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4328 f44b1f58 2020-02-02 tracey return NULL;
4329 f44b1f58 2020-02-02 tracey }
4330 f44b1f58 2020-02-02 tracey
4331 f44b1f58 2020-02-02 tracey static const struct got_error *
4332 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4333 f44b1f58 2020-02-02 tracey {
4334 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4335 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4336 f44b1f58 2020-02-02 tracey int lineno;
4337 78eecffc 2022-06-23 thomas char *line = NULL;
4338 826082fe 2020-12-10 stsp size_t linesize = 0;
4339 826082fe 2020-12-10 stsp ssize_t linelen;
4340 f44b1f58 2020-02-02 tracey
4341 f44b1f58 2020-02-02 tracey if (!view->searching) {
4342 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4343 f44b1f58 2020-02-02 tracey return NULL;
4344 f44b1f58 2020-02-02 tracey }
4345 f44b1f58 2020-02-02 tracey
4346 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4347 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4348 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4349 f44b1f58 2020-02-02 tracey else
4350 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4351 4b3f9dac 2021-12-17 thomas } else
4352 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4353 f44b1f58 2020-02-02 tracey
4354 f44b1f58 2020-02-02 tracey while (1) {
4355 f44b1f58 2020-02-02 tracey off_t offset;
4356 f44b1f58 2020-02-02 tracey
4357 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4358 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4359 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4360 f44b1f58 2020-02-02 tracey break;
4361 f44b1f58 2020-02-02 tracey }
4362 f44b1f58 2020-02-02 tracey
4363 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4364 f44b1f58 2020-02-02 tracey lineno = 1;
4365 f44b1f58 2020-02-02 tracey else
4366 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4367 f44b1f58 2020-02-02 tracey }
4368 f44b1f58 2020-02-02 tracey
4369 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4370 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4371 f44b1f58 2020-02-02 tracey free(line);
4372 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4373 f44b1f58 2020-02-02 tracey }
4374 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4375 78eecffc 2022-06-23 thomas if (linelen != -1) {
4376 78eecffc 2022-06-23 thomas char *exstr;
4377 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4378 78eecffc 2022-06-23 thomas if (err)
4379 78eecffc 2022-06-23 thomas break;
4380 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4381 78eecffc 2022-06-23 thomas &view->regmatch)) {
4382 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4384 78eecffc 2022-06-23 thomas free(exstr);
4385 78eecffc 2022-06-23 thomas break;
4386 78eecffc 2022-06-23 thomas }
4387 78eecffc 2022-06-23 thomas free(exstr);
4388 f44b1f58 2020-02-02 tracey }
4389 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4390 f44b1f58 2020-02-02 tracey lineno++;
4391 f44b1f58 2020-02-02 tracey else
4392 f44b1f58 2020-02-02 tracey lineno--;
4393 f44b1f58 2020-02-02 tracey }
4394 826082fe 2020-12-10 stsp free(line);
4395 f44b1f58 2020-02-02 tracey
4396 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4397 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4398 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4399 f44b1f58 2020-02-02 tracey }
4400 f44b1f58 2020-02-02 tracey
4401 05171be4 2022-06-23 thomas return err;
4402 f5215bb9 2019-02-22 stsp }
4403 f5215bb9 2019-02-22 stsp
4404 48ae06ee 2018-10-18 stsp static const struct got_error *
4405 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4406 a0f32f33 2022-06-13 thomas {
4407 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4408 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4409 a0f32f33 2022-06-13 thomas
4410 a0f32f33 2022-06-13 thomas free(s->id1);
4411 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4412 a0f32f33 2022-06-13 thomas free(s->id2);
4413 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4414 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4415 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4416 a0f32f33 2022-06-13 thomas s->f = NULL;
4417 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4418 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4419 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4420 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4421 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4422 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4423 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4424 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4425 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4426 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4427 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4428 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4429 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4430 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4431 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4432 a0f32f33 2022-06-13 thomas s->nlines = 0;
4433 a0f32f33 2022-06-13 thomas return err;
4434 a0f32f33 2022-06-13 thomas }
4435 a0f32f33 2022-06-13 thomas
4436 a0f32f33 2022-06-13 thomas static const struct got_error *
4437 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4438 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4439 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4440 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4441 48ae06ee 2018-10-18 stsp {
4442 48ae06ee 2018-10-18 stsp const struct got_error *err;
4443 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4444 5dc9f4bc 2018-08-04 stsp
4445 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4446 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4447 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4448 a0f32f33 2022-06-13 thomas
4449 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4450 15a94983 2018-12-23 stsp int type1, type2;
4451 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4452 15a94983 2018-12-23 stsp if (err)
4453 15a94983 2018-12-23 stsp return err;
4454 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4455 15a94983 2018-12-23 stsp if (err)
4456 15a94983 2018-12-23 stsp return err;
4457 15a94983 2018-12-23 stsp
4458 15a94983 2018-12-23 stsp if (type1 != type2)
4459 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4460 15a94983 2018-12-23 stsp }
4461 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4462 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4463 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4464 f44b1f58 2020-02-02 tracey s->repo = repo;
4465 f44b1f58 2020-02-02 tracey s->id1 = id1;
4466 f44b1f58 2020-02-02 tracey s->id2 = id2;
4467 3dbaef42 2020-11-24 stsp s->label1 = label1;
4468 3dbaef42 2020-11-24 stsp s->label2 = label2;
4469 48ae06ee 2018-10-18 stsp
4470 15a94983 2018-12-23 stsp if (id1) {
4471 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4472 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4473 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4474 48ae06ee 2018-10-18 stsp } else
4475 5465d566 2020-02-01 tracey s->id1 = NULL;
4476 48ae06ee 2018-10-18 stsp
4477 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4478 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4479 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4480 a0f32f33 2022-06-13 thomas goto done;
4481 48ae06ee 2018-10-18 stsp }
4482 a0f32f33 2022-06-13 thomas
4483 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4484 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4485 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4486 9a1d7435 2022-06-23 thomas goto done;
4487 9a1d7435 2022-06-23 thomas }
4488 9a1d7435 2022-06-23 thomas
4489 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4490 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4491 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4492 a0f32f33 2022-06-13 thomas goto done;
4493 a0f32f33 2022-06-13 thomas }
4494 a0f32f33 2022-06-13 thomas
4495 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4496 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4497 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4498 19a6a6b5 2022-07-01 thomas goto done;
4499 19a6a6b5 2022-07-01 thomas }
4500 19a6a6b5 2022-07-01 thomas
4501 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4502 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4503 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4504 19a6a6b5 2022-07-01 thomas goto done;
4505 19a6a6b5 2022-07-01 thomas }
4506 19a6a6b5 2022-07-01 thomas
4507 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4508 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4509 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4510 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4511 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4512 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4513 5465d566 2020-02-01 tracey s->repo = repo;
4514 6d17833f 2019-11-08 stsp
4515 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4516 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4517 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4518 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4519 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4520 6d17833f 2019-11-08 stsp if (err)
4521 a0f32f33 2022-06-13 thomas goto done;
4522 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4523 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4524 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4525 a0f32f33 2022-06-13 thomas if (err)
4526 a0f32f33 2022-06-13 thomas goto done;
4527 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4528 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4529 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4530 a0f32f33 2022-06-13 thomas if (err)
4531 a0f32f33 2022-06-13 thomas goto done;
4532 6d17833f 2019-11-08 stsp
4533 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4534 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4535 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4536 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4537 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4538 a0f32f33 2022-06-13 thomas if (err)
4539 a0f32f33 2022-06-13 thomas goto done;
4540 11b20872 2019-11-08 stsp
4541 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4542 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4543 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4544 a0f32f33 2022-06-13 thomas if (err)
4545 a0f32f33 2022-06-13 thomas goto done;
4546 11b20872 2019-11-08 stsp
4547 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4548 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4549 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4550 a0f32f33 2022-06-13 thomas if (err)
4551 a0f32f33 2022-06-13 thomas goto done;
4552 6d17833f 2019-11-08 stsp }
4553 5dc9f4bc 2018-08-04 stsp
4554 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4555 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4556 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4557 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4558 f5215bb9 2019-02-22 stsp
4559 5465d566 2020-02-01 tracey err = create_diff(s);
4560 48ae06ee 2018-10-18 stsp
4561 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4562 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4563 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4564 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4565 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4566 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4567 a0f32f33 2022-06-13 thomas done:
4568 a0f32f33 2022-06-13 thomas if (err)
4569 a0f32f33 2022-06-13 thomas close_diff_view(view);
4570 e5a0f69f 2018-08-18 stsp return err;
4571 5dc9f4bc 2018-08-04 stsp }
4572 5dc9f4bc 2018-08-04 stsp
4573 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4574 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4575 5dc9f4bc 2018-08-04 stsp {
4576 a3404814 2018-09-02 stsp const struct got_error *err;
4577 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4578 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4579 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4580 a3404814 2018-09-02 stsp
4581 a3404814 2018-09-02 stsp if (s->id1) {
4582 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4583 a3404814 2018-09-02 stsp if (err)
4584 a3404814 2018-09-02 stsp return err;
4585 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4586 3dbaef42 2020-11-24 stsp } else
4587 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4588 3dbaef42 2020-11-24 stsp
4589 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4590 a3404814 2018-09-02 stsp if (err)
4591 a3404814 2018-09-02 stsp return err;
4592 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4593 26ed57b2 2018-05-19 stsp
4594 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4595 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4596 a3404814 2018-09-02 stsp free(id_str1);
4597 a3404814 2018-09-02 stsp free(id_str2);
4598 a3404814 2018-09-02 stsp return err;
4599 a3404814 2018-09-02 stsp }
4600 a3404814 2018-09-02 stsp free(id_str1);
4601 a3404814 2018-09-02 stsp free(id_str2);
4602 a3404814 2018-09-02 stsp
4603 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4604 267bb3b8 2021-08-01 stsp free(header);
4605 267bb3b8 2021-08-01 stsp return err;
4606 15a087fe 2019-02-21 stsp }
4607 15a087fe 2019-02-21 stsp
4608 15a087fe 2019-02-21 stsp static const struct got_error *
4609 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4610 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4611 15a087fe 2019-02-21 stsp {
4612 d7a04538 2019-02-21 stsp const struct got_error *err;
4613 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4614 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4615 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4616 15a087fe 2019-02-21 stsp
4617 15a087fe 2019-02-21 stsp free(s->id2);
4618 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4619 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4620 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4621 15a087fe 2019-02-21 stsp
4622 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4623 d7a04538 2019-02-21 stsp if (err)
4624 d7a04538 2019-02-21 stsp return err;
4625 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4626 15a087fe 2019-02-21 stsp free(s->id1);
4627 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4628 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4629 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4630 15a087fe 2019-02-21 stsp return NULL;
4631 0cf4efb1 2018-09-29 stsp }
4632 0cf4efb1 2018-09-29 stsp
4633 0cf4efb1 2018-09-29 stsp static const struct got_error *
4634 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4635 adf4c9e0 2022-07-03 thomas {
4636 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4637 adf4c9e0 2022-07-03 thomas
4638 adf4c9e0 2022-07-03 thomas view->count = 0;
4639 adf4c9e0 2022-07-03 thomas wclear(view->window);
4640 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4641 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4642 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4643 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4644 adf4c9e0 2022-07-03 thomas return create_diff(s);
4645 adf4c9e0 2022-07-03 thomas }
4646 adf4c9e0 2022-07-03 thomas
4647 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4648 4fc71f3b 2022-07-12 thomas int, int, int);
4649 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4650 4fc71f3b 2022-07-12 thomas int, int);
4651 4fc71f3b 2022-07-12 thomas
4652 adf4c9e0 2022-07-03 thomas static const struct got_error *
4653 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4654 e5a0f69f 2018-08-18 stsp {
4655 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4656 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4657 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4658 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4659 826082fe 2020-12-10 stsp char *line = NULL;
4660 826082fe 2020-12-10 stsp size_t linesize = 0;
4661 826082fe 2020-12-10 stsp ssize_t linelen;
4662 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4663 e5a0f69f 2018-08-18 stsp
4664 e5a0f69f 2018-08-18 stsp switch (ch) {
4665 05171be4 2022-06-23 thomas case '0':
4666 05171be4 2022-06-23 thomas view->x = 0;
4667 05171be4 2022-06-23 thomas break;
4668 05171be4 2022-06-23 thomas case '$':
4669 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4670 07b0611c 2022-06-23 thomas view->count = 0;
4671 05171be4 2022-06-23 thomas break;
4672 05171be4 2022-06-23 thomas case KEY_RIGHT:
4673 05171be4 2022-06-23 thomas case 'l':
4674 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4675 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4676 07b0611c 2022-06-23 thomas else
4677 07b0611c 2022-06-23 thomas view->count = 0;
4678 05171be4 2022-06-23 thomas break;
4679 05171be4 2022-06-23 thomas case KEY_LEFT:
4680 05171be4 2022-06-23 thomas case 'h':
4681 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4682 07b0611c 2022-06-23 thomas if (view->x <= 0)
4683 07b0611c 2022-06-23 thomas view->count = 0;
4684 05171be4 2022-06-23 thomas break;
4685 64453f7e 2020-11-21 stsp case 'a':
4686 3dbaef42 2020-11-24 stsp case 'w':
4687 3dbaef42 2020-11-24 stsp if (ch == 'a')
4688 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4689 3dbaef42 2020-11-24 stsp if (ch == 'w')
4690 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4691 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4692 912a3f79 2021-08-30 j break;
4693 912a3f79 2021-08-30 j case 'g':
4694 912a3f79 2021-08-30 j case KEY_HOME:
4695 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4696 07b0611c 2022-06-23 thomas view->count = 0;
4697 64453f7e 2020-11-21 stsp break;
4698 912a3f79 2021-08-30 j case 'G':
4699 912a3f79 2021-08-30 j case KEY_END:
4700 07b0611c 2022-06-23 thomas view->count = 0;
4701 912a3f79 2021-08-30 j if (s->eof)
4702 912a3f79 2021-08-30 j break;
4703 912a3f79 2021-08-30 j
4704 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4705 912a3f79 2021-08-30 j s->eof = 1;
4706 912a3f79 2021-08-30 j break;
4707 1e37a5c2 2019-05-12 jcs case 'k':
4708 1e37a5c2 2019-05-12 jcs case KEY_UP:
4709 f7140bf5 2021-10-17 thomas case CTRL('p'):
4710 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4711 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4712 07b0611c 2022-06-23 thomas else
4713 07b0611c 2022-06-23 thomas view->count = 0;
4714 1e37a5c2 2019-05-12 jcs break;
4715 70f17a53 2022-06-13 thomas case CTRL('u'):
4716 23427b14 2022-06-23 thomas case 'u':
4717 70f17a53 2022-06-13 thomas nscroll /= 2;
4718 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4719 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4720 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4721 1c5e5faa 2022-06-23 thomas case 'b':
4722 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4723 07b0611c 2022-06-23 thomas view->count = 0;
4724 26ed57b2 2018-05-19 stsp break;
4725 07b0611c 2022-06-23 thomas }
4726 1e37a5c2 2019-05-12 jcs i = 0;
4727 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4728 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4729 1e37a5c2 2019-05-12 jcs break;
4730 1e37a5c2 2019-05-12 jcs case 'j':
4731 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4732 f7140bf5 2021-10-17 thomas case CTRL('n'):
4733 1e37a5c2 2019-05-12 jcs if (!s->eof)
4734 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4735 07b0611c 2022-06-23 thomas else
4736 07b0611c 2022-06-23 thomas view->count = 0;
4737 1e37a5c2 2019-05-12 jcs break;
4738 70f17a53 2022-06-13 thomas case CTRL('d'):
4739 23427b14 2022-06-23 thomas case 'd':
4740 70f17a53 2022-06-13 thomas nscroll /= 2;
4741 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4742 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4743 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4744 1c5e5faa 2022-06-23 thomas case 'f':
4745 1e37a5c2 2019-05-12 jcs case ' ':
4746 07b0611c 2022-06-23 thomas if (s->eof) {
4747 07b0611c 2022-06-23 thomas view->count = 0;
4748 1e37a5c2 2019-05-12 jcs break;
4749 07b0611c 2022-06-23 thomas }
4750 1e37a5c2 2019-05-12 jcs i = 0;
4751 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4752 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4753 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4754 826082fe 2020-12-10 stsp if (linelen == -1) {
4755 826082fe 2020-12-10 stsp if (feof(s->f)) {
4756 826082fe 2020-12-10 stsp s->eof = 1;
4757 826082fe 2020-12-10 stsp } else
4758 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4759 34bc9ec9 2019-02-22 stsp break;
4760 826082fe 2020-12-10 stsp }
4761 1e37a5c2 2019-05-12 jcs }
4762 826082fe 2020-12-10 stsp free(line);
4763 1e37a5c2 2019-05-12 jcs break;
4764 1e37a5c2 2019-05-12 jcs case '[':
4765 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4766 1e37a5c2 2019-05-12 jcs s->diff_context--;
4767 aa61903a 2021-12-31 thomas s->matched_line = 0;
4768 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4769 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4770 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4771 27829c9e 2020-11-21 stsp s->nlines) {
4772 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4773 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4774 27829c9e 2020-11-21 stsp }
4775 07b0611c 2022-06-23 thomas } else
4776 07b0611c 2022-06-23 thomas view->count = 0;
4777 1e37a5c2 2019-05-12 jcs break;
4778 1e37a5c2 2019-05-12 jcs case ']':
4779 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4780 1e37a5c2 2019-05-12 jcs s->diff_context++;
4781 aa61903a 2021-12-31 thomas s->matched_line = 0;
4782 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4783 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4784 07b0611c 2022-06-23 thomas } else
4785 07b0611c 2022-06-23 thomas view->count = 0;
4786 1e37a5c2 2019-05-12 jcs break;
4787 1e37a5c2 2019-05-12 jcs case '<':
4788 1e37a5c2 2019-05-12 jcs case ',':
4789 777aae21 2022-07-20 thomas case 'K':
4790 4fc71f3b 2022-07-12 thomas up = 1;
4791 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4792 4fc71f3b 2022-07-12 thomas case '>':
4793 4fc71f3b 2022-07-12 thomas case '.':
4794 777aae21 2022-07-20 thomas case 'J':
4795 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4796 07b0611c 2022-06-23 thomas view->count = 0;
4797 48ae06ee 2018-10-18 stsp break;
4798 07b0611c 2022-06-23 thomas }
4799 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4800 6524637e 2019-02-21 stsp
4801 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4802 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4803 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4804 15a087fe 2019-02-21 stsp
4805 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4806 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4807 4fc71f3b 2022-07-12 thomas if (err)
4808 4fc71f3b 2022-07-12 thomas break;
4809 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4810 15a087fe 2019-02-21 stsp
4811 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4812 4fc71f3b 2022-07-12 thomas break;
4813 15a087fe 2019-02-21 stsp
4814 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4815 4fc71f3b 2022-07-12 thomas if (err)
4816 4fc71f3b 2022-07-12 thomas break;
4817 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4818 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4819 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4820 5e224a3e 2019-02-22 stsp
4821 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4822 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4823 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4824 4fc71f3b 2022-07-12 thomas
4825 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4826 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4827 4fc71f3b 2022-07-12 thomas if (err)
4828 4fc71f3b 2022-07-12 thomas break;
4829 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4830 5a8b5076 2020-12-05 stsp
4831 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4832 4fc71f3b 2022-07-12 thomas break;
4833 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4834 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4835 4fc71f3b 2022-07-12 thomas bs->selected_line);
4836 4fc71f3b 2022-07-12 thomas if (id == NULL)
4837 4fc71f3b 2022-07-12 thomas break;
4838 15a087fe 2019-02-21 stsp
4839 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4840 4fc71f3b 2022-07-12 thomas break;
4841 15a087fe 2019-02-21 stsp
4842 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4843 4fc71f3b 2022-07-12 thomas if (err)
4844 4fc71f3b 2022-07-12 thomas break;
4845 4fc71f3b 2022-07-12 thomas }
4846 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4847 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4848 aa61903a 2021-12-31 thomas s->matched_line = 0;
4849 05171be4 2022-06-23 thomas view->x = 0;
4850 1e37a5c2 2019-05-12 jcs
4851 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4852 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4853 1e37a5c2 2019-05-12 jcs break;
4854 1e37a5c2 2019-05-12 jcs default:
4855 07b0611c 2022-06-23 thomas view->count = 0;
4856 1e37a5c2 2019-05-12 jcs break;
4857 26ed57b2 2018-05-19 stsp }
4858 e5a0f69f 2018-08-18 stsp
4859 bcbd79e2 2018-08-19 stsp return err;
4860 26ed57b2 2018-05-19 stsp }
4861 26ed57b2 2018-05-19 stsp
4862 4ed7e80c 2018-05-20 stsp static const struct got_error *
4863 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4864 9f7d7167 2018-04-29 stsp {
4865 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4866 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4867 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4868 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4869 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4870 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4871 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4872 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4873 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4874 3dbaef42 2020-11-24 stsp const char *errstr;
4875 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4876 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4877 70ac5f84 2019-03-28 stsp
4878 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4879 26ed57b2 2018-05-19 stsp switch (ch) {
4880 64453f7e 2020-11-21 stsp case 'a':
4881 64453f7e 2020-11-21 stsp force_text_diff = 1;
4882 3dbaef42 2020-11-24 stsp break;
4883 3dbaef42 2020-11-24 stsp case 'C':
4884 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4885 3dbaef42 2020-11-24 stsp &errstr);
4886 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4887 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4888 8f666e67 2022-02-12 thomas errstr, errstr);
4889 64453f7e 2020-11-21 stsp break;
4890 09b5bff8 2020-02-23 naddy case 'r':
4891 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4892 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4893 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4894 09b5bff8 2020-02-23 naddy optarg);
4895 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4896 09b5bff8 2020-02-23 naddy break;
4897 3dbaef42 2020-11-24 stsp case 'w':
4898 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4899 3dbaef42 2020-11-24 stsp break;
4900 26ed57b2 2018-05-19 stsp default:
4901 17020d27 2019-03-07 stsp usage_diff();
4902 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4903 26ed57b2 2018-05-19 stsp }
4904 26ed57b2 2018-05-19 stsp }
4905 26ed57b2 2018-05-19 stsp
4906 26ed57b2 2018-05-19 stsp argc -= optind;
4907 26ed57b2 2018-05-19 stsp argv += optind;
4908 26ed57b2 2018-05-19 stsp
4909 26ed57b2 2018-05-19 stsp if (argc == 0) {
4910 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4911 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4912 15a94983 2018-12-23 stsp id_str1 = argv[0];
4913 15a94983 2018-12-23 stsp id_str2 = argv[1];
4914 26ed57b2 2018-05-19 stsp } else
4915 26ed57b2 2018-05-19 stsp usage_diff();
4916 eb6600df 2019-01-04 stsp
4917 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4918 7cd52833 2022-06-23 thomas if (error)
4919 7cd52833 2022-06-23 thomas goto done;
4920 7cd52833 2022-06-23 thomas
4921 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4922 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4923 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4924 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4925 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4926 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4927 c156c7a4 2020-12-18 stsp goto done;
4928 a273ac94 2020-02-23 naddy if (worktree)
4929 a273ac94 2020-02-23 naddy repo_path =
4930 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4931 a273ac94 2020-02-23 naddy else
4932 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4933 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4934 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4935 c156c7a4 2020-12-18 stsp goto done;
4936 c156c7a4 2020-12-18 stsp }
4937 a273ac94 2020-02-23 naddy }
4938 a273ac94 2020-02-23 naddy
4939 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4940 eb6600df 2019-01-04 stsp if (error)
4941 eb6600df 2019-01-04 stsp goto done;
4942 26ed57b2 2018-05-19 stsp
4943 a273ac94 2020-02-23 naddy init_curses();
4944 a273ac94 2020-02-23 naddy
4945 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4946 51a10b52 2020-12-26 stsp if (error)
4947 51a10b52 2020-12-26 stsp goto done;
4948 51a10b52 2020-12-26 stsp
4949 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4950 26ed57b2 2018-05-19 stsp if (error)
4951 26ed57b2 2018-05-19 stsp goto done;
4952 26ed57b2 2018-05-19 stsp
4953 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4954 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4955 26ed57b2 2018-05-19 stsp if (error)
4956 26ed57b2 2018-05-19 stsp goto done;
4957 26ed57b2 2018-05-19 stsp
4958 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4959 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4960 26ed57b2 2018-05-19 stsp if (error)
4961 26ed57b2 2018-05-19 stsp goto done;
4962 26ed57b2 2018-05-19 stsp
4963 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4964 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4965 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4966 ea5e7bb5 2018-08-01 stsp goto done;
4967 ea5e7bb5 2018-08-01 stsp }
4968 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4969 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4970 5dc9f4bc 2018-08-04 stsp if (error)
4971 5dc9f4bc 2018-08-04 stsp goto done;
4972 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4973 26ed57b2 2018-05-19 stsp done:
4974 3dbaef42 2020-11-24 stsp free(label1);
4975 3dbaef42 2020-11-24 stsp free(label2);
4976 c02c541e 2019-03-29 stsp free(repo_path);
4977 a273ac94 2020-02-23 naddy free(cwd);
4978 1d0f4054 2021-06-17 stsp if (repo) {
4979 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4980 1d0f4054 2021-06-17 stsp if (error == NULL)
4981 1d0f4054 2021-06-17 stsp error = close_err;
4982 1d0f4054 2021-06-17 stsp }
4983 a273ac94 2020-02-23 naddy if (worktree)
4984 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4985 7cd52833 2022-06-23 thomas if (pack_fds) {
4986 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4987 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4988 7cd52833 2022-06-23 thomas if (error == NULL)
4989 7cd52833 2022-06-23 thomas error = pack_err;
4990 7cd52833 2022-06-23 thomas }
4991 51a10b52 2020-12-26 stsp tog_free_refs();
4992 26ed57b2 2018-05-19 stsp return error;
4993 9f7d7167 2018-04-29 stsp }
4994 9f7d7167 2018-04-29 stsp
4995 4ed7e80c 2018-05-20 stsp __dead static void
4996 9f7d7167 2018-04-29 stsp usage_blame(void)
4997 9f7d7167 2018-04-29 stsp {
4998 80ddbec8 2018-04-29 stsp endwin();
4999 91198554 2022-06-23 thomas fprintf(stderr,
5000 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5001 9f7d7167 2018-04-29 stsp getprogname());
5002 9f7d7167 2018-04-29 stsp exit(1);
5003 9f7d7167 2018-04-29 stsp }
5004 84451b3e 2018-07-10 stsp
5005 84451b3e 2018-07-10 stsp struct tog_blame_line {
5006 84451b3e 2018-07-10 stsp int annotated;
5007 84451b3e 2018-07-10 stsp struct got_object_id *id;
5008 84451b3e 2018-07-10 stsp };
5009 9f7d7167 2018-04-29 stsp
5010 4ed7e80c 2018-05-20 stsp static const struct got_error *
5011 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5012 84451b3e 2018-07-10 stsp {
5013 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5014 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5015 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5016 84451b3e 2018-07-10 stsp const struct got_error *err;
5017 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5018 826082fe 2020-12-10 stsp char *line = NULL;
5019 826082fe 2020-12-10 stsp size_t linesize = 0;
5020 826082fe 2020-12-10 stsp ssize_t linelen;
5021 84451b3e 2018-07-10 stsp wchar_t *wline;
5022 27a741e5 2019-09-11 stsp int width;
5023 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5024 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5025 ab089a2a 2018-07-12 stsp char *id_str;
5026 11b20872 2019-11-08 stsp struct tog_color *tc;
5027 ab089a2a 2018-07-12 stsp
5028 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5029 ab089a2a 2018-07-12 stsp if (err)
5030 ab089a2a 2018-07-12 stsp return err;
5031 84451b3e 2018-07-10 stsp
5032 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5033 f7d12f7e 2018-08-01 stsp werase(view->window);
5034 84451b3e 2018-07-10 stsp
5035 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5036 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5037 ab089a2a 2018-07-12 stsp free(id_str);
5038 ab089a2a 2018-07-12 stsp return err;
5039 ab089a2a 2018-07-12 stsp }
5040 ab089a2a 2018-07-12 stsp
5041 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5042 ab089a2a 2018-07-12 stsp free(line);
5043 2550e4c3 2018-07-13 stsp line = NULL;
5044 1cae65b4 2019-09-22 stsp if (err)
5045 1cae65b4 2019-09-22 stsp return err;
5046 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5047 a3404814 2018-09-02 stsp wstandout(view->window);
5048 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5049 11b20872 2019-11-08 stsp if (tc)
5050 11b20872 2019-11-08 stsp wattr_on(view->window,
5051 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5052 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5053 11b20872 2019-11-08 stsp if (tc)
5054 11b20872 2019-11-08 stsp wattr_off(view->window,
5055 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5056 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5057 a3404814 2018-09-02 stsp wstandend(view->window);
5058 2550e4c3 2018-07-13 stsp free(wline);
5059 2550e4c3 2018-07-13 stsp wline = NULL;
5060 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5061 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5062 ab089a2a 2018-07-12 stsp
5063 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5064 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5065 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5066 ab089a2a 2018-07-12 stsp free(id_str);
5067 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5068 ab089a2a 2018-07-12 stsp }
5069 ab089a2a 2018-07-12 stsp free(id_str);
5070 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5071 3f60a8ef 2018-07-10 stsp free(line);
5072 2550e4c3 2018-07-13 stsp line = NULL;
5073 3f60a8ef 2018-07-10 stsp if (err)
5074 3f60a8ef 2018-07-10 stsp return err;
5075 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5076 2550e4c3 2018-07-13 stsp free(wline);
5077 2550e4c3 2018-07-13 stsp wline = NULL;
5078 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5079 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5080 3f60a8ef 2018-07-10 stsp
5081 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5082 05171be4 2022-06-23 thomas view->maxx = 0;
5083 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5084 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5085 826082fe 2020-12-10 stsp if (linelen == -1) {
5086 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5087 826082fe 2020-12-10 stsp s->eof = 1;
5088 826082fe 2020-12-10 stsp break;
5089 826082fe 2020-12-10 stsp }
5090 84451b3e 2018-07-10 stsp free(line);
5091 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5092 84451b3e 2018-07-10 stsp }
5093 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5094 826082fe 2020-12-10 stsp continue;
5095 cbea3800 2022-06-23 thomas
5096 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5097 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5098 cbea3800 2022-06-23 thomas if (err) {
5099 cbea3800 2022-06-23 thomas free(line);
5100 cbea3800 2022-06-23 thomas return err;
5101 cbea3800 2022-06-23 thomas }
5102 cbea3800 2022-06-23 thomas free(wline);
5103 cbea3800 2022-06-23 thomas wline = NULL;
5104 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5105 84451b3e 2018-07-10 stsp
5106 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5107 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5108 b700b5d6 2018-07-10 stsp
5109 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5110 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5111 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5112 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5113 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5114 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5115 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5116 8d0fe45a 2019-08-12 stsp char *id_str;
5117 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5118 91198554 2022-06-23 thomas blame_line->id);
5119 8d0fe45a 2019-08-12 stsp if (err) {
5120 8d0fe45a 2019-08-12 stsp free(line);
5121 8d0fe45a 2019-08-12 stsp return err;
5122 8d0fe45a 2019-08-12 stsp }
5123 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5124 11b20872 2019-11-08 stsp if (tc)
5125 11b20872 2019-11-08 stsp wattr_on(view->window,
5126 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5127 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5128 11b20872 2019-11-08 stsp if (tc)
5129 11b20872 2019-11-08 stsp wattr_off(view->window,
5130 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5131 8d0fe45a 2019-08-12 stsp free(id_str);
5132 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5133 8d0fe45a 2019-08-12 stsp } else {
5134 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5135 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5136 84451b3e 2018-07-10 stsp }
5137 ee41ec32 2018-07-10 stsp } else {
5138 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5139 ee41ec32 2018-07-10 stsp prev_id = NULL;
5140 ee41ec32 2018-07-10 stsp }
5141 84451b3e 2018-07-10 stsp
5142 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5143 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5144 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5145 27a741e5 2019-09-11 stsp
5146 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5147 41605754 2020-11-12 stsp width = 9;
5148 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5149 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5150 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5151 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5152 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5153 41605754 2020-11-12 stsp if (err) {
5154 41605754 2020-11-12 stsp free(line);
5155 41605754 2020-11-12 stsp return err;
5156 41605754 2020-11-12 stsp }
5157 41605754 2020-11-12 stsp width += 9;
5158 41605754 2020-11-12 stsp } else {
5159 923086fd 2022-06-23 thomas int skip;
5160 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5161 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5162 923086fd 2022-06-23 thomas if (err) {
5163 923086fd 2022-06-23 thomas free(line);
5164 923086fd 2022-06-23 thomas return err;
5165 05171be4 2022-06-23 thomas }
5166 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5167 02bce7e2 2022-06-23 thomas width += 9;
5168 41605754 2020-11-12 stsp free(wline);
5169 41605754 2020-11-12 stsp wline = NULL;
5170 41605754 2020-11-12 stsp }
5171 41605754 2020-11-12 stsp
5172 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5173 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5174 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5175 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5176 84451b3e 2018-07-10 stsp }
5177 826082fe 2020-12-10 stsp free(line);
5178 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5179 84451b3e 2018-07-10 stsp
5180 a5d43cac 2022-07-01 thomas view_border(view);
5181 84451b3e 2018-07-10 stsp
5182 84451b3e 2018-07-10 stsp return NULL;
5183 84451b3e 2018-07-10 stsp }
5184 84451b3e 2018-07-10 stsp
5185 84451b3e 2018-07-10 stsp static const struct got_error *
5186 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5187 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5188 84451b3e 2018-07-10 stsp {
5189 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5190 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5191 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5192 1a76625f 2018-10-22 stsp int errcode;
5193 84451b3e 2018-07-10 stsp
5194 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5195 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5196 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5197 84451b3e 2018-07-10 stsp
5198 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5199 1a76625f 2018-10-22 stsp if (errcode)
5200 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5201 84451b3e 2018-07-10 stsp
5202 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5203 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5204 d68a0a7d 2018-07-10 stsp goto done;
5205 d68a0a7d 2018-07-10 stsp }
5206 d68a0a7d 2018-07-10 stsp
5207 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5208 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5209 d68a0a7d 2018-07-10 stsp
5210 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5211 d68a0a7d 2018-07-10 stsp if (line->annotated)
5212 d68a0a7d 2018-07-10 stsp goto done;
5213 d68a0a7d 2018-07-10 stsp
5214 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5215 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5216 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5217 84451b3e 2018-07-10 stsp goto done;
5218 84451b3e 2018-07-10 stsp }
5219 84451b3e 2018-07-10 stsp line->annotated = 1;
5220 84451b3e 2018-07-10 stsp done:
5221 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5222 1a76625f 2018-10-22 stsp if (errcode)
5223 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5224 84451b3e 2018-07-10 stsp return err;
5225 84451b3e 2018-07-10 stsp }
5226 84451b3e 2018-07-10 stsp
5227 84451b3e 2018-07-10 stsp static void *
5228 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5229 84451b3e 2018-07-10 stsp {
5230 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5231 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5232 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5233 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5234 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5235 00a8878e 2022-07-01 thomas
5236 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5237 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5238 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5239 9117a7b7 2022-07-01 thomas
5240 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5241 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5242 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5243 9117a7b7 2022-07-01 thomas goto done;
5244 9117a7b7 2022-07-01 thomas }
5245 18430de3 2018-07-10 stsp
5246 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5247 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5248 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5249 9117a7b7 2022-07-01 thomas goto done;
5250 9117a7b7 2022-07-01 thomas }
5251 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5252 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5253 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5254 9117a7b7 2022-07-01 thomas goto done;
5255 9117a7b7 2022-07-01 thomas }
5256 9117a7b7 2022-07-01 thomas
5257 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5258 61266923 2020-01-14 stsp if (err)
5259 9117a7b7 2022-07-01 thomas goto done;
5260 61266923 2020-01-14 stsp
5261 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5262 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5263 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5264 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5265 fc06ba56 2019-08-22 stsp err = NULL;
5266 18430de3 2018-07-10 stsp
5267 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5268 9117a7b7 2022-07-01 thomas if (errcode) {
5269 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5270 9117a7b7 2022-07-01 thomas goto done;
5271 9117a7b7 2022-07-01 thomas }
5272 18430de3 2018-07-10 stsp
5273 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5274 1d0f4054 2021-06-17 stsp if (err == NULL)
5275 1d0f4054 2021-06-17 stsp err = close_err;
5276 c9beca56 2018-07-22 stsp ta->repo = NULL;
5277 c9beca56 2018-07-22 stsp *ta->complete = 1;
5278 18430de3 2018-07-10 stsp
5279 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5280 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5281 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5282 18430de3 2018-07-10 stsp
5283 9117a7b7 2022-07-01 thomas done:
5284 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5285 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5286 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5287 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5288 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5289 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5290 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5291 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5292 00a8878e 2022-07-01 thomas
5293 18430de3 2018-07-10 stsp return (void *)err;
5294 84451b3e 2018-07-10 stsp }
5295 84451b3e 2018-07-10 stsp
5296 245d91c1 2018-07-12 stsp static struct got_object_id *
5297 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5298 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5299 245d91c1 2018-07-12 stsp {
5300 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5301 8d0fe45a 2019-08-12 stsp
5302 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5303 8d0fe45a 2019-08-12 stsp return NULL;
5304 b880a918 2018-07-10 stsp
5305 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5306 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5307 4fc71f3b 2022-07-12 thomas return NULL;
5308 4fc71f3b 2022-07-12 thomas
5309 4fc71f3b 2022-07-12 thomas return line->id;
5310 4fc71f3b 2022-07-12 thomas }
5311 4fc71f3b 2022-07-12 thomas
5312 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5313 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5314 4fc71f3b 2022-07-12 thomas int lineno)
5315 4fc71f3b 2022-07-12 thomas {
5316 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5317 4fc71f3b 2022-07-12 thomas
5318 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5319 4fc71f3b 2022-07-12 thomas return NULL;
5320 4fc71f3b 2022-07-12 thomas
5321 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5322 245d91c1 2018-07-12 stsp if (!line->annotated)
5323 245d91c1 2018-07-12 stsp return NULL;
5324 245d91c1 2018-07-12 stsp
5325 245d91c1 2018-07-12 stsp return line->id;
5326 b880a918 2018-07-10 stsp }
5327 245d91c1 2018-07-12 stsp
5328 b880a918 2018-07-10 stsp static const struct got_error *
5329 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5330 a70480e0 2018-06-23 stsp {
5331 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5332 245d91c1 2018-07-12 stsp int i;
5333 245d91c1 2018-07-12 stsp
5334 245d91c1 2018-07-12 stsp if (blame->thread) {
5335 1a76625f 2018-10-22 stsp int errcode;
5336 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5337 1a76625f 2018-10-22 stsp if (errcode)
5338 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5339 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5340 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5341 1a76625f 2018-10-22 stsp if (errcode)
5342 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5343 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5344 1a76625f 2018-10-22 stsp if (errcode)
5345 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5346 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5347 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5348 245d91c1 2018-07-12 stsp err = NULL;
5349 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5350 245d91c1 2018-07-12 stsp }
5351 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5352 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5353 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5354 1d0f4054 2021-06-17 stsp if (err == NULL)
5355 1d0f4054 2021-06-17 stsp err = close_err;
5356 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5357 245d91c1 2018-07-12 stsp }
5358 245d91c1 2018-07-12 stsp if (blame->f) {
5359 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5360 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5361 245d91c1 2018-07-12 stsp blame->f = NULL;
5362 245d91c1 2018-07-12 stsp }
5363 57670559 2018-12-23 stsp if (blame->lines) {
5364 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5365 57670559 2018-12-23 stsp free(blame->lines[i].id);
5366 57670559 2018-12-23 stsp free(blame->lines);
5367 57670559 2018-12-23 stsp blame->lines = NULL;
5368 57670559 2018-12-23 stsp }
5369 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5370 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5371 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5372 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5373 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5374 7cd52833 2022-06-23 thomas if (err == NULL)
5375 7cd52833 2022-06-23 thomas err = pack_err;
5376 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5377 7cd52833 2022-06-23 thomas }
5378 245d91c1 2018-07-12 stsp return err;
5379 245d91c1 2018-07-12 stsp }
5380 245d91c1 2018-07-12 stsp
5381 245d91c1 2018-07-12 stsp static const struct got_error *
5382 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5383 fc06ba56 2019-08-22 stsp {
5384 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5385 fc06ba56 2019-08-22 stsp int *done = arg;
5386 fc06ba56 2019-08-22 stsp int errcode;
5387 fc06ba56 2019-08-22 stsp
5388 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5389 fc06ba56 2019-08-22 stsp if (errcode)
5390 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5391 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5392 fc06ba56 2019-08-22 stsp
5393 fc06ba56 2019-08-22 stsp if (*done)
5394 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5395 fc06ba56 2019-08-22 stsp
5396 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5397 fc06ba56 2019-08-22 stsp if (errcode)
5398 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5399 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5400 fc06ba56 2019-08-22 stsp
5401 fc06ba56 2019-08-22 stsp return err;
5402 fc06ba56 2019-08-22 stsp }
5403 fc06ba56 2019-08-22 stsp
5404 fc06ba56 2019-08-22 stsp static const struct got_error *
5405 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5406 245d91c1 2018-07-12 stsp {
5407 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5408 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5409 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5410 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5411 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5412 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5413 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5414 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5415 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5416 a70480e0 2018-06-23 stsp
5417 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5418 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5419 27d434c2 2018-09-15 stsp if (err)
5420 15a94983 2018-12-23 stsp return err;
5421 f4ae6ddb 2022-07-01 thomas
5422 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5423 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5424 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5425 f4ae6ddb 2022-07-01 thomas goto done;
5426 f4ae6ddb 2022-07-01 thomas }
5427 945f9229 2022-04-16 thomas
5428 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5429 945f9229 2022-04-16 thomas if (err)
5430 945f9229 2022-04-16 thomas goto done;
5431 27d434c2 2018-09-15 stsp
5432 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5433 84451b3e 2018-07-10 stsp if (err)
5434 84451b3e 2018-07-10 stsp goto done;
5435 27d434c2 2018-09-15 stsp
5436 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5437 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5438 84451b3e 2018-07-10 stsp goto done;
5439 84451b3e 2018-07-10 stsp }
5440 a70480e0 2018-06-23 stsp
5441 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5442 a70480e0 2018-06-23 stsp if (err)
5443 a70480e0 2018-06-23 stsp goto done;
5444 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5445 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5446 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5447 84451b3e 2018-07-10 stsp goto done;
5448 84451b3e 2018-07-10 stsp }
5449 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5450 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5451 1fddf795 2021-01-20 stsp if (err)
5452 1fddf795 2021-01-20 stsp goto done;
5453 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5454 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5455 84451b3e 2018-07-10 stsp goto done;
5456 1fddf795 2021-01-20 stsp }
5457 b02560ec 2019-08-19 stsp
5458 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5459 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5460 b02560ec 2019-08-19 stsp blame->nlines--;
5461 a70480e0 2018-06-23 stsp
5462 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5463 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5464 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5465 84451b3e 2018-07-10 stsp goto done;
5466 84451b3e 2018-07-10 stsp }
5467 a70480e0 2018-06-23 stsp
5468 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5469 bd24772e 2018-07-11 stsp if (err)
5470 bd24772e 2018-07-11 stsp goto done;
5471 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5472 7cd52833 2022-06-23 thomas pack_fds);
5473 7cd52833 2022-06-23 thomas if (err)
5474 7cd52833 2022-06-23 thomas goto done;
5475 bd24772e 2018-07-11 stsp
5476 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5477 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5478 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5479 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5480 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5481 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5482 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5483 245d91c1 2018-07-12 stsp goto done;
5484 245d91c1 2018-07-12 stsp }
5485 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5486 245d91c1 2018-07-12 stsp
5487 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5488 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5489 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5490 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5491 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5492 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5493 a5388363 2020-12-01 naddy s->blame_complete = 0;
5494 f5a09613 2020-12-13 naddy
5495 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5496 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5497 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5498 f5a09613 2020-12-13 naddy s->selected_line = 1;
5499 f5a09613 2020-12-13 naddy }
5500 aa61903a 2021-12-31 thomas s->matched_line = 0;
5501 245d91c1 2018-07-12 stsp
5502 245d91c1 2018-07-12 stsp done:
5503 945f9229 2022-04-16 thomas if (commit)
5504 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5505 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5506 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5507 245d91c1 2018-07-12 stsp if (blob)
5508 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5509 27d434c2 2018-09-15 stsp free(obj_id);
5510 245d91c1 2018-07-12 stsp if (err)
5511 245d91c1 2018-07-12 stsp stop_blame(blame);
5512 245d91c1 2018-07-12 stsp return err;
5513 245d91c1 2018-07-12 stsp }
5514 245d91c1 2018-07-12 stsp
5515 245d91c1 2018-07-12 stsp static const struct got_error *
5516 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5517 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5518 245d91c1 2018-07-12 stsp {
5519 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5520 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5521 dbc6a6b6 2018-07-12 stsp
5522 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5523 245d91c1 2018-07-12 stsp
5524 c4843652 2019-08-12 stsp s->path = strdup(path);
5525 c4843652 2019-08-12 stsp if (s->path == NULL)
5526 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5527 c4843652 2019-08-12 stsp
5528 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5529 c4843652 2019-08-12 stsp if (err) {
5530 c4843652 2019-08-12 stsp free(s->path);
5531 7cbe629d 2018-08-04 stsp return err;
5532 c4843652 2019-08-12 stsp }
5533 245d91c1 2018-07-12 stsp
5534 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5535 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5536 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5537 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5538 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5539 fb2756b9 2018-08-04 stsp s->repo = repo;
5540 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5541 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5542 7cbe629d 2018-08-04 stsp
5543 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5544 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5545 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5546 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5547 11b20872 2019-11-08 stsp if (err)
5548 11b20872 2019-11-08 stsp return err;
5549 11b20872 2019-11-08 stsp }
5550 11b20872 2019-11-08 stsp
5551 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5552 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5553 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5554 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5555 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5556 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5557 e5a0f69f 2018-08-18 stsp
5558 a5388363 2020-12-01 naddy return run_blame(view);
5559 7cbe629d 2018-08-04 stsp }
5560 7cbe629d 2018-08-04 stsp
5561 e5a0f69f 2018-08-18 stsp static const struct got_error *
5562 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5563 7cbe629d 2018-08-04 stsp {
5564 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5565 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5566 7cbe629d 2018-08-04 stsp
5567 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5568 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5569 e5a0f69f 2018-08-18 stsp
5570 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5571 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5572 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5573 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5574 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5575 7cbe629d 2018-08-04 stsp }
5576 e5a0f69f 2018-08-18 stsp
5577 e5a0f69f 2018-08-18 stsp free(s->path);
5578 11b20872 2019-11-08 stsp free_colors(&s->colors);
5579 e5a0f69f 2018-08-18 stsp return err;
5580 7cbe629d 2018-08-04 stsp }
5581 7cbe629d 2018-08-04 stsp
5582 7cbe629d 2018-08-04 stsp static const struct got_error *
5583 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5584 6c4c42e0 2019-06-24 stsp {
5585 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5586 6c4c42e0 2019-06-24 stsp
5587 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5588 6c4c42e0 2019-06-24 stsp return NULL;
5589 6c4c42e0 2019-06-24 stsp }
5590 6c4c42e0 2019-06-24 stsp
5591 6c4c42e0 2019-06-24 stsp static const struct got_error *
5592 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5593 6c4c42e0 2019-06-24 stsp {
5594 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5595 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5596 6c4c42e0 2019-06-24 stsp int lineno;
5597 78eecffc 2022-06-23 thomas char *line = NULL;
5598 826082fe 2020-12-10 stsp size_t linesize = 0;
5599 826082fe 2020-12-10 stsp ssize_t linelen;
5600 6c4c42e0 2019-06-24 stsp
5601 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5602 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5603 6c4c42e0 2019-06-24 stsp return NULL;
5604 6c4c42e0 2019-06-24 stsp }
5605 6c4c42e0 2019-06-24 stsp
5606 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5607 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5608 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5609 6c4c42e0 2019-06-24 stsp else
5610 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5611 4b3f9dac 2021-12-17 thomas } else
5612 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5613 6c4c42e0 2019-06-24 stsp
5614 6c4c42e0 2019-06-24 stsp while (1) {
5615 6c4c42e0 2019-06-24 stsp off_t offset;
5616 6c4c42e0 2019-06-24 stsp
5617 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5618 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5619 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5620 6c4c42e0 2019-06-24 stsp break;
5621 6c4c42e0 2019-06-24 stsp }
5622 2246482e 2019-06-25 stsp
5623 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5624 6c4c42e0 2019-06-24 stsp lineno = 1;
5625 6c4c42e0 2019-06-24 stsp else
5626 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5627 6c4c42e0 2019-06-24 stsp }
5628 6c4c42e0 2019-06-24 stsp
5629 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5630 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5631 6c4c42e0 2019-06-24 stsp free(line);
5632 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5633 6c4c42e0 2019-06-24 stsp }
5634 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5635 78eecffc 2022-06-23 thomas if (linelen != -1) {
5636 78eecffc 2022-06-23 thomas char *exstr;
5637 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5638 78eecffc 2022-06-23 thomas if (err)
5639 78eecffc 2022-06-23 thomas break;
5640 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5641 78eecffc 2022-06-23 thomas &view->regmatch)) {
5642 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5643 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5644 78eecffc 2022-06-23 thomas free(exstr);
5645 78eecffc 2022-06-23 thomas break;
5646 78eecffc 2022-06-23 thomas }
5647 78eecffc 2022-06-23 thomas free(exstr);
5648 6c4c42e0 2019-06-24 stsp }
5649 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5650 6c4c42e0 2019-06-24 stsp lineno++;
5651 6c4c42e0 2019-06-24 stsp else
5652 6c4c42e0 2019-06-24 stsp lineno--;
5653 6c4c42e0 2019-06-24 stsp }
5654 826082fe 2020-12-10 stsp free(line);
5655 6c4c42e0 2019-06-24 stsp
5656 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5657 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5658 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5659 6c4c42e0 2019-06-24 stsp }
5660 6c4c42e0 2019-06-24 stsp
5661 05171be4 2022-06-23 thomas return err;
5662 6c4c42e0 2019-06-24 stsp }
5663 6c4c42e0 2019-06-24 stsp
5664 6c4c42e0 2019-06-24 stsp static const struct got_error *
5665 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5666 7cbe629d 2018-08-04 stsp {
5667 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5668 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5669 2b380cc8 2018-10-24 stsp int errcode;
5670 2b380cc8 2018-10-24 stsp
5671 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5672 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5673 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5674 2b380cc8 2018-10-24 stsp if (errcode)
5675 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5676 51fe7530 2019-08-19 stsp
5677 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5678 2b380cc8 2018-10-24 stsp }
5679 e5a0f69f 2018-08-18 stsp
5680 51fe7530 2019-08-19 stsp if (s->blame_complete)
5681 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5682 51fe7530 2019-08-19 stsp
5683 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5684 e5a0f69f 2018-08-18 stsp
5685 a5d43cac 2022-07-01 thomas view_border(view);
5686 e5a0f69f 2018-08-18 stsp return err;
5687 e5a0f69f 2018-08-18 stsp }
5688 e5a0f69f 2018-08-18 stsp
5689 e5a0f69f 2018-08-18 stsp static const struct got_error *
5690 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5691 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5692 eaeaa612 2022-07-20 thomas {
5693 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5694 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5695 eaeaa612 2022-07-20 thomas
5696 eaeaa612 2022-07-20 thomas *new_view = NULL;
5697 eaeaa612 2022-07-20 thomas
5698 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5699 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5700 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5701 eaeaa612 2022-07-20 thomas
5702 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5703 eaeaa612 2022-07-20 thomas if (err)
5704 eaeaa612 2022-07-20 thomas view_close(log_view);
5705 eaeaa612 2022-07-20 thomas else
5706 eaeaa612 2022-07-20 thomas *new_view = log_view;
5707 eaeaa612 2022-07-20 thomas
5708 eaeaa612 2022-07-20 thomas return err;
5709 eaeaa612 2022-07-20 thomas }
5710 eaeaa612 2022-07-20 thomas
5711 eaeaa612 2022-07-20 thomas static const struct got_error *
5712 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5713 e5a0f69f 2018-08-18 stsp {
5714 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5715 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
5716 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5717 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5718 a5d43cac 2022-07-01 thomas
5719 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5720 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5721 a5d43cac 2022-07-01 thomas --eos; /* border */
5722 7cbe629d 2018-08-04 stsp
5723 e5a0f69f 2018-08-18 stsp switch (ch) {
5724 05171be4 2022-06-23 thomas case '0':
5725 05171be4 2022-06-23 thomas view->x = 0;
5726 05171be4 2022-06-23 thomas break;
5727 05171be4 2022-06-23 thomas case '$':
5728 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5729 07b0611c 2022-06-23 thomas view->count = 0;
5730 05171be4 2022-06-23 thomas break;
5731 05171be4 2022-06-23 thomas case KEY_RIGHT:
5732 05171be4 2022-06-23 thomas case 'l':
5733 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5734 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5735 07b0611c 2022-06-23 thomas else
5736 07b0611c 2022-06-23 thomas view->count = 0;
5737 05171be4 2022-06-23 thomas break;
5738 05171be4 2022-06-23 thomas case KEY_LEFT:
5739 05171be4 2022-06-23 thomas case 'h':
5740 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5741 07b0611c 2022-06-23 thomas if (view->x <= 0)
5742 07b0611c 2022-06-23 thomas view->count = 0;
5743 05171be4 2022-06-23 thomas break;
5744 1e37a5c2 2019-05-12 jcs case 'q':
5745 1e37a5c2 2019-05-12 jcs s->done = 1;
5746 4deef56f 2021-09-02 naddy break;
5747 4deef56f 2021-09-02 naddy case 'g':
5748 4deef56f 2021-09-02 naddy case KEY_HOME:
5749 4deef56f 2021-09-02 naddy s->selected_line = 1;
5750 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5751 07b0611c 2022-06-23 thomas view->count = 0;
5752 4deef56f 2021-09-02 naddy break;
5753 4deef56f 2021-09-02 naddy case 'G':
5754 4deef56f 2021-09-02 naddy case KEY_END:
5755 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5756 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5757 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5758 4deef56f 2021-09-02 naddy } else {
5759 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5760 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5761 4deef56f 2021-09-02 naddy }
5762 07b0611c 2022-06-23 thomas view->count = 0;
5763 1e37a5c2 2019-05-12 jcs break;
5764 1e37a5c2 2019-05-12 jcs case 'k':
5765 1e37a5c2 2019-05-12 jcs case KEY_UP:
5766 f7140bf5 2021-10-17 thomas case CTRL('p'):
5767 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5768 1e37a5c2 2019-05-12 jcs s->selected_line--;
5769 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5770 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5771 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5772 07b0611c 2022-06-23 thomas else
5773 07b0611c 2022-06-23 thomas view->count = 0;
5774 1e37a5c2 2019-05-12 jcs break;
5775 70f17a53 2022-06-13 thomas case CTRL('u'):
5776 23427b14 2022-06-23 thomas case 'u':
5777 70f17a53 2022-06-13 thomas nscroll /= 2;
5778 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5779 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5780 ea025d1d 2020-02-22 naddy case CTRL('b'):
5781 1c5e5faa 2022-06-23 thomas case 'b':
5782 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5783 07b0611c 2022-06-23 thomas if (view->count > 1)
5784 07b0611c 2022-06-23 thomas nscroll += nscroll;
5785 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5786 07b0611c 2022-06-23 thomas view->count = 0;
5787 e5a0f69f 2018-08-18 stsp break;
5788 1e37a5c2 2019-05-12 jcs }
5789 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5790 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5791 1e37a5c2 2019-05-12 jcs else
5792 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5793 1e37a5c2 2019-05-12 jcs break;
5794 1e37a5c2 2019-05-12 jcs case 'j':
5795 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5796 f7140bf5 2021-10-17 thomas case CTRL('n'):
5797 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5798 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5799 1e37a5c2 2019-05-12 jcs s->selected_line++;
5800 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5801 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5802 07b0611c 2022-06-23 thomas else
5803 07b0611c 2022-06-23 thomas view->count = 0;
5804 1e37a5c2 2019-05-12 jcs break;
5805 1c5e5faa 2022-06-23 thomas case 'c':
5806 1e37a5c2 2019-05-12 jcs case 'p': {
5807 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5808 07b0611c 2022-06-23 thomas
5809 07b0611c 2022-06-23 thomas view->count = 0;
5810 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5811 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5812 1e37a5c2 2019-05-12 jcs if (id == NULL)
5813 e5a0f69f 2018-08-18 stsp break;
5814 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5815 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5816 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5817 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5818 1e37a5c2 2019-05-12 jcs int obj_type;
5819 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5820 1e37a5c2 2019-05-12 jcs s->repo, id);
5821 e5a0f69f 2018-08-18 stsp if (err)
5822 e5a0f69f 2018-08-18 stsp break;
5823 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5824 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5825 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5826 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5827 e5a0f69f 2018-08-18 stsp break;
5828 e5a0f69f 2018-08-18 stsp }
5829 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5830 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5831 ec242592 2022-04-22 thomas s->repo, &pid->id);
5832 945f9229 2022-04-16 thomas if (err)
5833 945f9229 2022-04-16 thomas break;
5834 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5835 945f9229 2022-04-16 thomas pcommit, s->path);
5836 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5837 e5a0f69f 2018-08-18 stsp if (err) {
5838 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5839 1e37a5c2 2019-05-12 jcs err = NULL;
5840 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5841 e5a0f69f 2018-08-18 stsp break;
5842 e5a0f69f 2018-08-18 stsp }
5843 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5844 1e37a5c2 2019-05-12 jcs blob_id);
5845 1e37a5c2 2019-05-12 jcs free(blob_id);
5846 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5847 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5848 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5849 e5a0f69f 2018-08-18 stsp break;
5850 1e37a5c2 2019-05-12 jcs }
5851 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5852 ec242592 2022-04-22 thomas &pid->id);
5853 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5854 1e37a5c2 2019-05-12 jcs } else {
5855 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5856 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5857 1e37a5c2 2019-05-12 jcs break;
5858 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5859 1e37a5c2 2019-05-12 jcs id);
5860 1e37a5c2 2019-05-12 jcs }
5861 1e37a5c2 2019-05-12 jcs if (err)
5862 e5a0f69f 2018-08-18 stsp break;
5863 1e37a5c2 2019-05-12 jcs s->done = 1;
5864 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5865 1e37a5c2 2019-05-12 jcs s->done = 0;
5866 1e37a5c2 2019-05-12 jcs if (thread_err)
5867 1e37a5c2 2019-05-12 jcs break;
5868 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5869 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5870 a5388363 2020-12-01 naddy err = run_blame(view);
5871 1e37a5c2 2019-05-12 jcs if (err)
5872 1e37a5c2 2019-05-12 jcs break;
5873 1e37a5c2 2019-05-12 jcs break;
5874 1e37a5c2 2019-05-12 jcs }
5875 1c5e5faa 2022-06-23 thomas case 'C': {
5876 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5877 07b0611c 2022-06-23 thomas
5878 07b0611c 2022-06-23 thomas view->count = 0;
5879 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5880 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5881 1e37a5c2 2019-05-12 jcs break;
5882 1e37a5c2 2019-05-12 jcs s->done = 1;
5883 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5884 1e37a5c2 2019-05-12 jcs s->done = 0;
5885 1e37a5c2 2019-05-12 jcs if (thread_err)
5886 1e37a5c2 2019-05-12 jcs break;
5887 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5888 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5889 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5890 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5891 a5388363 2020-12-01 naddy err = run_blame(view);
5892 1e37a5c2 2019-05-12 jcs if (err)
5893 1e37a5c2 2019-05-12 jcs break;
5894 1e37a5c2 2019-05-12 jcs break;
5895 1e37a5c2 2019-05-12 jcs }
5896 2a31b33b 2022-07-23 thomas case 'L':
5897 eaeaa612 2022-07-20 thomas view->count = 0;
5898 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
5899 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
5900 2a31b33b 2022-07-23 thomas if (s->id_to_log)
5901 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
5902 eaeaa612 2022-07-20 thomas break;
5903 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5904 1e37a5c2 2019-05-12 jcs case '\r': {
5905 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5906 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5907 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5908 07b0611c 2022-06-23 thomas
5909 07b0611c 2022-06-23 thomas view->count = 0;
5910 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5911 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5912 1e37a5c2 2019-05-12 jcs if (id == NULL)
5913 1e37a5c2 2019-05-12 jcs break;
5914 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5915 1e37a5c2 2019-05-12 jcs if (err)
5916 1e37a5c2 2019-05-12 jcs break;
5917 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5918 4fc71f3b 2022-07-12 thomas if (*new_view) {
5919 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5920 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
5921 4fc71f3b 2022-07-12 thomas if (err)
5922 4fc71f3b 2022-07-12 thomas break;
5923 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
5924 4fc71f3b 2022-07-12 thomas } else {
5925 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
5926 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
5927 a5d43cac 2022-07-01 thomas
5928 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
5929 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
5930 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
5931 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
5932 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
5933 4fc71f3b 2022-07-12 thomas break;
5934 4fc71f3b 2022-07-12 thomas }
5935 15a94983 2018-12-23 stsp }
5936 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5937 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
5938 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5939 1e37a5c2 2019-05-12 jcs if (err) {
5940 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5941 1e37a5c2 2019-05-12 jcs break;
5942 1e37a5c2 2019-05-12 jcs }
5943 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
5944 4fc71f3b 2022-07-12 thomas s->selected_line;
5945 4fc71f3b 2022-07-12 thomas if (*new_view)
5946 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
5947 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
5948 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5949 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
5950 a5d43cac 2022-07-01 thomas if (err)
5951 a5d43cac 2022-07-01 thomas break;
5952 a5d43cac 2022-07-01 thomas }
5953 a5d43cac 2022-07-01 thomas
5954 e78dc838 2020-12-04 stsp view->focussed = 0;
5955 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5956 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
5957 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
5958 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5959 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
5960 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5961 1e37a5c2 2019-05-12 jcs if (err)
5962 34bc9ec9 2019-02-22 stsp break;
5963 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
5964 40236d76 2022-06-23 thomas if (err)
5965 40236d76 2022-06-23 thomas break;
5966 e78dc838 2020-12-04 stsp view->focus_child = 1;
5967 1e37a5c2 2019-05-12 jcs } else
5968 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5969 1e37a5c2 2019-05-12 jcs if (err)
5970 e5a0f69f 2018-08-18 stsp break;
5971 1e37a5c2 2019-05-12 jcs break;
5972 1e37a5c2 2019-05-12 jcs }
5973 70f17a53 2022-06-13 thomas case CTRL('d'):
5974 23427b14 2022-06-23 thomas case 'd':
5975 70f17a53 2022-06-13 thomas nscroll /= 2;
5976 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5977 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5978 ea025d1d 2020-02-22 naddy case CTRL('f'):
5979 1c5e5faa 2022-06-23 thomas case 'f':
5980 1e37a5c2 2019-05-12 jcs case ' ':
5981 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5982 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5983 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5984 07b0611c 2022-06-23 thomas view->count = 0;
5985 e5a0f69f 2018-08-18 stsp break;
5986 1e37a5c2 2019-05-12 jcs }
5987 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5988 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5989 70f17a53 2022-06-13 thomas s->selected_line +=
5990 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5991 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5992 1e37a5c2 2019-05-12 jcs }
5993 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5994 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5995 1e37a5c2 2019-05-12 jcs else
5996 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5997 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5998 1e37a5c2 2019-05-12 jcs break;
5999 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6000 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6001 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6002 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6003 1e37a5c2 2019-05-12 jcs }
6004 1e37a5c2 2019-05-12 jcs break;
6005 1e37a5c2 2019-05-12 jcs default:
6006 07b0611c 2022-06-23 thomas view->count = 0;
6007 1e37a5c2 2019-05-12 jcs break;
6008 a70480e0 2018-06-23 stsp }
6009 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6010 adf4c9e0 2022-07-03 thomas }
6011 adf4c9e0 2022-07-03 thomas
6012 adf4c9e0 2022-07-03 thomas static const struct got_error *
6013 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6014 adf4c9e0 2022-07-03 thomas {
6015 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6016 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6017 adf4c9e0 2022-07-03 thomas
6018 adf4c9e0 2022-07-03 thomas view->count = 0;
6019 adf4c9e0 2022-07-03 thomas s->done = 1;
6020 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6021 adf4c9e0 2022-07-03 thomas s->done = 0;
6022 adf4c9e0 2022-07-03 thomas if (err)
6023 adf4c9e0 2022-07-03 thomas return err;
6024 adf4c9e0 2022-07-03 thomas return run_blame(view);
6025 a70480e0 2018-06-23 stsp }
6026 a70480e0 2018-06-23 stsp
6027 a70480e0 2018-06-23 stsp static const struct got_error *
6028 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6029 9f7d7167 2018-04-29 stsp {
6030 a70480e0 2018-06-23 stsp const struct got_error *error;
6031 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6032 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6033 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6034 0587e10c 2020-07-23 stsp char *link_target = NULL;
6035 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6036 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6037 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6038 a70480e0 2018-06-23 stsp int ch;
6039 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6040 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6041 a70480e0 2018-06-23 stsp
6042 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6043 a70480e0 2018-06-23 stsp switch (ch) {
6044 a70480e0 2018-06-23 stsp case 'c':
6045 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6046 a70480e0 2018-06-23 stsp break;
6047 69069811 2018-08-02 stsp case 'r':
6048 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6049 69069811 2018-08-02 stsp if (repo_path == NULL)
6050 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6051 9ba1d308 2019-10-21 stsp optarg);
6052 69069811 2018-08-02 stsp break;
6053 a70480e0 2018-06-23 stsp default:
6054 17020d27 2019-03-07 stsp usage_blame();
6055 a70480e0 2018-06-23 stsp /* NOTREACHED */
6056 a70480e0 2018-06-23 stsp }
6057 a70480e0 2018-06-23 stsp }
6058 a70480e0 2018-06-23 stsp
6059 a70480e0 2018-06-23 stsp argc -= optind;
6060 a70480e0 2018-06-23 stsp argv += optind;
6061 a70480e0 2018-06-23 stsp
6062 f135c941 2020-02-20 stsp if (argc != 1)
6063 a70480e0 2018-06-23 stsp usage_blame();
6064 6962eb72 2020-02-20 stsp
6065 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6066 7cd52833 2022-06-23 thomas if (error != NULL)
6067 7cd52833 2022-06-23 thomas goto done;
6068 7cd52833 2022-06-23 thomas
6069 69069811 2018-08-02 stsp if (repo_path == NULL) {
6070 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6071 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6072 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6073 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6074 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6075 c156c7a4 2020-12-18 stsp goto done;
6076 f135c941 2020-02-20 stsp if (worktree)
6077 eb41ed75 2019-02-05 stsp repo_path =
6078 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6079 f135c941 2020-02-20 stsp else
6080 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6081 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6082 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6083 c156c7a4 2020-12-18 stsp goto done;
6084 c156c7a4 2020-12-18 stsp }
6085 f135c941 2020-02-20 stsp }
6086 a915003a 2019-02-05 stsp
6087 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6088 c02c541e 2019-03-29 stsp if (error != NULL)
6089 8e94dd5b 2019-01-04 stsp goto done;
6090 69069811 2018-08-02 stsp
6091 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6092 f135c941 2020-02-20 stsp worktree);
6093 c02c541e 2019-03-29 stsp if (error)
6094 92205607 2019-01-04 stsp goto done;
6095 69069811 2018-08-02 stsp
6096 f135c941 2020-02-20 stsp init_curses();
6097 f135c941 2020-02-20 stsp
6098 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6099 51a10b52 2020-12-26 stsp if (error)
6100 51a10b52 2020-12-26 stsp goto done;
6101 51a10b52 2020-12-26 stsp
6102 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6103 eb41ed75 2019-02-05 stsp if (error)
6104 69069811 2018-08-02 stsp goto done;
6105 a70480e0 2018-06-23 stsp
6106 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6107 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6108 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6109 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6110 a70480e0 2018-06-23 stsp if (error != NULL)
6111 66b4983c 2018-06-23 stsp goto done;
6112 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6113 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6114 a70480e0 2018-06-23 stsp } else {
6115 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6116 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6117 a70480e0 2018-06-23 stsp }
6118 a19e88aa 2018-06-23 stsp if (error != NULL)
6119 8b473291 2019-02-21 stsp goto done;
6120 8b473291 2019-02-21 stsp
6121 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6122 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6123 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6124 e1cd8fed 2018-08-01 stsp goto done;
6125 e1cd8fed 2018-08-01 stsp }
6126 0587e10c 2020-07-23 stsp
6127 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6128 945f9229 2022-04-16 thomas if (error)
6129 945f9229 2022-04-16 thomas goto done;
6130 945f9229 2022-04-16 thomas
6131 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6132 945f9229 2022-04-16 thomas commit, repo);
6133 7cbe629d 2018-08-04 stsp if (error)
6134 7cbe629d 2018-08-04 stsp goto done;
6135 0587e10c 2020-07-23 stsp
6136 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6137 78756c87 2020-11-24 stsp commit_id, repo);
6138 0587e10c 2020-07-23 stsp if (error)
6139 0587e10c 2020-07-23 stsp goto done;
6140 12314ad4 2019-08-31 stsp if (worktree) {
6141 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6142 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6143 12314ad4 2019-08-31 stsp worktree = NULL;
6144 12314ad4 2019-08-31 stsp }
6145 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6146 a70480e0 2018-06-23 stsp done:
6147 69069811 2018-08-02 stsp free(repo_path);
6148 f135c941 2020-02-20 stsp free(in_repo_path);
6149 0587e10c 2020-07-23 stsp free(link_target);
6150 69069811 2018-08-02 stsp free(cwd);
6151 a70480e0 2018-06-23 stsp free(commit_id);
6152 945f9229 2022-04-16 thomas if (commit)
6153 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6154 eb41ed75 2019-02-05 stsp if (worktree)
6155 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6156 1d0f4054 2021-06-17 stsp if (repo) {
6157 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6158 1d0f4054 2021-06-17 stsp if (error == NULL)
6159 1d0f4054 2021-06-17 stsp error = close_err;
6160 1d0f4054 2021-06-17 stsp }
6161 7cd52833 2022-06-23 thomas if (pack_fds) {
6162 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6163 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6164 7cd52833 2022-06-23 thomas if (error == NULL)
6165 7cd52833 2022-06-23 thomas error = pack_err;
6166 7cd52833 2022-06-23 thomas }
6167 51a10b52 2020-12-26 stsp tog_free_refs();
6168 a70480e0 2018-06-23 stsp return error;
6169 ffd1d5e5 2018-06-23 stsp }
6170 ffd1d5e5 2018-06-23 stsp
6171 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6172 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6173 ffd1d5e5 2018-06-23 stsp {
6174 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6175 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6176 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6177 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6178 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6179 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6180 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6181 ffd1d5e5 2018-06-23 stsp
6182 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6183 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6184 a5d43cac 2022-07-01 thomas --limit; /* border */
6185 ffd1d5e5 2018-06-23 stsp
6186 f7d12f7e 2018-08-01 stsp werase(view->window);
6187 ffd1d5e5 2018-06-23 stsp
6188 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6189 ffd1d5e5 2018-06-23 stsp return NULL;
6190 ffd1d5e5 2018-06-23 stsp
6191 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6192 f91a2b48 2022-06-23 thomas 0, 0);
6193 ffd1d5e5 2018-06-23 stsp if (err)
6194 ffd1d5e5 2018-06-23 stsp return err;
6195 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6196 a3404814 2018-09-02 stsp wstandout(view->window);
6197 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6198 11b20872 2019-11-08 stsp if (tc)
6199 11b20872 2019-11-08 stsp wattr_on(view->window,
6200 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6201 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6202 11b20872 2019-11-08 stsp if (tc)
6203 11b20872 2019-11-08 stsp wattr_off(view->window,
6204 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6205 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6206 a3404814 2018-09-02 stsp wstandend(view->window);
6207 2550e4c3 2018-07-13 stsp free(wline);
6208 2550e4c3 2018-07-13 stsp wline = NULL;
6209 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6210 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6211 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6212 ffd1d5e5 2018-06-23 stsp return NULL;
6213 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6214 f91a2b48 2022-06-23 thomas 0, 0);
6215 ce52c690 2018-06-23 stsp if (err)
6216 ce52c690 2018-06-23 stsp return err;
6217 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6218 2550e4c3 2018-07-13 stsp free(wline);
6219 2550e4c3 2018-07-13 stsp wline = NULL;
6220 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6221 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6222 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6223 ffd1d5e5 2018-06-23 stsp return NULL;
6224 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6225 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6226 a1eca9bb 2018-06-23 stsp return NULL;
6227 ffd1d5e5 2018-06-23 stsp
6228 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6229 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6230 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6231 0cf4efb1 2018-09-29 stsp if (view->focussed)
6232 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6233 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6234 ffd1d5e5 2018-06-23 stsp }
6235 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6236 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6237 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6238 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6239 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6240 ffd1d5e5 2018-06-23 stsp return NULL;
6241 ffd1d5e5 2018-06-23 stsp n = 1;
6242 ffd1d5e5 2018-06-23 stsp } else {
6243 ffd1d5e5 2018-06-23 stsp n = 0;
6244 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6245 ffd1d5e5 2018-06-23 stsp }
6246 ffd1d5e5 2018-06-23 stsp
6247 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6248 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6249 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6250 848d6979 2019-08-12 stsp const char *modestr = "";
6251 56e0773d 2019-11-28 stsp mode_t mode;
6252 1d13200f 2018-07-12 stsp
6253 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6254 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6255 56e0773d 2019-11-28 stsp
6256 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6257 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6258 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6259 1d13200f 2018-07-12 stsp if (err)
6260 638f9024 2019-05-13 stsp return got_error_from_errno(
6261 230a42bd 2019-05-11 jcs "got_object_id_str");
6262 1d13200f 2018-07-12 stsp }
6263 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6264 63c5ca5d 2019-08-24 stsp modestr = "$";
6265 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6266 0d6c6ee3 2020-05-20 stsp int i;
6267 0d6c6ee3 2020-05-20 stsp
6268 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6269 d86d3b18 2020-12-01 naddy te, s->repo);
6270 0d6c6ee3 2020-05-20 stsp if (err) {
6271 0d6c6ee3 2020-05-20 stsp free(id_str);
6272 0d6c6ee3 2020-05-20 stsp return err;
6273 0d6c6ee3 2020-05-20 stsp }
6274 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6275 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6276 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6277 0d6c6ee3 2020-05-20 stsp }
6278 848d6979 2019-08-12 stsp modestr = "@";
6279 0d6c6ee3 2020-05-20 stsp }
6280 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6281 848d6979 2019-08-12 stsp modestr = "/";
6282 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6283 848d6979 2019-08-12 stsp modestr = "*";
6284 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6285 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6286 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6287 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6288 1d13200f 2018-07-12 stsp free(id_str);
6289 0d6c6ee3 2020-05-20 stsp free(link_target);
6290 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6291 1d13200f 2018-07-12 stsp }
6292 1d13200f 2018-07-12 stsp free(id_str);
6293 0d6c6ee3 2020-05-20 stsp free(link_target);
6294 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6295 f91a2b48 2022-06-23 thomas 0, 0);
6296 ffd1d5e5 2018-06-23 stsp if (err) {
6297 ffd1d5e5 2018-06-23 stsp free(line);
6298 ffd1d5e5 2018-06-23 stsp break;
6299 ffd1d5e5 2018-06-23 stsp }
6300 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6301 0cf4efb1 2018-09-29 stsp if (view->focussed)
6302 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6303 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6304 ffd1d5e5 2018-06-23 stsp }
6305 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6306 f26dddb7 2019-11-08 stsp if (tc)
6307 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6308 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6309 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6310 f26dddb7 2019-11-08 stsp if (tc)
6311 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6312 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6313 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6314 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6315 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6316 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6317 ffd1d5e5 2018-06-23 stsp free(line);
6318 2550e4c3 2018-07-13 stsp free(wline);
6319 2550e4c3 2018-07-13 stsp wline = NULL;
6320 ffd1d5e5 2018-06-23 stsp n++;
6321 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6322 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6323 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6324 ffd1d5e5 2018-06-23 stsp break;
6325 ffd1d5e5 2018-06-23 stsp }
6326 ffd1d5e5 2018-06-23 stsp
6327 ffd1d5e5 2018-06-23 stsp return err;
6328 ffd1d5e5 2018-06-23 stsp }
6329 ffd1d5e5 2018-06-23 stsp
6330 ffd1d5e5 2018-06-23 stsp static void
6331 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6332 ffd1d5e5 2018-06-23 stsp {
6333 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6334 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6335 fa86c4bf 2020-11-29 stsp int i = 0;
6336 ffd1d5e5 2018-06-23 stsp
6337 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6338 ffd1d5e5 2018-06-23 stsp return;
6339 ffd1d5e5 2018-06-23 stsp
6340 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6341 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6342 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6343 fa86c4bf 2020-11-29 stsp if (!isroot)
6344 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6345 fa86c4bf 2020-11-29 stsp break;
6346 fa86c4bf 2020-11-29 stsp }
6347 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6348 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6349 ffd1d5e5 2018-06-23 stsp }
6350 ffd1d5e5 2018-06-23 stsp }
6351 ffd1d5e5 2018-06-23 stsp
6352 a5d43cac 2022-07-01 thomas static const struct got_error *
6353 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6354 ffd1d5e5 2018-06-23 stsp {
6355 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6356 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6357 ffd1d5e5 2018-06-23 stsp int n = 0;
6358 ffd1d5e5 2018-06-23 stsp
6359 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6360 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6361 694d3271 2020-12-01 naddy s->first_displayed_entry);
6362 694d3271 2020-12-01 naddy else
6363 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6364 56e0773d 2019-11-28 stsp
6365 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6366 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6367 a5d43cac 2022-07-01 thomas if (last)
6368 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6369 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6370 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6371 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6372 768394f3 2019-01-24 stsp }
6373 ffd1d5e5 2018-06-23 stsp }
6374 a5d43cac 2022-07-01 thomas
6375 a5d43cac 2022-07-01 thomas return NULL;
6376 ffd1d5e5 2018-06-23 stsp }
6377 ffd1d5e5 2018-06-23 stsp
6378 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6379 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6380 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6381 ffd1d5e5 2018-06-23 stsp {
6382 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6383 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6384 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6385 ffd1d5e5 2018-06-23 stsp
6386 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6387 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6388 56e0773d 2019-11-28 stsp + 1 /* slash */;
6389 ce52c690 2018-06-23 stsp if (te)
6390 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6391 ce52c690 2018-06-23 stsp
6392 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6393 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6394 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6395 ffd1d5e5 2018-06-23 stsp
6396 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6397 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6398 d9765a41 2018-06-23 stsp while (pt) {
6399 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6400 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6401 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6402 cb2ebc8a 2018-06-23 stsp goto done;
6403 cb2ebc8a 2018-06-23 stsp }
6404 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6405 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6406 cb2ebc8a 2018-06-23 stsp goto done;
6407 cb2ebc8a 2018-06-23 stsp }
6408 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6409 ffd1d5e5 2018-06-23 stsp }
6410 ce52c690 2018-06-23 stsp if (te) {
6411 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6412 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6413 ce52c690 2018-06-23 stsp goto done;
6414 ce52c690 2018-06-23 stsp }
6415 cb2ebc8a 2018-06-23 stsp }
6416 ce52c690 2018-06-23 stsp done:
6417 ce52c690 2018-06-23 stsp if (err) {
6418 ce52c690 2018-06-23 stsp free(*path);
6419 ce52c690 2018-06-23 stsp *path = NULL;
6420 ce52c690 2018-06-23 stsp }
6421 ce52c690 2018-06-23 stsp return err;
6422 ce52c690 2018-06-23 stsp }
6423 ce52c690 2018-06-23 stsp
6424 ce52c690 2018-06-23 stsp static const struct got_error *
6425 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6426 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6427 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6428 ce52c690 2018-06-23 stsp {
6429 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6430 ce52c690 2018-06-23 stsp char *path;
6431 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6432 a0de39f3 2019-08-09 stsp
6433 a0de39f3 2019-08-09 stsp *new_view = NULL;
6434 69efd4c4 2018-07-18 stsp
6435 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6436 ce52c690 2018-06-23 stsp if (err)
6437 ce52c690 2018-06-23 stsp return err;
6438 ffd1d5e5 2018-06-23 stsp
6439 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6440 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6441 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6442 83ce39e3 2019-08-12 stsp goto done;
6443 83ce39e3 2019-08-12 stsp }
6444 cdf1ee82 2018-08-01 stsp
6445 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6446 e5a0f69f 2018-08-18 stsp if (err) {
6447 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6448 fc06ba56 2019-08-22 stsp err = NULL;
6449 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6450 e5a0f69f 2018-08-18 stsp } else
6451 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6452 83ce39e3 2019-08-12 stsp done:
6453 83ce39e3 2019-08-12 stsp free(path);
6454 69efd4c4 2018-07-18 stsp return err;
6455 69efd4c4 2018-07-18 stsp }
6456 69efd4c4 2018-07-18 stsp
6457 69efd4c4 2018-07-18 stsp static const struct got_error *
6458 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6459 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6460 69efd4c4 2018-07-18 stsp {
6461 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6462 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6463 69efd4c4 2018-07-18 stsp char *path;
6464 a0de39f3 2019-08-09 stsp
6465 a0de39f3 2019-08-09 stsp *new_view = NULL;
6466 69efd4c4 2018-07-18 stsp
6467 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6468 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6469 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6470 e5a0f69f 2018-08-18 stsp
6471 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6472 69efd4c4 2018-07-18 stsp if (err)
6473 69efd4c4 2018-07-18 stsp return err;
6474 69efd4c4 2018-07-18 stsp
6475 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6476 4e97c21c 2020-12-06 stsp path, 0);
6477 ba4f502b 2018-08-04 stsp if (err)
6478 e5a0f69f 2018-08-18 stsp view_close(log_view);
6479 e5a0f69f 2018-08-18 stsp else
6480 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6481 cb2ebc8a 2018-06-23 stsp free(path);
6482 cb2ebc8a 2018-06-23 stsp return err;
6483 ffd1d5e5 2018-06-23 stsp }
6484 ffd1d5e5 2018-06-23 stsp
6485 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6486 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6487 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6488 ffd1d5e5 2018-06-23 stsp {
6489 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6490 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6491 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6492 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6493 ffd1d5e5 2018-06-23 stsp
6494 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6495 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6496 bc573f3b 2021-07-10 stsp
6497 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6498 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6499 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6500 ffd1d5e5 2018-06-23 stsp
6501 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6502 bc573f3b 2021-07-10 stsp if (err)
6503 bc573f3b 2021-07-10 stsp goto done;
6504 bc573f3b 2021-07-10 stsp
6505 bc573f3b 2021-07-10 stsp /*
6506 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6507 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6508 bc573f3b 2021-07-10 stsp * closed on demand.
6509 bc573f3b 2021-07-10 stsp */
6510 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6511 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6512 bc573f3b 2021-07-10 stsp if (err)
6513 bc573f3b 2021-07-10 stsp goto done;
6514 bc573f3b 2021-07-10 stsp s->tree = s->root;
6515 bc573f3b 2021-07-10 stsp
6516 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6517 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6518 ffd1d5e5 2018-06-23 stsp goto done;
6519 ffd1d5e5 2018-06-23 stsp
6520 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6521 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6522 ffd1d5e5 2018-06-23 stsp goto done;
6523 ffd1d5e5 2018-06-23 stsp }
6524 ffd1d5e5 2018-06-23 stsp
6525 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6526 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6527 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6528 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6529 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6530 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6531 9cd7cbd1 2020-12-07 stsp goto done;
6532 9cd7cbd1 2020-12-07 stsp }
6533 9cd7cbd1 2020-12-07 stsp }
6534 fb2756b9 2018-08-04 stsp s->repo = repo;
6535 c0b01bdb 2019-11-08 stsp
6536 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6537 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6538 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6539 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6540 c0b01bdb 2019-11-08 stsp if (err)
6541 c0b01bdb 2019-11-08 stsp goto done;
6542 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6543 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6544 bc573f3b 2021-07-10 stsp if (err)
6545 c0b01bdb 2019-11-08 stsp goto done;
6546 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6547 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6548 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6549 bc573f3b 2021-07-10 stsp if (err)
6550 c0b01bdb 2019-11-08 stsp goto done;
6551 e5a0f69f 2018-08-18 stsp
6552 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6553 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6554 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6555 bc573f3b 2021-07-10 stsp if (err)
6556 11b20872 2019-11-08 stsp goto done;
6557 11b20872 2019-11-08 stsp
6558 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6559 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6560 bc573f3b 2021-07-10 stsp if (err)
6561 c0b01bdb 2019-11-08 stsp goto done;
6562 c0b01bdb 2019-11-08 stsp }
6563 c0b01bdb 2019-11-08 stsp
6564 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6565 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6566 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6567 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6568 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6569 ad80ab7b 2018-08-04 stsp done:
6570 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6571 bc573f3b 2021-07-10 stsp if (commit)
6572 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6573 bc573f3b 2021-07-10 stsp if (err)
6574 bc573f3b 2021-07-10 stsp close_tree_view(view);
6575 ad80ab7b 2018-08-04 stsp return err;
6576 ad80ab7b 2018-08-04 stsp }
6577 ad80ab7b 2018-08-04 stsp
6578 e5a0f69f 2018-08-18 stsp static const struct got_error *
6579 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6580 ad80ab7b 2018-08-04 stsp {
6581 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6582 ad80ab7b 2018-08-04 stsp
6583 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6584 fb2756b9 2018-08-04 stsp free(s->tree_label);
6585 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6586 6484ec90 2018-09-29 stsp free(s->commit_id);
6587 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6588 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6589 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6590 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6591 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6592 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6593 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6594 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6595 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6596 ad80ab7b 2018-08-04 stsp free(parent);
6597 ad80ab7b 2018-08-04 stsp
6598 ad80ab7b 2018-08-04 stsp }
6599 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6600 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6601 bc573f3b 2021-07-10 stsp if (s->root)
6602 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6603 7c32bd05 2019-06-22 stsp return NULL;
6604 7c32bd05 2019-06-22 stsp }
6605 7c32bd05 2019-06-22 stsp
6606 7c32bd05 2019-06-22 stsp static const struct got_error *
6607 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6608 7c32bd05 2019-06-22 stsp {
6609 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6610 7c32bd05 2019-06-22 stsp
6611 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6612 7c32bd05 2019-06-22 stsp return NULL;
6613 7c32bd05 2019-06-22 stsp }
6614 7c32bd05 2019-06-22 stsp
6615 7c32bd05 2019-06-22 stsp static int
6616 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6617 7c32bd05 2019-06-22 stsp {
6618 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6619 7c32bd05 2019-06-22 stsp
6620 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6621 56e0773d 2019-11-28 stsp 0) == 0;
6622 7c32bd05 2019-06-22 stsp }
6623 7c32bd05 2019-06-22 stsp
6624 7c32bd05 2019-06-22 stsp static const struct got_error *
6625 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6626 7c32bd05 2019-06-22 stsp {
6627 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6628 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6629 7c32bd05 2019-06-22 stsp
6630 7c32bd05 2019-06-22 stsp if (!view->searching) {
6631 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6632 7c32bd05 2019-06-22 stsp return NULL;
6633 7c32bd05 2019-06-22 stsp }
6634 7c32bd05 2019-06-22 stsp
6635 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6636 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6637 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6638 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6639 56e0773d 2019-11-28 stsp s->selected_entry);
6640 7c32bd05 2019-06-22 stsp else
6641 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6642 56e0773d 2019-11-28 stsp } else {
6643 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6644 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6645 56e0773d 2019-11-28 stsp else
6646 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6647 56e0773d 2019-11-28 stsp s->selected_entry);
6648 7c32bd05 2019-06-22 stsp }
6649 7c32bd05 2019-06-22 stsp } else {
6650 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6651 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6652 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6653 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6654 56e0773d 2019-11-28 stsp else
6655 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6656 7c32bd05 2019-06-22 stsp }
6657 7c32bd05 2019-06-22 stsp
6658 7c32bd05 2019-06-22 stsp while (1) {
6659 56e0773d 2019-11-28 stsp if (te == NULL) {
6660 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6661 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6662 ac66afb8 2019-06-24 stsp return NULL;
6663 ac66afb8 2019-06-24 stsp }
6664 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6665 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6666 56e0773d 2019-11-28 stsp else
6667 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6668 7c32bd05 2019-06-22 stsp }
6669 7c32bd05 2019-06-22 stsp
6670 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6671 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6672 56e0773d 2019-11-28 stsp s->matched_entry = te;
6673 7c32bd05 2019-06-22 stsp break;
6674 7c32bd05 2019-06-22 stsp }
6675 7c32bd05 2019-06-22 stsp
6676 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6677 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6678 56e0773d 2019-11-28 stsp else
6679 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6680 7c32bd05 2019-06-22 stsp }
6681 e5a0f69f 2018-08-18 stsp
6682 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6683 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6684 7c32bd05 2019-06-22 stsp s->selected = 0;
6685 7c32bd05 2019-06-22 stsp }
6686 7c32bd05 2019-06-22 stsp
6687 e5a0f69f 2018-08-18 stsp return NULL;
6688 ad80ab7b 2018-08-04 stsp }
6689 ad80ab7b 2018-08-04 stsp
6690 ad80ab7b 2018-08-04 stsp static const struct got_error *
6691 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6692 ad80ab7b 2018-08-04 stsp {
6693 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6694 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6695 e5a0f69f 2018-08-18 stsp char *parent_path;
6696 ad80ab7b 2018-08-04 stsp
6697 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6698 e5a0f69f 2018-08-18 stsp if (err)
6699 e5a0f69f 2018-08-18 stsp return err;
6700 ffd1d5e5 2018-06-23 stsp
6701 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6702 e5a0f69f 2018-08-18 stsp free(parent_path);
6703 669b5ffa 2018-10-07 stsp
6704 a5d43cac 2022-07-01 thomas view_border(view);
6705 e5a0f69f 2018-08-18 stsp return err;
6706 e5a0f69f 2018-08-18 stsp }
6707 ce52c690 2018-06-23 stsp
6708 e5a0f69f 2018-08-18 stsp static const struct got_error *
6709 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6710 e5a0f69f 2018-08-18 stsp {
6711 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6712 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6713 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6714 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
6715 ffd1d5e5 2018-06-23 stsp
6716 e5a0f69f 2018-08-18 stsp switch (ch) {
6717 1e37a5c2 2019-05-12 jcs case 'i':
6718 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6719 07b0611c 2022-06-23 thomas view->count = 0;
6720 1e37a5c2 2019-05-12 jcs break;
6721 1be4947a 2022-07-22 thomas case 'L':
6722 07b0611c 2022-06-23 thomas view->count = 0;
6723 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6724 ffd1d5e5 2018-06-23 stsp break;
6725 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6726 152c1c93 2020-11-29 stsp break;
6727 1be4947a 2022-07-22 thomas case 'R':
6728 07b0611c 2022-06-23 thomas view->count = 0;
6729 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
6730 e4526bf5 2021-09-03 naddy break;
6731 e4526bf5 2021-09-03 naddy case 'g':
6732 e4526bf5 2021-09-03 naddy case KEY_HOME:
6733 e4526bf5 2021-09-03 naddy s->selected = 0;
6734 07b0611c 2022-06-23 thomas view->count = 0;
6735 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6736 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6737 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6738 e4526bf5 2021-09-03 naddy else
6739 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6740 1e37a5c2 2019-05-12 jcs break;
6741 e4526bf5 2021-09-03 naddy case 'G':
6742 a5d43cac 2022-07-01 thomas case KEY_END: {
6743 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6744 a5d43cac 2022-07-01 thomas
6745 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6746 a5d43cac 2022-07-01 thomas --eos; /* border */
6747 e4526bf5 2021-09-03 naddy s->selected = 0;
6748 07b0611c 2022-06-23 thomas view->count = 0;
6749 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6750 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6751 e4526bf5 2021-09-03 naddy if (te == NULL) {
6752 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
6753 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6754 e4526bf5 2021-09-03 naddy n++;
6755 e4526bf5 2021-09-03 naddy }
6756 e4526bf5 2021-09-03 naddy break;
6757 e4526bf5 2021-09-03 naddy }
6758 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6759 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6760 e4526bf5 2021-09-03 naddy }
6761 e4526bf5 2021-09-03 naddy if (n > 0)
6762 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6763 e4526bf5 2021-09-03 naddy break;
6764 a5d43cac 2022-07-01 thomas }
6765 1e37a5c2 2019-05-12 jcs case 'k':
6766 1e37a5c2 2019-05-12 jcs case KEY_UP:
6767 f7140bf5 2021-10-17 thomas case CTRL('p'):
6768 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6769 1e37a5c2 2019-05-12 jcs s->selected--;
6770 fa86c4bf 2020-11-29 stsp break;
6771 1e37a5c2 2019-05-12 jcs }
6772 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6773 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6774 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6775 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6776 07b0611c 2022-06-23 thomas view->count = 0;
6777 1e37a5c2 2019-05-12 jcs break;
6778 70f17a53 2022-06-13 thomas case CTRL('u'):
6779 23427b14 2022-06-23 thomas case 'u':
6780 70f17a53 2022-06-13 thomas nscroll /= 2;
6781 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6782 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6783 ea025d1d 2020-02-22 naddy case CTRL('b'):
6784 1c5e5faa 2022-06-23 thomas case 'b':
6785 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6786 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6787 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6788 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6789 fa86c4bf 2020-11-29 stsp } else {
6790 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6791 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6792 fa86c4bf 2020-11-29 stsp }
6793 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6794 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6795 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6796 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6797 07b0611c 2022-06-23 thomas view->count = 0;
6798 1e37a5c2 2019-05-12 jcs break;
6799 1e37a5c2 2019-05-12 jcs case 'j':
6800 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6801 f7140bf5 2021-10-17 thomas case CTRL('n'):
6802 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6803 1e37a5c2 2019-05-12 jcs s->selected++;
6804 1e37a5c2 2019-05-12 jcs break;
6805 1e37a5c2 2019-05-12 jcs }
6806 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6807 07b0611c 2022-06-23 thomas == NULL) {
6808 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6809 07b0611c 2022-06-23 thomas view->count = 0;
6810 1e37a5c2 2019-05-12 jcs break;
6811 07b0611c 2022-06-23 thomas }
6812 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6813 1e37a5c2 2019-05-12 jcs break;
6814 70f17a53 2022-06-13 thomas case CTRL('d'):
6815 23427b14 2022-06-23 thomas case 'd':
6816 70f17a53 2022-06-13 thomas nscroll /= 2;
6817 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6818 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6819 ea025d1d 2020-02-22 naddy case CTRL('f'):
6820 1c5e5faa 2022-06-23 thomas case 'f':
6821 4c2d69cb 2022-06-23 thomas case ' ':
6822 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6823 1e37a5c2 2019-05-12 jcs == NULL) {
6824 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6825 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6826 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6827 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6828 07b0611c 2022-06-23 thomas else
6829 07b0611c 2022-06-23 thomas view->count = 0;
6830 1e37a5c2 2019-05-12 jcs break;
6831 1e37a5c2 2019-05-12 jcs }
6832 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6833 1e37a5c2 2019-05-12 jcs break;
6834 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6835 1e37a5c2 2019-05-12 jcs case '\r':
6836 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6837 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6838 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6839 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6840 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6841 07b0611c 2022-06-23 thomas view->count = 0;
6842 1e37a5c2 2019-05-12 jcs break;
6843 07b0611c 2022-06-23 thomas }
6844 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6845 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6846 1e37a5c2 2019-05-12 jcs entry);
6847 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6848 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6849 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6850 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6851 1e37a5c2 2019-05-12 jcs s->selected_entry =
6852 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6853 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6854 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6855 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6856 a5d43cac 2022-07-01 thomas if (err)
6857 a5d43cac 2022-07-01 thomas break;
6858 a5d43cac 2022-07-01 thomas }
6859 1e37a5c2 2019-05-12 jcs free(parent);
6860 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6861 56e0773d 2019-11-28 stsp s->selected_entry))) {
6862 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6863 07b0611c 2022-06-23 thomas view->count = 0;
6864 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6865 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6866 1e37a5c2 2019-05-12 jcs if (err)
6867 1e37a5c2 2019-05-12 jcs break;
6868 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6869 941e9f74 2019-05-21 stsp if (err) {
6870 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6871 1e37a5c2 2019-05-12 jcs break;
6872 1e37a5c2 2019-05-12 jcs }
6873 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
6874 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
6875 1e37a5c2 2019-05-12 jcs break;
6876 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6877 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6878 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6879 07b0611c 2022-06-23 thomas view->count = 0;
6880 1e37a5c2 2019-05-12 jcs break;
6881 1e37a5c2 2019-05-12 jcs default:
6882 07b0611c 2022-06-23 thomas view->count = 0;
6883 1e37a5c2 2019-05-12 jcs break;
6884 ffd1d5e5 2018-06-23 stsp }
6885 e5a0f69f 2018-08-18 stsp
6886 ffd1d5e5 2018-06-23 stsp return err;
6887 9f7d7167 2018-04-29 stsp }
6888 9f7d7167 2018-04-29 stsp
6889 ffd1d5e5 2018-06-23 stsp __dead static void
6890 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6891 ffd1d5e5 2018-06-23 stsp {
6892 ffd1d5e5 2018-06-23 stsp endwin();
6893 91198554 2022-06-23 thomas fprintf(stderr,
6894 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6895 ffd1d5e5 2018-06-23 stsp getprogname());
6896 ffd1d5e5 2018-06-23 stsp exit(1);
6897 ffd1d5e5 2018-06-23 stsp }
6898 ffd1d5e5 2018-06-23 stsp
6899 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6900 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6901 ffd1d5e5 2018-06-23 stsp {
6902 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6903 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6904 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6905 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6906 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6907 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6908 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6909 4e97c21c 2020-12-06 stsp char *label = NULL;
6910 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6911 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6912 ffd1d5e5 2018-06-23 stsp int ch;
6913 5221c383 2018-08-01 stsp struct tog_view *view;
6914 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6915 70ac5f84 2019-03-28 stsp
6916 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6917 ffd1d5e5 2018-06-23 stsp switch (ch) {
6918 ffd1d5e5 2018-06-23 stsp case 'c':
6919 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6920 74283ab8 2020-02-07 stsp break;
6921 74283ab8 2020-02-07 stsp case 'r':
6922 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6923 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6924 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6925 74283ab8 2020-02-07 stsp optarg);
6926 ffd1d5e5 2018-06-23 stsp break;
6927 ffd1d5e5 2018-06-23 stsp default:
6928 e99e2d15 2020-11-24 naddy usage_tree();
6929 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6930 ffd1d5e5 2018-06-23 stsp }
6931 ffd1d5e5 2018-06-23 stsp }
6932 ffd1d5e5 2018-06-23 stsp
6933 ffd1d5e5 2018-06-23 stsp argc -= optind;
6934 ffd1d5e5 2018-06-23 stsp argv += optind;
6935 ffd1d5e5 2018-06-23 stsp
6936 55cccc34 2020-02-20 stsp if (argc > 1)
6937 e99e2d15 2020-11-24 naddy usage_tree();
6938 7cd52833 2022-06-23 thomas
6939 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6940 7cd52833 2022-06-23 thomas if (error != NULL)
6941 7cd52833 2022-06-23 thomas goto done;
6942 74283ab8 2020-02-07 stsp
6943 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6944 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6945 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6946 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6947 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6948 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6949 c156c7a4 2020-12-18 stsp goto done;
6950 55cccc34 2020-02-20 stsp if (worktree)
6951 52185f70 2019-02-05 stsp repo_path =
6952 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6953 55cccc34 2020-02-20 stsp else
6954 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6955 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6956 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6957 c156c7a4 2020-12-18 stsp goto done;
6958 c156c7a4 2020-12-18 stsp }
6959 55cccc34 2020-02-20 stsp }
6960 a915003a 2019-02-05 stsp
6961 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6962 c02c541e 2019-03-29 stsp if (error != NULL)
6963 52185f70 2019-02-05 stsp goto done;
6964 d188b9a6 2019-01-04 stsp
6965 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6966 55cccc34 2020-02-20 stsp repo, worktree);
6967 55cccc34 2020-02-20 stsp if (error)
6968 55cccc34 2020-02-20 stsp goto done;
6969 55cccc34 2020-02-20 stsp
6970 55cccc34 2020-02-20 stsp init_curses();
6971 55cccc34 2020-02-20 stsp
6972 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6973 c02c541e 2019-03-29 stsp if (error)
6974 52185f70 2019-02-05 stsp goto done;
6975 ffd1d5e5 2018-06-23 stsp
6976 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6977 51a10b52 2020-12-26 stsp if (error)
6978 51a10b52 2020-12-26 stsp goto done;
6979 51a10b52 2020-12-26 stsp
6980 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6981 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6982 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6983 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6984 4e97c21c 2020-12-06 stsp if (error)
6985 4e97c21c 2020-12-06 stsp goto done;
6986 4e97c21c 2020-12-06 stsp head_ref_name = label;
6987 4e97c21c 2020-12-06 stsp } else {
6988 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6989 4e97c21c 2020-12-06 stsp if (error == NULL)
6990 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6991 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6992 4e97c21c 2020-12-06 stsp goto done;
6993 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6994 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6995 4e97c21c 2020-12-06 stsp if (error)
6996 4e97c21c 2020-12-06 stsp goto done;
6997 4e97c21c 2020-12-06 stsp }
6998 ffd1d5e5 2018-06-23 stsp
6999 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7000 945f9229 2022-04-16 thomas if (error)
7001 945f9229 2022-04-16 thomas goto done;
7002 945f9229 2022-04-16 thomas
7003 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7004 5221c383 2018-08-01 stsp if (view == NULL) {
7005 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7006 5221c383 2018-08-01 stsp goto done;
7007 5221c383 2018-08-01 stsp }
7008 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7009 ad80ab7b 2018-08-04 stsp if (error)
7010 ad80ab7b 2018-08-04 stsp goto done;
7011 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7012 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7013 d91faf3b 2020-12-01 naddy in_repo_path);
7014 55cccc34 2020-02-20 stsp if (error)
7015 55cccc34 2020-02-20 stsp goto done;
7016 55cccc34 2020-02-20 stsp }
7017 55cccc34 2020-02-20 stsp
7018 55cccc34 2020-02-20 stsp if (worktree) {
7019 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7020 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7021 55cccc34 2020-02-20 stsp worktree = NULL;
7022 55cccc34 2020-02-20 stsp }
7023 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7024 ffd1d5e5 2018-06-23 stsp done:
7025 52185f70 2019-02-05 stsp free(repo_path);
7026 e4a0e26d 2020-02-20 stsp free(cwd);
7027 ffd1d5e5 2018-06-23 stsp free(commit_id);
7028 4e97c21c 2020-12-06 stsp free(label);
7029 486cd271 2020-12-06 stsp if (ref)
7030 486cd271 2020-12-06 stsp got_ref_close(ref);
7031 1d0f4054 2021-06-17 stsp if (repo) {
7032 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7033 1d0f4054 2021-06-17 stsp if (error == NULL)
7034 1d0f4054 2021-06-17 stsp error = close_err;
7035 1d0f4054 2021-06-17 stsp }
7036 7cd52833 2022-06-23 thomas if (pack_fds) {
7037 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7038 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7039 7cd52833 2022-06-23 thomas if (error == NULL)
7040 7cd52833 2022-06-23 thomas error = pack_err;
7041 7cd52833 2022-06-23 thomas }
7042 51a10b52 2020-12-26 stsp tog_free_refs();
7043 ffd1d5e5 2018-06-23 stsp return error;
7044 6458efa5 2020-11-24 stsp }
7045 6458efa5 2020-11-24 stsp
7046 6458efa5 2020-11-24 stsp static const struct got_error *
7047 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7048 6458efa5 2020-11-24 stsp {
7049 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7050 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7051 6458efa5 2020-11-24 stsp
7052 6458efa5 2020-11-24 stsp s->nrefs = 0;
7053 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7054 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7055 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7056 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7057 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7058 6458efa5 2020-11-24 stsp continue;
7059 6458efa5 2020-11-24 stsp
7060 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7061 6458efa5 2020-11-24 stsp if (re == NULL)
7062 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7063 6458efa5 2020-11-24 stsp
7064 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7065 8924d611 2020-12-26 stsp if (re->ref == NULL)
7066 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7067 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7068 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7069 6458efa5 2020-11-24 stsp }
7070 6458efa5 2020-11-24 stsp
7071 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7072 6458efa5 2020-11-24 stsp return NULL;
7073 6458efa5 2020-11-24 stsp }
7074 6458efa5 2020-11-24 stsp
7075 ef20f542 2022-06-26 thomas static void
7076 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7077 6458efa5 2020-11-24 stsp {
7078 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7079 6458efa5 2020-11-24 stsp
7080 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7081 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7082 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7083 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7084 6458efa5 2020-11-24 stsp free(re);
7085 6458efa5 2020-11-24 stsp }
7086 6458efa5 2020-11-24 stsp }
7087 6458efa5 2020-11-24 stsp
7088 6458efa5 2020-11-24 stsp static const struct got_error *
7089 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7090 6458efa5 2020-11-24 stsp {
7091 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7092 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7093 6458efa5 2020-11-24 stsp
7094 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7095 6458efa5 2020-11-24 stsp s->repo = repo;
7096 6458efa5 2020-11-24 stsp
7097 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7098 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7099 6458efa5 2020-11-24 stsp
7100 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7101 6458efa5 2020-11-24 stsp if (err)
7102 6458efa5 2020-11-24 stsp return err;
7103 34ba6917 2020-11-30 stsp
7104 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7105 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7106 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7107 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7108 6458efa5 2020-11-24 stsp if (err)
7109 6458efa5 2020-11-24 stsp goto done;
7110 6458efa5 2020-11-24 stsp
7111 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7112 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7113 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7114 6458efa5 2020-11-24 stsp if (err)
7115 6458efa5 2020-11-24 stsp goto done;
7116 6458efa5 2020-11-24 stsp
7117 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7118 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7119 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7120 2183bbf6 2022-01-23 thomas if (err)
7121 2183bbf6 2022-01-23 thomas goto done;
7122 2183bbf6 2022-01-23 thomas
7123 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7124 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7125 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7126 6458efa5 2020-11-24 stsp if (err)
7127 6458efa5 2020-11-24 stsp goto done;
7128 6458efa5 2020-11-24 stsp }
7129 6458efa5 2020-11-24 stsp
7130 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7131 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7132 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7133 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7134 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7135 6458efa5 2020-11-24 stsp done:
7136 6458efa5 2020-11-24 stsp if (err)
7137 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7138 6458efa5 2020-11-24 stsp return err;
7139 6458efa5 2020-11-24 stsp }
7140 6458efa5 2020-11-24 stsp
7141 6458efa5 2020-11-24 stsp static const struct got_error *
7142 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7143 6458efa5 2020-11-24 stsp {
7144 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7145 6458efa5 2020-11-24 stsp
7146 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7147 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7148 6458efa5 2020-11-24 stsp
7149 6458efa5 2020-11-24 stsp return NULL;
7150 9f7d7167 2018-04-29 stsp }
7151 ce5b7c56 2019-07-09 stsp
7152 6458efa5 2020-11-24 stsp static const struct got_error *
7153 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7154 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7155 6458efa5 2020-11-24 stsp {
7156 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7157 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7158 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7159 6458efa5 2020-11-24 stsp int obj_type;
7160 6458efa5 2020-11-24 stsp
7161 c42c9805 2020-11-24 stsp *commit_id = NULL;
7162 6458efa5 2020-11-24 stsp
7163 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7164 6458efa5 2020-11-24 stsp if (err)
7165 6458efa5 2020-11-24 stsp return err;
7166 6458efa5 2020-11-24 stsp
7167 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7168 6458efa5 2020-11-24 stsp if (err)
7169 6458efa5 2020-11-24 stsp goto done;
7170 6458efa5 2020-11-24 stsp
7171 6458efa5 2020-11-24 stsp switch (obj_type) {
7172 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7173 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7174 6458efa5 2020-11-24 stsp break;
7175 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7176 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7177 6458efa5 2020-11-24 stsp if (err)
7178 6458efa5 2020-11-24 stsp goto done;
7179 c42c9805 2020-11-24 stsp free(obj_id);
7180 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7181 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7182 c42c9805 2020-11-24 stsp if (err)
7183 6458efa5 2020-11-24 stsp goto done;
7184 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7185 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7186 c42c9805 2020-11-24 stsp goto done;
7187 c42c9805 2020-11-24 stsp }
7188 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7189 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7190 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7191 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7192 c42c9805 2020-11-24 stsp goto done;
7193 c42c9805 2020-11-24 stsp }
7194 6458efa5 2020-11-24 stsp break;
7195 6458efa5 2020-11-24 stsp default:
7196 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7197 c42c9805 2020-11-24 stsp break;
7198 6458efa5 2020-11-24 stsp }
7199 6458efa5 2020-11-24 stsp
7200 c42c9805 2020-11-24 stsp done:
7201 c42c9805 2020-11-24 stsp if (tag)
7202 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7203 c42c9805 2020-11-24 stsp if (err) {
7204 c42c9805 2020-11-24 stsp free(*commit_id);
7205 c42c9805 2020-11-24 stsp *commit_id = NULL;
7206 c42c9805 2020-11-24 stsp }
7207 c42c9805 2020-11-24 stsp return err;
7208 c42c9805 2020-11-24 stsp }
7209 c42c9805 2020-11-24 stsp
7210 c42c9805 2020-11-24 stsp static const struct got_error *
7211 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7212 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7213 c42c9805 2020-11-24 stsp {
7214 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7215 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7216 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7217 c42c9805 2020-11-24 stsp
7218 c42c9805 2020-11-24 stsp *new_view = NULL;
7219 c42c9805 2020-11-24 stsp
7220 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7221 c42c9805 2020-11-24 stsp if (err) {
7222 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7223 c42c9805 2020-11-24 stsp return err;
7224 c42c9805 2020-11-24 stsp else
7225 c42c9805 2020-11-24 stsp return NULL;
7226 c42c9805 2020-11-24 stsp }
7227 c42c9805 2020-11-24 stsp
7228 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7229 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7230 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7231 6458efa5 2020-11-24 stsp goto done;
7232 6458efa5 2020-11-24 stsp }
7233 6458efa5 2020-11-24 stsp
7234 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7235 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7236 6458efa5 2020-11-24 stsp done:
7237 6458efa5 2020-11-24 stsp if (err)
7238 6458efa5 2020-11-24 stsp view_close(log_view);
7239 6458efa5 2020-11-24 stsp else
7240 6458efa5 2020-11-24 stsp *new_view = log_view;
7241 c42c9805 2020-11-24 stsp free(commit_id);
7242 6458efa5 2020-11-24 stsp return err;
7243 6458efa5 2020-11-24 stsp }
7244 6458efa5 2020-11-24 stsp
7245 ce5b7c56 2019-07-09 stsp static void
7246 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7247 6458efa5 2020-11-24 stsp {
7248 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7249 34ba6917 2020-11-30 stsp int i = 0;
7250 6458efa5 2020-11-24 stsp
7251 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7252 6458efa5 2020-11-24 stsp return;
7253 6458efa5 2020-11-24 stsp
7254 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7255 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7256 34ba6917 2020-11-30 stsp if (re == NULL)
7257 34ba6917 2020-11-30 stsp break;
7258 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7259 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7260 6458efa5 2020-11-24 stsp }
7261 6458efa5 2020-11-24 stsp }
7262 6458efa5 2020-11-24 stsp
7263 a5d43cac 2022-07-01 thomas static const struct got_error *
7264 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7265 6458efa5 2020-11-24 stsp {
7266 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7267 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7268 6458efa5 2020-11-24 stsp int n = 0;
7269 6458efa5 2020-11-24 stsp
7270 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7271 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7272 6458efa5 2020-11-24 stsp else
7273 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7274 6458efa5 2020-11-24 stsp
7275 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7276 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7277 a5d43cac 2022-07-01 thomas if (last)
7278 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7279 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7280 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7281 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7282 6458efa5 2020-11-24 stsp }
7283 6458efa5 2020-11-24 stsp }
7284 a5d43cac 2022-07-01 thomas
7285 a5d43cac 2022-07-01 thomas return NULL;
7286 6458efa5 2020-11-24 stsp }
7287 6458efa5 2020-11-24 stsp
7288 6458efa5 2020-11-24 stsp static const struct got_error *
7289 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7290 6458efa5 2020-11-24 stsp {
7291 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7292 6458efa5 2020-11-24 stsp
7293 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7294 6458efa5 2020-11-24 stsp return NULL;
7295 6458efa5 2020-11-24 stsp }
7296 6458efa5 2020-11-24 stsp
7297 6458efa5 2020-11-24 stsp static int
7298 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7299 6458efa5 2020-11-24 stsp {
7300 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7301 6458efa5 2020-11-24 stsp
7302 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7303 6458efa5 2020-11-24 stsp 0) == 0;
7304 6458efa5 2020-11-24 stsp }
7305 6458efa5 2020-11-24 stsp
7306 6458efa5 2020-11-24 stsp static const struct got_error *
7307 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7308 6458efa5 2020-11-24 stsp {
7309 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7310 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7311 6458efa5 2020-11-24 stsp
7312 6458efa5 2020-11-24 stsp if (!view->searching) {
7313 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7314 6458efa5 2020-11-24 stsp return NULL;
7315 6458efa5 2020-11-24 stsp }
7316 6458efa5 2020-11-24 stsp
7317 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7318 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7319 6458efa5 2020-11-24 stsp if (s->selected_entry)
7320 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7321 6458efa5 2020-11-24 stsp else
7322 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7323 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7324 6458efa5 2020-11-24 stsp } else {
7325 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7326 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7327 6458efa5 2020-11-24 stsp else
7328 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7329 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7330 6458efa5 2020-11-24 stsp }
7331 6458efa5 2020-11-24 stsp } else {
7332 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7333 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7334 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7335 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7336 6458efa5 2020-11-24 stsp else
7337 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7338 6458efa5 2020-11-24 stsp }
7339 6458efa5 2020-11-24 stsp
7340 6458efa5 2020-11-24 stsp while (1) {
7341 6458efa5 2020-11-24 stsp if (re == NULL) {
7342 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7343 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7344 6458efa5 2020-11-24 stsp return NULL;
7345 6458efa5 2020-11-24 stsp }
7346 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7347 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7348 6458efa5 2020-11-24 stsp else
7349 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7350 6458efa5 2020-11-24 stsp }
7351 6458efa5 2020-11-24 stsp
7352 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7353 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7354 6458efa5 2020-11-24 stsp s->matched_entry = re;
7355 6458efa5 2020-11-24 stsp break;
7356 6458efa5 2020-11-24 stsp }
7357 6458efa5 2020-11-24 stsp
7358 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7359 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7360 6458efa5 2020-11-24 stsp else
7361 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7362 6458efa5 2020-11-24 stsp }
7363 6458efa5 2020-11-24 stsp
7364 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7365 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7366 6458efa5 2020-11-24 stsp s->selected = 0;
7367 6458efa5 2020-11-24 stsp }
7368 6458efa5 2020-11-24 stsp
7369 6458efa5 2020-11-24 stsp return NULL;
7370 6458efa5 2020-11-24 stsp }
7371 6458efa5 2020-11-24 stsp
7372 6458efa5 2020-11-24 stsp static const struct got_error *
7373 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7374 6458efa5 2020-11-24 stsp {
7375 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7376 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7377 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7378 6458efa5 2020-11-24 stsp char *line = NULL;
7379 6458efa5 2020-11-24 stsp wchar_t *wline;
7380 6458efa5 2020-11-24 stsp struct tog_color *tc;
7381 6458efa5 2020-11-24 stsp int width, n;
7382 6458efa5 2020-11-24 stsp int limit = view->nlines;
7383 6458efa5 2020-11-24 stsp
7384 6458efa5 2020-11-24 stsp werase(view->window);
7385 6458efa5 2020-11-24 stsp
7386 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7387 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7388 a5d43cac 2022-07-01 thomas --limit; /* border */
7389 6458efa5 2020-11-24 stsp
7390 6458efa5 2020-11-24 stsp if (limit == 0)
7391 6458efa5 2020-11-24 stsp return NULL;
7392 6458efa5 2020-11-24 stsp
7393 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7394 6458efa5 2020-11-24 stsp
7395 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7396 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7397 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7398 6458efa5 2020-11-24 stsp
7399 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7400 6458efa5 2020-11-24 stsp if (err) {
7401 6458efa5 2020-11-24 stsp free(line);
7402 6458efa5 2020-11-24 stsp return err;
7403 6458efa5 2020-11-24 stsp }
7404 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7405 6458efa5 2020-11-24 stsp wstandout(view->window);
7406 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7407 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7408 6458efa5 2020-11-24 stsp wstandend(view->window);
7409 6458efa5 2020-11-24 stsp free(wline);
7410 6458efa5 2020-11-24 stsp wline = NULL;
7411 6458efa5 2020-11-24 stsp free(line);
7412 6458efa5 2020-11-24 stsp line = NULL;
7413 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7414 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7415 6458efa5 2020-11-24 stsp if (--limit <= 0)
7416 6458efa5 2020-11-24 stsp return NULL;
7417 6458efa5 2020-11-24 stsp
7418 6458efa5 2020-11-24 stsp n = 0;
7419 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7420 6458efa5 2020-11-24 stsp char *line = NULL;
7421 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7422 6458efa5 2020-11-24 stsp
7423 84227eb1 2022-06-23 thomas if (s->show_date) {
7424 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7425 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7426 84227eb1 2022-06-23 thomas struct got_object_id *id;
7427 84227eb1 2022-06-23 thomas struct tm tm;
7428 84227eb1 2022-06-23 thomas time_t t;
7429 84227eb1 2022-06-23 thomas
7430 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7431 84227eb1 2022-06-23 thomas if (err)
7432 84227eb1 2022-06-23 thomas return err;
7433 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7434 84227eb1 2022-06-23 thomas if (err) {
7435 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7436 84227eb1 2022-06-23 thomas free(id);
7437 84227eb1 2022-06-23 thomas return err;
7438 84227eb1 2022-06-23 thomas }
7439 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7440 84227eb1 2022-06-23 thomas id);
7441 84227eb1 2022-06-23 thomas if (err) {
7442 84227eb1 2022-06-23 thomas free(id);
7443 84227eb1 2022-06-23 thomas return err;
7444 84227eb1 2022-06-23 thomas }
7445 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7446 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7447 84227eb1 2022-06-23 thomas } else {
7448 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7449 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7450 84227eb1 2022-06-23 thomas }
7451 84227eb1 2022-06-23 thomas free(id);
7452 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7453 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7454 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7455 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7456 84227eb1 2022-06-23 thomas }
7457 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7458 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7459 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7460 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7461 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7462 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7463 6458efa5 2020-11-24 stsp struct got_object_id *id;
7464 6458efa5 2020-11-24 stsp char *id_str;
7465 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7466 6458efa5 2020-11-24 stsp if (err)
7467 6458efa5 2020-11-24 stsp return err;
7468 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7469 6458efa5 2020-11-24 stsp if (err) {
7470 6458efa5 2020-11-24 stsp free(id);
7471 6458efa5 2020-11-24 stsp return err;
7472 6458efa5 2020-11-24 stsp }
7473 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7474 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7475 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7476 6458efa5 2020-11-24 stsp free(id);
7477 6458efa5 2020-11-24 stsp free(id_str);
7478 6458efa5 2020-11-24 stsp return err;
7479 6458efa5 2020-11-24 stsp }
7480 6458efa5 2020-11-24 stsp free(id);
7481 6458efa5 2020-11-24 stsp free(id_str);
7482 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7483 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7484 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7485 6458efa5 2020-11-24 stsp
7486 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7487 f91a2b48 2022-06-23 thomas 0, 0);
7488 6458efa5 2020-11-24 stsp if (err) {
7489 6458efa5 2020-11-24 stsp free(line);
7490 6458efa5 2020-11-24 stsp return err;
7491 6458efa5 2020-11-24 stsp }
7492 6458efa5 2020-11-24 stsp if (n == s->selected) {
7493 6458efa5 2020-11-24 stsp if (view->focussed)
7494 6458efa5 2020-11-24 stsp wstandout(view->window);
7495 6458efa5 2020-11-24 stsp s->selected_entry = re;
7496 6458efa5 2020-11-24 stsp }
7497 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7498 6458efa5 2020-11-24 stsp if (tc)
7499 6458efa5 2020-11-24 stsp wattr_on(view->window,
7500 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7501 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7502 6458efa5 2020-11-24 stsp if (tc)
7503 6458efa5 2020-11-24 stsp wattr_off(view->window,
7504 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7505 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7506 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7507 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7508 6458efa5 2020-11-24 stsp wstandend(view->window);
7509 6458efa5 2020-11-24 stsp free(line);
7510 6458efa5 2020-11-24 stsp free(wline);
7511 6458efa5 2020-11-24 stsp wline = NULL;
7512 6458efa5 2020-11-24 stsp n++;
7513 6458efa5 2020-11-24 stsp s->ndisplayed++;
7514 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7515 6458efa5 2020-11-24 stsp
7516 6458efa5 2020-11-24 stsp limit--;
7517 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7518 6458efa5 2020-11-24 stsp }
7519 6458efa5 2020-11-24 stsp
7520 a5d43cac 2022-07-01 thomas view_border(view);
7521 6458efa5 2020-11-24 stsp return err;
7522 6458efa5 2020-11-24 stsp }
7523 6458efa5 2020-11-24 stsp
7524 6458efa5 2020-11-24 stsp static const struct got_error *
7525 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7526 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7527 c42c9805 2020-11-24 stsp {
7528 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7529 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7530 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7531 c42c9805 2020-11-24 stsp
7532 c42c9805 2020-11-24 stsp *new_view = NULL;
7533 c42c9805 2020-11-24 stsp
7534 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7535 c42c9805 2020-11-24 stsp if (err) {
7536 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7537 c42c9805 2020-11-24 stsp return err;
7538 c42c9805 2020-11-24 stsp else
7539 c42c9805 2020-11-24 stsp return NULL;
7540 c42c9805 2020-11-24 stsp }
7541 c42c9805 2020-11-24 stsp
7542 c42c9805 2020-11-24 stsp
7543 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7544 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7545 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7546 c42c9805 2020-11-24 stsp goto done;
7547 c42c9805 2020-11-24 stsp }
7548 c42c9805 2020-11-24 stsp
7549 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7550 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7551 c42c9805 2020-11-24 stsp if (err)
7552 c42c9805 2020-11-24 stsp goto done;
7553 c42c9805 2020-11-24 stsp
7554 c42c9805 2020-11-24 stsp *new_view = tree_view;
7555 c42c9805 2020-11-24 stsp done:
7556 c42c9805 2020-11-24 stsp free(commit_id);
7557 c42c9805 2020-11-24 stsp return err;
7558 c42c9805 2020-11-24 stsp }
7559 c42c9805 2020-11-24 stsp static const struct got_error *
7560 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7561 6458efa5 2020-11-24 stsp {
7562 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7563 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7564 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7565 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
7566 6458efa5 2020-11-24 stsp
7567 6458efa5 2020-11-24 stsp switch (ch) {
7568 6458efa5 2020-11-24 stsp case 'i':
7569 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7570 07b0611c 2022-06-23 thomas view->count = 0;
7571 3bfadbd4 2021-11-20 thomas break;
7572 84227eb1 2022-06-23 thomas case 'm':
7573 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7574 07b0611c 2022-06-23 thomas view->count = 0;
7575 84227eb1 2022-06-23 thomas break;
7576 98182bd0 2021-11-20 thomas case 'o':
7577 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7578 07b0611c 2022-06-23 thomas view->count = 0;
7579 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7580 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7581 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7582 c591440f 2021-11-20 thomas if (err)
7583 c591440f 2021-11-20 thomas break;
7584 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7585 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7586 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7587 3bfadbd4 2021-11-20 thomas if (err)
7588 3bfadbd4 2021-11-20 thomas break;
7589 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7590 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7591 6458efa5 2020-11-24 stsp break;
7592 6458efa5 2020-11-24 stsp case KEY_ENTER:
7593 6458efa5 2020-11-24 stsp case '\r':
7594 07b0611c 2022-06-23 thomas view->count = 0;
7595 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7596 a5d43cac 2022-07-01 thomas break;
7597 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7598 6458efa5 2020-11-24 stsp break;
7599 1be4947a 2022-07-22 thomas case 'T':
7600 07b0611c 2022-06-23 thomas view->count = 0;
7601 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7602 c42c9805 2020-11-24 stsp break;
7603 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
7604 c42c9805 2020-11-24 stsp break;
7605 e4526bf5 2021-09-03 naddy case 'g':
7606 e4526bf5 2021-09-03 naddy case KEY_HOME:
7607 e4526bf5 2021-09-03 naddy s->selected = 0;
7608 07b0611c 2022-06-23 thomas view->count = 0;
7609 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7610 e4526bf5 2021-09-03 naddy break;
7611 e4526bf5 2021-09-03 naddy case 'G':
7612 a5d43cac 2022-07-01 thomas case KEY_END: {
7613 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7614 a5d43cac 2022-07-01 thomas
7615 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7616 a5d43cac 2022-07-01 thomas --eos; /* border */
7617 e4526bf5 2021-09-03 naddy s->selected = 0;
7618 07b0611c 2022-06-23 thomas view->count = 0;
7619 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7620 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7621 e4526bf5 2021-09-03 naddy if (re == NULL)
7622 e4526bf5 2021-09-03 naddy break;
7623 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7624 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7625 e4526bf5 2021-09-03 naddy }
7626 e4526bf5 2021-09-03 naddy if (n > 0)
7627 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7628 e4526bf5 2021-09-03 naddy break;
7629 a5d43cac 2022-07-01 thomas }
7630 6458efa5 2020-11-24 stsp case 'k':
7631 6458efa5 2020-11-24 stsp case KEY_UP:
7632 f7140bf5 2021-10-17 thomas case CTRL('p'):
7633 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7634 6458efa5 2020-11-24 stsp s->selected--;
7635 6458efa5 2020-11-24 stsp break;
7636 34ba6917 2020-11-30 stsp }
7637 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7638 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7639 07b0611c 2022-06-23 thomas view->count = 0;
7640 6458efa5 2020-11-24 stsp break;
7641 70f17a53 2022-06-13 thomas case CTRL('u'):
7642 23427b14 2022-06-23 thomas case 'u':
7643 70f17a53 2022-06-13 thomas nscroll /= 2;
7644 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7645 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7646 6458efa5 2020-11-24 stsp case CTRL('b'):
7647 1c5e5faa 2022-06-23 thomas case 'b':
7648 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7649 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7650 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7651 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7652 07b0611c 2022-06-23 thomas view->count = 0;
7653 6458efa5 2020-11-24 stsp break;
7654 6458efa5 2020-11-24 stsp case 'j':
7655 6458efa5 2020-11-24 stsp case KEY_DOWN:
7656 f7140bf5 2021-10-17 thomas case CTRL('n'):
7657 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7658 6458efa5 2020-11-24 stsp s->selected++;
7659 6458efa5 2020-11-24 stsp break;
7660 6458efa5 2020-11-24 stsp }
7661 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7662 6458efa5 2020-11-24 stsp /* can't scroll any further */
7663 07b0611c 2022-06-23 thomas view->count = 0;
7664 6458efa5 2020-11-24 stsp break;
7665 07b0611c 2022-06-23 thomas }
7666 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7667 6458efa5 2020-11-24 stsp break;
7668 70f17a53 2022-06-13 thomas case CTRL('d'):
7669 23427b14 2022-06-23 thomas case 'd':
7670 70f17a53 2022-06-13 thomas nscroll /= 2;
7671 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7672 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7673 6458efa5 2020-11-24 stsp case CTRL('f'):
7674 1c5e5faa 2022-06-23 thomas case 'f':
7675 4c2d69cb 2022-06-23 thomas case ' ':
7676 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7677 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7678 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7679 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7680 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7681 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7682 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7683 07b0611c 2022-06-23 thomas view->count = 0;
7684 6458efa5 2020-11-24 stsp break;
7685 6458efa5 2020-11-24 stsp }
7686 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7687 6458efa5 2020-11-24 stsp break;
7688 6458efa5 2020-11-24 stsp case CTRL('l'):
7689 07b0611c 2022-06-23 thomas view->count = 0;
7690 8924d611 2020-12-26 stsp tog_free_refs();
7691 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7692 8924d611 2020-12-26 stsp if (err)
7693 8924d611 2020-12-26 stsp break;
7694 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7695 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7696 6458efa5 2020-11-24 stsp break;
7697 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7698 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7699 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7700 6458efa5 2020-11-24 stsp break;
7701 6458efa5 2020-11-24 stsp default:
7702 07b0611c 2022-06-23 thomas view->count = 0;
7703 6458efa5 2020-11-24 stsp break;
7704 6458efa5 2020-11-24 stsp }
7705 6458efa5 2020-11-24 stsp
7706 6458efa5 2020-11-24 stsp return err;
7707 6458efa5 2020-11-24 stsp }
7708 6458efa5 2020-11-24 stsp
7709 6458efa5 2020-11-24 stsp __dead static void
7710 6458efa5 2020-11-24 stsp usage_ref(void)
7711 6458efa5 2020-11-24 stsp {
7712 6458efa5 2020-11-24 stsp endwin();
7713 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7714 6458efa5 2020-11-24 stsp getprogname());
7715 6458efa5 2020-11-24 stsp exit(1);
7716 6458efa5 2020-11-24 stsp }
7717 6458efa5 2020-11-24 stsp
7718 6458efa5 2020-11-24 stsp static const struct got_error *
7719 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7720 6458efa5 2020-11-24 stsp {
7721 6458efa5 2020-11-24 stsp const struct got_error *error;
7722 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7723 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7724 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7725 6458efa5 2020-11-24 stsp int ch;
7726 6458efa5 2020-11-24 stsp struct tog_view *view;
7727 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7728 6458efa5 2020-11-24 stsp
7729 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7730 6458efa5 2020-11-24 stsp switch (ch) {
7731 6458efa5 2020-11-24 stsp case 'r':
7732 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7733 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7734 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7735 6458efa5 2020-11-24 stsp optarg);
7736 6458efa5 2020-11-24 stsp break;
7737 6458efa5 2020-11-24 stsp default:
7738 e99e2d15 2020-11-24 naddy usage_ref();
7739 6458efa5 2020-11-24 stsp /* NOTREACHED */
7740 6458efa5 2020-11-24 stsp }
7741 6458efa5 2020-11-24 stsp }
7742 6458efa5 2020-11-24 stsp
7743 6458efa5 2020-11-24 stsp argc -= optind;
7744 6458efa5 2020-11-24 stsp argv += optind;
7745 6458efa5 2020-11-24 stsp
7746 6458efa5 2020-11-24 stsp if (argc > 1)
7747 e99e2d15 2020-11-24 naddy usage_ref();
7748 7cd52833 2022-06-23 thomas
7749 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7750 7cd52833 2022-06-23 thomas if (error != NULL)
7751 7cd52833 2022-06-23 thomas goto done;
7752 6458efa5 2020-11-24 stsp
7753 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7754 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7755 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7756 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7757 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7758 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7759 c156c7a4 2020-12-18 stsp goto done;
7760 6458efa5 2020-11-24 stsp if (worktree)
7761 6458efa5 2020-11-24 stsp repo_path =
7762 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7763 6458efa5 2020-11-24 stsp else
7764 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7765 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7766 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7767 c156c7a4 2020-12-18 stsp goto done;
7768 c156c7a4 2020-12-18 stsp }
7769 6458efa5 2020-11-24 stsp }
7770 6458efa5 2020-11-24 stsp
7771 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7772 6458efa5 2020-11-24 stsp if (error != NULL)
7773 6458efa5 2020-11-24 stsp goto done;
7774 6458efa5 2020-11-24 stsp
7775 6458efa5 2020-11-24 stsp init_curses();
7776 6458efa5 2020-11-24 stsp
7777 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7778 51a10b52 2020-12-26 stsp if (error)
7779 51a10b52 2020-12-26 stsp goto done;
7780 51a10b52 2020-12-26 stsp
7781 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7782 6458efa5 2020-11-24 stsp if (error)
7783 6458efa5 2020-11-24 stsp goto done;
7784 6458efa5 2020-11-24 stsp
7785 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7786 6458efa5 2020-11-24 stsp if (view == NULL) {
7787 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7788 6458efa5 2020-11-24 stsp goto done;
7789 6458efa5 2020-11-24 stsp }
7790 6458efa5 2020-11-24 stsp
7791 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7792 6458efa5 2020-11-24 stsp if (error)
7793 6458efa5 2020-11-24 stsp goto done;
7794 6458efa5 2020-11-24 stsp
7795 6458efa5 2020-11-24 stsp if (worktree) {
7796 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7797 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7798 6458efa5 2020-11-24 stsp worktree = NULL;
7799 6458efa5 2020-11-24 stsp }
7800 6458efa5 2020-11-24 stsp error = view_loop(view);
7801 6458efa5 2020-11-24 stsp done:
7802 6458efa5 2020-11-24 stsp free(repo_path);
7803 6458efa5 2020-11-24 stsp free(cwd);
7804 1d0f4054 2021-06-17 stsp if (repo) {
7805 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7806 1d0f4054 2021-06-17 stsp if (close_err)
7807 1d0f4054 2021-06-17 stsp error = close_err;
7808 1d0f4054 2021-06-17 stsp }
7809 7cd52833 2022-06-23 thomas if (pack_fds) {
7810 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7811 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7812 7cd52833 2022-06-23 thomas if (error == NULL)
7813 7cd52833 2022-06-23 thomas error = pack_err;
7814 7cd52833 2022-06-23 thomas }
7815 51a10b52 2020-12-26 stsp tog_free_refs();
7816 6458efa5 2020-11-24 stsp return error;
7817 6458efa5 2020-11-24 stsp }
7818 6458efa5 2020-11-24 stsp
7819 2a31b33b 2022-07-23 thomas static const struct got_error *
7820 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
7821 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
7822 2a31b33b 2022-07-23 thomas {
7823 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
7824 2a31b33b 2022-07-23 thomas
7825 2a31b33b 2022-07-23 thomas *new_view = NULL;
7826 2a31b33b 2022-07-23 thomas
7827 2a31b33b 2022-07-23 thomas switch (request) {
7828 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
7829 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
7830 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
7831 2a31b33b 2022-07-23 thomas
7832 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
7833 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
7834 2a31b33b 2022-07-23 thomas view, s->repo);
7835 2a31b33b 2022-07-23 thomas } else
7836 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
7837 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7838 2a31b33b 2022-07-23 thomas break;
7839 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
7840 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
7841 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
7842 2a31b33b 2022-07-23 thomas
7843 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
7844 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
7845 2a31b33b 2022-07-23 thomas s->repo);
7846 2a31b33b 2022-07-23 thomas } else
7847 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
7848 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7849 2a31b33b 2022-07-23 thomas break;
7850 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
7851 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
7852 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
7853 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
7854 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
7855 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
7856 2a31b33b 2022-07-23 thomas &view->state.tree);
7857 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
7858 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
7859 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
7860 2a31b33b 2022-07-23 thomas view->state.ref.repo);
7861 2a31b33b 2022-07-23 thomas else
7862 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
7863 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7864 2a31b33b 2022-07-23 thomas break;
7865 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
7866 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
7867 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
7868 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
7869 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
7870 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
7871 2a31b33b 2022-07-23 thomas view->state.log.repo);
7872 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
7873 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
7874 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
7875 2a31b33b 2022-07-23 thomas view->state.ref.repo);
7876 2a31b33b 2022-07-23 thomas else
7877 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
7878 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7879 2a31b33b 2022-07-23 thomas break;
7880 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
7881 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
7882 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
7883 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
7884 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
7885 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
7886 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
7887 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
7888 2a31b33b 2022-07-23 thomas else
7889 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
7890 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
7891 2a31b33b 2022-07-23 thomas if (err)
7892 2a31b33b 2022-07-23 thomas view_close(*new_view);
7893 2a31b33b 2022-07-23 thomas break;
7894 2a31b33b 2022-07-23 thomas default:
7895 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
7896 2a31b33b 2022-07-23 thomas }
7897 2a31b33b 2022-07-23 thomas
7898 2a31b33b 2022-07-23 thomas return err;
7899 2a31b33b 2022-07-23 thomas }
7900 2a31b33b 2022-07-23 thomas
7901 a5d43cac 2022-07-01 thomas /*
7902 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
7903 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
7904 a5d43cac 2022-07-01 thomas */
7905 6458efa5 2020-11-24 stsp static void
7906 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
7907 a5d43cac 2022-07-01 thomas {
7908 a5d43cac 2022-07-01 thomas switch (view->type) {
7909 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7910 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7911 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
7912 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
7913 a5d43cac 2022-07-01 thomas 1);
7914 a5d43cac 2022-07-01 thomas break;
7915 a5d43cac 2022-07-01 thomas }
7916 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
7917 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
7918 a5d43cac 2022-07-01 thomas else
7919 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
7920 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
7921 a5d43cac 2022-07-01 thomas break;
7922 a5d43cac 2022-07-01 thomas }
7923 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
7924 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
7925 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
7926 a5d43cac 2022-07-01 thomas break;
7927 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
7928 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
7929 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
7930 a5d43cac 2022-07-01 thomas break;
7931 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
7932 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
7933 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
7934 a5d43cac 2022-07-01 thomas break;
7935 a5d43cac 2022-07-01 thomas default:
7936 a5d43cac 2022-07-01 thomas break;
7937 a5d43cac 2022-07-01 thomas }
7938 a5d43cac 2022-07-01 thomas
7939 a5d43cac 2022-07-01 thomas view->offset = 0;
7940 a5d43cac 2022-07-01 thomas }
7941 a5d43cac 2022-07-01 thomas
7942 a5d43cac 2022-07-01 thomas /*
7943 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
7944 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
7945 a5d43cac 2022-07-01 thomas */
7946 a5d43cac 2022-07-01 thomas static const struct got_error *
7947 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
7948 a5d43cac 2022-07-01 thomas {
7949 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
7950 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
7951 a5d43cac 2022-07-01 thomas int *selected = NULL;
7952 a5d43cac 2022-07-01 thomas int header, offset;
7953 a5d43cac 2022-07-01 thomas
7954 a5d43cac 2022-07-01 thomas switch (view->type) {
7955 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7956 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7957 a5d43cac 2022-07-01 thomas header = 3;
7958 a5d43cac 2022-07-01 thomas scrolld = NULL;
7959 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
7960 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
7961 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
7962 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
7963 a5d43cac 2022-07-01 thomas view->offset = offset;
7964 a5d43cac 2022-07-01 thomas }
7965 a5d43cac 2022-07-01 thomas break;
7966 a5d43cac 2022-07-01 thomas }
7967 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
7968 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
7969 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
7970 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
7971 a5d43cac 2022-07-01 thomas selected = &s->selected;
7972 a5d43cac 2022-07-01 thomas break;
7973 a5d43cac 2022-07-01 thomas }
7974 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
7975 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7976 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
7977 a5d43cac 2022-07-01 thomas header = 3;
7978 a5d43cac 2022-07-01 thomas selected = &s->selected;
7979 a5d43cac 2022-07-01 thomas break;
7980 a5d43cac 2022-07-01 thomas }
7981 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
7982 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7983 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
7984 a5d43cac 2022-07-01 thomas header = 5;
7985 a5d43cac 2022-07-01 thomas selected = &s->selected;
7986 a5d43cac 2022-07-01 thomas break;
7987 a5d43cac 2022-07-01 thomas }
7988 a5d43cac 2022-07-01 thomas default:
7989 a5d43cac 2022-07-01 thomas selected = NULL;
7990 a5d43cac 2022-07-01 thomas scrolld = NULL;
7991 a5d43cac 2022-07-01 thomas header = 0;
7992 a5d43cac 2022-07-01 thomas break;
7993 a5d43cac 2022-07-01 thomas }
7994 a5d43cac 2022-07-01 thomas
7995 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
7996 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
7997 a5d43cac 2022-07-01 thomas view->offset = offset;
7998 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
7999 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8000 a5d43cac 2022-07-01 thomas *selected -= offset;
8001 a5d43cac 2022-07-01 thomas }
8002 a5d43cac 2022-07-01 thomas }
8003 a5d43cac 2022-07-01 thomas
8004 a5d43cac 2022-07-01 thomas return err;
8005 a5d43cac 2022-07-01 thomas }
8006 a5d43cac 2022-07-01 thomas
8007 a5d43cac 2022-07-01 thomas static void
8008 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8009 ce5b7c56 2019-07-09 stsp {
8010 6059809a 2020-12-17 stsp size_t i;
8011 9f7d7167 2018-04-29 stsp
8012 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8013 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8014 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8015 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8016 ce5b7c56 2019-07-09 stsp }
8017 6879ba42 2020-10-01 naddy fputc('\n', fp);
8018 ce5b7c56 2019-07-09 stsp }
8019 ce5b7c56 2019-07-09 stsp
8020 4ed7e80c 2018-05-20 stsp __dead static void
8021 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8022 9f7d7167 2018-04-29 stsp {
8023 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8024 6879ba42 2020-10-01 naddy
8025 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8026 6879ba42 2020-10-01 naddy getprogname());
8027 6879ba42 2020-10-01 naddy if (hflag) {
8028 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8029 6879ba42 2020-10-01 naddy list_commands(fp);
8030 ee85c5e8 2020-02-29 stsp }
8031 6879ba42 2020-10-01 naddy exit(status);
8032 9f7d7167 2018-04-29 stsp }
8033 9f7d7167 2018-04-29 stsp
8034 c2301be8 2018-04-30 stsp static char **
8035 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8036 c2301be8 2018-04-30 stsp {
8037 ee85c5e8 2020-02-29 stsp va_list ap;
8038 c2301be8 2018-04-30 stsp char **argv;
8039 ee85c5e8 2020-02-29 stsp int i;
8040 c2301be8 2018-04-30 stsp
8041 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8042 ee85c5e8 2020-02-29 stsp
8043 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8044 c2301be8 2018-04-30 stsp if (argv == NULL)
8045 c2301be8 2018-04-30 stsp err(1, "calloc");
8046 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8047 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8048 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8049 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8050 c2301be8 2018-04-30 stsp }
8051 c2301be8 2018-04-30 stsp
8052 ee85c5e8 2020-02-29 stsp va_end(ap);
8053 c2301be8 2018-04-30 stsp return argv;
8054 ee85c5e8 2020-02-29 stsp }
8055 ee85c5e8 2020-02-29 stsp
8056 ee85c5e8 2020-02-29 stsp /*
8057 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8058 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8059 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8060 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8061 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8062 ee85c5e8 2020-02-29 stsp */
8063 ee85c5e8 2020-02-29 stsp static const struct got_error *
8064 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8065 ee85c5e8 2020-02-29 stsp {
8066 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8067 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8068 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8069 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8070 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8071 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8072 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8073 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8074 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8075 84de9106 2020-12-26 stsp
8076 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8077 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8078 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8079 ee85c5e8 2020-02-29 stsp
8080 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8081 7cd52833 2022-06-23 thomas if (error != NULL)
8082 7cd52833 2022-06-23 thomas goto done;
8083 7cd52833 2022-06-23 thomas
8084 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8085 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8086 ee85c5e8 2020-02-29 stsp goto done;
8087 ee85c5e8 2020-02-29 stsp
8088 ee85c5e8 2020-02-29 stsp if (worktree)
8089 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8090 ee85c5e8 2020-02-29 stsp else
8091 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8092 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8093 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8094 ee85c5e8 2020-02-29 stsp goto done;
8095 ee85c5e8 2020-02-29 stsp }
8096 ee85c5e8 2020-02-29 stsp
8097 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8098 ee85c5e8 2020-02-29 stsp if (error != NULL)
8099 ee85c5e8 2020-02-29 stsp goto done;
8100 ee85c5e8 2020-02-29 stsp
8101 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8102 ee85c5e8 2020-02-29 stsp repo, worktree);
8103 ee85c5e8 2020-02-29 stsp if (error)
8104 ee85c5e8 2020-02-29 stsp goto done;
8105 ee85c5e8 2020-02-29 stsp
8106 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8107 84de9106 2020-12-26 stsp if (error)
8108 84de9106 2020-12-26 stsp goto done;
8109 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8110 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8111 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8112 ee85c5e8 2020-02-29 stsp if (error)
8113 ee85c5e8 2020-02-29 stsp goto done;
8114 ee85c5e8 2020-02-29 stsp
8115 ee85c5e8 2020-02-29 stsp if (worktree) {
8116 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8117 ee85c5e8 2020-02-29 stsp worktree = NULL;
8118 ee85c5e8 2020-02-29 stsp }
8119 ee85c5e8 2020-02-29 stsp
8120 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8121 945f9229 2022-04-16 thomas if (error)
8122 945f9229 2022-04-16 thomas goto done;
8123 945f9229 2022-04-16 thomas
8124 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8125 ee85c5e8 2020-02-29 stsp if (error) {
8126 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8127 ee85c5e8 2020-02-29 stsp goto done;
8128 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8129 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8130 6879ba42 2020-10-01 naddy usage(1, 1);
8131 ee85c5e8 2020-02-29 stsp /* not reached */
8132 ee85c5e8 2020-02-29 stsp }
8133 ee85c5e8 2020-02-29 stsp
8134 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8135 1d0f4054 2021-06-17 stsp if (error == NULL)
8136 1d0f4054 2021-06-17 stsp error = close_err;
8137 ee85c5e8 2020-02-29 stsp repo = NULL;
8138 ee85c5e8 2020-02-29 stsp
8139 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8140 ee85c5e8 2020-02-29 stsp if (error)
8141 ee85c5e8 2020-02-29 stsp goto done;
8142 ee85c5e8 2020-02-29 stsp
8143 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8144 ee85c5e8 2020-02-29 stsp argc = 4;
8145 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8146 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8147 ee85c5e8 2020-02-29 stsp done:
8148 1d0f4054 2021-06-17 stsp if (repo) {
8149 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8150 1d0f4054 2021-06-17 stsp if (error == NULL)
8151 1d0f4054 2021-06-17 stsp error = close_err;
8152 1d0f4054 2021-06-17 stsp }
8153 945f9229 2022-04-16 thomas if (commit)
8154 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8155 ee85c5e8 2020-02-29 stsp if (worktree)
8156 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8157 7cd52833 2022-06-23 thomas if (pack_fds) {
8158 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8159 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8160 7cd52833 2022-06-23 thomas if (error == NULL)
8161 7cd52833 2022-06-23 thomas error = pack_err;
8162 7cd52833 2022-06-23 thomas }
8163 ee85c5e8 2020-02-29 stsp free(id);
8164 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8165 ee85c5e8 2020-02-29 stsp free(commit_id);
8166 ee85c5e8 2020-02-29 stsp free(cwd);
8167 ee85c5e8 2020-02-29 stsp free(repo_path);
8168 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8169 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8170 ee85c5e8 2020-02-29 stsp int i;
8171 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8172 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8173 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8174 ee85c5e8 2020-02-29 stsp }
8175 87670572 2020-12-26 naddy tog_free_refs();
8176 ee85c5e8 2020-02-29 stsp return error;
8177 c2301be8 2018-04-30 stsp }
8178 c2301be8 2018-04-30 stsp
8179 9f7d7167 2018-04-29 stsp int
8180 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8181 9f7d7167 2018-04-29 stsp {
8182 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8183 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8184 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8185 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8186 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8187 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8188 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8189 83cd27f8 2020-01-13 stsp };
8190 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8191 9f7d7167 2018-04-29 stsp
8192 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8193 9f7d7167 2018-04-29 stsp
8194 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8195 9f7d7167 2018-04-29 stsp switch (ch) {
8196 9f7d7167 2018-04-29 stsp case 'h':
8197 9f7d7167 2018-04-29 stsp hflag = 1;
8198 9f7d7167 2018-04-29 stsp break;
8199 53ccebc2 2019-07-30 stsp case 'V':
8200 53ccebc2 2019-07-30 stsp Vflag = 1;
8201 53ccebc2 2019-07-30 stsp break;
8202 9f7d7167 2018-04-29 stsp default:
8203 6879ba42 2020-10-01 naddy usage(hflag, 1);
8204 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8205 9f7d7167 2018-04-29 stsp }
8206 9f7d7167 2018-04-29 stsp }
8207 9f7d7167 2018-04-29 stsp
8208 9f7d7167 2018-04-29 stsp argc -= optind;
8209 9f7d7167 2018-04-29 stsp argv += optind;
8210 9814e6a3 2020-09-27 naddy optind = 1;
8211 c2301be8 2018-04-30 stsp optreset = 1;
8212 9f7d7167 2018-04-29 stsp
8213 53ccebc2 2019-07-30 stsp if (Vflag) {
8214 53ccebc2 2019-07-30 stsp got_version_print_str();
8215 6879ba42 2020-10-01 naddy return 0;
8216 53ccebc2 2019-07-30 stsp }
8217 4010e238 2020-12-04 stsp
8218 4010e238 2020-12-04 stsp #ifndef PROFILE
8219 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8220 4010e238 2020-12-04 stsp NULL) == -1)
8221 4010e238 2020-12-04 stsp err(1, "pledge");
8222 4010e238 2020-12-04 stsp #endif
8223 53ccebc2 2019-07-30 stsp
8224 c2301be8 2018-04-30 stsp if (argc == 0) {
8225 f29d3e89 2018-06-23 stsp if (hflag)
8226 6879ba42 2020-10-01 naddy usage(hflag, 0);
8227 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8228 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8229 c2301be8 2018-04-30 stsp argc = 1;
8230 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8231 c2301be8 2018-04-30 stsp } else {
8232 6059809a 2020-12-17 stsp size_t i;
8233 9f7d7167 2018-04-29 stsp
8234 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8235 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8236 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8237 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8238 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8239 9f7d7167 2018-04-29 stsp break;
8240 9f7d7167 2018-04-29 stsp }
8241 9f7d7167 2018-04-29 stsp }
8242 ee85c5e8 2020-02-29 stsp }
8243 3642c4c6 2019-07-09 stsp
8244 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8245 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8246 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8247 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8248 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8249 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8250 adf4c9e0 2022-07-03 thomas }
8251 adf4c9e0 2022-07-03 thomas
8252 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8253 ee85c5e8 2020-02-29 stsp if (argc != 1)
8254 6879ba42 2020-10-01 naddy usage(0, 1);
8255 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8256 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8257 ee85c5e8 2020-02-29 stsp } else {
8258 ee85c5e8 2020-02-29 stsp if (hflag)
8259 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8260 ee85c5e8 2020-02-29 stsp else
8261 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8262 9f7d7167 2018-04-29 stsp }
8263 9f7d7167 2018-04-29 stsp
8264 9f7d7167 2018-04-29 stsp endwin();
8265 b46c1e04 2020-09-20 naddy putchar('\n');
8266 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8267 a2f4a359 2020-02-28 stsp int i;
8268 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8269 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8270 a2f4a359 2020-02-28 stsp free(cmd_argv);
8271 a2f4a359 2020-02-28 stsp }
8272 a2f4a359 2020-02-28 stsp
8273 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8274 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8275 9f7d7167 2018-04-29 stsp return 0;
8276 9f7d7167 2018-04-29 stsp }