Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
24 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 def2f970 2021-09-28 thomas #endif
26 9f7d7167 2018-04-29 stsp #include <curses.h>
27 9f7d7167 2018-04-29 stsp #include <panel.h>
28 9f7d7167 2018-04-29 stsp #include <locale.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
44 9f7d7167 2018-04-29 stsp
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
46 dd038bc6 2021-09-21 thomas.ad
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
61 9f7d7167 2018-04-29 stsp
62 881b2d3e 2018-04-30 stsp #ifndef MIN
63 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 881b2d3e 2018-04-30 stsp #endif
65 881b2d3e 2018-04-30 stsp
66 2bd27830 2018-10-22 stsp #ifndef MAX
67 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 2bd27830 2018-10-22 stsp #endif
69 2bd27830 2018-10-22 stsp
70 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
71 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
72 acf52a76 2021-09-21 thomas.ad #endif
73 2bd27830 2018-10-22 stsp
74 9f7d7167 2018-04-29 stsp #ifndef nitems
75 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 9f7d7167 2018-04-29 stsp #endif
77 9f7d7167 2018-04-29 stsp
78 9f7d7167 2018-04-29 stsp struct tog_cmd {
79 c2301be8 2018-04-30 stsp const char *name;
80 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
82 9f7d7167 2018-04-29 stsp };
83 9f7d7167 2018-04-29 stsp
84 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
88 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
89 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 9f7d7167 2018-04-29 stsp
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
94 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
95 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 9f7d7167 2018-04-29 stsp
97 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
98 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
99 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
100 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
101 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
102 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
103 9f7d7167 2018-04-29 stsp };
104 9f7d7167 2018-04-29 stsp
105 d6b05b5b 2018-08-04 stsp enum tog_view_type {
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
107 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
108 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
109 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
110 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
111 d6b05b5b 2018-08-04 stsp };
112 c3e9aa98 2019-05-13 jcs
113 a5d43cac 2022-07-01 thomas enum tog_view_mode {
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
117 a5d43cac 2022-07-01 thomas };
118 a5d43cac 2022-07-01 thomas
119 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 a5d43cac 2022-07-01 thomas
121 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
122 d6b05b5b 2018-08-04 stsp
123 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
124 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
125 ba4f502b 2018-08-04 stsp struct got_object_id *id;
126 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
127 1a76625f 2018-10-22 stsp int idx;
128 ba4f502b 2018-08-04 stsp };
129 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 ba4f502b 2018-08-04 stsp struct commit_queue {
131 ba4f502b 2018-08-04 stsp int ncommits;
132 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
133 6d17833f 2019-11-08 stsp };
134 6d17833f 2019-11-08 stsp
135 f26dddb7 2019-11-08 stsp struct tog_color {
136 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
137 6d17833f 2019-11-08 stsp regex_t regex;
138 6d17833f 2019-11-08 stsp short colorpair;
139 15a087fe 2019-02-21 stsp };
140 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
141 11b20872 2019-11-08 stsp
142 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
144 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 2183bbf6 2022-01-23 thomas
146 2183bbf6 2022-01-23 thomas static const struct got_error *
147 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 2183bbf6 2022-01-23 thomas struct got_reference* re2)
149 2183bbf6 2022-01-23 thomas {
150 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
151 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
152 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
153 2183bbf6 2022-01-23 thomas
154 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
155 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
158 2183bbf6 2022-01-23 thomas *cmp = -1;
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
161 2183bbf6 2022-01-23 thomas *cmp = 1;
162 2183bbf6 2022-01-23 thomas return NULL;
163 2183bbf6 2022-01-23 thomas }
164 2183bbf6 2022-01-23 thomas
165 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 2183bbf6 2022-01-23 thomas return NULL;
167 2183bbf6 2022-01-23 thomas }
168 51a10b52 2020-12-26 stsp
169 11b20872 2019-11-08 stsp static const struct got_error *
170 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
171 51a10b52 2020-12-26 stsp {
172 51a10b52 2020-12-26 stsp const struct got_error *err;
173 51a10b52 2020-12-26 stsp
174 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 3bfadbd4 2021-11-20 thomas repo);
177 51a10b52 2020-12-26 stsp if (err)
178 51a10b52 2020-12-26 stsp return err;
179 51a10b52 2020-12-26 stsp
180 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 51a10b52 2020-12-26 stsp repo);
182 51a10b52 2020-12-26 stsp }
183 51a10b52 2020-12-26 stsp
184 51a10b52 2020-12-26 stsp static void
185 51a10b52 2020-12-26 stsp tog_free_refs(void)
186 51a10b52 2020-12-26 stsp {
187 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
188 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
189 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
190 51a10b52 2020-12-26 stsp }
191 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
192 51a10b52 2020-12-26 stsp }
193 51a10b52 2020-12-26 stsp
194 51a10b52 2020-12-26 stsp static const struct got_error *
195 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
196 11b20872 2019-11-08 stsp int idx, short color)
197 11b20872 2019-11-08 stsp {
198 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
199 11b20872 2019-11-08 stsp struct tog_color *tc;
200 11b20872 2019-11-08 stsp int regerr = 0;
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
203 11b20872 2019-11-08 stsp return NULL;
204 11b20872 2019-11-08 stsp
205 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
206 11b20872 2019-11-08 stsp
207 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
208 11b20872 2019-11-08 stsp if (tc == NULL)
209 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
210 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
211 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 11b20872 2019-11-08 stsp if (regerr) {
213 11b20872 2019-11-08 stsp static char regerr_msg[512];
214 11b20872 2019-11-08 stsp static char err_msg[512];
215 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
216 11b20872 2019-11-08 stsp sizeof(regerr_msg));
217 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 11b20872 2019-11-08 stsp regerr_msg);
219 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 11b20872 2019-11-08 stsp free(tc);
221 11b20872 2019-11-08 stsp return err;
222 11b20872 2019-11-08 stsp }
223 11b20872 2019-11-08 stsp tc->colorpair = idx;
224 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
225 11b20872 2019-11-08 stsp return NULL;
226 11b20872 2019-11-08 stsp }
227 11b20872 2019-11-08 stsp
228 11b20872 2019-11-08 stsp static void
229 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
230 11b20872 2019-11-08 stsp {
231 11b20872 2019-11-08 stsp struct tog_color *tc;
232 11b20872 2019-11-08 stsp
233 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
234 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
235 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
236 11b20872 2019-11-08 stsp regfree(&tc->regex);
237 11b20872 2019-11-08 stsp free(tc);
238 11b20872 2019-11-08 stsp }
239 11b20872 2019-11-08 stsp }
240 11b20872 2019-11-08 stsp
241 ef20f542 2022-06-26 thomas static struct tog_color *
242 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
243 11b20872 2019-11-08 stsp {
244 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
245 11b20872 2019-11-08 stsp
246 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
247 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
248 11b20872 2019-11-08 stsp return tc;
249 11b20872 2019-11-08 stsp }
250 11b20872 2019-11-08 stsp
251 11b20872 2019-11-08 stsp return NULL;
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 11b20872 2019-11-08 stsp static int
255 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
256 11b20872 2019-11-08 stsp {
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_CYAN;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 11b20872 2019-11-08 stsp return COLOR_YELLOW;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 11b20872 2019-11-08 stsp return COLOR_GREEN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_GREEN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 11b20872 2019-11-08 stsp return COLOR_CYAN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 11b20872 2019-11-08 stsp return COLOR_YELLOW;
279 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 6458efa5 2020-11-24 stsp return COLOR_GREEN;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
285 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
287 11b20872 2019-11-08 stsp
288 11b20872 2019-11-08 stsp return -1;
289 11b20872 2019-11-08 stsp }
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp static int
292 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
293 11b20872 2019-11-08 stsp {
294 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
295 11b20872 2019-11-08 stsp
296 11b20872 2019-11-08 stsp if (val == NULL)
297 11b20872 2019-11-08 stsp return default_color_value(envvar);
298 15a087fe 2019-02-21 stsp
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
300 11b20872 2019-11-08 stsp return COLOR_BLACK;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
302 11b20872 2019-11-08 stsp return COLOR_RED;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
304 11b20872 2019-11-08 stsp return COLOR_GREEN;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
306 11b20872 2019-11-08 stsp return COLOR_YELLOW;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
308 11b20872 2019-11-08 stsp return COLOR_BLUE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
310 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
312 11b20872 2019-11-08 stsp return COLOR_CYAN;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
314 11b20872 2019-11-08 stsp return COLOR_WHITE;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
316 11b20872 2019-11-08 stsp return -1;
317 11b20872 2019-11-08 stsp
318 11b20872 2019-11-08 stsp return default_color_value(envvar);
319 11b20872 2019-11-08 stsp }
320 11b20872 2019-11-08 stsp
321 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
322 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
323 3dbaef42 2020-11-24 stsp const char *label1, *label2;
324 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
325 19a6a6b5 2022-07-01 thomas int fd1, fd2;
326 82c78e96 2022-08-06 thomas int lineno;
327 15a087fe 2019-02-21 stsp int first_displayed_line;
328 15a087fe 2019-02-21 stsp int last_displayed_line;
329 15a087fe 2019-02-21 stsp int eof;
330 15a087fe 2019-02-21 stsp int diff_context;
331 3dbaef42 2020-11-24 stsp int ignore_whitespace;
332 64453f7e 2020-11-21 stsp int force_text_diff;
333 15a087fe 2019-02-21 stsp struct got_repository *repo;
334 82c78e96 2022-08-06 thomas struct got_diff_line *lines;
335 fe621944 2020-11-10 stsp size_t nlines;
336 f44b1f58 2020-02-02 tracey int matched_line;
337 f44b1f58 2020-02-02 tracey int selected_line;
338 15a087fe 2019-02-21 stsp
339 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
340 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
341 b01e7d3b 2018-08-04 stsp };
342 b01e7d3b 2018-08-04 stsp
343 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
345 1a76625f 2018-10-22 stsp
346 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
347 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
348 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
349 1a76625f 2018-10-22 stsp int commits_needed;
350 fb280deb 2021-08-30 stsp int load_all;
351 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
352 1a76625f 2018-10-22 stsp struct commit_queue *commits;
353 1a76625f 2018-10-22 stsp const char *in_repo_path;
354 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
355 1a76625f 2018-10-22 stsp struct got_repository *repo;
356 1af5eddf 2022-06-23 thomas int *pack_fds;
357 1a76625f 2018-10-22 stsp int log_complete;
358 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
359 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
361 13add988 2019-10-15 stsp int *searching;
362 13add988 2019-10-15 stsp int *search_next_done;
363 13add988 2019-10-15 stsp regex_t *regex;
364 1a76625f 2018-10-22 stsp };
365 1a76625f 2018-10-22 stsp
366 1a76625f 2018-10-22 stsp struct tog_log_view_state {
367 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
368 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
371 b01e7d3b 2018-08-04 stsp int selected;
372 b01e7d3b 2018-08-04 stsp char *in_repo_path;
373 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
374 b672a97a 2020-01-27 stsp int log_branches;
375 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
376 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
377 1a76625f 2018-10-22 stsp sig_atomic_t quit;
378 1a76625f 2018-10-22 stsp pthread_t thread;
379 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
380 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
381 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
382 11b20872 2019-11-08 stsp struct tog_colors colors;
383 f69c5a46 2022-07-19 thomas int use_committer;
384 ba4f502b 2018-08-04 stsp };
385 11b20872 2019-11-08 stsp
386 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
390 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
394 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
395 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
396 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
397 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
400 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
401 ba4f502b 2018-08-04 stsp
402 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
403 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
404 e9424729 2018-08-04 stsp int nlines;
405 e9424729 2018-08-04 stsp
406 e9424729 2018-08-04 stsp struct tog_view *view;
407 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
408 e9424729 2018-08-04 stsp int *quit;
409 e9424729 2018-08-04 stsp };
410 e9424729 2018-08-04 stsp
411 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
412 e9424729 2018-08-04 stsp const char *path;
413 e9424729 2018-08-04 stsp struct got_repository *repo;
414 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
415 e9424729 2018-08-04 stsp int *complete;
416 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
417 fc06ba56 2019-08-22 stsp void *cancel_arg;
418 e9424729 2018-08-04 stsp };
419 e9424729 2018-08-04 stsp
420 e9424729 2018-08-04 stsp struct tog_blame {
421 e9424729 2018-08-04 stsp FILE *f;
422 be659d10 2020-11-18 stsp off_t filesize;
423 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
424 6fcac457 2018-11-19 stsp int nlines;
425 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
426 e9424729 2018-08-04 stsp pthread_t thread;
427 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
428 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
429 e9424729 2018-08-04 stsp const char *path;
430 7cd52833 2022-06-23 thomas int *pack_fds;
431 e9424729 2018-08-04 stsp };
432 e9424729 2018-08-04 stsp
433 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
434 7cbe629d 2018-08-04 stsp int first_displayed_line;
435 7cbe629d 2018-08-04 stsp int last_displayed_line;
436 7cbe629d 2018-08-04 stsp int selected_line;
437 4fc71f3b 2022-07-12 thomas int last_diffed_line;
438 7cbe629d 2018-08-04 stsp int blame_complete;
439 e5a0f69f 2018-08-18 stsp int eof;
440 e5a0f69f 2018-08-18 stsp int done;
441 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
442 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
443 e5a0f69f 2018-08-18 stsp char *path;
444 7cbe629d 2018-08-04 stsp struct got_repository *repo;
445 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
446 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
447 e9424729 2018-08-04 stsp struct tog_blame blame;
448 6c4c42e0 2019-06-24 stsp int matched_line;
449 11b20872 2019-11-08 stsp struct tog_colors colors;
450 ad80ab7b 2018-08-04 stsp };
451 ad80ab7b 2018-08-04 stsp
452 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
453 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
454 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 ad80ab7b 2018-08-04 stsp int selected;
458 ad80ab7b 2018-08-04 stsp };
459 ad80ab7b 2018-08-04 stsp
460 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
461 ad80ab7b 2018-08-04 stsp
462 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
463 ad80ab7b 2018-08-04 stsp char *tree_label;
464 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
465 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
466 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
469 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
470 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
471 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
473 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
474 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
475 6458efa5 2020-11-24 stsp struct tog_colors colors;
476 6458efa5 2020-11-24 stsp };
477 6458efa5 2020-11-24 stsp
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
479 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
480 6458efa5 2020-11-24 stsp struct got_reference *ref;
481 6458efa5 2020-11-24 stsp int idx;
482 6458efa5 2020-11-24 stsp };
483 6458efa5 2020-11-24 stsp
484 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
485 6458efa5 2020-11-24 stsp
486 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
487 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
490 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
491 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 6458efa5 2020-11-24 stsp struct got_repository *repo;
493 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
494 bddb1296 2019-11-08 stsp struct tog_colors colors;
495 7cbe629d 2018-08-04 stsp };
496 7cbe629d 2018-08-04 stsp
497 669b5ffa 2018-10-07 stsp /*
498 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
499 669b5ffa 2018-10-07 stsp *
500 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
501 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
502 669b5ffa 2018-10-07 stsp * there is enough screen estate.
503 669b5ffa 2018-10-07 stsp *
504 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
505 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
506 669b5ffa 2018-10-07 stsp *
507 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
508 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
509 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
510 669b5ffa 2018-10-07 stsp *
511 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
512 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
513 669b5ffa 2018-10-07 stsp */
514 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
515 669b5ffa 2018-10-07 stsp
516 cc3c9aac 2018-08-01 stsp struct tog_view {
517 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
518 26ed57b2 2018-05-19 stsp WINDOW *window;
519 26ed57b2 2018-05-19 stsp PANEL *panel;
520 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
522 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
523 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
524 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
526 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
527 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
528 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
529 9970f7fc 2020-12-03 stsp int dying;
530 669b5ffa 2018-10-07 stsp struct tog_view *parent;
531 669b5ffa 2018-10-07 stsp struct tog_view *child;
532 5dc9f4bc 2018-08-04 stsp
533 e78dc838 2020-12-04 stsp /*
534 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
535 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
536 e78dc838 2020-12-04 stsp * between parent and child.
537 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
538 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
539 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
540 e78dc838 2020-12-04 stsp * situations.
541 e78dc838 2020-12-04 stsp */
542 e78dc838 2020-12-04 stsp int focus_child;
543 e78dc838 2020-12-04 stsp
544 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
545 5dc9f4bc 2018-08-04 stsp /* type-specific state */
546 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
547 5dc9f4bc 2018-08-04 stsp union {
548 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
549 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
550 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
551 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
552 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
553 5dc9f4bc 2018-08-04 stsp } state;
554 e5a0f69f 2018-08-18 stsp
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
556 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
557 e78dc838 2020-12-04 stsp struct tog_view *, int);
558 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
559 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
560 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
561 60493ae3 2019-06-20 stsp
562 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
563 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
564 c0c4acc8 2021-01-24 stsp int search_started;
565 60493ae3 2019-06-20 stsp int searching;
566 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
567 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
568 60493ae3 2019-06-20 stsp int search_next_done;
569 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
570 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
571 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
572 1803e47f 2019-06-22 stsp regex_t regex;
573 41605754 2020-11-12 stsp regmatch_t regmatch;
574 cc3c9aac 2018-08-01 stsp };
575 cd0acaa7 2018-05-20 stsp
576 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
577 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
578 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
579 78756c87 2020-11-24 stsp struct got_repository *);
580 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
581 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
582 e78dc838 2020-12-04 stsp struct tog_view *, int);
583 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
584 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
585 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
586 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp
588 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
589 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
590 78756c87 2020-11-24 stsp const char *, const char *, int);
591 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
593 e78dc838 2020-12-04 stsp struct tog_view *, int);
594 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
596 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
597 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
598 e5a0f69f 2018-08-18 stsp
599 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
600 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
601 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
602 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
603 e78dc838 2020-12-04 stsp struct tog_view *, int);
604 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
605 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
606 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
607 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
608 e5a0f69f 2018-08-18 stsp
609 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
610 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
611 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
612 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
613 e78dc838 2020-12-04 stsp struct tog_view *, int);
614 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
615 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
616 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
617 6458efa5 2020-11-24 stsp
618 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
619 6458efa5 2020-11-24 stsp struct got_repository *);
620 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
621 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
622 e78dc838 2020-12-04 stsp struct tog_view *, int);
623 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
624 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
625 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
626 25791caa 2018-10-24 stsp
627 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
628 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
629 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
630 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
631 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
632 25791caa 2018-10-24 stsp
633 25791caa 2018-10-24 stsp static void
634 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
635 25791caa 2018-10-24 stsp {
636 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
637 83baff54 2019-08-12 stsp }
638 83baff54 2019-08-12 stsp
639 83baff54 2019-08-12 stsp static void
640 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
641 83baff54 2019-08-12 stsp {
642 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
643 25791caa 2018-10-24 stsp }
644 26ed57b2 2018-05-19 stsp
645 61266923 2020-01-14 stsp static void
646 61266923 2020-01-14 stsp tog_sigcont(int signo)
647 61266923 2020-01-14 stsp {
648 61266923 2020-01-14 stsp tog_sigcont_received = 1;
649 61266923 2020-01-14 stsp }
650 61266923 2020-01-14 stsp
651 296152d1 2022-05-31 thomas static void
652 296152d1 2022-05-31 thomas tog_sigint(int signo)
653 296152d1 2022-05-31 thomas {
654 296152d1 2022-05-31 thomas tog_sigint_received = 1;
655 296152d1 2022-05-31 thomas }
656 296152d1 2022-05-31 thomas
657 296152d1 2022-05-31 thomas static void
658 296152d1 2022-05-31 thomas tog_sigterm(int signo)
659 296152d1 2022-05-31 thomas {
660 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
661 296152d1 2022-05-31 thomas }
662 296152d1 2022-05-31 thomas
663 296152d1 2022-05-31 thomas static int
664 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
665 296152d1 2022-05-31 thomas {
666 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
667 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
668 296152d1 2022-05-31 thomas }
669 296152d1 2022-05-31 thomas
670 e5a0f69f 2018-08-18 stsp static const struct got_error *
671 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
672 ea5e7bb5 2018-08-01 stsp {
673 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
674 e5a0f69f 2018-08-18 stsp
675 669b5ffa 2018-10-07 stsp if (view->child) {
676 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
677 669b5ffa 2018-10-07 stsp view->child = NULL;
678 669b5ffa 2018-10-07 stsp }
679 e5a0f69f 2018-08-18 stsp if (view->close)
680 e5a0f69f 2018-08-18 stsp err = view->close(view);
681 ea5e7bb5 2018-08-01 stsp if (view->panel)
682 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
683 ea5e7bb5 2018-08-01 stsp if (view->window)
684 ea5e7bb5 2018-08-01 stsp delwin(view->window);
685 ea5e7bb5 2018-08-01 stsp free(view);
686 f2d749db 2022-07-12 thomas return err ? err : child_err;
687 ea5e7bb5 2018-08-01 stsp }
688 ea5e7bb5 2018-08-01 stsp
689 ea5e7bb5 2018-08-01 stsp static struct tog_view *
690 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
691 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
692 ea5e7bb5 2018-08-01 stsp {
693 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
694 ea5e7bb5 2018-08-01 stsp
695 ea5e7bb5 2018-08-01 stsp if (view == NULL)
696 ea5e7bb5 2018-08-01 stsp return NULL;
697 ea5e7bb5 2018-08-01 stsp
698 d6b05b5b 2018-08-04 stsp view->type = type;
699 f7d12f7e 2018-08-01 stsp view->lines = LINES;
700 f7d12f7e 2018-08-01 stsp view->cols = COLS;
701 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
702 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
703 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
704 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
705 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
706 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
709 ea5e7bb5 2018-08-01 stsp }
710 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
711 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
712 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
713 96a765a8 2018-08-04 stsp view_close(view);
714 ea5e7bb5 2018-08-01 stsp return NULL;
715 ea5e7bb5 2018-08-01 stsp }
716 ea5e7bb5 2018-08-01 stsp
717 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
718 ea5e7bb5 2018-08-01 stsp return view;
719 cdf1ee82 2018-08-01 stsp }
720 cdf1ee82 2018-08-01 stsp
721 0cf4efb1 2018-09-29 stsp static int
722 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
723 0cf4efb1 2018-09-29 stsp {
724 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
725 0cf4efb1 2018-09-29 stsp return 0;
726 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
727 5c60c32a 2018-10-18 stsp }
728 5c60c32a 2018-10-18 stsp
729 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
730 a5d43cac 2022-07-01 thomas static int
731 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
732 a5d43cac 2022-07-01 thomas {
733 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
734 a5d43cac 2022-07-01 thomas }
735 a5d43cac 2022-07-01 thomas
736 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
737 5c60c32a 2018-10-18 stsp
738 5c60c32a 2018-10-18 stsp static const struct got_error *
739 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
740 5c60c32a 2018-10-18 stsp {
741 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
742 5c60c32a 2018-10-18 stsp
743 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
744 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
745 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
746 53d2bdd3 2022-07-10 thomas else
747 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
748 a5d43cac 2022-07-01 thomas view->begin_x = 0;
749 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
750 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
751 53d2bdd3 2022-07-10 thomas view->cols > 119)
752 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
753 53d2bdd3 2022-07-10 thomas else
754 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
755 a5d43cac 2022-07-01 thomas view->begin_y = 0;
756 a5d43cac 2022-07-01 thomas }
757 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
758 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
759 5c60c32a 2018-10-18 stsp view->lines = LINES;
760 5c60c32a 2018-10-18 stsp view->cols = COLS;
761 5c60c32a 2018-10-18 stsp err = view_resize(view);
762 5c60c32a 2018-10-18 stsp if (err)
763 5c60c32a 2018-10-18 stsp return err;
764 5c60c32a 2018-10-18 stsp
765 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
766 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
767 a5d43cac 2022-07-01 thomas
768 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
769 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
770 5c60c32a 2018-10-18 stsp
771 5c60c32a 2018-10-18 stsp return NULL;
772 5c60c32a 2018-10-18 stsp }
773 5c60c32a 2018-10-18 stsp
774 5c60c32a 2018-10-18 stsp static const struct got_error *
775 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
776 5c60c32a 2018-10-18 stsp {
777 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
778 5c60c32a 2018-10-18 stsp
779 5c60c32a 2018-10-18 stsp view->begin_x = 0;
780 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
781 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
782 5c60c32a 2018-10-18 stsp view->ncols = COLS;
783 5c60c32a 2018-10-18 stsp view->lines = LINES;
784 5c60c32a 2018-10-18 stsp view->cols = COLS;
785 5c60c32a 2018-10-18 stsp err = view_resize(view);
786 5c60c32a 2018-10-18 stsp if (err)
787 5c60c32a 2018-10-18 stsp return err;
788 5c60c32a 2018-10-18 stsp
789 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
790 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
791 5c60c32a 2018-10-18 stsp
792 5c60c32a 2018-10-18 stsp return NULL;
793 0cf4efb1 2018-09-29 stsp }
794 0cf4efb1 2018-09-29 stsp
795 5c60c32a 2018-10-18 stsp static int
796 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
797 5c60c32a 2018-10-18 stsp {
798 5c60c32a 2018-10-18 stsp return view->parent == NULL;
799 5c60c32a 2018-10-18 stsp }
800 5c60c32a 2018-10-18 stsp
801 b65b3ea0 2022-06-23 thomas static int
802 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
803 b65b3ea0 2022-06-23 thomas {
804 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
805 e44940c3 2022-07-01 thomas }
806 e44940c3 2022-07-01 thomas
807 e44940c3 2022-07-01 thomas static int
808 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
809 e44940c3 2022-07-01 thomas {
810 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
811 b65b3ea0 2022-06-23 thomas }
812 b65b3ea0 2022-06-23 thomas
813 444d5325 2022-07-03 thomas static int
814 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
815 444d5325 2022-07-03 thomas {
816 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
817 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
818 444d5325 2022-07-03 thomas }
819 444d5325 2022-07-03 thomas
820 a5d43cac 2022-07-01 thomas static void
821 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
822 a5d43cac 2022-07-01 thomas {
823 a5d43cac 2022-07-01 thomas PANEL *panel;
824 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
825 b65b3ea0 2022-06-23 thomas
826 a5d43cac 2022-07-01 thomas if (view->parent)
827 a5d43cac 2022-07-01 thomas return view_border(view->parent);
828 a5d43cac 2022-07-01 thomas
829 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
830 a5d43cac 2022-07-01 thomas if (panel == NULL)
831 a5d43cac 2022-07-01 thomas return;
832 a5d43cac 2022-07-01 thomas
833 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
834 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
835 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
836 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
837 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
838 a5d43cac 2022-07-01 thomas else
839 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
840 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
841 a5d43cac 2022-07-01 thomas }
842 a5d43cac 2022-07-01 thomas
843 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
844 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
845 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
846 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
847 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
848 a5d43cac 2022-07-01 thomas
849 4d8c2215 2018-08-19 stsp static const struct got_error *
850 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
851 f7d12f7e 2018-08-01 stsp {
852 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
853 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
854 f7d12f7e 2018-08-01 stsp
855 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
856 a5d43cac 2022-07-01 thomas
857 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
858 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
859 0cf4efb1 2018-09-29 stsp else
860 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
861 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
862 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
863 0cf4efb1 2018-09-29 stsp else
864 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
865 6d0fee91 2018-08-01 stsp
866 4918811f 2022-07-01 thomas if (view->child) {
867 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
868 a5d43cac 2022-07-01 thomas
869 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
870 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
871 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
872 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
873 40236d76 2022-06-23 thomas ncols = COLS;
874 40236d76 2022-06-23 thomas
875 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
876 5c60c32a 2018-10-18 stsp if (view->child->focussed)
877 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
878 5c60c32a 2018-10-18 stsp else
879 5c60c32a 2018-10-18 stsp show_panel(view->panel);
880 5c60c32a 2018-10-18 stsp } else {
881 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
882 40236d76 2022-06-23 thomas
883 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
884 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
885 a5d43cac 2022-07-01 thomas }
886 a5d43cac 2022-07-01 thomas /*
887 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
888 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
889 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
890 a5d43cac 2022-07-01 thomas */
891 a5d43cac 2022-07-01 thomas if (hs) {
892 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
893 a5d43cac 2022-07-01 thomas if (err)
894 a5d43cac 2022-07-01 thomas return err;
895 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
896 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
897 a5d43cac 2022-07-01 thomas if (err)
898 a5d43cac 2022-07-01 thomas return err;
899 a5d43cac 2022-07-01 thomas }
900 a5d43cac 2022-07-01 thomas view_border(view);
901 a5d43cac 2022-07-01 thomas update_panels();
902 a5d43cac 2022-07-01 thomas doupdate();
903 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
904 a5d43cac 2022-07-01 thomas nlines = view->nlines;
905 a5d43cac 2022-07-01 thomas }
906 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
907 40236d76 2022-06-23 thomas ncols = COLS;
908 669b5ffa 2018-10-07 stsp
909 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
910 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
911 ea0bff04 2022-07-19 thomas if (err)
912 ea0bff04 2022-07-19 thomas return err;
913 ea0bff04 2022-07-19 thomas }
914 ea0bff04 2022-07-19 thomas
915 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
916 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
917 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
918 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
919 40236d76 2022-06-23 thomas wclear(view->window);
920 40236d76 2022-06-23 thomas
921 40236d76 2022-06-23 thomas view->nlines = nlines;
922 40236d76 2022-06-23 thomas view->ncols = ncols;
923 40236d76 2022-06-23 thomas view->lines = LINES;
924 40236d76 2022-06-23 thomas view->cols = COLS;
925 40236d76 2022-06-23 thomas
926 5c60c32a 2018-10-18 stsp return NULL;
927 ea0bff04 2022-07-19 thomas }
928 ea0bff04 2022-07-19 thomas
929 ea0bff04 2022-07-19 thomas static const struct got_error *
930 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
931 ea0bff04 2022-07-19 thomas {
932 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
933 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
934 3a0139e8 2022-07-22 thomas int n = 0;
935 ea0bff04 2022-07-19 thomas
936 3a0139e8 2022-07-22 thomas if (s->selected_entry)
937 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
938 3a0139e8 2022-07-22 thomas
939 ea0bff04 2022-07-19 thomas /*
940 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
941 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
942 ea0bff04 2022-07-19 thomas */
943 ea0bff04 2022-07-19 thomas if (s->commits.ncommits < n) {
944 ea0bff04 2022-07-19 thomas view->nscrolled = n - s->commits.ncommits + increase + 1;
945 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
946 ea0bff04 2022-07-19 thomas }
947 ea0bff04 2022-07-19 thomas
948 ea0bff04 2022-07-19 thomas return err;
949 97cb21cd 2022-07-12 thomas }
950 97cb21cd 2022-07-12 thomas
951 97cb21cd 2022-07-12 thomas static void
952 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
953 97cb21cd 2022-07-12 thomas {
954 97cb21cd 2022-07-12 thomas if (n == 0)
955 97cb21cd 2022-07-12 thomas return;
956 97cb21cd 2022-07-12 thomas
957 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
958 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
959 97cb21cd 2022-07-12 thomas view->parent->offset += n;
960 97cb21cd 2022-07-12 thomas else
961 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
962 97cb21cd 2022-07-12 thomas } else if (view->offset) {
963 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
964 97cb21cd 2022-07-12 thomas view->offset -= n;
965 97cb21cd 2022-07-12 thomas else
966 97cb21cd 2022-07-12 thomas view->offset = 0;
967 97cb21cd 2022-07-12 thomas }
968 53d2bdd3 2022-07-10 thomas }
969 53d2bdd3 2022-07-10 thomas
970 53d2bdd3 2022-07-10 thomas static const struct got_error *
971 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
972 53d2bdd3 2022-07-10 thomas {
973 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
974 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
975 53d2bdd3 2022-07-10 thomas
976 53d2bdd3 2022-07-10 thomas if (view->parent)
977 53d2bdd3 2022-07-10 thomas v = view->parent;
978 53d2bdd3 2022-07-10 thomas else
979 53d2bdd3 2022-07-10 thomas v = view;
980 53d2bdd3 2022-07-10 thomas
981 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
982 53d2bdd3 2022-07-10 thomas return NULL;
983 53d2bdd3 2022-07-10 thomas
984 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
985 53d2bdd3 2022-07-10 thomas
986 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
987 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
988 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
989 53d2bdd3 2022-07-10 thomas if (view->parent)
990 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
991 53d2bdd3 2022-07-10 thomas else
992 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
993 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
994 53d2bdd3 2022-07-10 thomas view->count = 0;
995 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
996 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
997 53d2bdd3 2022-07-10 thomas view->count = 0;
998 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
999 53d2bdd3 2022-07-10 thomas }
1000 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1001 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1002 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1003 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1004 53d2bdd3 2022-07-10 thomas if (err)
1005 53d2bdd3 2022-07-10 thomas return err;
1006 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1007 53d2bdd3 2022-07-10 thomas } else {
1008 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1009 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1010 53d2bdd3 2022-07-10 thomas if (view->parent)
1011 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1012 53d2bdd3 2022-07-10 thomas else
1013 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1014 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1015 53d2bdd3 2022-07-10 thomas view->count = 0;
1016 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1017 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1018 53d2bdd3 2022-07-10 thomas view->count = 0;
1019 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1020 53d2bdd3 2022-07-10 thomas }
1021 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1022 53d2bdd3 2022-07-10 thomas }
1023 53d2bdd3 2022-07-10 thomas
1024 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1025 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1026 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1027 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1028 53d2bdd3 2022-07-10 thomas
1029 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1030 53d2bdd3 2022-07-10 thomas if (err)
1031 53d2bdd3 2022-07-10 thomas return err;
1032 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1033 53d2bdd3 2022-07-10 thomas if (err)
1034 53d2bdd3 2022-07-10 thomas return err;
1035 53d2bdd3 2022-07-10 thomas
1036 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1037 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1038 53d2bdd3 2022-07-10 thomas if (err)
1039 53d2bdd3 2022-07-10 thomas return err;
1040 53d2bdd3 2022-07-10 thomas }
1041 53d2bdd3 2022-07-10 thomas
1042 3a0139e8 2022-07-22 thomas if (v->resize)
1043 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1044 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1045 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1046 53d2bdd3 2022-07-10 thomas
1047 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1048 53d2bdd3 2022-07-10 thomas
1049 53d2bdd3 2022-07-10 thomas return err;
1050 53d2bdd3 2022-07-10 thomas }
1051 53d2bdd3 2022-07-10 thomas
1052 53d2bdd3 2022-07-10 thomas static void
1053 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1054 53d2bdd3 2022-07-10 thomas {
1055 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1056 53d2bdd3 2022-07-10 thomas
1057 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1058 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1059 669b5ffa 2018-10-07 stsp }
1060 669b5ffa 2018-10-07 stsp
1061 669b5ffa 2018-10-07 stsp static const struct got_error *
1062 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1063 669b5ffa 2018-10-07 stsp {
1064 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1065 669b5ffa 2018-10-07 stsp
1066 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1067 669b5ffa 2018-10-07 stsp return NULL;
1068 669b5ffa 2018-10-07 stsp
1069 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1070 669b5ffa 2018-10-07 stsp view->child = NULL;
1071 669b5ffa 2018-10-07 stsp return err;
1072 669b5ffa 2018-10-07 stsp }
1073 669b5ffa 2018-10-07 stsp
1074 40236d76 2022-06-23 thomas static const struct got_error *
1075 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1076 669b5ffa 2018-10-07 stsp {
1077 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1078 53d2bdd3 2022-07-10 thomas
1079 669b5ffa 2018-10-07 stsp view->child = child;
1080 669b5ffa 2018-10-07 stsp child->parent = view;
1081 40236d76 2022-06-23 thomas
1082 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1083 53d2bdd3 2022-07-10 thomas if (err)
1084 53d2bdd3 2022-07-10 thomas return err;
1085 53d2bdd3 2022-07-10 thomas
1086 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1087 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1088 53d2bdd3 2022-07-10 thomas
1089 53d2bdd3 2022-07-10 thomas return err;
1090 bfddd0d9 2018-09-29 stsp }
1091 2a31b33b 2022-07-23 thomas
1092 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1093 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1094 2a31b33b 2022-07-23 thomas
1095 2a31b33b 2022-07-23 thomas static const struct got_error *
1096 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1097 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1098 2a31b33b 2022-07-23 thomas {
1099 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1100 2a31b33b 2022-07-23 thomas const struct got_error *err;
1101 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1102 2a31b33b 2022-07-23 thomas
1103 2a31b33b 2022-07-23 thomas *requested = NULL;
1104 2a31b33b 2022-07-23 thomas
1105 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view))
1106 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1107 bfddd0d9 2018-09-29 stsp
1108 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1109 2a31b33b 2022-07-23 thomas if (err)
1110 2a31b33b 2022-07-23 thomas return err;
1111 2a31b33b 2022-07-23 thomas
1112 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1113 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1114 2a31b33b 2022-07-23 thomas if (err)
1115 2a31b33b 2022-07-23 thomas return err;
1116 2a31b33b 2022-07-23 thomas }
1117 2a31b33b 2022-07-23 thomas
1118 2a31b33b 2022-07-23 thomas view->focussed = 0;
1119 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1120 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1121 2a31b33b 2022-07-23 thomas new_view->nlines = view->lines - y;
1122 2a31b33b 2022-07-23 thomas
1123 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view)) {
1124 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1125 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1126 2a31b33b 2022-07-23 thomas if (err)
1127 2a31b33b 2022-07-23 thomas return err;
1128 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1129 2a31b33b 2022-07-23 thomas if (err)
1130 2a31b33b 2022-07-23 thomas return err;
1131 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1132 2a31b33b 2022-07-23 thomas } else
1133 2a31b33b 2022-07-23 thomas *requested = new_view;
1134 2a31b33b 2022-07-23 thomas
1135 2a31b33b 2022-07-23 thomas return NULL;
1136 2a31b33b 2022-07-23 thomas }
1137 2a31b33b 2022-07-23 thomas
1138 34bc9ec9 2019-02-22 stsp static void
1139 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1140 25791caa 2018-10-24 stsp {
1141 25791caa 2018-10-24 stsp int cols, lines;
1142 25791caa 2018-10-24 stsp struct winsize size;
1143 25791caa 2018-10-24 stsp
1144 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1145 25791caa 2018-10-24 stsp cols = 80; /* Default */
1146 25791caa 2018-10-24 stsp lines = 24;
1147 25791caa 2018-10-24 stsp } else {
1148 25791caa 2018-10-24 stsp cols = size.ws_col;
1149 25791caa 2018-10-24 stsp lines = size.ws_row;
1150 25791caa 2018-10-24 stsp }
1151 25791caa 2018-10-24 stsp resize_term(lines, cols);
1152 2b49a8ae 2019-06-22 stsp }
1153 2b49a8ae 2019-06-22 stsp
1154 2b49a8ae 2019-06-22 stsp static const struct got_error *
1155 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1156 2b49a8ae 2019-06-22 stsp {
1157 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1158 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1159 2b49a8ae 2019-06-22 stsp char pattern[1024];
1160 2b49a8ae 2019-06-22 stsp int ret;
1161 c0c4acc8 2021-01-24 stsp
1162 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1163 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1164 c0c4acc8 2021-01-24 stsp view->searching = 0;
1165 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1166 c0c4acc8 2021-01-24 stsp }
1167 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1168 2b49a8ae 2019-06-22 stsp
1169 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1170 2b49a8ae 2019-06-22 stsp return NULL;
1171 2b49a8ae 2019-06-22 stsp
1172 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1173 a5d43cac 2022-07-01 thomas v = view->child;
1174 2b49a8ae 2019-06-22 stsp
1175 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1176 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1177 a5d43cac 2022-07-01 thomas
1178 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1179 2b49a8ae 2019-06-22 stsp nocbreak();
1180 2b49a8ae 2019-06-22 stsp echo();
1181 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1182 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1183 2b49a8ae 2019-06-22 stsp cbreak();
1184 2b49a8ae 2019-06-22 stsp noecho();
1185 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1186 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1187 2b49a8ae 2019-06-22 stsp return NULL;
1188 2b49a8ae 2019-06-22 stsp
1189 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1190 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1191 7c32bd05 2019-06-22 stsp if (err) {
1192 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1193 7c32bd05 2019-06-22 stsp return err;
1194 7c32bd05 2019-06-22 stsp }
1195 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1196 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1197 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1198 2b49a8ae 2019-06-22 stsp view->search_next(view);
1199 2b49a8ae 2019-06-22 stsp }
1200 2b49a8ae 2019-06-22 stsp
1201 2b49a8ae 2019-06-22 stsp return NULL;
1202 64486692 2022-07-07 thomas }
1203 64486692 2022-07-07 thomas
1204 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1205 64486692 2022-07-07 thomas static const struct got_error *
1206 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1207 64486692 2022-07-07 thomas {
1208 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1209 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1210 64486692 2022-07-07 thomas
1211 64486692 2022-07-07 thomas if (view->parent)
1212 64486692 2022-07-07 thomas v = view->parent;
1213 64486692 2022-07-07 thomas else
1214 64486692 2022-07-07 thomas v = view;
1215 64486692 2022-07-07 thomas
1216 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1217 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1218 ddbc4d37 2022-07-12 thomas else
1219 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1220 64486692 2022-07-07 thomas
1221 ddbc4d37 2022-07-12 thomas if (!v->child)
1222 ddbc4d37 2022-07-12 thomas return NULL;
1223 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1224 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1225 ddbc4d37 2022-07-12 thomas
1226 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1227 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1228 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1229 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1230 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1231 64486692 2022-07-07 thomas
1232 ddbc4d37 2022-07-12 thomas
1233 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1234 64486692 2022-07-07 thomas v->ncols = COLS;
1235 64486692 2022-07-07 thomas v->child->ncols = COLS;
1236 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1237 64486692 2022-07-07 thomas
1238 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1239 64486692 2022-07-07 thomas if (err)
1240 64486692 2022-07-07 thomas return err;
1241 64486692 2022-07-07 thomas }
1242 64486692 2022-07-07 thomas v->child->mode = v->mode;
1243 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1244 64486692 2022-07-07 thomas v->focus_child = 1;
1245 64486692 2022-07-07 thomas
1246 64486692 2022-07-07 thomas err = view_fullscreen(v);
1247 64486692 2022-07-07 thomas if (err)
1248 64486692 2022-07-07 thomas return err;
1249 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1250 64486692 2022-07-07 thomas if (err)
1251 64486692 2022-07-07 thomas return err;
1252 64486692 2022-07-07 thomas
1253 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1254 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1255 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1256 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1257 cf1fe301 2022-07-29 thomas if (err)
1258 cf1fe301 2022-07-29 thomas return err;
1259 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1260 cf1fe301 2022-07-29 thomas if (err)
1261 cf1fe301 2022-07-29 thomas return err;
1262 ddbc4d37 2022-07-12 thomas } else {
1263 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1264 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1265 64486692 2022-07-07 thomas }
1266 f4e6231a 2022-07-22 thomas if (v->resize)
1267 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1268 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1269 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1270 64486692 2022-07-07 thomas
1271 64486692 2022-07-07 thomas return err;
1272 0cf4efb1 2018-09-29 stsp }
1273 6d0fee91 2018-08-01 stsp
1274 07b0611c 2022-06-23 thomas /*
1275 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1276 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1277 07b0611c 2022-06-23 thomas */
1278 07b0611c 2022-06-23 thomas static int
1279 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1280 07b0611c 2022-06-23 thomas {
1281 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1282 a5d43cac 2022-07-01 thomas int x, n = 0;
1283 07b0611c 2022-06-23 thomas
1284 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1285 a5d43cac 2022-07-01 thomas v = view->child;
1286 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1287 a5d43cac 2022-07-01 thomas v = view->parent;
1288 a5d43cac 2022-07-01 thomas
1289 07b0611c 2022-06-23 thomas view->count = 0;
1290 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1291 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1292 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1293 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1294 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1295 07b0611c 2022-06-23 thomas
1296 07b0611c 2022-06-23 thomas do {
1297 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1298 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1299 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1300 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1301 a5d43cac 2022-07-01 thomas }
1302 a5d43cac 2022-07-01 thomas
1303 07b0611c 2022-06-23 thomas /*
1304 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1305 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1306 07b0611c 2022-06-23 thomas */
1307 07b0611c 2022-06-23 thomas if (n >= 9999999)
1308 07b0611c 2022-06-23 thomas n = 9999999;
1309 07b0611c 2022-06-23 thomas else
1310 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1311 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1312 07b0611c 2022-06-23 thomas
1313 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1314 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1315 07dd3ed3 2022-08-06 thomas n = 0;
1316 07dd3ed3 2022-08-06 thomas c = 0;
1317 07dd3ed3 2022-08-06 thomas }
1318 07dd3ed3 2022-08-06 thomas
1319 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1320 07b0611c 2022-06-23 thomas view->count = n;
1321 07b0611c 2022-06-23 thomas
1322 07b0611c 2022-06-23 thomas return c;
1323 07b0611c 2022-06-23 thomas }
1324 07b0611c 2022-06-23 thomas
1325 0cf4efb1 2018-09-29 stsp static const struct got_error *
1326 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1327 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1328 e5a0f69f 2018-08-18 stsp {
1329 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1330 669b5ffa 2018-10-07 stsp struct tog_view *v;
1331 1a76625f 2018-10-22 stsp int ch, errcode;
1332 e5a0f69f 2018-08-18 stsp
1333 e5a0f69f 2018-08-18 stsp *new = NULL;
1334 8f4ed634 2020-03-26 stsp
1335 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1336 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1337 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1338 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1339 07b0611c 2022-06-23 thomas view->count = 0;
1340 07b0611c 2022-06-23 thomas }
1341 e5a0f69f 2018-08-18 stsp
1342 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1343 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1344 82954512 2020-02-03 stsp if (errcode)
1345 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1346 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1347 3da8ef85 2021-09-21 stsp sched_yield();
1348 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1349 82954512 2020-02-03 stsp if (errcode)
1350 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1351 82954512 2020-02-03 stsp "pthread_mutex_lock");
1352 60493ae3 2019-06-20 stsp view->search_next(view);
1353 60493ae3 2019-06-20 stsp return NULL;
1354 60493ae3 2019-06-20 stsp }
1355 60493ae3 2019-06-20 stsp
1356 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1357 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1358 1a76625f 2018-10-22 stsp if (errcode)
1359 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1360 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1361 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1362 634cb454 2022-07-03 thomas cbreak();
1363 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1364 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1365 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1366 634cb454 2022-07-03 thomas view->count = 0;
1367 634cb454 2022-07-03 thomas else
1368 634cb454 2022-07-03 thomas ch = view->ch;
1369 634cb454 2022-07-03 thomas } else {
1370 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1371 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1372 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1373 07b0611c 2022-06-23 thomas }
1374 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1375 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1376 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1377 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1378 1a76625f 2018-10-22 stsp if (errcode)
1379 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1380 25791caa 2018-10-24 stsp
1381 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1382 25791caa 2018-10-24 stsp tog_resizeterm();
1383 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1384 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1385 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1386 25791caa 2018-10-24 stsp err = view_resize(v);
1387 25791caa 2018-10-24 stsp if (err)
1388 25791caa 2018-10-24 stsp return err;
1389 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1390 25791caa 2018-10-24 stsp if (err)
1391 25791caa 2018-10-24 stsp return err;
1392 cdfcfb03 2020-12-06 stsp if (v->child) {
1393 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1394 cdfcfb03 2020-12-06 stsp if (err)
1395 cdfcfb03 2020-12-06 stsp return err;
1396 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1397 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1398 cdfcfb03 2020-12-06 stsp if (err)
1399 cdfcfb03 2020-12-06 stsp return err;
1400 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1401 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1402 53d2bdd3 2022-07-10 thomas if (err)
1403 53d2bdd3 2022-07-10 thomas return err;
1404 53d2bdd3 2022-07-10 thomas }
1405 cdfcfb03 2020-12-06 stsp }
1406 25791caa 2018-10-24 stsp }
1407 25791caa 2018-10-24 stsp }
1408 25791caa 2018-10-24 stsp
1409 e5a0f69f 2018-08-18 stsp switch (ch) {
1410 1e37a5c2 2019-05-12 jcs case '\t':
1411 07b0611c 2022-06-23 thomas view->count = 0;
1412 1e37a5c2 2019-05-12 jcs if (view->child) {
1413 e78dc838 2020-12-04 stsp view->focussed = 0;
1414 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1415 e78dc838 2020-12-04 stsp view->focus_child = 1;
1416 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1417 e78dc838 2020-12-04 stsp view->focussed = 0;
1418 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1419 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1420 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1421 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1422 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1423 3a0139e8 2022-07-22 thomas 0);
1424 a5d43cac 2022-07-01 thomas if (err)
1425 a5d43cac 2022-07-01 thomas return err;
1426 a5d43cac 2022-07-01 thomas }
1427 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1428 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1429 a5d43cac 2022-07-01 thomas if (err)
1430 a5d43cac 2022-07-01 thomas return err;
1431 a5d43cac 2022-07-01 thomas }
1432 1e37a5c2 2019-05-12 jcs }
1433 1e37a5c2 2019-05-12 jcs break;
1434 1e37a5c2 2019-05-12 jcs case 'q':
1435 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1436 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1437 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1438 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1439 a5d43cac 2022-07-01 thomas if (err)
1440 a5d43cac 2022-07-01 thomas break;
1441 a5d43cac 2022-07-01 thomas }
1442 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1443 a5d43cac 2022-07-01 thomas }
1444 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1445 9970f7fc 2020-12-03 stsp view->dying = 1;
1446 1e37a5c2 2019-05-12 jcs break;
1447 1e37a5c2 2019-05-12 jcs case 'Q':
1448 1e37a5c2 2019-05-12 jcs *done = 1;
1449 1e37a5c2 2019-05-12 jcs break;
1450 1c5e5faa 2022-06-23 thomas case 'F':
1451 07b0611c 2022-06-23 thomas view->count = 0;
1452 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1453 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1454 1e37a5c2 2019-05-12 jcs break;
1455 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1456 e78dc838 2020-12-04 stsp view->focussed = 0;
1457 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1458 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1459 53d2bdd3 2022-07-10 thomas } else {
1460 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1461 53d2bdd3 2022-07-10 thomas if (!err)
1462 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1463 53d2bdd3 2022-07-10 thomas }
1464 1e37a5c2 2019-05-12 jcs if (err)
1465 1e37a5c2 2019-05-12 jcs break;
1466 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1467 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1468 1e37a5c2 2019-05-12 jcs } else {
1469 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1470 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1471 e78dc838 2020-12-04 stsp view->focussed = 1;
1472 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1473 1e37a5c2 2019-05-12 jcs } else {
1474 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1475 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1476 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1477 53d2bdd3 2022-07-10 thomas if (!err)
1478 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1479 669b5ffa 2018-10-07 stsp }
1480 1e37a5c2 2019-05-12 jcs if (err)
1481 1e37a5c2 2019-05-12 jcs break;
1482 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1483 a5d43cac 2022-07-01 thomas }
1484 a5d43cac 2022-07-01 thomas if (err)
1485 a5d43cac 2022-07-01 thomas break;
1486 3a0139e8 2022-07-22 thomas if (view->resize) {
1487 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1488 a5d43cac 2022-07-01 thomas if (err)
1489 a5d43cac 2022-07-01 thomas break;
1490 1e37a5c2 2019-05-12 jcs }
1491 a5d43cac 2022-07-01 thomas if (view->parent)
1492 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1493 a5d43cac 2022-07-01 thomas if (!err)
1494 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1495 1e37a5c2 2019-05-12 jcs break;
1496 64486692 2022-07-07 thomas case 'S':
1497 53d2bdd3 2022-07-10 thomas view->count = 0;
1498 64486692 2022-07-07 thomas err = switch_split(view);
1499 53d2bdd3 2022-07-10 thomas break;
1500 53d2bdd3 2022-07-10 thomas case '-':
1501 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1502 64486692 2022-07-07 thomas break;
1503 53d2bdd3 2022-07-10 thomas case '+':
1504 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1505 53d2bdd3 2022-07-10 thomas break;
1506 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1507 60493ae3 2019-06-20 stsp break;
1508 60493ae3 2019-06-20 stsp case '/':
1509 07b0611c 2022-06-23 thomas view->count = 0;
1510 60493ae3 2019-06-20 stsp if (view->search_start)
1511 2b49a8ae 2019-06-22 stsp view_search_start(view);
1512 60493ae3 2019-06-20 stsp else
1513 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1514 1e37a5c2 2019-05-12 jcs break;
1515 b1bf1435 2019-06-21 stsp case 'N':
1516 60493ae3 2019-06-20 stsp case 'n':
1517 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1518 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1519 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1520 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1521 60493ae3 2019-06-20 stsp view->search_next(view);
1522 60493ae3 2019-06-20 stsp } else
1523 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1524 adf4c9e0 2022-07-03 thomas break;
1525 adf4c9e0 2022-07-03 thomas case 'A':
1526 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1527 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1528 adf4c9e0 2022-07-03 thomas else
1529 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1530 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1531 adf4c9e0 2022-07-03 thomas if (v->reset) {
1532 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1533 adf4c9e0 2022-07-03 thomas if (err)
1534 adf4c9e0 2022-07-03 thomas return err;
1535 adf4c9e0 2022-07-03 thomas }
1536 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1537 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1538 adf4c9e0 2022-07-03 thomas if (err)
1539 adf4c9e0 2022-07-03 thomas return err;
1540 adf4c9e0 2022-07-03 thomas }
1541 adf4c9e0 2022-07-03 thomas }
1542 60493ae3 2019-06-20 stsp break;
1543 1e37a5c2 2019-05-12 jcs default:
1544 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1545 1e37a5c2 2019-05-12 jcs break;
1546 e5a0f69f 2018-08-18 stsp }
1547 e5a0f69f 2018-08-18 stsp
1548 e5a0f69f 2018-08-18 stsp return err;
1549 bcbd79e2 2018-08-19 stsp }
1550 bcbd79e2 2018-08-19 stsp
1551 ef20f542 2022-06-26 thomas static int
1552 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1553 a3404814 2018-09-02 stsp {
1554 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1555 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1556 669b5ffa 2018-10-07 stsp return 0;
1557 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1558 669b5ffa 2018-10-07 stsp return 0;
1559 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1560 a3404814 2018-09-02 stsp return 0;
1561 a3404814 2018-09-02 stsp
1562 669b5ffa 2018-10-07 stsp return view->focussed;
1563 a3404814 2018-09-02 stsp }
1564 a3404814 2018-09-02 stsp
1565 bcbd79e2 2018-08-19 stsp static const struct got_error *
1566 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1567 e5a0f69f 2018-08-18 stsp {
1568 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1569 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1570 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1571 64486692 2022-07-07 thomas char *mode;
1572 fd823528 2018-10-22 stsp int fast_refresh = 10;
1573 1a76625f 2018-10-22 stsp int done = 0, errcode;
1574 e5a0f69f 2018-08-18 stsp
1575 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1576 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1577 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1578 64486692 2022-07-07 thomas else
1579 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1580 64486692 2022-07-07 thomas
1581 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1582 1a76625f 2018-10-22 stsp if (errcode)
1583 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1584 1a76625f 2018-10-22 stsp
1585 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1586 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1587 e5a0f69f 2018-08-18 stsp
1588 1004088d 2018-09-29 stsp view->focussed = 1;
1589 878940b7 2018-09-29 stsp err = view->show(view);
1590 0cf4efb1 2018-09-29 stsp if (err)
1591 0cf4efb1 2018-09-29 stsp return err;
1592 0cf4efb1 2018-09-29 stsp update_panels();
1593 0cf4efb1 2018-09-29 stsp doupdate();
1594 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1595 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1596 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1597 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1598 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1599 fd823528 2018-10-22 stsp
1600 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1601 e5a0f69f 2018-08-18 stsp if (err)
1602 e5a0f69f 2018-08-18 stsp break;
1603 9970f7fc 2020-12-03 stsp if (view->dying) {
1604 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1605 669b5ffa 2018-10-07 stsp
1606 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1607 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1608 9970f7fc 2020-12-03 stsp entry);
1609 e78dc838 2020-12-04 stsp else if (view->parent)
1610 669b5ffa 2018-10-07 stsp prev = view->parent;
1611 669b5ffa 2018-10-07 stsp
1612 e78dc838 2020-12-04 stsp if (view->parent) {
1613 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1614 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1615 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1616 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1617 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1618 40236d76 2022-06-23 thomas if (err)
1619 40236d76 2022-06-23 thomas break;
1620 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1621 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1622 e78dc838 2020-12-04 stsp } else
1623 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1624 669b5ffa 2018-10-07 stsp
1625 9970f7fc 2020-12-03 stsp err = view_close(view);
1626 fb59748f 2020-12-05 stsp if (err)
1627 e5a0f69f 2018-08-18 stsp goto done;
1628 669b5ffa 2018-10-07 stsp
1629 e78dc838 2020-12-04 stsp view = NULL;
1630 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1631 e78dc838 2020-12-04 stsp if (v->focussed)
1632 e78dc838 2020-12-04 stsp break;
1633 0cf4efb1 2018-09-29 stsp }
1634 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1635 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1636 e78dc838 2020-12-04 stsp if (prev)
1637 e78dc838 2020-12-04 stsp view = prev;
1638 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1639 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1640 e78dc838 2020-12-04 stsp tog_view_list_head);
1641 e78dc838 2020-12-04 stsp }
1642 e78dc838 2020-12-04 stsp if (view) {
1643 e78dc838 2020-12-04 stsp if (view->focus_child) {
1644 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1645 e78dc838 2020-12-04 stsp view = view->child;
1646 e78dc838 2020-12-04 stsp } else
1647 e78dc838 2020-12-04 stsp view->focussed = 1;
1648 e78dc838 2020-12-04 stsp }
1649 e78dc838 2020-12-04 stsp }
1650 e5a0f69f 2018-08-18 stsp }
1651 bcbd79e2 2018-08-19 stsp if (new_view) {
1652 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1653 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1654 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1655 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1656 86c66b02 2018-10-18 stsp continue;
1657 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1658 86c66b02 2018-10-18 stsp err = view_close(v);
1659 86c66b02 2018-10-18 stsp if (err)
1660 86c66b02 2018-10-18 stsp goto done;
1661 86c66b02 2018-10-18 stsp break;
1662 86c66b02 2018-10-18 stsp }
1663 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1664 fed7eaa8 2018-10-24 stsp view = new_view;
1665 7cd52833 2022-06-23 thomas }
1666 669b5ffa 2018-10-07 stsp if (view) {
1667 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1668 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1669 e78dc838 2020-12-04 stsp view = view->child;
1670 e78dc838 2020-12-04 stsp } else {
1671 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1672 e78dc838 2020-12-04 stsp view = view->parent;
1673 1a76625f 2018-10-22 stsp }
1674 e78dc838 2020-12-04 stsp show_panel(view->panel);
1675 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1676 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1677 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1678 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1679 669b5ffa 2018-10-07 stsp if (err)
1680 1a76625f 2018-10-22 stsp goto done;
1681 669b5ffa 2018-10-07 stsp }
1682 669b5ffa 2018-10-07 stsp err = view->show(view);
1683 0cf4efb1 2018-09-29 stsp if (err)
1684 1a76625f 2018-10-22 stsp goto done;
1685 669b5ffa 2018-10-07 stsp if (view->child) {
1686 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1687 669b5ffa 2018-10-07 stsp if (err)
1688 1a76625f 2018-10-22 stsp goto done;
1689 669b5ffa 2018-10-07 stsp }
1690 1a76625f 2018-10-22 stsp update_panels();
1691 1a76625f 2018-10-22 stsp doupdate();
1692 0cf4efb1 2018-09-29 stsp }
1693 e5a0f69f 2018-08-18 stsp }
1694 e5a0f69f 2018-08-18 stsp done:
1695 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1696 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1697 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1698 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1699 f2d749db 2022-07-12 thomas close_err = view_close(view);
1700 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1701 f2d749db 2022-07-12 thomas err = close_err;
1702 e5a0f69f 2018-08-18 stsp }
1703 1a76625f 2018-10-22 stsp
1704 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1705 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1706 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1707 1a76625f 2018-10-22 stsp
1708 e5a0f69f 2018-08-18 stsp return err;
1709 ea5e7bb5 2018-08-01 stsp }
1710 ea5e7bb5 2018-08-01 stsp
1711 4ed7e80c 2018-05-20 stsp __dead static void
1712 9f7d7167 2018-04-29 stsp usage_log(void)
1713 9f7d7167 2018-04-29 stsp {
1714 80ddbec8 2018-04-29 stsp endwin();
1715 c70c5802 2018-08-01 stsp fprintf(stderr,
1716 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1717 9f7d7167 2018-04-29 stsp getprogname());
1718 9f7d7167 2018-04-29 stsp exit(1);
1719 80ddbec8 2018-04-29 stsp }
1720 80ddbec8 2018-04-29 stsp
1721 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1722 80ddbec8 2018-04-29 stsp static const struct got_error *
1723 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1724 963b370f 2018-05-20 stsp {
1725 00dfcb92 2018-06-11 stsp char *vis = NULL;
1726 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1727 963b370f 2018-05-20 stsp
1728 963b370f 2018-05-20 stsp *ws = NULL;
1729 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1730 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1731 00dfcb92 2018-06-11 stsp int vislen;
1732 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1733 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1734 00dfcb92 2018-06-11 stsp
1735 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1736 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1737 00dfcb92 2018-06-11 stsp if (err)
1738 00dfcb92 2018-06-11 stsp return err;
1739 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1740 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1741 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1742 a7f50699 2018-06-11 stsp goto done;
1743 a7f50699 2018-06-11 stsp }
1744 00dfcb92 2018-06-11 stsp }
1745 963b370f 2018-05-20 stsp
1746 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1747 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1748 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1749 a7f50699 2018-06-11 stsp goto done;
1750 a7f50699 2018-06-11 stsp }
1751 963b370f 2018-05-20 stsp
1752 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1753 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1754 a7f50699 2018-06-11 stsp done:
1755 00dfcb92 2018-06-11 stsp free(vis);
1756 963b370f 2018-05-20 stsp if (err) {
1757 963b370f 2018-05-20 stsp free(*ws);
1758 963b370f 2018-05-20 stsp *ws = NULL;
1759 963b370f 2018-05-20 stsp *wlen = 0;
1760 963b370f 2018-05-20 stsp }
1761 963b370f 2018-05-20 stsp return err;
1762 05171be4 2022-06-23 thomas }
1763 05171be4 2022-06-23 thomas
1764 05171be4 2022-06-23 thomas static const struct got_error *
1765 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1766 05171be4 2022-06-23 thomas {
1767 05171be4 2022-06-23 thomas char *dst;
1768 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1769 05171be4 2022-06-23 thomas
1770 05171be4 2022-06-23 thomas *ptr = NULL;
1771 05171be4 2022-06-23 thomas n = len = strlen(src);
1772 83316834 2022-06-23 thomas dst = malloc(n + 1);
1773 05171be4 2022-06-23 thomas if (dst == NULL)
1774 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1775 05171be4 2022-06-23 thomas
1776 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1777 05171be4 2022-06-23 thomas const char c = src[idx];
1778 05171be4 2022-06-23 thomas
1779 05171be4 2022-06-23 thomas if (c == '\t') {
1780 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1781 b235ad5d 2022-06-23 thomas char *p;
1782 b235ad5d 2022-06-23 thomas
1783 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1784 83316834 2022-06-23 thomas if (p == NULL) {
1785 83316834 2022-06-23 thomas free(dst);
1786 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1787 83316834 2022-06-23 thomas
1788 83316834 2022-06-23 thomas }
1789 83316834 2022-06-23 thomas dst = p;
1790 05171be4 2022-06-23 thomas n += nb;
1791 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1792 05171be4 2022-06-23 thomas sz += nb;
1793 05171be4 2022-06-23 thomas } else
1794 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1795 05171be4 2022-06-23 thomas ++idx;
1796 05171be4 2022-06-23 thomas }
1797 05171be4 2022-06-23 thomas
1798 05171be4 2022-06-23 thomas dst[sz] = '\0';
1799 05171be4 2022-06-23 thomas *ptr = dst;
1800 05171be4 2022-06-23 thomas return NULL;
1801 963b370f 2018-05-20 stsp }
1802 963b370f 2018-05-20 stsp
1803 8d208d34 2022-06-23 thomas /*
1804 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1805 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1806 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1807 8d208d34 2022-06-23 thomas * *rcol.
1808 f91a2b48 2022-06-23 thomas */
1809 8d208d34 2022-06-23 thomas static int
1810 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1811 8d208d34 2022-06-23 thomas {
1812 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1813 f91a2b48 2022-06-23 thomas
1814 8d208d34 2022-06-23 thomas if (n == 0) {
1815 8d208d34 2022-06-23 thomas *rcol = cols;
1816 8d208d34 2022-06-23 thomas return off;
1817 8d208d34 2022-06-23 thomas }
1818 f91a2b48 2022-06-23 thomas
1819 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1820 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1821 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1822 8d208d34 2022-06-23 thomas else
1823 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1824 f91a2b48 2022-06-23 thomas
1825 8d208d34 2022-06-23 thomas if (width == -1) {
1826 8d208d34 2022-06-23 thomas width = 1;
1827 8d208d34 2022-06-23 thomas wline[i] = L'.';
1828 f91a2b48 2022-06-23 thomas }
1829 f91a2b48 2022-06-23 thomas
1830 8d208d34 2022-06-23 thomas if (cols + width > n)
1831 8d208d34 2022-06-23 thomas break;
1832 8d208d34 2022-06-23 thomas cols += width;
1833 f91a2b48 2022-06-23 thomas }
1834 f91a2b48 2022-06-23 thomas
1835 8d208d34 2022-06-23 thomas *rcol = cols;
1836 8d208d34 2022-06-23 thomas return i;
1837 f91a2b48 2022-06-23 thomas }
1838 f91a2b48 2022-06-23 thomas
1839 f91a2b48 2022-06-23 thomas /*
1840 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1841 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1842 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1843 f91a2b48 2022-06-23 thomas */
1844 f91a2b48 2022-06-23 thomas static const struct got_error *
1845 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1846 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1847 f91a2b48 2022-06-23 thomas {
1848 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1849 8d208d34 2022-06-23 thomas int cols;
1850 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1851 05171be4 2022-06-23 thomas char *exstr = NULL;
1852 963b370f 2018-05-20 stsp size_t wlen;
1853 8d208d34 2022-06-23 thomas int i, scrollx;
1854 963b370f 2018-05-20 stsp
1855 963b370f 2018-05-20 stsp *wlinep = NULL;
1856 b700b5d6 2018-07-10 stsp *widthp = 0;
1857 963b370f 2018-05-20 stsp
1858 05171be4 2022-06-23 thomas if (expand) {
1859 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1860 05171be4 2022-06-23 thomas if (err)
1861 05171be4 2022-06-23 thomas return err;
1862 05171be4 2022-06-23 thomas }
1863 05171be4 2022-06-23 thomas
1864 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1865 05171be4 2022-06-23 thomas free(exstr);
1866 963b370f 2018-05-20 stsp if (err)
1867 963b370f 2018-05-20 stsp return err;
1868 963b370f 2018-05-20 stsp
1869 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1870 f91a2b48 2022-06-23 thomas
1871 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1872 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1873 3f670bfb 2020-12-10 stsp wlen--;
1874 3f670bfb 2020-12-10 stsp }
1875 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1876 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1877 3f670bfb 2020-12-10 stsp wlen--;
1878 3f670bfb 2020-12-10 stsp }
1879 3f670bfb 2020-12-10 stsp
1880 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1881 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1882 27a741e5 2019-09-11 stsp
1883 b700b5d6 2018-07-10 stsp if (widthp)
1884 b700b5d6 2018-07-10 stsp *widthp = cols;
1885 f91a2b48 2022-06-23 thomas if (scrollxp)
1886 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1887 963b370f 2018-05-20 stsp if (err)
1888 963b370f 2018-05-20 stsp free(wline);
1889 963b370f 2018-05-20 stsp else
1890 963b370f 2018-05-20 stsp *wlinep = wline;
1891 963b370f 2018-05-20 stsp return err;
1892 963b370f 2018-05-20 stsp }
1893 963b370f 2018-05-20 stsp
1894 8b473291 2019-02-21 stsp static const struct got_error*
1895 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1896 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1897 8b473291 2019-02-21 stsp {
1898 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1899 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1900 8b473291 2019-02-21 stsp char *s;
1901 8b473291 2019-02-21 stsp const char *name;
1902 8b473291 2019-02-21 stsp
1903 8b473291 2019-02-21 stsp *refs_str = NULL;
1904 8b473291 2019-02-21 stsp
1905 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1906 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1907 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1908 52b5abe1 2019-08-13 stsp int cmp;
1909 52b5abe1 2019-08-13 stsp
1910 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1911 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1912 8b473291 2019-02-21 stsp continue;
1913 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1914 8b473291 2019-02-21 stsp name += 5;
1915 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1916 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1917 7143d404 2019-03-12 stsp continue;
1918 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1919 8b473291 2019-02-21 stsp name += 6;
1920 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1921 8b473291 2019-02-21 stsp name += 8;
1922 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1923 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1924 79cc719f 2020-04-24 stsp continue;
1925 79cc719f 2020-04-24 stsp }
1926 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1927 48cae60d 2020-09-22 stsp if (err)
1928 48cae60d 2020-09-22 stsp break;
1929 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1930 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1931 5d844a1e 2019-08-13 stsp if (err) {
1932 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1933 48cae60d 2020-09-22 stsp free(ref_id);
1934 5d844a1e 2019-08-13 stsp break;
1935 48cae60d 2020-09-22 stsp }
1936 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1937 5d844a1e 2019-08-13 stsp err = NULL;
1938 5d844a1e 2019-08-13 stsp tag = NULL;
1939 5d844a1e 2019-08-13 stsp }
1940 52b5abe1 2019-08-13 stsp }
1941 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1942 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1943 48cae60d 2020-09-22 stsp free(ref_id);
1944 52b5abe1 2019-08-13 stsp if (tag)
1945 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1946 52b5abe1 2019-08-13 stsp if (cmp != 0)
1947 52b5abe1 2019-08-13 stsp continue;
1948 8b473291 2019-02-21 stsp s = *refs_str;
1949 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1950 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1951 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1952 8b473291 2019-02-21 stsp free(s);
1953 8b473291 2019-02-21 stsp *refs_str = NULL;
1954 8b473291 2019-02-21 stsp break;
1955 8b473291 2019-02-21 stsp }
1956 8b473291 2019-02-21 stsp free(s);
1957 8b473291 2019-02-21 stsp }
1958 8b473291 2019-02-21 stsp
1959 8b473291 2019-02-21 stsp return err;
1960 8b473291 2019-02-21 stsp }
1961 8b473291 2019-02-21 stsp
1962 963b370f 2018-05-20 stsp static const struct got_error *
1963 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1964 27a741e5 2019-09-11 stsp int col_tab_align)
1965 5813d178 2019-03-09 stsp {
1966 e6b8b890 2020-12-29 naddy char *smallerthan;
1967 5813d178 2019-03-09 stsp
1968 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1969 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1970 5813d178 2019-03-09 stsp author = smallerthan + 1;
1971 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1972 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1973 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1974 5813d178 2019-03-09 stsp }
1975 5813d178 2019-03-09 stsp
1976 5813d178 2019-03-09 stsp static const struct got_error *
1977 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1978 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1979 8fdc79fe 2020-12-01 naddy int author_display_cols)
1980 80ddbec8 2018-04-29 stsp {
1981 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1982 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1983 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1984 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1985 5813d178 2019-03-09 stsp char *author = NULL;
1986 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1987 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1988 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1989 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1990 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1991 ccb26ccd 2018-11-05 stsp struct tm tm;
1992 45d799e2 2018-12-23 stsp time_t committer_time;
1993 11b20872 2019-11-08 stsp struct tog_color *tc;
1994 80ddbec8 2018-04-29 stsp
1995 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1996 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1997 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1998 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1999 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2000 b39d25c7 2018-07-10 stsp
2001 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2002 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2003 b39d25c7 2018-07-10 stsp else
2004 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2005 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2006 11b20872 2019-11-08 stsp if (tc)
2007 11b20872 2019-11-08 stsp wattr_on(view->window,
2008 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2009 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2010 11b20872 2019-11-08 stsp if (tc)
2011 11b20872 2019-11-08 stsp wattr_off(view->window,
2012 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2013 27a741e5 2019-09-11 stsp col = limit;
2014 b39d25c7 2018-07-10 stsp if (col > avail)
2015 b39d25c7 2018-07-10 stsp goto done;
2016 6570a66d 2019-11-08 stsp
2017 6570a66d 2019-11-08 stsp if (avail >= 120) {
2018 6570a66d 2019-11-08 stsp char *id_str;
2019 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2020 6570a66d 2019-11-08 stsp if (err)
2021 6570a66d 2019-11-08 stsp goto done;
2022 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2023 11b20872 2019-11-08 stsp if (tc)
2024 11b20872 2019-11-08 stsp wattr_on(view->window,
2025 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2026 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2027 11b20872 2019-11-08 stsp if (tc)
2028 11b20872 2019-11-08 stsp wattr_off(view->window,
2029 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2030 6570a66d 2019-11-08 stsp free(id_str);
2031 6570a66d 2019-11-08 stsp col += 9;
2032 6570a66d 2019-11-08 stsp if (col > avail)
2033 6570a66d 2019-11-08 stsp goto done;
2034 6570a66d 2019-11-08 stsp }
2035 b39d25c7 2018-07-10 stsp
2036 f69c5a46 2022-07-19 thomas if (s->use_committer)
2037 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2038 f69c5a46 2022-07-19 thomas else
2039 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2040 5813d178 2019-03-09 stsp if (author == NULL) {
2041 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2042 80ddbec8 2018-04-29 stsp goto done;
2043 80ddbec8 2018-04-29 stsp }
2044 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2045 bb737323 2018-05-20 stsp if (err)
2046 bb737323 2018-05-20 stsp goto done;
2047 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2048 11b20872 2019-11-08 stsp if (tc)
2049 11b20872 2019-11-08 stsp wattr_on(view->window,
2050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2051 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2052 11b20872 2019-11-08 stsp if (tc)
2053 11b20872 2019-11-08 stsp wattr_off(view->window,
2054 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2055 bb737323 2018-05-20 stsp col += author_width;
2056 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2057 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2058 bb737323 2018-05-20 stsp col++;
2059 bb737323 2018-05-20 stsp author_width++;
2060 bb737323 2018-05-20 stsp }
2061 9c2eaf34 2018-05-20 stsp if (col > avail)
2062 9c2eaf34 2018-05-20 stsp goto done;
2063 80ddbec8 2018-04-29 stsp
2064 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2065 5943eee2 2019-08-13 stsp if (err)
2066 6d9fbc00 2018-04-29 stsp goto done;
2067 bb737323 2018-05-20 stsp logmsg = logmsg0;
2068 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2069 bb737323 2018-05-20 stsp logmsg++;
2070 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2071 bb737323 2018-05-20 stsp if (newline)
2072 bb737323 2018-05-20 stsp *newline = '\0';
2073 f91a2b48 2022-06-23 thomas limit = avail - col;
2074 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2075 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2076 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2077 f91a2b48 2022-06-23 thomas limit, col, 1);
2078 331b1a16 2022-06-23 thomas if (err)
2079 331b1a16 2022-06-23 thomas goto done;
2080 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2081 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2082 27a741e5 2019-09-11 stsp while (col < avail) {
2083 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2084 bb737323 2018-05-20 stsp col++;
2085 881b2d3e 2018-04-30 stsp }
2086 80ddbec8 2018-04-29 stsp done:
2087 80ddbec8 2018-04-29 stsp free(logmsg0);
2088 bb737323 2018-05-20 stsp free(wlogmsg);
2089 5813d178 2019-03-09 stsp free(author);
2090 bb737323 2018-05-20 stsp free(wauthor);
2091 80ddbec8 2018-04-29 stsp free(line);
2092 80ddbec8 2018-04-29 stsp return err;
2093 80ddbec8 2018-04-29 stsp }
2094 26ed57b2 2018-05-19 stsp
2095 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2096 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2097 899d86c2 2018-05-10 stsp struct got_object_id *id)
2098 80ddbec8 2018-04-29 stsp {
2099 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2100 80ddbec8 2018-04-29 stsp
2101 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2102 80ddbec8 2018-04-29 stsp if (entry == NULL)
2103 899d86c2 2018-05-10 stsp return NULL;
2104 99db9666 2018-05-07 stsp
2105 899d86c2 2018-05-10 stsp entry->id = id;
2106 99db9666 2018-05-07 stsp entry->commit = commit;
2107 899d86c2 2018-05-10 stsp return entry;
2108 99db9666 2018-05-07 stsp }
2109 80ddbec8 2018-04-29 stsp
2110 99db9666 2018-05-07 stsp static void
2111 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2112 99db9666 2018-05-07 stsp {
2113 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2114 99db9666 2018-05-07 stsp
2115 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2116 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2117 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2118 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2119 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2120 99db9666 2018-05-07 stsp free(entry);
2121 99db9666 2018-05-07 stsp }
2122 99db9666 2018-05-07 stsp
2123 99db9666 2018-05-07 stsp static void
2124 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2125 99db9666 2018-05-07 stsp {
2126 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2127 99db9666 2018-05-07 stsp pop_commit(commits);
2128 c4972b91 2018-05-07 stsp }
2129 c4972b91 2018-05-07 stsp
2130 c4972b91 2018-05-07 stsp static const struct got_error *
2131 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2132 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2133 13add988 2019-10-15 stsp {
2134 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2135 13add988 2019-10-15 stsp regmatch_t regmatch;
2136 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2137 13add988 2019-10-15 stsp
2138 13add988 2019-10-15 stsp *have_match = 0;
2139 13add988 2019-10-15 stsp
2140 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2141 13add988 2019-10-15 stsp if (err)
2142 13add988 2019-10-15 stsp return err;
2143 13add988 2019-10-15 stsp
2144 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2145 13add988 2019-10-15 stsp if (err)
2146 13add988 2019-10-15 stsp goto done;
2147 13add988 2019-10-15 stsp
2148 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2149 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2150 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2151 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2152 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2153 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2154 13add988 2019-10-15 stsp *have_match = 1;
2155 13add988 2019-10-15 stsp done:
2156 13add988 2019-10-15 stsp free(id_str);
2157 13add988 2019-10-15 stsp free(logmsg);
2158 13add988 2019-10-15 stsp return err;
2159 13add988 2019-10-15 stsp }
2160 13add988 2019-10-15 stsp
2161 13add988 2019-10-15 stsp static const struct got_error *
2162 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2163 c4972b91 2018-05-07 stsp {
2164 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2165 9ba79e04 2018-06-11 stsp
2166 1a76625f 2018-10-22 stsp /*
2167 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2168 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2169 1a76625f 2018-10-22 stsp * while updating the display.
2170 1a76625f 2018-10-22 stsp */
2171 4e0d2870 2020-12-07 naddy do {
2172 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2173 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2174 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2175 1a76625f 2018-10-22 stsp int errcode;
2176 899d86c2 2018-05-10 stsp
2177 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2178 4e0d2870 2020-12-07 naddy NULL, NULL);
2179 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2180 ecb28ae0 2018-07-16 stsp break;
2181 899d86c2 2018-05-10 stsp
2182 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2183 9ba79e04 2018-06-11 stsp if (err)
2184 9ba79e04 2018-06-11 stsp break;
2185 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2186 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2187 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2188 9ba79e04 2018-06-11 stsp break;
2189 9ba79e04 2018-06-11 stsp }
2190 93e45b7c 2018-09-24 stsp
2191 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2192 1a76625f 2018-10-22 stsp if (errcode) {
2193 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2194 13add988 2019-10-15 stsp "pthread_mutex_lock");
2195 1a76625f 2018-10-22 stsp break;
2196 1a76625f 2018-10-22 stsp }
2197 1a76625f 2018-10-22 stsp
2198 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2199 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2200 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2201 1a76625f 2018-10-22 stsp
2202 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2203 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2204 7c1452c1 2020-03-26 stsp int have_match;
2205 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2206 7c1452c1 2020-03-26 stsp if (err)
2207 7c1452c1 2020-03-26 stsp break;
2208 7c1452c1 2020-03-26 stsp if (have_match)
2209 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2210 13add988 2019-10-15 stsp }
2211 13add988 2019-10-15 stsp
2212 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2213 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2214 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2215 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2216 7c1452c1 2020-03-26 stsp if (err)
2217 13add988 2019-10-15 stsp break;
2218 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2219 899d86c2 2018-05-10 stsp
2220 9ba79e04 2018-06-11 stsp return err;
2221 0553a4e3 2018-04-30 stsp }
2222 0553a4e3 2018-04-30 stsp
2223 2b779855 2020-12-05 naddy static void
2224 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2225 2b779855 2020-12-05 naddy {
2226 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2227 2b779855 2020-12-05 naddy int ncommits = 0;
2228 2b779855 2020-12-05 naddy
2229 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2230 2b779855 2020-12-05 naddy while (entry) {
2231 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2232 2b779855 2020-12-05 naddy s->selected_entry = entry;
2233 2b779855 2020-12-05 naddy break;
2234 2b779855 2020-12-05 naddy }
2235 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2236 2b779855 2020-12-05 naddy ncommits++;
2237 2b779855 2020-12-05 naddy }
2238 2b779855 2020-12-05 naddy }
2239 2b779855 2020-12-05 naddy
2240 0553a4e3 2018-04-30 stsp static const struct got_error *
2241 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2242 0553a4e3 2018-04-30 stsp {
2243 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2244 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2245 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2246 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2247 60493ae3 2019-06-20 stsp int width;
2248 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2249 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2250 8b473291 2019-02-21 stsp char *refs_str = NULL;
2251 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2252 11b20872 2019-11-08 stsp struct tog_color *tc;
2253 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2254 0553a4e3 2018-04-30 stsp
2255 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2256 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2257 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2258 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2259 1a76625f 2018-10-22 stsp if (err)
2260 ecb28ae0 2018-07-16 stsp return err;
2261 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2262 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2263 d2075bf3 2020-12-25 stsp if (refs) {
2264 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2265 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2266 d2075bf3 2020-12-25 stsp if (err)
2267 d2075bf3 2020-12-25 stsp goto done;
2268 d2075bf3 2020-12-25 stsp }
2269 867c6645 2018-07-10 stsp }
2270 359bfafd 2019-02-22 stsp
2271 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2272 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2273 1a76625f 2018-10-22 stsp
2274 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2275 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2276 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2277 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2278 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2279 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2280 8f4ed634 2020-03-26 stsp goto done;
2281 8f4ed634 2020-03-26 stsp }
2282 8f4ed634 2020-03-26 stsp } else {
2283 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2284 f9686aa5 2020-03-27 stsp
2285 f9686aa5 2020-03-27 stsp if (view->searching) {
2286 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2287 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2288 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2289 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2290 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2291 f9686aa5 2020-03-27 stsp search_str = "searching...";
2292 f9686aa5 2020-03-27 stsp }
2293 f9686aa5 2020-03-27 stsp
2294 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2295 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2296 f9686aa5 2020-03-27 stsp search_str ? search_str :
2297 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2298 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2299 8f4ed634 2020-03-26 stsp goto done;
2300 8f4ed634 2020-03-26 stsp }
2301 8b473291 2019-02-21 stsp }
2302 1a76625f 2018-10-22 stsp
2303 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2304 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2305 91198554 2022-06-23 thomas "........................................",
2306 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2307 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2308 1a76625f 2018-10-22 stsp header = NULL;
2309 1a76625f 2018-10-22 stsp goto done;
2310 1a76625f 2018-10-22 stsp }
2311 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2312 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2313 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2314 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2315 1a76625f 2018-10-22 stsp header = NULL;
2316 1a76625f 2018-10-22 stsp goto done;
2317 ecb28ae0 2018-07-16 stsp }
2318 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2319 1a76625f 2018-10-22 stsp if (err)
2320 1a76625f 2018-10-22 stsp goto done;
2321 867c6645 2018-07-10 stsp
2322 2814baeb 2018-08-01 stsp werase(view->window);
2323 867c6645 2018-07-10 stsp
2324 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2325 a3404814 2018-09-02 stsp wstandout(view->window);
2326 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2327 11b20872 2019-11-08 stsp if (tc)
2328 11b20872 2019-11-08 stsp wattr_on(view->window,
2329 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2330 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2331 11b20872 2019-11-08 stsp if (tc)
2332 11b20872 2019-11-08 stsp wattr_off(view->window,
2333 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2334 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2335 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2336 1a76625f 2018-10-22 stsp width++;
2337 1a76625f 2018-10-22 stsp }
2338 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2339 a3404814 2018-09-02 stsp wstandend(view->window);
2340 ecb28ae0 2018-07-16 stsp free(wline);
2341 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2342 1a76625f 2018-10-22 stsp goto done;
2343 0553a4e3 2018-04-30 stsp
2344 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2345 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2346 5813d178 2019-03-09 stsp ncommits = 0;
2347 05171be4 2022-06-23 thomas view->maxx = 0;
2348 5813d178 2019-03-09 stsp while (entry) {
2349 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2350 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2351 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2352 5813d178 2019-03-09 stsp int width;
2353 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2354 5813d178 2019-03-09 stsp break;
2355 f69c5a46 2022-07-19 thomas if (s->use_committer)
2356 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2357 f69c5a46 2022-07-19 thomas else
2358 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2359 5813d178 2019-03-09 stsp if (author == NULL) {
2360 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2361 5813d178 2019-03-09 stsp goto done;
2362 5813d178 2019-03-09 stsp }
2363 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2364 27a741e5 2019-09-11 stsp date_display_cols);
2365 5813d178 2019-03-09 stsp if (author_cols < width)
2366 5813d178 2019-03-09 stsp author_cols = width;
2367 5813d178 2019-03-09 stsp free(wauthor);
2368 5813d178 2019-03-09 stsp free(author);
2369 ef944b8b 2022-07-21 thomas if (err)
2370 ef944b8b 2022-07-21 thomas goto done;
2371 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2372 05171be4 2022-06-23 thomas if (err)
2373 05171be4 2022-06-23 thomas goto done;
2374 05171be4 2022-06-23 thomas msg = msg0;
2375 05171be4 2022-06-23 thomas while (*msg == '\n')
2376 05171be4 2022-06-23 thomas ++msg;
2377 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2378 331b1a16 2022-06-23 thomas *eol = '\0';
2379 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2380 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2381 331b1a16 2022-06-23 thomas if (err)
2382 331b1a16 2022-06-23 thomas goto done;
2383 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2384 05171be4 2022-06-23 thomas free(msg0);
2385 331b1a16 2022-06-23 thomas free(wmsg);
2386 7ca04879 2019-10-19 stsp ncommits++;
2387 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2388 5813d178 2019-03-09 stsp }
2389 5813d178 2019-03-09 stsp
2390 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2391 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2392 867c6645 2018-07-10 stsp ncommits = 0;
2393 899d86c2 2018-05-10 stsp while (entry) {
2394 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2395 899d86c2 2018-05-10 stsp break;
2396 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2397 2814baeb 2018-08-01 stsp wstandout(view->window);
2398 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2399 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2400 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2401 2814baeb 2018-08-01 stsp wstandend(view->window);
2402 0553a4e3 2018-04-30 stsp if (err)
2403 60493ae3 2019-06-20 stsp goto done;
2404 0553a4e3 2018-04-30 stsp ncommits++;
2405 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2406 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2407 80ddbec8 2018-04-29 stsp }
2408 80ddbec8 2018-04-29 stsp
2409 a5d43cac 2022-07-01 thomas view_border(view);
2410 1a76625f 2018-10-22 stsp done:
2411 1a76625f 2018-10-22 stsp free(id_str);
2412 8b473291 2019-02-21 stsp free(refs_str);
2413 1a76625f 2018-10-22 stsp free(ncommits_str);
2414 1a76625f 2018-10-22 stsp free(header);
2415 80ddbec8 2018-04-29 stsp return err;
2416 9f7d7167 2018-04-29 stsp }
2417 07b55e75 2018-05-10 stsp
2418 07b55e75 2018-05-10 stsp static void
2419 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2420 07b55e75 2018-05-10 stsp {
2421 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2422 07b55e75 2018-05-10 stsp int nscrolled = 0;
2423 07b55e75 2018-05-10 stsp
2424 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2425 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2426 07b55e75 2018-05-10 stsp return;
2427 9f7d7167 2018-04-29 stsp
2428 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2429 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2430 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2431 07b55e75 2018-05-10 stsp if (entry) {
2432 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2433 07b55e75 2018-05-10 stsp nscrolled++;
2434 07b55e75 2018-05-10 stsp }
2435 07b55e75 2018-05-10 stsp }
2436 aa075928 2018-05-10 stsp }
2437 aa075928 2018-05-10 stsp
2438 aa075928 2018-05-10 stsp static const struct got_error *
2439 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2440 aa075928 2018-05-10 stsp {
2441 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2442 5e224a3e 2019-02-22 stsp int errcode;
2443 8a42fca8 2019-02-22 stsp
2444 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2445 aa075928 2018-05-10 stsp
2446 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2447 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2448 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2449 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2450 7aafa0d1 2019-02-22 stsp if (errcode)
2451 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2452 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2453 7c1452c1 2020-03-26 stsp
2454 7c1452c1 2020-03-26 stsp /*
2455 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2456 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2457 7c1452c1 2020-03-26 stsp */
2458 7c1452c1 2020-03-26 stsp if (!wait)
2459 7c1452c1 2020-03-26 stsp break;
2460 7c1452c1 2020-03-26 stsp
2461 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2462 ffe38506 2020-12-01 naddy show_log_view(view);
2463 7c1452c1 2020-03-26 stsp update_panels();
2464 7c1452c1 2020-03-26 stsp doupdate();
2465 7c1452c1 2020-03-26 stsp
2466 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2467 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2468 82954512 2020-02-03 stsp if (errcode)
2469 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2470 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2471 82954512 2020-02-03 stsp
2472 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2473 ffe38506 2020-12-01 naddy show_log_view(view);
2474 7c1452c1 2020-03-26 stsp update_panels();
2475 7c1452c1 2020-03-26 stsp doupdate();
2476 5e224a3e 2019-02-22 stsp }
2477 5e224a3e 2019-02-22 stsp
2478 5e224a3e 2019-02-22 stsp return NULL;
2479 a5d43cac 2022-07-01 thomas }
2480 a5d43cac 2022-07-01 thomas
2481 a5d43cac 2022-07-01 thomas static const struct got_error *
2482 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2483 a5d43cac 2022-07-01 thomas {
2484 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2485 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2486 fe731b51 2022-07-14 thomas
2487 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2488 fe731b51 2022-07-14 thomas return NULL;
2489 a5d43cac 2022-07-01 thomas
2490 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2491 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2492 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2493 a5d43cac 2022-07-01 thomas
2494 a5d43cac 2022-07-01 thomas return err;
2495 5e224a3e 2019-02-22 stsp }
2496 5e224a3e 2019-02-22 stsp
2497 5e224a3e 2019-02-22 stsp static const struct got_error *
2498 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2499 5e224a3e 2019-02-22 stsp {
2500 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2501 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2502 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2503 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2504 5e224a3e 2019-02-22 stsp
2505 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2506 5e224a3e 2019-02-22 stsp return NULL;
2507 5e224a3e 2019-02-22 stsp
2508 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2509 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2510 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2511 08ebd0a9 2019-02-22 stsp /*
2512 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2513 08ebd0a9 2019-02-22 stsp */
2514 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2515 07dd3ed3 2022-08-06 thomas ncommits_needed - s->commits.ncommits;
2516 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2517 5e224a3e 2019-02-22 stsp if (err)
2518 5e224a3e 2019-02-22 stsp return err;
2519 7aafa0d1 2019-02-22 stsp }
2520 b295e71b 2019-02-22 stsp
2521 7aafa0d1 2019-02-22 stsp do {
2522 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2523 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2524 88048b54 2019-02-21 stsp break;
2525 88048b54 2019-02-21 stsp
2526 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2527 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2528 aa075928 2018-05-10 stsp
2529 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2530 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2531 dd0a52c1 2018-05-20 stsp break;
2532 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2533 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2534 aa075928 2018-05-10 stsp
2535 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2536 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2537 a5d43cac 2022-07-01 thomas else
2538 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2539 a5d43cac 2022-07-01 thomas
2540 dd0a52c1 2018-05-20 stsp return err;
2541 07b55e75 2018-05-10 stsp }
2542 4a7f7875 2018-05-10 stsp
2543 cd0acaa7 2018-05-20 stsp static const struct got_error *
2544 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2545 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2546 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2547 cd0acaa7 2018-05-20 stsp {
2548 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2549 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2550 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2551 cd0acaa7 2018-05-20 stsp
2552 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2553 15a94983 2018-12-23 stsp if (diff_view == NULL)
2554 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2555 ea5e7bb5 2018-08-01 stsp
2556 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2557 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2558 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2559 e5a0f69f 2018-08-18 stsp if (err == NULL)
2560 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2561 cd0acaa7 2018-05-20 stsp return err;
2562 4a7f7875 2018-05-10 stsp }
2563 4a7f7875 2018-05-10 stsp
2564 80ddbec8 2018-04-29 stsp static const struct got_error *
2565 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2566 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2567 9343a5fb 2018-06-23 stsp {
2568 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2569 941e9f74 2019-05-21 stsp
2570 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2571 941e9f74 2019-05-21 stsp if (parent == NULL)
2572 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2573 941e9f74 2019-05-21 stsp
2574 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2575 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2576 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2577 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2578 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2579 941e9f74 2019-05-21 stsp s->tree = subtree;
2580 941e9f74 2019-05-21 stsp s->selected = 0;
2581 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2582 941e9f74 2019-05-21 stsp return NULL;
2583 941e9f74 2019-05-21 stsp }
2584 941e9f74 2019-05-21 stsp
2585 941e9f74 2019-05-21 stsp static const struct got_error *
2586 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2587 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2588 941e9f74 2019-05-21 stsp {
2589 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2590 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2591 941e9f74 2019-05-21 stsp const char *p;
2592 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2593 9343a5fb 2018-06-23 stsp
2594 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2595 941e9f74 2019-05-21 stsp p = path;
2596 941e9f74 2019-05-21 stsp while (*p) {
2597 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2598 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2599 56e0773d 2019-11-28 stsp char *te_name;
2600 33cbf02b 2020-01-12 stsp
2601 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2602 33cbf02b 2020-01-12 stsp p++;
2603 941e9f74 2019-05-21 stsp
2604 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2605 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2606 941e9f74 2019-05-21 stsp if (slash == NULL)
2607 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2608 33cbf02b 2020-01-12 stsp else
2609 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2610 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2611 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2612 56e0773d 2019-11-28 stsp break;
2613 941e9f74 2019-05-21 stsp }
2614 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2615 56e0773d 2019-11-28 stsp if (te == NULL) {
2616 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2617 56e0773d 2019-11-28 stsp free(te_name);
2618 941e9f74 2019-05-21 stsp break;
2619 941e9f74 2019-05-21 stsp }
2620 56e0773d 2019-11-28 stsp free(te_name);
2621 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2622 941e9f74 2019-05-21 stsp
2623 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2624 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2625 b03c880f 2019-05-21 stsp
2626 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2627 941e9f74 2019-05-21 stsp if (slash)
2628 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2629 941e9f74 2019-05-21 stsp else
2630 941e9f74 2019-05-21 stsp subpath = strdup(path);
2631 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2632 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2633 941e9f74 2019-05-21 stsp break;
2634 941e9f74 2019-05-21 stsp }
2635 941e9f74 2019-05-21 stsp
2636 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2637 941e9f74 2019-05-21 stsp subpath);
2638 941e9f74 2019-05-21 stsp if (err)
2639 941e9f74 2019-05-21 stsp break;
2640 941e9f74 2019-05-21 stsp
2641 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2642 941e9f74 2019-05-21 stsp free(tree_id);
2643 941e9f74 2019-05-21 stsp if (err)
2644 941e9f74 2019-05-21 stsp break;
2645 941e9f74 2019-05-21 stsp
2646 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2647 941e9f74 2019-05-21 stsp if (err) {
2648 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2649 941e9f74 2019-05-21 stsp break;
2650 941e9f74 2019-05-21 stsp }
2651 941e9f74 2019-05-21 stsp if (slash == NULL)
2652 941e9f74 2019-05-21 stsp break;
2653 941e9f74 2019-05-21 stsp free(subpath);
2654 941e9f74 2019-05-21 stsp subpath = NULL;
2655 941e9f74 2019-05-21 stsp p = slash;
2656 941e9f74 2019-05-21 stsp }
2657 941e9f74 2019-05-21 stsp
2658 941e9f74 2019-05-21 stsp free(subpath);
2659 1a76625f 2018-10-22 stsp return err;
2660 61266923 2020-01-14 stsp }
2661 61266923 2020-01-14 stsp
2662 61266923 2020-01-14 stsp static const struct got_error *
2663 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2664 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2665 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2666 55cccc34 2020-02-20 stsp {
2667 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2668 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2669 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2670 55cccc34 2020-02-20 stsp
2671 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2672 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2673 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2674 55cccc34 2020-02-20 stsp
2675 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2676 bc573f3b 2021-07-10 stsp if (err)
2677 55cccc34 2020-02-20 stsp return err;
2678 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2679 55cccc34 2020-02-20 stsp
2680 55cccc34 2020-02-20 stsp *new_view = tree_view;
2681 55cccc34 2020-02-20 stsp
2682 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2683 55cccc34 2020-02-20 stsp return NULL;
2684 55cccc34 2020-02-20 stsp
2685 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2686 55cccc34 2020-02-20 stsp }
2687 55cccc34 2020-02-20 stsp
2688 55cccc34 2020-02-20 stsp static const struct got_error *
2689 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2690 61266923 2020-01-14 stsp {
2691 61266923 2020-01-14 stsp sigset_t sigset;
2692 61266923 2020-01-14 stsp int errcode;
2693 61266923 2020-01-14 stsp
2694 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2695 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2696 61266923 2020-01-14 stsp
2697 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2698 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2699 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2700 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2701 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2702 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2703 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2704 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2705 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2706 61266923 2020-01-14 stsp
2707 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2708 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2709 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2710 61266923 2020-01-14 stsp
2711 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2712 61266923 2020-01-14 stsp if (errcode)
2713 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2714 61266923 2020-01-14 stsp
2715 61266923 2020-01-14 stsp return NULL;
2716 1a76625f 2018-10-22 stsp }
2717 1a76625f 2018-10-22 stsp
2718 1a76625f 2018-10-22 stsp static void *
2719 1a76625f 2018-10-22 stsp log_thread(void *arg)
2720 1a76625f 2018-10-22 stsp {
2721 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2722 1a76625f 2018-10-22 stsp int errcode = 0;
2723 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2724 1a76625f 2018-10-22 stsp int done = 0;
2725 61266923 2020-01-14 stsp
2726 f2d749db 2022-07-12 thomas /*
2727 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2728 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2729 f2d749db 2022-07-12 thomas */
2730 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2731 f2d749db 2022-07-12 thomas if (errcode) {
2732 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2733 61266923 2020-01-14 stsp return (void *)err;
2734 f2d749db 2022-07-12 thomas }
2735 1a76625f 2018-10-22 stsp
2736 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2737 f2d749db 2022-07-12 thomas if (err) {
2738 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2739 f2d749db 2022-07-12 thomas goto done;
2740 f2d749db 2022-07-12 thomas }
2741 f2d749db 2022-07-12 thomas
2742 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2743 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2744 f2d749db 2022-07-12 thomas if (errcode) {
2745 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2746 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2747 f2d749db 2022-07-12 thomas goto done;
2748 f2d749db 2022-07-12 thomas }
2749 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2750 1a76625f 2018-10-22 stsp if (err) {
2751 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2752 f2d749db 2022-07-12 thomas goto done;
2753 1a76625f 2018-10-22 stsp err = NULL;
2754 1a76625f 2018-10-22 stsp done = 1;
2755 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2756 1a76625f 2018-10-22 stsp a->commits_needed--;
2757 1a76625f 2018-10-22 stsp
2758 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2759 3abe8080 2019-04-10 stsp if (errcode) {
2760 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2761 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2762 f2d749db 2022-07-12 thomas goto done;
2763 3abe8080 2019-04-10 stsp } else if (*a->quit)
2764 1a76625f 2018-10-22 stsp done = 1;
2765 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2766 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2767 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2768 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2769 1a76625f 2018-10-22 stsp }
2770 1a76625f 2018-10-22 stsp
2771 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2772 7c1452c1 2020-03-26 stsp if (errcode) {
2773 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2774 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2775 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2776 f2d749db 2022-07-12 thomas goto done;
2777 7c1452c1 2020-03-26 stsp }
2778 7c1452c1 2020-03-26 stsp
2779 1a76625f 2018-10-22 stsp if (done)
2780 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2781 7c1452c1 2020-03-26 stsp else {
2782 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2783 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2784 7c1452c1 2020-03-26 stsp &tog_mutex);
2785 f2d749db 2022-07-12 thomas if (errcode) {
2786 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2787 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2788 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2789 f2d749db 2022-07-12 thomas goto done;
2790 f2d749db 2022-07-12 thomas }
2791 21355643 2020-12-06 stsp if (*a->quit)
2792 21355643 2020-12-06 stsp done = 1;
2793 7c1452c1 2020-03-26 stsp }
2794 1a76625f 2018-10-22 stsp }
2795 1a76625f 2018-10-22 stsp }
2796 3abe8080 2019-04-10 stsp a->log_complete = 1;
2797 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2798 f2d749db 2022-07-12 thomas if (errcode)
2799 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2800 f2d749db 2022-07-12 thomas done:
2801 f2d749db 2022-07-12 thomas if (err) {
2802 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2803 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2804 f2d749db 2022-07-12 thomas }
2805 1a76625f 2018-10-22 stsp return (void *)err;
2806 1a76625f 2018-10-22 stsp }
2807 1a76625f 2018-10-22 stsp
2808 1a76625f 2018-10-22 stsp static const struct got_error *
2809 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2810 1a76625f 2018-10-22 stsp {
2811 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2812 1a76625f 2018-10-22 stsp int errcode;
2813 1a76625f 2018-10-22 stsp
2814 1a76625f 2018-10-22 stsp if (s->thread) {
2815 1a76625f 2018-10-22 stsp s->quit = 1;
2816 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2817 1a76625f 2018-10-22 stsp if (errcode)
2818 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2819 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2820 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2821 1a76625f 2018-10-22 stsp if (errcode)
2822 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2823 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2824 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2825 1a76625f 2018-10-22 stsp if (errcode)
2826 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2827 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2828 1a76625f 2018-10-22 stsp if (errcode)
2829 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2830 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2831 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2832 1a76625f 2018-10-22 stsp }
2833 1a76625f 2018-10-22 stsp
2834 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2835 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2836 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2837 1af5eddf 2022-06-23 thomas }
2838 1af5eddf 2022-06-23 thomas
2839 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2840 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2841 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2842 1af5eddf 2022-06-23 thomas if (err == NULL)
2843 1af5eddf 2022-06-23 thomas err = pack_err;
2844 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2845 1a76625f 2018-10-22 stsp }
2846 1a76625f 2018-10-22 stsp
2847 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2848 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2849 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2850 1a76625f 2018-10-22 stsp }
2851 1a76625f 2018-10-22 stsp
2852 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2853 9343a5fb 2018-06-23 stsp }
2854 9343a5fb 2018-06-23 stsp
2855 9343a5fb 2018-06-23 stsp static const struct got_error *
2856 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2857 1a76625f 2018-10-22 stsp {
2858 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2859 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2860 276b94a1 2020-11-13 naddy int errcode;
2861 1a76625f 2018-10-22 stsp
2862 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2863 276b94a1 2020-11-13 naddy
2864 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2865 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2866 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2867 276b94a1 2020-11-13 naddy
2868 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2869 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2870 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2871 276b94a1 2020-11-13 naddy
2872 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2873 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2874 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2875 1a76625f 2018-10-22 stsp free(s->start_id);
2876 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2877 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2878 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2879 1a76625f 2018-10-22 stsp return err;
2880 1a76625f 2018-10-22 stsp }
2881 1a76625f 2018-10-22 stsp
2882 1a76625f 2018-10-22 stsp static const struct got_error *
2883 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2884 60493ae3 2019-06-20 stsp {
2885 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2886 60493ae3 2019-06-20 stsp
2887 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2888 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2889 60493ae3 2019-06-20 stsp return NULL;
2890 60493ae3 2019-06-20 stsp }
2891 60493ae3 2019-06-20 stsp
2892 60493ae3 2019-06-20 stsp static const struct got_error *
2893 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2894 60493ae3 2019-06-20 stsp {
2895 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2896 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2897 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2898 60493ae3 2019-06-20 stsp
2899 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2900 f9686aa5 2020-03-27 stsp show_log_view(view);
2901 f9686aa5 2020-03-27 stsp update_panels();
2902 f9686aa5 2020-03-27 stsp doupdate();
2903 f9686aa5 2020-03-27 stsp
2904 96e2b566 2019-07-08 stsp if (s->search_entry) {
2905 13add988 2019-10-15 stsp int errcode, ch;
2906 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2907 13add988 2019-10-15 stsp if (errcode)
2908 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2909 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2910 13add988 2019-10-15 stsp ch = wgetch(view->window);
2911 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2912 13add988 2019-10-15 stsp if (errcode)
2913 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2914 13add988 2019-10-15 stsp "pthread_mutex_lock");
2915 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2916 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2917 678cbce5 2019-07-28 stsp return NULL;
2918 678cbce5 2019-07-28 stsp }
2919 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2920 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2921 96e2b566 2019-07-08 stsp else
2922 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2923 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2924 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2925 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2926 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2927 df5e841d 2022-06-23 thomas
2928 df5e841d 2022-06-23 thomas /*
2929 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2930 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2931 1f4d5162 2022-06-23 thomas * might have changed.
2932 df5e841d 2022-06-23 thomas */
2933 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2934 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2935 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2936 df5e841d 2022-06-23 thomas else
2937 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2938 b74feda6 2022-06-23 thomas } else {
2939 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2940 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2941 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2942 df5e841d 2022-06-23 thomas else
2943 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2944 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2945 b74feda6 2022-06-23 thomas }
2946 20be8d96 2019-06-21 stsp } else {
2947 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2948 20be8d96 2019-06-21 stsp }
2949 60493ae3 2019-06-20 stsp
2950 60493ae3 2019-06-20 stsp while (1) {
2951 13add988 2019-10-15 stsp int have_match = 0;
2952 13add988 2019-10-15 stsp
2953 60493ae3 2019-06-20 stsp if (entry == NULL) {
2954 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2955 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2956 f9967bca 2020-03-27 stsp view->search_next_done =
2957 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2958 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2959 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2960 f801134a 2019-06-25 stsp return NULL;
2961 60493ae3 2019-06-20 stsp }
2962 96e2b566 2019-07-08 stsp /*
2963 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2964 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2965 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2966 96e2b566 2019-07-08 stsp */
2967 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2968 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2969 60493ae3 2019-06-20 stsp }
2970 60493ae3 2019-06-20 stsp
2971 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2972 13add988 2019-10-15 stsp &view->regex);
2973 5943eee2 2019-08-13 stsp if (err)
2974 13add988 2019-10-15 stsp break;
2975 13add988 2019-10-15 stsp if (have_match) {
2976 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2977 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2978 60493ae3 2019-06-20 stsp break;
2979 60493ae3 2019-06-20 stsp }
2980 13add988 2019-10-15 stsp
2981 96e2b566 2019-07-08 stsp s->search_entry = entry;
2982 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2983 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2984 b1bf1435 2019-06-21 stsp else
2985 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2986 60493ae3 2019-06-20 stsp }
2987 60493ae3 2019-06-20 stsp
2988 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2989 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2990 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2991 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2992 60493ae3 2019-06-20 stsp if (err)
2993 60493ae3 2019-06-20 stsp return err;
2994 ead14cbe 2019-06-21 stsp cur++;
2995 ead14cbe 2019-06-21 stsp }
2996 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2997 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2998 60493ae3 2019-06-20 stsp if (err)
2999 60493ae3 2019-06-20 stsp return err;
3000 ead14cbe 2019-06-21 stsp cur--;
3001 60493ae3 2019-06-20 stsp }
3002 60493ae3 2019-06-20 stsp }
3003 60493ae3 2019-06-20 stsp
3004 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3005 96e2b566 2019-07-08 stsp
3006 60493ae3 2019-06-20 stsp return NULL;
3007 60493ae3 2019-06-20 stsp }
3008 60493ae3 2019-06-20 stsp
3009 60493ae3 2019-06-20 stsp static const struct got_error *
3010 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3011 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3012 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3013 80ddbec8 2018-04-29 stsp {
3014 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3015 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3016 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3017 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3018 1a76625f 2018-10-22 stsp int errcode;
3019 80ddbec8 2018-04-29 stsp
3020 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3021 f135c941 2020-02-20 stsp free(s->in_repo_path);
3022 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3023 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3024 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3025 f135c941 2020-02-20 stsp }
3026 ecb28ae0 2018-07-16 stsp
3027 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3028 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3029 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3030 78756c87 2020-11-24 stsp
3031 fb2756b9 2018-08-04 stsp s->repo = repo;
3032 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3033 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3034 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3035 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3036 9cd7cbd1 2020-12-07 stsp goto done;
3037 9cd7cbd1 2020-12-07 stsp }
3038 9cd7cbd1 2020-12-07 stsp }
3039 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3040 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3041 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3042 5036bf37 2018-09-24 stsp goto done;
3043 5036bf37 2018-09-24 stsp }
3044 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3045 e5a0f69f 2018-08-18 stsp
3046 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3047 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3048 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3049 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3050 11b20872 2019-11-08 stsp if (err)
3051 11b20872 2019-11-08 stsp goto done;
3052 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3053 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3054 11b20872 2019-11-08 stsp if (err) {
3055 11b20872 2019-11-08 stsp free_colors(&s->colors);
3056 11b20872 2019-11-08 stsp goto done;
3057 11b20872 2019-11-08 stsp }
3058 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3059 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3060 11b20872 2019-11-08 stsp if (err) {
3061 11b20872 2019-11-08 stsp free_colors(&s->colors);
3062 11b20872 2019-11-08 stsp goto done;
3063 11b20872 2019-11-08 stsp }
3064 11b20872 2019-11-08 stsp }
3065 11b20872 2019-11-08 stsp
3066 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3067 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3068 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3069 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3070 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3071 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3072 1a76625f 2018-10-22 stsp
3073 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3074 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3075 1af5eddf 2022-06-23 thomas if (err)
3076 1af5eddf 2022-06-23 thomas goto done;
3077 1af5eddf 2022-06-23 thomas }
3078 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3079 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3080 7cd52833 2022-06-23 thomas if (err)
3081 7cd52833 2022-06-23 thomas goto done;
3082 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3083 b672a97a 2020-01-27 stsp !s->log_branches);
3084 1a76625f 2018-10-22 stsp if (err)
3085 1a76625f 2018-10-22 stsp goto done;
3086 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3087 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3088 c5b78334 2020-01-12 stsp if (err)
3089 c5b78334 2020-01-12 stsp goto done;
3090 1a76625f 2018-10-22 stsp
3091 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3092 1a76625f 2018-10-22 stsp if (errcode) {
3093 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3094 1a76625f 2018-10-22 stsp goto done;
3095 1a76625f 2018-10-22 stsp }
3096 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3097 7c1452c1 2020-03-26 stsp if (errcode) {
3098 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3099 7c1452c1 2020-03-26 stsp goto done;
3100 7c1452c1 2020-03-26 stsp }
3101 1a76625f 2018-10-22 stsp
3102 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3103 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3104 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3105 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3106 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3107 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3108 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3109 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3110 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3111 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3112 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3113 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3114 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3115 ba4f502b 2018-08-04 stsp done:
3116 1a76625f 2018-10-22 stsp if (err)
3117 1a76625f 2018-10-22 stsp close_log_view(view);
3118 ba4f502b 2018-08-04 stsp return err;
3119 ba4f502b 2018-08-04 stsp }
3120 ba4f502b 2018-08-04 stsp
3121 e5a0f69f 2018-08-18 stsp static const struct got_error *
3122 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3123 ba4f502b 2018-08-04 stsp {
3124 f2f6d207 2020-11-24 stsp const struct got_error *err;
3125 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3126 ba4f502b 2018-08-04 stsp
3127 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3128 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3129 2b380cc8 2018-10-24 stsp &s->thread_args);
3130 2b380cc8 2018-10-24 stsp if (errcode)
3131 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3132 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3133 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3134 f2f6d207 2020-11-24 stsp if (err)
3135 f2f6d207 2020-11-24 stsp return err;
3136 f2f6d207 2020-11-24 stsp }
3137 2b380cc8 2018-10-24 stsp }
3138 2b380cc8 2018-10-24 stsp
3139 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3140 b31f89ff 2022-07-01 thomas }
3141 b31f89ff 2022-07-01 thomas
3142 b31f89ff 2022-07-01 thomas static void
3143 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3144 b31f89ff 2022-07-01 thomas {
3145 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3146 b31f89ff 2022-07-01 thomas
3147 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3148 b31f89ff 2022-07-01 thomas view->count = 0;
3149 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3150 b31f89ff 2022-07-01 thomas return;
3151 b31f89ff 2022-07-01 thomas
3152 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3153 b31f89ff 2022-07-01 thomas || home)
3154 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3155 b31f89ff 2022-07-01 thomas
3156 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3157 b31f89ff 2022-07-01 thomas --s->selected;
3158 b31f89ff 2022-07-01 thomas else
3159 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3160 b31f89ff 2022-07-01 thomas
3161 b31f89ff 2022-07-01 thomas select_commit(s);
3162 b31f89ff 2022-07-01 thomas return;
3163 b31f89ff 2022-07-01 thomas }
3164 b31f89ff 2022-07-01 thomas
3165 b31f89ff 2022-07-01 thomas static const struct got_error *
3166 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3167 b31f89ff 2022-07-01 thomas {
3168 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3169 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3170 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3171 b31f89ff 2022-07-01 thomas
3172 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3173 b31f89ff 2022-07-01 thomas if (first == NULL) {
3174 b31f89ff 2022-07-01 thomas view->count = 0;
3175 b31f89ff 2022-07-01 thomas return NULL;
3176 b31f89ff 2022-07-01 thomas }
3177 b31f89ff 2022-07-01 thomas
3178 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3179 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3180 b31f89ff 2022-07-01 thomas return NULL;
3181 b31f89ff 2022-07-01 thomas
3182 b31f89ff 2022-07-01 thomas if (!page) {
3183 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3184 b31f89ff 2022-07-01 thomas
3185 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3186 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3187 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3188 b31f89ff 2022-07-01 thomas ++s->selected;
3189 b31f89ff 2022-07-01 thomas else
3190 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3191 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3192 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3193 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3194 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3195 b31f89ff 2022-07-01 thomas else
3196 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3197 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3198 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3199 b31f89ff 2022-07-01 thomas } else {
3200 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3201 b31f89ff 2022-07-01 thomas if (err)
3202 b31f89ff 2022-07-01 thomas return err;
3203 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3204 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3205 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3206 b31f89ff 2022-07-01 thomas }
3207 b31f89ff 2022-07-01 thomas }
3208 b31f89ff 2022-07-01 thomas if (err)
3209 b31f89ff 2022-07-01 thomas return err;
3210 b31f89ff 2022-07-01 thomas
3211 a5d43cac 2022-07-01 thomas /*
3212 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3213 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3214 a5d43cac 2022-07-01 thomas */
3215 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3216 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3217 a5d43cac 2022-07-01 thomas
3218 b31f89ff 2022-07-01 thomas select_commit(s);
3219 b31f89ff 2022-07-01 thomas
3220 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3221 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3222 b31f89ff 2022-07-01 thomas view->count = 0;
3223 b31f89ff 2022-07-01 thomas
3224 b31f89ff 2022-07-01 thomas return NULL;
3225 e5a0f69f 2018-08-18 stsp }
3226 04cc582a 2018-08-01 stsp
3227 a5d43cac 2022-07-01 thomas static void
3228 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3229 a5d43cac 2022-07-01 thomas {
3230 24415785 2022-07-03 thomas *x = 0;
3231 24415785 2022-07-03 thomas *y = 0;
3232 24415785 2022-07-03 thomas
3233 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3234 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3235 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3236 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3237 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3238 53d2bdd3 2022-07-10 thomas else
3239 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3240 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3241 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3242 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3243 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3244 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3245 53d2bdd3 2022-07-10 thomas else
3246 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3247 53d2bdd3 2022-07-10 thomas }
3248 a5d43cac 2022-07-01 thomas }
3249 a5d43cac 2022-07-01 thomas
3250 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3251 e5a0f69f 2018-08-18 stsp static const struct got_error *
3252 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3253 a5d43cac 2022-07-01 thomas {
3254 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3255 a5d43cac 2022-07-01 thomas
3256 a5d43cac 2022-07-01 thomas view->nlines = y;
3257 64486692 2022-07-07 thomas view->ncols = COLS;
3258 a5d43cac 2022-07-01 thomas err = view_resize(view);
3259 a5d43cac 2022-07-01 thomas if (err)
3260 a5d43cac 2022-07-01 thomas return err;
3261 a5d43cac 2022-07-01 thomas
3262 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3263 a5d43cac 2022-07-01 thomas
3264 a5d43cac 2022-07-01 thomas return err;
3265 07dd3ed3 2022-08-06 thomas }
3266 07dd3ed3 2022-08-06 thomas
3267 07dd3ed3 2022-08-06 thomas static const struct got_error *
3268 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3269 07dd3ed3 2022-08-06 thomas {
3270 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3271 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3272 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3273 07dd3ed3 2022-08-06 thomas
3274 07dd3ed3 2022-08-06 thomas g = view->gline;
3275 07dd3ed3 2022-08-06 thomas view->gline = 0;
3276 07dd3ed3 2022-08-06 thomas
3277 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3278 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3279 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3280 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3281 07dd3ed3 2022-08-06 thomas select_commit(s);
3282 07dd3ed3 2022-08-06 thomas return NULL;
3283 07dd3ed3 2022-08-06 thomas }
3284 07dd3ed3 2022-08-06 thomas
3285 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3286 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3287 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3288 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3289 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3290 07dd3ed3 2022-08-06 thomas if (err)
3291 07dd3ed3 2022-08-06 thomas return err;
3292 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3293 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3294 07dd3ed3 2022-08-06 thomas
3295 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3296 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3297 07dd3ed3 2022-08-06 thomas
3298 07dd3ed3 2022-08-06 thomas select_commit(s);
3299 07dd3ed3 2022-08-06 thomas return NULL;
3300 07dd3ed3 2022-08-06 thomas
3301 a5d43cac 2022-07-01 thomas }
3302 a5d43cac 2022-07-01 thomas
3303 a5d43cac 2022-07-01 thomas static const struct got_error *
3304 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3305 e5a0f69f 2018-08-18 stsp {
3306 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3307 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3308 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3309 2a31b33b 2022-07-23 thomas int eos, n, nscroll;
3310 80ddbec8 2018-04-29 stsp
3311 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3312 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3313 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3314 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3315 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3316 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3317 fb280deb 2021-08-30 stsp }
3318 b31f89ff 2022-07-01 thomas return err;
3319 528dedf3 2021-08-30 stsp }
3320 068ab281 2022-07-01 thomas
3321 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3322 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3323 068ab281 2022-07-01 thomas --eos; /* border */
3324 07dd3ed3 2022-08-06 thomas
3325 07dd3ed3 2022-08-06 thomas if (view->gline)
3326 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3327 068ab281 2022-07-01 thomas
3328 528dedf3 2021-08-30 stsp switch (ch) {
3329 1e37a5c2 2019-05-12 jcs case 'q':
3330 1e37a5c2 2019-05-12 jcs s->quit = 1;
3331 05171be4 2022-06-23 thomas break;
3332 05171be4 2022-06-23 thomas case '0':
3333 05171be4 2022-06-23 thomas view->x = 0;
3334 05171be4 2022-06-23 thomas break;
3335 05171be4 2022-06-23 thomas case '$':
3336 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3337 07b0611c 2022-06-23 thomas view->count = 0;
3338 05171be4 2022-06-23 thomas break;
3339 05171be4 2022-06-23 thomas case KEY_RIGHT:
3340 05171be4 2022-06-23 thomas case 'l':
3341 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3342 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3343 07b0611c 2022-06-23 thomas else
3344 07b0611c 2022-06-23 thomas view->count = 0;
3345 1e37a5c2 2019-05-12 jcs break;
3346 05171be4 2022-06-23 thomas case KEY_LEFT:
3347 05171be4 2022-06-23 thomas case 'h':
3348 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3349 07b0611c 2022-06-23 thomas if (view->x <= 0)
3350 07b0611c 2022-06-23 thomas view->count = 0;
3351 05171be4 2022-06-23 thomas break;
3352 1e37a5c2 2019-05-12 jcs case 'k':
3353 1e37a5c2 2019-05-12 jcs case KEY_UP:
3354 1e37a5c2 2019-05-12 jcs case '<':
3355 1e37a5c2 2019-05-12 jcs case ',':
3356 f7140bf5 2021-10-17 thomas case CTRL('p'):
3357 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3358 912a3f79 2021-08-30 j break;
3359 912a3f79 2021-08-30 j case 'g':
3360 912a3f79 2021-08-30 j case KEY_HOME:
3361 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3362 07b0611c 2022-06-23 thomas view->count = 0;
3363 1e37a5c2 2019-05-12 jcs break;
3364 70f17a53 2022-06-13 thomas case CTRL('u'):
3365 23427b14 2022-06-23 thomas case 'u':
3366 70f17a53 2022-06-13 thomas nscroll /= 2;
3367 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3368 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3369 a4292ac5 2019-05-12 jcs case CTRL('b'):
3370 1c5e5faa 2022-06-23 thomas case 'b':
3371 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3372 1e37a5c2 2019-05-12 jcs break;
3373 1e37a5c2 2019-05-12 jcs case 'j':
3374 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3375 1e37a5c2 2019-05-12 jcs case '>':
3376 1e37a5c2 2019-05-12 jcs case '.':
3377 f7140bf5 2021-10-17 thomas case CTRL('n'):
3378 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3379 912a3f79 2021-08-30 j break;
3380 f69c5a46 2022-07-19 thomas case '@':
3381 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3382 f69c5a46 2022-07-19 thomas break;
3383 912a3f79 2021-08-30 j case 'G':
3384 912a3f79 2021-08-30 j case KEY_END: {
3385 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3386 912a3f79 2021-08-30 j * traverse them all. */
3387 07b0611c 2022-06-23 thomas view->count = 0;
3388 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3389 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3390 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3391 80ddbec8 2018-04-29 stsp }
3392 912a3f79 2021-08-30 j
3393 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3394 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3395 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3396 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3397 f3bc9f1d 2021-09-05 naddy break;
3398 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3399 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3400 f3bc9f1d 2021-09-05 naddy }
3401 f3bc9f1d 2021-09-05 naddy if (n > 0)
3402 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3403 2b779855 2020-12-05 naddy select_commit(s);
3404 1e37a5c2 2019-05-12 jcs break;
3405 912a3f79 2021-08-30 j }
3406 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3407 23427b14 2022-06-23 thomas case 'd':
3408 70f17a53 2022-06-13 thomas nscroll /= 2;
3409 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3410 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3411 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3412 4c2d69cb 2022-06-23 thomas case 'f':
3413 b31f89ff 2022-07-01 thomas case ' ':
3414 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3415 1e37a5c2 2019-05-12 jcs break;
3416 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3417 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3418 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3419 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3420 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3421 2b779855 2020-12-05 naddy select_commit(s);
3422 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3423 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3424 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3425 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3426 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3427 0bf7f153 2020-12-02 naddy }
3428 1e37a5c2 2019-05-12 jcs break;
3429 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3430 444d5325 2022-07-03 thomas case '\r':
3431 07b0611c 2022-06-23 thomas view->count = 0;
3432 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3433 e5a0f69f 2018-08-18 stsp break;
3434 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3435 1e37a5c2 2019-05-12 jcs break;
3436 1be4947a 2022-07-22 thomas case 'T':
3437 07b0611c 2022-06-23 thomas view->count = 0;
3438 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3439 5036bf37 2018-09-24 stsp break;
3440 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3441 1e37a5c2 2019-05-12 jcs break;
3442 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3443 21355643 2020-12-06 stsp case CTRL('l'):
3444 21355643 2020-12-06 stsp case 'B':
3445 07b0611c 2022-06-23 thomas view->count = 0;
3446 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3447 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3448 1e37a5c2 2019-05-12 jcs break;
3449 21355643 2020-12-06 stsp err = stop_log_thread(s);
3450 74cfe85e 2020-10-20 stsp if (err)
3451 74cfe85e 2020-10-20 stsp return err;
3452 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3453 21355643 2020-12-06 stsp char *parent_path;
3454 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3455 21355643 2020-12-06 stsp if (err)
3456 21355643 2020-12-06 stsp return err;
3457 21355643 2020-12-06 stsp free(s->in_repo_path);
3458 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3459 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3460 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3461 21355643 2020-12-06 stsp struct got_object_id *start_id;
3462 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3463 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3464 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3465 21355643 2020-12-06 stsp if (err)
3466 21355643 2020-12-06 stsp return err;
3467 21355643 2020-12-06 stsp free(s->start_id);
3468 21355643 2020-12-06 stsp s->start_id = start_id;
3469 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3470 21355643 2020-12-06 stsp } else /* 'B' */
3471 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3472 21355643 2020-12-06 stsp
3473 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3474 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3475 bf7e79b3 2022-06-23 thomas if (err)
3476 bf7e79b3 2022-06-23 thomas return err;
3477 bf7e79b3 2022-06-23 thomas }
3478 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3479 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3480 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3481 74cfe85e 2020-10-20 stsp if (err)
3482 21355643 2020-12-06 stsp return err;
3483 51a10b52 2020-12-26 stsp tog_free_refs();
3484 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3485 ca51c541 2020-12-07 stsp if (err)
3486 ca51c541 2020-12-07 stsp return err;
3487 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3488 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3489 d01904d4 2019-06-25 stsp if (err)
3490 d01904d4 2019-06-25 stsp return err;
3491 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3492 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3493 b672a97a 2020-01-27 stsp if (err)
3494 b672a97a 2020-01-27 stsp return err;
3495 21355643 2020-12-06 stsp free_commits(&s->commits);
3496 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3497 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3498 21355643 2020-12-06 stsp s->selected_entry = NULL;
3499 21355643 2020-12-06 stsp s->selected = 0;
3500 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3501 21355643 2020-12-06 stsp s->quit = 0;
3502 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3503 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3504 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3505 94ecf40d 2022-07-22 thomas view->offset = 0;
3506 d01904d4 2019-06-25 stsp break;
3507 1be4947a 2022-07-22 thomas case 'R':
3508 07b0611c 2022-06-23 thomas view->count = 0;
3509 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3510 6458efa5 2020-11-24 stsp break;
3511 1e37a5c2 2019-05-12 jcs default:
3512 07b0611c 2022-06-23 thomas view->count = 0;
3513 1e37a5c2 2019-05-12 jcs break;
3514 899d86c2 2018-05-10 stsp }
3515 e5a0f69f 2018-08-18 stsp
3516 80ddbec8 2018-04-29 stsp return err;
3517 80ddbec8 2018-04-29 stsp }
3518 80ddbec8 2018-04-29 stsp
3519 4ed7e80c 2018-05-20 stsp static const struct got_error *
3520 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3521 c2db6724 2019-01-04 stsp {
3522 c2db6724 2019-01-04 stsp const struct got_error *error;
3523 c2db6724 2019-01-04 stsp
3524 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3525 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3526 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3527 37c06ea4 2019-07-15 stsp #endif
3528 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3529 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3530 c2db6724 2019-01-04 stsp
3531 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3532 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3533 c2db6724 2019-01-04 stsp
3534 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3535 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3536 c2db6724 2019-01-04 stsp
3537 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3538 c2db6724 2019-01-04 stsp if (error != NULL)
3539 c2db6724 2019-01-04 stsp return error;
3540 c2db6724 2019-01-04 stsp
3541 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3542 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3543 c2db6724 2019-01-04 stsp
3544 c2db6724 2019-01-04 stsp return NULL;
3545 c2db6724 2019-01-04 stsp }
3546 c2db6724 2019-01-04 stsp
3547 a915003a 2019-02-05 stsp static void
3548 a915003a 2019-02-05 stsp init_curses(void)
3549 a915003a 2019-02-05 stsp {
3550 296152d1 2022-05-31 thomas /*
3551 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3552 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3553 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3554 296152d1 2022-05-31 thomas */
3555 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3556 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3557 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3558 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3559 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3560 296152d1 2022-05-31 thomas
3561 a915003a 2019-02-05 stsp initscr();
3562 a915003a 2019-02-05 stsp cbreak();
3563 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3564 a915003a 2019-02-05 stsp noecho();
3565 a915003a 2019-02-05 stsp nonl();
3566 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3567 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3568 a915003a 2019-02-05 stsp curs_set(0);
3569 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3570 6d17833f 2019-11-08 stsp start_color();
3571 6d17833f 2019-11-08 stsp use_default_colors();
3572 6d17833f 2019-11-08 stsp }
3573 a915003a 2019-02-05 stsp }
3574 a915003a 2019-02-05 stsp
3575 c2db6724 2019-01-04 stsp static const struct got_error *
3576 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3577 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3578 f135c941 2020-02-20 stsp {
3579 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3580 f135c941 2020-02-20 stsp
3581 f135c941 2020-02-20 stsp if (argc == 0) {
3582 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3583 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3584 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3585 f135c941 2020-02-20 stsp return NULL;
3586 f135c941 2020-02-20 stsp }
3587 f135c941 2020-02-20 stsp
3588 f135c941 2020-02-20 stsp if (worktree) {
3589 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3590 bfd61697 2020-11-14 stsp char *p;
3591 f135c941 2020-02-20 stsp
3592 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3593 f135c941 2020-02-20 stsp if (err)
3594 f135c941 2020-02-20 stsp return err;
3595 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3596 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3597 bfd61697 2020-11-14 stsp p) == -1) {
3598 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3599 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3600 f135c941 2020-02-20 stsp }
3601 f135c941 2020-02-20 stsp free(p);
3602 f135c941 2020-02-20 stsp } else
3603 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3604 f135c941 2020-02-20 stsp
3605 f135c941 2020-02-20 stsp return err;
3606 f135c941 2020-02-20 stsp }
3607 f135c941 2020-02-20 stsp
3608 f135c941 2020-02-20 stsp static const struct got_error *
3609 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3610 9f7d7167 2018-04-29 stsp {
3611 80ddbec8 2018-04-29 stsp const struct got_error *error;
3612 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3613 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3614 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3615 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3616 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3617 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3618 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3619 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3620 04cc582a 2018-08-01 stsp struct tog_view *view;
3621 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3622 80ddbec8 2018-04-29 stsp
3623 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3624 80ddbec8 2018-04-29 stsp switch (ch) {
3625 b672a97a 2020-01-27 stsp case 'b':
3626 b672a97a 2020-01-27 stsp log_branches = 1;
3627 b672a97a 2020-01-27 stsp break;
3628 80ddbec8 2018-04-29 stsp case 'c':
3629 80ddbec8 2018-04-29 stsp start_commit = optarg;
3630 80ddbec8 2018-04-29 stsp break;
3631 ecb28ae0 2018-07-16 stsp case 'r':
3632 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3633 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3634 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3635 9ba1d308 2019-10-21 stsp optarg);
3636 ecb28ae0 2018-07-16 stsp break;
3637 80ddbec8 2018-04-29 stsp default:
3638 17020d27 2019-03-07 stsp usage_log();
3639 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3640 80ddbec8 2018-04-29 stsp }
3641 80ddbec8 2018-04-29 stsp }
3642 80ddbec8 2018-04-29 stsp
3643 80ddbec8 2018-04-29 stsp argc -= optind;
3644 80ddbec8 2018-04-29 stsp argv += optind;
3645 80ddbec8 2018-04-29 stsp
3646 f135c941 2020-02-20 stsp if (argc > 1)
3647 f135c941 2020-02-20 stsp usage_log();
3648 963f97a1 2019-03-18 stsp
3649 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3650 7cd52833 2022-06-23 thomas if (error != NULL)
3651 7cd52833 2022-06-23 thomas goto done;
3652 7cd52833 2022-06-23 thomas
3653 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3654 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3655 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3656 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3657 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3658 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3659 c156c7a4 2020-12-18 stsp goto done;
3660 a1fbf39a 2019-08-11 stsp if (worktree)
3661 6962eb72 2020-02-20 stsp repo_path =
3662 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3663 a1fbf39a 2019-08-11 stsp else
3664 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3665 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3666 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3667 c156c7a4 2020-12-18 stsp goto done;
3668 c156c7a4 2020-12-18 stsp }
3669 ecb28ae0 2018-07-16 stsp }
3670 ecb28ae0 2018-07-16 stsp
3671 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3672 80ddbec8 2018-04-29 stsp if (error != NULL)
3673 ecb28ae0 2018-07-16 stsp goto done;
3674 80ddbec8 2018-04-29 stsp
3675 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3676 f135c941 2020-02-20 stsp repo, worktree);
3677 f135c941 2020-02-20 stsp if (error)
3678 f135c941 2020-02-20 stsp goto done;
3679 f135c941 2020-02-20 stsp
3680 f135c941 2020-02-20 stsp init_curses();
3681 f135c941 2020-02-20 stsp
3682 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3683 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3684 c02c541e 2019-03-29 stsp if (error)
3685 c02c541e 2019-03-29 stsp goto done;
3686 c02c541e 2019-03-29 stsp
3687 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3688 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3689 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3690 87670572 2020-12-26 naddy if (error)
3691 87670572 2020-12-26 naddy goto done;
3692 87670572 2020-12-26 naddy }
3693 51a10b52 2020-12-26 stsp
3694 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3695 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3696 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3697 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3698 d8f38dc4 2020-12-05 stsp if (error)
3699 d8f38dc4 2020-12-05 stsp goto done;
3700 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3701 d8f38dc4 2020-12-05 stsp } else {
3702 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3703 d8f38dc4 2020-12-05 stsp if (error == NULL)
3704 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3705 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3706 d8f38dc4 2020-12-05 stsp goto done;
3707 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3708 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3709 d8f38dc4 2020-12-05 stsp if (error)
3710 d8f38dc4 2020-12-05 stsp goto done;
3711 d8f38dc4 2020-12-05 stsp }
3712 8b473291 2019-02-21 stsp
3713 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3714 04cc582a 2018-08-01 stsp if (view == NULL) {
3715 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3716 04cc582a 2018-08-01 stsp goto done;
3717 04cc582a 2018-08-01 stsp }
3718 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3719 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3720 ba4f502b 2018-08-04 stsp if (error)
3721 ba4f502b 2018-08-04 stsp goto done;
3722 2fc00ff4 2019-08-31 stsp if (worktree) {
3723 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3724 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3725 2fc00ff4 2019-08-31 stsp worktree = NULL;
3726 2fc00ff4 2019-08-31 stsp }
3727 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3728 ecb28ae0 2018-07-16 stsp done:
3729 f135c941 2020-02-20 stsp free(in_repo_path);
3730 ecb28ae0 2018-07-16 stsp free(repo_path);
3731 ecb28ae0 2018-07-16 stsp free(cwd);
3732 899d86c2 2018-05-10 stsp free(start_id);
3733 d8f38dc4 2020-12-05 stsp free(label);
3734 d8f38dc4 2020-12-05 stsp if (ref)
3735 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3736 1d0f4054 2021-06-17 stsp if (repo) {
3737 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3738 1d0f4054 2021-06-17 stsp if (error == NULL)
3739 1d0f4054 2021-06-17 stsp error = close_err;
3740 1d0f4054 2021-06-17 stsp }
3741 ec142235 2019-03-07 stsp if (worktree)
3742 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3743 7cd52833 2022-06-23 thomas if (pack_fds) {
3744 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3745 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3746 7cd52833 2022-06-23 thomas if (error == NULL)
3747 7cd52833 2022-06-23 thomas error = pack_err;
3748 7cd52833 2022-06-23 thomas }
3749 51a10b52 2020-12-26 stsp tog_free_refs();
3750 80ddbec8 2018-04-29 stsp return error;
3751 9f7d7167 2018-04-29 stsp }
3752 9f7d7167 2018-04-29 stsp
3753 4ed7e80c 2018-05-20 stsp __dead static void
3754 9f7d7167 2018-04-29 stsp usage_diff(void)
3755 9f7d7167 2018-04-29 stsp {
3756 80ddbec8 2018-04-29 stsp endwin();
3757 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3758 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3759 9f7d7167 2018-04-29 stsp exit(1);
3760 b304db33 2018-05-20 stsp }
3761 b304db33 2018-05-20 stsp
3762 6d17833f 2019-11-08 stsp static int
3763 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3764 41605754 2020-11-12 stsp regmatch_t *regmatch)
3765 6d17833f 2019-11-08 stsp {
3766 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3767 6d17833f 2019-11-08 stsp }
3768 6d17833f 2019-11-08 stsp
3769 ef20f542 2022-06-26 thomas static struct tog_color *
3770 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3771 6d17833f 2019-11-08 stsp {
3772 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3773 6d17833f 2019-11-08 stsp
3774 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3775 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3776 6d17833f 2019-11-08 stsp return tc;
3777 6d17833f 2019-11-08 stsp }
3778 6d17833f 2019-11-08 stsp
3779 6d17833f 2019-11-08 stsp return NULL;
3780 6d17833f 2019-11-08 stsp }
3781 6d17833f 2019-11-08 stsp
3782 4ed7e80c 2018-05-20 stsp static const struct got_error *
3783 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3784 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3785 41605754 2020-11-12 stsp {
3786 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3787 666f7b10 2022-06-23 thomas char *exstr = NULL;
3788 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3789 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3790 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3791 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3792 41605754 2020-11-12 stsp
3793 41605754 2020-11-12 stsp *wtotal = 0;
3794 cbea3800 2022-06-23 thomas
3795 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3796 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3797 41605754 2020-11-12 stsp
3798 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3799 666f7b10 2022-06-23 thomas if (err)
3800 666f7b10 2022-06-23 thomas return err;
3801 666f7b10 2022-06-23 thomas
3802 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3803 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3804 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3805 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3806 666f7b10 2022-06-23 thomas goto done;
3807 666f7b10 2022-06-23 thomas }
3808 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3809 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3810 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3811 cbea3800 2022-06-23 thomas goto done;
3812 cbea3800 2022-06-23 thomas }
3813 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3814 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3815 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3816 cbea3800 2022-06-23 thomas goto done;
3817 cbea3800 2022-06-23 thomas }
3818 05171be4 2022-06-23 thomas
3819 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3820 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, 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 n = MAX(width0 - skipcol, 0);
3825 05171be4 2022-06-23 thomas if (n) {
3826 cbea3800 2022-06-23 thomas free(wline);
3827 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3828 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3829 cbea3800 2022-06-23 thomas if (err)
3830 cbea3800 2022-06-23 thomas goto done;
3831 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3832 cbea3800 2022-06-23 thomas wlimit -= width;
3833 cbea3800 2022-06-23 thomas *wtotal += width;
3834 41605754 2020-11-12 stsp }
3835 41605754 2020-11-12 stsp
3836 41605754 2020-11-12 stsp if (wlimit > 0) {
3837 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3838 cbea3800 2022-06-23 thomas size_t wlen;
3839 cbea3800 2022-06-23 thomas
3840 cbea3800 2022-06-23 thomas free(wline);
3841 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3842 cbea3800 2022-06-23 thomas col_tab_align, 1);
3843 cbea3800 2022-06-23 thomas if (err)
3844 cbea3800 2022-06-23 thomas goto done;
3845 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3846 cbea3800 2022-06-23 thomas while (i < wlen) {
3847 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3848 cbea3800 2022-06-23 thomas if (width == -1) {
3849 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3850 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3851 cbea3800 2022-06-23 thomas goto done;
3852 cbea3800 2022-06-23 thomas }
3853 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3854 cbea3800 2022-06-23 thomas break;
3855 1c5e5faa 2022-06-23 thomas w += width;
3856 cbea3800 2022-06-23 thomas i++;
3857 41605754 2020-11-12 stsp }
3858 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3859 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3860 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3861 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3862 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3863 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3864 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3865 41605754 2020-11-12 stsp }
3866 41605754 2020-11-12 stsp }
3867 41605754 2020-11-12 stsp
3868 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3869 cbea3800 2022-06-23 thomas free(wline);
3870 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3871 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3872 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3873 cbea3800 2022-06-23 thomas col_tab_align, 1);
3874 cbea3800 2022-06-23 thomas if (err)
3875 cbea3800 2022-06-23 thomas goto done;
3876 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3877 cbea3800 2022-06-23 thomas } else {
3878 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3879 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3880 cbea3800 2022-06-23 thomas if (err)
3881 cbea3800 2022-06-23 thomas goto done;
3882 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3883 cbea3800 2022-06-23 thomas }
3884 cbea3800 2022-06-23 thomas *wtotal += width2;
3885 41605754 2020-11-12 stsp }
3886 cbea3800 2022-06-23 thomas done:
3887 05171be4 2022-06-23 thomas free(wline);
3888 666f7b10 2022-06-23 thomas free(exstr);
3889 cbea3800 2022-06-23 thomas free(seg0);
3890 cbea3800 2022-06-23 thomas free(seg1);
3891 cbea3800 2022-06-23 thomas free(seg2);
3892 cbea3800 2022-06-23 thomas return err;
3893 41605754 2020-11-12 stsp }
3894 07dd3ed3 2022-08-06 thomas
3895 07dd3ed3 2022-08-06 thomas static int
3896 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
3897 07dd3ed3 2022-08-06 thomas {
3898 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
3899 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
3900 07dd3ed3 2022-08-06 thomas
3901 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
3902 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
3903 41605754 2020-11-12 stsp
3904 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3905 07dd3ed3 2022-08-06 thomas selected = first;
3906 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3907 07dd3ed3 2022-08-06 thomas f = s->f;
3908 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
3909 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
3910 07dd3ed3 2022-08-06 thomas
3911 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
3912 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
3913 07dd3ed3 2022-08-06 thomas eof = &s->eof;
3914 07dd3ed3 2022-08-06 thomas f = s->blame.f;
3915 07dd3ed3 2022-08-06 thomas } else
3916 07dd3ed3 2022-08-06 thomas return 0;
3917 07dd3ed3 2022-08-06 thomas
3918 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
3919 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
3920 07dd3ed3 2022-08-06 thomas return 0;
3921 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3922 07dd3ed3 2022-08-06 thomas rewind(f);
3923 07dd3ed3 2022-08-06 thomas *eof = 0;
3924 07dd3ed3 2022-08-06 thomas *first = 1;
3925 07dd3ed3 2022-08-06 thomas *lineno = 0;
3926 07dd3ed3 2022-08-06 thomas *nprinted = 0;
3927 07dd3ed3 2022-08-06 thomas return 0;
3928 07dd3ed3 2022-08-06 thomas }
3929 07dd3ed3 2022-08-06 thomas
3930 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
3931 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
3932 07dd3ed3 2022-08-06 thomas view->gline = 0;
3933 07dd3ed3 2022-08-06 thomas
3934 07dd3ed3 2022-08-06 thomas return 1;
3935 07dd3ed3 2022-08-06 thomas }
3936 07dd3ed3 2022-08-06 thomas
3937 41605754 2020-11-12 stsp static const struct got_error *
3938 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3939 26ed57b2 2018-05-19 stsp {
3940 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3941 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3942 61e69b96 2018-05-20 stsp const struct got_error *err;
3943 82c78e96 2022-08-06 thomas int nprinted = 0;
3944 b304db33 2018-05-20 stsp char *line;
3945 826082fe 2020-12-10 stsp size_t linesize = 0;
3946 826082fe 2020-12-10 stsp ssize_t linelen;
3947 61e69b96 2018-05-20 stsp wchar_t *wline;
3948 e0b650dd 2018-05-20 stsp int width;
3949 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3950 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3951 fe621944 2020-11-10 stsp off_t line_offset;
3952 26ed57b2 2018-05-19 stsp
3953 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
3954 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
3955 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3956 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3957 fe621944 2020-11-10 stsp
3958 f7d12f7e 2018-08-01 stsp werase(view->window);
3959 a3404814 2018-09-02 stsp
3960 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
3961 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
3962 07dd3ed3 2022-08-06 thomas
3963 a3404814 2018-09-02 stsp if (header) {
3964 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3965 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
3966 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
3967 07dd3ed3 2022-08-06 thomas
3968 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3969 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3970 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3971 f91a2b48 2022-06-23 thomas 0, 0);
3972 135a2da0 2020-11-11 stsp free(line);
3973 135a2da0 2020-11-11 stsp if (err)
3974 a3404814 2018-09-02 stsp return err;
3975 a3404814 2018-09-02 stsp
3976 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3977 a3404814 2018-09-02 stsp wstandout(view->window);
3978 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3979 e54cc94a 2020-11-11 stsp free(wline);
3980 e54cc94a 2020-11-11 stsp wline = NULL;
3981 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3982 a3404814 2018-09-02 stsp wstandend(view->window);
3983 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3984 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3985 26ed57b2 2018-05-19 stsp
3986 a3404814 2018-09-02 stsp if (max_lines <= 1)
3987 a3404814 2018-09-02 stsp return NULL;
3988 a3404814 2018-09-02 stsp max_lines--;
3989 a3404814 2018-09-02 stsp }
3990 a3404814 2018-09-02 stsp
3991 89f1a395 2020-12-01 naddy s->eof = 0;
3992 05171be4 2022-06-23 thomas view->maxx = 0;
3993 826082fe 2020-12-10 stsp line = NULL;
3994 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3995 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
3996 6568f0aa 2022-08-06 thomas attr_t attr = 0;
3997 6568f0aa 2022-08-06 thomas
3998 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3999 826082fe 2020-12-10 stsp if (linelen == -1) {
4000 826082fe 2020-12-10 stsp if (feof(s->f)) {
4001 826082fe 2020-12-10 stsp s->eof = 1;
4002 826082fe 2020-12-10 stsp break;
4003 826082fe 2020-12-10 stsp }
4004 826082fe 2020-12-10 stsp free(line);
4005 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4006 61e69b96 2018-05-20 stsp }
4007 05171be4 2022-06-23 thomas
4008 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4009 07dd3ed3 2022-08-06 thomas continue;
4010 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4011 07dd3ed3 2022-08-06 thomas continue;
4012 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4013 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4014 07dd3ed3 2022-08-06 thomas
4015 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4016 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4017 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4018 cbea3800 2022-06-23 thomas if (err) {
4019 cbea3800 2022-06-23 thomas free(line);
4020 cbea3800 2022-06-23 thomas return err;
4021 cbea3800 2022-06-23 thomas }
4022 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4023 cbea3800 2022-06-23 thomas free(wline);
4024 cbea3800 2022-06-23 thomas wline = NULL;
4025 cbea3800 2022-06-23 thomas
4026 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4027 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4028 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4029 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4030 07dd3ed3 2022-08-06 thomas if (attr)
4031 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4032 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4033 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4034 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4035 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4036 41605754 2020-11-12 stsp if (err) {
4037 41605754 2020-11-12 stsp free(line);
4038 41605754 2020-11-12 stsp return err;
4039 41605754 2020-11-12 stsp }
4040 41605754 2020-11-12 stsp } else {
4041 f1c0ec19 2022-06-23 thomas int skip;
4042 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4043 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4044 f1c0ec19 2022-06-23 thomas if (err) {
4045 f1c0ec19 2022-06-23 thomas free(line);
4046 f1c0ec19 2022-06-23 thomas return err;
4047 f1c0ec19 2022-06-23 thomas }
4048 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4049 f1c0ec19 2022-06-23 thomas free(wline);
4050 41605754 2020-11-12 stsp wline = NULL;
4051 41605754 2020-11-12 stsp }
4052 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4053 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4054 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4055 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4056 07dd3ed3 2022-08-06 thomas } else {
4057 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4058 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4059 07dd3ed3 2022-08-06 thomas }
4060 07dd3ed3 2022-08-06 thomas if (attr)
4061 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4062 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4063 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4064 826082fe 2020-12-10 stsp }
4065 826082fe 2020-12-10 stsp free(line);
4066 fe621944 2020-11-10 stsp if (nprinted >= 1)
4067 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4068 89f1a395 2020-12-01 naddy (nprinted - 1);
4069 fe621944 2020-11-10 stsp else
4070 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4071 26ed57b2 2018-05-19 stsp
4072 a5d43cac 2022-07-01 thomas view_border(view);
4073 c3e9aa98 2019-05-13 jcs
4074 89f1a395 2020-12-01 naddy if (s->eof) {
4075 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4076 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4077 c3e9aa98 2019-05-13 jcs nprinted++;
4078 c3e9aa98 2019-05-13 jcs }
4079 c3e9aa98 2019-05-13 jcs
4080 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4081 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4082 c3e9aa98 2019-05-13 jcs if (err) {
4083 c3e9aa98 2019-05-13 jcs return err;
4084 c3e9aa98 2019-05-13 jcs }
4085 26ed57b2 2018-05-19 stsp
4086 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4087 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4088 e54cc94a 2020-11-11 stsp free(wline);
4089 e54cc94a 2020-11-11 stsp wline = NULL;
4090 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4091 c3e9aa98 2019-05-13 jcs }
4092 c3e9aa98 2019-05-13 jcs
4093 26ed57b2 2018-05-19 stsp return NULL;
4094 abd2672a 2018-12-23 stsp }
4095 abd2672a 2018-12-23 stsp
4096 abd2672a 2018-12-23 stsp static char *
4097 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4098 abd2672a 2018-12-23 stsp {
4099 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4100 09867e48 2019-08-13 stsp char *p, *s;
4101 09867e48 2019-08-13 stsp
4102 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4103 09867e48 2019-08-13 stsp if (tm == NULL)
4104 09867e48 2019-08-13 stsp return NULL;
4105 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4106 09867e48 2019-08-13 stsp if (s == NULL)
4107 09867e48 2019-08-13 stsp return NULL;
4108 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4109 abd2672a 2018-12-23 stsp if (p)
4110 abd2672a 2018-12-23 stsp *p = '\0';
4111 abd2672a 2018-12-23 stsp return s;
4112 9f7d7167 2018-04-29 stsp }
4113 9f7d7167 2018-04-29 stsp
4114 4ed7e80c 2018-05-20 stsp static const struct got_error *
4115 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4116 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4117 0208f208 2020-05-05 stsp {
4118 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4119 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4120 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4121 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4122 0208f208 2020-05-05 stsp
4123 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4124 0208f208 2020-05-05 stsp if (qid != NULL) {
4125 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4126 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4127 ec242592 2022-04-22 thomas &qid->id);
4128 0208f208 2020-05-05 stsp if (err)
4129 0208f208 2020-05-05 stsp return err;
4130 0208f208 2020-05-05 stsp
4131 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4132 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4133 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4134 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4135 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4136 aa8b5dd0 2021-08-01 stsp }
4137 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4138 0208f208 2020-05-05 stsp
4139 0208f208 2020-05-05 stsp }
4140 0208f208 2020-05-05 stsp
4141 0208f208 2020-05-05 stsp if (tree_id1) {
4142 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4143 0208f208 2020-05-05 stsp if (err)
4144 0208f208 2020-05-05 stsp goto done;
4145 0208f208 2020-05-05 stsp }
4146 0208f208 2020-05-05 stsp
4147 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4148 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4149 0208f208 2020-05-05 stsp if (err)
4150 0208f208 2020-05-05 stsp goto done;
4151 0208f208 2020-05-05 stsp
4152 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4153 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4154 0208f208 2020-05-05 stsp done:
4155 0208f208 2020-05-05 stsp if (tree1)
4156 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4157 0208f208 2020-05-05 stsp if (tree2)
4158 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4159 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4160 0208f208 2020-05-05 stsp return err;
4161 0208f208 2020-05-05 stsp }
4162 0208f208 2020-05-05 stsp
4163 0208f208 2020-05-05 stsp static const struct got_error *
4164 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4165 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4166 abd2672a 2018-12-23 stsp {
4167 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4168 fe621944 2020-11-10 stsp
4169 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4170 fe621944 2020-11-10 stsp if (p == NULL)
4171 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4172 82c78e96 2022-08-06 thomas *lines = p;
4173 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4174 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4175 fe621944 2020-11-10 stsp (*nlines)++;
4176 82c78e96 2022-08-06 thomas
4177 fe621944 2020-11-10 stsp return NULL;
4178 fe621944 2020-11-10 stsp }
4179 fe621944 2020-11-10 stsp
4180 fe621944 2020-11-10 stsp static const struct got_error *
4181 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4182 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4183 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4184 fe621944 2020-11-10 stsp {
4185 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4186 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4187 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4188 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4189 45d799e2 2018-12-23 stsp time_t committer_time;
4190 45d799e2 2018-12-23 stsp const char *author, *committer;
4191 8b473291 2019-02-21 stsp char *refs_str = NULL;
4192 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4193 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4194 fe621944 2020-11-10 stsp off_t outoff = 0;
4195 fe621944 2020-11-10 stsp int n;
4196 abd2672a 2018-12-23 stsp
4197 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4198 0208f208 2020-05-05 stsp
4199 8b473291 2019-02-21 stsp if (refs) {
4200 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4201 8b473291 2019-02-21 stsp if (err)
4202 8b473291 2019-02-21 stsp return err;
4203 8b473291 2019-02-21 stsp }
4204 8b473291 2019-02-21 stsp
4205 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4206 abd2672a 2018-12-23 stsp if (err)
4207 abd2672a 2018-12-23 stsp return err;
4208 abd2672a 2018-12-23 stsp
4209 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4210 15a94983 2018-12-23 stsp if (err) {
4211 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4212 15a94983 2018-12-23 stsp goto done;
4213 15a94983 2018-12-23 stsp }
4214 abd2672a 2018-12-23 stsp
4215 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4216 fe621944 2020-11-10 stsp if (err)
4217 fe621944 2020-11-10 stsp goto done;
4218 fe621944 2020-11-10 stsp
4219 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4220 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4221 fe621944 2020-11-10 stsp if (n < 0) {
4222 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4223 abd2672a 2018-12-23 stsp goto done;
4224 abd2672a 2018-12-23 stsp }
4225 fe621944 2020-11-10 stsp outoff += n;
4226 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4227 fe621944 2020-11-10 stsp if (err)
4228 fe621944 2020-11-10 stsp goto done;
4229 fe621944 2020-11-10 stsp
4230 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4231 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4232 fe621944 2020-11-10 stsp if (n < 0) {
4233 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4234 abd2672a 2018-12-23 stsp goto done;
4235 abd2672a 2018-12-23 stsp }
4236 fe621944 2020-11-10 stsp outoff += n;
4237 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4238 fe621944 2020-11-10 stsp if (err)
4239 fe621944 2020-11-10 stsp goto done;
4240 fe621944 2020-11-10 stsp
4241 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4242 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4243 fe621944 2020-11-10 stsp if (datestr) {
4244 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4245 fe621944 2020-11-10 stsp if (n < 0) {
4246 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4247 fe621944 2020-11-10 stsp goto done;
4248 fe621944 2020-11-10 stsp }
4249 fe621944 2020-11-10 stsp outoff += n;
4250 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4251 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4252 fe621944 2020-11-10 stsp if (err)
4253 fe621944 2020-11-10 stsp goto done;
4254 abd2672a 2018-12-23 stsp }
4255 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4256 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4257 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4258 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4259 fe621944 2020-11-10 stsp if (n < 0) {
4260 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4261 fe621944 2020-11-10 stsp goto done;
4262 fe621944 2020-11-10 stsp }
4263 fe621944 2020-11-10 stsp outoff += n;
4264 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4265 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_AUTHOR);
4266 fe621944 2020-11-10 stsp if (err)
4267 fe621944 2020-11-10 stsp goto done;
4268 abd2672a 2018-12-23 stsp }
4269 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4270 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4271 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4272 ac4dc263 2021-09-24 thomas int pn = 1;
4273 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4274 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4275 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4276 ac4dc263 2021-09-24 thomas if (err)
4277 ac4dc263 2021-09-24 thomas goto done;
4278 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4279 ac4dc263 2021-09-24 thomas if (n < 0) {
4280 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4281 ac4dc263 2021-09-24 thomas goto done;
4282 ac4dc263 2021-09-24 thomas }
4283 ac4dc263 2021-09-24 thomas outoff += n;
4284 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4285 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4286 ac4dc263 2021-09-24 thomas if (err)
4287 ac4dc263 2021-09-24 thomas goto done;
4288 ac4dc263 2021-09-24 thomas free(id_str);
4289 ac4dc263 2021-09-24 thomas id_str = NULL;
4290 ac4dc263 2021-09-24 thomas }
4291 ac4dc263 2021-09-24 thomas }
4292 ac4dc263 2021-09-24 thomas
4293 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4294 5943eee2 2019-08-13 stsp if (err)
4295 5943eee2 2019-08-13 stsp goto done;
4296 fe621944 2020-11-10 stsp s = logmsg;
4297 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4298 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4299 fe621944 2020-11-10 stsp if (n < 0) {
4300 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4301 fe621944 2020-11-10 stsp goto done;
4302 fe621944 2020-11-10 stsp }
4303 fe621944 2020-11-10 stsp outoff += n;
4304 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4305 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4306 fe621944 2020-11-10 stsp if (err)
4307 fe621944 2020-11-10 stsp goto done;
4308 abd2672a 2018-12-23 stsp }
4309 fe621944 2020-11-10 stsp
4310 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4311 0208f208 2020-05-05 stsp if (err)
4312 0208f208 2020-05-05 stsp goto done;
4313 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4314 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4315 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4316 fe621944 2020-11-10 stsp if (n < 0) {
4317 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4318 fe621944 2020-11-10 stsp goto done;
4319 fe621944 2020-11-10 stsp }
4320 fe621944 2020-11-10 stsp outoff += n;
4321 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4322 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4323 fe621944 2020-11-10 stsp if (err)
4324 fe621944 2020-11-10 stsp goto done;
4325 0208f208 2020-05-05 stsp free((char *)pe->path);
4326 0208f208 2020-05-05 stsp free(pe->data);
4327 0208f208 2020-05-05 stsp }
4328 fe621944 2020-11-10 stsp
4329 0208f208 2020-05-05 stsp fputc('\n', outfile);
4330 fe621944 2020-11-10 stsp outoff++;
4331 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4332 abd2672a 2018-12-23 stsp done:
4333 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4334 abd2672a 2018-12-23 stsp free(id_str);
4335 5943eee2 2019-08-13 stsp free(logmsg);
4336 8b473291 2019-02-21 stsp free(refs_str);
4337 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4338 7510f233 2020-08-09 stsp if (err) {
4339 82c78e96 2022-08-06 thomas free(*lines);
4340 82c78e96 2022-08-06 thomas *lines = NULL;
4341 ae6a6978 2020-08-09 stsp *nlines = 0;
4342 7510f233 2020-08-09 stsp }
4343 fe621944 2020-11-10 stsp return err;
4344 abd2672a 2018-12-23 stsp }
4345 abd2672a 2018-12-23 stsp
4346 abd2672a 2018-12-23 stsp static const struct got_error *
4347 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4348 26ed57b2 2018-05-19 stsp {
4349 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4350 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4351 15a94983 2018-12-23 stsp int obj_type;
4352 fe621944 2020-11-10 stsp
4353 82c78e96 2022-08-06 thomas free(s->lines);
4354 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4355 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4356 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4357 fe621944 2020-11-10 stsp s->nlines = 0;
4358 26ed57b2 2018-05-19 stsp
4359 511a516b 2018-05-19 stsp f = got_opentemp();
4360 48ae06ee 2018-10-18 stsp if (f == NULL) {
4361 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4362 48ae06ee 2018-10-18 stsp goto done;
4363 48ae06ee 2018-10-18 stsp }
4364 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4365 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4366 fb43ecf1 2019-02-11 stsp goto done;
4367 fb43ecf1 2019-02-11 stsp }
4368 48ae06ee 2018-10-18 stsp s->f = f;
4369 26ed57b2 2018-05-19 stsp
4370 15a94983 2018-12-23 stsp if (s->id1)
4371 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4372 15a94983 2018-12-23 stsp else
4373 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4374 15a94983 2018-12-23 stsp if (err)
4375 15a94983 2018-12-23 stsp goto done;
4376 15a94983 2018-12-23 stsp
4377 15a94983 2018-12-23 stsp switch (obj_type) {
4378 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4379 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4380 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4381 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4382 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4383 26ed57b2 2018-05-19 stsp break;
4384 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4385 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4386 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4387 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4388 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4389 26ed57b2 2018-05-19 stsp break;
4390 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4391 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4392 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4393 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4394 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4395 abd2672a 2018-12-23 stsp
4396 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4397 abd2672a 2018-12-23 stsp if (err)
4398 3ffacbe1 2020-02-02 tracey goto done;
4399 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4400 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4401 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4402 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4403 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4404 f44b1f58 2020-02-02 tracey if (err)
4405 f44b1f58 2020-02-02 tracey goto done;
4406 f44b1f58 2020-02-02 tracey } else {
4407 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4408 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4409 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4410 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4411 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4412 82c78e96 2022-08-06 thomas s->f);
4413 f44b1f58 2020-02-02 tracey if (err)
4414 f44b1f58 2020-02-02 tracey goto done;
4415 f5404e4e 2020-02-02 tracey break;
4416 15a087fe 2019-02-21 stsp }
4417 abd2672a 2018-12-23 stsp }
4418 abd2672a 2018-12-23 stsp }
4419 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4420 abd2672a 2018-12-23 stsp
4421 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4422 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4423 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4424 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4425 26ed57b2 2018-05-19 stsp break;
4426 abd2672a 2018-12-23 stsp }
4427 26ed57b2 2018-05-19 stsp default:
4428 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4429 48ae06ee 2018-10-18 stsp break;
4430 26ed57b2 2018-05-19 stsp }
4431 48ae06ee 2018-10-18 stsp done:
4432 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4433 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4434 48ae06ee 2018-10-18 stsp return err;
4435 48ae06ee 2018-10-18 stsp }
4436 26ed57b2 2018-05-19 stsp
4437 f5215bb9 2019-02-22 stsp static void
4438 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4439 f5215bb9 2019-02-22 stsp {
4440 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4441 f5215bb9 2019-02-22 stsp update_panels();
4442 f5215bb9 2019-02-22 stsp doupdate();
4443 f44b1f58 2020-02-02 tracey }
4444 f44b1f58 2020-02-02 tracey
4445 f44b1f58 2020-02-02 tracey static const struct got_error *
4446 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4447 f44b1f58 2020-02-02 tracey {
4448 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4449 f44b1f58 2020-02-02 tracey
4450 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4451 f44b1f58 2020-02-02 tracey return NULL;
4452 f44b1f58 2020-02-02 tracey }
4453 f44b1f58 2020-02-02 tracey
4454 f44b1f58 2020-02-02 tracey static const struct got_error *
4455 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4456 f44b1f58 2020-02-02 tracey {
4457 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4458 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4459 f44b1f58 2020-02-02 tracey int lineno;
4460 78eecffc 2022-06-23 thomas char *line = NULL;
4461 826082fe 2020-12-10 stsp size_t linesize = 0;
4462 826082fe 2020-12-10 stsp ssize_t linelen;
4463 f44b1f58 2020-02-02 tracey
4464 f44b1f58 2020-02-02 tracey if (!view->searching) {
4465 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4466 f44b1f58 2020-02-02 tracey return NULL;
4467 f44b1f58 2020-02-02 tracey }
4468 f44b1f58 2020-02-02 tracey
4469 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4470 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4471 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4472 f44b1f58 2020-02-02 tracey else
4473 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4474 4b3f9dac 2021-12-17 thomas } else
4475 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4476 f44b1f58 2020-02-02 tracey
4477 f44b1f58 2020-02-02 tracey while (1) {
4478 f44b1f58 2020-02-02 tracey off_t offset;
4479 f44b1f58 2020-02-02 tracey
4480 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4481 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4482 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4483 f44b1f58 2020-02-02 tracey break;
4484 f44b1f58 2020-02-02 tracey }
4485 f44b1f58 2020-02-02 tracey
4486 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4487 f44b1f58 2020-02-02 tracey lineno = 1;
4488 f44b1f58 2020-02-02 tracey else
4489 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4490 f44b1f58 2020-02-02 tracey }
4491 f44b1f58 2020-02-02 tracey
4492 82c78e96 2022-08-06 thomas offset = s->lines[lineno - 1].offset;
4493 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4494 f44b1f58 2020-02-02 tracey free(line);
4495 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4496 f44b1f58 2020-02-02 tracey }
4497 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4498 78eecffc 2022-06-23 thomas if (linelen != -1) {
4499 78eecffc 2022-06-23 thomas char *exstr;
4500 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4501 78eecffc 2022-06-23 thomas if (err)
4502 78eecffc 2022-06-23 thomas break;
4503 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4504 78eecffc 2022-06-23 thomas &view->regmatch)) {
4505 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4506 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4507 78eecffc 2022-06-23 thomas free(exstr);
4508 78eecffc 2022-06-23 thomas break;
4509 78eecffc 2022-06-23 thomas }
4510 78eecffc 2022-06-23 thomas free(exstr);
4511 f44b1f58 2020-02-02 tracey }
4512 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4513 f44b1f58 2020-02-02 tracey lineno++;
4514 f44b1f58 2020-02-02 tracey else
4515 f44b1f58 2020-02-02 tracey lineno--;
4516 f44b1f58 2020-02-02 tracey }
4517 826082fe 2020-12-10 stsp free(line);
4518 f44b1f58 2020-02-02 tracey
4519 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4520 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4521 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4522 f44b1f58 2020-02-02 tracey }
4523 f44b1f58 2020-02-02 tracey
4524 05171be4 2022-06-23 thomas return err;
4525 f5215bb9 2019-02-22 stsp }
4526 f5215bb9 2019-02-22 stsp
4527 48ae06ee 2018-10-18 stsp static const struct got_error *
4528 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4529 a0f32f33 2022-06-13 thomas {
4530 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4531 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4532 a0f32f33 2022-06-13 thomas
4533 a0f32f33 2022-06-13 thomas free(s->id1);
4534 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4535 a0f32f33 2022-06-13 thomas free(s->id2);
4536 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4537 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4538 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4539 a0f32f33 2022-06-13 thomas s->f = NULL;
4540 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4541 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4542 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4543 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4544 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4545 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4546 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4547 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4548 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4549 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4550 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4551 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4552 82c78e96 2022-08-06 thomas free(s->lines);
4553 82c78e96 2022-08-06 thomas s->lines = NULL;
4554 a0f32f33 2022-06-13 thomas s->nlines = 0;
4555 a0f32f33 2022-06-13 thomas return err;
4556 a0f32f33 2022-06-13 thomas }
4557 a0f32f33 2022-06-13 thomas
4558 a0f32f33 2022-06-13 thomas static const struct got_error *
4559 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4560 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4561 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4562 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4563 48ae06ee 2018-10-18 stsp {
4564 48ae06ee 2018-10-18 stsp const struct got_error *err;
4565 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4566 5dc9f4bc 2018-08-04 stsp
4567 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4568 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4569 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4570 a0f32f33 2022-06-13 thomas
4571 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4572 15a94983 2018-12-23 stsp int type1, type2;
4573 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4574 15a94983 2018-12-23 stsp if (err)
4575 15a94983 2018-12-23 stsp return err;
4576 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4577 15a94983 2018-12-23 stsp if (err)
4578 15a94983 2018-12-23 stsp return err;
4579 15a94983 2018-12-23 stsp
4580 15a94983 2018-12-23 stsp if (type1 != type2)
4581 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4582 15a94983 2018-12-23 stsp }
4583 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4584 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4585 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4586 f44b1f58 2020-02-02 tracey s->repo = repo;
4587 f44b1f58 2020-02-02 tracey s->id1 = id1;
4588 f44b1f58 2020-02-02 tracey s->id2 = id2;
4589 3dbaef42 2020-11-24 stsp s->label1 = label1;
4590 3dbaef42 2020-11-24 stsp s->label2 = label2;
4591 48ae06ee 2018-10-18 stsp
4592 15a94983 2018-12-23 stsp if (id1) {
4593 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4594 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4595 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4596 48ae06ee 2018-10-18 stsp } else
4597 5465d566 2020-02-01 tracey s->id1 = NULL;
4598 48ae06ee 2018-10-18 stsp
4599 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4600 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4601 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4602 a0f32f33 2022-06-13 thomas goto done;
4603 48ae06ee 2018-10-18 stsp }
4604 a0f32f33 2022-06-13 thomas
4605 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4606 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4607 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4608 9a1d7435 2022-06-23 thomas goto done;
4609 9a1d7435 2022-06-23 thomas }
4610 9a1d7435 2022-06-23 thomas
4611 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4612 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4613 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4614 a0f32f33 2022-06-13 thomas goto done;
4615 a0f32f33 2022-06-13 thomas }
4616 a0f32f33 2022-06-13 thomas
4617 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4618 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4619 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4620 19a6a6b5 2022-07-01 thomas goto done;
4621 19a6a6b5 2022-07-01 thomas }
4622 19a6a6b5 2022-07-01 thomas
4623 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4624 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4625 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4626 19a6a6b5 2022-07-01 thomas goto done;
4627 19a6a6b5 2022-07-01 thomas }
4628 19a6a6b5 2022-07-01 thomas
4629 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4630 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4631 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4632 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4633 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4634 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4635 5465d566 2020-02-01 tracey s->repo = repo;
4636 6d17833f 2019-11-08 stsp
4637 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4638 6568f0aa 2022-08-06 thomas int rc;
4639 6d17833f 2019-11-08 stsp
4640 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
4641 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4642 6568f0aa 2022-08-06 thomas if (rc != ERR)
4643 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
4644 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4645 6568f0aa 2022-08-06 thomas if (rc != ERR)
4646 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
4647 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4648 6568f0aa 2022-08-06 thomas if (rc != ERR)
4649 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
4650 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4651 6568f0aa 2022-08-06 thomas if (rc != ERR)
4652 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
4653 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4654 6568f0aa 2022-08-06 thomas if (rc != ERR)
4655 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4656 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4657 6568f0aa 2022-08-06 thomas if (rc != ERR)
4658 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4659 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4660 6568f0aa 2022-08-06 thomas if (rc != ERR)
4661 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4662 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
4663 6568f0aa 2022-08-06 thomas if (rc != ERR)
4664 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
4665 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
4666 6568f0aa 2022-08-06 thomas if (rc == ERR) {
4667 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
4668 a0f32f33 2022-06-13 thomas goto done;
4669 6568f0aa 2022-08-06 thomas }
4670 6d17833f 2019-11-08 stsp }
4671 5dc9f4bc 2018-08-04 stsp
4672 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4673 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4674 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4675 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4676 f5215bb9 2019-02-22 stsp
4677 5465d566 2020-02-01 tracey err = create_diff(s);
4678 48ae06ee 2018-10-18 stsp
4679 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4680 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4681 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4682 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4683 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4684 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4685 a0f32f33 2022-06-13 thomas done:
4686 a0f32f33 2022-06-13 thomas if (err)
4687 a0f32f33 2022-06-13 thomas close_diff_view(view);
4688 e5a0f69f 2018-08-18 stsp return err;
4689 5dc9f4bc 2018-08-04 stsp }
4690 5dc9f4bc 2018-08-04 stsp
4691 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4692 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4693 5dc9f4bc 2018-08-04 stsp {
4694 a3404814 2018-09-02 stsp const struct got_error *err;
4695 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4696 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4697 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4698 a3404814 2018-09-02 stsp
4699 a3404814 2018-09-02 stsp if (s->id1) {
4700 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4701 a3404814 2018-09-02 stsp if (err)
4702 a3404814 2018-09-02 stsp return err;
4703 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4704 3dbaef42 2020-11-24 stsp } else
4705 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4706 3dbaef42 2020-11-24 stsp
4707 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4708 a3404814 2018-09-02 stsp if (err)
4709 a3404814 2018-09-02 stsp return err;
4710 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4711 26ed57b2 2018-05-19 stsp
4712 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4713 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4714 a3404814 2018-09-02 stsp free(id_str1);
4715 a3404814 2018-09-02 stsp free(id_str2);
4716 a3404814 2018-09-02 stsp return err;
4717 a3404814 2018-09-02 stsp }
4718 a3404814 2018-09-02 stsp free(id_str1);
4719 a3404814 2018-09-02 stsp free(id_str2);
4720 a3404814 2018-09-02 stsp
4721 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4722 267bb3b8 2021-08-01 stsp free(header);
4723 267bb3b8 2021-08-01 stsp return err;
4724 15a087fe 2019-02-21 stsp }
4725 15a087fe 2019-02-21 stsp
4726 15a087fe 2019-02-21 stsp static const struct got_error *
4727 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4728 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4729 15a087fe 2019-02-21 stsp {
4730 d7a04538 2019-02-21 stsp const struct got_error *err;
4731 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4732 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4733 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4734 15a087fe 2019-02-21 stsp
4735 15a087fe 2019-02-21 stsp free(s->id2);
4736 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4737 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4738 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4739 15a087fe 2019-02-21 stsp
4740 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4741 d7a04538 2019-02-21 stsp if (err)
4742 d7a04538 2019-02-21 stsp return err;
4743 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4744 15a087fe 2019-02-21 stsp free(s->id1);
4745 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4746 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4747 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4748 15a087fe 2019-02-21 stsp return NULL;
4749 0cf4efb1 2018-09-29 stsp }
4750 0cf4efb1 2018-09-29 stsp
4751 0cf4efb1 2018-09-29 stsp static const struct got_error *
4752 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4753 adf4c9e0 2022-07-03 thomas {
4754 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4755 adf4c9e0 2022-07-03 thomas
4756 adf4c9e0 2022-07-03 thomas view->count = 0;
4757 adf4c9e0 2022-07-03 thomas wclear(view->window);
4758 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4759 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4760 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4761 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4762 adf4c9e0 2022-07-03 thomas return create_diff(s);
4763 82c78e96 2022-08-06 thomas }
4764 82c78e96 2022-08-06 thomas
4765 82c78e96 2022-08-06 thomas static void
4766 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4767 82c78e96 2022-08-06 thomas {
4768 82c78e96 2022-08-06 thomas int start, i;
4769 82c78e96 2022-08-06 thomas
4770 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
4771 82c78e96 2022-08-06 thomas
4772 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4773 82c78e96 2022-08-06 thomas if (i == 0)
4774 82c78e96 2022-08-06 thomas i = s->nlines - 1;
4775 82c78e96 2022-08-06 thomas if (--i == start)
4776 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4777 82c78e96 2022-08-06 thomas }
4778 82c78e96 2022-08-06 thomas
4779 82c78e96 2022-08-06 thomas s->selected_line = 1;
4780 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4781 adf4c9e0 2022-07-03 thomas }
4782 adf4c9e0 2022-07-03 thomas
4783 82c78e96 2022-08-06 thomas static void
4784 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4785 82c78e96 2022-08-06 thomas {
4786 82c78e96 2022-08-06 thomas int start, i;
4787 82c78e96 2022-08-06 thomas
4788 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
4789 82c78e96 2022-08-06 thomas
4790 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4791 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
4792 82c78e96 2022-08-06 thomas i = 0;
4793 82c78e96 2022-08-06 thomas if (++i == start)
4794 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4795 82c78e96 2022-08-06 thomas }
4796 82c78e96 2022-08-06 thomas
4797 82c78e96 2022-08-06 thomas s->selected_line = 1;
4798 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4799 82c78e96 2022-08-06 thomas }
4800 82c78e96 2022-08-06 thomas
4801 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4802 4fc71f3b 2022-07-12 thomas int, int, int);
4803 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4804 4fc71f3b 2022-07-12 thomas int, int);
4805 4fc71f3b 2022-07-12 thomas
4806 adf4c9e0 2022-07-03 thomas static const struct got_error *
4807 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4808 e5a0f69f 2018-08-18 stsp {
4809 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4810 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4811 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4812 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4813 826082fe 2020-12-10 stsp char *line = NULL;
4814 826082fe 2020-12-10 stsp size_t linesize = 0;
4815 826082fe 2020-12-10 stsp ssize_t linelen;
4816 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4817 e5a0f69f 2018-08-18 stsp
4818 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
4819 82c78e96 2022-08-06 thomas
4820 e5a0f69f 2018-08-18 stsp switch (ch) {
4821 05171be4 2022-06-23 thomas case '0':
4822 05171be4 2022-06-23 thomas view->x = 0;
4823 05171be4 2022-06-23 thomas break;
4824 05171be4 2022-06-23 thomas case '$':
4825 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4826 07b0611c 2022-06-23 thomas view->count = 0;
4827 05171be4 2022-06-23 thomas break;
4828 05171be4 2022-06-23 thomas case KEY_RIGHT:
4829 05171be4 2022-06-23 thomas case 'l':
4830 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4831 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4832 07b0611c 2022-06-23 thomas else
4833 07b0611c 2022-06-23 thomas view->count = 0;
4834 05171be4 2022-06-23 thomas break;
4835 05171be4 2022-06-23 thomas case KEY_LEFT:
4836 05171be4 2022-06-23 thomas case 'h':
4837 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4838 07b0611c 2022-06-23 thomas if (view->x <= 0)
4839 07b0611c 2022-06-23 thomas view->count = 0;
4840 05171be4 2022-06-23 thomas break;
4841 64453f7e 2020-11-21 stsp case 'a':
4842 3dbaef42 2020-11-24 stsp case 'w':
4843 3dbaef42 2020-11-24 stsp if (ch == 'a')
4844 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4845 3dbaef42 2020-11-24 stsp if (ch == 'w')
4846 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4847 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4848 912a3f79 2021-08-30 j break;
4849 912a3f79 2021-08-30 j case 'g':
4850 912a3f79 2021-08-30 j case KEY_HOME:
4851 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4852 07b0611c 2022-06-23 thomas view->count = 0;
4853 64453f7e 2020-11-21 stsp break;
4854 912a3f79 2021-08-30 j case 'G':
4855 912a3f79 2021-08-30 j case KEY_END:
4856 07b0611c 2022-06-23 thomas view->count = 0;
4857 912a3f79 2021-08-30 j if (s->eof)
4858 912a3f79 2021-08-30 j break;
4859 912a3f79 2021-08-30 j
4860 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4861 912a3f79 2021-08-30 j s->eof = 1;
4862 912a3f79 2021-08-30 j break;
4863 1e37a5c2 2019-05-12 jcs case 'k':
4864 1e37a5c2 2019-05-12 jcs case KEY_UP:
4865 f7140bf5 2021-10-17 thomas case CTRL('p'):
4866 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4867 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4868 07b0611c 2022-06-23 thomas else
4869 07b0611c 2022-06-23 thomas view->count = 0;
4870 1e37a5c2 2019-05-12 jcs break;
4871 70f17a53 2022-06-13 thomas case CTRL('u'):
4872 23427b14 2022-06-23 thomas case 'u':
4873 70f17a53 2022-06-13 thomas nscroll /= 2;
4874 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4875 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4876 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4877 1c5e5faa 2022-06-23 thomas case 'b':
4878 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4879 07b0611c 2022-06-23 thomas view->count = 0;
4880 26ed57b2 2018-05-19 stsp break;
4881 07b0611c 2022-06-23 thomas }
4882 1e37a5c2 2019-05-12 jcs i = 0;
4883 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4884 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4885 1e37a5c2 2019-05-12 jcs break;
4886 1e37a5c2 2019-05-12 jcs case 'j':
4887 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4888 f7140bf5 2021-10-17 thomas case CTRL('n'):
4889 1e37a5c2 2019-05-12 jcs if (!s->eof)
4890 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4891 07b0611c 2022-06-23 thomas else
4892 07b0611c 2022-06-23 thomas view->count = 0;
4893 1e37a5c2 2019-05-12 jcs break;
4894 70f17a53 2022-06-13 thomas case CTRL('d'):
4895 23427b14 2022-06-23 thomas case 'd':
4896 70f17a53 2022-06-13 thomas nscroll /= 2;
4897 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4898 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4899 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4900 1c5e5faa 2022-06-23 thomas case 'f':
4901 1e37a5c2 2019-05-12 jcs case ' ':
4902 07b0611c 2022-06-23 thomas if (s->eof) {
4903 07b0611c 2022-06-23 thomas view->count = 0;
4904 1e37a5c2 2019-05-12 jcs break;
4905 07b0611c 2022-06-23 thomas }
4906 1e37a5c2 2019-05-12 jcs i = 0;
4907 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4908 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4909 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4910 826082fe 2020-12-10 stsp if (linelen == -1) {
4911 826082fe 2020-12-10 stsp if (feof(s->f)) {
4912 826082fe 2020-12-10 stsp s->eof = 1;
4913 826082fe 2020-12-10 stsp } else
4914 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4915 34bc9ec9 2019-02-22 stsp break;
4916 826082fe 2020-12-10 stsp }
4917 1e37a5c2 2019-05-12 jcs }
4918 826082fe 2020-12-10 stsp free(line);
4919 1e37a5c2 2019-05-12 jcs break;
4920 82c78e96 2022-08-06 thomas case '(':
4921 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
4922 82c78e96 2022-08-06 thomas break;
4923 82c78e96 2022-08-06 thomas case ')':
4924 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
4925 82c78e96 2022-08-06 thomas break;
4926 82c78e96 2022-08-06 thomas case '{':
4927 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
4928 82c78e96 2022-08-06 thomas break;
4929 82c78e96 2022-08-06 thomas case '}':
4930 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
4931 82c78e96 2022-08-06 thomas break;
4932 1e37a5c2 2019-05-12 jcs case '[':
4933 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4934 1e37a5c2 2019-05-12 jcs s->diff_context--;
4935 aa61903a 2021-12-31 thomas s->matched_line = 0;
4936 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4937 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4938 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4939 27829c9e 2020-11-21 stsp s->nlines) {
4940 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4941 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4942 27829c9e 2020-11-21 stsp }
4943 07b0611c 2022-06-23 thomas } else
4944 07b0611c 2022-06-23 thomas view->count = 0;
4945 1e37a5c2 2019-05-12 jcs break;
4946 1e37a5c2 2019-05-12 jcs case ']':
4947 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4948 1e37a5c2 2019-05-12 jcs s->diff_context++;
4949 aa61903a 2021-12-31 thomas s->matched_line = 0;
4950 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4951 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4952 07b0611c 2022-06-23 thomas } else
4953 07b0611c 2022-06-23 thomas view->count = 0;
4954 1e37a5c2 2019-05-12 jcs break;
4955 1e37a5c2 2019-05-12 jcs case '<':
4956 1e37a5c2 2019-05-12 jcs case ',':
4957 777aae21 2022-07-20 thomas case 'K':
4958 4fc71f3b 2022-07-12 thomas up = 1;
4959 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4960 4fc71f3b 2022-07-12 thomas case '>':
4961 4fc71f3b 2022-07-12 thomas case '.':
4962 777aae21 2022-07-20 thomas case 'J':
4963 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4964 07b0611c 2022-06-23 thomas view->count = 0;
4965 48ae06ee 2018-10-18 stsp break;
4966 07b0611c 2022-06-23 thomas }
4967 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4968 6524637e 2019-02-21 stsp
4969 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4970 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4971 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4972 15a087fe 2019-02-21 stsp
4973 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4974 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4975 4fc71f3b 2022-07-12 thomas if (err)
4976 4fc71f3b 2022-07-12 thomas break;
4977 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4978 15a087fe 2019-02-21 stsp
4979 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4980 4fc71f3b 2022-07-12 thomas break;
4981 15a087fe 2019-02-21 stsp
4982 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4983 4fc71f3b 2022-07-12 thomas if (err)
4984 4fc71f3b 2022-07-12 thomas break;
4985 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4986 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4987 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4988 5e224a3e 2019-02-22 stsp
4989 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4990 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4991 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4992 4fc71f3b 2022-07-12 thomas
4993 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4994 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4995 4fc71f3b 2022-07-12 thomas if (err)
4996 4fc71f3b 2022-07-12 thomas break;
4997 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4998 5a8b5076 2020-12-05 stsp
4999 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5000 4fc71f3b 2022-07-12 thomas break;
5001 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5002 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5003 4fc71f3b 2022-07-12 thomas bs->selected_line);
5004 4fc71f3b 2022-07-12 thomas if (id == NULL)
5005 4fc71f3b 2022-07-12 thomas break;
5006 15a087fe 2019-02-21 stsp
5007 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5008 4fc71f3b 2022-07-12 thomas break;
5009 15a087fe 2019-02-21 stsp
5010 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5011 4fc71f3b 2022-07-12 thomas if (err)
5012 4fc71f3b 2022-07-12 thomas break;
5013 4fc71f3b 2022-07-12 thomas }
5014 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5015 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5016 aa61903a 2021-12-31 thomas s->matched_line = 0;
5017 05171be4 2022-06-23 thomas view->x = 0;
5018 1e37a5c2 2019-05-12 jcs
5019 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5020 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5021 1e37a5c2 2019-05-12 jcs break;
5022 1e37a5c2 2019-05-12 jcs default:
5023 07b0611c 2022-06-23 thomas view->count = 0;
5024 1e37a5c2 2019-05-12 jcs break;
5025 26ed57b2 2018-05-19 stsp }
5026 e5a0f69f 2018-08-18 stsp
5027 bcbd79e2 2018-08-19 stsp return err;
5028 26ed57b2 2018-05-19 stsp }
5029 26ed57b2 2018-05-19 stsp
5030 4ed7e80c 2018-05-20 stsp static const struct got_error *
5031 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5032 9f7d7167 2018-04-29 stsp {
5033 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5034 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5035 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5036 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5037 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5038 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5039 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5040 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5041 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5042 3dbaef42 2020-11-24 stsp const char *errstr;
5043 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5044 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5045 70ac5f84 2019-03-28 stsp
5046 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5047 26ed57b2 2018-05-19 stsp switch (ch) {
5048 64453f7e 2020-11-21 stsp case 'a':
5049 64453f7e 2020-11-21 stsp force_text_diff = 1;
5050 3dbaef42 2020-11-24 stsp break;
5051 3dbaef42 2020-11-24 stsp case 'C':
5052 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5053 3dbaef42 2020-11-24 stsp &errstr);
5054 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5055 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5056 8f666e67 2022-02-12 thomas errstr, errstr);
5057 64453f7e 2020-11-21 stsp break;
5058 09b5bff8 2020-02-23 naddy case 'r':
5059 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5060 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5061 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5062 09b5bff8 2020-02-23 naddy optarg);
5063 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5064 09b5bff8 2020-02-23 naddy break;
5065 3dbaef42 2020-11-24 stsp case 'w':
5066 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5067 3dbaef42 2020-11-24 stsp break;
5068 26ed57b2 2018-05-19 stsp default:
5069 17020d27 2019-03-07 stsp usage_diff();
5070 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5071 26ed57b2 2018-05-19 stsp }
5072 26ed57b2 2018-05-19 stsp }
5073 26ed57b2 2018-05-19 stsp
5074 26ed57b2 2018-05-19 stsp argc -= optind;
5075 26ed57b2 2018-05-19 stsp argv += optind;
5076 26ed57b2 2018-05-19 stsp
5077 26ed57b2 2018-05-19 stsp if (argc == 0) {
5078 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5079 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5080 15a94983 2018-12-23 stsp id_str1 = argv[0];
5081 15a94983 2018-12-23 stsp id_str2 = argv[1];
5082 26ed57b2 2018-05-19 stsp } else
5083 26ed57b2 2018-05-19 stsp usage_diff();
5084 eb6600df 2019-01-04 stsp
5085 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5086 7cd52833 2022-06-23 thomas if (error)
5087 7cd52833 2022-06-23 thomas goto done;
5088 7cd52833 2022-06-23 thomas
5089 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5090 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5091 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5092 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5093 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5094 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5095 c156c7a4 2020-12-18 stsp goto done;
5096 a273ac94 2020-02-23 naddy if (worktree)
5097 a273ac94 2020-02-23 naddy repo_path =
5098 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5099 a273ac94 2020-02-23 naddy else
5100 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5101 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5102 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5103 c156c7a4 2020-12-18 stsp goto done;
5104 c156c7a4 2020-12-18 stsp }
5105 a273ac94 2020-02-23 naddy }
5106 a273ac94 2020-02-23 naddy
5107 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5108 eb6600df 2019-01-04 stsp if (error)
5109 eb6600df 2019-01-04 stsp goto done;
5110 26ed57b2 2018-05-19 stsp
5111 a273ac94 2020-02-23 naddy init_curses();
5112 a273ac94 2020-02-23 naddy
5113 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5114 51a10b52 2020-12-26 stsp if (error)
5115 51a10b52 2020-12-26 stsp goto done;
5116 51a10b52 2020-12-26 stsp
5117 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5118 26ed57b2 2018-05-19 stsp if (error)
5119 26ed57b2 2018-05-19 stsp goto done;
5120 26ed57b2 2018-05-19 stsp
5121 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5122 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5123 26ed57b2 2018-05-19 stsp if (error)
5124 26ed57b2 2018-05-19 stsp goto done;
5125 26ed57b2 2018-05-19 stsp
5126 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5127 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5128 26ed57b2 2018-05-19 stsp if (error)
5129 26ed57b2 2018-05-19 stsp goto done;
5130 26ed57b2 2018-05-19 stsp
5131 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5132 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5133 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5134 ea5e7bb5 2018-08-01 stsp goto done;
5135 ea5e7bb5 2018-08-01 stsp }
5136 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5137 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5138 5dc9f4bc 2018-08-04 stsp if (error)
5139 5dc9f4bc 2018-08-04 stsp goto done;
5140 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5141 26ed57b2 2018-05-19 stsp done:
5142 3dbaef42 2020-11-24 stsp free(label1);
5143 3dbaef42 2020-11-24 stsp free(label2);
5144 c02c541e 2019-03-29 stsp free(repo_path);
5145 a273ac94 2020-02-23 naddy free(cwd);
5146 1d0f4054 2021-06-17 stsp if (repo) {
5147 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5148 1d0f4054 2021-06-17 stsp if (error == NULL)
5149 1d0f4054 2021-06-17 stsp error = close_err;
5150 1d0f4054 2021-06-17 stsp }
5151 a273ac94 2020-02-23 naddy if (worktree)
5152 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5153 7cd52833 2022-06-23 thomas if (pack_fds) {
5154 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5155 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5156 7cd52833 2022-06-23 thomas if (error == NULL)
5157 7cd52833 2022-06-23 thomas error = pack_err;
5158 7cd52833 2022-06-23 thomas }
5159 51a10b52 2020-12-26 stsp tog_free_refs();
5160 26ed57b2 2018-05-19 stsp return error;
5161 9f7d7167 2018-04-29 stsp }
5162 9f7d7167 2018-04-29 stsp
5163 4ed7e80c 2018-05-20 stsp __dead static void
5164 9f7d7167 2018-04-29 stsp usage_blame(void)
5165 9f7d7167 2018-04-29 stsp {
5166 80ddbec8 2018-04-29 stsp endwin();
5167 91198554 2022-06-23 thomas fprintf(stderr,
5168 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5169 9f7d7167 2018-04-29 stsp getprogname());
5170 9f7d7167 2018-04-29 stsp exit(1);
5171 9f7d7167 2018-04-29 stsp }
5172 84451b3e 2018-07-10 stsp
5173 84451b3e 2018-07-10 stsp struct tog_blame_line {
5174 84451b3e 2018-07-10 stsp int annotated;
5175 84451b3e 2018-07-10 stsp struct got_object_id *id;
5176 84451b3e 2018-07-10 stsp };
5177 9f7d7167 2018-04-29 stsp
5178 4ed7e80c 2018-05-20 stsp static const struct got_error *
5179 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5180 84451b3e 2018-07-10 stsp {
5181 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5182 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5183 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5184 84451b3e 2018-07-10 stsp const struct got_error *err;
5185 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5186 826082fe 2020-12-10 stsp char *line = NULL;
5187 826082fe 2020-12-10 stsp size_t linesize = 0;
5188 826082fe 2020-12-10 stsp ssize_t linelen;
5189 84451b3e 2018-07-10 stsp wchar_t *wline;
5190 27a741e5 2019-09-11 stsp int width;
5191 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5192 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5193 ab089a2a 2018-07-12 stsp char *id_str;
5194 11b20872 2019-11-08 stsp struct tog_color *tc;
5195 ab089a2a 2018-07-12 stsp
5196 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5197 ab089a2a 2018-07-12 stsp if (err)
5198 ab089a2a 2018-07-12 stsp return err;
5199 84451b3e 2018-07-10 stsp
5200 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5201 f7d12f7e 2018-08-01 stsp werase(view->window);
5202 84451b3e 2018-07-10 stsp
5203 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5204 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5205 ab089a2a 2018-07-12 stsp free(id_str);
5206 ab089a2a 2018-07-12 stsp return err;
5207 ab089a2a 2018-07-12 stsp }
5208 ab089a2a 2018-07-12 stsp
5209 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5210 ab089a2a 2018-07-12 stsp free(line);
5211 2550e4c3 2018-07-13 stsp line = NULL;
5212 1cae65b4 2019-09-22 stsp if (err)
5213 1cae65b4 2019-09-22 stsp return err;
5214 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5215 a3404814 2018-09-02 stsp wstandout(view->window);
5216 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5217 11b20872 2019-11-08 stsp if (tc)
5218 11b20872 2019-11-08 stsp wattr_on(view->window,
5219 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5220 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5221 11b20872 2019-11-08 stsp if (tc)
5222 11b20872 2019-11-08 stsp wattr_off(view->window,
5223 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5224 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5225 a3404814 2018-09-02 stsp wstandend(view->window);
5226 2550e4c3 2018-07-13 stsp free(wline);
5227 2550e4c3 2018-07-13 stsp wline = NULL;
5228 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5229 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5230 ab089a2a 2018-07-12 stsp
5231 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5232 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5233 07dd3ed3 2022-08-06 thomas
5234 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5235 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5236 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5237 ab089a2a 2018-07-12 stsp free(id_str);
5238 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5239 ab089a2a 2018-07-12 stsp }
5240 ab089a2a 2018-07-12 stsp free(id_str);
5241 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5242 3f60a8ef 2018-07-10 stsp free(line);
5243 2550e4c3 2018-07-13 stsp line = NULL;
5244 3f60a8ef 2018-07-10 stsp if (err)
5245 3f60a8ef 2018-07-10 stsp return err;
5246 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5247 2550e4c3 2018-07-13 stsp free(wline);
5248 2550e4c3 2018-07-13 stsp wline = NULL;
5249 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5250 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5251 3f60a8ef 2018-07-10 stsp
5252 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5253 05171be4 2022-06-23 thomas view->maxx = 0;
5254 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5255 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5256 826082fe 2020-12-10 stsp if (linelen == -1) {
5257 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5258 826082fe 2020-12-10 stsp s->eof = 1;
5259 826082fe 2020-12-10 stsp break;
5260 826082fe 2020-12-10 stsp }
5261 84451b3e 2018-07-10 stsp free(line);
5262 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5263 84451b3e 2018-07-10 stsp }
5264 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5265 07dd3ed3 2022-08-06 thomas continue;
5266 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5267 826082fe 2020-12-10 stsp continue;
5268 cbea3800 2022-06-23 thomas
5269 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5270 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5271 cbea3800 2022-06-23 thomas if (err) {
5272 cbea3800 2022-06-23 thomas free(line);
5273 cbea3800 2022-06-23 thomas return err;
5274 cbea3800 2022-06-23 thomas }
5275 cbea3800 2022-06-23 thomas free(wline);
5276 cbea3800 2022-06-23 thomas wline = NULL;
5277 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5278 84451b3e 2018-07-10 stsp
5279 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5280 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5281 b700b5d6 2018-07-10 stsp
5282 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5283 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5284 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5285 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5286 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5287 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5288 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5289 8d0fe45a 2019-08-12 stsp char *id_str;
5290 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5291 91198554 2022-06-23 thomas blame_line->id);
5292 8d0fe45a 2019-08-12 stsp if (err) {
5293 8d0fe45a 2019-08-12 stsp free(line);
5294 8d0fe45a 2019-08-12 stsp return err;
5295 8d0fe45a 2019-08-12 stsp }
5296 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5297 11b20872 2019-11-08 stsp if (tc)
5298 11b20872 2019-11-08 stsp wattr_on(view->window,
5299 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5300 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5301 11b20872 2019-11-08 stsp if (tc)
5302 11b20872 2019-11-08 stsp wattr_off(view->window,
5303 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5304 8d0fe45a 2019-08-12 stsp free(id_str);
5305 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5306 8d0fe45a 2019-08-12 stsp } else {
5307 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5308 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5309 84451b3e 2018-07-10 stsp }
5310 ee41ec32 2018-07-10 stsp } else {
5311 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5312 ee41ec32 2018-07-10 stsp prev_id = NULL;
5313 ee41ec32 2018-07-10 stsp }
5314 84451b3e 2018-07-10 stsp
5315 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5316 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5317 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5318 27a741e5 2019-09-11 stsp
5319 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5320 41605754 2020-11-12 stsp width = 9;
5321 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5322 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5323 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5324 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5325 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5326 41605754 2020-11-12 stsp if (err) {
5327 41605754 2020-11-12 stsp free(line);
5328 41605754 2020-11-12 stsp return err;
5329 41605754 2020-11-12 stsp }
5330 41605754 2020-11-12 stsp width += 9;
5331 41605754 2020-11-12 stsp } else {
5332 923086fd 2022-06-23 thomas int skip;
5333 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5334 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5335 923086fd 2022-06-23 thomas if (err) {
5336 923086fd 2022-06-23 thomas free(line);
5337 923086fd 2022-06-23 thomas return err;
5338 05171be4 2022-06-23 thomas }
5339 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5340 02bce7e2 2022-06-23 thomas width += 9;
5341 41605754 2020-11-12 stsp free(wline);
5342 41605754 2020-11-12 stsp wline = NULL;
5343 41605754 2020-11-12 stsp }
5344 41605754 2020-11-12 stsp
5345 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5346 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5347 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5348 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5349 84451b3e 2018-07-10 stsp }
5350 826082fe 2020-12-10 stsp free(line);
5351 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5352 84451b3e 2018-07-10 stsp
5353 a5d43cac 2022-07-01 thomas view_border(view);
5354 84451b3e 2018-07-10 stsp
5355 84451b3e 2018-07-10 stsp return NULL;
5356 84451b3e 2018-07-10 stsp }
5357 84451b3e 2018-07-10 stsp
5358 84451b3e 2018-07-10 stsp static const struct got_error *
5359 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5360 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5361 84451b3e 2018-07-10 stsp {
5362 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5363 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5364 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5365 1a76625f 2018-10-22 stsp int errcode;
5366 84451b3e 2018-07-10 stsp
5367 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5368 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5369 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5370 84451b3e 2018-07-10 stsp
5371 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5372 1a76625f 2018-10-22 stsp if (errcode)
5373 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5374 84451b3e 2018-07-10 stsp
5375 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5376 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5377 d68a0a7d 2018-07-10 stsp goto done;
5378 d68a0a7d 2018-07-10 stsp }
5379 d68a0a7d 2018-07-10 stsp
5380 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5381 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5382 d68a0a7d 2018-07-10 stsp
5383 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5384 d68a0a7d 2018-07-10 stsp if (line->annotated)
5385 d68a0a7d 2018-07-10 stsp goto done;
5386 d68a0a7d 2018-07-10 stsp
5387 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5388 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5389 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5390 84451b3e 2018-07-10 stsp goto done;
5391 84451b3e 2018-07-10 stsp }
5392 84451b3e 2018-07-10 stsp line->annotated = 1;
5393 84451b3e 2018-07-10 stsp done:
5394 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5395 1a76625f 2018-10-22 stsp if (errcode)
5396 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5397 84451b3e 2018-07-10 stsp return err;
5398 84451b3e 2018-07-10 stsp }
5399 84451b3e 2018-07-10 stsp
5400 84451b3e 2018-07-10 stsp static void *
5401 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5402 84451b3e 2018-07-10 stsp {
5403 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5404 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5405 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5406 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5407 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5408 00a8878e 2022-07-01 thomas
5409 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5410 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5411 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5412 9117a7b7 2022-07-01 thomas
5413 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5414 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5415 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5416 9117a7b7 2022-07-01 thomas goto done;
5417 9117a7b7 2022-07-01 thomas }
5418 18430de3 2018-07-10 stsp
5419 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5420 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5421 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5422 9117a7b7 2022-07-01 thomas goto done;
5423 9117a7b7 2022-07-01 thomas }
5424 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5425 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5426 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5427 9117a7b7 2022-07-01 thomas goto done;
5428 9117a7b7 2022-07-01 thomas }
5429 9117a7b7 2022-07-01 thomas
5430 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5431 61266923 2020-01-14 stsp if (err)
5432 9117a7b7 2022-07-01 thomas goto done;
5433 61266923 2020-01-14 stsp
5434 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5435 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5436 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5437 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5438 fc06ba56 2019-08-22 stsp err = NULL;
5439 18430de3 2018-07-10 stsp
5440 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5441 9117a7b7 2022-07-01 thomas if (errcode) {
5442 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5443 9117a7b7 2022-07-01 thomas goto done;
5444 9117a7b7 2022-07-01 thomas }
5445 18430de3 2018-07-10 stsp
5446 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5447 1d0f4054 2021-06-17 stsp if (err == NULL)
5448 1d0f4054 2021-06-17 stsp err = close_err;
5449 c9beca56 2018-07-22 stsp ta->repo = NULL;
5450 c9beca56 2018-07-22 stsp *ta->complete = 1;
5451 18430de3 2018-07-10 stsp
5452 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5453 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5454 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5455 18430de3 2018-07-10 stsp
5456 9117a7b7 2022-07-01 thomas done:
5457 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5458 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5459 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5460 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5461 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5462 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5463 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5464 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5465 00a8878e 2022-07-01 thomas
5466 18430de3 2018-07-10 stsp return (void *)err;
5467 84451b3e 2018-07-10 stsp }
5468 84451b3e 2018-07-10 stsp
5469 245d91c1 2018-07-12 stsp static struct got_object_id *
5470 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5471 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5472 245d91c1 2018-07-12 stsp {
5473 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5474 8d0fe45a 2019-08-12 stsp
5475 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5476 8d0fe45a 2019-08-12 stsp return NULL;
5477 b880a918 2018-07-10 stsp
5478 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5479 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5480 4fc71f3b 2022-07-12 thomas return NULL;
5481 4fc71f3b 2022-07-12 thomas
5482 4fc71f3b 2022-07-12 thomas return line->id;
5483 4fc71f3b 2022-07-12 thomas }
5484 4fc71f3b 2022-07-12 thomas
5485 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5486 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5487 4fc71f3b 2022-07-12 thomas int lineno)
5488 4fc71f3b 2022-07-12 thomas {
5489 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5490 4fc71f3b 2022-07-12 thomas
5491 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5492 4fc71f3b 2022-07-12 thomas return NULL;
5493 4fc71f3b 2022-07-12 thomas
5494 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5495 245d91c1 2018-07-12 stsp if (!line->annotated)
5496 245d91c1 2018-07-12 stsp return NULL;
5497 245d91c1 2018-07-12 stsp
5498 245d91c1 2018-07-12 stsp return line->id;
5499 b880a918 2018-07-10 stsp }
5500 245d91c1 2018-07-12 stsp
5501 b880a918 2018-07-10 stsp static const struct got_error *
5502 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5503 a70480e0 2018-06-23 stsp {
5504 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5505 245d91c1 2018-07-12 stsp int i;
5506 245d91c1 2018-07-12 stsp
5507 245d91c1 2018-07-12 stsp if (blame->thread) {
5508 1a76625f 2018-10-22 stsp int errcode;
5509 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5510 1a76625f 2018-10-22 stsp if (errcode)
5511 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5512 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5513 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5514 1a76625f 2018-10-22 stsp if (errcode)
5515 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5516 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5517 1a76625f 2018-10-22 stsp if (errcode)
5518 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5519 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5520 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5521 245d91c1 2018-07-12 stsp err = NULL;
5522 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5523 245d91c1 2018-07-12 stsp }
5524 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5525 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5526 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5527 1d0f4054 2021-06-17 stsp if (err == NULL)
5528 1d0f4054 2021-06-17 stsp err = close_err;
5529 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5530 245d91c1 2018-07-12 stsp }
5531 245d91c1 2018-07-12 stsp if (blame->f) {
5532 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5533 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5534 245d91c1 2018-07-12 stsp blame->f = NULL;
5535 245d91c1 2018-07-12 stsp }
5536 57670559 2018-12-23 stsp if (blame->lines) {
5537 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5538 57670559 2018-12-23 stsp free(blame->lines[i].id);
5539 57670559 2018-12-23 stsp free(blame->lines);
5540 57670559 2018-12-23 stsp blame->lines = NULL;
5541 57670559 2018-12-23 stsp }
5542 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5543 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5544 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5545 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5546 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5547 7cd52833 2022-06-23 thomas if (err == NULL)
5548 7cd52833 2022-06-23 thomas err = pack_err;
5549 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5550 7cd52833 2022-06-23 thomas }
5551 245d91c1 2018-07-12 stsp return err;
5552 245d91c1 2018-07-12 stsp }
5553 245d91c1 2018-07-12 stsp
5554 245d91c1 2018-07-12 stsp static const struct got_error *
5555 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5556 fc06ba56 2019-08-22 stsp {
5557 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5558 fc06ba56 2019-08-22 stsp int *done = arg;
5559 fc06ba56 2019-08-22 stsp int errcode;
5560 fc06ba56 2019-08-22 stsp
5561 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5562 fc06ba56 2019-08-22 stsp if (errcode)
5563 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5564 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5565 fc06ba56 2019-08-22 stsp
5566 fc06ba56 2019-08-22 stsp if (*done)
5567 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5568 fc06ba56 2019-08-22 stsp
5569 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5570 fc06ba56 2019-08-22 stsp if (errcode)
5571 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5572 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5573 fc06ba56 2019-08-22 stsp
5574 fc06ba56 2019-08-22 stsp return err;
5575 fc06ba56 2019-08-22 stsp }
5576 fc06ba56 2019-08-22 stsp
5577 fc06ba56 2019-08-22 stsp static const struct got_error *
5578 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5579 245d91c1 2018-07-12 stsp {
5580 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5581 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5582 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5583 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5584 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5585 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5586 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5587 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5588 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5589 a70480e0 2018-06-23 stsp
5590 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5591 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5592 27d434c2 2018-09-15 stsp if (err)
5593 15a94983 2018-12-23 stsp return err;
5594 f4ae6ddb 2022-07-01 thomas
5595 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5596 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5597 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5598 f4ae6ddb 2022-07-01 thomas goto done;
5599 f4ae6ddb 2022-07-01 thomas }
5600 945f9229 2022-04-16 thomas
5601 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5602 945f9229 2022-04-16 thomas if (err)
5603 945f9229 2022-04-16 thomas goto done;
5604 27d434c2 2018-09-15 stsp
5605 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5606 84451b3e 2018-07-10 stsp if (err)
5607 84451b3e 2018-07-10 stsp goto done;
5608 27d434c2 2018-09-15 stsp
5609 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5610 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5611 84451b3e 2018-07-10 stsp goto done;
5612 84451b3e 2018-07-10 stsp }
5613 a70480e0 2018-06-23 stsp
5614 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5615 a70480e0 2018-06-23 stsp if (err)
5616 a70480e0 2018-06-23 stsp goto done;
5617 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5618 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5619 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5620 84451b3e 2018-07-10 stsp goto done;
5621 84451b3e 2018-07-10 stsp }
5622 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5623 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5624 1fddf795 2021-01-20 stsp if (err)
5625 1fddf795 2021-01-20 stsp goto done;
5626 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5627 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5628 84451b3e 2018-07-10 stsp goto done;
5629 1fddf795 2021-01-20 stsp }
5630 b02560ec 2019-08-19 stsp
5631 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5632 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5633 b02560ec 2019-08-19 stsp blame->nlines--;
5634 a70480e0 2018-06-23 stsp
5635 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5636 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5637 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5638 84451b3e 2018-07-10 stsp goto done;
5639 84451b3e 2018-07-10 stsp }
5640 a70480e0 2018-06-23 stsp
5641 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5642 bd24772e 2018-07-11 stsp if (err)
5643 bd24772e 2018-07-11 stsp goto done;
5644 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5645 7cd52833 2022-06-23 thomas pack_fds);
5646 7cd52833 2022-06-23 thomas if (err)
5647 7cd52833 2022-06-23 thomas goto done;
5648 bd24772e 2018-07-11 stsp
5649 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5650 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5651 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5652 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5653 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5654 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5655 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5656 245d91c1 2018-07-12 stsp goto done;
5657 245d91c1 2018-07-12 stsp }
5658 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5659 245d91c1 2018-07-12 stsp
5660 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5661 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5662 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5663 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5664 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5665 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5666 a5388363 2020-12-01 naddy s->blame_complete = 0;
5667 f5a09613 2020-12-13 naddy
5668 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5669 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5670 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5671 f5a09613 2020-12-13 naddy s->selected_line = 1;
5672 f5a09613 2020-12-13 naddy }
5673 aa61903a 2021-12-31 thomas s->matched_line = 0;
5674 245d91c1 2018-07-12 stsp
5675 245d91c1 2018-07-12 stsp done:
5676 945f9229 2022-04-16 thomas if (commit)
5677 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5678 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5679 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5680 245d91c1 2018-07-12 stsp if (blob)
5681 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5682 27d434c2 2018-09-15 stsp free(obj_id);
5683 245d91c1 2018-07-12 stsp if (err)
5684 245d91c1 2018-07-12 stsp stop_blame(blame);
5685 245d91c1 2018-07-12 stsp return err;
5686 245d91c1 2018-07-12 stsp }
5687 245d91c1 2018-07-12 stsp
5688 245d91c1 2018-07-12 stsp static const struct got_error *
5689 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5690 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5691 245d91c1 2018-07-12 stsp {
5692 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5693 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5694 dbc6a6b6 2018-07-12 stsp
5695 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5696 245d91c1 2018-07-12 stsp
5697 c4843652 2019-08-12 stsp s->path = strdup(path);
5698 c4843652 2019-08-12 stsp if (s->path == NULL)
5699 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5700 c4843652 2019-08-12 stsp
5701 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5702 c4843652 2019-08-12 stsp if (err) {
5703 c4843652 2019-08-12 stsp free(s->path);
5704 7cbe629d 2018-08-04 stsp return err;
5705 c4843652 2019-08-12 stsp }
5706 245d91c1 2018-07-12 stsp
5707 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5708 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5709 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5710 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5711 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5712 fb2756b9 2018-08-04 stsp s->repo = repo;
5713 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5714 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5715 7cbe629d 2018-08-04 stsp
5716 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5717 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5718 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5719 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5720 11b20872 2019-11-08 stsp if (err)
5721 11b20872 2019-11-08 stsp return err;
5722 11b20872 2019-11-08 stsp }
5723 11b20872 2019-11-08 stsp
5724 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5725 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5726 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5727 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5728 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5729 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5730 e5a0f69f 2018-08-18 stsp
5731 a5388363 2020-12-01 naddy return run_blame(view);
5732 7cbe629d 2018-08-04 stsp }
5733 7cbe629d 2018-08-04 stsp
5734 e5a0f69f 2018-08-18 stsp static const struct got_error *
5735 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5736 7cbe629d 2018-08-04 stsp {
5737 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5738 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5739 7cbe629d 2018-08-04 stsp
5740 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5741 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5742 e5a0f69f 2018-08-18 stsp
5743 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5744 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5745 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5746 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5747 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5748 7cbe629d 2018-08-04 stsp }
5749 e5a0f69f 2018-08-18 stsp
5750 e5a0f69f 2018-08-18 stsp free(s->path);
5751 11b20872 2019-11-08 stsp free_colors(&s->colors);
5752 e5a0f69f 2018-08-18 stsp return err;
5753 7cbe629d 2018-08-04 stsp }
5754 7cbe629d 2018-08-04 stsp
5755 7cbe629d 2018-08-04 stsp static const struct got_error *
5756 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5757 6c4c42e0 2019-06-24 stsp {
5758 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5759 6c4c42e0 2019-06-24 stsp
5760 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5761 6c4c42e0 2019-06-24 stsp return NULL;
5762 6c4c42e0 2019-06-24 stsp }
5763 6c4c42e0 2019-06-24 stsp
5764 6c4c42e0 2019-06-24 stsp static const struct got_error *
5765 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5766 6c4c42e0 2019-06-24 stsp {
5767 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5768 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5769 6c4c42e0 2019-06-24 stsp int lineno;
5770 78eecffc 2022-06-23 thomas char *line = NULL;
5771 826082fe 2020-12-10 stsp size_t linesize = 0;
5772 826082fe 2020-12-10 stsp ssize_t linelen;
5773 6c4c42e0 2019-06-24 stsp
5774 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5775 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5776 6c4c42e0 2019-06-24 stsp return NULL;
5777 6c4c42e0 2019-06-24 stsp }
5778 6c4c42e0 2019-06-24 stsp
5779 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5780 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5781 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5782 6c4c42e0 2019-06-24 stsp else
5783 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5784 4b3f9dac 2021-12-17 thomas } else
5785 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5786 6c4c42e0 2019-06-24 stsp
5787 6c4c42e0 2019-06-24 stsp while (1) {
5788 6c4c42e0 2019-06-24 stsp off_t offset;
5789 6c4c42e0 2019-06-24 stsp
5790 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5791 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5792 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5793 6c4c42e0 2019-06-24 stsp break;
5794 6c4c42e0 2019-06-24 stsp }
5795 2246482e 2019-06-25 stsp
5796 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5797 6c4c42e0 2019-06-24 stsp lineno = 1;
5798 6c4c42e0 2019-06-24 stsp else
5799 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5800 6c4c42e0 2019-06-24 stsp }
5801 6c4c42e0 2019-06-24 stsp
5802 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5803 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5804 6c4c42e0 2019-06-24 stsp free(line);
5805 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5806 6c4c42e0 2019-06-24 stsp }
5807 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5808 78eecffc 2022-06-23 thomas if (linelen != -1) {
5809 78eecffc 2022-06-23 thomas char *exstr;
5810 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5811 78eecffc 2022-06-23 thomas if (err)
5812 78eecffc 2022-06-23 thomas break;
5813 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5814 78eecffc 2022-06-23 thomas &view->regmatch)) {
5815 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5816 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5817 78eecffc 2022-06-23 thomas free(exstr);
5818 78eecffc 2022-06-23 thomas break;
5819 78eecffc 2022-06-23 thomas }
5820 78eecffc 2022-06-23 thomas free(exstr);
5821 6c4c42e0 2019-06-24 stsp }
5822 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5823 6c4c42e0 2019-06-24 stsp lineno++;
5824 6c4c42e0 2019-06-24 stsp else
5825 6c4c42e0 2019-06-24 stsp lineno--;
5826 6c4c42e0 2019-06-24 stsp }
5827 826082fe 2020-12-10 stsp free(line);
5828 6c4c42e0 2019-06-24 stsp
5829 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5830 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5831 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5832 6c4c42e0 2019-06-24 stsp }
5833 6c4c42e0 2019-06-24 stsp
5834 05171be4 2022-06-23 thomas return err;
5835 6c4c42e0 2019-06-24 stsp }
5836 6c4c42e0 2019-06-24 stsp
5837 6c4c42e0 2019-06-24 stsp static const struct got_error *
5838 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5839 7cbe629d 2018-08-04 stsp {
5840 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5841 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5842 2b380cc8 2018-10-24 stsp int errcode;
5843 2b380cc8 2018-10-24 stsp
5844 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5845 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5846 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5847 2b380cc8 2018-10-24 stsp if (errcode)
5848 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5849 51fe7530 2019-08-19 stsp
5850 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5851 2b380cc8 2018-10-24 stsp }
5852 e5a0f69f 2018-08-18 stsp
5853 51fe7530 2019-08-19 stsp if (s->blame_complete)
5854 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5855 51fe7530 2019-08-19 stsp
5856 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5857 e5a0f69f 2018-08-18 stsp
5858 a5d43cac 2022-07-01 thomas view_border(view);
5859 e5a0f69f 2018-08-18 stsp return err;
5860 e5a0f69f 2018-08-18 stsp }
5861 e5a0f69f 2018-08-18 stsp
5862 e5a0f69f 2018-08-18 stsp static const struct got_error *
5863 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5864 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
5865 eaeaa612 2022-07-20 thomas {
5866 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
5867 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
5868 eaeaa612 2022-07-20 thomas
5869 eaeaa612 2022-07-20 thomas *new_view = NULL;
5870 eaeaa612 2022-07-20 thomas
5871 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5872 eaeaa612 2022-07-20 thomas if (log_view == NULL)
5873 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
5874 eaeaa612 2022-07-20 thomas
5875 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5876 eaeaa612 2022-07-20 thomas if (err)
5877 eaeaa612 2022-07-20 thomas view_close(log_view);
5878 eaeaa612 2022-07-20 thomas else
5879 eaeaa612 2022-07-20 thomas *new_view = log_view;
5880 eaeaa612 2022-07-20 thomas
5881 eaeaa612 2022-07-20 thomas return err;
5882 eaeaa612 2022-07-20 thomas }
5883 eaeaa612 2022-07-20 thomas
5884 eaeaa612 2022-07-20 thomas static const struct got_error *
5885 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5886 e5a0f69f 2018-08-18 stsp {
5887 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5888 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
5889 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5890 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5891 a5d43cac 2022-07-01 thomas
5892 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5893 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5894 a5d43cac 2022-07-01 thomas --eos; /* border */
5895 7cbe629d 2018-08-04 stsp
5896 e5a0f69f 2018-08-18 stsp switch (ch) {
5897 05171be4 2022-06-23 thomas case '0':
5898 05171be4 2022-06-23 thomas view->x = 0;
5899 05171be4 2022-06-23 thomas break;
5900 05171be4 2022-06-23 thomas case '$':
5901 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5902 07b0611c 2022-06-23 thomas view->count = 0;
5903 05171be4 2022-06-23 thomas break;
5904 05171be4 2022-06-23 thomas case KEY_RIGHT:
5905 05171be4 2022-06-23 thomas case 'l':
5906 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5907 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5908 07b0611c 2022-06-23 thomas else
5909 07b0611c 2022-06-23 thomas view->count = 0;
5910 05171be4 2022-06-23 thomas break;
5911 05171be4 2022-06-23 thomas case KEY_LEFT:
5912 05171be4 2022-06-23 thomas case 'h':
5913 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5914 07b0611c 2022-06-23 thomas if (view->x <= 0)
5915 07b0611c 2022-06-23 thomas view->count = 0;
5916 05171be4 2022-06-23 thomas break;
5917 1e37a5c2 2019-05-12 jcs case 'q':
5918 1e37a5c2 2019-05-12 jcs s->done = 1;
5919 4deef56f 2021-09-02 naddy break;
5920 4deef56f 2021-09-02 naddy case 'g':
5921 4deef56f 2021-09-02 naddy case KEY_HOME:
5922 4deef56f 2021-09-02 naddy s->selected_line = 1;
5923 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5924 07b0611c 2022-06-23 thomas view->count = 0;
5925 4deef56f 2021-09-02 naddy break;
5926 4deef56f 2021-09-02 naddy case 'G':
5927 4deef56f 2021-09-02 naddy case KEY_END:
5928 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5929 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5930 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5931 4deef56f 2021-09-02 naddy } else {
5932 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5933 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5934 4deef56f 2021-09-02 naddy }
5935 07b0611c 2022-06-23 thomas view->count = 0;
5936 1e37a5c2 2019-05-12 jcs break;
5937 1e37a5c2 2019-05-12 jcs case 'k':
5938 1e37a5c2 2019-05-12 jcs case KEY_UP:
5939 f7140bf5 2021-10-17 thomas case CTRL('p'):
5940 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5941 1e37a5c2 2019-05-12 jcs s->selected_line--;
5942 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5943 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5944 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5945 07b0611c 2022-06-23 thomas else
5946 07b0611c 2022-06-23 thomas view->count = 0;
5947 1e37a5c2 2019-05-12 jcs break;
5948 70f17a53 2022-06-13 thomas case CTRL('u'):
5949 23427b14 2022-06-23 thomas case 'u':
5950 70f17a53 2022-06-13 thomas nscroll /= 2;
5951 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5952 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5953 ea025d1d 2020-02-22 naddy case CTRL('b'):
5954 1c5e5faa 2022-06-23 thomas case 'b':
5955 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5956 07b0611c 2022-06-23 thomas if (view->count > 1)
5957 07b0611c 2022-06-23 thomas nscroll += nscroll;
5958 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5959 07b0611c 2022-06-23 thomas view->count = 0;
5960 e5a0f69f 2018-08-18 stsp break;
5961 1e37a5c2 2019-05-12 jcs }
5962 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5963 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5964 1e37a5c2 2019-05-12 jcs else
5965 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5966 1e37a5c2 2019-05-12 jcs break;
5967 1e37a5c2 2019-05-12 jcs case 'j':
5968 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5969 f7140bf5 2021-10-17 thomas case CTRL('n'):
5970 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5971 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5972 1e37a5c2 2019-05-12 jcs s->selected_line++;
5973 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5974 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5975 07b0611c 2022-06-23 thomas else
5976 07b0611c 2022-06-23 thomas view->count = 0;
5977 1e37a5c2 2019-05-12 jcs break;
5978 1c5e5faa 2022-06-23 thomas case 'c':
5979 1e37a5c2 2019-05-12 jcs case 'p': {
5980 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5981 07b0611c 2022-06-23 thomas
5982 07b0611c 2022-06-23 thomas view->count = 0;
5983 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5984 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5985 1e37a5c2 2019-05-12 jcs if (id == NULL)
5986 e5a0f69f 2018-08-18 stsp break;
5987 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5988 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5989 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5990 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5991 1e37a5c2 2019-05-12 jcs int obj_type;
5992 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5993 1e37a5c2 2019-05-12 jcs s->repo, id);
5994 e5a0f69f 2018-08-18 stsp if (err)
5995 e5a0f69f 2018-08-18 stsp break;
5996 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5997 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5998 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5999 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6000 e5a0f69f 2018-08-18 stsp break;
6001 e5a0f69f 2018-08-18 stsp }
6002 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6003 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6004 ec242592 2022-04-22 thomas s->repo, &pid->id);
6005 945f9229 2022-04-16 thomas if (err)
6006 945f9229 2022-04-16 thomas break;
6007 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6008 945f9229 2022-04-16 thomas pcommit, s->path);
6009 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6010 e5a0f69f 2018-08-18 stsp if (err) {
6011 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6012 1e37a5c2 2019-05-12 jcs err = NULL;
6013 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6014 e5a0f69f 2018-08-18 stsp break;
6015 e5a0f69f 2018-08-18 stsp }
6016 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6017 1e37a5c2 2019-05-12 jcs blob_id);
6018 1e37a5c2 2019-05-12 jcs free(blob_id);
6019 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6020 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6021 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6022 e5a0f69f 2018-08-18 stsp break;
6023 1e37a5c2 2019-05-12 jcs }
6024 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6025 ec242592 2022-04-22 thomas &pid->id);
6026 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6027 1e37a5c2 2019-05-12 jcs } else {
6028 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6029 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6030 1e37a5c2 2019-05-12 jcs break;
6031 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6032 1e37a5c2 2019-05-12 jcs id);
6033 1e37a5c2 2019-05-12 jcs }
6034 1e37a5c2 2019-05-12 jcs if (err)
6035 e5a0f69f 2018-08-18 stsp break;
6036 1e37a5c2 2019-05-12 jcs s->done = 1;
6037 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6038 1e37a5c2 2019-05-12 jcs s->done = 0;
6039 1e37a5c2 2019-05-12 jcs if (thread_err)
6040 1e37a5c2 2019-05-12 jcs break;
6041 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6042 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6043 a5388363 2020-12-01 naddy err = run_blame(view);
6044 1e37a5c2 2019-05-12 jcs if (err)
6045 1e37a5c2 2019-05-12 jcs break;
6046 1e37a5c2 2019-05-12 jcs break;
6047 1e37a5c2 2019-05-12 jcs }
6048 1c5e5faa 2022-06-23 thomas case 'C': {
6049 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6050 07b0611c 2022-06-23 thomas
6051 07b0611c 2022-06-23 thomas view->count = 0;
6052 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6053 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6054 1e37a5c2 2019-05-12 jcs break;
6055 1e37a5c2 2019-05-12 jcs s->done = 1;
6056 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6057 1e37a5c2 2019-05-12 jcs s->done = 0;
6058 1e37a5c2 2019-05-12 jcs if (thread_err)
6059 1e37a5c2 2019-05-12 jcs break;
6060 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6061 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6062 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6063 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6064 a5388363 2020-12-01 naddy err = run_blame(view);
6065 1e37a5c2 2019-05-12 jcs if (err)
6066 1e37a5c2 2019-05-12 jcs break;
6067 1e37a5c2 2019-05-12 jcs break;
6068 1e37a5c2 2019-05-12 jcs }
6069 2a31b33b 2022-07-23 thomas case 'L':
6070 eaeaa612 2022-07-20 thomas view->count = 0;
6071 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6072 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6073 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6074 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6075 eaeaa612 2022-07-20 thomas break;
6076 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6077 1e37a5c2 2019-05-12 jcs case '\r': {
6078 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6079 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6080 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6081 07b0611c 2022-06-23 thomas
6082 07b0611c 2022-06-23 thomas view->count = 0;
6083 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6084 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6085 1e37a5c2 2019-05-12 jcs if (id == NULL)
6086 1e37a5c2 2019-05-12 jcs break;
6087 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6088 1e37a5c2 2019-05-12 jcs if (err)
6089 1e37a5c2 2019-05-12 jcs break;
6090 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6091 4fc71f3b 2022-07-12 thomas if (*new_view) {
6092 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6093 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6094 4fc71f3b 2022-07-12 thomas if (err)
6095 4fc71f3b 2022-07-12 thomas break;
6096 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6097 4fc71f3b 2022-07-12 thomas } else {
6098 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6099 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6100 a5d43cac 2022-07-01 thomas
6101 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6102 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6103 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6104 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6105 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6106 4fc71f3b 2022-07-12 thomas break;
6107 4fc71f3b 2022-07-12 thomas }
6108 15a94983 2018-12-23 stsp }
6109 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6110 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6111 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6112 1e37a5c2 2019-05-12 jcs if (err) {
6113 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6114 1e37a5c2 2019-05-12 jcs break;
6115 1e37a5c2 2019-05-12 jcs }
6116 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6117 4fc71f3b 2022-07-12 thomas s->selected_line;
6118 4fc71f3b 2022-07-12 thomas if (*new_view)
6119 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6120 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6121 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6122 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6123 a5d43cac 2022-07-01 thomas if (err)
6124 a5d43cac 2022-07-01 thomas break;
6125 a5d43cac 2022-07-01 thomas }
6126 a5d43cac 2022-07-01 thomas
6127 e78dc838 2020-12-04 stsp view->focussed = 0;
6128 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6129 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6130 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6131 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6132 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6133 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6134 1e37a5c2 2019-05-12 jcs if (err)
6135 34bc9ec9 2019-02-22 stsp break;
6136 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6137 40236d76 2022-06-23 thomas if (err)
6138 40236d76 2022-06-23 thomas break;
6139 e78dc838 2020-12-04 stsp view->focus_child = 1;
6140 1e37a5c2 2019-05-12 jcs } else
6141 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6142 1e37a5c2 2019-05-12 jcs if (err)
6143 e5a0f69f 2018-08-18 stsp break;
6144 1e37a5c2 2019-05-12 jcs break;
6145 1e37a5c2 2019-05-12 jcs }
6146 70f17a53 2022-06-13 thomas case CTRL('d'):
6147 23427b14 2022-06-23 thomas case 'd':
6148 70f17a53 2022-06-13 thomas nscroll /= 2;
6149 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6150 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6151 ea025d1d 2020-02-22 naddy case CTRL('f'):
6152 1c5e5faa 2022-06-23 thomas case 'f':
6153 1e37a5c2 2019-05-12 jcs case ' ':
6154 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6155 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6156 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6157 07b0611c 2022-06-23 thomas view->count = 0;
6158 e5a0f69f 2018-08-18 stsp break;
6159 1e37a5c2 2019-05-12 jcs }
6160 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6161 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6162 70f17a53 2022-06-13 thomas s->selected_line +=
6163 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6164 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6165 1e37a5c2 2019-05-12 jcs }
6166 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6167 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6168 1e37a5c2 2019-05-12 jcs else
6169 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6170 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6171 1e37a5c2 2019-05-12 jcs break;
6172 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6173 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6174 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6175 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6176 1e37a5c2 2019-05-12 jcs }
6177 1e37a5c2 2019-05-12 jcs break;
6178 1e37a5c2 2019-05-12 jcs default:
6179 07b0611c 2022-06-23 thomas view->count = 0;
6180 1e37a5c2 2019-05-12 jcs break;
6181 a70480e0 2018-06-23 stsp }
6182 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6183 adf4c9e0 2022-07-03 thomas }
6184 adf4c9e0 2022-07-03 thomas
6185 adf4c9e0 2022-07-03 thomas static const struct got_error *
6186 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6187 adf4c9e0 2022-07-03 thomas {
6188 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6189 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6190 adf4c9e0 2022-07-03 thomas
6191 adf4c9e0 2022-07-03 thomas view->count = 0;
6192 adf4c9e0 2022-07-03 thomas s->done = 1;
6193 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6194 adf4c9e0 2022-07-03 thomas s->done = 0;
6195 adf4c9e0 2022-07-03 thomas if (err)
6196 adf4c9e0 2022-07-03 thomas return err;
6197 adf4c9e0 2022-07-03 thomas return run_blame(view);
6198 a70480e0 2018-06-23 stsp }
6199 a70480e0 2018-06-23 stsp
6200 a70480e0 2018-06-23 stsp static const struct got_error *
6201 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6202 9f7d7167 2018-04-29 stsp {
6203 a70480e0 2018-06-23 stsp const struct got_error *error;
6204 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6205 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6206 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6207 0587e10c 2020-07-23 stsp char *link_target = NULL;
6208 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6209 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6210 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6211 a70480e0 2018-06-23 stsp int ch;
6212 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6213 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6214 a70480e0 2018-06-23 stsp
6215 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6216 a70480e0 2018-06-23 stsp switch (ch) {
6217 a70480e0 2018-06-23 stsp case 'c':
6218 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6219 a70480e0 2018-06-23 stsp break;
6220 69069811 2018-08-02 stsp case 'r':
6221 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6222 69069811 2018-08-02 stsp if (repo_path == NULL)
6223 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6224 9ba1d308 2019-10-21 stsp optarg);
6225 69069811 2018-08-02 stsp break;
6226 a70480e0 2018-06-23 stsp default:
6227 17020d27 2019-03-07 stsp usage_blame();
6228 a70480e0 2018-06-23 stsp /* NOTREACHED */
6229 a70480e0 2018-06-23 stsp }
6230 a70480e0 2018-06-23 stsp }
6231 a70480e0 2018-06-23 stsp
6232 a70480e0 2018-06-23 stsp argc -= optind;
6233 a70480e0 2018-06-23 stsp argv += optind;
6234 a70480e0 2018-06-23 stsp
6235 f135c941 2020-02-20 stsp if (argc != 1)
6236 a70480e0 2018-06-23 stsp usage_blame();
6237 6962eb72 2020-02-20 stsp
6238 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6239 7cd52833 2022-06-23 thomas if (error != NULL)
6240 7cd52833 2022-06-23 thomas goto done;
6241 7cd52833 2022-06-23 thomas
6242 69069811 2018-08-02 stsp if (repo_path == NULL) {
6243 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6244 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6245 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6246 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6247 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6248 c156c7a4 2020-12-18 stsp goto done;
6249 f135c941 2020-02-20 stsp if (worktree)
6250 eb41ed75 2019-02-05 stsp repo_path =
6251 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6252 f135c941 2020-02-20 stsp else
6253 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6254 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6255 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6256 c156c7a4 2020-12-18 stsp goto done;
6257 c156c7a4 2020-12-18 stsp }
6258 f135c941 2020-02-20 stsp }
6259 a915003a 2019-02-05 stsp
6260 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6261 c02c541e 2019-03-29 stsp if (error != NULL)
6262 8e94dd5b 2019-01-04 stsp goto done;
6263 69069811 2018-08-02 stsp
6264 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6265 f135c941 2020-02-20 stsp worktree);
6266 c02c541e 2019-03-29 stsp if (error)
6267 92205607 2019-01-04 stsp goto done;
6268 69069811 2018-08-02 stsp
6269 f135c941 2020-02-20 stsp init_curses();
6270 f135c941 2020-02-20 stsp
6271 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6272 51a10b52 2020-12-26 stsp if (error)
6273 51a10b52 2020-12-26 stsp goto done;
6274 51a10b52 2020-12-26 stsp
6275 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6276 eb41ed75 2019-02-05 stsp if (error)
6277 69069811 2018-08-02 stsp goto done;
6278 a70480e0 2018-06-23 stsp
6279 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6280 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6281 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6282 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6283 a70480e0 2018-06-23 stsp if (error != NULL)
6284 66b4983c 2018-06-23 stsp goto done;
6285 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6286 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6287 a70480e0 2018-06-23 stsp } else {
6288 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6289 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6290 a70480e0 2018-06-23 stsp }
6291 a19e88aa 2018-06-23 stsp if (error != NULL)
6292 8b473291 2019-02-21 stsp goto done;
6293 8b473291 2019-02-21 stsp
6294 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6295 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6296 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6297 e1cd8fed 2018-08-01 stsp goto done;
6298 e1cd8fed 2018-08-01 stsp }
6299 0587e10c 2020-07-23 stsp
6300 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6301 945f9229 2022-04-16 thomas if (error)
6302 945f9229 2022-04-16 thomas goto done;
6303 945f9229 2022-04-16 thomas
6304 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6305 945f9229 2022-04-16 thomas commit, repo);
6306 7cbe629d 2018-08-04 stsp if (error)
6307 7cbe629d 2018-08-04 stsp goto done;
6308 0587e10c 2020-07-23 stsp
6309 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6310 78756c87 2020-11-24 stsp commit_id, repo);
6311 0587e10c 2020-07-23 stsp if (error)
6312 0587e10c 2020-07-23 stsp goto done;
6313 12314ad4 2019-08-31 stsp if (worktree) {
6314 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6315 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6316 12314ad4 2019-08-31 stsp worktree = NULL;
6317 12314ad4 2019-08-31 stsp }
6318 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6319 a70480e0 2018-06-23 stsp done:
6320 69069811 2018-08-02 stsp free(repo_path);
6321 f135c941 2020-02-20 stsp free(in_repo_path);
6322 0587e10c 2020-07-23 stsp free(link_target);
6323 69069811 2018-08-02 stsp free(cwd);
6324 a70480e0 2018-06-23 stsp free(commit_id);
6325 945f9229 2022-04-16 thomas if (commit)
6326 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6327 eb41ed75 2019-02-05 stsp if (worktree)
6328 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6329 1d0f4054 2021-06-17 stsp if (repo) {
6330 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6331 1d0f4054 2021-06-17 stsp if (error == NULL)
6332 1d0f4054 2021-06-17 stsp error = close_err;
6333 1d0f4054 2021-06-17 stsp }
6334 7cd52833 2022-06-23 thomas if (pack_fds) {
6335 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6336 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6337 7cd52833 2022-06-23 thomas if (error == NULL)
6338 7cd52833 2022-06-23 thomas error = pack_err;
6339 7cd52833 2022-06-23 thomas }
6340 51a10b52 2020-12-26 stsp tog_free_refs();
6341 a70480e0 2018-06-23 stsp return error;
6342 ffd1d5e5 2018-06-23 stsp }
6343 ffd1d5e5 2018-06-23 stsp
6344 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6345 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6346 ffd1d5e5 2018-06-23 stsp {
6347 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6348 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6349 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6350 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6351 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6352 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6353 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6354 ffd1d5e5 2018-06-23 stsp
6355 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6356 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6357 a5d43cac 2022-07-01 thomas --limit; /* border */
6358 ffd1d5e5 2018-06-23 stsp
6359 f7d12f7e 2018-08-01 stsp werase(view->window);
6360 ffd1d5e5 2018-06-23 stsp
6361 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6362 ffd1d5e5 2018-06-23 stsp return NULL;
6363 ffd1d5e5 2018-06-23 stsp
6364 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6365 f91a2b48 2022-06-23 thomas 0, 0);
6366 ffd1d5e5 2018-06-23 stsp if (err)
6367 ffd1d5e5 2018-06-23 stsp return err;
6368 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6369 a3404814 2018-09-02 stsp wstandout(view->window);
6370 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6371 11b20872 2019-11-08 stsp if (tc)
6372 11b20872 2019-11-08 stsp wattr_on(view->window,
6373 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6374 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6375 11b20872 2019-11-08 stsp if (tc)
6376 11b20872 2019-11-08 stsp wattr_off(view->window,
6377 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6378 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6379 a3404814 2018-09-02 stsp wstandend(view->window);
6380 2550e4c3 2018-07-13 stsp free(wline);
6381 2550e4c3 2018-07-13 stsp wline = NULL;
6382 07dd3ed3 2022-08-06 thomas
6383 07dd3ed3 2022-08-06 thomas if (s->selected_entry) {
6384 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(s->selected_entry);
6385 07dd3ed3 2022-08-06 thomas i += s->tree == s->root ? 1 : 2; /* account for ".." entry */
6386 07dd3ed3 2022-08-06 thomas }
6387 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6388 07dd3ed3 2022-08-06 thomas wprintw(view->window, " [%d/%d]", i,
6389 07dd3ed3 2022-08-06 thomas nentries + (s->tree == s->root ? 0 : 1)); /* ".." in !root tree */
6390 07dd3ed3 2022-08-06 thomas
6391 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6392 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6393 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6394 ffd1d5e5 2018-06-23 stsp return NULL;
6395 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6396 f91a2b48 2022-06-23 thomas 0, 0);
6397 ce52c690 2018-06-23 stsp if (err)
6398 ce52c690 2018-06-23 stsp return err;
6399 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6400 2550e4c3 2018-07-13 stsp free(wline);
6401 2550e4c3 2018-07-13 stsp wline = NULL;
6402 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6403 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6404 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6405 ffd1d5e5 2018-06-23 stsp return NULL;
6406 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6407 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6408 a1eca9bb 2018-06-23 stsp return NULL;
6409 ffd1d5e5 2018-06-23 stsp
6410 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6411 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6412 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6413 0cf4efb1 2018-09-29 stsp if (view->focussed)
6414 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6415 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6416 ffd1d5e5 2018-06-23 stsp }
6417 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6418 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6419 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6420 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6421 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6422 ffd1d5e5 2018-06-23 stsp return NULL;
6423 ffd1d5e5 2018-06-23 stsp n = 1;
6424 ffd1d5e5 2018-06-23 stsp } else {
6425 ffd1d5e5 2018-06-23 stsp n = 0;
6426 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6427 ffd1d5e5 2018-06-23 stsp }
6428 ffd1d5e5 2018-06-23 stsp
6429 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6430 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6431 848d6979 2019-08-12 stsp const char *modestr = "";
6432 56e0773d 2019-11-28 stsp mode_t mode;
6433 1d13200f 2018-07-12 stsp
6434 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6435 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6436 56e0773d 2019-11-28 stsp
6437 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6438 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6439 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6440 1d13200f 2018-07-12 stsp if (err)
6441 638f9024 2019-05-13 stsp return got_error_from_errno(
6442 230a42bd 2019-05-11 jcs "got_object_id_str");
6443 1d13200f 2018-07-12 stsp }
6444 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6445 63c5ca5d 2019-08-24 stsp modestr = "$";
6446 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6447 0d6c6ee3 2020-05-20 stsp int i;
6448 0d6c6ee3 2020-05-20 stsp
6449 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6450 d86d3b18 2020-12-01 naddy te, s->repo);
6451 0d6c6ee3 2020-05-20 stsp if (err) {
6452 0d6c6ee3 2020-05-20 stsp free(id_str);
6453 0d6c6ee3 2020-05-20 stsp return err;
6454 0d6c6ee3 2020-05-20 stsp }
6455 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6456 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6457 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6458 0d6c6ee3 2020-05-20 stsp }
6459 848d6979 2019-08-12 stsp modestr = "@";
6460 0d6c6ee3 2020-05-20 stsp }
6461 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6462 848d6979 2019-08-12 stsp modestr = "/";
6463 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6464 848d6979 2019-08-12 stsp modestr = "*";
6465 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6466 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6467 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6468 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6469 1d13200f 2018-07-12 stsp free(id_str);
6470 0d6c6ee3 2020-05-20 stsp free(link_target);
6471 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6472 1d13200f 2018-07-12 stsp }
6473 1d13200f 2018-07-12 stsp free(id_str);
6474 0d6c6ee3 2020-05-20 stsp free(link_target);
6475 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6476 f91a2b48 2022-06-23 thomas 0, 0);
6477 ffd1d5e5 2018-06-23 stsp if (err) {
6478 ffd1d5e5 2018-06-23 stsp free(line);
6479 ffd1d5e5 2018-06-23 stsp break;
6480 ffd1d5e5 2018-06-23 stsp }
6481 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6482 0cf4efb1 2018-09-29 stsp if (view->focussed)
6483 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6484 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6485 ffd1d5e5 2018-06-23 stsp }
6486 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6487 f26dddb7 2019-11-08 stsp if (tc)
6488 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6489 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6490 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6491 f26dddb7 2019-11-08 stsp if (tc)
6492 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6493 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6494 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6495 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6496 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6497 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6498 ffd1d5e5 2018-06-23 stsp free(line);
6499 2550e4c3 2018-07-13 stsp free(wline);
6500 2550e4c3 2018-07-13 stsp wline = NULL;
6501 ffd1d5e5 2018-06-23 stsp n++;
6502 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6503 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6504 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6505 ffd1d5e5 2018-06-23 stsp break;
6506 ffd1d5e5 2018-06-23 stsp }
6507 ffd1d5e5 2018-06-23 stsp
6508 ffd1d5e5 2018-06-23 stsp return err;
6509 ffd1d5e5 2018-06-23 stsp }
6510 ffd1d5e5 2018-06-23 stsp
6511 ffd1d5e5 2018-06-23 stsp static void
6512 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6513 ffd1d5e5 2018-06-23 stsp {
6514 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6515 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6516 fa86c4bf 2020-11-29 stsp int i = 0;
6517 ffd1d5e5 2018-06-23 stsp
6518 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6519 ffd1d5e5 2018-06-23 stsp return;
6520 ffd1d5e5 2018-06-23 stsp
6521 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6522 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6523 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6524 fa86c4bf 2020-11-29 stsp if (!isroot)
6525 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6526 fa86c4bf 2020-11-29 stsp break;
6527 fa86c4bf 2020-11-29 stsp }
6528 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6529 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6530 ffd1d5e5 2018-06-23 stsp }
6531 ffd1d5e5 2018-06-23 stsp }
6532 ffd1d5e5 2018-06-23 stsp
6533 a5d43cac 2022-07-01 thomas static const struct got_error *
6534 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6535 ffd1d5e5 2018-06-23 stsp {
6536 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6537 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6538 ffd1d5e5 2018-06-23 stsp int n = 0;
6539 ffd1d5e5 2018-06-23 stsp
6540 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6541 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6542 694d3271 2020-12-01 naddy s->first_displayed_entry);
6543 694d3271 2020-12-01 naddy else
6544 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6545 56e0773d 2019-11-28 stsp
6546 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6547 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6548 07dd3ed3 2022-08-06 thomas if (last) {
6549 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6550 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6551 07dd3ed3 2022-08-06 thomas }
6552 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6553 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6554 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6555 768394f3 2019-01-24 stsp }
6556 ffd1d5e5 2018-06-23 stsp }
6557 a5d43cac 2022-07-01 thomas
6558 a5d43cac 2022-07-01 thomas return NULL;
6559 ffd1d5e5 2018-06-23 stsp }
6560 ffd1d5e5 2018-06-23 stsp
6561 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6562 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6563 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6564 ffd1d5e5 2018-06-23 stsp {
6565 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6566 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6567 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6568 ffd1d5e5 2018-06-23 stsp
6569 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6570 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6571 56e0773d 2019-11-28 stsp + 1 /* slash */;
6572 ce52c690 2018-06-23 stsp if (te)
6573 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6574 ce52c690 2018-06-23 stsp
6575 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6576 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6577 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6578 ffd1d5e5 2018-06-23 stsp
6579 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6580 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6581 d9765a41 2018-06-23 stsp while (pt) {
6582 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6583 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6584 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6585 cb2ebc8a 2018-06-23 stsp goto done;
6586 cb2ebc8a 2018-06-23 stsp }
6587 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6588 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6589 cb2ebc8a 2018-06-23 stsp goto done;
6590 cb2ebc8a 2018-06-23 stsp }
6591 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6592 ffd1d5e5 2018-06-23 stsp }
6593 ce52c690 2018-06-23 stsp if (te) {
6594 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6595 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6596 ce52c690 2018-06-23 stsp goto done;
6597 ce52c690 2018-06-23 stsp }
6598 cb2ebc8a 2018-06-23 stsp }
6599 ce52c690 2018-06-23 stsp done:
6600 ce52c690 2018-06-23 stsp if (err) {
6601 ce52c690 2018-06-23 stsp free(*path);
6602 ce52c690 2018-06-23 stsp *path = NULL;
6603 ce52c690 2018-06-23 stsp }
6604 ce52c690 2018-06-23 stsp return err;
6605 ce52c690 2018-06-23 stsp }
6606 ce52c690 2018-06-23 stsp
6607 ce52c690 2018-06-23 stsp static const struct got_error *
6608 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6609 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6610 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6611 ce52c690 2018-06-23 stsp {
6612 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6613 ce52c690 2018-06-23 stsp char *path;
6614 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6615 a0de39f3 2019-08-09 stsp
6616 a0de39f3 2019-08-09 stsp *new_view = NULL;
6617 69efd4c4 2018-07-18 stsp
6618 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6619 ce52c690 2018-06-23 stsp if (err)
6620 ce52c690 2018-06-23 stsp return err;
6621 ffd1d5e5 2018-06-23 stsp
6622 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6623 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6624 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6625 83ce39e3 2019-08-12 stsp goto done;
6626 83ce39e3 2019-08-12 stsp }
6627 cdf1ee82 2018-08-01 stsp
6628 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6629 e5a0f69f 2018-08-18 stsp if (err) {
6630 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6631 fc06ba56 2019-08-22 stsp err = NULL;
6632 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6633 e5a0f69f 2018-08-18 stsp } else
6634 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6635 83ce39e3 2019-08-12 stsp done:
6636 83ce39e3 2019-08-12 stsp free(path);
6637 69efd4c4 2018-07-18 stsp return err;
6638 69efd4c4 2018-07-18 stsp }
6639 69efd4c4 2018-07-18 stsp
6640 69efd4c4 2018-07-18 stsp static const struct got_error *
6641 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6642 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6643 69efd4c4 2018-07-18 stsp {
6644 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6645 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6646 69efd4c4 2018-07-18 stsp char *path;
6647 a0de39f3 2019-08-09 stsp
6648 a0de39f3 2019-08-09 stsp *new_view = NULL;
6649 69efd4c4 2018-07-18 stsp
6650 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6651 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6652 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6653 e5a0f69f 2018-08-18 stsp
6654 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6655 69efd4c4 2018-07-18 stsp if (err)
6656 69efd4c4 2018-07-18 stsp return err;
6657 69efd4c4 2018-07-18 stsp
6658 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6659 4e97c21c 2020-12-06 stsp path, 0);
6660 ba4f502b 2018-08-04 stsp if (err)
6661 e5a0f69f 2018-08-18 stsp view_close(log_view);
6662 e5a0f69f 2018-08-18 stsp else
6663 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6664 cb2ebc8a 2018-06-23 stsp free(path);
6665 cb2ebc8a 2018-06-23 stsp return err;
6666 ffd1d5e5 2018-06-23 stsp }
6667 ffd1d5e5 2018-06-23 stsp
6668 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6669 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6670 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6671 ffd1d5e5 2018-06-23 stsp {
6672 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6673 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6674 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6675 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6676 ffd1d5e5 2018-06-23 stsp
6677 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6678 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6679 bc573f3b 2021-07-10 stsp
6680 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6681 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6682 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6683 ffd1d5e5 2018-06-23 stsp
6684 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6685 bc573f3b 2021-07-10 stsp if (err)
6686 bc573f3b 2021-07-10 stsp goto done;
6687 bc573f3b 2021-07-10 stsp
6688 bc573f3b 2021-07-10 stsp /*
6689 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6690 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6691 bc573f3b 2021-07-10 stsp * closed on demand.
6692 bc573f3b 2021-07-10 stsp */
6693 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6694 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6695 bc573f3b 2021-07-10 stsp if (err)
6696 bc573f3b 2021-07-10 stsp goto done;
6697 bc573f3b 2021-07-10 stsp s->tree = s->root;
6698 bc573f3b 2021-07-10 stsp
6699 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6700 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6701 ffd1d5e5 2018-06-23 stsp goto done;
6702 ffd1d5e5 2018-06-23 stsp
6703 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6704 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6705 ffd1d5e5 2018-06-23 stsp goto done;
6706 ffd1d5e5 2018-06-23 stsp }
6707 ffd1d5e5 2018-06-23 stsp
6708 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6709 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6710 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6711 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6712 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6713 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6714 9cd7cbd1 2020-12-07 stsp goto done;
6715 9cd7cbd1 2020-12-07 stsp }
6716 9cd7cbd1 2020-12-07 stsp }
6717 fb2756b9 2018-08-04 stsp s->repo = repo;
6718 c0b01bdb 2019-11-08 stsp
6719 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6720 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6721 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6722 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6723 c0b01bdb 2019-11-08 stsp if (err)
6724 c0b01bdb 2019-11-08 stsp goto done;
6725 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6726 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6727 bc573f3b 2021-07-10 stsp if (err)
6728 c0b01bdb 2019-11-08 stsp goto done;
6729 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6730 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6731 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6732 bc573f3b 2021-07-10 stsp if (err)
6733 c0b01bdb 2019-11-08 stsp goto done;
6734 e5a0f69f 2018-08-18 stsp
6735 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6736 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6737 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6738 bc573f3b 2021-07-10 stsp if (err)
6739 11b20872 2019-11-08 stsp goto done;
6740 11b20872 2019-11-08 stsp
6741 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6742 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6743 bc573f3b 2021-07-10 stsp if (err)
6744 c0b01bdb 2019-11-08 stsp goto done;
6745 c0b01bdb 2019-11-08 stsp }
6746 c0b01bdb 2019-11-08 stsp
6747 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6748 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6749 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6750 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6751 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6752 ad80ab7b 2018-08-04 stsp done:
6753 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6754 bc573f3b 2021-07-10 stsp if (commit)
6755 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6756 bc573f3b 2021-07-10 stsp if (err)
6757 bc573f3b 2021-07-10 stsp close_tree_view(view);
6758 ad80ab7b 2018-08-04 stsp return err;
6759 ad80ab7b 2018-08-04 stsp }
6760 ad80ab7b 2018-08-04 stsp
6761 e5a0f69f 2018-08-18 stsp static const struct got_error *
6762 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6763 ad80ab7b 2018-08-04 stsp {
6764 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6765 ad80ab7b 2018-08-04 stsp
6766 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6767 fb2756b9 2018-08-04 stsp free(s->tree_label);
6768 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6769 6484ec90 2018-09-29 stsp free(s->commit_id);
6770 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6771 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6772 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6773 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6774 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6775 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6776 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6777 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6778 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6779 ad80ab7b 2018-08-04 stsp free(parent);
6780 ad80ab7b 2018-08-04 stsp
6781 ad80ab7b 2018-08-04 stsp }
6782 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6783 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6784 bc573f3b 2021-07-10 stsp if (s->root)
6785 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6786 7c32bd05 2019-06-22 stsp return NULL;
6787 7c32bd05 2019-06-22 stsp }
6788 7c32bd05 2019-06-22 stsp
6789 7c32bd05 2019-06-22 stsp static const struct got_error *
6790 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6791 7c32bd05 2019-06-22 stsp {
6792 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6793 7c32bd05 2019-06-22 stsp
6794 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6795 7c32bd05 2019-06-22 stsp return NULL;
6796 7c32bd05 2019-06-22 stsp }
6797 7c32bd05 2019-06-22 stsp
6798 7c32bd05 2019-06-22 stsp static int
6799 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6800 7c32bd05 2019-06-22 stsp {
6801 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6802 7c32bd05 2019-06-22 stsp
6803 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6804 56e0773d 2019-11-28 stsp 0) == 0;
6805 7c32bd05 2019-06-22 stsp }
6806 7c32bd05 2019-06-22 stsp
6807 7c32bd05 2019-06-22 stsp static const struct got_error *
6808 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6809 7c32bd05 2019-06-22 stsp {
6810 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6811 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6812 7c32bd05 2019-06-22 stsp
6813 7c32bd05 2019-06-22 stsp if (!view->searching) {
6814 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6815 7c32bd05 2019-06-22 stsp return NULL;
6816 7c32bd05 2019-06-22 stsp }
6817 7c32bd05 2019-06-22 stsp
6818 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6819 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6820 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6821 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6822 56e0773d 2019-11-28 stsp s->selected_entry);
6823 7c32bd05 2019-06-22 stsp else
6824 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6825 56e0773d 2019-11-28 stsp } else {
6826 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6827 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6828 56e0773d 2019-11-28 stsp else
6829 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6830 56e0773d 2019-11-28 stsp s->selected_entry);
6831 7c32bd05 2019-06-22 stsp }
6832 7c32bd05 2019-06-22 stsp } else {
6833 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6834 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6835 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6836 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6837 56e0773d 2019-11-28 stsp else
6838 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6839 7c32bd05 2019-06-22 stsp }
6840 7c32bd05 2019-06-22 stsp
6841 7c32bd05 2019-06-22 stsp while (1) {
6842 56e0773d 2019-11-28 stsp if (te == NULL) {
6843 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6844 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6845 ac66afb8 2019-06-24 stsp return NULL;
6846 ac66afb8 2019-06-24 stsp }
6847 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6848 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6849 56e0773d 2019-11-28 stsp else
6850 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6851 7c32bd05 2019-06-22 stsp }
6852 7c32bd05 2019-06-22 stsp
6853 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6854 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6855 56e0773d 2019-11-28 stsp s->matched_entry = te;
6856 7c32bd05 2019-06-22 stsp break;
6857 7c32bd05 2019-06-22 stsp }
6858 7c32bd05 2019-06-22 stsp
6859 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6860 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6861 56e0773d 2019-11-28 stsp else
6862 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6863 7c32bd05 2019-06-22 stsp }
6864 e5a0f69f 2018-08-18 stsp
6865 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6866 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6867 7c32bd05 2019-06-22 stsp s->selected = 0;
6868 7c32bd05 2019-06-22 stsp }
6869 7c32bd05 2019-06-22 stsp
6870 e5a0f69f 2018-08-18 stsp return NULL;
6871 ad80ab7b 2018-08-04 stsp }
6872 ad80ab7b 2018-08-04 stsp
6873 ad80ab7b 2018-08-04 stsp static const struct got_error *
6874 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6875 ad80ab7b 2018-08-04 stsp {
6876 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6877 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6878 e5a0f69f 2018-08-18 stsp char *parent_path;
6879 ad80ab7b 2018-08-04 stsp
6880 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6881 e5a0f69f 2018-08-18 stsp if (err)
6882 e5a0f69f 2018-08-18 stsp return err;
6883 ffd1d5e5 2018-06-23 stsp
6884 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6885 e5a0f69f 2018-08-18 stsp free(parent_path);
6886 669b5ffa 2018-10-07 stsp
6887 a5d43cac 2022-07-01 thomas view_border(view);
6888 e5a0f69f 2018-08-18 stsp return err;
6889 07dd3ed3 2022-08-06 thomas }
6890 07dd3ed3 2022-08-06 thomas
6891 07dd3ed3 2022-08-06 thomas static const struct got_error *
6892 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
6893 07dd3ed3 2022-08-06 thomas {
6894 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
6895 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
6896 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
6897 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
6898 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
6899 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
6900 07dd3ed3 2022-08-06 thomas
6901 07dd3ed3 2022-08-06 thomas g = view->gline;
6902 07dd3ed3 2022-08-06 thomas view->gline = 0;
6903 07dd3ed3 2022-08-06 thomas
6904 07dd3ed3 2022-08-06 thomas if (g == 0)
6905 07dd3ed3 2022-08-06 thomas g = 1;
6906 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
6907 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6908 07dd3ed3 2022-08-06 thomas
6909 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
6910 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
6911 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
6912 07dd3ed3 2022-08-06 thomas
6913 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
6914 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6915 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
6916 07dd3ed3 2022-08-06 thomas }
6917 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
6918 07dd3ed3 2022-08-06 thomas last += off;
6919 07dd3ed3 2022-08-06 thomas
6920 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
6921 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6922 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
6923 07dd3ed3 2022-08-06 thomas }
6924 07dd3ed3 2022-08-06 thomas
6925 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
6926 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
6927 07dd3ed3 2022-08-06 thomas i += off;
6928 07dd3ed3 2022-08-06 thomas }
6929 07dd3ed3 2022-08-06 thomas
6930 07dd3ed3 2022-08-06 thomas if (i < g) {
6931 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
6932 07dd3ed3 2022-08-06 thomas if (err)
6933 07dd3ed3 2022-08-06 thomas return err;
6934 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
6935 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
6936 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
6937 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
6938 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
6939 07dd3ed3 2022-08-06 thomas first += off;
6940 07dd3ed3 2022-08-06 thomas s->selected = g - first;
6941 07dd3ed3 2022-08-06 thomas }
6942 07dd3ed3 2022-08-06 thomas } else if (i > g)
6943 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
6944 07dd3ed3 2022-08-06 thomas
6945 07dd3ed3 2022-08-06 thomas if (g < nlines &&
6946 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6947 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
6948 07dd3ed3 2022-08-06 thomas
6949 07dd3ed3 2022-08-06 thomas return NULL;
6950 e5a0f69f 2018-08-18 stsp }
6951 ce52c690 2018-06-23 stsp
6952 e5a0f69f 2018-08-18 stsp static const struct got_error *
6953 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6954 e5a0f69f 2018-08-18 stsp {
6955 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6956 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6957 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6958 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
6959 ffd1d5e5 2018-06-23 stsp
6960 07dd3ed3 2022-08-06 thomas if (view->gline)
6961 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
6962 07dd3ed3 2022-08-06 thomas
6963 e5a0f69f 2018-08-18 stsp switch (ch) {
6964 1e37a5c2 2019-05-12 jcs case 'i':
6965 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6966 07b0611c 2022-06-23 thomas view->count = 0;
6967 1e37a5c2 2019-05-12 jcs break;
6968 1be4947a 2022-07-22 thomas case 'L':
6969 07b0611c 2022-06-23 thomas view->count = 0;
6970 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6971 ffd1d5e5 2018-06-23 stsp break;
6972 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6973 152c1c93 2020-11-29 stsp break;
6974 1be4947a 2022-07-22 thomas case 'R':
6975 07b0611c 2022-06-23 thomas view->count = 0;
6976 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
6977 e4526bf5 2021-09-03 naddy break;
6978 e4526bf5 2021-09-03 naddy case 'g':
6979 e4526bf5 2021-09-03 naddy case KEY_HOME:
6980 e4526bf5 2021-09-03 naddy s->selected = 0;
6981 07b0611c 2022-06-23 thomas view->count = 0;
6982 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6983 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6984 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6985 e4526bf5 2021-09-03 naddy else
6986 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6987 1e37a5c2 2019-05-12 jcs break;
6988 e4526bf5 2021-09-03 naddy case 'G':
6989 a5d43cac 2022-07-01 thomas case KEY_END: {
6990 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6991 a5d43cac 2022-07-01 thomas
6992 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6993 a5d43cac 2022-07-01 thomas --eos; /* border */
6994 e4526bf5 2021-09-03 naddy s->selected = 0;
6995 07b0611c 2022-06-23 thomas view->count = 0;
6996 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6997 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6998 e4526bf5 2021-09-03 naddy if (te == NULL) {
6999 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7000 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7001 e4526bf5 2021-09-03 naddy n++;
7002 e4526bf5 2021-09-03 naddy }
7003 e4526bf5 2021-09-03 naddy break;
7004 e4526bf5 2021-09-03 naddy }
7005 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7006 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7007 e4526bf5 2021-09-03 naddy }
7008 e4526bf5 2021-09-03 naddy if (n > 0)
7009 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7010 e4526bf5 2021-09-03 naddy break;
7011 a5d43cac 2022-07-01 thomas }
7012 1e37a5c2 2019-05-12 jcs case 'k':
7013 1e37a5c2 2019-05-12 jcs case KEY_UP:
7014 f7140bf5 2021-10-17 thomas case CTRL('p'):
7015 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7016 1e37a5c2 2019-05-12 jcs s->selected--;
7017 fa86c4bf 2020-11-29 stsp break;
7018 1e37a5c2 2019-05-12 jcs }
7019 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7020 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7021 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7022 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7023 07b0611c 2022-06-23 thomas view->count = 0;
7024 1e37a5c2 2019-05-12 jcs break;
7025 70f17a53 2022-06-13 thomas case CTRL('u'):
7026 23427b14 2022-06-23 thomas case 'u':
7027 70f17a53 2022-06-13 thomas nscroll /= 2;
7028 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7029 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7030 ea025d1d 2020-02-22 naddy case CTRL('b'):
7031 1c5e5faa 2022-06-23 thomas case 'b':
7032 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7033 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7034 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7035 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7036 fa86c4bf 2020-11-29 stsp } else {
7037 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7038 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7039 fa86c4bf 2020-11-29 stsp }
7040 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7041 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7042 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7043 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7044 07b0611c 2022-06-23 thomas view->count = 0;
7045 1e37a5c2 2019-05-12 jcs break;
7046 1e37a5c2 2019-05-12 jcs case 'j':
7047 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7048 f7140bf5 2021-10-17 thomas case CTRL('n'):
7049 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7050 1e37a5c2 2019-05-12 jcs s->selected++;
7051 1e37a5c2 2019-05-12 jcs break;
7052 1e37a5c2 2019-05-12 jcs }
7053 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7054 07b0611c 2022-06-23 thomas == NULL) {
7055 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7056 07b0611c 2022-06-23 thomas view->count = 0;
7057 1e37a5c2 2019-05-12 jcs break;
7058 07b0611c 2022-06-23 thomas }
7059 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7060 1e37a5c2 2019-05-12 jcs break;
7061 70f17a53 2022-06-13 thomas case CTRL('d'):
7062 23427b14 2022-06-23 thomas case 'd':
7063 70f17a53 2022-06-13 thomas nscroll /= 2;
7064 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7065 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7066 ea025d1d 2020-02-22 naddy case CTRL('f'):
7067 1c5e5faa 2022-06-23 thomas case 'f':
7068 4c2d69cb 2022-06-23 thomas case ' ':
7069 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7070 1e37a5c2 2019-05-12 jcs == NULL) {
7071 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7072 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7073 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7074 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7075 07b0611c 2022-06-23 thomas else
7076 07b0611c 2022-06-23 thomas view->count = 0;
7077 1e37a5c2 2019-05-12 jcs break;
7078 1e37a5c2 2019-05-12 jcs }
7079 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7080 1e37a5c2 2019-05-12 jcs break;
7081 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7082 1e37a5c2 2019-05-12 jcs case '\r':
7083 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7084 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7085 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7086 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7087 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7088 07b0611c 2022-06-23 thomas view->count = 0;
7089 1e37a5c2 2019-05-12 jcs break;
7090 07b0611c 2022-06-23 thomas }
7091 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7092 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7093 1e37a5c2 2019-05-12 jcs entry);
7094 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7095 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7096 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7097 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7098 1e37a5c2 2019-05-12 jcs s->selected_entry =
7099 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7100 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7101 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7102 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7103 a5d43cac 2022-07-01 thomas if (err)
7104 a5d43cac 2022-07-01 thomas break;
7105 a5d43cac 2022-07-01 thomas }
7106 1e37a5c2 2019-05-12 jcs free(parent);
7107 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7108 56e0773d 2019-11-28 stsp s->selected_entry))) {
7109 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7110 07b0611c 2022-06-23 thomas view->count = 0;
7111 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7112 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7113 1e37a5c2 2019-05-12 jcs if (err)
7114 1e37a5c2 2019-05-12 jcs break;
7115 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7116 941e9f74 2019-05-21 stsp if (err) {
7117 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7118 1e37a5c2 2019-05-12 jcs break;
7119 1e37a5c2 2019-05-12 jcs }
7120 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7121 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7122 1e37a5c2 2019-05-12 jcs break;
7123 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7124 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7125 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7126 07b0611c 2022-06-23 thomas view->count = 0;
7127 1e37a5c2 2019-05-12 jcs break;
7128 1e37a5c2 2019-05-12 jcs default:
7129 07b0611c 2022-06-23 thomas view->count = 0;
7130 1e37a5c2 2019-05-12 jcs break;
7131 ffd1d5e5 2018-06-23 stsp }
7132 e5a0f69f 2018-08-18 stsp
7133 ffd1d5e5 2018-06-23 stsp return err;
7134 9f7d7167 2018-04-29 stsp }
7135 9f7d7167 2018-04-29 stsp
7136 ffd1d5e5 2018-06-23 stsp __dead static void
7137 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7138 ffd1d5e5 2018-06-23 stsp {
7139 ffd1d5e5 2018-06-23 stsp endwin();
7140 91198554 2022-06-23 thomas fprintf(stderr,
7141 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7142 ffd1d5e5 2018-06-23 stsp getprogname());
7143 ffd1d5e5 2018-06-23 stsp exit(1);
7144 ffd1d5e5 2018-06-23 stsp }
7145 ffd1d5e5 2018-06-23 stsp
7146 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7147 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7148 ffd1d5e5 2018-06-23 stsp {
7149 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7150 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7151 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7152 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7153 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7154 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7155 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7156 4e97c21c 2020-12-06 stsp char *label = NULL;
7157 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7158 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7159 ffd1d5e5 2018-06-23 stsp int ch;
7160 5221c383 2018-08-01 stsp struct tog_view *view;
7161 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7162 70ac5f84 2019-03-28 stsp
7163 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7164 ffd1d5e5 2018-06-23 stsp switch (ch) {
7165 ffd1d5e5 2018-06-23 stsp case 'c':
7166 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7167 74283ab8 2020-02-07 stsp break;
7168 74283ab8 2020-02-07 stsp case 'r':
7169 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7170 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7171 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7172 74283ab8 2020-02-07 stsp optarg);
7173 ffd1d5e5 2018-06-23 stsp break;
7174 ffd1d5e5 2018-06-23 stsp default:
7175 e99e2d15 2020-11-24 naddy usage_tree();
7176 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7177 ffd1d5e5 2018-06-23 stsp }
7178 ffd1d5e5 2018-06-23 stsp }
7179 ffd1d5e5 2018-06-23 stsp
7180 ffd1d5e5 2018-06-23 stsp argc -= optind;
7181 ffd1d5e5 2018-06-23 stsp argv += optind;
7182 ffd1d5e5 2018-06-23 stsp
7183 55cccc34 2020-02-20 stsp if (argc > 1)
7184 e99e2d15 2020-11-24 naddy usage_tree();
7185 7cd52833 2022-06-23 thomas
7186 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7187 7cd52833 2022-06-23 thomas if (error != NULL)
7188 7cd52833 2022-06-23 thomas goto done;
7189 74283ab8 2020-02-07 stsp
7190 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7191 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7192 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7193 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7194 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7195 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7196 c156c7a4 2020-12-18 stsp goto done;
7197 55cccc34 2020-02-20 stsp if (worktree)
7198 52185f70 2019-02-05 stsp repo_path =
7199 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7200 55cccc34 2020-02-20 stsp else
7201 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7202 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7203 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7204 c156c7a4 2020-12-18 stsp goto done;
7205 c156c7a4 2020-12-18 stsp }
7206 55cccc34 2020-02-20 stsp }
7207 a915003a 2019-02-05 stsp
7208 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7209 c02c541e 2019-03-29 stsp if (error != NULL)
7210 52185f70 2019-02-05 stsp goto done;
7211 d188b9a6 2019-01-04 stsp
7212 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7213 55cccc34 2020-02-20 stsp repo, worktree);
7214 55cccc34 2020-02-20 stsp if (error)
7215 55cccc34 2020-02-20 stsp goto done;
7216 55cccc34 2020-02-20 stsp
7217 55cccc34 2020-02-20 stsp init_curses();
7218 55cccc34 2020-02-20 stsp
7219 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7220 c02c541e 2019-03-29 stsp if (error)
7221 52185f70 2019-02-05 stsp goto done;
7222 ffd1d5e5 2018-06-23 stsp
7223 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7224 51a10b52 2020-12-26 stsp if (error)
7225 51a10b52 2020-12-26 stsp goto done;
7226 51a10b52 2020-12-26 stsp
7227 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7228 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7229 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7230 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7231 4e97c21c 2020-12-06 stsp if (error)
7232 4e97c21c 2020-12-06 stsp goto done;
7233 4e97c21c 2020-12-06 stsp head_ref_name = label;
7234 4e97c21c 2020-12-06 stsp } else {
7235 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7236 4e97c21c 2020-12-06 stsp if (error == NULL)
7237 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7238 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7239 4e97c21c 2020-12-06 stsp goto done;
7240 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7241 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7242 4e97c21c 2020-12-06 stsp if (error)
7243 4e97c21c 2020-12-06 stsp goto done;
7244 4e97c21c 2020-12-06 stsp }
7245 ffd1d5e5 2018-06-23 stsp
7246 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7247 945f9229 2022-04-16 thomas if (error)
7248 945f9229 2022-04-16 thomas goto done;
7249 945f9229 2022-04-16 thomas
7250 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7251 5221c383 2018-08-01 stsp if (view == NULL) {
7252 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7253 5221c383 2018-08-01 stsp goto done;
7254 5221c383 2018-08-01 stsp }
7255 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7256 ad80ab7b 2018-08-04 stsp if (error)
7257 ad80ab7b 2018-08-04 stsp goto done;
7258 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7259 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7260 d91faf3b 2020-12-01 naddy in_repo_path);
7261 55cccc34 2020-02-20 stsp if (error)
7262 55cccc34 2020-02-20 stsp goto done;
7263 55cccc34 2020-02-20 stsp }
7264 55cccc34 2020-02-20 stsp
7265 55cccc34 2020-02-20 stsp if (worktree) {
7266 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7267 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7268 55cccc34 2020-02-20 stsp worktree = NULL;
7269 55cccc34 2020-02-20 stsp }
7270 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7271 ffd1d5e5 2018-06-23 stsp done:
7272 52185f70 2019-02-05 stsp free(repo_path);
7273 e4a0e26d 2020-02-20 stsp free(cwd);
7274 ffd1d5e5 2018-06-23 stsp free(commit_id);
7275 4e97c21c 2020-12-06 stsp free(label);
7276 486cd271 2020-12-06 stsp if (ref)
7277 486cd271 2020-12-06 stsp got_ref_close(ref);
7278 1d0f4054 2021-06-17 stsp if (repo) {
7279 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7280 1d0f4054 2021-06-17 stsp if (error == NULL)
7281 1d0f4054 2021-06-17 stsp error = close_err;
7282 1d0f4054 2021-06-17 stsp }
7283 7cd52833 2022-06-23 thomas if (pack_fds) {
7284 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7285 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7286 7cd52833 2022-06-23 thomas if (error == NULL)
7287 7cd52833 2022-06-23 thomas error = pack_err;
7288 7cd52833 2022-06-23 thomas }
7289 51a10b52 2020-12-26 stsp tog_free_refs();
7290 ffd1d5e5 2018-06-23 stsp return error;
7291 6458efa5 2020-11-24 stsp }
7292 6458efa5 2020-11-24 stsp
7293 6458efa5 2020-11-24 stsp static const struct got_error *
7294 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7295 6458efa5 2020-11-24 stsp {
7296 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7297 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7298 6458efa5 2020-11-24 stsp
7299 6458efa5 2020-11-24 stsp s->nrefs = 0;
7300 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7301 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7302 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7303 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7304 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7305 6458efa5 2020-11-24 stsp continue;
7306 6458efa5 2020-11-24 stsp
7307 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7308 6458efa5 2020-11-24 stsp if (re == NULL)
7309 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7310 6458efa5 2020-11-24 stsp
7311 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7312 8924d611 2020-12-26 stsp if (re->ref == NULL)
7313 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7314 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7315 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7316 6458efa5 2020-11-24 stsp }
7317 6458efa5 2020-11-24 stsp
7318 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7319 6458efa5 2020-11-24 stsp return NULL;
7320 6458efa5 2020-11-24 stsp }
7321 6458efa5 2020-11-24 stsp
7322 ef20f542 2022-06-26 thomas static void
7323 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7324 6458efa5 2020-11-24 stsp {
7325 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7326 6458efa5 2020-11-24 stsp
7327 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7328 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7329 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7330 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7331 6458efa5 2020-11-24 stsp free(re);
7332 6458efa5 2020-11-24 stsp }
7333 6458efa5 2020-11-24 stsp }
7334 6458efa5 2020-11-24 stsp
7335 6458efa5 2020-11-24 stsp static const struct got_error *
7336 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7337 6458efa5 2020-11-24 stsp {
7338 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7339 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7340 6458efa5 2020-11-24 stsp
7341 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7342 6458efa5 2020-11-24 stsp s->repo = repo;
7343 6458efa5 2020-11-24 stsp
7344 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7345 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7346 6458efa5 2020-11-24 stsp
7347 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7348 6458efa5 2020-11-24 stsp if (err)
7349 6458efa5 2020-11-24 stsp return err;
7350 34ba6917 2020-11-30 stsp
7351 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7352 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7353 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7354 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7355 6458efa5 2020-11-24 stsp if (err)
7356 6458efa5 2020-11-24 stsp goto done;
7357 6458efa5 2020-11-24 stsp
7358 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7359 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7360 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7361 6458efa5 2020-11-24 stsp if (err)
7362 6458efa5 2020-11-24 stsp goto done;
7363 6458efa5 2020-11-24 stsp
7364 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7365 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7366 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7367 2183bbf6 2022-01-23 thomas if (err)
7368 2183bbf6 2022-01-23 thomas goto done;
7369 2183bbf6 2022-01-23 thomas
7370 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7371 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7372 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7373 6458efa5 2020-11-24 stsp if (err)
7374 6458efa5 2020-11-24 stsp goto done;
7375 6458efa5 2020-11-24 stsp }
7376 6458efa5 2020-11-24 stsp
7377 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7378 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7379 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7380 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7381 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7382 6458efa5 2020-11-24 stsp done:
7383 6458efa5 2020-11-24 stsp if (err)
7384 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7385 6458efa5 2020-11-24 stsp return err;
7386 6458efa5 2020-11-24 stsp }
7387 6458efa5 2020-11-24 stsp
7388 6458efa5 2020-11-24 stsp static const struct got_error *
7389 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7390 6458efa5 2020-11-24 stsp {
7391 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7392 6458efa5 2020-11-24 stsp
7393 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7394 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7395 6458efa5 2020-11-24 stsp
7396 6458efa5 2020-11-24 stsp return NULL;
7397 9f7d7167 2018-04-29 stsp }
7398 ce5b7c56 2019-07-09 stsp
7399 6458efa5 2020-11-24 stsp static const struct got_error *
7400 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7401 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7402 6458efa5 2020-11-24 stsp {
7403 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7404 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7405 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7406 6458efa5 2020-11-24 stsp int obj_type;
7407 6458efa5 2020-11-24 stsp
7408 c42c9805 2020-11-24 stsp *commit_id = NULL;
7409 6458efa5 2020-11-24 stsp
7410 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7411 6458efa5 2020-11-24 stsp if (err)
7412 6458efa5 2020-11-24 stsp return err;
7413 6458efa5 2020-11-24 stsp
7414 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7415 6458efa5 2020-11-24 stsp if (err)
7416 6458efa5 2020-11-24 stsp goto done;
7417 6458efa5 2020-11-24 stsp
7418 6458efa5 2020-11-24 stsp switch (obj_type) {
7419 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7420 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7421 6458efa5 2020-11-24 stsp break;
7422 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7423 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7424 6458efa5 2020-11-24 stsp if (err)
7425 6458efa5 2020-11-24 stsp goto done;
7426 c42c9805 2020-11-24 stsp free(obj_id);
7427 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7428 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7429 c42c9805 2020-11-24 stsp if (err)
7430 6458efa5 2020-11-24 stsp goto done;
7431 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7432 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7433 c42c9805 2020-11-24 stsp goto done;
7434 c42c9805 2020-11-24 stsp }
7435 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7436 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7437 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7438 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7439 c42c9805 2020-11-24 stsp goto done;
7440 c42c9805 2020-11-24 stsp }
7441 6458efa5 2020-11-24 stsp break;
7442 6458efa5 2020-11-24 stsp default:
7443 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7444 c42c9805 2020-11-24 stsp break;
7445 6458efa5 2020-11-24 stsp }
7446 6458efa5 2020-11-24 stsp
7447 c42c9805 2020-11-24 stsp done:
7448 c42c9805 2020-11-24 stsp if (tag)
7449 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7450 c42c9805 2020-11-24 stsp if (err) {
7451 c42c9805 2020-11-24 stsp free(*commit_id);
7452 c42c9805 2020-11-24 stsp *commit_id = NULL;
7453 c42c9805 2020-11-24 stsp }
7454 c42c9805 2020-11-24 stsp return err;
7455 c42c9805 2020-11-24 stsp }
7456 c42c9805 2020-11-24 stsp
7457 c42c9805 2020-11-24 stsp static const struct got_error *
7458 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7459 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7460 c42c9805 2020-11-24 stsp {
7461 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7462 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7463 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7464 c42c9805 2020-11-24 stsp
7465 c42c9805 2020-11-24 stsp *new_view = NULL;
7466 c42c9805 2020-11-24 stsp
7467 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7468 c42c9805 2020-11-24 stsp if (err) {
7469 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7470 c42c9805 2020-11-24 stsp return err;
7471 c42c9805 2020-11-24 stsp else
7472 c42c9805 2020-11-24 stsp return NULL;
7473 c42c9805 2020-11-24 stsp }
7474 c42c9805 2020-11-24 stsp
7475 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7476 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7477 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7478 6458efa5 2020-11-24 stsp goto done;
7479 6458efa5 2020-11-24 stsp }
7480 6458efa5 2020-11-24 stsp
7481 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7482 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7483 6458efa5 2020-11-24 stsp done:
7484 6458efa5 2020-11-24 stsp if (err)
7485 6458efa5 2020-11-24 stsp view_close(log_view);
7486 6458efa5 2020-11-24 stsp else
7487 6458efa5 2020-11-24 stsp *new_view = log_view;
7488 c42c9805 2020-11-24 stsp free(commit_id);
7489 6458efa5 2020-11-24 stsp return err;
7490 6458efa5 2020-11-24 stsp }
7491 6458efa5 2020-11-24 stsp
7492 ce5b7c56 2019-07-09 stsp static void
7493 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7494 6458efa5 2020-11-24 stsp {
7495 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7496 34ba6917 2020-11-30 stsp int i = 0;
7497 6458efa5 2020-11-24 stsp
7498 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7499 6458efa5 2020-11-24 stsp return;
7500 6458efa5 2020-11-24 stsp
7501 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7502 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7503 34ba6917 2020-11-30 stsp if (re == NULL)
7504 34ba6917 2020-11-30 stsp break;
7505 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7506 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7507 6458efa5 2020-11-24 stsp }
7508 6458efa5 2020-11-24 stsp }
7509 6458efa5 2020-11-24 stsp
7510 a5d43cac 2022-07-01 thomas static const struct got_error *
7511 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7512 6458efa5 2020-11-24 stsp {
7513 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7514 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7515 6458efa5 2020-11-24 stsp int n = 0;
7516 6458efa5 2020-11-24 stsp
7517 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7518 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7519 6458efa5 2020-11-24 stsp else
7520 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7521 6458efa5 2020-11-24 stsp
7522 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7523 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7524 07dd3ed3 2022-08-06 thomas if (last) {
7525 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7526 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7527 07dd3ed3 2022-08-06 thomas }
7528 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7529 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7530 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7531 6458efa5 2020-11-24 stsp }
7532 6458efa5 2020-11-24 stsp }
7533 a5d43cac 2022-07-01 thomas
7534 a5d43cac 2022-07-01 thomas return NULL;
7535 6458efa5 2020-11-24 stsp }
7536 6458efa5 2020-11-24 stsp
7537 6458efa5 2020-11-24 stsp static const struct got_error *
7538 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7539 6458efa5 2020-11-24 stsp {
7540 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7541 6458efa5 2020-11-24 stsp
7542 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7543 6458efa5 2020-11-24 stsp return NULL;
7544 6458efa5 2020-11-24 stsp }
7545 6458efa5 2020-11-24 stsp
7546 6458efa5 2020-11-24 stsp static int
7547 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7548 6458efa5 2020-11-24 stsp {
7549 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7550 6458efa5 2020-11-24 stsp
7551 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7552 6458efa5 2020-11-24 stsp 0) == 0;
7553 6458efa5 2020-11-24 stsp }
7554 6458efa5 2020-11-24 stsp
7555 6458efa5 2020-11-24 stsp static const struct got_error *
7556 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7557 6458efa5 2020-11-24 stsp {
7558 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7559 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7560 6458efa5 2020-11-24 stsp
7561 6458efa5 2020-11-24 stsp if (!view->searching) {
7562 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7563 6458efa5 2020-11-24 stsp return NULL;
7564 6458efa5 2020-11-24 stsp }
7565 6458efa5 2020-11-24 stsp
7566 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7567 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7568 6458efa5 2020-11-24 stsp if (s->selected_entry)
7569 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7570 6458efa5 2020-11-24 stsp else
7571 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7572 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7573 6458efa5 2020-11-24 stsp } else {
7574 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7575 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7576 6458efa5 2020-11-24 stsp else
7577 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7578 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7579 6458efa5 2020-11-24 stsp }
7580 6458efa5 2020-11-24 stsp } else {
7581 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7582 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7583 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7584 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7585 6458efa5 2020-11-24 stsp else
7586 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7587 6458efa5 2020-11-24 stsp }
7588 6458efa5 2020-11-24 stsp
7589 6458efa5 2020-11-24 stsp while (1) {
7590 6458efa5 2020-11-24 stsp if (re == NULL) {
7591 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7592 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7593 6458efa5 2020-11-24 stsp return NULL;
7594 6458efa5 2020-11-24 stsp }
7595 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7596 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7597 6458efa5 2020-11-24 stsp else
7598 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7599 6458efa5 2020-11-24 stsp }
7600 6458efa5 2020-11-24 stsp
7601 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7602 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7603 6458efa5 2020-11-24 stsp s->matched_entry = re;
7604 6458efa5 2020-11-24 stsp break;
7605 6458efa5 2020-11-24 stsp }
7606 6458efa5 2020-11-24 stsp
7607 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7608 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7609 6458efa5 2020-11-24 stsp else
7610 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7611 6458efa5 2020-11-24 stsp }
7612 6458efa5 2020-11-24 stsp
7613 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7614 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7615 6458efa5 2020-11-24 stsp s->selected = 0;
7616 6458efa5 2020-11-24 stsp }
7617 6458efa5 2020-11-24 stsp
7618 6458efa5 2020-11-24 stsp return NULL;
7619 6458efa5 2020-11-24 stsp }
7620 6458efa5 2020-11-24 stsp
7621 6458efa5 2020-11-24 stsp static const struct got_error *
7622 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7623 6458efa5 2020-11-24 stsp {
7624 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7625 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7626 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7627 6458efa5 2020-11-24 stsp char *line = NULL;
7628 6458efa5 2020-11-24 stsp wchar_t *wline;
7629 6458efa5 2020-11-24 stsp struct tog_color *tc;
7630 6458efa5 2020-11-24 stsp int width, n;
7631 6458efa5 2020-11-24 stsp int limit = view->nlines;
7632 6458efa5 2020-11-24 stsp
7633 6458efa5 2020-11-24 stsp werase(view->window);
7634 6458efa5 2020-11-24 stsp
7635 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7636 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7637 a5d43cac 2022-07-01 thomas --limit; /* border */
7638 6458efa5 2020-11-24 stsp
7639 6458efa5 2020-11-24 stsp if (limit == 0)
7640 6458efa5 2020-11-24 stsp return NULL;
7641 6458efa5 2020-11-24 stsp
7642 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7643 6458efa5 2020-11-24 stsp
7644 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7645 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7646 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7647 6458efa5 2020-11-24 stsp
7648 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7649 6458efa5 2020-11-24 stsp if (err) {
7650 6458efa5 2020-11-24 stsp free(line);
7651 6458efa5 2020-11-24 stsp return err;
7652 6458efa5 2020-11-24 stsp }
7653 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7654 6458efa5 2020-11-24 stsp wstandout(view->window);
7655 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7656 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7657 6458efa5 2020-11-24 stsp wstandend(view->window);
7658 6458efa5 2020-11-24 stsp free(wline);
7659 6458efa5 2020-11-24 stsp wline = NULL;
7660 6458efa5 2020-11-24 stsp free(line);
7661 6458efa5 2020-11-24 stsp line = NULL;
7662 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7663 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7664 6458efa5 2020-11-24 stsp if (--limit <= 0)
7665 6458efa5 2020-11-24 stsp return NULL;
7666 6458efa5 2020-11-24 stsp
7667 6458efa5 2020-11-24 stsp n = 0;
7668 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7669 6458efa5 2020-11-24 stsp char *line = NULL;
7670 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7671 6458efa5 2020-11-24 stsp
7672 84227eb1 2022-06-23 thomas if (s->show_date) {
7673 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7674 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7675 84227eb1 2022-06-23 thomas struct got_object_id *id;
7676 84227eb1 2022-06-23 thomas struct tm tm;
7677 84227eb1 2022-06-23 thomas time_t t;
7678 84227eb1 2022-06-23 thomas
7679 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7680 84227eb1 2022-06-23 thomas if (err)
7681 84227eb1 2022-06-23 thomas return err;
7682 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7683 84227eb1 2022-06-23 thomas if (err) {
7684 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7685 84227eb1 2022-06-23 thomas free(id);
7686 84227eb1 2022-06-23 thomas return err;
7687 84227eb1 2022-06-23 thomas }
7688 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7689 84227eb1 2022-06-23 thomas id);
7690 84227eb1 2022-06-23 thomas if (err) {
7691 84227eb1 2022-06-23 thomas free(id);
7692 84227eb1 2022-06-23 thomas return err;
7693 84227eb1 2022-06-23 thomas }
7694 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7695 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7696 84227eb1 2022-06-23 thomas } else {
7697 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7698 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7699 84227eb1 2022-06-23 thomas }
7700 84227eb1 2022-06-23 thomas free(id);
7701 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7702 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7703 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7704 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7705 84227eb1 2022-06-23 thomas }
7706 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7707 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7708 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7709 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7710 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7711 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7712 6458efa5 2020-11-24 stsp struct got_object_id *id;
7713 6458efa5 2020-11-24 stsp char *id_str;
7714 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7715 6458efa5 2020-11-24 stsp if (err)
7716 6458efa5 2020-11-24 stsp return err;
7717 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7718 6458efa5 2020-11-24 stsp if (err) {
7719 6458efa5 2020-11-24 stsp free(id);
7720 6458efa5 2020-11-24 stsp return err;
7721 6458efa5 2020-11-24 stsp }
7722 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7723 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7724 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7725 6458efa5 2020-11-24 stsp free(id);
7726 6458efa5 2020-11-24 stsp free(id_str);
7727 6458efa5 2020-11-24 stsp return err;
7728 6458efa5 2020-11-24 stsp }
7729 6458efa5 2020-11-24 stsp free(id);
7730 6458efa5 2020-11-24 stsp free(id_str);
7731 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7732 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7733 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7734 6458efa5 2020-11-24 stsp
7735 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7736 f91a2b48 2022-06-23 thomas 0, 0);
7737 6458efa5 2020-11-24 stsp if (err) {
7738 6458efa5 2020-11-24 stsp free(line);
7739 6458efa5 2020-11-24 stsp return err;
7740 6458efa5 2020-11-24 stsp }
7741 6458efa5 2020-11-24 stsp if (n == s->selected) {
7742 6458efa5 2020-11-24 stsp if (view->focussed)
7743 6458efa5 2020-11-24 stsp wstandout(view->window);
7744 6458efa5 2020-11-24 stsp s->selected_entry = re;
7745 6458efa5 2020-11-24 stsp }
7746 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7747 6458efa5 2020-11-24 stsp if (tc)
7748 6458efa5 2020-11-24 stsp wattr_on(view->window,
7749 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7750 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7751 6458efa5 2020-11-24 stsp if (tc)
7752 6458efa5 2020-11-24 stsp wattr_off(view->window,
7753 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7754 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7755 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7756 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7757 6458efa5 2020-11-24 stsp wstandend(view->window);
7758 6458efa5 2020-11-24 stsp free(line);
7759 6458efa5 2020-11-24 stsp free(wline);
7760 6458efa5 2020-11-24 stsp wline = NULL;
7761 6458efa5 2020-11-24 stsp n++;
7762 6458efa5 2020-11-24 stsp s->ndisplayed++;
7763 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7764 6458efa5 2020-11-24 stsp
7765 6458efa5 2020-11-24 stsp limit--;
7766 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7767 6458efa5 2020-11-24 stsp }
7768 6458efa5 2020-11-24 stsp
7769 a5d43cac 2022-07-01 thomas view_border(view);
7770 6458efa5 2020-11-24 stsp return err;
7771 6458efa5 2020-11-24 stsp }
7772 6458efa5 2020-11-24 stsp
7773 6458efa5 2020-11-24 stsp static const struct got_error *
7774 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7775 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7776 c42c9805 2020-11-24 stsp {
7777 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7778 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7779 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7780 c42c9805 2020-11-24 stsp
7781 c42c9805 2020-11-24 stsp *new_view = NULL;
7782 c42c9805 2020-11-24 stsp
7783 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7784 c42c9805 2020-11-24 stsp if (err) {
7785 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7786 c42c9805 2020-11-24 stsp return err;
7787 c42c9805 2020-11-24 stsp else
7788 c42c9805 2020-11-24 stsp return NULL;
7789 c42c9805 2020-11-24 stsp }
7790 c42c9805 2020-11-24 stsp
7791 c42c9805 2020-11-24 stsp
7792 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7793 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7794 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7795 c42c9805 2020-11-24 stsp goto done;
7796 c42c9805 2020-11-24 stsp }
7797 c42c9805 2020-11-24 stsp
7798 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7799 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7800 c42c9805 2020-11-24 stsp if (err)
7801 c42c9805 2020-11-24 stsp goto done;
7802 c42c9805 2020-11-24 stsp
7803 c42c9805 2020-11-24 stsp *new_view = tree_view;
7804 c42c9805 2020-11-24 stsp done:
7805 c42c9805 2020-11-24 stsp free(commit_id);
7806 c42c9805 2020-11-24 stsp return err;
7807 07dd3ed3 2022-08-06 thomas }
7808 07dd3ed3 2022-08-06 thomas
7809 07dd3ed3 2022-08-06 thomas static const struct got_error *
7810 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
7811 07dd3ed3 2022-08-06 thomas {
7812 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7813 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
7814 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
7815 07dd3ed3 2022-08-06 thomas
7816 07dd3ed3 2022-08-06 thomas g = view->gline;
7817 07dd3ed3 2022-08-06 thomas view->gline = 0;
7818 07dd3ed3 2022-08-06 thomas
7819 07dd3ed3 2022-08-06 thomas if (g == 0)
7820 07dd3ed3 2022-08-06 thomas g = 1;
7821 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
7822 07dd3ed3 2022-08-06 thomas g = s->nrefs;
7823 07dd3ed3 2022-08-06 thomas
7824 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
7825 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
7826 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
7827 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7828 07dd3ed3 2022-08-06 thomas return NULL;
7829 07dd3ed3 2022-08-06 thomas }
7830 07dd3ed3 2022-08-06 thomas
7831 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
7832 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
7833 07dd3ed3 2022-08-06 thomas if (err)
7834 07dd3ed3 2022-08-06 thomas return err;
7835 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7836 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
7837 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
7838 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
7839 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
7840 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
7841 07dd3ed3 2022-08-06 thomas
7842 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
7843 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7844 07dd3ed3 2022-08-06 thomas
7845 07dd3ed3 2022-08-06 thomas return NULL;
7846 07dd3ed3 2022-08-06 thomas
7847 c42c9805 2020-11-24 stsp }
7848 07dd3ed3 2022-08-06 thomas
7849 c42c9805 2020-11-24 stsp static const struct got_error *
7850 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7851 6458efa5 2020-11-24 stsp {
7852 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7853 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7854 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7855 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
7856 6458efa5 2020-11-24 stsp
7857 07dd3ed3 2022-08-06 thomas if (view->gline)
7858 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
7859 07dd3ed3 2022-08-06 thomas
7860 6458efa5 2020-11-24 stsp switch (ch) {
7861 6458efa5 2020-11-24 stsp case 'i':
7862 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7863 07b0611c 2022-06-23 thomas view->count = 0;
7864 3bfadbd4 2021-11-20 thomas break;
7865 84227eb1 2022-06-23 thomas case 'm':
7866 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7867 07b0611c 2022-06-23 thomas view->count = 0;
7868 84227eb1 2022-06-23 thomas break;
7869 98182bd0 2021-11-20 thomas case 'o':
7870 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7871 07b0611c 2022-06-23 thomas view->count = 0;
7872 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7873 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7874 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7875 c591440f 2021-11-20 thomas if (err)
7876 c591440f 2021-11-20 thomas break;
7877 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7878 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7879 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7880 3bfadbd4 2021-11-20 thomas if (err)
7881 3bfadbd4 2021-11-20 thomas break;
7882 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7883 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7884 6458efa5 2020-11-24 stsp break;
7885 6458efa5 2020-11-24 stsp case KEY_ENTER:
7886 6458efa5 2020-11-24 stsp case '\r':
7887 07b0611c 2022-06-23 thomas view->count = 0;
7888 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7889 a5d43cac 2022-07-01 thomas break;
7890 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7891 6458efa5 2020-11-24 stsp break;
7892 1be4947a 2022-07-22 thomas case 'T':
7893 07b0611c 2022-06-23 thomas view->count = 0;
7894 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7895 c42c9805 2020-11-24 stsp break;
7896 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
7897 c42c9805 2020-11-24 stsp break;
7898 e4526bf5 2021-09-03 naddy case 'g':
7899 e4526bf5 2021-09-03 naddy case KEY_HOME:
7900 e4526bf5 2021-09-03 naddy s->selected = 0;
7901 07b0611c 2022-06-23 thomas view->count = 0;
7902 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7903 e4526bf5 2021-09-03 naddy break;
7904 e4526bf5 2021-09-03 naddy case 'G':
7905 a5d43cac 2022-07-01 thomas case KEY_END: {
7906 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7907 a5d43cac 2022-07-01 thomas
7908 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7909 a5d43cac 2022-07-01 thomas --eos; /* border */
7910 e4526bf5 2021-09-03 naddy s->selected = 0;
7911 07b0611c 2022-06-23 thomas view->count = 0;
7912 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7913 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7914 e4526bf5 2021-09-03 naddy if (re == NULL)
7915 e4526bf5 2021-09-03 naddy break;
7916 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7917 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7918 e4526bf5 2021-09-03 naddy }
7919 e4526bf5 2021-09-03 naddy if (n > 0)
7920 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7921 e4526bf5 2021-09-03 naddy break;
7922 a5d43cac 2022-07-01 thomas }
7923 6458efa5 2020-11-24 stsp case 'k':
7924 6458efa5 2020-11-24 stsp case KEY_UP:
7925 f7140bf5 2021-10-17 thomas case CTRL('p'):
7926 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7927 6458efa5 2020-11-24 stsp s->selected--;
7928 6458efa5 2020-11-24 stsp break;
7929 34ba6917 2020-11-30 stsp }
7930 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7931 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7932 07b0611c 2022-06-23 thomas view->count = 0;
7933 6458efa5 2020-11-24 stsp break;
7934 70f17a53 2022-06-13 thomas case CTRL('u'):
7935 23427b14 2022-06-23 thomas case 'u':
7936 70f17a53 2022-06-13 thomas nscroll /= 2;
7937 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7938 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7939 6458efa5 2020-11-24 stsp case CTRL('b'):
7940 1c5e5faa 2022-06-23 thomas case 'b':
7941 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7942 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7943 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7944 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7945 07b0611c 2022-06-23 thomas view->count = 0;
7946 6458efa5 2020-11-24 stsp break;
7947 6458efa5 2020-11-24 stsp case 'j':
7948 6458efa5 2020-11-24 stsp case KEY_DOWN:
7949 f7140bf5 2021-10-17 thomas case CTRL('n'):
7950 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7951 6458efa5 2020-11-24 stsp s->selected++;
7952 6458efa5 2020-11-24 stsp break;
7953 6458efa5 2020-11-24 stsp }
7954 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7955 6458efa5 2020-11-24 stsp /* can't scroll any further */
7956 07b0611c 2022-06-23 thomas view->count = 0;
7957 6458efa5 2020-11-24 stsp break;
7958 07b0611c 2022-06-23 thomas }
7959 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7960 6458efa5 2020-11-24 stsp break;
7961 70f17a53 2022-06-13 thomas case CTRL('d'):
7962 23427b14 2022-06-23 thomas case 'd':
7963 70f17a53 2022-06-13 thomas nscroll /= 2;
7964 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7965 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7966 6458efa5 2020-11-24 stsp case CTRL('f'):
7967 1c5e5faa 2022-06-23 thomas case 'f':
7968 4c2d69cb 2022-06-23 thomas case ' ':
7969 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7970 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7971 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7972 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7973 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7974 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7975 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7976 07b0611c 2022-06-23 thomas view->count = 0;
7977 6458efa5 2020-11-24 stsp break;
7978 6458efa5 2020-11-24 stsp }
7979 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7980 6458efa5 2020-11-24 stsp break;
7981 6458efa5 2020-11-24 stsp case CTRL('l'):
7982 07b0611c 2022-06-23 thomas view->count = 0;
7983 8924d611 2020-12-26 stsp tog_free_refs();
7984 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7985 8924d611 2020-12-26 stsp if (err)
7986 8924d611 2020-12-26 stsp break;
7987 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7988 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7989 6458efa5 2020-11-24 stsp break;
7990 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7991 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7992 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7993 6458efa5 2020-11-24 stsp break;
7994 6458efa5 2020-11-24 stsp default:
7995 07b0611c 2022-06-23 thomas view->count = 0;
7996 6458efa5 2020-11-24 stsp break;
7997 6458efa5 2020-11-24 stsp }
7998 6458efa5 2020-11-24 stsp
7999 6458efa5 2020-11-24 stsp return err;
8000 6458efa5 2020-11-24 stsp }
8001 6458efa5 2020-11-24 stsp
8002 6458efa5 2020-11-24 stsp __dead static void
8003 6458efa5 2020-11-24 stsp usage_ref(void)
8004 6458efa5 2020-11-24 stsp {
8005 6458efa5 2020-11-24 stsp endwin();
8006 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8007 6458efa5 2020-11-24 stsp getprogname());
8008 6458efa5 2020-11-24 stsp exit(1);
8009 6458efa5 2020-11-24 stsp }
8010 6458efa5 2020-11-24 stsp
8011 6458efa5 2020-11-24 stsp static const struct got_error *
8012 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8013 6458efa5 2020-11-24 stsp {
8014 6458efa5 2020-11-24 stsp const struct got_error *error;
8015 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8016 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8017 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8018 6458efa5 2020-11-24 stsp int ch;
8019 6458efa5 2020-11-24 stsp struct tog_view *view;
8020 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8021 6458efa5 2020-11-24 stsp
8022 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8023 6458efa5 2020-11-24 stsp switch (ch) {
8024 6458efa5 2020-11-24 stsp case 'r':
8025 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8026 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8027 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8028 6458efa5 2020-11-24 stsp optarg);
8029 6458efa5 2020-11-24 stsp break;
8030 6458efa5 2020-11-24 stsp default:
8031 e99e2d15 2020-11-24 naddy usage_ref();
8032 6458efa5 2020-11-24 stsp /* NOTREACHED */
8033 6458efa5 2020-11-24 stsp }
8034 6458efa5 2020-11-24 stsp }
8035 6458efa5 2020-11-24 stsp
8036 6458efa5 2020-11-24 stsp argc -= optind;
8037 6458efa5 2020-11-24 stsp argv += optind;
8038 6458efa5 2020-11-24 stsp
8039 6458efa5 2020-11-24 stsp if (argc > 1)
8040 e99e2d15 2020-11-24 naddy usage_ref();
8041 7cd52833 2022-06-23 thomas
8042 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8043 7cd52833 2022-06-23 thomas if (error != NULL)
8044 7cd52833 2022-06-23 thomas goto done;
8045 6458efa5 2020-11-24 stsp
8046 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8047 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8048 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8049 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8050 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8051 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8052 c156c7a4 2020-12-18 stsp goto done;
8053 6458efa5 2020-11-24 stsp if (worktree)
8054 6458efa5 2020-11-24 stsp repo_path =
8055 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8056 6458efa5 2020-11-24 stsp else
8057 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8058 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8059 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8060 c156c7a4 2020-12-18 stsp goto done;
8061 c156c7a4 2020-12-18 stsp }
8062 6458efa5 2020-11-24 stsp }
8063 6458efa5 2020-11-24 stsp
8064 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8065 6458efa5 2020-11-24 stsp if (error != NULL)
8066 6458efa5 2020-11-24 stsp goto done;
8067 6458efa5 2020-11-24 stsp
8068 6458efa5 2020-11-24 stsp init_curses();
8069 6458efa5 2020-11-24 stsp
8070 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8071 51a10b52 2020-12-26 stsp if (error)
8072 51a10b52 2020-12-26 stsp goto done;
8073 51a10b52 2020-12-26 stsp
8074 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8075 6458efa5 2020-11-24 stsp if (error)
8076 6458efa5 2020-11-24 stsp goto done;
8077 6458efa5 2020-11-24 stsp
8078 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8079 6458efa5 2020-11-24 stsp if (view == NULL) {
8080 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8081 6458efa5 2020-11-24 stsp goto done;
8082 6458efa5 2020-11-24 stsp }
8083 6458efa5 2020-11-24 stsp
8084 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8085 6458efa5 2020-11-24 stsp if (error)
8086 6458efa5 2020-11-24 stsp goto done;
8087 6458efa5 2020-11-24 stsp
8088 6458efa5 2020-11-24 stsp if (worktree) {
8089 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8090 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8091 6458efa5 2020-11-24 stsp worktree = NULL;
8092 6458efa5 2020-11-24 stsp }
8093 6458efa5 2020-11-24 stsp error = view_loop(view);
8094 6458efa5 2020-11-24 stsp done:
8095 6458efa5 2020-11-24 stsp free(repo_path);
8096 6458efa5 2020-11-24 stsp free(cwd);
8097 1d0f4054 2021-06-17 stsp if (repo) {
8098 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8099 1d0f4054 2021-06-17 stsp if (close_err)
8100 1d0f4054 2021-06-17 stsp error = close_err;
8101 1d0f4054 2021-06-17 stsp }
8102 7cd52833 2022-06-23 thomas if (pack_fds) {
8103 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8104 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8105 7cd52833 2022-06-23 thomas if (error == NULL)
8106 7cd52833 2022-06-23 thomas error = pack_err;
8107 7cd52833 2022-06-23 thomas }
8108 51a10b52 2020-12-26 stsp tog_free_refs();
8109 6458efa5 2020-11-24 stsp return error;
8110 6458efa5 2020-11-24 stsp }
8111 6458efa5 2020-11-24 stsp
8112 2a31b33b 2022-07-23 thomas static const struct got_error *
8113 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8114 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8115 2a31b33b 2022-07-23 thomas {
8116 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8117 2a31b33b 2022-07-23 thomas
8118 2a31b33b 2022-07-23 thomas *new_view = NULL;
8119 2a31b33b 2022-07-23 thomas
8120 2a31b33b 2022-07-23 thomas switch (request) {
8121 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8122 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8123 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8124 2a31b33b 2022-07-23 thomas
8125 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8126 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8127 2a31b33b 2022-07-23 thomas view, s->repo);
8128 2a31b33b 2022-07-23 thomas } else
8129 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8130 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8131 2a31b33b 2022-07-23 thomas break;
8132 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8133 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8134 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8135 2a31b33b 2022-07-23 thomas
8136 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8137 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8138 2a31b33b 2022-07-23 thomas s->repo);
8139 2a31b33b 2022-07-23 thomas } else
8140 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8141 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8142 2a31b33b 2022-07-23 thomas break;
8143 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8144 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8145 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8146 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8147 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8148 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8149 2a31b33b 2022-07-23 thomas &view->state.tree);
8150 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8151 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8152 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8153 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8154 2a31b33b 2022-07-23 thomas else
8155 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8156 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8157 2a31b33b 2022-07-23 thomas break;
8158 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8159 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8160 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8161 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8162 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8163 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8164 2a31b33b 2022-07-23 thomas view->state.log.repo);
8165 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8166 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8167 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8168 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8169 2a31b33b 2022-07-23 thomas else
8170 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8171 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8172 2a31b33b 2022-07-23 thomas break;
8173 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
8174 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8175 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
8176 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
8177 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8178 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
8179 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8180 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
8181 2a31b33b 2022-07-23 thomas else
8182 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
8183 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8184 2a31b33b 2022-07-23 thomas if (err)
8185 2a31b33b 2022-07-23 thomas view_close(*new_view);
8186 2a31b33b 2022-07-23 thomas break;
8187 2a31b33b 2022-07-23 thomas default:
8188 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8189 2a31b33b 2022-07-23 thomas }
8190 2a31b33b 2022-07-23 thomas
8191 2a31b33b 2022-07-23 thomas return err;
8192 2a31b33b 2022-07-23 thomas }
8193 2a31b33b 2022-07-23 thomas
8194 a5d43cac 2022-07-01 thomas /*
8195 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8196 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8197 a5d43cac 2022-07-01 thomas */
8198 6458efa5 2020-11-24 stsp static void
8199 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8200 a5d43cac 2022-07-01 thomas {
8201 a5d43cac 2022-07-01 thomas switch (view->type) {
8202 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8203 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8204 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8205 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8206 a5d43cac 2022-07-01 thomas 1);
8207 a5d43cac 2022-07-01 thomas break;
8208 a5d43cac 2022-07-01 thomas }
8209 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8210 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8211 a5d43cac 2022-07-01 thomas else
8212 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8213 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8214 a5d43cac 2022-07-01 thomas break;
8215 a5d43cac 2022-07-01 thomas }
8216 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8217 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8218 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8219 a5d43cac 2022-07-01 thomas break;
8220 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8221 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8222 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8223 a5d43cac 2022-07-01 thomas break;
8224 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8225 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8226 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8227 a5d43cac 2022-07-01 thomas break;
8228 a5d43cac 2022-07-01 thomas default:
8229 a5d43cac 2022-07-01 thomas break;
8230 a5d43cac 2022-07-01 thomas }
8231 a5d43cac 2022-07-01 thomas
8232 a5d43cac 2022-07-01 thomas view->offset = 0;
8233 a5d43cac 2022-07-01 thomas }
8234 a5d43cac 2022-07-01 thomas
8235 a5d43cac 2022-07-01 thomas /*
8236 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8237 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8238 a5d43cac 2022-07-01 thomas */
8239 a5d43cac 2022-07-01 thomas static const struct got_error *
8240 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8241 a5d43cac 2022-07-01 thomas {
8242 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8243 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8244 a5d43cac 2022-07-01 thomas int *selected = NULL;
8245 a5d43cac 2022-07-01 thomas int header, offset;
8246 a5d43cac 2022-07-01 thomas
8247 a5d43cac 2022-07-01 thomas switch (view->type) {
8248 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8249 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8250 a5d43cac 2022-07-01 thomas header = 3;
8251 a5d43cac 2022-07-01 thomas scrolld = NULL;
8252 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8253 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8254 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8255 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8256 a5d43cac 2022-07-01 thomas view->offset = offset;
8257 a5d43cac 2022-07-01 thomas }
8258 a5d43cac 2022-07-01 thomas break;
8259 a5d43cac 2022-07-01 thomas }
8260 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8261 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8262 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8263 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8264 a5d43cac 2022-07-01 thomas selected = &s->selected;
8265 a5d43cac 2022-07-01 thomas break;
8266 a5d43cac 2022-07-01 thomas }
8267 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8268 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8269 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8270 a5d43cac 2022-07-01 thomas header = 3;
8271 a5d43cac 2022-07-01 thomas selected = &s->selected;
8272 a5d43cac 2022-07-01 thomas break;
8273 a5d43cac 2022-07-01 thomas }
8274 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8275 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8276 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8277 a5d43cac 2022-07-01 thomas header = 5;
8278 a5d43cac 2022-07-01 thomas selected = &s->selected;
8279 a5d43cac 2022-07-01 thomas break;
8280 a5d43cac 2022-07-01 thomas }
8281 a5d43cac 2022-07-01 thomas default:
8282 a5d43cac 2022-07-01 thomas selected = NULL;
8283 a5d43cac 2022-07-01 thomas scrolld = NULL;
8284 a5d43cac 2022-07-01 thomas header = 0;
8285 a5d43cac 2022-07-01 thomas break;
8286 a5d43cac 2022-07-01 thomas }
8287 a5d43cac 2022-07-01 thomas
8288 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8289 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8290 a5d43cac 2022-07-01 thomas view->offset = offset;
8291 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8292 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8293 a5d43cac 2022-07-01 thomas *selected -= offset;
8294 a5d43cac 2022-07-01 thomas }
8295 a5d43cac 2022-07-01 thomas }
8296 a5d43cac 2022-07-01 thomas
8297 a5d43cac 2022-07-01 thomas return err;
8298 a5d43cac 2022-07-01 thomas }
8299 a5d43cac 2022-07-01 thomas
8300 a5d43cac 2022-07-01 thomas static void
8301 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8302 ce5b7c56 2019-07-09 stsp {
8303 6059809a 2020-12-17 stsp size_t i;
8304 9f7d7167 2018-04-29 stsp
8305 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8306 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8307 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8308 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8309 ce5b7c56 2019-07-09 stsp }
8310 6879ba42 2020-10-01 naddy fputc('\n', fp);
8311 ce5b7c56 2019-07-09 stsp }
8312 ce5b7c56 2019-07-09 stsp
8313 4ed7e80c 2018-05-20 stsp __dead static void
8314 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8315 9f7d7167 2018-04-29 stsp {
8316 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8317 6879ba42 2020-10-01 naddy
8318 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8319 6879ba42 2020-10-01 naddy getprogname());
8320 6879ba42 2020-10-01 naddy if (hflag) {
8321 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8322 6879ba42 2020-10-01 naddy list_commands(fp);
8323 ee85c5e8 2020-02-29 stsp }
8324 6879ba42 2020-10-01 naddy exit(status);
8325 9f7d7167 2018-04-29 stsp }
8326 9f7d7167 2018-04-29 stsp
8327 c2301be8 2018-04-30 stsp static char **
8328 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8329 c2301be8 2018-04-30 stsp {
8330 ee85c5e8 2020-02-29 stsp va_list ap;
8331 c2301be8 2018-04-30 stsp char **argv;
8332 ee85c5e8 2020-02-29 stsp int i;
8333 c2301be8 2018-04-30 stsp
8334 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8335 ee85c5e8 2020-02-29 stsp
8336 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8337 c2301be8 2018-04-30 stsp if (argv == NULL)
8338 c2301be8 2018-04-30 stsp err(1, "calloc");
8339 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8340 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8341 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8342 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8343 c2301be8 2018-04-30 stsp }
8344 c2301be8 2018-04-30 stsp
8345 ee85c5e8 2020-02-29 stsp va_end(ap);
8346 c2301be8 2018-04-30 stsp return argv;
8347 ee85c5e8 2020-02-29 stsp }
8348 ee85c5e8 2020-02-29 stsp
8349 ee85c5e8 2020-02-29 stsp /*
8350 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8351 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8352 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8353 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8354 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8355 ee85c5e8 2020-02-29 stsp */
8356 ee85c5e8 2020-02-29 stsp static const struct got_error *
8357 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8358 ee85c5e8 2020-02-29 stsp {
8359 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8360 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8361 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8362 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8363 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8364 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8365 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8366 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8367 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8368 84de9106 2020-12-26 stsp
8369 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8370 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8371 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8372 ee85c5e8 2020-02-29 stsp
8373 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8374 7cd52833 2022-06-23 thomas if (error != NULL)
8375 7cd52833 2022-06-23 thomas goto done;
8376 7cd52833 2022-06-23 thomas
8377 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8378 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8379 ee85c5e8 2020-02-29 stsp goto done;
8380 ee85c5e8 2020-02-29 stsp
8381 ee85c5e8 2020-02-29 stsp if (worktree)
8382 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8383 ee85c5e8 2020-02-29 stsp else
8384 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8385 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8386 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8387 ee85c5e8 2020-02-29 stsp goto done;
8388 ee85c5e8 2020-02-29 stsp }
8389 ee85c5e8 2020-02-29 stsp
8390 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8391 ee85c5e8 2020-02-29 stsp if (error != NULL)
8392 ee85c5e8 2020-02-29 stsp goto done;
8393 ee85c5e8 2020-02-29 stsp
8394 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8395 ee85c5e8 2020-02-29 stsp repo, worktree);
8396 ee85c5e8 2020-02-29 stsp if (error)
8397 ee85c5e8 2020-02-29 stsp goto done;
8398 ee85c5e8 2020-02-29 stsp
8399 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8400 84de9106 2020-12-26 stsp if (error)
8401 84de9106 2020-12-26 stsp goto done;
8402 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8403 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8404 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8405 ee85c5e8 2020-02-29 stsp if (error)
8406 ee85c5e8 2020-02-29 stsp goto done;
8407 ee85c5e8 2020-02-29 stsp
8408 ee85c5e8 2020-02-29 stsp if (worktree) {
8409 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8410 ee85c5e8 2020-02-29 stsp worktree = NULL;
8411 ee85c5e8 2020-02-29 stsp }
8412 ee85c5e8 2020-02-29 stsp
8413 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8414 945f9229 2022-04-16 thomas if (error)
8415 945f9229 2022-04-16 thomas goto done;
8416 945f9229 2022-04-16 thomas
8417 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8418 ee85c5e8 2020-02-29 stsp if (error) {
8419 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8420 ee85c5e8 2020-02-29 stsp goto done;
8421 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8422 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8423 6879ba42 2020-10-01 naddy usage(1, 1);
8424 ee85c5e8 2020-02-29 stsp /* not reached */
8425 ee85c5e8 2020-02-29 stsp }
8426 ee85c5e8 2020-02-29 stsp
8427 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8428 ee85c5e8 2020-02-29 stsp if (error)
8429 ee85c5e8 2020-02-29 stsp goto done;
8430 ee85c5e8 2020-02-29 stsp
8431 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8432 ee85c5e8 2020-02-29 stsp argc = 4;
8433 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8434 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8435 ee85c5e8 2020-02-29 stsp done:
8436 1d0f4054 2021-06-17 stsp if (repo) {
8437 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8438 1d0f4054 2021-06-17 stsp if (error == NULL)
8439 1d0f4054 2021-06-17 stsp error = close_err;
8440 1d0f4054 2021-06-17 stsp }
8441 945f9229 2022-04-16 thomas if (commit)
8442 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8443 ee85c5e8 2020-02-29 stsp if (worktree)
8444 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8445 7cd52833 2022-06-23 thomas if (pack_fds) {
8446 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8447 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8448 7cd52833 2022-06-23 thomas if (error == NULL)
8449 7cd52833 2022-06-23 thomas error = pack_err;
8450 7cd52833 2022-06-23 thomas }
8451 ee85c5e8 2020-02-29 stsp free(id);
8452 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8453 ee85c5e8 2020-02-29 stsp free(commit_id);
8454 ee85c5e8 2020-02-29 stsp free(cwd);
8455 ee85c5e8 2020-02-29 stsp free(repo_path);
8456 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8457 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8458 ee85c5e8 2020-02-29 stsp int i;
8459 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8460 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8461 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8462 ee85c5e8 2020-02-29 stsp }
8463 87670572 2020-12-26 naddy tog_free_refs();
8464 ee85c5e8 2020-02-29 stsp return error;
8465 c2301be8 2018-04-30 stsp }
8466 c2301be8 2018-04-30 stsp
8467 9f7d7167 2018-04-29 stsp int
8468 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8469 9f7d7167 2018-04-29 stsp {
8470 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8471 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8472 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8473 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8474 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8475 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8476 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8477 83cd27f8 2020-01-13 stsp };
8478 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8479 9f7d7167 2018-04-29 stsp
8480 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8481 9f7d7167 2018-04-29 stsp
8482 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8483 9f7d7167 2018-04-29 stsp switch (ch) {
8484 9f7d7167 2018-04-29 stsp case 'h':
8485 9f7d7167 2018-04-29 stsp hflag = 1;
8486 9f7d7167 2018-04-29 stsp break;
8487 53ccebc2 2019-07-30 stsp case 'V':
8488 53ccebc2 2019-07-30 stsp Vflag = 1;
8489 53ccebc2 2019-07-30 stsp break;
8490 9f7d7167 2018-04-29 stsp default:
8491 6879ba42 2020-10-01 naddy usage(hflag, 1);
8492 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8493 9f7d7167 2018-04-29 stsp }
8494 9f7d7167 2018-04-29 stsp }
8495 9f7d7167 2018-04-29 stsp
8496 9f7d7167 2018-04-29 stsp argc -= optind;
8497 9f7d7167 2018-04-29 stsp argv += optind;
8498 9814e6a3 2020-09-27 naddy optind = 1;
8499 c2301be8 2018-04-30 stsp optreset = 1;
8500 9f7d7167 2018-04-29 stsp
8501 53ccebc2 2019-07-30 stsp if (Vflag) {
8502 53ccebc2 2019-07-30 stsp got_version_print_str();
8503 6879ba42 2020-10-01 naddy return 0;
8504 53ccebc2 2019-07-30 stsp }
8505 4010e238 2020-12-04 stsp
8506 4010e238 2020-12-04 stsp #ifndef PROFILE
8507 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8508 4010e238 2020-12-04 stsp NULL) == -1)
8509 4010e238 2020-12-04 stsp err(1, "pledge");
8510 4010e238 2020-12-04 stsp #endif
8511 53ccebc2 2019-07-30 stsp
8512 c2301be8 2018-04-30 stsp if (argc == 0) {
8513 f29d3e89 2018-06-23 stsp if (hflag)
8514 6879ba42 2020-10-01 naddy usage(hflag, 0);
8515 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8516 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8517 c2301be8 2018-04-30 stsp argc = 1;
8518 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8519 c2301be8 2018-04-30 stsp } else {
8520 6059809a 2020-12-17 stsp size_t i;
8521 9f7d7167 2018-04-29 stsp
8522 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8523 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8524 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8525 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8526 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8527 9f7d7167 2018-04-29 stsp break;
8528 9f7d7167 2018-04-29 stsp }
8529 9f7d7167 2018-04-29 stsp }
8530 ee85c5e8 2020-02-29 stsp }
8531 3642c4c6 2019-07-09 stsp
8532 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8533 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8534 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8535 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8536 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8537 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8538 adf4c9e0 2022-07-03 thomas }
8539 adf4c9e0 2022-07-03 thomas
8540 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8541 ee85c5e8 2020-02-29 stsp if (argc != 1)
8542 6879ba42 2020-10-01 naddy usage(0, 1);
8543 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8544 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8545 ee85c5e8 2020-02-29 stsp } else {
8546 ee85c5e8 2020-02-29 stsp if (hflag)
8547 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8548 ee85c5e8 2020-02-29 stsp else
8549 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8550 9f7d7167 2018-04-29 stsp }
8551 9f7d7167 2018-04-29 stsp
8552 9f7d7167 2018-04-29 stsp endwin();
8553 b46c1e04 2020-09-20 naddy putchar('\n');
8554 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8555 a2f4a359 2020-02-28 stsp int i;
8556 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8557 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8558 a2f4a359 2020-02-28 stsp free(cmd_argv);
8559 a2f4a359 2020-02-28 stsp }
8560 a2f4a359 2020-02-28 stsp
8561 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8562 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8563 9f7d7167 2018-04-29 stsp return 0;
8564 9f7d7167 2018-04-29 stsp }