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 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
18 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
19 80ddbec8 2018-04-29 stsp
20 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
23 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 def2f970 2021-09-28 thomas #endif
25 9f7d7167 2018-04-29 stsp #include <curses.h>
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
43 9f7d7167 2018-04-29 stsp
44 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
45 dd038bc6 2021-09-21 thomas.ad
46 53ccebc2 2019-07-30 stsp #include "got_version.h"
47 9f7d7167 2018-04-29 stsp #include "got_error.h"
48 80ddbec8 2018-04-29 stsp #include "got_object.h"
49 80ddbec8 2018-04-29 stsp #include "got_reference.h"
50 80ddbec8 2018-04-29 stsp #include "got_repository.h"
51 80ddbec8 2018-04-29 stsp #include "got_diff.h"
52 511a516b 2018-05-19 stsp #include "got_opentemp.h"
53 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
54 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
55 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
56 a70480e0 2018-06-23 stsp #include "got_blame.h"
57 c2db6724 2019-01-04 stsp #include "got_privsep.h"
58 1dd54920 2019-05-11 stsp #include "got_path.h"
59 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 9f7d7167 2018-04-29 stsp
61 dd038bc6 2021-09-21 thomas.ad //#define update_panels() (0)
62 dd038bc6 2021-09-21 thomas.ad //#define doupdate() (0)
63 dd038bc6 2021-09-21 thomas.ad
64 881b2d3e 2018-04-30 stsp #ifndef MIN
65 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 881b2d3e 2018-04-30 stsp #endif
67 881b2d3e 2018-04-30 stsp
68 2bd27830 2018-10-22 stsp #ifndef MAX
69 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 2bd27830 2018-10-22 stsp #endif
71 2bd27830 2018-10-22 stsp
72 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
73 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
74 acf52a76 2021-09-21 thomas.ad #endif
75 2bd27830 2018-10-22 stsp
76 9f7d7167 2018-04-29 stsp #ifndef nitems
77 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 9f7d7167 2018-04-29 stsp #endif
79 9f7d7167 2018-04-29 stsp
80 9f7d7167 2018-04-29 stsp struct tog_cmd {
81 c2301be8 2018-04-30 stsp const char *name;
82 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
84 9f7d7167 2018-04-29 stsp };
85 9f7d7167 2018-04-29 stsp
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
89 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
90 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
91 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
92 9f7d7167 2018-04-29 stsp
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
96 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
97 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
98 9f7d7167 2018-04-29 stsp
99 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
100 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
101 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
102 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
103 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
104 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
105 9f7d7167 2018-04-29 stsp };
106 9f7d7167 2018-04-29 stsp
107 d6b05b5b 2018-08-04 stsp enum tog_view_type {
108 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
109 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
110 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
111 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
112 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
113 d6b05b5b 2018-08-04 stsp };
114 c3e9aa98 2019-05-13 jcs
115 a5d43cac 2022-07-01 thomas enum tog_view_mode {
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
117 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
118 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
119 a5d43cac 2022-07-01 thomas };
120 a5d43cac 2022-07-01 thomas
121 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
122 a5d43cac 2022-07-01 thomas
123 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
124 d6b05b5b 2018-08-04 stsp
125 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
126 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
127 ba4f502b 2018-08-04 stsp struct got_object_id *id;
128 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
129 1a76625f 2018-10-22 stsp int idx;
130 ba4f502b 2018-08-04 stsp };
131 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
132 ba4f502b 2018-08-04 stsp struct commit_queue {
133 ba4f502b 2018-08-04 stsp int ncommits;
134 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
135 6d17833f 2019-11-08 stsp };
136 6d17833f 2019-11-08 stsp
137 f26dddb7 2019-11-08 stsp struct tog_color {
138 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
139 6d17833f 2019-11-08 stsp regex_t regex;
140 6d17833f 2019-11-08 stsp short colorpair;
141 15a087fe 2019-02-21 stsp };
142 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
143 11b20872 2019-11-08 stsp
144 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
145 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
146 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
147 2183bbf6 2022-01-23 thomas
148 2183bbf6 2022-01-23 thomas static const struct got_error *
149 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
150 2183bbf6 2022-01-23 thomas struct got_reference* re2)
151 2183bbf6 2022-01-23 thomas {
152 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
153 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
154 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
155 2183bbf6 2022-01-23 thomas
156 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
157 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
158 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
159 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
160 2183bbf6 2022-01-23 thomas *cmp = -1;
161 2183bbf6 2022-01-23 thomas return NULL;
162 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
163 2183bbf6 2022-01-23 thomas *cmp = 1;
164 2183bbf6 2022-01-23 thomas return NULL;
165 2183bbf6 2022-01-23 thomas }
166 2183bbf6 2022-01-23 thomas
167 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
168 2183bbf6 2022-01-23 thomas return NULL;
169 2183bbf6 2022-01-23 thomas }
170 51a10b52 2020-12-26 stsp
171 11b20872 2019-11-08 stsp static const struct got_error *
172 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
173 51a10b52 2020-12-26 stsp {
174 51a10b52 2020-12-26 stsp const struct got_error *err;
175 51a10b52 2020-12-26 stsp
176 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
177 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
178 3bfadbd4 2021-11-20 thomas repo);
179 51a10b52 2020-12-26 stsp if (err)
180 51a10b52 2020-12-26 stsp return err;
181 51a10b52 2020-12-26 stsp
182 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
183 51a10b52 2020-12-26 stsp repo);
184 51a10b52 2020-12-26 stsp }
185 51a10b52 2020-12-26 stsp
186 51a10b52 2020-12-26 stsp static void
187 51a10b52 2020-12-26 stsp tog_free_refs(void)
188 51a10b52 2020-12-26 stsp {
189 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
190 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
191 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
192 51a10b52 2020-12-26 stsp }
193 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
194 51a10b52 2020-12-26 stsp }
195 51a10b52 2020-12-26 stsp
196 51a10b52 2020-12-26 stsp static const struct got_error *
197 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
198 11b20872 2019-11-08 stsp int idx, short color)
199 11b20872 2019-11-08 stsp {
200 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
201 11b20872 2019-11-08 stsp struct tog_color *tc;
202 11b20872 2019-11-08 stsp int regerr = 0;
203 11b20872 2019-11-08 stsp
204 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
205 11b20872 2019-11-08 stsp return NULL;
206 11b20872 2019-11-08 stsp
207 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
208 11b20872 2019-11-08 stsp
209 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
210 11b20872 2019-11-08 stsp if (tc == NULL)
211 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
212 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
213 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
214 11b20872 2019-11-08 stsp if (regerr) {
215 11b20872 2019-11-08 stsp static char regerr_msg[512];
216 11b20872 2019-11-08 stsp static char err_msg[512];
217 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
218 11b20872 2019-11-08 stsp sizeof(regerr_msg));
219 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
220 11b20872 2019-11-08 stsp regerr_msg);
221 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
222 11b20872 2019-11-08 stsp free(tc);
223 11b20872 2019-11-08 stsp return err;
224 11b20872 2019-11-08 stsp }
225 11b20872 2019-11-08 stsp tc->colorpair = idx;
226 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
227 11b20872 2019-11-08 stsp return NULL;
228 11b20872 2019-11-08 stsp }
229 11b20872 2019-11-08 stsp
230 11b20872 2019-11-08 stsp static void
231 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
232 11b20872 2019-11-08 stsp {
233 11b20872 2019-11-08 stsp struct tog_color *tc;
234 11b20872 2019-11-08 stsp
235 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
236 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
237 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
238 11b20872 2019-11-08 stsp regfree(&tc->regex);
239 11b20872 2019-11-08 stsp free(tc);
240 11b20872 2019-11-08 stsp }
241 11b20872 2019-11-08 stsp }
242 11b20872 2019-11-08 stsp
243 ef20f542 2022-06-26 thomas static struct tog_color *
244 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
245 11b20872 2019-11-08 stsp {
246 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
247 11b20872 2019-11-08 stsp
248 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
249 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
250 11b20872 2019-11-08 stsp return tc;
251 11b20872 2019-11-08 stsp }
252 11b20872 2019-11-08 stsp
253 11b20872 2019-11-08 stsp return NULL;
254 11b20872 2019-11-08 stsp }
255 11b20872 2019-11-08 stsp
256 11b20872 2019-11-08 stsp static int
257 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
258 11b20872 2019-11-08 stsp {
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
262 11b20872 2019-11-08 stsp return COLOR_CYAN;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
264 11b20872 2019-11-08 stsp return COLOR_YELLOW;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
266 11b20872 2019-11-08 stsp return COLOR_GREEN;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
268 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
272 91b8c405 2020-01-25 stsp return COLOR_CYAN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
276 11b20872 2019-11-08 stsp return COLOR_GREEN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
278 11b20872 2019-11-08 stsp return COLOR_CYAN;
279 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
280 11b20872 2019-11-08 stsp return COLOR_YELLOW;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_GREEN;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
285 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
286 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
287 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
288 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
289 11b20872 2019-11-08 stsp
290 11b20872 2019-11-08 stsp return -1;
291 11b20872 2019-11-08 stsp }
292 11b20872 2019-11-08 stsp
293 11b20872 2019-11-08 stsp static int
294 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
295 11b20872 2019-11-08 stsp {
296 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
297 11b20872 2019-11-08 stsp
298 11b20872 2019-11-08 stsp if (val == NULL)
299 11b20872 2019-11-08 stsp return default_color_value(envvar);
300 15a087fe 2019-02-21 stsp
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
302 11b20872 2019-11-08 stsp return COLOR_BLACK;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
304 11b20872 2019-11-08 stsp return COLOR_RED;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
306 11b20872 2019-11-08 stsp return COLOR_GREEN;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
308 11b20872 2019-11-08 stsp return COLOR_YELLOW;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
310 11b20872 2019-11-08 stsp return COLOR_BLUE;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
312 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
314 11b20872 2019-11-08 stsp return COLOR_CYAN;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
316 11b20872 2019-11-08 stsp return COLOR_WHITE;
317 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
318 11b20872 2019-11-08 stsp return -1;
319 11b20872 2019-11-08 stsp
320 11b20872 2019-11-08 stsp return default_color_value(envvar);
321 11b20872 2019-11-08 stsp }
322 11b20872 2019-11-08 stsp
323 11b20872 2019-11-08 stsp
324 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
325 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
326 3dbaef42 2020-11-24 stsp const char *label1, *label2;
327 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
328 19a6a6b5 2022-07-01 thomas int fd1, fd2;
329 15a087fe 2019-02-21 stsp int first_displayed_line;
330 15a087fe 2019-02-21 stsp int last_displayed_line;
331 15a087fe 2019-02-21 stsp int eof;
332 15a087fe 2019-02-21 stsp int diff_context;
333 3dbaef42 2020-11-24 stsp int ignore_whitespace;
334 64453f7e 2020-11-21 stsp int force_text_diff;
335 15a087fe 2019-02-21 stsp struct got_repository *repo;
336 bddb1296 2019-11-08 stsp struct tog_colors colors;
337 fe621944 2020-11-10 stsp size_t nlines;
338 f44b1f58 2020-02-02 tracey off_t *line_offsets;
339 f44b1f58 2020-02-02 tracey int matched_line;
340 f44b1f58 2020-02-02 tracey int selected_line;
341 15a087fe 2019-02-21 stsp
342 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
343 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
344 b01e7d3b 2018-08-04 stsp };
345 b01e7d3b 2018-08-04 stsp
346 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
347 1a76625f 2018-10-22 stsp
348 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
349 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
350 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
351 1a76625f 2018-10-22 stsp int commits_needed;
352 fb280deb 2021-08-30 stsp int load_all;
353 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
354 1a76625f 2018-10-22 stsp struct commit_queue *commits;
355 1a76625f 2018-10-22 stsp const char *in_repo_path;
356 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
357 1a76625f 2018-10-22 stsp struct got_repository *repo;
358 1af5eddf 2022-06-23 thomas int *pack_fds;
359 1a76625f 2018-10-22 stsp int log_complete;
360 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
361 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
362 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
363 13add988 2019-10-15 stsp int *searching;
364 13add988 2019-10-15 stsp int *search_next_done;
365 13add988 2019-10-15 stsp regex_t *regex;
366 1a76625f 2018-10-22 stsp };
367 1a76625f 2018-10-22 stsp
368 1a76625f 2018-10-22 stsp struct tog_log_view_state {
369 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
371 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
372 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
373 b01e7d3b 2018-08-04 stsp int selected;
374 b01e7d3b 2018-08-04 stsp char *in_repo_path;
375 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
376 b672a97a 2020-01-27 stsp int log_branches;
377 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
378 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
379 1a76625f 2018-10-22 stsp sig_atomic_t quit;
380 1a76625f 2018-10-22 stsp pthread_t thread;
381 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
382 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
383 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
384 11b20872 2019-11-08 stsp struct tog_colors colors;
385 ba4f502b 2018-08-04 stsp };
386 11b20872 2019-11-08 stsp
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
394 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
395 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
396 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
397 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
400 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
401 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
402 ba4f502b 2018-08-04 stsp
403 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
404 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
405 e9424729 2018-08-04 stsp int nlines;
406 e9424729 2018-08-04 stsp
407 e9424729 2018-08-04 stsp struct tog_view *view;
408 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
409 e9424729 2018-08-04 stsp int *quit;
410 e9424729 2018-08-04 stsp };
411 e9424729 2018-08-04 stsp
412 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
413 e9424729 2018-08-04 stsp const char *path;
414 e9424729 2018-08-04 stsp struct got_repository *repo;
415 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
416 e9424729 2018-08-04 stsp int *complete;
417 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
418 fc06ba56 2019-08-22 stsp void *cancel_arg;
419 e9424729 2018-08-04 stsp };
420 e9424729 2018-08-04 stsp
421 e9424729 2018-08-04 stsp struct tog_blame {
422 e9424729 2018-08-04 stsp FILE *f;
423 be659d10 2020-11-18 stsp off_t filesize;
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
425 6fcac457 2018-11-19 stsp int nlines;
426 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
427 e9424729 2018-08-04 stsp pthread_t thread;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
429 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
430 e9424729 2018-08-04 stsp const char *path;
431 7cd52833 2022-06-23 thomas int *pack_fds;
432 e9424729 2018-08-04 stsp };
433 e9424729 2018-08-04 stsp
434 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
435 7cbe629d 2018-08-04 stsp int first_displayed_line;
436 7cbe629d 2018-08-04 stsp int last_displayed_line;
437 7cbe629d 2018-08-04 stsp int selected_line;
438 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 e9424729 2018-08-04 stsp struct tog_blame blame;
447 6c4c42e0 2019-06-24 stsp int matched_line;
448 11b20872 2019-11-08 stsp struct tog_colors colors;
449 ad80ab7b 2018-08-04 stsp };
450 ad80ab7b 2018-08-04 stsp
451 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
452 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
453 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
454 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
456 ad80ab7b 2018-08-04 stsp int selected;
457 ad80ab7b 2018-08-04 stsp };
458 ad80ab7b 2018-08-04 stsp
459 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
460 ad80ab7b 2018-08-04 stsp
461 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
462 ad80ab7b 2018-08-04 stsp char *tree_label;
463 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
464 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
465 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
466 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
469 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
470 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
471 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
472 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
473 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
474 6458efa5 2020-11-24 stsp struct tog_colors colors;
475 6458efa5 2020-11-24 stsp };
476 6458efa5 2020-11-24 stsp
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
478 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
479 6458efa5 2020-11-24 stsp struct got_reference *ref;
480 6458efa5 2020-11-24 stsp int idx;
481 6458efa5 2020-11-24 stsp };
482 6458efa5 2020-11-24 stsp
483 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
484 6458efa5 2020-11-24 stsp
485 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
486 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
487 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
490 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
491 6458efa5 2020-11-24 stsp struct got_repository *repo;
492 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
493 bddb1296 2019-11-08 stsp struct tog_colors colors;
494 7cbe629d 2018-08-04 stsp };
495 7cbe629d 2018-08-04 stsp
496 669b5ffa 2018-10-07 stsp /*
497 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
498 669b5ffa 2018-10-07 stsp *
499 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
500 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
501 669b5ffa 2018-10-07 stsp * there is enough screen estate.
502 669b5ffa 2018-10-07 stsp *
503 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
504 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
505 669b5ffa 2018-10-07 stsp *
506 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
507 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
508 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
509 669b5ffa 2018-10-07 stsp *
510 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
511 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
512 669b5ffa 2018-10-07 stsp */
513 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
514 669b5ffa 2018-10-07 stsp
515 cc3c9aac 2018-08-01 stsp struct tog_view {
516 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
517 26ed57b2 2018-05-19 stsp WINDOW *window;
518 26ed57b2 2018-05-19 stsp PANEL *panel;
519 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
520 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
521 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
522 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
523 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
524 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
525 9970f7fc 2020-12-03 stsp int dying;
526 669b5ffa 2018-10-07 stsp struct tog_view *parent;
527 669b5ffa 2018-10-07 stsp struct tog_view *child;
528 5dc9f4bc 2018-08-04 stsp
529 e78dc838 2020-12-04 stsp /*
530 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
531 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
532 e78dc838 2020-12-04 stsp * between parent and child.
533 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
534 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
535 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
536 e78dc838 2020-12-04 stsp * situations.
537 e78dc838 2020-12-04 stsp */
538 e78dc838 2020-12-04 stsp int focus_child;
539 e78dc838 2020-12-04 stsp
540 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
541 5dc9f4bc 2018-08-04 stsp /* type-specific state */
542 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
543 5dc9f4bc 2018-08-04 stsp union {
544 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
545 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
546 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
547 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
548 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
549 5dc9f4bc 2018-08-04 stsp } state;
550 e5a0f69f 2018-08-18 stsp
551 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
552 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
553 e78dc838 2020-12-04 stsp struct tog_view *, int);
554 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
556 60493ae3 2019-06-20 stsp
557 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
558 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
559 c0c4acc8 2021-01-24 stsp int search_started;
560 60493ae3 2019-06-20 stsp int searching;
561 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
562 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
563 60493ae3 2019-06-20 stsp int search_next_done;
564 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
565 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
566 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
567 1803e47f 2019-06-22 stsp regex_t regex;
568 41605754 2020-11-12 stsp regmatch_t regmatch;
569 cc3c9aac 2018-08-01 stsp };
570 cd0acaa7 2018-05-20 stsp
571 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
572 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
573 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
574 78756c87 2020-11-24 stsp struct got_repository *);
575 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
576 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
577 e78dc838 2020-12-04 stsp struct tog_view *, int);
578 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
579 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
580 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
581 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
582 e5a0f69f 2018-08-18 stsp
583 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
584 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
585 78756c87 2020-11-24 stsp const char *, const char *, int);
586 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
588 e78dc838 2020-12-04 stsp struct tog_view *, int);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
590 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp
593 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
594 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
595 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
596 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
597 e78dc838 2020-12-04 stsp struct tog_view *, int);
598 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
600 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
602 e5a0f69f 2018-08-18 stsp
603 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
604 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
605 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
606 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
607 e78dc838 2020-12-04 stsp struct tog_view *, int);
608 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
609 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
611 6458efa5 2020-11-24 stsp
612 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
613 6458efa5 2020-11-24 stsp struct got_repository *);
614 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
615 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
616 e78dc838 2020-12-04 stsp struct tog_view *, int);
617 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
618 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
620 25791caa 2018-10-24 stsp
621 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
622 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
623 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
624 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
625 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
626 25791caa 2018-10-24 stsp
627 25791caa 2018-10-24 stsp static void
628 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
629 25791caa 2018-10-24 stsp {
630 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
631 83baff54 2019-08-12 stsp }
632 83baff54 2019-08-12 stsp
633 83baff54 2019-08-12 stsp static void
634 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
635 83baff54 2019-08-12 stsp {
636 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
637 25791caa 2018-10-24 stsp }
638 26ed57b2 2018-05-19 stsp
639 61266923 2020-01-14 stsp static void
640 61266923 2020-01-14 stsp tog_sigcont(int signo)
641 61266923 2020-01-14 stsp {
642 61266923 2020-01-14 stsp tog_sigcont_received = 1;
643 61266923 2020-01-14 stsp }
644 61266923 2020-01-14 stsp
645 296152d1 2022-05-31 thomas static void
646 296152d1 2022-05-31 thomas tog_sigint(int signo)
647 296152d1 2022-05-31 thomas {
648 296152d1 2022-05-31 thomas tog_sigint_received = 1;
649 296152d1 2022-05-31 thomas }
650 296152d1 2022-05-31 thomas
651 296152d1 2022-05-31 thomas static void
652 296152d1 2022-05-31 thomas tog_sigterm(int signo)
653 296152d1 2022-05-31 thomas {
654 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
655 296152d1 2022-05-31 thomas }
656 296152d1 2022-05-31 thomas
657 296152d1 2022-05-31 thomas static int
658 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
659 296152d1 2022-05-31 thomas {
660 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
661 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
662 296152d1 2022-05-31 thomas }
663 296152d1 2022-05-31 thomas
664 e5a0f69f 2018-08-18 stsp static const struct got_error *
665 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
666 ea5e7bb5 2018-08-01 stsp {
667 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
668 e5a0f69f 2018-08-18 stsp
669 669b5ffa 2018-10-07 stsp if (view->child) {
670 669b5ffa 2018-10-07 stsp view_close(view->child);
671 669b5ffa 2018-10-07 stsp view->child = NULL;
672 669b5ffa 2018-10-07 stsp }
673 e5a0f69f 2018-08-18 stsp if (view->close)
674 e5a0f69f 2018-08-18 stsp err = view->close(view);
675 ea5e7bb5 2018-08-01 stsp if (view->panel)
676 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
677 ea5e7bb5 2018-08-01 stsp if (view->window)
678 ea5e7bb5 2018-08-01 stsp delwin(view->window);
679 ea5e7bb5 2018-08-01 stsp free(view);
680 e5a0f69f 2018-08-18 stsp return err;
681 ea5e7bb5 2018-08-01 stsp }
682 ea5e7bb5 2018-08-01 stsp
683 ea5e7bb5 2018-08-01 stsp static struct tog_view *
684 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
685 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
686 ea5e7bb5 2018-08-01 stsp {
687 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
688 ea5e7bb5 2018-08-01 stsp
689 ea5e7bb5 2018-08-01 stsp if (view == NULL)
690 ea5e7bb5 2018-08-01 stsp return NULL;
691 ea5e7bb5 2018-08-01 stsp
692 d6b05b5b 2018-08-04 stsp view->type = type;
693 f7d12f7e 2018-08-01 stsp view->lines = LINES;
694 f7d12f7e 2018-08-01 stsp view->cols = COLS;
695 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
696 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
697 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
698 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
699 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
700 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
701 96a765a8 2018-08-04 stsp view_close(view);
702 ea5e7bb5 2018-08-01 stsp return NULL;
703 ea5e7bb5 2018-08-01 stsp }
704 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
705 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
706 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
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
711 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
712 ea5e7bb5 2018-08-01 stsp return view;
713 cdf1ee82 2018-08-01 stsp }
714 cdf1ee82 2018-08-01 stsp
715 0cf4efb1 2018-09-29 stsp static int
716 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
717 0cf4efb1 2018-09-29 stsp {
718 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
719 0cf4efb1 2018-09-29 stsp return 0;
720 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
721 5c60c32a 2018-10-18 stsp }
722 5c60c32a 2018-10-18 stsp
723 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
724 a5d43cac 2022-07-01 thomas static int
725 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
726 a5d43cac 2022-07-01 thomas {
727 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
728 a5d43cac 2022-07-01 thomas }
729 a5d43cac 2022-07-01 thomas
730 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
731 5c60c32a 2018-10-18 stsp
732 5c60c32a 2018-10-18 stsp static const struct got_error *
733 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
734 5c60c32a 2018-10-18 stsp {
735 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
736 5c60c32a 2018-10-18 stsp
737 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
738 a5d43cac 2022-07-01 thomas view->begin_y = view_split_begin_y(view->nlines);
739 a5d43cac 2022-07-01 thomas view->begin_x = 0;
740 a5d43cac 2022-07-01 thomas } else {
741 a5d43cac 2022-07-01 thomas view->begin_x = view_split_begin_x(0);
742 a5d43cac 2022-07-01 thomas view->begin_y = 0;
743 a5d43cac 2022-07-01 thomas }
744 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
745 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
746 5c60c32a 2018-10-18 stsp view->lines = LINES;
747 5c60c32a 2018-10-18 stsp view->cols = COLS;
748 5c60c32a 2018-10-18 stsp err = view_resize(view);
749 5c60c32a 2018-10-18 stsp if (err)
750 5c60c32a 2018-10-18 stsp return err;
751 5c60c32a 2018-10-18 stsp
752 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
753 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
754 a5d43cac 2022-07-01 thomas
755 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
756 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
757 5c60c32a 2018-10-18 stsp
758 5c60c32a 2018-10-18 stsp return NULL;
759 5c60c32a 2018-10-18 stsp }
760 5c60c32a 2018-10-18 stsp
761 5c60c32a 2018-10-18 stsp static const struct got_error *
762 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
763 5c60c32a 2018-10-18 stsp {
764 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
765 5c60c32a 2018-10-18 stsp
766 5c60c32a 2018-10-18 stsp view->begin_x = 0;
767 5c60c32a 2018-10-18 stsp view->begin_y = 0;
768 5c60c32a 2018-10-18 stsp view->nlines = LINES;
769 5c60c32a 2018-10-18 stsp view->ncols = COLS;
770 5c60c32a 2018-10-18 stsp view->lines = LINES;
771 5c60c32a 2018-10-18 stsp view->cols = COLS;
772 5c60c32a 2018-10-18 stsp err = view_resize(view);
773 5c60c32a 2018-10-18 stsp if (err)
774 5c60c32a 2018-10-18 stsp return err;
775 5c60c32a 2018-10-18 stsp
776 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
777 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
778 5c60c32a 2018-10-18 stsp
779 5c60c32a 2018-10-18 stsp return NULL;
780 0cf4efb1 2018-09-29 stsp }
781 0cf4efb1 2018-09-29 stsp
782 5c60c32a 2018-10-18 stsp static int
783 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
784 5c60c32a 2018-10-18 stsp {
785 5c60c32a 2018-10-18 stsp return view->parent == NULL;
786 5c60c32a 2018-10-18 stsp }
787 5c60c32a 2018-10-18 stsp
788 b65b3ea0 2022-06-23 thomas static int
789 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
790 b65b3ea0 2022-06-23 thomas {
791 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
792 e44940c3 2022-07-01 thomas }
793 e44940c3 2022-07-01 thomas
794 e44940c3 2022-07-01 thomas static int
795 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
796 e44940c3 2022-07-01 thomas {
797 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
798 b65b3ea0 2022-06-23 thomas }
799 b65b3ea0 2022-06-23 thomas
800 444d5325 2022-07-03 thomas static int
801 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
802 444d5325 2022-07-03 thomas {
803 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
804 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
805 444d5325 2022-07-03 thomas }
806 444d5325 2022-07-03 thomas
807 a5d43cac 2022-07-01 thomas static void
808 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
809 a5d43cac 2022-07-01 thomas {
810 a5d43cac 2022-07-01 thomas PANEL *panel;
811 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
812 b65b3ea0 2022-06-23 thomas
813 a5d43cac 2022-07-01 thomas if (view->parent)
814 a5d43cac 2022-07-01 thomas return view_border(view->parent);
815 a5d43cac 2022-07-01 thomas
816 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
817 a5d43cac 2022-07-01 thomas if (panel == NULL)
818 a5d43cac 2022-07-01 thomas return;
819 a5d43cac 2022-07-01 thomas
820 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
821 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
822 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
823 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
824 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
825 a5d43cac 2022-07-01 thomas else
826 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
827 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
828 a5d43cac 2022-07-01 thomas }
829 a5d43cac 2022-07-01 thomas
830 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
831 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
832 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
833 a5d43cac 2022-07-01 thomas
834 4d8c2215 2018-08-19 stsp static const struct got_error *
835 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
836 f7d12f7e 2018-08-01 stsp {
837 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
838 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
839 f7d12f7e 2018-08-01 stsp
840 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
841 a5d43cac 2022-07-01 thomas
842 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
843 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
844 0cf4efb1 2018-09-29 stsp else
845 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
846 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
847 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
848 0cf4efb1 2018-09-29 stsp else
849 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
850 6d0fee91 2018-08-01 stsp
851 4918811f 2022-07-01 thomas if (view->child) {
852 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
853 a5d43cac 2022-07-01 thomas
854 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
855 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
856 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
857 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
858 40236d76 2022-06-23 thomas ncols = COLS;
859 40236d76 2022-06-23 thomas
860 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
861 5c60c32a 2018-10-18 stsp if (view->child->focussed)
862 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
863 5c60c32a 2018-10-18 stsp else
864 5c60c32a 2018-10-18 stsp show_panel(view->panel);
865 5c60c32a 2018-10-18 stsp } else {
866 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
867 40236d76 2022-06-23 thomas
868 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
869 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
870 5c60c32a 2018-10-18 stsp }
871 a5d43cac 2022-07-01 thomas /*
872 a5d43cac 2022-07-01 thomas * Request commits if terminal height was increased in a log
873 a5d43cac 2022-07-01 thomas * view so we have enough commits loaded to populate the view.
874 a5d43cac 2022-07-01 thomas */
875 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG && dif > 0) {
876 a5d43cac 2022-07-01 thomas struct tog_log_view_state *ts = &view->state.log;
877 a5d43cac 2022-07-01 thomas
878 a5d43cac 2022-07-01 thomas if (ts->commits.ncommits < ts->selected_entry->idx +
879 a5d43cac 2022-07-01 thomas view->lines - ts->selected) {
880 a5d43cac 2022-07-01 thomas view->nscrolled = ts->selected_entry->idx +
881 a5d43cac 2022-07-01 thomas view->lines - ts->selected -
882 a5d43cac 2022-07-01 thomas ts->commits.ncommits + dif;
883 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
884 a5d43cac 2022-07-01 thomas if (err)
885 a5d43cac 2022-07-01 thomas return err;
886 a5d43cac 2022-07-01 thomas }
887 a5d43cac 2022-07-01 thomas }
888 a5d43cac 2022-07-01 thomas
889 a5d43cac 2022-07-01 thomas /*
890 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
891 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
892 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
893 a5d43cac 2022-07-01 thomas */
894 a5d43cac 2022-07-01 thomas if (hs) {
895 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
896 a5d43cac 2022-07-01 thomas if (err)
897 a5d43cac 2022-07-01 thomas return err;
898 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
899 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
900 a5d43cac 2022-07-01 thomas if (err)
901 a5d43cac 2022-07-01 thomas return err;
902 a5d43cac 2022-07-01 thomas }
903 a5d43cac 2022-07-01 thomas view_border(view);
904 a5d43cac 2022-07-01 thomas update_panels();
905 a5d43cac 2022-07-01 thomas doupdate();
906 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
907 a5d43cac 2022-07-01 thomas nlines = view->nlines;
908 a5d43cac 2022-07-01 thomas }
909 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
910 40236d76 2022-06-23 thomas ncols = COLS;
911 669b5ffa 2018-10-07 stsp
912 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
913 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
914 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
915 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
916 40236d76 2022-06-23 thomas wclear(view->window);
917 40236d76 2022-06-23 thomas
918 40236d76 2022-06-23 thomas view->nlines = nlines;
919 40236d76 2022-06-23 thomas view->ncols = ncols;
920 40236d76 2022-06-23 thomas view->lines = LINES;
921 40236d76 2022-06-23 thomas view->cols = COLS;
922 40236d76 2022-06-23 thomas
923 5c60c32a 2018-10-18 stsp return NULL;
924 669b5ffa 2018-10-07 stsp }
925 669b5ffa 2018-10-07 stsp
926 669b5ffa 2018-10-07 stsp static const struct got_error *
927 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
928 669b5ffa 2018-10-07 stsp {
929 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
930 669b5ffa 2018-10-07 stsp
931 669b5ffa 2018-10-07 stsp if (view->child == NULL)
932 669b5ffa 2018-10-07 stsp return NULL;
933 669b5ffa 2018-10-07 stsp
934 669b5ffa 2018-10-07 stsp err = view_close(view->child);
935 669b5ffa 2018-10-07 stsp view->child = NULL;
936 669b5ffa 2018-10-07 stsp return err;
937 669b5ffa 2018-10-07 stsp }
938 669b5ffa 2018-10-07 stsp
939 40236d76 2022-06-23 thomas static const struct got_error *
940 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
941 669b5ffa 2018-10-07 stsp {
942 669b5ffa 2018-10-07 stsp view->child = child;
943 669b5ffa 2018-10-07 stsp child->parent = view;
944 40236d76 2022-06-23 thomas
945 40236d76 2022-06-23 thomas return view_resize(view);
946 bfddd0d9 2018-09-29 stsp }
947 bfddd0d9 2018-09-29 stsp
948 34bc9ec9 2019-02-22 stsp static void
949 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
950 25791caa 2018-10-24 stsp {
951 25791caa 2018-10-24 stsp int cols, lines;
952 25791caa 2018-10-24 stsp struct winsize size;
953 25791caa 2018-10-24 stsp
954 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
955 25791caa 2018-10-24 stsp cols = 80; /* Default */
956 25791caa 2018-10-24 stsp lines = 24;
957 25791caa 2018-10-24 stsp } else {
958 25791caa 2018-10-24 stsp cols = size.ws_col;
959 25791caa 2018-10-24 stsp lines = size.ws_row;
960 25791caa 2018-10-24 stsp }
961 25791caa 2018-10-24 stsp resize_term(lines, cols);
962 2b49a8ae 2019-06-22 stsp }
963 2b49a8ae 2019-06-22 stsp
964 2b49a8ae 2019-06-22 stsp static const struct got_error *
965 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
966 2b49a8ae 2019-06-22 stsp {
967 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
968 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
969 2b49a8ae 2019-06-22 stsp char pattern[1024];
970 2b49a8ae 2019-06-22 stsp int ret;
971 c0c4acc8 2021-01-24 stsp
972 c0c4acc8 2021-01-24 stsp if (view->search_started) {
973 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
974 c0c4acc8 2021-01-24 stsp view->searching = 0;
975 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
976 c0c4acc8 2021-01-24 stsp }
977 c0c4acc8 2021-01-24 stsp view->search_started = 0;
978 2b49a8ae 2019-06-22 stsp
979 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
980 2b49a8ae 2019-06-22 stsp return NULL;
981 2b49a8ae 2019-06-22 stsp
982 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
983 a5d43cac 2022-07-01 thomas v = view->child;
984 2b49a8ae 2019-06-22 stsp
985 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
986 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
987 a5d43cac 2022-07-01 thomas
988 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
989 2b49a8ae 2019-06-22 stsp nocbreak();
990 2b49a8ae 2019-06-22 stsp echo();
991 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
992 a5d43cac 2022-07-01 thomas wrefresh(v->window);
993 2b49a8ae 2019-06-22 stsp cbreak();
994 2b49a8ae 2019-06-22 stsp noecho();
995 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
996 2b49a8ae 2019-06-22 stsp if (ret == ERR)
997 2b49a8ae 2019-06-22 stsp return NULL;
998 2b49a8ae 2019-06-22 stsp
999 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1000 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1001 7c32bd05 2019-06-22 stsp if (err) {
1002 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1003 7c32bd05 2019-06-22 stsp return err;
1004 7c32bd05 2019-06-22 stsp }
1005 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1006 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1007 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1008 2b49a8ae 2019-06-22 stsp view->search_next(view);
1009 2b49a8ae 2019-06-22 stsp }
1010 2b49a8ae 2019-06-22 stsp
1011 2b49a8ae 2019-06-22 stsp return NULL;
1012 0cf4efb1 2018-09-29 stsp }
1013 6d0fee91 2018-08-01 stsp
1014 07b0611c 2022-06-23 thomas /*
1015 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1016 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1017 07b0611c 2022-06-23 thomas */
1018 07b0611c 2022-06-23 thomas static int
1019 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1020 07b0611c 2022-06-23 thomas {
1021 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1022 a5d43cac 2022-07-01 thomas int x, n = 0;
1023 07b0611c 2022-06-23 thomas
1024 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1025 a5d43cac 2022-07-01 thomas v = view->child;
1026 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1027 a5d43cac 2022-07-01 thomas v = view->parent;
1028 a5d43cac 2022-07-01 thomas
1029 07b0611c 2022-06-23 thomas view->count = 0;
1030 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1031 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1032 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1033 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1034 07b0611c 2022-06-23 thomas
1035 07b0611c 2022-06-23 thomas do {
1036 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1037 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1038 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1039 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1040 a5d43cac 2022-07-01 thomas }
1041 a5d43cac 2022-07-01 thomas
1042 07b0611c 2022-06-23 thomas /*
1043 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1044 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1045 07b0611c 2022-06-23 thomas */
1046 07b0611c 2022-06-23 thomas if (n >= 9999999)
1047 07b0611c 2022-06-23 thomas n = 9999999;
1048 07b0611c 2022-06-23 thomas else
1049 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1050 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1051 07b0611c 2022-06-23 thomas
1052 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1053 07b0611c 2022-06-23 thomas view->count = n;
1054 07b0611c 2022-06-23 thomas
1055 07b0611c 2022-06-23 thomas return c;
1056 07b0611c 2022-06-23 thomas }
1057 07b0611c 2022-06-23 thomas
1058 0cf4efb1 2018-09-29 stsp static const struct got_error *
1059 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1060 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1061 e5a0f69f 2018-08-18 stsp {
1062 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1063 669b5ffa 2018-10-07 stsp struct tog_view *v;
1064 1a76625f 2018-10-22 stsp int ch, errcode;
1065 e5a0f69f 2018-08-18 stsp
1066 e5a0f69f 2018-08-18 stsp *new = NULL;
1067 8f4ed634 2020-03-26 stsp
1068 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1069 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1070 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1071 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1072 07b0611c 2022-06-23 thomas view->count = 0;
1073 07b0611c 2022-06-23 thomas }
1074 e5a0f69f 2018-08-18 stsp
1075 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1076 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1077 82954512 2020-02-03 stsp if (errcode)
1078 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1079 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1080 3da8ef85 2021-09-21 stsp sched_yield();
1081 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1082 82954512 2020-02-03 stsp if (errcode)
1083 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1084 82954512 2020-02-03 stsp "pthread_mutex_lock");
1085 60493ae3 2019-06-20 stsp view->search_next(view);
1086 60493ae3 2019-06-20 stsp return NULL;
1087 60493ae3 2019-06-20 stsp }
1088 60493ae3 2019-06-20 stsp
1089 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1090 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1091 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1092 1a76625f 2018-10-22 stsp if (errcode)
1093 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1094 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1095 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1096 634cb454 2022-07-03 thomas cbreak();
1097 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1098 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1099 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1100 634cb454 2022-07-03 thomas view->count = 0;
1101 634cb454 2022-07-03 thomas else
1102 634cb454 2022-07-03 thomas ch = view->ch;
1103 634cb454 2022-07-03 thomas } else {
1104 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1105 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1106 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1107 07b0611c 2022-06-23 thomas }
1108 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1109 1a76625f 2018-10-22 stsp if (errcode)
1110 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1111 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1112 25791caa 2018-10-24 stsp
1113 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1114 25791caa 2018-10-24 stsp tog_resizeterm();
1115 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1116 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1117 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1118 25791caa 2018-10-24 stsp err = view_resize(v);
1119 25791caa 2018-10-24 stsp if (err)
1120 25791caa 2018-10-24 stsp return err;
1121 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1122 25791caa 2018-10-24 stsp if (err)
1123 25791caa 2018-10-24 stsp return err;
1124 cdfcfb03 2020-12-06 stsp if (v->child) {
1125 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1126 cdfcfb03 2020-12-06 stsp if (err)
1127 cdfcfb03 2020-12-06 stsp return err;
1128 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1129 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1130 cdfcfb03 2020-12-06 stsp if (err)
1131 cdfcfb03 2020-12-06 stsp return err;
1132 cdfcfb03 2020-12-06 stsp }
1133 25791caa 2018-10-24 stsp }
1134 25791caa 2018-10-24 stsp }
1135 25791caa 2018-10-24 stsp
1136 e5a0f69f 2018-08-18 stsp switch (ch) {
1137 1e37a5c2 2019-05-12 jcs case '\t':
1138 07b0611c 2022-06-23 thomas view->count = 0;
1139 1e37a5c2 2019-05-12 jcs if (view->child) {
1140 e78dc838 2020-12-04 stsp view->focussed = 0;
1141 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1142 e78dc838 2020-12-04 stsp view->focus_child = 1;
1143 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1144 e78dc838 2020-12-04 stsp view->focussed = 0;
1145 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1146 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1147 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1148 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1149 a5d43cac 2022-07-01 thomas view->parent->type == TOG_VIEW_LOG) {
1150 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1151 a5d43cac 2022-07-01 thomas if (err)
1152 a5d43cac 2022-07-01 thomas return err;
1153 a5d43cac 2022-07-01 thomas }
1154 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1155 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1156 a5d43cac 2022-07-01 thomas if (err)
1157 a5d43cac 2022-07-01 thomas return err;
1158 a5d43cac 2022-07-01 thomas }
1159 1e37a5c2 2019-05-12 jcs }
1160 1e37a5c2 2019-05-12 jcs break;
1161 1e37a5c2 2019-05-12 jcs case 'q':
1162 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1163 a5d43cac 2022-07-01 thomas if (view->parent->type == TOG_VIEW_LOG) {
1164 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1165 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1166 a5d43cac 2022-07-01 thomas if (err)
1167 a5d43cac 2022-07-01 thomas break;
1168 a5d43cac 2022-07-01 thomas }
1169 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1170 a5d43cac 2022-07-01 thomas view->parent->mode = TOG_VIEW_SPLIT_NONE;
1171 a5d43cac 2022-07-01 thomas }
1172 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1173 9970f7fc 2020-12-03 stsp view->dying = 1;
1174 1e37a5c2 2019-05-12 jcs break;
1175 1e37a5c2 2019-05-12 jcs case 'Q':
1176 1e37a5c2 2019-05-12 jcs *done = 1;
1177 1e37a5c2 2019-05-12 jcs break;
1178 1c5e5faa 2022-06-23 thomas case 'F':
1179 07b0611c 2022-06-23 thomas view->count = 0;
1180 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1181 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1182 1e37a5c2 2019-05-12 jcs break;
1183 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1184 e78dc838 2020-12-04 stsp view->focussed = 0;
1185 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1186 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1187 1e37a5c2 2019-05-12 jcs } else
1188 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1189 1e37a5c2 2019-05-12 jcs if (err)
1190 1e37a5c2 2019-05-12 jcs break;
1191 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1192 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1193 1e37a5c2 2019-05-12 jcs } else {
1194 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1195 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1196 e78dc838 2020-12-04 stsp view->focussed = 1;
1197 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1198 1e37a5c2 2019-05-12 jcs } else {
1199 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1200 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1201 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1202 669b5ffa 2018-10-07 stsp }
1203 1e37a5c2 2019-05-12 jcs if (err)
1204 1e37a5c2 2019-05-12 jcs break;
1205 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1206 a5d43cac 2022-07-01 thomas }
1207 a5d43cac 2022-07-01 thomas if (err)
1208 a5d43cac 2022-07-01 thomas break;
1209 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG) {
1210 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
1211 a5d43cac 2022-07-01 thomas if (err)
1212 a5d43cac 2022-07-01 thomas break;
1213 1e37a5c2 2019-05-12 jcs }
1214 a5d43cac 2022-07-01 thomas if (view->parent)
1215 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1216 a5d43cac 2022-07-01 thomas if (!err)
1217 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1218 1e37a5c2 2019-05-12 jcs break;
1219 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1220 60493ae3 2019-06-20 stsp break;
1221 60493ae3 2019-06-20 stsp case '/':
1222 07b0611c 2022-06-23 thomas view->count = 0;
1223 60493ae3 2019-06-20 stsp if (view->search_start)
1224 2b49a8ae 2019-06-22 stsp view_search_start(view);
1225 60493ae3 2019-06-20 stsp else
1226 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1227 1e37a5c2 2019-05-12 jcs break;
1228 b1bf1435 2019-06-21 stsp case 'N':
1229 60493ae3 2019-06-20 stsp case 'n':
1230 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1231 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1232 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1233 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1234 60493ae3 2019-06-20 stsp view->search_next(view);
1235 60493ae3 2019-06-20 stsp } else
1236 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1237 adf4c9e0 2022-07-03 thomas break;
1238 adf4c9e0 2022-07-03 thomas case 'A':
1239 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1240 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1241 adf4c9e0 2022-07-03 thomas else
1242 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1243 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1244 adf4c9e0 2022-07-03 thomas if (v->reset) {
1245 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1246 adf4c9e0 2022-07-03 thomas if (err)
1247 adf4c9e0 2022-07-03 thomas return err;
1248 adf4c9e0 2022-07-03 thomas }
1249 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1250 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1251 adf4c9e0 2022-07-03 thomas if (err)
1252 adf4c9e0 2022-07-03 thomas return err;
1253 adf4c9e0 2022-07-03 thomas }
1254 adf4c9e0 2022-07-03 thomas }
1255 60493ae3 2019-06-20 stsp break;
1256 1e37a5c2 2019-05-12 jcs default:
1257 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1258 1e37a5c2 2019-05-12 jcs break;
1259 e5a0f69f 2018-08-18 stsp }
1260 e5a0f69f 2018-08-18 stsp
1261 e5a0f69f 2018-08-18 stsp return err;
1262 bcbd79e2 2018-08-19 stsp }
1263 bcbd79e2 2018-08-19 stsp
1264 ef20f542 2022-06-26 thomas static int
1265 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1266 a3404814 2018-09-02 stsp {
1267 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1268 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1269 669b5ffa 2018-10-07 stsp return 0;
1270 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1271 669b5ffa 2018-10-07 stsp return 0;
1272 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1273 a3404814 2018-09-02 stsp return 0;
1274 a3404814 2018-09-02 stsp
1275 669b5ffa 2018-10-07 stsp return view->focussed;
1276 a3404814 2018-09-02 stsp }
1277 a3404814 2018-09-02 stsp
1278 bcbd79e2 2018-08-19 stsp static const struct got_error *
1279 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1280 e5a0f69f 2018-08-18 stsp {
1281 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1282 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1283 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1284 fd823528 2018-10-22 stsp int fast_refresh = 10;
1285 1a76625f 2018-10-22 stsp int done = 0, errcode;
1286 e5a0f69f 2018-08-18 stsp
1287 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1288 1a76625f 2018-10-22 stsp if (errcode)
1289 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1290 1a76625f 2018-10-22 stsp
1291 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1292 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1293 e5a0f69f 2018-08-18 stsp
1294 1004088d 2018-09-29 stsp view->focussed = 1;
1295 878940b7 2018-09-29 stsp err = view->show(view);
1296 0cf4efb1 2018-09-29 stsp if (err)
1297 0cf4efb1 2018-09-29 stsp return err;
1298 0cf4efb1 2018-09-29 stsp update_panels();
1299 0cf4efb1 2018-09-29 stsp doupdate();
1300 296152d1 2022-05-31 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1301 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1302 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1303 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1304 fd823528 2018-10-22 stsp
1305 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1306 e5a0f69f 2018-08-18 stsp if (err)
1307 e5a0f69f 2018-08-18 stsp break;
1308 9970f7fc 2020-12-03 stsp if (view->dying) {
1309 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1310 669b5ffa 2018-10-07 stsp
1311 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1312 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1313 9970f7fc 2020-12-03 stsp entry);
1314 e78dc838 2020-12-04 stsp else if (view->parent)
1315 669b5ffa 2018-10-07 stsp prev = view->parent;
1316 669b5ffa 2018-10-07 stsp
1317 e78dc838 2020-12-04 stsp if (view->parent) {
1318 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1319 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1320 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1321 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1322 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1323 40236d76 2022-06-23 thomas if (err)
1324 40236d76 2022-06-23 thomas break;
1325 e78dc838 2020-12-04 stsp } else
1326 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1327 669b5ffa 2018-10-07 stsp
1328 9970f7fc 2020-12-03 stsp err = view_close(view);
1329 fb59748f 2020-12-05 stsp if (err)
1330 e5a0f69f 2018-08-18 stsp goto done;
1331 669b5ffa 2018-10-07 stsp
1332 e78dc838 2020-12-04 stsp view = NULL;
1333 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1334 e78dc838 2020-12-04 stsp if (v->focussed)
1335 e78dc838 2020-12-04 stsp break;
1336 0cf4efb1 2018-09-29 stsp }
1337 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1338 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1339 e78dc838 2020-12-04 stsp if (prev)
1340 e78dc838 2020-12-04 stsp view = prev;
1341 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1342 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1343 e78dc838 2020-12-04 stsp tog_view_list_head);
1344 e78dc838 2020-12-04 stsp }
1345 e78dc838 2020-12-04 stsp if (view) {
1346 e78dc838 2020-12-04 stsp if (view->focus_child) {
1347 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1348 e78dc838 2020-12-04 stsp view = view->child;
1349 e78dc838 2020-12-04 stsp } else
1350 e78dc838 2020-12-04 stsp view->focussed = 1;
1351 e78dc838 2020-12-04 stsp }
1352 e78dc838 2020-12-04 stsp }
1353 e5a0f69f 2018-08-18 stsp }
1354 bcbd79e2 2018-08-19 stsp if (new_view) {
1355 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1356 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1357 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1358 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1359 86c66b02 2018-10-18 stsp continue;
1360 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1361 86c66b02 2018-10-18 stsp err = view_close(v);
1362 86c66b02 2018-10-18 stsp if (err)
1363 86c66b02 2018-10-18 stsp goto done;
1364 86c66b02 2018-10-18 stsp break;
1365 86c66b02 2018-10-18 stsp }
1366 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1367 fed7eaa8 2018-10-24 stsp view = new_view;
1368 7cd52833 2022-06-23 thomas }
1369 669b5ffa 2018-10-07 stsp if (view) {
1370 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1371 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1372 e78dc838 2020-12-04 stsp view = view->child;
1373 e78dc838 2020-12-04 stsp } else {
1374 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1375 e78dc838 2020-12-04 stsp view = view->parent;
1376 1a76625f 2018-10-22 stsp }
1377 e78dc838 2020-12-04 stsp show_panel(view->panel);
1378 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1379 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1380 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1381 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1382 669b5ffa 2018-10-07 stsp if (err)
1383 1a76625f 2018-10-22 stsp goto done;
1384 669b5ffa 2018-10-07 stsp }
1385 669b5ffa 2018-10-07 stsp err = view->show(view);
1386 0cf4efb1 2018-09-29 stsp if (err)
1387 1a76625f 2018-10-22 stsp goto done;
1388 669b5ffa 2018-10-07 stsp if (view->child) {
1389 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1390 669b5ffa 2018-10-07 stsp if (err)
1391 1a76625f 2018-10-22 stsp goto done;
1392 669b5ffa 2018-10-07 stsp }
1393 1a76625f 2018-10-22 stsp update_panels();
1394 1a76625f 2018-10-22 stsp doupdate();
1395 0cf4efb1 2018-09-29 stsp }
1396 e5a0f69f 2018-08-18 stsp }
1397 e5a0f69f 2018-08-18 stsp done:
1398 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1399 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1400 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1401 e5a0f69f 2018-08-18 stsp view_close(view);
1402 e5a0f69f 2018-08-18 stsp }
1403 1a76625f 2018-10-22 stsp
1404 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1405 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1406 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1407 1a76625f 2018-10-22 stsp
1408 e5a0f69f 2018-08-18 stsp return err;
1409 ea5e7bb5 2018-08-01 stsp }
1410 ea5e7bb5 2018-08-01 stsp
1411 4ed7e80c 2018-05-20 stsp __dead static void
1412 9f7d7167 2018-04-29 stsp usage_log(void)
1413 9f7d7167 2018-04-29 stsp {
1414 80ddbec8 2018-04-29 stsp endwin();
1415 c70c5802 2018-08-01 stsp fprintf(stderr,
1416 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1417 9f7d7167 2018-04-29 stsp getprogname());
1418 9f7d7167 2018-04-29 stsp exit(1);
1419 80ddbec8 2018-04-29 stsp }
1420 80ddbec8 2018-04-29 stsp
1421 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1422 80ddbec8 2018-04-29 stsp static const struct got_error *
1423 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1424 963b370f 2018-05-20 stsp {
1425 00dfcb92 2018-06-11 stsp char *vis = NULL;
1426 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1427 963b370f 2018-05-20 stsp
1428 963b370f 2018-05-20 stsp *ws = NULL;
1429 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1430 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1431 00dfcb92 2018-06-11 stsp int vislen;
1432 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1433 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1434 00dfcb92 2018-06-11 stsp
1435 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1436 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1437 00dfcb92 2018-06-11 stsp if (err)
1438 00dfcb92 2018-06-11 stsp return err;
1439 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1440 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1441 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1442 a7f50699 2018-06-11 stsp goto done;
1443 a7f50699 2018-06-11 stsp }
1444 00dfcb92 2018-06-11 stsp }
1445 963b370f 2018-05-20 stsp
1446 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1447 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1448 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1449 a7f50699 2018-06-11 stsp goto done;
1450 a7f50699 2018-06-11 stsp }
1451 963b370f 2018-05-20 stsp
1452 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1453 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1454 a7f50699 2018-06-11 stsp done:
1455 00dfcb92 2018-06-11 stsp free(vis);
1456 963b370f 2018-05-20 stsp if (err) {
1457 963b370f 2018-05-20 stsp free(*ws);
1458 963b370f 2018-05-20 stsp *ws = NULL;
1459 963b370f 2018-05-20 stsp *wlen = 0;
1460 963b370f 2018-05-20 stsp }
1461 963b370f 2018-05-20 stsp return err;
1462 05171be4 2022-06-23 thomas }
1463 05171be4 2022-06-23 thomas
1464 05171be4 2022-06-23 thomas static const struct got_error *
1465 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1466 05171be4 2022-06-23 thomas {
1467 05171be4 2022-06-23 thomas char *dst;
1468 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1469 05171be4 2022-06-23 thomas
1470 05171be4 2022-06-23 thomas *ptr = NULL;
1471 05171be4 2022-06-23 thomas n = len = strlen(src);
1472 83316834 2022-06-23 thomas dst = malloc(n + 1);
1473 05171be4 2022-06-23 thomas if (dst == NULL)
1474 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1475 05171be4 2022-06-23 thomas
1476 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1477 05171be4 2022-06-23 thomas const char c = src[idx];
1478 05171be4 2022-06-23 thomas
1479 05171be4 2022-06-23 thomas if (c == '\t') {
1480 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1481 b235ad5d 2022-06-23 thomas char *p;
1482 b235ad5d 2022-06-23 thomas
1483 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1484 83316834 2022-06-23 thomas if (p == NULL) {
1485 83316834 2022-06-23 thomas free(dst);
1486 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1487 83316834 2022-06-23 thomas
1488 83316834 2022-06-23 thomas }
1489 83316834 2022-06-23 thomas dst = p;
1490 05171be4 2022-06-23 thomas n += nb;
1491 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1492 05171be4 2022-06-23 thomas sz += nb;
1493 05171be4 2022-06-23 thomas } else
1494 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1495 05171be4 2022-06-23 thomas ++idx;
1496 05171be4 2022-06-23 thomas }
1497 05171be4 2022-06-23 thomas
1498 05171be4 2022-06-23 thomas dst[sz] = '\0';
1499 05171be4 2022-06-23 thomas *ptr = dst;
1500 05171be4 2022-06-23 thomas return NULL;
1501 963b370f 2018-05-20 stsp }
1502 963b370f 2018-05-20 stsp
1503 8d208d34 2022-06-23 thomas /*
1504 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1505 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1506 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1507 8d208d34 2022-06-23 thomas * *rcol.
1508 f91a2b48 2022-06-23 thomas */
1509 8d208d34 2022-06-23 thomas static int
1510 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1511 8d208d34 2022-06-23 thomas {
1512 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1513 f91a2b48 2022-06-23 thomas
1514 8d208d34 2022-06-23 thomas if (n == 0) {
1515 8d208d34 2022-06-23 thomas *rcol = cols;
1516 8d208d34 2022-06-23 thomas return off;
1517 8d208d34 2022-06-23 thomas }
1518 f91a2b48 2022-06-23 thomas
1519 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1520 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1521 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1522 8d208d34 2022-06-23 thomas else
1523 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1524 f91a2b48 2022-06-23 thomas
1525 8d208d34 2022-06-23 thomas if (width == -1) {
1526 8d208d34 2022-06-23 thomas width = 1;
1527 8d208d34 2022-06-23 thomas wline[i] = L'.';
1528 f91a2b48 2022-06-23 thomas }
1529 f91a2b48 2022-06-23 thomas
1530 8d208d34 2022-06-23 thomas if (cols + width > n)
1531 8d208d34 2022-06-23 thomas break;
1532 8d208d34 2022-06-23 thomas cols += width;
1533 f91a2b48 2022-06-23 thomas }
1534 f91a2b48 2022-06-23 thomas
1535 8d208d34 2022-06-23 thomas *rcol = cols;
1536 8d208d34 2022-06-23 thomas return i;
1537 f91a2b48 2022-06-23 thomas }
1538 f91a2b48 2022-06-23 thomas
1539 f91a2b48 2022-06-23 thomas /*
1540 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1541 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1542 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1543 f91a2b48 2022-06-23 thomas */
1544 f91a2b48 2022-06-23 thomas static const struct got_error *
1545 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1546 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1547 f91a2b48 2022-06-23 thomas {
1548 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1549 8d208d34 2022-06-23 thomas int cols;
1550 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1551 05171be4 2022-06-23 thomas char *exstr = NULL;
1552 963b370f 2018-05-20 stsp size_t wlen;
1553 8d208d34 2022-06-23 thomas int i, scrollx;
1554 963b370f 2018-05-20 stsp
1555 963b370f 2018-05-20 stsp *wlinep = NULL;
1556 b700b5d6 2018-07-10 stsp *widthp = 0;
1557 963b370f 2018-05-20 stsp
1558 05171be4 2022-06-23 thomas if (expand) {
1559 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1560 05171be4 2022-06-23 thomas if (err)
1561 05171be4 2022-06-23 thomas return err;
1562 05171be4 2022-06-23 thomas }
1563 05171be4 2022-06-23 thomas
1564 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1565 05171be4 2022-06-23 thomas free(exstr);
1566 963b370f 2018-05-20 stsp if (err)
1567 963b370f 2018-05-20 stsp return err;
1568 963b370f 2018-05-20 stsp
1569 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1570 f91a2b48 2022-06-23 thomas
1571 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1572 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1573 3f670bfb 2020-12-10 stsp wlen--;
1574 3f670bfb 2020-12-10 stsp }
1575 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1576 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1577 3f670bfb 2020-12-10 stsp wlen--;
1578 3f670bfb 2020-12-10 stsp }
1579 3f670bfb 2020-12-10 stsp
1580 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1581 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1582 27a741e5 2019-09-11 stsp
1583 b700b5d6 2018-07-10 stsp if (widthp)
1584 b700b5d6 2018-07-10 stsp *widthp = cols;
1585 f91a2b48 2022-06-23 thomas if (scrollxp)
1586 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1587 963b370f 2018-05-20 stsp if (err)
1588 963b370f 2018-05-20 stsp free(wline);
1589 963b370f 2018-05-20 stsp else
1590 963b370f 2018-05-20 stsp *wlinep = wline;
1591 963b370f 2018-05-20 stsp return err;
1592 963b370f 2018-05-20 stsp }
1593 963b370f 2018-05-20 stsp
1594 8b473291 2019-02-21 stsp static const struct got_error*
1595 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1596 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1597 8b473291 2019-02-21 stsp {
1598 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1599 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1600 8b473291 2019-02-21 stsp char *s;
1601 8b473291 2019-02-21 stsp const char *name;
1602 8b473291 2019-02-21 stsp
1603 8b473291 2019-02-21 stsp *refs_str = NULL;
1604 8b473291 2019-02-21 stsp
1605 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1606 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1607 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1608 52b5abe1 2019-08-13 stsp int cmp;
1609 52b5abe1 2019-08-13 stsp
1610 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1611 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1612 8b473291 2019-02-21 stsp continue;
1613 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1614 8b473291 2019-02-21 stsp name += 5;
1615 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1616 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1617 7143d404 2019-03-12 stsp continue;
1618 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1619 8b473291 2019-02-21 stsp name += 6;
1620 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1621 8b473291 2019-02-21 stsp name += 8;
1622 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1623 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1624 79cc719f 2020-04-24 stsp continue;
1625 79cc719f 2020-04-24 stsp }
1626 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1627 48cae60d 2020-09-22 stsp if (err)
1628 48cae60d 2020-09-22 stsp break;
1629 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1630 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1631 5d844a1e 2019-08-13 stsp if (err) {
1632 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1633 48cae60d 2020-09-22 stsp free(ref_id);
1634 5d844a1e 2019-08-13 stsp break;
1635 48cae60d 2020-09-22 stsp }
1636 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1637 5d844a1e 2019-08-13 stsp err = NULL;
1638 5d844a1e 2019-08-13 stsp tag = NULL;
1639 5d844a1e 2019-08-13 stsp }
1640 52b5abe1 2019-08-13 stsp }
1641 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1642 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1643 48cae60d 2020-09-22 stsp free(ref_id);
1644 52b5abe1 2019-08-13 stsp if (tag)
1645 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1646 52b5abe1 2019-08-13 stsp if (cmp != 0)
1647 52b5abe1 2019-08-13 stsp continue;
1648 8b473291 2019-02-21 stsp s = *refs_str;
1649 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1650 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1651 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1652 8b473291 2019-02-21 stsp free(s);
1653 8b473291 2019-02-21 stsp *refs_str = NULL;
1654 8b473291 2019-02-21 stsp break;
1655 8b473291 2019-02-21 stsp }
1656 8b473291 2019-02-21 stsp free(s);
1657 8b473291 2019-02-21 stsp }
1658 8b473291 2019-02-21 stsp
1659 8b473291 2019-02-21 stsp return err;
1660 8b473291 2019-02-21 stsp }
1661 8b473291 2019-02-21 stsp
1662 963b370f 2018-05-20 stsp static const struct got_error *
1663 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1664 27a741e5 2019-09-11 stsp int col_tab_align)
1665 5813d178 2019-03-09 stsp {
1666 e6b8b890 2020-12-29 naddy char *smallerthan;
1667 5813d178 2019-03-09 stsp
1668 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1669 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1670 5813d178 2019-03-09 stsp author = smallerthan + 1;
1671 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1672 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1673 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1674 5813d178 2019-03-09 stsp }
1675 5813d178 2019-03-09 stsp
1676 5813d178 2019-03-09 stsp static const struct got_error *
1677 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1678 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1679 8fdc79fe 2020-12-01 naddy int author_display_cols)
1680 80ddbec8 2018-04-29 stsp {
1681 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1682 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1683 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1684 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1685 5813d178 2019-03-09 stsp char *author = NULL;
1686 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1687 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1688 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1689 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1690 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1691 ccb26ccd 2018-11-05 stsp struct tm tm;
1692 45d799e2 2018-12-23 stsp time_t committer_time;
1693 11b20872 2019-11-08 stsp struct tog_color *tc;
1694 80ddbec8 2018-04-29 stsp
1695 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1696 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1697 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1698 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1699 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1700 b39d25c7 2018-07-10 stsp
1701 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1702 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1703 b39d25c7 2018-07-10 stsp else
1704 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1705 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1706 11b20872 2019-11-08 stsp if (tc)
1707 11b20872 2019-11-08 stsp wattr_on(view->window,
1708 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1709 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1710 11b20872 2019-11-08 stsp if (tc)
1711 11b20872 2019-11-08 stsp wattr_off(view->window,
1712 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1713 27a741e5 2019-09-11 stsp col = limit;
1714 b39d25c7 2018-07-10 stsp if (col > avail)
1715 b39d25c7 2018-07-10 stsp goto done;
1716 6570a66d 2019-11-08 stsp
1717 6570a66d 2019-11-08 stsp if (avail >= 120) {
1718 6570a66d 2019-11-08 stsp char *id_str;
1719 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1720 6570a66d 2019-11-08 stsp if (err)
1721 6570a66d 2019-11-08 stsp goto done;
1722 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1723 11b20872 2019-11-08 stsp if (tc)
1724 11b20872 2019-11-08 stsp wattr_on(view->window,
1725 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1726 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1727 11b20872 2019-11-08 stsp if (tc)
1728 11b20872 2019-11-08 stsp wattr_off(view->window,
1729 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1730 6570a66d 2019-11-08 stsp free(id_str);
1731 6570a66d 2019-11-08 stsp col += 9;
1732 6570a66d 2019-11-08 stsp if (col > avail)
1733 6570a66d 2019-11-08 stsp goto done;
1734 6570a66d 2019-11-08 stsp }
1735 b39d25c7 2018-07-10 stsp
1736 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1737 5813d178 2019-03-09 stsp if (author == NULL) {
1738 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1739 80ddbec8 2018-04-29 stsp goto done;
1740 80ddbec8 2018-04-29 stsp }
1741 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1742 bb737323 2018-05-20 stsp if (err)
1743 bb737323 2018-05-20 stsp goto done;
1744 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1745 11b20872 2019-11-08 stsp if (tc)
1746 11b20872 2019-11-08 stsp wattr_on(view->window,
1747 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1748 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1749 11b20872 2019-11-08 stsp if (tc)
1750 11b20872 2019-11-08 stsp wattr_off(view->window,
1751 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1752 bb737323 2018-05-20 stsp col += author_width;
1753 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1754 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1755 bb737323 2018-05-20 stsp col++;
1756 bb737323 2018-05-20 stsp author_width++;
1757 bb737323 2018-05-20 stsp }
1758 9c2eaf34 2018-05-20 stsp if (col > avail)
1759 9c2eaf34 2018-05-20 stsp goto done;
1760 80ddbec8 2018-04-29 stsp
1761 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1762 5943eee2 2019-08-13 stsp if (err)
1763 6d9fbc00 2018-04-29 stsp goto done;
1764 bb737323 2018-05-20 stsp logmsg = logmsg0;
1765 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1766 bb737323 2018-05-20 stsp logmsg++;
1767 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1768 bb737323 2018-05-20 stsp if (newline)
1769 bb737323 2018-05-20 stsp *newline = '\0';
1770 f91a2b48 2022-06-23 thomas limit = avail - col;
1771 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
1772 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
1773 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1774 f91a2b48 2022-06-23 thomas limit, col, 1);
1775 331b1a16 2022-06-23 thomas if (err)
1776 331b1a16 2022-06-23 thomas goto done;
1777 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
1778 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
1779 27a741e5 2019-09-11 stsp while (col < avail) {
1780 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1781 bb737323 2018-05-20 stsp col++;
1782 881b2d3e 2018-04-30 stsp }
1783 80ddbec8 2018-04-29 stsp done:
1784 80ddbec8 2018-04-29 stsp free(logmsg0);
1785 bb737323 2018-05-20 stsp free(wlogmsg);
1786 5813d178 2019-03-09 stsp free(author);
1787 bb737323 2018-05-20 stsp free(wauthor);
1788 80ddbec8 2018-04-29 stsp free(line);
1789 80ddbec8 2018-04-29 stsp return err;
1790 80ddbec8 2018-04-29 stsp }
1791 26ed57b2 2018-05-19 stsp
1792 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1793 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1794 899d86c2 2018-05-10 stsp struct got_object_id *id)
1795 80ddbec8 2018-04-29 stsp {
1796 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1797 80ddbec8 2018-04-29 stsp
1798 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1799 80ddbec8 2018-04-29 stsp if (entry == NULL)
1800 899d86c2 2018-05-10 stsp return NULL;
1801 99db9666 2018-05-07 stsp
1802 899d86c2 2018-05-10 stsp entry->id = id;
1803 99db9666 2018-05-07 stsp entry->commit = commit;
1804 899d86c2 2018-05-10 stsp return entry;
1805 99db9666 2018-05-07 stsp }
1806 80ddbec8 2018-04-29 stsp
1807 99db9666 2018-05-07 stsp static void
1808 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1809 99db9666 2018-05-07 stsp {
1810 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1811 99db9666 2018-05-07 stsp
1812 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1813 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1814 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1815 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1816 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1817 99db9666 2018-05-07 stsp free(entry);
1818 99db9666 2018-05-07 stsp }
1819 99db9666 2018-05-07 stsp
1820 99db9666 2018-05-07 stsp static void
1821 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1822 99db9666 2018-05-07 stsp {
1823 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1824 99db9666 2018-05-07 stsp pop_commit(commits);
1825 c4972b91 2018-05-07 stsp }
1826 c4972b91 2018-05-07 stsp
1827 c4972b91 2018-05-07 stsp static const struct got_error *
1828 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1829 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1830 13add988 2019-10-15 stsp {
1831 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1832 13add988 2019-10-15 stsp regmatch_t regmatch;
1833 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1834 13add988 2019-10-15 stsp
1835 13add988 2019-10-15 stsp *have_match = 0;
1836 13add988 2019-10-15 stsp
1837 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1838 13add988 2019-10-15 stsp if (err)
1839 13add988 2019-10-15 stsp return err;
1840 13add988 2019-10-15 stsp
1841 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1842 13add988 2019-10-15 stsp if (err)
1843 13add988 2019-10-15 stsp goto done;
1844 13add988 2019-10-15 stsp
1845 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1846 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1847 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1848 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1849 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1850 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1851 13add988 2019-10-15 stsp *have_match = 1;
1852 13add988 2019-10-15 stsp done:
1853 13add988 2019-10-15 stsp free(id_str);
1854 13add988 2019-10-15 stsp free(logmsg);
1855 13add988 2019-10-15 stsp return err;
1856 13add988 2019-10-15 stsp }
1857 13add988 2019-10-15 stsp
1858 13add988 2019-10-15 stsp static const struct got_error *
1859 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1860 c4972b91 2018-05-07 stsp {
1861 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1862 9ba79e04 2018-06-11 stsp
1863 1a76625f 2018-10-22 stsp /*
1864 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1865 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1866 1a76625f 2018-10-22 stsp * while updating the display.
1867 1a76625f 2018-10-22 stsp */
1868 4e0d2870 2020-12-07 naddy do {
1869 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1870 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1871 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1872 1a76625f 2018-10-22 stsp int errcode;
1873 899d86c2 2018-05-10 stsp
1874 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1875 4e0d2870 2020-12-07 naddy NULL, NULL);
1876 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1877 ecb28ae0 2018-07-16 stsp break;
1878 899d86c2 2018-05-10 stsp
1879 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1880 9ba79e04 2018-06-11 stsp if (err)
1881 9ba79e04 2018-06-11 stsp break;
1882 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1883 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1884 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1885 9ba79e04 2018-06-11 stsp break;
1886 9ba79e04 2018-06-11 stsp }
1887 93e45b7c 2018-09-24 stsp
1888 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1889 1a76625f 2018-10-22 stsp if (errcode) {
1890 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1891 13add988 2019-10-15 stsp "pthread_mutex_lock");
1892 1a76625f 2018-10-22 stsp break;
1893 1a76625f 2018-10-22 stsp }
1894 1a76625f 2018-10-22 stsp
1895 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1896 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1897 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1898 1a76625f 2018-10-22 stsp
1899 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1900 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1901 7c1452c1 2020-03-26 stsp int have_match;
1902 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1903 7c1452c1 2020-03-26 stsp if (err)
1904 7c1452c1 2020-03-26 stsp break;
1905 7c1452c1 2020-03-26 stsp if (have_match)
1906 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1907 13add988 2019-10-15 stsp }
1908 13add988 2019-10-15 stsp
1909 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1910 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1911 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1912 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1913 7c1452c1 2020-03-26 stsp if (err)
1914 13add988 2019-10-15 stsp break;
1915 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1916 899d86c2 2018-05-10 stsp
1917 9ba79e04 2018-06-11 stsp return err;
1918 0553a4e3 2018-04-30 stsp }
1919 0553a4e3 2018-04-30 stsp
1920 2b779855 2020-12-05 naddy static void
1921 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1922 2b779855 2020-12-05 naddy {
1923 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1924 2b779855 2020-12-05 naddy int ncommits = 0;
1925 2b779855 2020-12-05 naddy
1926 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1927 2b779855 2020-12-05 naddy while (entry) {
1928 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1929 2b779855 2020-12-05 naddy s->selected_entry = entry;
1930 2b779855 2020-12-05 naddy break;
1931 2b779855 2020-12-05 naddy }
1932 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1933 2b779855 2020-12-05 naddy ncommits++;
1934 2b779855 2020-12-05 naddy }
1935 2b779855 2020-12-05 naddy }
1936 2b779855 2020-12-05 naddy
1937 0553a4e3 2018-04-30 stsp static const struct got_error *
1938 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1939 0553a4e3 2018-04-30 stsp {
1940 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1941 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1942 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1943 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1944 60493ae3 2019-06-20 stsp int width;
1945 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1946 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1947 8b473291 2019-02-21 stsp char *refs_str = NULL;
1948 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1949 11b20872 2019-11-08 stsp struct tog_color *tc;
1950 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1951 0553a4e3 2018-04-30 stsp
1952 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1953 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1954 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1955 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1956 1a76625f 2018-10-22 stsp if (err)
1957 ecb28ae0 2018-07-16 stsp return err;
1958 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1959 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1960 d2075bf3 2020-12-25 stsp if (refs) {
1961 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1962 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1963 d2075bf3 2020-12-25 stsp if (err)
1964 d2075bf3 2020-12-25 stsp goto done;
1965 d2075bf3 2020-12-25 stsp }
1966 867c6645 2018-07-10 stsp }
1967 359bfafd 2019-02-22 stsp
1968 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1969 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1970 1a76625f 2018-10-22 stsp
1971 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1972 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1973 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1974 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1975 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1976 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1977 8f4ed634 2020-03-26 stsp goto done;
1978 8f4ed634 2020-03-26 stsp }
1979 8f4ed634 2020-03-26 stsp } else {
1980 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1981 f9686aa5 2020-03-27 stsp
1982 f9686aa5 2020-03-27 stsp if (view->searching) {
1983 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1984 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1985 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1986 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1987 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1988 f9686aa5 2020-03-27 stsp search_str = "searching...";
1989 f9686aa5 2020-03-27 stsp }
1990 f9686aa5 2020-03-27 stsp
1991 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1992 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1993 f9686aa5 2020-03-27 stsp search_str ? search_str :
1994 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1995 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1996 8f4ed634 2020-03-26 stsp goto done;
1997 8f4ed634 2020-03-26 stsp }
1998 8b473291 2019-02-21 stsp }
1999 1a76625f 2018-10-22 stsp
2000 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2001 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2002 91198554 2022-06-23 thomas "........................................",
2003 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2004 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2005 1a76625f 2018-10-22 stsp header = NULL;
2006 1a76625f 2018-10-22 stsp goto done;
2007 1a76625f 2018-10-22 stsp }
2008 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2009 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2010 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2011 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2012 1a76625f 2018-10-22 stsp header = NULL;
2013 1a76625f 2018-10-22 stsp goto done;
2014 ecb28ae0 2018-07-16 stsp }
2015 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2016 1a76625f 2018-10-22 stsp if (err)
2017 1a76625f 2018-10-22 stsp goto done;
2018 867c6645 2018-07-10 stsp
2019 2814baeb 2018-08-01 stsp werase(view->window);
2020 867c6645 2018-07-10 stsp
2021 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2022 a3404814 2018-09-02 stsp wstandout(view->window);
2023 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2024 11b20872 2019-11-08 stsp if (tc)
2025 11b20872 2019-11-08 stsp wattr_on(view->window,
2026 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2027 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2028 11b20872 2019-11-08 stsp if (tc)
2029 11b20872 2019-11-08 stsp wattr_off(view->window,
2030 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2031 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2032 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2033 1a76625f 2018-10-22 stsp width++;
2034 1a76625f 2018-10-22 stsp }
2035 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2036 a3404814 2018-09-02 stsp wstandend(view->window);
2037 ecb28ae0 2018-07-16 stsp free(wline);
2038 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2039 1a76625f 2018-10-22 stsp goto done;
2040 0553a4e3 2018-04-30 stsp
2041 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2042 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2043 5813d178 2019-03-09 stsp ncommits = 0;
2044 05171be4 2022-06-23 thomas view->maxx = 0;
2045 5813d178 2019-03-09 stsp while (entry) {
2046 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2047 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2048 5813d178 2019-03-09 stsp int width;
2049 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2050 5813d178 2019-03-09 stsp break;
2051 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2052 5813d178 2019-03-09 stsp if (author == NULL) {
2053 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2054 5813d178 2019-03-09 stsp goto done;
2055 5813d178 2019-03-09 stsp }
2056 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2057 27a741e5 2019-09-11 stsp date_display_cols);
2058 5813d178 2019-03-09 stsp if (author_cols < width)
2059 5813d178 2019-03-09 stsp author_cols = width;
2060 5813d178 2019-03-09 stsp free(wauthor);
2061 5813d178 2019-03-09 stsp free(author);
2062 05171be4 2022-06-23 thomas err = got_object_commit_get_logmsg(&msg0, entry->commit);
2063 05171be4 2022-06-23 thomas if (err)
2064 05171be4 2022-06-23 thomas goto done;
2065 05171be4 2022-06-23 thomas msg = msg0;
2066 05171be4 2022-06-23 thomas while (*msg == '\n')
2067 05171be4 2022-06-23 thomas ++msg;
2068 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2069 331b1a16 2022-06-23 thomas *eol = '\0';
2070 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2071 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2072 331b1a16 2022-06-23 thomas if (err)
2073 331b1a16 2022-06-23 thomas goto done;
2074 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2075 05171be4 2022-06-23 thomas free(msg0);
2076 331b1a16 2022-06-23 thomas free(wmsg);
2077 7ca04879 2019-10-19 stsp ncommits++;
2078 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2079 5813d178 2019-03-09 stsp }
2080 5813d178 2019-03-09 stsp
2081 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2082 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2083 867c6645 2018-07-10 stsp ncommits = 0;
2084 899d86c2 2018-05-10 stsp while (entry) {
2085 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2086 899d86c2 2018-05-10 stsp break;
2087 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2088 2814baeb 2018-08-01 stsp wstandout(view->window);
2089 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2090 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2091 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2092 2814baeb 2018-08-01 stsp wstandend(view->window);
2093 0553a4e3 2018-04-30 stsp if (err)
2094 60493ae3 2019-06-20 stsp goto done;
2095 0553a4e3 2018-04-30 stsp ncommits++;
2096 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2097 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2098 80ddbec8 2018-04-29 stsp }
2099 80ddbec8 2018-04-29 stsp
2100 a5d43cac 2022-07-01 thomas view_border(view);
2101 1a76625f 2018-10-22 stsp done:
2102 1a76625f 2018-10-22 stsp free(id_str);
2103 8b473291 2019-02-21 stsp free(refs_str);
2104 1a76625f 2018-10-22 stsp free(ncommits_str);
2105 1a76625f 2018-10-22 stsp free(header);
2106 80ddbec8 2018-04-29 stsp return err;
2107 9f7d7167 2018-04-29 stsp }
2108 07b55e75 2018-05-10 stsp
2109 07b55e75 2018-05-10 stsp static void
2110 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2111 07b55e75 2018-05-10 stsp {
2112 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2113 07b55e75 2018-05-10 stsp int nscrolled = 0;
2114 07b55e75 2018-05-10 stsp
2115 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2116 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2117 07b55e75 2018-05-10 stsp return;
2118 9f7d7167 2018-04-29 stsp
2119 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2120 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2121 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2122 07b55e75 2018-05-10 stsp if (entry) {
2123 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2124 07b55e75 2018-05-10 stsp nscrolled++;
2125 07b55e75 2018-05-10 stsp }
2126 07b55e75 2018-05-10 stsp }
2127 aa075928 2018-05-10 stsp }
2128 aa075928 2018-05-10 stsp
2129 aa075928 2018-05-10 stsp static const struct got_error *
2130 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2131 aa075928 2018-05-10 stsp {
2132 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2133 5e224a3e 2019-02-22 stsp int errcode;
2134 8a42fca8 2019-02-22 stsp
2135 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2136 aa075928 2018-05-10 stsp
2137 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2138 ffe38506 2020-12-01 naddy if (ta->log_complete)
2139 5e224a3e 2019-02-22 stsp break;
2140 b295e71b 2019-02-22 stsp
2141 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2142 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2143 7aafa0d1 2019-02-22 stsp if (errcode)
2144 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2145 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2146 7c1452c1 2020-03-26 stsp
2147 7c1452c1 2020-03-26 stsp /*
2148 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2149 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2150 7c1452c1 2020-03-26 stsp */
2151 7c1452c1 2020-03-26 stsp if (!wait)
2152 7c1452c1 2020-03-26 stsp break;
2153 7c1452c1 2020-03-26 stsp
2154 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2155 ffe38506 2020-12-01 naddy show_log_view(view);
2156 7c1452c1 2020-03-26 stsp update_panels();
2157 7c1452c1 2020-03-26 stsp doupdate();
2158 7c1452c1 2020-03-26 stsp
2159 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2160 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2161 82954512 2020-02-03 stsp if (errcode)
2162 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2163 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2164 82954512 2020-02-03 stsp
2165 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2166 ffe38506 2020-12-01 naddy show_log_view(view);
2167 7c1452c1 2020-03-26 stsp update_panels();
2168 7c1452c1 2020-03-26 stsp doupdate();
2169 5e224a3e 2019-02-22 stsp }
2170 5e224a3e 2019-02-22 stsp
2171 5e224a3e 2019-02-22 stsp return NULL;
2172 a5d43cac 2022-07-01 thomas }
2173 a5d43cac 2022-07-01 thomas
2174 a5d43cac 2022-07-01 thomas static const struct got_error *
2175 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2176 a5d43cac 2022-07-01 thomas {
2177 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2178 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2179 a5d43cac 2022-07-01 thomas
2180 a5d43cac 2022-07-01 thomas state->thread_args.commits_needed = view->nscrolled;
2181 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2182 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2183 a5d43cac 2022-07-01 thomas
2184 a5d43cac 2022-07-01 thomas return err;
2185 5e224a3e 2019-02-22 stsp }
2186 5e224a3e 2019-02-22 stsp
2187 5e224a3e 2019-02-22 stsp static const struct got_error *
2188 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2189 5e224a3e 2019-02-22 stsp {
2190 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2191 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2192 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2193 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2194 5e224a3e 2019-02-22 stsp
2195 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2196 5e224a3e 2019-02-22 stsp return NULL;
2197 5e224a3e 2019-02-22 stsp
2198 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2199 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2200 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2201 08ebd0a9 2019-02-22 stsp /*
2202 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2203 08ebd0a9 2019-02-22 stsp */
2204 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2205 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2206 5e224a3e 2019-02-22 stsp if (err)
2207 5e224a3e 2019-02-22 stsp return err;
2208 7aafa0d1 2019-02-22 stsp }
2209 b295e71b 2019-02-22 stsp
2210 7aafa0d1 2019-02-22 stsp do {
2211 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2212 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2213 88048b54 2019-02-21 stsp break;
2214 88048b54 2019-02-21 stsp
2215 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2216 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2217 aa075928 2018-05-10 stsp
2218 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2219 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2220 dd0a52c1 2018-05-20 stsp break;
2221 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2222 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2223 aa075928 2018-05-10 stsp
2224 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
2225 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2226 a5d43cac 2022-07-01 thomas else
2227 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2228 a5d43cac 2022-07-01 thomas
2229 dd0a52c1 2018-05-20 stsp return err;
2230 07b55e75 2018-05-10 stsp }
2231 4a7f7875 2018-05-10 stsp
2232 cd0acaa7 2018-05-20 stsp static const struct got_error *
2233 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2234 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2235 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2236 cd0acaa7 2018-05-20 stsp {
2237 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2238 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2239 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2240 cd0acaa7 2018-05-20 stsp
2241 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2242 15a94983 2018-12-23 stsp if (diff_view == NULL)
2243 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2244 ea5e7bb5 2018-08-01 stsp
2245 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2246 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2247 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2248 e5a0f69f 2018-08-18 stsp if (err == NULL)
2249 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2250 cd0acaa7 2018-05-20 stsp return err;
2251 4a7f7875 2018-05-10 stsp }
2252 4a7f7875 2018-05-10 stsp
2253 80ddbec8 2018-04-29 stsp static const struct got_error *
2254 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2255 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2256 9343a5fb 2018-06-23 stsp {
2257 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2258 941e9f74 2019-05-21 stsp
2259 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2260 941e9f74 2019-05-21 stsp if (parent == NULL)
2261 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2262 941e9f74 2019-05-21 stsp
2263 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2264 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2265 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2266 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2267 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2268 941e9f74 2019-05-21 stsp s->tree = subtree;
2269 941e9f74 2019-05-21 stsp s->selected = 0;
2270 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2271 941e9f74 2019-05-21 stsp return NULL;
2272 941e9f74 2019-05-21 stsp }
2273 941e9f74 2019-05-21 stsp
2274 941e9f74 2019-05-21 stsp static const struct got_error *
2275 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2276 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2277 941e9f74 2019-05-21 stsp {
2278 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2279 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2280 941e9f74 2019-05-21 stsp const char *p;
2281 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2282 9343a5fb 2018-06-23 stsp
2283 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2284 941e9f74 2019-05-21 stsp p = path;
2285 941e9f74 2019-05-21 stsp while (*p) {
2286 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2287 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2288 56e0773d 2019-11-28 stsp char *te_name;
2289 33cbf02b 2020-01-12 stsp
2290 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2291 33cbf02b 2020-01-12 stsp p++;
2292 941e9f74 2019-05-21 stsp
2293 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2294 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2295 941e9f74 2019-05-21 stsp if (slash == NULL)
2296 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2297 33cbf02b 2020-01-12 stsp else
2298 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2299 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2300 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2301 56e0773d 2019-11-28 stsp break;
2302 941e9f74 2019-05-21 stsp }
2303 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2304 56e0773d 2019-11-28 stsp if (te == NULL) {
2305 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2306 56e0773d 2019-11-28 stsp free(te_name);
2307 941e9f74 2019-05-21 stsp break;
2308 941e9f74 2019-05-21 stsp }
2309 56e0773d 2019-11-28 stsp free(te_name);
2310 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2311 941e9f74 2019-05-21 stsp
2312 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2313 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2314 b03c880f 2019-05-21 stsp
2315 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2316 941e9f74 2019-05-21 stsp if (slash)
2317 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2318 941e9f74 2019-05-21 stsp else
2319 941e9f74 2019-05-21 stsp subpath = strdup(path);
2320 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2321 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2322 941e9f74 2019-05-21 stsp break;
2323 941e9f74 2019-05-21 stsp }
2324 941e9f74 2019-05-21 stsp
2325 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2326 941e9f74 2019-05-21 stsp subpath);
2327 941e9f74 2019-05-21 stsp if (err)
2328 941e9f74 2019-05-21 stsp break;
2329 941e9f74 2019-05-21 stsp
2330 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2331 941e9f74 2019-05-21 stsp free(tree_id);
2332 941e9f74 2019-05-21 stsp if (err)
2333 941e9f74 2019-05-21 stsp break;
2334 941e9f74 2019-05-21 stsp
2335 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2336 941e9f74 2019-05-21 stsp if (err) {
2337 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2338 941e9f74 2019-05-21 stsp break;
2339 941e9f74 2019-05-21 stsp }
2340 941e9f74 2019-05-21 stsp if (slash == NULL)
2341 941e9f74 2019-05-21 stsp break;
2342 941e9f74 2019-05-21 stsp free(subpath);
2343 941e9f74 2019-05-21 stsp subpath = NULL;
2344 941e9f74 2019-05-21 stsp p = slash;
2345 941e9f74 2019-05-21 stsp }
2346 941e9f74 2019-05-21 stsp
2347 941e9f74 2019-05-21 stsp free(subpath);
2348 1a76625f 2018-10-22 stsp return err;
2349 61266923 2020-01-14 stsp }
2350 61266923 2020-01-14 stsp
2351 61266923 2020-01-14 stsp static const struct got_error *
2352 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2353 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2354 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2355 55cccc34 2020-02-20 stsp {
2356 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2357 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2358 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2359 55cccc34 2020-02-20 stsp
2360 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2361 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2362 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2363 55cccc34 2020-02-20 stsp
2364 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2365 bc573f3b 2021-07-10 stsp if (err)
2366 55cccc34 2020-02-20 stsp return err;
2367 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2368 55cccc34 2020-02-20 stsp
2369 55cccc34 2020-02-20 stsp *new_view = tree_view;
2370 55cccc34 2020-02-20 stsp
2371 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2372 55cccc34 2020-02-20 stsp return NULL;
2373 55cccc34 2020-02-20 stsp
2374 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2375 55cccc34 2020-02-20 stsp }
2376 55cccc34 2020-02-20 stsp
2377 55cccc34 2020-02-20 stsp static const struct got_error *
2378 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2379 61266923 2020-01-14 stsp {
2380 61266923 2020-01-14 stsp sigset_t sigset;
2381 61266923 2020-01-14 stsp int errcode;
2382 61266923 2020-01-14 stsp
2383 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2384 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2385 61266923 2020-01-14 stsp
2386 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2387 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2388 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2389 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2390 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2391 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2392 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2393 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2394 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2395 61266923 2020-01-14 stsp
2396 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2397 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2398 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2399 61266923 2020-01-14 stsp
2400 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2401 61266923 2020-01-14 stsp if (errcode)
2402 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2403 61266923 2020-01-14 stsp
2404 61266923 2020-01-14 stsp return NULL;
2405 1a76625f 2018-10-22 stsp }
2406 1a76625f 2018-10-22 stsp
2407 1a76625f 2018-10-22 stsp static void *
2408 1a76625f 2018-10-22 stsp log_thread(void *arg)
2409 1a76625f 2018-10-22 stsp {
2410 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2411 1a76625f 2018-10-22 stsp int errcode = 0;
2412 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2413 1a76625f 2018-10-22 stsp int done = 0;
2414 61266923 2020-01-14 stsp
2415 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2416 61266923 2020-01-14 stsp if (err)
2417 61266923 2020-01-14 stsp return (void *)err;
2418 1a76625f 2018-10-22 stsp
2419 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2420 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2421 1a76625f 2018-10-22 stsp if (err) {
2422 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2423 1a76625f 2018-10-22 stsp return (void *)err;
2424 1a76625f 2018-10-22 stsp err = NULL;
2425 1a76625f 2018-10-22 stsp done = 1;
2426 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2427 1a76625f 2018-10-22 stsp a->commits_needed--;
2428 1a76625f 2018-10-22 stsp
2429 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2430 3abe8080 2019-04-10 stsp if (errcode) {
2431 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2432 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2433 3abe8080 2019-04-10 stsp break;
2434 3abe8080 2019-04-10 stsp } else if (*a->quit)
2435 1a76625f 2018-10-22 stsp done = 1;
2436 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2437 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2438 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2439 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2440 1a76625f 2018-10-22 stsp }
2441 1a76625f 2018-10-22 stsp
2442 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2443 7c1452c1 2020-03-26 stsp if (errcode) {
2444 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2445 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2446 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2447 7c1452c1 2020-03-26 stsp break;
2448 7c1452c1 2020-03-26 stsp }
2449 7c1452c1 2020-03-26 stsp
2450 1a76625f 2018-10-22 stsp if (done)
2451 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2452 7c1452c1 2020-03-26 stsp else {
2453 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2454 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2455 7c1452c1 2020-03-26 stsp &tog_mutex);
2456 7c1452c1 2020-03-26 stsp if (errcode)
2457 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2458 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2459 21355643 2020-12-06 stsp if (*a->quit)
2460 21355643 2020-12-06 stsp done = 1;
2461 7c1452c1 2020-03-26 stsp }
2462 1a76625f 2018-10-22 stsp }
2463 1a76625f 2018-10-22 stsp
2464 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2465 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2466 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2467 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2468 1a76625f 2018-10-22 stsp }
2469 3abe8080 2019-04-10 stsp a->log_complete = 1;
2470 1a76625f 2018-10-22 stsp return (void *)err;
2471 1a76625f 2018-10-22 stsp }
2472 1a76625f 2018-10-22 stsp
2473 1a76625f 2018-10-22 stsp static const struct got_error *
2474 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2475 1a76625f 2018-10-22 stsp {
2476 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2477 1a76625f 2018-10-22 stsp int errcode;
2478 1a76625f 2018-10-22 stsp
2479 1a76625f 2018-10-22 stsp if (s->thread) {
2480 1a76625f 2018-10-22 stsp s->quit = 1;
2481 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2482 1a76625f 2018-10-22 stsp if (errcode)
2483 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2484 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2485 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2486 1a76625f 2018-10-22 stsp if (errcode)
2487 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2488 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2489 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2490 1a76625f 2018-10-22 stsp if (errcode)
2491 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2492 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2493 1a76625f 2018-10-22 stsp if (errcode)
2494 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2495 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2496 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2497 1a76625f 2018-10-22 stsp }
2498 1a76625f 2018-10-22 stsp
2499 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2500 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2501 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2502 1af5eddf 2022-06-23 thomas }
2503 1af5eddf 2022-06-23 thomas
2504 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2505 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2506 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2507 1af5eddf 2022-06-23 thomas if (err == NULL)
2508 1af5eddf 2022-06-23 thomas err = pack_err;
2509 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2510 1a76625f 2018-10-22 stsp }
2511 1a76625f 2018-10-22 stsp
2512 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2513 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2514 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2515 1a76625f 2018-10-22 stsp }
2516 1a76625f 2018-10-22 stsp
2517 9343a5fb 2018-06-23 stsp return err;
2518 9343a5fb 2018-06-23 stsp }
2519 9343a5fb 2018-06-23 stsp
2520 9343a5fb 2018-06-23 stsp static const struct got_error *
2521 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2522 1a76625f 2018-10-22 stsp {
2523 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2524 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2525 276b94a1 2020-11-13 naddy int errcode;
2526 1a76625f 2018-10-22 stsp
2527 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2528 276b94a1 2020-11-13 naddy
2529 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2530 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2531 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2532 276b94a1 2020-11-13 naddy
2533 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2534 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2535 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2536 276b94a1 2020-11-13 naddy
2537 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2538 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2539 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2540 1a76625f 2018-10-22 stsp free(s->start_id);
2541 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2542 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2543 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2544 1a76625f 2018-10-22 stsp return err;
2545 1a76625f 2018-10-22 stsp }
2546 1a76625f 2018-10-22 stsp
2547 1a76625f 2018-10-22 stsp static const struct got_error *
2548 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2549 60493ae3 2019-06-20 stsp {
2550 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2551 60493ae3 2019-06-20 stsp
2552 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2553 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2554 60493ae3 2019-06-20 stsp return NULL;
2555 60493ae3 2019-06-20 stsp }
2556 60493ae3 2019-06-20 stsp
2557 60493ae3 2019-06-20 stsp static const struct got_error *
2558 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2559 60493ae3 2019-06-20 stsp {
2560 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2561 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2562 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2563 60493ae3 2019-06-20 stsp
2564 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2565 f9686aa5 2020-03-27 stsp show_log_view(view);
2566 f9686aa5 2020-03-27 stsp update_panels();
2567 f9686aa5 2020-03-27 stsp doupdate();
2568 f9686aa5 2020-03-27 stsp
2569 96e2b566 2019-07-08 stsp if (s->search_entry) {
2570 13add988 2019-10-15 stsp int errcode, ch;
2571 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2572 13add988 2019-10-15 stsp if (errcode)
2573 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2574 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2575 13add988 2019-10-15 stsp ch = wgetch(view->window);
2576 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2577 13add988 2019-10-15 stsp if (errcode)
2578 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2579 13add988 2019-10-15 stsp "pthread_mutex_lock");
2580 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2581 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2582 678cbce5 2019-07-28 stsp return NULL;
2583 678cbce5 2019-07-28 stsp }
2584 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2585 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2586 96e2b566 2019-07-08 stsp else
2587 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2588 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2589 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2590 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2591 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2592 df5e841d 2022-06-23 thomas
2593 df5e841d 2022-06-23 thomas /*
2594 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2595 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2596 1f4d5162 2022-06-23 thomas * might have changed.
2597 df5e841d 2022-06-23 thomas */
2598 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2599 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2600 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2601 df5e841d 2022-06-23 thomas else
2602 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2603 b74feda6 2022-06-23 thomas } else {
2604 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2605 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2606 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2607 df5e841d 2022-06-23 thomas else
2608 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2609 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2610 b74feda6 2022-06-23 thomas }
2611 20be8d96 2019-06-21 stsp } else {
2612 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2613 20be8d96 2019-06-21 stsp }
2614 60493ae3 2019-06-20 stsp
2615 60493ae3 2019-06-20 stsp while (1) {
2616 13add988 2019-10-15 stsp int have_match = 0;
2617 13add988 2019-10-15 stsp
2618 60493ae3 2019-06-20 stsp if (entry == NULL) {
2619 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2620 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2621 f9967bca 2020-03-27 stsp view->search_next_done =
2622 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2623 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2624 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2625 f801134a 2019-06-25 stsp return NULL;
2626 60493ae3 2019-06-20 stsp }
2627 96e2b566 2019-07-08 stsp /*
2628 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2629 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2630 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2631 96e2b566 2019-07-08 stsp */
2632 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2633 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2634 60493ae3 2019-06-20 stsp }
2635 60493ae3 2019-06-20 stsp
2636 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2637 13add988 2019-10-15 stsp &view->regex);
2638 5943eee2 2019-08-13 stsp if (err)
2639 13add988 2019-10-15 stsp break;
2640 13add988 2019-10-15 stsp if (have_match) {
2641 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2642 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2643 60493ae3 2019-06-20 stsp break;
2644 60493ae3 2019-06-20 stsp }
2645 13add988 2019-10-15 stsp
2646 96e2b566 2019-07-08 stsp s->search_entry = entry;
2647 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2648 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2649 b1bf1435 2019-06-21 stsp else
2650 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2651 60493ae3 2019-06-20 stsp }
2652 60493ae3 2019-06-20 stsp
2653 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2654 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2655 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2656 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2657 60493ae3 2019-06-20 stsp if (err)
2658 60493ae3 2019-06-20 stsp return err;
2659 ead14cbe 2019-06-21 stsp cur++;
2660 ead14cbe 2019-06-21 stsp }
2661 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2662 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2663 60493ae3 2019-06-20 stsp if (err)
2664 60493ae3 2019-06-20 stsp return err;
2665 ead14cbe 2019-06-21 stsp cur--;
2666 60493ae3 2019-06-20 stsp }
2667 60493ae3 2019-06-20 stsp }
2668 60493ae3 2019-06-20 stsp
2669 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2670 96e2b566 2019-07-08 stsp
2671 60493ae3 2019-06-20 stsp return NULL;
2672 60493ae3 2019-06-20 stsp }
2673 60493ae3 2019-06-20 stsp
2674 60493ae3 2019-06-20 stsp static const struct got_error *
2675 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2676 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2677 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2678 80ddbec8 2018-04-29 stsp {
2679 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2680 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2681 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2682 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2683 1a76625f 2018-10-22 stsp int errcode;
2684 80ddbec8 2018-04-29 stsp
2685 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2686 f135c941 2020-02-20 stsp free(s->in_repo_path);
2687 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2688 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2689 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2690 f135c941 2020-02-20 stsp }
2691 ecb28ae0 2018-07-16 stsp
2692 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2693 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2694 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2695 78756c87 2020-11-24 stsp
2696 fb2756b9 2018-08-04 stsp s->repo = repo;
2697 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2698 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2699 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2700 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2701 9cd7cbd1 2020-12-07 stsp goto done;
2702 9cd7cbd1 2020-12-07 stsp }
2703 9cd7cbd1 2020-12-07 stsp }
2704 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2705 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2706 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2707 5036bf37 2018-09-24 stsp goto done;
2708 5036bf37 2018-09-24 stsp }
2709 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2710 e5a0f69f 2018-08-18 stsp
2711 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2712 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2713 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2714 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2715 11b20872 2019-11-08 stsp if (err)
2716 11b20872 2019-11-08 stsp goto done;
2717 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2718 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2719 11b20872 2019-11-08 stsp if (err) {
2720 11b20872 2019-11-08 stsp free_colors(&s->colors);
2721 11b20872 2019-11-08 stsp goto done;
2722 11b20872 2019-11-08 stsp }
2723 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2724 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2725 11b20872 2019-11-08 stsp if (err) {
2726 11b20872 2019-11-08 stsp free_colors(&s->colors);
2727 11b20872 2019-11-08 stsp goto done;
2728 11b20872 2019-11-08 stsp }
2729 11b20872 2019-11-08 stsp }
2730 11b20872 2019-11-08 stsp
2731 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2732 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2733 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2734 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2735 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2736 1a76625f 2018-10-22 stsp
2737 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2738 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2739 1af5eddf 2022-06-23 thomas if (err)
2740 1af5eddf 2022-06-23 thomas goto done;
2741 1af5eddf 2022-06-23 thomas }
2742 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2743 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2744 7cd52833 2022-06-23 thomas if (err)
2745 7cd52833 2022-06-23 thomas goto done;
2746 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2747 b672a97a 2020-01-27 stsp !s->log_branches);
2748 1a76625f 2018-10-22 stsp if (err)
2749 1a76625f 2018-10-22 stsp goto done;
2750 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2751 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2752 c5b78334 2020-01-12 stsp if (err)
2753 c5b78334 2020-01-12 stsp goto done;
2754 1a76625f 2018-10-22 stsp
2755 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2756 1a76625f 2018-10-22 stsp if (errcode) {
2757 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2758 1a76625f 2018-10-22 stsp goto done;
2759 1a76625f 2018-10-22 stsp }
2760 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2761 7c1452c1 2020-03-26 stsp if (errcode) {
2762 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2763 7c1452c1 2020-03-26 stsp goto done;
2764 7c1452c1 2020-03-26 stsp }
2765 1a76625f 2018-10-22 stsp
2766 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2767 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2768 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2769 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2770 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2771 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2772 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2773 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2774 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2775 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2776 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2777 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2778 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2779 ba4f502b 2018-08-04 stsp done:
2780 1a76625f 2018-10-22 stsp if (err)
2781 1a76625f 2018-10-22 stsp close_log_view(view);
2782 ba4f502b 2018-08-04 stsp return err;
2783 ba4f502b 2018-08-04 stsp }
2784 ba4f502b 2018-08-04 stsp
2785 e5a0f69f 2018-08-18 stsp static const struct got_error *
2786 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2787 ba4f502b 2018-08-04 stsp {
2788 f2f6d207 2020-11-24 stsp const struct got_error *err;
2789 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2790 ba4f502b 2018-08-04 stsp
2791 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2792 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2793 2b380cc8 2018-10-24 stsp &s->thread_args);
2794 2b380cc8 2018-10-24 stsp if (errcode)
2795 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2796 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2797 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2798 f2f6d207 2020-11-24 stsp if (err)
2799 f2f6d207 2020-11-24 stsp return err;
2800 f2f6d207 2020-11-24 stsp }
2801 2b380cc8 2018-10-24 stsp }
2802 2b380cc8 2018-10-24 stsp
2803 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2804 b31f89ff 2022-07-01 thomas }
2805 b31f89ff 2022-07-01 thomas
2806 b31f89ff 2022-07-01 thomas static void
2807 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
2808 b31f89ff 2022-07-01 thomas {
2809 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
2810 b31f89ff 2022-07-01 thomas
2811 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
2812 b31f89ff 2022-07-01 thomas view->count = 0;
2813 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
2814 b31f89ff 2022-07-01 thomas return;
2815 b31f89ff 2022-07-01 thomas
2816 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2817 b31f89ff 2022-07-01 thomas || home)
2818 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2819 b31f89ff 2022-07-01 thomas
2820 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
2821 b31f89ff 2022-07-01 thomas --s->selected;
2822 b31f89ff 2022-07-01 thomas else
2823 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2824 b31f89ff 2022-07-01 thomas
2825 b31f89ff 2022-07-01 thomas select_commit(s);
2826 b31f89ff 2022-07-01 thomas return;
2827 b31f89ff 2022-07-01 thomas }
2828 b31f89ff 2022-07-01 thomas
2829 b31f89ff 2022-07-01 thomas static const struct got_error *
2830 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
2831 b31f89ff 2022-07-01 thomas {
2832 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
2833 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
2834 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
2835 b31f89ff 2022-07-01 thomas
2836 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
2837 b31f89ff 2022-07-01 thomas if (first == NULL) {
2838 b31f89ff 2022-07-01 thomas view->count = 0;
2839 b31f89ff 2022-07-01 thomas return NULL;
2840 b31f89ff 2022-07-01 thomas }
2841 b31f89ff 2022-07-01 thomas
2842 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
2843 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
2844 b31f89ff 2022-07-01 thomas return NULL;
2845 b31f89ff 2022-07-01 thomas
2846 b31f89ff 2022-07-01 thomas if (!page) {
2847 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
2848 b31f89ff 2022-07-01 thomas
2849 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
2850 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
2851 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
2852 b31f89ff 2022-07-01 thomas ++s->selected;
2853 b31f89ff 2022-07-01 thomas else
2854 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
2855 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
2856 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2857 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
2858 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
2859 b31f89ff 2022-07-01 thomas else
2860 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
2861 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
2862 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2863 b31f89ff 2022-07-01 thomas } else {
2864 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
2865 b31f89ff 2022-07-01 thomas if (err)
2866 b31f89ff 2022-07-01 thomas return err;
2867 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
2868 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2869 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
2870 b31f89ff 2022-07-01 thomas }
2871 b31f89ff 2022-07-01 thomas }
2872 b31f89ff 2022-07-01 thomas if (err)
2873 b31f89ff 2022-07-01 thomas return err;
2874 b31f89ff 2022-07-01 thomas
2875 a5d43cac 2022-07-01 thomas /*
2876 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
2877 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
2878 a5d43cac 2022-07-01 thomas */
2879 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
2880 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2881 a5d43cac 2022-07-01 thomas
2882 b31f89ff 2022-07-01 thomas select_commit(s);
2883 b31f89ff 2022-07-01 thomas
2884 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
2885 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
2886 b31f89ff 2022-07-01 thomas view->count = 0;
2887 b31f89ff 2022-07-01 thomas
2888 b31f89ff 2022-07-01 thomas return NULL;
2889 e5a0f69f 2018-08-18 stsp }
2890 04cc582a 2018-08-01 stsp
2891 a5d43cac 2022-07-01 thomas /*
2892 a5d43cac 2022-07-01 thomas * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2893 a5d43cac 2022-07-01 thomas * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2894 a5d43cac 2022-07-01 thomas * TOG_VIEW_SPLIT_HRZN horizontal split
2895 a5d43cac 2022-07-01 thomas * Assign start column and line of the new split to *x and *y, respectively,
2896 a5d43cac 2022-07-01 thomas * and assign view mode to view->mode.
2897 a5d43cac 2022-07-01 thomas */
2898 a5d43cac 2022-07-01 thomas static void
2899 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
2900 a5d43cac 2022-07-01 thomas {
2901 a5d43cac 2022-07-01 thomas char *mode;
2902 a5d43cac 2022-07-01 thomas
2903 24415785 2022-07-03 thomas *x = 0;
2904 24415785 2022-07-03 thomas *y = 0;
2905 24415785 2022-07-03 thomas
2906 a5d43cac 2022-07-01 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
2907 a5d43cac 2022-07-01 thomas
2908 a5d43cac 2022-07-01 thomas if (!mode || mode[0] != 'h') {
2909 a5d43cac 2022-07-01 thomas view->mode = TOG_VIEW_SPLIT_VERT;
2910 a5d43cac 2022-07-01 thomas *x = view_split_begin_x(view->begin_x);
2911 a5d43cac 2022-07-01 thomas } else if (mode && mode[0] == 'h') {
2912 a5d43cac 2022-07-01 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
2913 a5d43cac 2022-07-01 thomas *y = view_split_begin_y(view->lines);
2914 a5d43cac 2022-07-01 thomas }
2915 a5d43cac 2022-07-01 thomas }
2916 a5d43cac 2022-07-01 thomas
2917 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
2918 e5a0f69f 2018-08-18 stsp static const struct got_error *
2919 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
2920 a5d43cac 2022-07-01 thomas {
2921 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2922 a5d43cac 2022-07-01 thomas
2923 a5d43cac 2022-07-01 thomas view->nlines = y;
2924 a5d43cac 2022-07-01 thomas err = view_resize(view);
2925 a5d43cac 2022-07-01 thomas if (err)
2926 a5d43cac 2022-07-01 thomas return err;
2927 a5d43cac 2022-07-01 thomas
2928 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
2929 a5d43cac 2022-07-01 thomas
2930 a5d43cac 2022-07-01 thomas return err;
2931 a5d43cac 2022-07-01 thomas }
2932 a5d43cac 2022-07-01 thomas
2933 a5d43cac 2022-07-01 thomas static const struct got_error *
2934 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2935 e5a0f69f 2018-08-18 stsp {
2936 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2937 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2938 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2939 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2940 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2941 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
2942 80ddbec8 2018-04-29 stsp
2943 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2944 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
2945 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2946 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2947 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
2948 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
2949 fb280deb 2021-08-30 stsp }
2950 b31f89ff 2022-07-01 thomas return err;
2951 528dedf3 2021-08-30 stsp }
2952 068ab281 2022-07-01 thomas
2953 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
2954 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
2955 068ab281 2022-07-01 thomas --eos; /* border */
2956 068ab281 2022-07-01 thomas
2957 528dedf3 2021-08-30 stsp switch (ch) {
2958 1e37a5c2 2019-05-12 jcs case 'q':
2959 1e37a5c2 2019-05-12 jcs s->quit = 1;
2960 05171be4 2022-06-23 thomas break;
2961 05171be4 2022-06-23 thomas case '0':
2962 05171be4 2022-06-23 thomas view->x = 0;
2963 05171be4 2022-06-23 thomas break;
2964 05171be4 2022-06-23 thomas case '$':
2965 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
2966 07b0611c 2022-06-23 thomas view->count = 0;
2967 05171be4 2022-06-23 thomas break;
2968 05171be4 2022-06-23 thomas case KEY_RIGHT:
2969 05171be4 2022-06-23 thomas case 'l':
2970 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
2971 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
2972 07b0611c 2022-06-23 thomas else
2973 07b0611c 2022-06-23 thomas view->count = 0;
2974 1e37a5c2 2019-05-12 jcs break;
2975 05171be4 2022-06-23 thomas case KEY_LEFT:
2976 05171be4 2022-06-23 thomas case 'h':
2977 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
2978 07b0611c 2022-06-23 thomas if (view->x <= 0)
2979 07b0611c 2022-06-23 thomas view->count = 0;
2980 05171be4 2022-06-23 thomas break;
2981 1e37a5c2 2019-05-12 jcs case 'k':
2982 1e37a5c2 2019-05-12 jcs case KEY_UP:
2983 1e37a5c2 2019-05-12 jcs case '<':
2984 1e37a5c2 2019-05-12 jcs case ',':
2985 f7140bf5 2021-10-17 thomas case CTRL('p'):
2986 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
2987 912a3f79 2021-08-30 j break;
2988 912a3f79 2021-08-30 j case 'g':
2989 912a3f79 2021-08-30 j case KEY_HOME:
2990 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
2991 07b0611c 2022-06-23 thomas view->count = 0;
2992 1e37a5c2 2019-05-12 jcs break;
2993 70f17a53 2022-06-13 thomas case CTRL('u'):
2994 23427b14 2022-06-23 thomas case 'u':
2995 70f17a53 2022-06-13 thomas nscroll /= 2;
2996 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2997 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2998 a4292ac5 2019-05-12 jcs case CTRL('b'):
2999 1c5e5faa 2022-06-23 thomas case 'b':
3000 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3001 1e37a5c2 2019-05-12 jcs break;
3002 1e37a5c2 2019-05-12 jcs case 'j':
3003 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3004 1e37a5c2 2019-05-12 jcs case '>':
3005 1e37a5c2 2019-05-12 jcs case '.':
3006 f7140bf5 2021-10-17 thomas case CTRL('n'):
3007 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3008 912a3f79 2021-08-30 j break;
3009 912a3f79 2021-08-30 j case 'G':
3010 912a3f79 2021-08-30 j case KEY_END: {
3011 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3012 912a3f79 2021-08-30 j * traverse them all. */
3013 07b0611c 2022-06-23 thomas view->count = 0;
3014 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3015 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3016 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3017 80ddbec8 2018-04-29 stsp }
3018 912a3f79 2021-08-30 j
3019 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3020 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3021 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3022 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3023 f3bc9f1d 2021-09-05 naddy break;
3024 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3025 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3026 f3bc9f1d 2021-09-05 naddy }
3027 f3bc9f1d 2021-09-05 naddy if (n > 0)
3028 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3029 2b779855 2020-12-05 naddy select_commit(s);
3030 1e37a5c2 2019-05-12 jcs break;
3031 912a3f79 2021-08-30 j }
3032 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3033 23427b14 2022-06-23 thomas case 'd':
3034 70f17a53 2022-06-13 thomas nscroll /= 2;
3035 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3036 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3037 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3038 4c2d69cb 2022-06-23 thomas case 'f':
3039 b31f89ff 2022-07-01 thomas case ' ':
3040 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3041 1e37a5c2 2019-05-12 jcs break;
3042 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3043 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3044 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3045 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3046 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3047 2b779855 2020-12-05 naddy select_commit(s);
3048 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3049 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3050 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3051 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3052 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3053 0bf7f153 2020-12-02 naddy }
3054 1e37a5c2 2019-05-12 jcs break;
3055 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3056 444d5325 2022-07-03 thomas case '\r':
3057 07b0611c 2022-06-23 thomas view->count = 0;
3058 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3059 e5a0f69f 2018-08-18 stsp break;
3060 a5d43cac 2022-07-01 thomas
3061 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3062 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3063 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3064 a5d43cac 2022-07-01 thomas
3065 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3066 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3067 78756c87 2020-11-24 stsp view, s->repo);
3068 1e37a5c2 2019-05-12 jcs if (err)
3069 1e37a5c2 2019-05-12 jcs break;
3070 a5d43cac 2022-07-01 thomas
3071 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3072 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3073 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3074 a5d43cac 2022-07-01 thomas if (err)
3075 a5d43cac 2022-07-01 thomas break;
3076 a5d43cac 2022-07-01 thomas }
3077 a5d43cac 2022-07-01 thomas
3078 e78dc838 2020-12-04 stsp view->focussed = 0;
3079 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3080 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3081 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3082 a5d43cac 2022-07-01 thomas
3083 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3084 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3085 f7013a22 2018-10-24 stsp if (err)
3086 1e37a5c2 2019-05-12 jcs return err;
3087 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3088 40236d76 2022-06-23 thomas if (err)
3089 40236d76 2022-06-23 thomas return err;
3090 e78dc838 2020-12-04 stsp view->focus_child = 1;
3091 1e37a5c2 2019-05-12 jcs } else
3092 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3093 1e37a5c2 2019-05-12 jcs break;
3094 1e37a5c2 2019-05-12 jcs case 't':
3095 07b0611c 2022-06-23 thomas view->count = 0;
3096 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3097 5036bf37 2018-09-24 stsp break;
3098 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3099 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3100 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3101 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3102 4e97c21c 2020-12-06 stsp s->repo);
3103 1e37a5c2 2019-05-12 jcs if (err)
3104 e5a0f69f 2018-08-18 stsp break;
3105 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3106 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3107 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3108 444d5325 2022-07-03 thomas if (err)
3109 444d5325 2022-07-03 thomas break;
3110 444d5325 2022-07-03 thomas }
3111 e78dc838 2020-12-04 stsp view->focussed = 0;
3112 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3113 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3114 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3115 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3116 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3117 1e37a5c2 2019-05-12 jcs if (err)
3118 1e37a5c2 2019-05-12 jcs return err;
3119 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3120 40236d76 2022-06-23 thomas if (err)
3121 40236d76 2022-06-23 thomas return err;
3122 e78dc838 2020-12-04 stsp view->focus_child = 1;
3123 1e37a5c2 2019-05-12 jcs } else
3124 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3125 1e37a5c2 2019-05-12 jcs break;
3126 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3127 21355643 2020-12-06 stsp case CTRL('l'):
3128 21355643 2020-12-06 stsp case 'B':
3129 07b0611c 2022-06-23 thomas view->count = 0;
3130 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3131 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3132 1e37a5c2 2019-05-12 jcs break;
3133 21355643 2020-12-06 stsp err = stop_log_thread(s);
3134 74cfe85e 2020-10-20 stsp if (err)
3135 74cfe85e 2020-10-20 stsp return err;
3136 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3137 21355643 2020-12-06 stsp char *parent_path;
3138 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3139 21355643 2020-12-06 stsp if (err)
3140 21355643 2020-12-06 stsp return err;
3141 21355643 2020-12-06 stsp free(s->in_repo_path);
3142 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3143 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3144 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3145 21355643 2020-12-06 stsp struct got_object_id *start_id;
3146 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3147 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3148 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3149 21355643 2020-12-06 stsp if (err)
3150 21355643 2020-12-06 stsp return err;
3151 21355643 2020-12-06 stsp free(s->start_id);
3152 21355643 2020-12-06 stsp s->start_id = start_id;
3153 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3154 21355643 2020-12-06 stsp } else /* 'B' */
3155 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3156 21355643 2020-12-06 stsp
3157 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3158 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3159 bf7e79b3 2022-06-23 thomas if (err)
3160 bf7e79b3 2022-06-23 thomas return err;
3161 bf7e79b3 2022-06-23 thomas }
3162 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3163 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3164 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3165 74cfe85e 2020-10-20 stsp if (err)
3166 21355643 2020-12-06 stsp return err;
3167 51a10b52 2020-12-26 stsp tog_free_refs();
3168 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3169 ca51c541 2020-12-07 stsp if (err)
3170 ca51c541 2020-12-07 stsp return err;
3171 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3172 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3173 d01904d4 2019-06-25 stsp if (err)
3174 d01904d4 2019-06-25 stsp return err;
3175 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3176 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3177 b672a97a 2020-01-27 stsp if (err)
3178 b672a97a 2020-01-27 stsp return err;
3179 21355643 2020-12-06 stsp free_commits(&s->commits);
3180 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3181 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3182 21355643 2020-12-06 stsp s->selected_entry = NULL;
3183 21355643 2020-12-06 stsp s->selected = 0;
3184 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3185 21355643 2020-12-06 stsp s->quit = 0;
3186 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3187 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3188 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3189 d01904d4 2019-06-25 stsp break;
3190 6458efa5 2020-11-24 stsp case 'r':
3191 07b0611c 2022-06-23 thomas view->count = 0;
3192 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3193 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3194 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3195 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3196 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3197 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3198 6458efa5 2020-11-24 stsp if (err) {
3199 6458efa5 2020-11-24 stsp view_close(ref_view);
3200 6458efa5 2020-11-24 stsp return err;
3201 6458efa5 2020-11-24 stsp }
3202 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3203 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3204 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3205 444d5325 2022-07-03 thomas if (err)
3206 444d5325 2022-07-03 thomas break;
3207 444d5325 2022-07-03 thomas }
3208 e78dc838 2020-12-04 stsp view->focussed = 0;
3209 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3210 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3211 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3212 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3213 6458efa5 2020-11-24 stsp err = view_close_child(view);
3214 6458efa5 2020-11-24 stsp if (err)
3215 6458efa5 2020-11-24 stsp return err;
3216 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3217 40236d76 2022-06-23 thomas if (err)
3218 40236d76 2022-06-23 thomas return err;
3219 e78dc838 2020-12-04 stsp view->focus_child = 1;
3220 6458efa5 2020-11-24 stsp } else
3221 6458efa5 2020-11-24 stsp *new_view = ref_view;
3222 6458efa5 2020-11-24 stsp break;
3223 1e37a5c2 2019-05-12 jcs default:
3224 07b0611c 2022-06-23 thomas view->count = 0;
3225 1e37a5c2 2019-05-12 jcs break;
3226 899d86c2 2018-05-10 stsp }
3227 e5a0f69f 2018-08-18 stsp
3228 80ddbec8 2018-04-29 stsp return err;
3229 80ddbec8 2018-04-29 stsp }
3230 80ddbec8 2018-04-29 stsp
3231 4ed7e80c 2018-05-20 stsp static const struct got_error *
3232 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3233 c2db6724 2019-01-04 stsp {
3234 c2db6724 2019-01-04 stsp const struct got_error *error;
3235 c2db6724 2019-01-04 stsp
3236 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3237 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3238 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3239 37c06ea4 2019-07-15 stsp #endif
3240 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3241 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3242 c2db6724 2019-01-04 stsp
3243 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3244 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3245 c2db6724 2019-01-04 stsp
3246 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3247 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3248 c2db6724 2019-01-04 stsp
3249 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3250 c2db6724 2019-01-04 stsp if (error != NULL)
3251 c2db6724 2019-01-04 stsp return error;
3252 c2db6724 2019-01-04 stsp
3253 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3254 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3255 c2db6724 2019-01-04 stsp
3256 c2db6724 2019-01-04 stsp return NULL;
3257 c2db6724 2019-01-04 stsp }
3258 c2db6724 2019-01-04 stsp
3259 a915003a 2019-02-05 stsp static void
3260 a915003a 2019-02-05 stsp init_curses(void)
3261 a915003a 2019-02-05 stsp {
3262 296152d1 2022-05-31 thomas /*
3263 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3264 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3265 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3266 296152d1 2022-05-31 thomas */
3267 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3268 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3269 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3270 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3271 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3272 296152d1 2022-05-31 thomas
3273 a915003a 2019-02-05 stsp initscr();
3274 a915003a 2019-02-05 stsp cbreak();
3275 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3276 a915003a 2019-02-05 stsp noecho();
3277 a915003a 2019-02-05 stsp nonl();
3278 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3279 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3280 a915003a 2019-02-05 stsp curs_set(0);
3281 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3282 6d17833f 2019-11-08 stsp start_color();
3283 6d17833f 2019-11-08 stsp use_default_colors();
3284 6d17833f 2019-11-08 stsp }
3285 a915003a 2019-02-05 stsp }
3286 a915003a 2019-02-05 stsp
3287 c2db6724 2019-01-04 stsp static const struct got_error *
3288 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3289 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3290 f135c941 2020-02-20 stsp {
3291 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3292 f135c941 2020-02-20 stsp
3293 f135c941 2020-02-20 stsp if (argc == 0) {
3294 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3295 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3296 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3297 f135c941 2020-02-20 stsp return NULL;
3298 f135c941 2020-02-20 stsp }
3299 f135c941 2020-02-20 stsp
3300 f135c941 2020-02-20 stsp if (worktree) {
3301 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3302 bfd61697 2020-11-14 stsp char *p;
3303 f135c941 2020-02-20 stsp
3304 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3305 f135c941 2020-02-20 stsp if (err)
3306 f135c941 2020-02-20 stsp return err;
3307 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3308 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3309 bfd61697 2020-11-14 stsp p) == -1) {
3310 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3311 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3312 f135c941 2020-02-20 stsp }
3313 f135c941 2020-02-20 stsp free(p);
3314 f135c941 2020-02-20 stsp } else
3315 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3316 f135c941 2020-02-20 stsp
3317 f135c941 2020-02-20 stsp return err;
3318 f135c941 2020-02-20 stsp }
3319 f135c941 2020-02-20 stsp
3320 f135c941 2020-02-20 stsp static const struct got_error *
3321 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3322 9f7d7167 2018-04-29 stsp {
3323 80ddbec8 2018-04-29 stsp const struct got_error *error;
3324 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3325 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3326 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3327 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3328 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3329 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3330 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3331 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3332 04cc582a 2018-08-01 stsp struct tog_view *view;
3333 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3334 80ddbec8 2018-04-29 stsp
3335 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3336 80ddbec8 2018-04-29 stsp switch (ch) {
3337 b672a97a 2020-01-27 stsp case 'b':
3338 b672a97a 2020-01-27 stsp log_branches = 1;
3339 b672a97a 2020-01-27 stsp break;
3340 80ddbec8 2018-04-29 stsp case 'c':
3341 80ddbec8 2018-04-29 stsp start_commit = optarg;
3342 80ddbec8 2018-04-29 stsp break;
3343 ecb28ae0 2018-07-16 stsp case 'r':
3344 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3345 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3346 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3347 9ba1d308 2019-10-21 stsp optarg);
3348 ecb28ae0 2018-07-16 stsp break;
3349 80ddbec8 2018-04-29 stsp default:
3350 17020d27 2019-03-07 stsp usage_log();
3351 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3352 80ddbec8 2018-04-29 stsp }
3353 80ddbec8 2018-04-29 stsp }
3354 80ddbec8 2018-04-29 stsp
3355 80ddbec8 2018-04-29 stsp argc -= optind;
3356 80ddbec8 2018-04-29 stsp argv += optind;
3357 80ddbec8 2018-04-29 stsp
3358 f135c941 2020-02-20 stsp if (argc > 1)
3359 f135c941 2020-02-20 stsp usage_log();
3360 963f97a1 2019-03-18 stsp
3361 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3362 7cd52833 2022-06-23 thomas if (error != NULL)
3363 7cd52833 2022-06-23 thomas goto done;
3364 7cd52833 2022-06-23 thomas
3365 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3366 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3367 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3368 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3369 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3370 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3371 c156c7a4 2020-12-18 stsp goto done;
3372 a1fbf39a 2019-08-11 stsp if (worktree)
3373 6962eb72 2020-02-20 stsp repo_path =
3374 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3375 a1fbf39a 2019-08-11 stsp else
3376 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3377 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3378 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3379 c156c7a4 2020-12-18 stsp goto done;
3380 c156c7a4 2020-12-18 stsp }
3381 ecb28ae0 2018-07-16 stsp }
3382 ecb28ae0 2018-07-16 stsp
3383 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3384 80ddbec8 2018-04-29 stsp if (error != NULL)
3385 ecb28ae0 2018-07-16 stsp goto done;
3386 80ddbec8 2018-04-29 stsp
3387 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3388 f135c941 2020-02-20 stsp repo, worktree);
3389 f135c941 2020-02-20 stsp if (error)
3390 f135c941 2020-02-20 stsp goto done;
3391 f135c941 2020-02-20 stsp
3392 f135c941 2020-02-20 stsp init_curses();
3393 f135c941 2020-02-20 stsp
3394 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3395 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3396 c02c541e 2019-03-29 stsp if (error)
3397 c02c541e 2019-03-29 stsp goto done;
3398 c02c541e 2019-03-29 stsp
3399 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3400 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3401 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3402 87670572 2020-12-26 naddy if (error)
3403 87670572 2020-12-26 naddy goto done;
3404 87670572 2020-12-26 naddy }
3405 51a10b52 2020-12-26 stsp
3406 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3407 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3408 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3409 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3410 d8f38dc4 2020-12-05 stsp if (error)
3411 d8f38dc4 2020-12-05 stsp goto done;
3412 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3413 d8f38dc4 2020-12-05 stsp } else {
3414 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3415 d8f38dc4 2020-12-05 stsp if (error == NULL)
3416 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3417 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3418 d8f38dc4 2020-12-05 stsp goto done;
3419 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3420 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3421 d8f38dc4 2020-12-05 stsp if (error)
3422 d8f38dc4 2020-12-05 stsp goto done;
3423 d8f38dc4 2020-12-05 stsp }
3424 8b473291 2019-02-21 stsp
3425 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3426 04cc582a 2018-08-01 stsp if (view == NULL) {
3427 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3428 04cc582a 2018-08-01 stsp goto done;
3429 04cc582a 2018-08-01 stsp }
3430 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3431 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3432 ba4f502b 2018-08-04 stsp if (error)
3433 ba4f502b 2018-08-04 stsp goto done;
3434 2fc00ff4 2019-08-31 stsp if (worktree) {
3435 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3436 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3437 2fc00ff4 2019-08-31 stsp worktree = NULL;
3438 2fc00ff4 2019-08-31 stsp }
3439 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3440 ecb28ae0 2018-07-16 stsp done:
3441 f135c941 2020-02-20 stsp free(in_repo_path);
3442 ecb28ae0 2018-07-16 stsp free(repo_path);
3443 ecb28ae0 2018-07-16 stsp free(cwd);
3444 899d86c2 2018-05-10 stsp free(start_id);
3445 d8f38dc4 2020-12-05 stsp free(label);
3446 d8f38dc4 2020-12-05 stsp if (ref)
3447 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3448 1d0f4054 2021-06-17 stsp if (repo) {
3449 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3450 1d0f4054 2021-06-17 stsp if (error == NULL)
3451 1d0f4054 2021-06-17 stsp error = close_err;
3452 1d0f4054 2021-06-17 stsp }
3453 ec142235 2019-03-07 stsp if (worktree)
3454 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3455 7cd52833 2022-06-23 thomas if (pack_fds) {
3456 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3457 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3458 7cd52833 2022-06-23 thomas if (error == NULL)
3459 7cd52833 2022-06-23 thomas error = pack_err;
3460 7cd52833 2022-06-23 thomas }
3461 51a10b52 2020-12-26 stsp tog_free_refs();
3462 80ddbec8 2018-04-29 stsp return error;
3463 9f7d7167 2018-04-29 stsp }
3464 9f7d7167 2018-04-29 stsp
3465 4ed7e80c 2018-05-20 stsp __dead static void
3466 9f7d7167 2018-04-29 stsp usage_diff(void)
3467 9f7d7167 2018-04-29 stsp {
3468 80ddbec8 2018-04-29 stsp endwin();
3469 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3470 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3471 9f7d7167 2018-04-29 stsp exit(1);
3472 b304db33 2018-05-20 stsp }
3473 b304db33 2018-05-20 stsp
3474 6d17833f 2019-11-08 stsp static int
3475 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3476 41605754 2020-11-12 stsp regmatch_t *regmatch)
3477 6d17833f 2019-11-08 stsp {
3478 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3479 6d17833f 2019-11-08 stsp }
3480 6d17833f 2019-11-08 stsp
3481 ef20f542 2022-06-26 thomas static struct tog_color *
3482 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3483 6d17833f 2019-11-08 stsp {
3484 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3485 6d17833f 2019-11-08 stsp
3486 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3487 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3488 6d17833f 2019-11-08 stsp return tc;
3489 6d17833f 2019-11-08 stsp }
3490 6d17833f 2019-11-08 stsp
3491 6d17833f 2019-11-08 stsp return NULL;
3492 6d17833f 2019-11-08 stsp }
3493 6d17833f 2019-11-08 stsp
3494 4ed7e80c 2018-05-20 stsp static const struct got_error *
3495 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3496 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3497 41605754 2020-11-12 stsp {
3498 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3499 666f7b10 2022-06-23 thomas char *exstr = NULL;
3500 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3501 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3502 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3503 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3504 41605754 2020-11-12 stsp
3505 41605754 2020-11-12 stsp *wtotal = 0;
3506 cbea3800 2022-06-23 thomas
3507 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3508 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3509 41605754 2020-11-12 stsp
3510 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3511 666f7b10 2022-06-23 thomas if (err)
3512 666f7b10 2022-06-23 thomas return err;
3513 666f7b10 2022-06-23 thomas
3514 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3515 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3516 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3517 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3518 666f7b10 2022-06-23 thomas goto done;
3519 666f7b10 2022-06-23 thomas }
3520 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3521 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3522 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3523 cbea3800 2022-06-23 thomas goto done;
3524 cbea3800 2022-06-23 thomas }
3525 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3526 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3527 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3528 cbea3800 2022-06-23 thomas goto done;
3529 cbea3800 2022-06-23 thomas }
3530 05171be4 2022-06-23 thomas
3531 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3532 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3533 cbea3800 2022-06-23 thomas col_tab_align, 1);
3534 cbea3800 2022-06-23 thomas if (err)
3535 cbea3800 2022-06-23 thomas goto done;
3536 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3537 05171be4 2022-06-23 thomas if (n) {
3538 cbea3800 2022-06-23 thomas free(wline);
3539 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3540 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3541 cbea3800 2022-06-23 thomas if (err)
3542 cbea3800 2022-06-23 thomas goto done;
3543 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3544 cbea3800 2022-06-23 thomas wlimit -= width;
3545 cbea3800 2022-06-23 thomas *wtotal += width;
3546 41605754 2020-11-12 stsp }
3547 41605754 2020-11-12 stsp
3548 41605754 2020-11-12 stsp if (wlimit > 0) {
3549 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3550 cbea3800 2022-06-23 thomas size_t wlen;
3551 cbea3800 2022-06-23 thomas
3552 cbea3800 2022-06-23 thomas free(wline);
3553 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3554 cbea3800 2022-06-23 thomas col_tab_align, 1);
3555 cbea3800 2022-06-23 thomas if (err)
3556 cbea3800 2022-06-23 thomas goto done;
3557 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3558 cbea3800 2022-06-23 thomas while (i < wlen) {
3559 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3560 cbea3800 2022-06-23 thomas if (width == -1) {
3561 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3562 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3563 cbea3800 2022-06-23 thomas goto done;
3564 cbea3800 2022-06-23 thomas }
3565 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3566 cbea3800 2022-06-23 thomas break;
3567 1c5e5faa 2022-06-23 thomas w += width;
3568 cbea3800 2022-06-23 thomas i++;
3569 41605754 2020-11-12 stsp }
3570 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3571 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3572 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3573 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3574 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3575 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3576 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3577 41605754 2020-11-12 stsp }
3578 41605754 2020-11-12 stsp }
3579 41605754 2020-11-12 stsp
3580 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3581 cbea3800 2022-06-23 thomas free(wline);
3582 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3583 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3584 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3585 cbea3800 2022-06-23 thomas col_tab_align, 1);
3586 cbea3800 2022-06-23 thomas if (err)
3587 cbea3800 2022-06-23 thomas goto done;
3588 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3589 cbea3800 2022-06-23 thomas } else {
3590 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3591 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3592 cbea3800 2022-06-23 thomas if (err)
3593 cbea3800 2022-06-23 thomas goto done;
3594 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3595 cbea3800 2022-06-23 thomas }
3596 cbea3800 2022-06-23 thomas *wtotal += width2;
3597 41605754 2020-11-12 stsp }
3598 cbea3800 2022-06-23 thomas done:
3599 05171be4 2022-06-23 thomas free(wline);
3600 666f7b10 2022-06-23 thomas free(exstr);
3601 cbea3800 2022-06-23 thomas free(seg0);
3602 cbea3800 2022-06-23 thomas free(seg1);
3603 cbea3800 2022-06-23 thomas free(seg2);
3604 cbea3800 2022-06-23 thomas return err;
3605 41605754 2020-11-12 stsp }
3606 41605754 2020-11-12 stsp
3607 41605754 2020-11-12 stsp static const struct got_error *
3608 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3609 26ed57b2 2018-05-19 stsp {
3610 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3611 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3612 61e69b96 2018-05-20 stsp const struct got_error *err;
3613 fe621944 2020-11-10 stsp int nprinted = 0;
3614 b304db33 2018-05-20 stsp char *line;
3615 826082fe 2020-12-10 stsp size_t linesize = 0;
3616 826082fe 2020-12-10 stsp ssize_t linelen;
3617 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3618 61e69b96 2018-05-20 stsp wchar_t *wline;
3619 e0b650dd 2018-05-20 stsp int width;
3620 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3621 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3622 fe621944 2020-11-10 stsp off_t line_offset;
3623 26ed57b2 2018-05-19 stsp
3624 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3625 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3626 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3627 fe621944 2020-11-10 stsp
3628 f7d12f7e 2018-08-01 stsp werase(view->window);
3629 a3404814 2018-09-02 stsp
3630 a3404814 2018-09-02 stsp if (header) {
3631 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3632 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3633 135a2da0 2020-11-11 stsp header) == -1)
3634 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3635 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3636 f91a2b48 2022-06-23 thomas 0, 0);
3637 135a2da0 2020-11-11 stsp free(line);
3638 135a2da0 2020-11-11 stsp if (err)
3639 a3404814 2018-09-02 stsp return err;
3640 a3404814 2018-09-02 stsp
3641 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3642 a3404814 2018-09-02 stsp wstandout(view->window);
3643 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3644 e54cc94a 2020-11-11 stsp free(wline);
3645 e54cc94a 2020-11-11 stsp wline = NULL;
3646 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3647 a3404814 2018-09-02 stsp wstandend(view->window);
3648 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3649 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3650 26ed57b2 2018-05-19 stsp
3651 a3404814 2018-09-02 stsp if (max_lines <= 1)
3652 a3404814 2018-09-02 stsp return NULL;
3653 a3404814 2018-09-02 stsp max_lines--;
3654 a3404814 2018-09-02 stsp }
3655 a3404814 2018-09-02 stsp
3656 89f1a395 2020-12-01 naddy s->eof = 0;
3657 05171be4 2022-06-23 thomas view->maxx = 0;
3658 826082fe 2020-12-10 stsp line = NULL;
3659 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3660 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3661 826082fe 2020-12-10 stsp if (linelen == -1) {
3662 826082fe 2020-12-10 stsp if (feof(s->f)) {
3663 826082fe 2020-12-10 stsp s->eof = 1;
3664 826082fe 2020-12-10 stsp break;
3665 826082fe 2020-12-10 stsp }
3666 826082fe 2020-12-10 stsp free(line);
3667 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3668 61e69b96 2018-05-20 stsp }
3669 05171be4 2022-06-23 thomas
3670 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3671 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3672 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3673 cbea3800 2022-06-23 thomas if (err) {
3674 cbea3800 2022-06-23 thomas free(line);
3675 cbea3800 2022-06-23 thomas return err;
3676 cbea3800 2022-06-23 thomas }
3677 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3678 cbea3800 2022-06-23 thomas free(wline);
3679 cbea3800 2022-06-23 thomas wline = NULL;
3680 cbea3800 2022-06-23 thomas
3681 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3682 f26dddb7 2019-11-08 stsp if (tc)
3683 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3684 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3685 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3686 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3687 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3688 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3689 41605754 2020-11-12 stsp if (err) {
3690 41605754 2020-11-12 stsp free(line);
3691 41605754 2020-11-12 stsp return err;
3692 41605754 2020-11-12 stsp }
3693 41605754 2020-11-12 stsp } else {
3694 f1c0ec19 2022-06-23 thomas int skip;
3695 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3696 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3697 f1c0ec19 2022-06-23 thomas if (err) {
3698 f1c0ec19 2022-06-23 thomas free(line);
3699 f1c0ec19 2022-06-23 thomas return err;
3700 f1c0ec19 2022-06-23 thomas }
3701 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3702 f1c0ec19 2022-06-23 thomas free(wline);
3703 41605754 2020-11-12 stsp wline = NULL;
3704 41605754 2020-11-12 stsp }
3705 f26dddb7 2019-11-08 stsp if (tc)
3706 6d17833f 2019-11-08 stsp wattr_off(view->window,
3707 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3708 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3709 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3710 fe621944 2020-11-10 stsp nprinted++;
3711 826082fe 2020-12-10 stsp }
3712 826082fe 2020-12-10 stsp free(line);
3713 fe621944 2020-11-10 stsp if (nprinted >= 1)
3714 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3715 89f1a395 2020-12-01 naddy (nprinted - 1);
3716 fe621944 2020-11-10 stsp else
3717 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3718 26ed57b2 2018-05-19 stsp
3719 a5d43cac 2022-07-01 thomas view_border(view);
3720 c3e9aa98 2019-05-13 jcs
3721 89f1a395 2020-12-01 naddy if (s->eof) {
3722 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3723 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3724 c3e9aa98 2019-05-13 jcs nprinted++;
3725 c3e9aa98 2019-05-13 jcs }
3726 c3e9aa98 2019-05-13 jcs
3727 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3728 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
3729 c3e9aa98 2019-05-13 jcs if (err) {
3730 c3e9aa98 2019-05-13 jcs return err;
3731 c3e9aa98 2019-05-13 jcs }
3732 26ed57b2 2018-05-19 stsp
3733 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3734 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3735 e54cc94a 2020-11-11 stsp free(wline);
3736 e54cc94a 2020-11-11 stsp wline = NULL;
3737 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3738 c3e9aa98 2019-05-13 jcs }
3739 c3e9aa98 2019-05-13 jcs
3740 26ed57b2 2018-05-19 stsp return NULL;
3741 abd2672a 2018-12-23 stsp }
3742 abd2672a 2018-12-23 stsp
3743 abd2672a 2018-12-23 stsp static char *
3744 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3745 abd2672a 2018-12-23 stsp {
3746 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3747 09867e48 2019-08-13 stsp char *p, *s;
3748 09867e48 2019-08-13 stsp
3749 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3750 09867e48 2019-08-13 stsp if (tm == NULL)
3751 09867e48 2019-08-13 stsp return NULL;
3752 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3753 09867e48 2019-08-13 stsp if (s == NULL)
3754 09867e48 2019-08-13 stsp return NULL;
3755 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3756 abd2672a 2018-12-23 stsp if (p)
3757 abd2672a 2018-12-23 stsp *p = '\0';
3758 abd2672a 2018-12-23 stsp return s;
3759 9f7d7167 2018-04-29 stsp }
3760 9f7d7167 2018-04-29 stsp
3761 4ed7e80c 2018-05-20 stsp static const struct got_error *
3762 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3763 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3764 0208f208 2020-05-05 stsp {
3765 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3766 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3767 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3768 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3769 0208f208 2020-05-05 stsp
3770 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3771 0208f208 2020-05-05 stsp if (qid != NULL) {
3772 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3773 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3774 ec242592 2022-04-22 thomas &qid->id);
3775 0208f208 2020-05-05 stsp if (err)
3776 0208f208 2020-05-05 stsp return err;
3777 0208f208 2020-05-05 stsp
3778 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3779 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3780 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3781 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3782 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3783 aa8b5dd0 2021-08-01 stsp }
3784 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3785 0208f208 2020-05-05 stsp
3786 0208f208 2020-05-05 stsp }
3787 0208f208 2020-05-05 stsp
3788 0208f208 2020-05-05 stsp if (tree_id1) {
3789 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3790 0208f208 2020-05-05 stsp if (err)
3791 0208f208 2020-05-05 stsp goto done;
3792 0208f208 2020-05-05 stsp }
3793 0208f208 2020-05-05 stsp
3794 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3795 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3796 0208f208 2020-05-05 stsp if (err)
3797 0208f208 2020-05-05 stsp goto done;
3798 0208f208 2020-05-05 stsp
3799 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3800 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3801 0208f208 2020-05-05 stsp done:
3802 0208f208 2020-05-05 stsp if (tree1)
3803 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3804 0208f208 2020-05-05 stsp if (tree2)
3805 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3806 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3807 0208f208 2020-05-05 stsp return err;
3808 0208f208 2020-05-05 stsp }
3809 0208f208 2020-05-05 stsp
3810 0208f208 2020-05-05 stsp static const struct got_error *
3811 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3812 abd2672a 2018-12-23 stsp {
3813 fe621944 2020-11-10 stsp off_t *p;
3814 fe621944 2020-11-10 stsp
3815 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3816 fe621944 2020-11-10 stsp if (p == NULL)
3817 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3818 fe621944 2020-11-10 stsp *line_offsets = p;
3819 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3820 fe621944 2020-11-10 stsp (*nlines)++;
3821 fe621944 2020-11-10 stsp return NULL;
3822 fe621944 2020-11-10 stsp }
3823 fe621944 2020-11-10 stsp
3824 fe621944 2020-11-10 stsp static const struct got_error *
3825 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3826 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3827 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3828 fe621944 2020-11-10 stsp {
3829 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3830 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3831 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3832 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3833 45d799e2 2018-12-23 stsp time_t committer_time;
3834 45d799e2 2018-12-23 stsp const char *author, *committer;
3835 8b473291 2019-02-21 stsp char *refs_str = NULL;
3836 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3837 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3838 fe621944 2020-11-10 stsp off_t outoff = 0;
3839 fe621944 2020-11-10 stsp int n;
3840 abd2672a 2018-12-23 stsp
3841 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3842 0208f208 2020-05-05 stsp
3843 8b473291 2019-02-21 stsp if (refs) {
3844 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3845 8b473291 2019-02-21 stsp if (err)
3846 8b473291 2019-02-21 stsp return err;
3847 8b473291 2019-02-21 stsp }
3848 8b473291 2019-02-21 stsp
3849 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3850 abd2672a 2018-12-23 stsp if (err)
3851 abd2672a 2018-12-23 stsp return err;
3852 abd2672a 2018-12-23 stsp
3853 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3854 15a94983 2018-12-23 stsp if (err) {
3855 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3856 15a94983 2018-12-23 stsp goto done;
3857 15a94983 2018-12-23 stsp }
3858 abd2672a 2018-12-23 stsp
3859 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3860 fe621944 2020-11-10 stsp if (err)
3861 fe621944 2020-11-10 stsp goto done;
3862 fe621944 2020-11-10 stsp
3863 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3864 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3865 fe621944 2020-11-10 stsp if (n < 0) {
3866 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3867 abd2672a 2018-12-23 stsp goto done;
3868 abd2672a 2018-12-23 stsp }
3869 fe621944 2020-11-10 stsp outoff += n;
3870 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3871 fe621944 2020-11-10 stsp if (err)
3872 fe621944 2020-11-10 stsp goto done;
3873 fe621944 2020-11-10 stsp
3874 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3875 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3876 fe621944 2020-11-10 stsp if (n < 0) {
3877 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3878 abd2672a 2018-12-23 stsp goto done;
3879 abd2672a 2018-12-23 stsp }
3880 fe621944 2020-11-10 stsp outoff += n;
3881 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3882 fe621944 2020-11-10 stsp if (err)
3883 fe621944 2020-11-10 stsp goto done;
3884 fe621944 2020-11-10 stsp
3885 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3886 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3887 fe621944 2020-11-10 stsp if (datestr) {
3888 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3889 fe621944 2020-11-10 stsp if (n < 0) {
3890 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3891 fe621944 2020-11-10 stsp goto done;
3892 fe621944 2020-11-10 stsp }
3893 fe621944 2020-11-10 stsp outoff += n;
3894 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3895 fe621944 2020-11-10 stsp if (err)
3896 fe621944 2020-11-10 stsp goto done;
3897 abd2672a 2018-12-23 stsp }
3898 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3899 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3900 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3901 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3902 fe621944 2020-11-10 stsp if (n < 0) {
3903 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3904 fe621944 2020-11-10 stsp goto done;
3905 fe621944 2020-11-10 stsp }
3906 fe621944 2020-11-10 stsp outoff += n;
3907 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3908 fe621944 2020-11-10 stsp if (err)
3909 fe621944 2020-11-10 stsp goto done;
3910 abd2672a 2018-12-23 stsp }
3911 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3912 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3913 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3914 ac4dc263 2021-09-24 thomas int pn = 1;
3915 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3916 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3917 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3918 ac4dc263 2021-09-24 thomas if (err)
3919 ac4dc263 2021-09-24 thomas goto done;
3920 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3921 ac4dc263 2021-09-24 thomas if (n < 0) {
3922 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3923 ac4dc263 2021-09-24 thomas goto done;
3924 ac4dc263 2021-09-24 thomas }
3925 ac4dc263 2021-09-24 thomas outoff += n;
3926 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3927 ac4dc263 2021-09-24 thomas if (err)
3928 ac4dc263 2021-09-24 thomas goto done;
3929 ac4dc263 2021-09-24 thomas free(id_str);
3930 ac4dc263 2021-09-24 thomas id_str = NULL;
3931 ac4dc263 2021-09-24 thomas }
3932 ac4dc263 2021-09-24 thomas }
3933 ac4dc263 2021-09-24 thomas
3934 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3935 5943eee2 2019-08-13 stsp if (err)
3936 5943eee2 2019-08-13 stsp goto done;
3937 fe621944 2020-11-10 stsp s = logmsg;
3938 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3939 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3940 fe621944 2020-11-10 stsp if (n < 0) {
3941 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3942 fe621944 2020-11-10 stsp goto done;
3943 fe621944 2020-11-10 stsp }
3944 fe621944 2020-11-10 stsp outoff += n;
3945 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3946 fe621944 2020-11-10 stsp if (err)
3947 fe621944 2020-11-10 stsp goto done;
3948 abd2672a 2018-12-23 stsp }
3949 fe621944 2020-11-10 stsp
3950 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3951 0208f208 2020-05-05 stsp if (err)
3952 0208f208 2020-05-05 stsp goto done;
3953 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3954 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3955 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3956 fe621944 2020-11-10 stsp if (n < 0) {
3957 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3958 fe621944 2020-11-10 stsp goto done;
3959 fe621944 2020-11-10 stsp }
3960 fe621944 2020-11-10 stsp outoff += n;
3961 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3962 fe621944 2020-11-10 stsp if (err)
3963 fe621944 2020-11-10 stsp goto done;
3964 0208f208 2020-05-05 stsp free((char *)pe->path);
3965 0208f208 2020-05-05 stsp free(pe->data);
3966 0208f208 2020-05-05 stsp }
3967 fe621944 2020-11-10 stsp
3968 0208f208 2020-05-05 stsp fputc('\n', outfile);
3969 fe621944 2020-11-10 stsp outoff++;
3970 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3971 abd2672a 2018-12-23 stsp done:
3972 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3973 abd2672a 2018-12-23 stsp free(id_str);
3974 5943eee2 2019-08-13 stsp free(logmsg);
3975 8b473291 2019-02-21 stsp free(refs_str);
3976 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3977 7510f233 2020-08-09 stsp if (err) {
3978 ae6a6978 2020-08-09 stsp free(*line_offsets);
3979 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3980 ae6a6978 2020-08-09 stsp *nlines = 0;
3981 7510f233 2020-08-09 stsp }
3982 fe621944 2020-11-10 stsp return err;
3983 abd2672a 2018-12-23 stsp }
3984 abd2672a 2018-12-23 stsp
3985 abd2672a 2018-12-23 stsp static const struct got_error *
3986 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3987 26ed57b2 2018-05-19 stsp {
3988 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3989 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3990 15a94983 2018-12-23 stsp int obj_type;
3991 fe621944 2020-11-10 stsp
3992 fe621944 2020-11-10 stsp free(s->line_offsets);
3993 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3994 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3995 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3996 fe621944 2020-11-10 stsp s->nlines = 0;
3997 26ed57b2 2018-05-19 stsp
3998 511a516b 2018-05-19 stsp f = got_opentemp();
3999 48ae06ee 2018-10-18 stsp if (f == NULL) {
4000 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4001 48ae06ee 2018-10-18 stsp goto done;
4002 48ae06ee 2018-10-18 stsp }
4003 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4004 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4005 fb43ecf1 2019-02-11 stsp goto done;
4006 fb43ecf1 2019-02-11 stsp }
4007 48ae06ee 2018-10-18 stsp s->f = f;
4008 26ed57b2 2018-05-19 stsp
4009 15a94983 2018-12-23 stsp if (s->id1)
4010 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4011 15a94983 2018-12-23 stsp else
4012 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4013 15a94983 2018-12-23 stsp if (err)
4014 15a94983 2018-12-23 stsp goto done;
4015 15a94983 2018-12-23 stsp
4016 15a94983 2018-12-23 stsp switch (obj_type) {
4017 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4018 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4019 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4020 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4021 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4022 26ed57b2 2018-05-19 stsp break;
4023 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4024 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4025 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4026 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4027 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4028 26ed57b2 2018-05-19 stsp break;
4029 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4030 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4031 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4032 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4033 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4034 abd2672a 2018-12-23 stsp
4035 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4036 abd2672a 2018-12-23 stsp if (err)
4037 3ffacbe1 2020-02-02 tracey goto done;
4038 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4039 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4040 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4041 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4042 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4043 f44b1f58 2020-02-02 tracey if (err)
4044 f44b1f58 2020-02-02 tracey goto done;
4045 f44b1f58 2020-02-02 tracey } else {
4046 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4047 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4048 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4049 fe621944 2020-11-10 stsp err = write_commit_info(
4050 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4051 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4052 f44b1f58 2020-02-02 tracey if (err)
4053 f44b1f58 2020-02-02 tracey goto done;
4054 f5404e4e 2020-02-02 tracey break;
4055 15a087fe 2019-02-21 stsp }
4056 abd2672a 2018-12-23 stsp }
4057 abd2672a 2018-12-23 stsp }
4058 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4059 abd2672a 2018-12-23 stsp
4060 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4061 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4062 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4063 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4064 26ed57b2 2018-05-19 stsp break;
4065 abd2672a 2018-12-23 stsp }
4066 26ed57b2 2018-05-19 stsp default:
4067 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4068 48ae06ee 2018-10-18 stsp break;
4069 26ed57b2 2018-05-19 stsp }
4070 f44b1f58 2020-02-02 tracey if (err)
4071 f44b1f58 2020-02-02 tracey goto done;
4072 48ae06ee 2018-10-18 stsp done:
4073 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4074 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4075 48ae06ee 2018-10-18 stsp return err;
4076 48ae06ee 2018-10-18 stsp }
4077 26ed57b2 2018-05-19 stsp
4078 f5215bb9 2019-02-22 stsp static void
4079 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4080 f5215bb9 2019-02-22 stsp {
4081 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4082 f5215bb9 2019-02-22 stsp update_panels();
4083 f5215bb9 2019-02-22 stsp doupdate();
4084 f44b1f58 2020-02-02 tracey }
4085 f44b1f58 2020-02-02 tracey
4086 f44b1f58 2020-02-02 tracey static const struct got_error *
4087 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4088 f44b1f58 2020-02-02 tracey {
4089 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4090 f44b1f58 2020-02-02 tracey
4091 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4092 f44b1f58 2020-02-02 tracey return NULL;
4093 f44b1f58 2020-02-02 tracey }
4094 f44b1f58 2020-02-02 tracey
4095 f44b1f58 2020-02-02 tracey static const struct got_error *
4096 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4097 f44b1f58 2020-02-02 tracey {
4098 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4099 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4100 f44b1f58 2020-02-02 tracey int lineno;
4101 78eecffc 2022-06-23 thomas char *line = NULL;
4102 826082fe 2020-12-10 stsp size_t linesize = 0;
4103 826082fe 2020-12-10 stsp ssize_t linelen;
4104 f44b1f58 2020-02-02 tracey
4105 f44b1f58 2020-02-02 tracey if (!view->searching) {
4106 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4107 f44b1f58 2020-02-02 tracey return NULL;
4108 f44b1f58 2020-02-02 tracey }
4109 f44b1f58 2020-02-02 tracey
4110 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4111 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4112 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4113 f44b1f58 2020-02-02 tracey else
4114 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4115 4b3f9dac 2021-12-17 thomas } else
4116 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4117 f44b1f58 2020-02-02 tracey
4118 f44b1f58 2020-02-02 tracey while (1) {
4119 f44b1f58 2020-02-02 tracey off_t offset;
4120 f44b1f58 2020-02-02 tracey
4121 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4122 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4123 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4124 f44b1f58 2020-02-02 tracey break;
4125 f44b1f58 2020-02-02 tracey }
4126 f44b1f58 2020-02-02 tracey
4127 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4128 f44b1f58 2020-02-02 tracey lineno = 1;
4129 f44b1f58 2020-02-02 tracey else
4130 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4131 f44b1f58 2020-02-02 tracey }
4132 f44b1f58 2020-02-02 tracey
4133 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4134 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4135 f44b1f58 2020-02-02 tracey free(line);
4136 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4137 f44b1f58 2020-02-02 tracey }
4138 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4139 78eecffc 2022-06-23 thomas if (linelen != -1) {
4140 78eecffc 2022-06-23 thomas char *exstr;
4141 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4142 78eecffc 2022-06-23 thomas if (err)
4143 78eecffc 2022-06-23 thomas break;
4144 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4145 78eecffc 2022-06-23 thomas &view->regmatch)) {
4146 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4147 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4148 78eecffc 2022-06-23 thomas free(exstr);
4149 78eecffc 2022-06-23 thomas break;
4150 78eecffc 2022-06-23 thomas }
4151 78eecffc 2022-06-23 thomas free(exstr);
4152 f44b1f58 2020-02-02 tracey }
4153 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4154 f44b1f58 2020-02-02 tracey lineno++;
4155 f44b1f58 2020-02-02 tracey else
4156 f44b1f58 2020-02-02 tracey lineno--;
4157 f44b1f58 2020-02-02 tracey }
4158 826082fe 2020-12-10 stsp free(line);
4159 f44b1f58 2020-02-02 tracey
4160 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4161 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4162 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4163 f44b1f58 2020-02-02 tracey }
4164 f44b1f58 2020-02-02 tracey
4165 05171be4 2022-06-23 thomas return err;
4166 f5215bb9 2019-02-22 stsp }
4167 f5215bb9 2019-02-22 stsp
4168 48ae06ee 2018-10-18 stsp static const struct got_error *
4169 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4170 a0f32f33 2022-06-13 thomas {
4171 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4172 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4173 a0f32f33 2022-06-13 thomas
4174 a0f32f33 2022-06-13 thomas free(s->id1);
4175 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4176 a0f32f33 2022-06-13 thomas free(s->id2);
4177 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4178 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4179 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4180 a0f32f33 2022-06-13 thomas s->f = NULL;
4181 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4182 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4183 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4184 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4185 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4186 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4187 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4188 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4189 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4190 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4191 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4192 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4193 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4194 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4195 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4196 a0f32f33 2022-06-13 thomas s->nlines = 0;
4197 a0f32f33 2022-06-13 thomas return err;
4198 a0f32f33 2022-06-13 thomas }
4199 a0f32f33 2022-06-13 thomas
4200 a0f32f33 2022-06-13 thomas static const struct got_error *
4201 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4202 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4203 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4204 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4205 48ae06ee 2018-10-18 stsp {
4206 48ae06ee 2018-10-18 stsp const struct got_error *err;
4207 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4208 5dc9f4bc 2018-08-04 stsp
4209 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4210 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4211 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4212 a0f32f33 2022-06-13 thomas
4213 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4214 15a94983 2018-12-23 stsp int type1, type2;
4215 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4216 15a94983 2018-12-23 stsp if (err)
4217 15a94983 2018-12-23 stsp return err;
4218 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4219 15a94983 2018-12-23 stsp if (err)
4220 15a94983 2018-12-23 stsp return err;
4221 15a94983 2018-12-23 stsp
4222 15a94983 2018-12-23 stsp if (type1 != type2)
4223 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4224 15a94983 2018-12-23 stsp }
4225 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4226 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4227 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4228 f44b1f58 2020-02-02 tracey s->repo = repo;
4229 f44b1f58 2020-02-02 tracey s->id1 = id1;
4230 f44b1f58 2020-02-02 tracey s->id2 = id2;
4231 3dbaef42 2020-11-24 stsp s->label1 = label1;
4232 3dbaef42 2020-11-24 stsp s->label2 = label2;
4233 48ae06ee 2018-10-18 stsp
4234 15a94983 2018-12-23 stsp if (id1) {
4235 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4236 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4237 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4238 48ae06ee 2018-10-18 stsp } else
4239 5465d566 2020-02-01 tracey s->id1 = NULL;
4240 48ae06ee 2018-10-18 stsp
4241 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4242 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4243 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4244 a0f32f33 2022-06-13 thomas goto done;
4245 48ae06ee 2018-10-18 stsp }
4246 a0f32f33 2022-06-13 thomas
4247 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4248 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4249 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4250 9a1d7435 2022-06-23 thomas goto done;
4251 9a1d7435 2022-06-23 thomas }
4252 9a1d7435 2022-06-23 thomas
4253 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4254 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4255 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4256 a0f32f33 2022-06-13 thomas goto done;
4257 a0f32f33 2022-06-13 thomas }
4258 a0f32f33 2022-06-13 thomas
4259 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4260 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4261 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4262 19a6a6b5 2022-07-01 thomas goto done;
4263 19a6a6b5 2022-07-01 thomas }
4264 19a6a6b5 2022-07-01 thomas
4265 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4266 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4267 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4268 19a6a6b5 2022-07-01 thomas goto done;
4269 19a6a6b5 2022-07-01 thomas }
4270 19a6a6b5 2022-07-01 thomas
4271 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4272 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4273 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4274 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4275 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4276 5465d566 2020-02-01 tracey s->log_view = log_view;
4277 5465d566 2020-02-01 tracey s->repo = repo;
4278 6d17833f 2019-11-08 stsp
4279 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4280 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4281 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4282 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4283 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4284 6d17833f 2019-11-08 stsp if (err)
4285 a0f32f33 2022-06-13 thomas goto done;
4286 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4287 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4288 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4289 a0f32f33 2022-06-13 thomas if (err)
4290 a0f32f33 2022-06-13 thomas goto done;
4291 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4292 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4293 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4294 a0f32f33 2022-06-13 thomas if (err)
4295 a0f32f33 2022-06-13 thomas goto done;
4296 6d17833f 2019-11-08 stsp
4297 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4298 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4299 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4300 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4301 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4302 a0f32f33 2022-06-13 thomas if (err)
4303 a0f32f33 2022-06-13 thomas goto done;
4304 11b20872 2019-11-08 stsp
4305 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4306 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4307 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4308 a0f32f33 2022-06-13 thomas if (err)
4309 a0f32f33 2022-06-13 thomas goto done;
4310 11b20872 2019-11-08 stsp
4311 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4312 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4313 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4314 a0f32f33 2022-06-13 thomas if (err)
4315 a0f32f33 2022-06-13 thomas goto done;
4316 6d17833f 2019-11-08 stsp }
4317 5dc9f4bc 2018-08-04 stsp
4318 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4319 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4320 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4321 f5215bb9 2019-02-22 stsp
4322 5465d566 2020-02-01 tracey err = create_diff(s);
4323 48ae06ee 2018-10-18 stsp
4324 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4325 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4326 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4327 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4328 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4329 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4330 a0f32f33 2022-06-13 thomas done:
4331 a0f32f33 2022-06-13 thomas if (err)
4332 a0f32f33 2022-06-13 thomas close_diff_view(view);
4333 e5a0f69f 2018-08-18 stsp return err;
4334 5dc9f4bc 2018-08-04 stsp }
4335 5dc9f4bc 2018-08-04 stsp
4336 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4337 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4338 5dc9f4bc 2018-08-04 stsp {
4339 a3404814 2018-09-02 stsp const struct got_error *err;
4340 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4341 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4342 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4343 a3404814 2018-09-02 stsp
4344 a3404814 2018-09-02 stsp if (s->id1) {
4345 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4346 a3404814 2018-09-02 stsp if (err)
4347 a3404814 2018-09-02 stsp return err;
4348 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4349 3dbaef42 2020-11-24 stsp } else
4350 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4351 3dbaef42 2020-11-24 stsp
4352 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4353 a3404814 2018-09-02 stsp if (err)
4354 a3404814 2018-09-02 stsp return err;
4355 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4356 26ed57b2 2018-05-19 stsp
4357 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4358 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4359 a3404814 2018-09-02 stsp free(id_str1);
4360 a3404814 2018-09-02 stsp free(id_str2);
4361 a3404814 2018-09-02 stsp return err;
4362 a3404814 2018-09-02 stsp }
4363 a3404814 2018-09-02 stsp free(id_str1);
4364 a3404814 2018-09-02 stsp free(id_str2);
4365 a3404814 2018-09-02 stsp
4366 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4367 267bb3b8 2021-08-01 stsp free(header);
4368 267bb3b8 2021-08-01 stsp return err;
4369 15a087fe 2019-02-21 stsp }
4370 15a087fe 2019-02-21 stsp
4371 15a087fe 2019-02-21 stsp static const struct got_error *
4372 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4373 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4374 15a087fe 2019-02-21 stsp {
4375 d7a04538 2019-02-21 stsp const struct got_error *err;
4376 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4377 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4378 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4379 15a087fe 2019-02-21 stsp
4380 15a087fe 2019-02-21 stsp free(s->id2);
4381 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4382 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4383 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4384 15a087fe 2019-02-21 stsp
4385 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4386 d7a04538 2019-02-21 stsp if (err)
4387 d7a04538 2019-02-21 stsp return err;
4388 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4389 15a087fe 2019-02-21 stsp free(s->id1);
4390 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4391 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4392 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4393 15a087fe 2019-02-21 stsp return NULL;
4394 0cf4efb1 2018-09-29 stsp }
4395 0cf4efb1 2018-09-29 stsp
4396 0cf4efb1 2018-09-29 stsp static const struct got_error *
4397 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4398 adf4c9e0 2022-07-03 thomas {
4399 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4400 adf4c9e0 2022-07-03 thomas
4401 adf4c9e0 2022-07-03 thomas view->count = 0;
4402 adf4c9e0 2022-07-03 thomas wclear(view->window);
4403 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4404 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4405 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4406 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4407 adf4c9e0 2022-07-03 thomas return create_diff(s);
4408 adf4c9e0 2022-07-03 thomas }
4409 adf4c9e0 2022-07-03 thomas
4410 adf4c9e0 2022-07-03 thomas static const struct got_error *
4411 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4412 e5a0f69f 2018-08-18 stsp {
4413 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4414 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4415 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4416 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4417 826082fe 2020-12-10 stsp char *line = NULL;
4418 826082fe 2020-12-10 stsp size_t linesize = 0;
4419 826082fe 2020-12-10 stsp ssize_t linelen;
4420 70f17a53 2022-06-13 thomas int i, nscroll = view->nlines - 1;
4421 e5a0f69f 2018-08-18 stsp
4422 e5a0f69f 2018-08-18 stsp switch (ch) {
4423 05171be4 2022-06-23 thomas case '0':
4424 05171be4 2022-06-23 thomas view->x = 0;
4425 05171be4 2022-06-23 thomas break;
4426 05171be4 2022-06-23 thomas case '$':
4427 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4428 07b0611c 2022-06-23 thomas view->count = 0;
4429 05171be4 2022-06-23 thomas break;
4430 05171be4 2022-06-23 thomas case KEY_RIGHT:
4431 05171be4 2022-06-23 thomas case 'l':
4432 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4433 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4434 07b0611c 2022-06-23 thomas else
4435 07b0611c 2022-06-23 thomas view->count = 0;
4436 05171be4 2022-06-23 thomas break;
4437 05171be4 2022-06-23 thomas case KEY_LEFT:
4438 05171be4 2022-06-23 thomas case 'h':
4439 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4440 07b0611c 2022-06-23 thomas if (view->x <= 0)
4441 07b0611c 2022-06-23 thomas view->count = 0;
4442 05171be4 2022-06-23 thomas break;
4443 64453f7e 2020-11-21 stsp case 'a':
4444 3dbaef42 2020-11-24 stsp case 'w':
4445 3dbaef42 2020-11-24 stsp if (ch == 'a')
4446 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4447 3dbaef42 2020-11-24 stsp if (ch == 'w')
4448 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4449 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4450 912a3f79 2021-08-30 j break;
4451 912a3f79 2021-08-30 j case 'g':
4452 912a3f79 2021-08-30 j case KEY_HOME:
4453 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4454 07b0611c 2022-06-23 thomas view->count = 0;
4455 64453f7e 2020-11-21 stsp break;
4456 912a3f79 2021-08-30 j case 'G':
4457 912a3f79 2021-08-30 j case KEY_END:
4458 07b0611c 2022-06-23 thomas view->count = 0;
4459 912a3f79 2021-08-30 j if (s->eof)
4460 912a3f79 2021-08-30 j break;
4461 912a3f79 2021-08-30 j
4462 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4463 912a3f79 2021-08-30 j s->eof = 1;
4464 912a3f79 2021-08-30 j break;
4465 1e37a5c2 2019-05-12 jcs case 'k':
4466 1e37a5c2 2019-05-12 jcs case KEY_UP:
4467 f7140bf5 2021-10-17 thomas case CTRL('p'):
4468 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4469 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4470 07b0611c 2022-06-23 thomas else
4471 07b0611c 2022-06-23 thomas view->count = 0;
4472 1e37a5c2 2019-05-12 jcs break;
4473 70f17a53 2022-06-13 thomas case CTRL('u'):
4474 23427b14 2022-06-23 thomas case 'u':
4475 70f17a53 2022-06-13 thomas nscroll /= 2;
4476 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4477 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4478 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4479 1c5e5faa 2022-06-23 thomas case 'b':
4480 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4481 07b0611c 2022-06-23 thomas view->count = 0;
4482 26ed57b2 2018-05-19 stsp break;
4483 07b0611c 2022-06-23 thomas }
4484 1e37a5c2 2019-05-12 jcs i = 0;
4485 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4486 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4487 1e37a5c2 2019-05-12 jcs break;
4488 1e37a5c2 2019-05-12 jcs case 'j':
4489 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4490 f7140bf5 2021-10-17 thomas case CTRL('n'):
4491 1e37a5c2 2019-05-12 jcs if (!s->eof)
4492 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4493 07b0611c 2022-06-23 thomas else
4494 07b0611c 2022-06-23 thomas view->count = 0;
4495 1e37a5c2 2019-05-12 jcs break;
4496 70f17a53 2022-06-13 thomas case CTRL('d'):
4497 23427b14 2022-06-23 thomas case 'd':
4498 70f17a53 2022-06-13 thomas nscroll /= 2;
4499 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4500 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4501 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4502 1c5e5faa 2022-06-23 thomas case 'f':
4503 1e37a5c2 2019-05-12 jcs case ' ':
4504 07b0611c 2022-06-23 thomas if (s->eof) {
4505 07b0611c 2022-06-23 thomas view->count = 0;
4506 1e37a5c2 2019-05-12 jcs break;
4507 07b0611c 2022-06-23 thomas }
4508 1e37a5c2 2019-05-12 jcs i = 0;
4509 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4510 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4511 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4512 826082fe 2020-12-10 stsp if (linelen == -1) {
4513 826082fe 2020-12-10 stsp if (feof(s->f)) {
4514 826082fe 2020-12-10 stsp s->eof = 1;
4515 826082fe 2020-12-10 stsp } else
4516 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4517 34bc9ec9 2019-02-22 stsp break;
4518 826082fe 2020-12-10 stsp }
4519 1e37a5c2 2019-05-12 jcs }
4520 826082fe 2020-12-10 stsp free(line);
4521 1e37a5c2 2019-05-12 jcs break;
4522 1e37a5c2 2019-05-12 jcs case '[':
4523 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4524 1e37a5c2 2019-05-12 jcs s->diff_context--;
4525 aa61903a 2021-12-31 thomas s->matched_line = 0;
4526 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4527 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4528 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4529 27829c9e 2020-11-21 stsp s->nlines) {
4530 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4531 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4532 27829c9e 2020-11-21 stsp }
4533 07b0611c 2022-06-23 thomas } else
4534 07b0611c 2022-06-23 thomas view->count = 0;
4535 1e37a5c2 2019-05-12 jcs break;
4536 1e37a5c2 2019-05-12 jcs case ']':
4537 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4538 1e37a5c2 2019-05-12 jcs s->diff_context++;
4539 aa61903a 2021-12-31 thomas s->matched_line = 0;
4540 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4541 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4542 07b0611c 2022-06-23 thomas } else
4543 07b0611c 2022-06-23 thomas view->count = 0;
4544 1e37a5c2 2019-05-12 jcs break;
4545 1e37a5c2 2019-05-12 jcs case '<':
4546 1e37a5c2 2019-05-12 jcs case ',':
4547 07b0611c 2022-06-23 thomas if (s->log_view == NULL) {
4548 07b0611c 2022-06-23 thomas view->count = 0;
4549 48ae06ee 2018-10-18 stsp break;
4550 07b0611c 2022-06-23 thomas }
4551 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4552 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4553 6524637e 2019-02-21 stsp
4554 07b0611c 2022-06-23 thomas /* view->count handled in input_log_view() */
4555 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4556 1e37a5c2 2019-05-12 jcs if (err)
4557 1e37a5c2 2019-05-12 jcs break;
4558 15a087fe 2019-02-21 stsp
4559 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4560 5a8b5076 2020-12-05 stsp break;
4561 5a8b5076 2020-12-05 stsp
4562 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4563 1e37a5c2 2019-05-12 jcs if (err)
4564 1e37a5c2 2019-05-12 jcs break;
4565 15a087fe 2019-02-21 stsp
4566 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4567 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4568 aa61903a 2021-12-31 thomas s->matched_line = 0;
4569 05171be4 2022-06-23 thomas view->x = 0;
4570 15a087fe 2019-02-21 stsp
4571 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4572 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4573 1e37a5c2 2019-05-12 jcs break;
4574 1e37a5c2 2019-05-12 jcs case '>':
4575 1e37a5c2 2019-05-12 jcs case '.':
4576 07b0611c 2022-06-23 thomas if (s->log_view == NULL) {
4577 07b0611c 2022-06-23 thomas view->count = 0;
4578 15a087fe 2019-02-21 stsp break;
4579 07b0611c 2022-06-23 thomas }
4580 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4581 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4582 5e224a3e 2019-02-22 stsp
4583 07b0611c 2022-06-23 thomas /* view->count handled in input_log_view() */
4584 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4585 1e37a5c2 2019-05-12 jcs if (err)
4586 5a8b5076 2020-12-05 stsp break;
4587 5a8b5076 2020-12-05 stsp
4588 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4589 1e37a5c2 2019-05-12 jcs break;
4590 15a087fe 2019-02-21 stsp
4591 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4592 1e37a5c2 2019-05-12 jcs if (err)
4593 1e37a5c2 2019-05-12 jcs break;
4594 15a087fe 2019-02-21 stsp
4595 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4596 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4597 aa61903a 2021-12-31 thomas s->matched_line = 0;
4598 05171be4 2022-06-23 thomas view->x = 0;
4599 1e37a5c2 2019-05-12 jcs
4600 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4601 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4602 1e37a5c2 2019-05-12 jcs break;
4603 1e37a5c2 2019-05-12 jcs default:
4604 07b0611c 2022-06-23 thomas view->count = 0;
4605 1e37a5c2 2019-05-12 jcs break;
4606 26ed57b2 2018-05-19 stsp }
4607 e5a0f69f 2018-08-18 stsp
4608 bcbd79e2 2018-08-19 stsp return err;
4609 26ed57b2 2018-05-19 stsp }
4610 26ed57b2 2018-05-19 stsp
4611 4ed7e80c 2018-05-20 stsp static const struct got_error *
4612 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4613 9f7d7167 2018-04-29 stsp {
4614 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4615 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4616 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4617 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4618 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4619 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4620 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4621 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4622 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4623 3dbaef42 2020-11-24 stsp const char *errstr;
4624 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4625 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4626 70ac5f84 2019-03-28 stsp
4627 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4628 26ed57b2 2018-05-19 stsp switch (ch) {
4629 64453f7e 2020-11-21 stsp case 'a':
4630 64453f7e 2020-11-21 stsp force_text_diff = 1;
4631 3dbaef42 2020-11-24 stsp break;
4632 3dbaef42 2020-11-24 stsp case 'C':
4633 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4634 3dbaef42 2020-11-24 stsp &errstr);
4635 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4636 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4637 8f666e67 2022-02-12 thomas errstr, errstr);
4638 64453f7e 2020-11-21 stsp break;
4639 09b5bff8 2020-02-23 naddy case 'r':
4640 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4641 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4642 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4643 09b5bff8 2020-02-23 naddy optarg);
4644 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4645 09b5bff8 2020-02-23 naddy break;
4646 3dbaef42 2020-11-24 stsp case 'w':
4647 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4648 3dbaef42 2020-11-24 stsp break;
4649 26ed57b2 2018-05-19 stsp default:
4650 17020d27 2019-03-07 stsp usage_diff();
4651 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4652 26ed57b2 2018-05-19 stsp }
4653 26ed57b2 2018-05-19 stsp }
4654 26ed57b2 2018-05-19 stsp
4655 26ed57b2 2018-05-19 stsp argc -= optind;
4656 26ed57b2 2018-05-19 stsp argv += optind;
4657 26ed57b2 2018-05-19 stsp
4658 26ed57b2 2018-05-19 stsp if (argc == 0) {
4659 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4660 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4661 15a94983 2018-12-23 stsp id_str1 = argv[0];
4662 15a94983 2018-12-23 stsp id_str2 = argv[1];
4663 26ed57b2 2018-05-19 stsp } else
4664 26ed57b2 2018-05-19 stsp usage_diff();
4665 eb6600df 2019-01-04 stsp
4666 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4667 7cd52833 2022-06-23 thomas if (error)
4668 7cd52833 2022-06-23 thomas goto done;
4669 7cd52833 2022-06-23 thomas
4670 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4671 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4672 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4673 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4674 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4675 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4676 c156c7a4 2020-12-18 stsp goto done;
4677 a273ac94 2020-02-23 naddy if (worktree)
4678 a273ac94 2020-02-23 naddy repo_path =
4679 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4680 a273ac94 2020-02-23 naddy else
4681 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4682 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4683 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4684 c156c7a4 2020-12-18 stsp goto done;
4685 c156c7a4 2020-12-18 stsp }
4686 a273ac94 2020-02-23 naddy }
4687 a273ac94 2020-02-23 naddy
4688 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4689 eb6600df 2019-01-04 stsp if (error)
4690 eb6600df 2019-01-04 stsp goto done;
4691 26ed57b2 2018-05-19 stsp
4692 a273ac94 2020-02-23 naddy init_curses();
4693 a273ac94 2020-02-23 naddy
4694 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4695 51a10b52 2020-12-26 stsp if (error)
4696 51a10b52 2020-12-26 stsp goto done;
4697 51a10b52 2020-12-26 stsp
4698 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4699 26ed57b2 2018-05-19 stsp if (error)
4700 26ed57b2 2018-05-19 stsp goto done;
4701 26ed57b2 2018-05-19 stsp
4702 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4703 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4704 26ed57b2 2018-05-19 stsp if (error)
4705 26ed57b2 2018-05-19 stsp goto done;
4706 26ed57b2 2018-05-19 stsp
4707 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4708 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4709 26ed57b2 2018-05-19 stsp if (error)
4710 26ed57b2 2018-05-19 stsp goto done;
4711 26ed57b2 2018-05-19 stsp
4712 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4713 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4714 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4715 ea5e7bb5 2018-08-01 stsp goto done;
4716 ea5e7bb5 2018-08-01 stsp }
4717 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4718 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4719 5dc9f4bc 2018-08-04 stsp if (error)
4720 5dc9f4bc 2018-08-04 stsp goto done;
4721 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4722 26ed57b2 2018-05-19 stsp done:
4723 3dbaef42 2020-11-24 stsp free(label1);
4724 3dbaef42 2020-11-24 stsp free(label2);
4725 c02c541e 2019-03-29 stsp free(repo_path);
4726 a273ac94 2020-02-23 naddy free(cwd);
4727 1d0f4054 2021-06-17 stsp if (repo) {
4728 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4729 1d0f4054 2021-06-17 stsp if (error == NULL)
4730 1d0f4054 2021-06-17 stsp error = close_err;
4731 1d0f4054 2021-06-17 stsp }
4732 a273ac94 2020-02-23 naddy if (worktree)
4733 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4734 7cd52833 2022-06-23 thomas if (pack_fds) {
4735 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4736 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4737 7cd52833 2022-06-23 thomas if (error == NULL)
4738 7cd52833 2022-06-23 thomas error = pack_err;
4739 7cd52833 2022-06-23 thomas }
4740 51a10b52 2020-12-26 stsp tog_free_refs();
4741 26ed57b2 2018-05-19 stsp return error;
4742 9f7d7167 2018-04-29 stsp }
4743 9f7d7167 2018-04-29 stsp
4744 4ed7e80c 2018-05-20 stsp __dead static void
4745 9f7d7167 2018-04-29 stsp usage_blame(void)
4746 9f7d7167 2018-04-29 stsp {
4747 80ddbec8 2018-04-29 stsp endwin();
4748 91198554 2022-06-23 thomas fprintf(stderr,
4749 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
4750 9f7d7167 2018-04-29 stsp getprogname());
4751 9f7d7167 2018-04-29 stsp exit(1);
4752 9f7d7167 2018-04-29 stsp }
4753 84451b3e 2018-07-10 stsp
4754 84451b3e 2018-07-10 stsp struct tog_blame_line {
4755 84451b3e 2018-07-10 stsp int annotated;
4756 84451b3e 2018-07-10 stsp struct got_object_id *id;
4757 84451b3e 2018-07-10 stsp };
4758 9f7d7167 2018-04-29 stsp
4759 4ed7e80c 2018-05-20 stsp static const struct got_error *
4760 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4761 84451b3e 2018-07-10 stsp {
4762 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4763 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4764 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4765 84451b3e 2018-07-10 stsp const struct got_error *err;
4766 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
4767 826082fe 2020-12-10 stsp char *line = NULL;
4768 826082fe 2020-12-10 stsp size_t linesize = 0;
4769 826082fe 2020-12-10 stsp ssize_t linelen;
4770 84451b3e 2018-07-10 stsp wchar_t *wline;
4771 27a741e5 2019-09-11 stsp int width;
4772 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4773 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4774 ab089a2a 2018-07-12 stsp char *id_str;
4775 11b20872 2019-11-08 stsp struct tog_color *tc;
4776 ab089a2a 2018-07-12 stsp
4777 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4778 ab089a2a 2018-07-12 stsp if (err)
4779 ab089a2a 2018-07-12 stsp return err;
4780 84451b3e 2018-07-10 stsp
4781 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4782 f7d12f7e 2018-08-01 stsp werase(view->window);
4783 84451b3e 2018-07-10 stsp
4784 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4785 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4786 ab089a2a 2018-07-12 stsp free(id_str);
4787 ab089a2a 2018-07-12 stsp return err;
4788 ab089a2a 2018-07-12 stsp }
4789 ab089a2a 2018-07-12 stsp
4790 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4791 ab089a2a 2018-07-12 stsp free(line);
4792 2550e4c3 2018-07-13 stsp line = NULL;
4793 1cae65b4 2019-09-22 stsp if (err)
4794 1cae65b4 2019-09-22 stsp return err;
4795 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4796 a3404814 2018-09-02 stsp wstandout(view->window);
4797 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4798 11b20872 2019-11-08 stsp if (tc)
4799 11b20872 2019-11-08 stsp wattr_on(view->window,
4800 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4801 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4802 11b20872 2019-11-08 stsp if (tc)
4803 11b20872 2019-11-08 stsp wattr_off(view->window,
4804 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4805 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4806 a3404814 2018-09-02 stsp wstandend(view->window);
4807 2550e4c3 2018-07-13 stsp free(wline);
4808 2550e4c3 2018-07-13 stsp wline = NULL;
4809 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4810 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4811 ab089a2a 2018-07-12 stsp
4812 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4813 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4814 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4815 ab089a2a 2018-07-12 stsp free(id_str);
4816 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4817 ab089a2a 2018-07-12 stsp }
4818 ab089a2a 2018-07-12 stsp free(id_str);
4819 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4820 3f60a8ef 2018-07-10 stsp free(line);
4821 2550e4c3 2018-07-13 stsp line = NULL;
4822 3f60a8ef 2018-07-10 stsp if (err)
4823 3f60a8ef 2018-07-10 stsp return err;
4824 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4825 2550e4c3 2018-07-13 stsp free(wline);
4826 2550e4c3 2018-07-13 stsp wline = NULL;
4827 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4828 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4829 3f60a8ef 2018-07-10 stsp
4830 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4831 05171be4 2022-06-23 thomas view->maxx = 0;
4832 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4833 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4834 826082fe 2020-12-10 stsp if (linelen == -1) {
4835 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4836 826082fe 2020-12-10 stsp s->eof = 1;
4837 826082fe 2020-12-10 stsp break;
4838 826082fe 2020-12-10 stsp }
4839 84451b3e 2018-07-10 stsp free(line);
4840 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4841 84451b3e 2018-07-10 stsp }
4842 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4843 826082fe 2020-12-10 stsp continue;
4844 cbea3800 2022-06-23 thomas
4845 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4846 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4847 cbea3800 2022-06-23 thomas if (err) {
4848 cbea3800 2022-06-23 thomas free(line);
4849 cbea3800 2022-06-23 thomas return err;
4850 cbea3800 2022-06-23 thomas }
4851 cbea3800 2022-06-23 thomas free(wline);
4852 cbea3800 2022-06-23 thomas wline = NULL;
4853 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4854 84451b3e 2018-07-10 stsp
4855 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4856 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4857 b700b5d6 2018-07-10 stsp
4858 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4859 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4860 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4861 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4862 27a741e5 2019-09-11 stsp !(view->focussed &&
4863 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4864 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4865 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4866 8d0fe45a 2019-08-12 stsp char *id_str;
4867 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
4868 91198554 2022-06-23 thomas blame_line->id);
4869 8d0fe45a 2019-08-12 stsp if (err) {
4870 8d0fe45a 2019-08-12 stsp free(line);
4871 8d0fe45a 2019-08-12 stsp return err;
4872 8d0fe45a 2019-08-12 stsp }
4873 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4874 11b20872 2019-11-08 stsp if (tc)
4875 11b20872 2019-11-08 stsp wattr_on(view->window,
4876 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4877 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4878 11b20872 2019-11-08 stsp if (tc)
4879 11b20872 2019-11-08 stsp wattr_off(view->window,
4880 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4881 8d0fe45a 2019-08-12 stsp free(id_str);
4882 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4883 8d0fe45a 2019-08-12 stsp } else {
4884 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4885 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4886 84451b3e 2018-07-10 stsp }
4887 ee41ec32 2018-07-10 stsp } else {
4888 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4889 ee41ec32 2018-07-10 stsp prev_id = NULL;
4890 ee41ec32 2018-07-10 stsp }
4891 84451b3e 2018-07-10 stsp
4892 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4893 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4894 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4895 27a741e5 2019-09-11 stsp
4896 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4897 41605754 2020-11-12 stsp width = 9;
4898 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4899 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4900 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4901 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4902 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4903 41605754 2020-11-12 stsp if (err) {
4904 41605754 2020-11-12 stsp free(line);
4905 41605754 2020-11-12 stsp return err;
4906 41605754 2020-11-12 stsp }
4907 41605754 2020-11-12 stsp width += 9;
4908 41605754 2020-11-12 stsp } else {
4909 923086fd 2022-06-23 thomas int skip;
4910 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4911 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
4912 923086fd 2022-06-23 thomas if (err) {
4913 923086fd 2022-06-23 thomas free(line);
4914 923086fd 2022-06-23 thomas return err;
4915 05171be4 2022-06-23 thomas }
4916 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4917 02bce7e2 2022-06-23 thomas width += 9;
4918 41605754 2020-11-12 stsp free(wline);
4919 41605754 2020-11-12 stsp wline = NULL;
4920 41605754 2020-11-12 stsp }
4921 41605754 2020-11-12 stsp
4922 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4923 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4924 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4925 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4926 84451b3e 2018-07-10 stsp }
4927 826082fe 2020-12-10 stsp free(line);
4928 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4929 84451b3e 2018-07-10 stsp
4930 a5d43cac 2022-07-01 thomas view_border(view);
4931 84451b3e 2018-07-10 stsp
4932 84451b3e 2018-07-10 stsp return NULL;
4933 84451b3e 2018-07-10 stsp }
4934 84451b3e 2018-07-10 stsp
4935 84451b3e 2018-07-10 stsp static const struct got_error *
4936 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4937 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4938 84451b3e 2018-07-10 stsp {
4939 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4940 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4941 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4942 1a76625f 2018-10-22 stsp int errcode;
4943 84451b3e 2018-07-10 stsp
4944 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4945 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4946 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4947 84451b3e 2018-07-10 stsp
4948 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4949 1a76625f 2018-10-22 stsp if (errcode)
4950 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4951 84451b3e 2018-07-10 stsp
4952 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4953 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4954 d68a0a7d 2018-07-10 stsp goto done;
4955 d68a0a7d 2018-07-10 stsp }
4956 d68a0a7d 2018-07-10 stsp
4957 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4958 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4959 d68a0a7d 2018-07-10 stsp
4960 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4961 d68a0a7d 2018-07-10 stsp if (line->annotated)
4962 d68a0a7d 2018-07-10 stsp goto done;
4963 d68a0a7d 2018-07-10 stsp
4964 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4965 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4966 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4967 84451b3e 2018-07-10 stsp goto done;
4968 84451b3e 2018-07-10 stsp }
4969 84451b3e 2018-07-10 stsp line->annotated = 1;
4970 84451b3e 2018-07-10 stsp done:
4971 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4972 1a76625f 2018-10-22 stsp if (errcode)
4973 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4974 84451b3e 2018-07-10 stsp return err;
4975 84451b3e 2018-07-10 stsp }
4976 84451b3e 2018-07-10 stsp
4977 84451b3e 2018-07-10 stsp static void *
4978 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4979 84451b3e 2018-07-10 stsp {
4980 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4981 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4982 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4983 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
4984 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
4985 00a8878e 2022-07-01 thomas
4986 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
4987 9117a7b7 2022-07-01 thomas if (fd1 == -1)
4988 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
4989 9117a7b7 2022-07-01 thomas
4990 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
4991 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
4992 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4993 9117a7b7 2022-07-01 thomas goto done;
4994 9117a7b7 2022-07-01 thomas }
4995 18430de3 2018-07-10 stsp
4996 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
4997 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
4998 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
4999 9117a7b7 2022-07-01 thomas goto done;
5000 9117a7b7 2022-07-01 thomas }
5001 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5002 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5003 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5004 9117a7b7 2022-07-01 thomas goto done;
5005 9117a7b7 2022-07-01 thomas }
5006 9117a7b7 2022-07-01 thomas
5007 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5008 61266923 2020-01-14 stsp if (err)
5009 9117a7b7 2022-07-01 thomas goto done;
5010 61266923 2020-01-14 stsp
5011 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5012 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5013 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5014 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5015 fc06ba56 2019-08-22 stsp err = NULL;
5016 18430de3 2018-07-10 stsp
5017 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5018 9117a7b7 2022-07-01 thomas if (errcode) {
5019 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5020 9117a7b7 2022-07-01 thomas goto done;
5021 9117a7b7 2022-07-01 thomas }
5022 18430de3 2018-07-10 stsp
5023 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5024 1d0f4054 2021-06-17 stsp if (err == NULL)
5025 1d0f4054 2021-06-17 stsp err = close_err;
5026 c9beca56 2018-07-22 stsp ta->repo = NULL;
5027 c9beca56 2018-07-22 stsp *ta->complete = 1;
5028 18430de3 2018-07-10 stsp
5029 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5030 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5031 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5032 18430de3 2018-07-10 stsp
5033 9117a7b7 2022-07-01 thomas done:
5034 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5035 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5036 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5037 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5038 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5039 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5040 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5041 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5042 00a8878e 2022-07-01 thomas
5043 18430de3 2018-07-10 stsp return (void *)err;
5044 84451b3e 2018-07-10 stsp }
5045 84451b3e 2018-07-10 stsp
5046 245d91c1 2018-07-12 stsp static struct got_object_id *
5047 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5048 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5049 245d91c1 2018-07-12 stsp {
5050 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5051 8d0fe45a 2019-08-12 stsp
5052 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5053 8d0fe45a 2019-08-12 stsp return NULL;
5054 b880a918 2018-07-10 stsp
5055 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5056 245d91c1 2018-07-12 stsp if (!line->annotated)
5057 245d91c1 2018-07-12 stsp return NULL;
5058 245d91c1 2018-07-12 stsp
5059 245d91c1 2018-07-12 stsp return line->id;
5060 b880a918 2018-07-10 stsp }
5061 245d91c1 2018-07-12 stsp
5062 b880a918 2018-07-10 stsp static const struct got_error *
5063 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5064 a70480e0 2018-06-23 stsp {
5065 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5066 245d91c1 2018-07-12 stsp int i;
5067 245d91c1 2018-07-12 stsp
5068 245d91c1 2018-07-12 stsp if (blame->thread) {
5069 1a76625f 2018-10-22 stsp int errcode;
5070 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5071 1a76625f 2018-10-22 stsp if (errcode)
5072 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5073 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5074 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5075 1a76625f 2018-10-22 stsp if (errcode)
5076 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5077 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5078 1a76625f 2018-10-22 stsp if (errcode)
5079 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5080 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5081 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5082 245d91c1 2018-07-12 stsp err = NULL;
5083 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5084 245d91c1 2018-07-12 stsp }
5085 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5086 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5087 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5088 1d0f4054 2021-06-17 stsp if (err == NULL)
5089 1d0f4054 2021-06-17 stsp err = close_err;
5090 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5091 245d91c1 2018-07-12 stsp }
5092 245d91c1 2018-07-12 stsp if (blame->f) {
5093 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5094 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5095 245d91c1 2018-07-12 stsp blame->f = NULL;
5096 245d91c1 2018-07-12 stsp }
5097 57670559 2018-12-23 stsp if (blame->lines) {
5098 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5099 57670559 2018-12-23 stsp free(blame->lines[i].id);
5100 57670559 2018-12-23 stsp free(blame->lines);
5101 57670559 2018-12-23 stsp blame->lines = NULL;
5102 57670559 2018-12-23 stsp }
5103 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5104 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5105 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5106 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5107 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5108 7cd52833 2022-06-23 thomas if (err == NULL)
5109 7cd52833 2022-06-23 thomas err = pack_err;
5110 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5111 7cd52833 2022-06-23 thomas }
5112 245d91c1 2018-07-12 stsp return err;
5113 245d91c1 2018-07-12 stsp }
5114 245d91c1 2018-07-12 stsp
5115 245d91c1 2018-07-12 stsp static const struct got_error *
5116 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5117 fc06ba56 2019-08-22 stsp {
5118 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5119 fc06ba56 2019-08-22 stsp int *done = arg;
5120 fc06ba56 2019-08-22 stsp int errcode;
5121 fc06ba56 2019-08-22 stsp
5122 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5123 fc06ba56 2019-08-22 stsp if (errcode)
5124 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5125 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5126 fc06ba56 2019-08-22 stsp
5127 fc06ba56 2019-08-22 stsp if (*done)
5128 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5129 fc06ba56 2019-08-22 stsp
5130 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5131 fc06ba56 2019-08-22 stsp if (errcode)
5132 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5133 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5134 fc06ba56 2019-08-22 stsp
5135 fc06ba56 2019-08-22 stsp return err;
5136 fc06ba56 2019-08-22 stsp }
5137 fc06ba56 2019-08-22 stsp
5138 fc06ba56 2019-08-22 stsp static const struct got_error *
5139 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5140 245d91c1 2018-07-12 stsp {
5141 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5142 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5143 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5144 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5145 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5146 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5147 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5148 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5149 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5150 a70480e0 2018-06-23 stsp
5151 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5152 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5153 27d434c2 2018-09-15 stsp if (err)
5154 15a94983 2018-12-23 stsp return err;
5155 f4ae6ddb 2022-07-01 thomas
5156 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5157 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5158 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5159 f4ae6ddb 2022-07-01 thomas goto done;
5160 f4ae6ddb 2022-07-01 thomas }
5161 945f9229 2022-04-16 thomas
5162 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5163 945f9229 2022-04-16 thomas if (err)
5164 945f9229 2022-04-16 thomas goto done;
5165 27d434c2 2018-09-15 stsp
5166 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5167 84451b3e 2018-07-10 stsp if (err)
5168 84451b3e 2018-07-10 stsp goto done;
5169 27d434c2 2018-09-15 stsp
5170 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5171 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5172 84451b3e 2018-07-10 stsp goto done;
5173 84451b3e 2018-07-10 stsp }
5174 a70480e0 2018-06-23 stsp
5175 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5176 a70480e0 2018-06-23 stsp if (err)
5177 a70480e0 2018-06-23 stsp goto done;
5178 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5179 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5180 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5181 84451b3e 2018-07-10 stsp goto done;
5182 84451b3e 2018-07-10 stsp }
5183 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5184 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5185 1fddf795 2021-01-20 stsp if (err)
5186 1fddf795 2021-01-20 stsp goto done;
5187 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5188 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5189 84451b3e 2018-07-10 stsp goto done;
5190 1fddf795 2021-01-20 stsp }
5191 b02560ec 2019-08-19 stsp
5192 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5193 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5194 b02560ec 2019-08-19 stsp blame->nlines--;
5195 a70480e0 2018-06-23 stsp
5196 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5197 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5198 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5199 84451b3e 2018-07-10 stsp goto done;
5200 84451b3e 2018-07-10 stsp }
5201 a70480e0 2018-06-23 stsp
5202 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5203 bd24772e 2018-07-11 stsp if (err)
5204 bd24772e 2018-07-11 stsp goto done;
5205 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5206 7cd52833 2022-06-23 thomas pack_fds);
5207 7cd52833 2022-06-23 thomas if (err)
5208 7cd52833 2022-06-23 thomas goto done;
5209 bd24772e 2018-07-11 stsp
5210 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5211 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5212 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5213 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5214 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5215 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5216 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5217 245d91c1 2018-07-12 stsp goto done;
5218 245d91c1 2018-07-12 stsp }
5219 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5220 245d91c1 2018-07-12 stsp
5221 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5222 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5223 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5224 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5225 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5226 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5227 a5388363 2020-12-01 naddy s->blame_complete = 0;
5228 f5a09613 2020-12-13 naddy
5229 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5230 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5231 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5232 f5a09613 2020-12-13 naddy s->selected_line = 1;
5233 f5a09613 2020-12-13 naddy }
5234 aa61903a 2021-12-31 thomas s->matched_line = 0;
5235 245d91c1 2018-07-12 stsp
5236 245d91c1 2018-07-12 stsp done:
5237 945f9229 2022-04-16 thomas if (commit)
5238 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5239 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5240 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5241 245d91c1 2018-07-12 stsp if (blob)
5242 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5243 27d434c2 2018-09-15 stsp free(obj_id);
5244 245d91c1 2018-07-12 stsp if (err)
5245 245d91c1 2018-07-12 stsp stop_blame(blame);
5246 245d91c1 2018-07-12 stsp return err;
5247 245d91c1 2018-07-12 stsp }
5248 245d91c1 2018-07-12 stsp
5249 245d91c1 2018-07-12 stsp static const struct got_error *
5250 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5251 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5252 245d91c1 2018-07-12 stsp {
5253 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5254 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5255 dbc6a6b6 2018-07-12 stsp
5256 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5257 245d91c1 2018-07-12 stsp
5258 c4843652 2019-08-12 stsp s->path = strdup(path);
5259 c4843652 2019-08-12 stsp if (s->path == NULL)
5260 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5261 c4843652 2019-08-12 stsp
5262 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5263 c4843652 2019-08-12 stsp if (err) {
5264 c4843652 2019-08-12 stsp free(s->path);
5265 7cbe629d 2018-08-04 stsp return err;
5266 c4843652 2019-08-12 stsp }
5267 245d91c1 2018-07-12 stsp
5268 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5269 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5270 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5271 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5272 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5273 fb2756b9 2018-08-04 stsp s->repo = repo;
5274 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5275 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5276 7cbe629d 2018-08-04 stsp
5277 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5278 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5279 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5280 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5281 11b20872 2019-11-08 stsp if (err)
5282 11b20872 2019-11-08 stsp return err;
5283 11b20872 2019-11-08 stsp }
5284 11b20872 2019-11-08 stsp
5285 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5286 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5287 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5288 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5289 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5290 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5291 e5a0f69f 2018-08-18 stsp
5292 a5388363 2020-12-01 naddy return run_blame(view);
5293 7cbe629d 2018-08-04 stsp }
5294 7cbe629d 2018-08-04 stsp
5295 e5a0f69f 2018-08-18 stsp static const struct got_error *
5296 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5297 7cbe629d 2018-08-04 stsp {
5298 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5299 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5300 7cbe629d 2018-08-04 stsp
5301 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5302 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5303 e5a0f69f 2018-08-18 stsp
5304 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5305 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5306 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5307 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5308 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5309 7cbe629d 2018-08-04 stsp }
5310 e5a0f69f 2018-08-18 stsp
5311 e5a0f69f 2018-08-18 stsp free(s->path);
5312 11b20872 2019-11-08 stsp free_colors(&s->colors);
5313 e5a0f69f 2018-08-18 stsp return err;
5314 7cbe629d 2018-08-04 stsp }
5315 7cbe629d 2018-08-04 stsp
5316 7cbe629d 2018-08-04 stsp static const struct got_error *
5317 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5318 6c4c42e0 2019-06-24 stsp {
5319 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5320 6c4c42e0 2019-06-24 stsp
5321 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5322 6c4c42e0 2019-06-24 stsp return NULL;
5323 6c4c42e0 2019-06-24 stsp }
5324 6c4c42e0 2019-06-24 stsp
5325 6c4c42e0 2019-06-24 stsp static const struct got_error *
5326 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5327 6c4c42e0 2019-06-24 stsp {
5328 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5329 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5330 6c4c42e0 2019-06-24 stsp int lineno;
5331 78eecffc 2022-06-23 thomas char *line = NULL;
5332 826082fe 2020-12-10 stsp size_t linesize = 0;
5333 826082fe 2020-12-10 stsp ssize_t linelen;
5334 6c4c42e0 2019-06-24 stsp
5335 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5336 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5337 6c4c42e0 2019-06-24 stsp return NULL;
5338 6c4c42e0 2019-06-24 stsp }
5339 6c4c42e0 2019-06-24 stsp
5340 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5341 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5342 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5343 6c4c42e0 2019-06-24 stsp else
5344 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5345 4b3f9dac 2021-12-17 thomas } else
5346 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5347 6c4c42e0 2019-06-24 stsp
5348 6c4c42e0 2019-06-24 stsp while (1) {
5349 6c4c42e0 2019-06-24 stsp off_t offset;
5350 6c4c42e0 2019-06-24 stsp
5351 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5352 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5353 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5354 6c4c42e0 2019-06-24 stsp break;
5355 6c4c42e0 2019-06-24 stsp }
5356 2246482e 2019-06-25 stsp
5357 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5358 6c4c42e0 2019-06-24 stsp lineno = 1;
5359 6c4c42e0 2019-06-24 stsp else
5360 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5361 6c4c42e0 2019-06-24 stsp }
5362 6c4c42e0 2019-06-24 stsp
5363 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5364 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5365 6c4c42e0 2019-06-24 stsp free(line);
5366 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5367 6c4c42e0 2019-06-24 stsp }
5368 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5369 78eecffc 2022-06-23 thomas if (linelen != -1) {
5370 78eecffc 2022-06-23 thomas char *exstr;
5371 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5372 78eecffc 2022-06-23 thomas if (err)
5373 78eecffc 2022-06-23 thomas break;
5374 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5375 78eecffc 2022-06-23 thomas &view->regmatch)) {
5376 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5377 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5378 78eecffc 2022-06-23 thomas free(exstr);
5379 78eecffc 2022-06-23 thomas break;
5380 78eecffc 2022-06-23 thomas }
5381 78eecffc 2022-06-23 thomas free(exstr);
5382 6c4c42e0 2019-06-24 stsp }
5383 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5384 6c4c42e0 2019-06-24 stsp lineno++;
5385 6c4c42e0 2019-06-24 stsp else
5386 6c4c42e0 2019-06-24 stsp lineno--;
5387 6c4c42e0 2019-06-24 stsp }
5388 826082fe 2020-12-10 stsp free(line);
5389 6c4c42e0 2019-06-24 stsp
5390 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5391 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5392 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5393 6c4c42e0 2019-06-24 stsp }
5394 6c4c42e0 2019-06-24 stsp
5395 05171be4 2022-06-23 thomas return err;
5396 6c4c42e0 2019-06-24 stsp }
5397 6c4c42e0 2019-06-24 stsp
5398 6c4c42e0 2019-06-24 stsp static const struct got_error *
5399 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5400 7cbe629d 2018-08-04 stsp {
5401 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5402 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5403 2b380cc8 2018-10-24 stsp int errcode;
5404 2b380cc8 2018-10-24 stsp
5405 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5406 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5407 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5408 2b380cc8 2018-10-24 stsp if (errcode)
5409 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5410 51fe7530 2019-08-19 stsp
5411 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5412 2b380cc8 2018-10-24 stsp }
5413 e5a0f69f 2018-08-18 stsp
5414 51fe7530 2019-08-19 stsp if (s->blame_complete)
5415 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5416 51fe7530 2019-08-19 stsp
5417 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5418 e5a0f69f 2018-08-18 stsp
5419 a5d43cac 2022-07-01 thomas view_border(view);
5420 e5a0f69f 2018-08-18 stsp return err;
5421 e5a0f69f 2018-08-18 stsp }
5422 e5a0f69f 2018-08-18 stsp
5423 e5a0f69f 2018-08-18 stsp static const struct got_error *
5424 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5425 e5a0f69f 2018-08-18 stsp {
5426 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5427 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5428 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5429 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5430 a5d43cac 2022-07-01 thomas
5431 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5432 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5433 a5d43cac 2022-07-01 thomas --eos; /* border */
5434 7cbe629d 2018-08-04 stsp
5435 e5a0f69f 2018-08-18 stsp switch (ch) {
5436 05171be4 2022-06-23 thomas case '0':
5437 05171be4 2022-06-23 thomas view->x = 0;
5438 05171be4 2022-06-23 thomas break;
5439 05171be4 2022-06-23 thomas case '$':
5440 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5441 07b0611c 2022-06-23 thomas view->count = 0;
5442 05171be4 2022-06-23 thomas break;
5443 05171be4 2022-06-23 thomas case KEY_RIGHT:
5444 05171be4 2022-06-23 thomas case 'l':
5445 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5446 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5447 07b0611c 2022-06-23 thomas else
5448 07b0611c 2022-06-23 thomas view->count = 0;
5449 05171be4 2022-06-23 thomas break;
5450 05171be4 2022-06-23 thomas case KEY_LEFT:
5451 05171be4 2022-06-23 thomas case 'h':
5452 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5453 07b0611c 2022-06-23 thomas if (view->x <= 0)
5454 07b0611c 2022-06-23 thomas view->count = 0;
5455 05171be4 2022-06-23 thomas break;
5456 1e37a5c2 2019-05-12 jcs case 'q':
5457 1e37a5c2 2019-05-12 jcs s->done = 1;
5458 4deef56f 2021-09-02 naddy break;
5459 4deef56f 2021-09-02 naddy case 'g':
5460 4deef56f 2021-09-02 naddy case KEY_HOME:
5461 4deef56f 2021-09-02 naddy s->selected_line = 1;
5462 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5463 07b0611c 2022-06-23 thomas view->count = 0;
5464 4deef56f 2021-09-02 naddy break;
5465 4deef56f 2021-09-02 naddy case 'G':
5466 4deef56f 2021-09-02 naddy case KEY_END:
5467 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5468 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5469 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5470 4deef56f 2021-09-02 naddy } else {
5471 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5472 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5473 4deef56f 2021-09-02 naddy }
5474 07b0611c 2022-06-23 thomas view->count = 0;
5475 1e37a5c2 2019-05-12 jcs break;
5476 1e37a5c2 2019-05-12 jcs case 'k':
5477 1e37a5c2 2019-05-12 jcs case KEY_UP:
5478 f7140bf5 2021-10-17 thomas case CTRL('p'):
5479 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5480 1e37a5c2 2019-05-12 jcs s->selected_line--;
5481 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5482 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5483 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5484 07b0611c 2022-06-23 thomas else
5485 07b0611c 2022-06-23 thomas view->count = 0;
5486 1e37a5c2 2019-05-12 jcs break;
5487 70f17a53 2022-06-13 thomas case CTRL('u'):
5488 23427b14 2022-06-23 thomas case 'u':
5489 70f17a53 2022-06-13 thomas nscroll /= 2;
5490 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5491 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5492 ea025d1d 2020-02-22 naddy case CTRL('b'):
5493 1c5e5faa 2022-06-23 thomas case 'b':
5494 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5495 07b0611c 2022-06-23 thomas if (view->count > 1)
5496 07b0611c 2022-06-23 thomas nscroll += nscroll;
5497 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5498 07b0611c 2022-06-23 thomas view->count = 0;
5499 e5a0f69f 2018-08-18 stsp break;
5500 1e37a5c2 2019-05-12 jcs }
5501 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5502 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5503 1e37a5c2 2019-05-12 jcs else
5504 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5505 1e37a5c2 2019-05-12 jcs break;
5506 1e37a5c2 2019-05-12 jcs case 'j':
5507 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5508 f7140bf5 2021-10-17 thomas case CTRL('n'):
5509 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5510 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5511 1e37a5c2 2019-05-12 jcs s->selected_line++;
5512 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5513 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5514 07b0611c 2022-06-23 thomas else
5515 07b0611c 2022-06-23 thomas view->count = 0;
5516 1e37a5c2 2019-05-12 jcs break;
5517 1c5e5faa 2022-06-23 thomas case 'c':
5518 1e37a5c2 2019-05-12 jcs case 'p': {
5519 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5520 07b0611c 2022-06-23 thomas
5521 07b0611c 2022-06-23 thomas view->count = 0;
5522 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5523 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5524 1e37a5c2 2019-05-12 jcs if (id == NULL)
5525 e5a0f69f 2018-08-18 stsp break;
5526 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5527 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5528 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5529 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5530 1e37a5c2 2019-05-12 jcs int obj_type;
5531 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5532 1e37a5c2 2019-05-12 jcs s->repo, id);
5533 e5a0f69f 2018-08-18 stsp if (err)
5534 e5a0f69f 2018-08-18 stsp break;
5535 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5536 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5537 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5538 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5539 e5a0f69f 2018-08-18 stsp break;
5540 e5a0f69f 2018-08-18 stsp }
5541 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5542 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5543 ec242592 2022-04-22 thomas s->repo, &pid->id);
5544 945f9229 2022-04-16 thomas if (err)
5545 945f9229 2022-04-16 thomas break;
5546 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5547 945f9229 2022-04-16 thomas pcommit, s->path);
5548 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5549 e5a0f69f 2018-08-18 stsp if (err) {
5550 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5551 1e37a5c2 2019-05-12 jcs err = NULL;
5552 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5553 e5a0f69f 2018-08-18 stsp break;
5554 e5a0f69f 2018-08-18 stsp }
5555 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5556 1e37a5c2 2019-05-12 jcs blob_id);
5557 1e37a5c2 2019-05-12 jcs free(blob_id);
5558 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5559 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5560 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5561 e5a0f69f 2018-08-18 stsp break;
5562 1e37a5c2 2019-05-12 jcs }
5563 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5564 ec242592 2022-04-22 thomas &pid->id);
5565 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5566 1e37a5c2 2019-05-12 jcs } else {
5567 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5568 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5569 1e37a5c2 2019-05-12 jcs break;
5570 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5571 1e37a5c2 2019-05-12 jcs id);
5572 1e37a5c2 2019-05-12 jcs }
5573 1e37a5c2 2019-05-12 jcs if (err)
5574 e5a0f69f 2018-08-18 stsp break;
5575 1e37a5c2 2019-05-12 jcs s->done = 1;
5576 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5577 1e37a5c2 2019-05-12 jcs s->done = 0;
5578 1e37a5c2 2019-05-12 jcs if (thread_err)
5579 1e37a5c2 2019-05-12 jcs break;
5580 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5581 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5582 a5388363 2020-12-01 naddy err = run_blame(view);
5583 1e37a5c2 2019-05-12 jcs if (err)
5584 1e37a5c2 2019-05-12 jcs break;
5585 1e37a5c2 2019-05-12 jcs break;
5586 1e37a5c2 2019-05-12 jcs }
5587 1c5e5faa 2022-06-23 thomas case 'C': {
5588 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5589 07b0611c 2022-06-23 thomas
5590 07b0611c 2022-06-23 thomas view->count = 0;
5591 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5592 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5593 1e37a5c2 2019-05-12 jcs break;
5594 1e37a5c2 2019-05-12 jcs s->done = 1;
5595 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5596 1e37a5c2 2019-05-12 jcs s->done = 0;
5597 1e37a5c2 2019-05-12 jcs if (thread_err)
5598 1e37a5c2 2019-05-12 jcs break;
5599 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5600 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5601 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5602 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5603 a5388363 2020-12-01 naddy err = run_blame(view);
5604 1e37a5c2 2019-05-12 jcs if (err)
5605 1e37a5c2 2019-05-12 jcs break;
5606 1e37a5c2 2019-05-12 jcs break;
5607 1e37a5c2 2019-05-12 jcs }
5608 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5609 1e37a5c2 2019-05-12 jcs case '\r': {
5610 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5611 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5612 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5613 07b0611c 2022-06-23 thomas
5614 07b0611c 2022-06-23 thomas view->count = 0;
5615 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5616 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5617 1e37a5c2 2019-05-12 jcs if (id == NULL)
5618 1e37a5c2 2019-05-12 jcs break;
5619 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5620 1e37a5c2 2019-05-12 jcs if (err)
5621 1e37a5c2 2019-05-12 jcs break;
5622 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5623 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5624 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
5625 a5d43cac 2022-07-01 thomas
5626 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5627 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5628 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5629 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5630 1e37a5c2 2019-05-12 jcs break;
5631 15a94983 2018-12-23 stsp }
5632 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5633 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5634 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5635 1e37a5c2 2019-05-12 jcs if (err) {
5636 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5637 1e37a5c2 2019-05-12 jcs break;
5638 1e37a5c2 2019-05-12 jcs }
5639 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
5640 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5641 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
5642 a5d43cac 2022-07-01 thomas if (err)
5643 a5d43cac 2022-07-01 thomas break;
5644 a5d43cac 2022-07-01 thomas }
5645 a5d43cac 2022-07-01 thomas
5646 e78dc838 2020-12-04 stsp view->focussed = 0;
5647 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5648 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
5649 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
5650 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5651 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5652 1e37a5c2 2019-05-12 jcs if (err)
5653 34bc9ec9 2019-02-22 stsp break;
5654 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
5655 40236d76 2022-06-23 thomas if (err)
5656 40236d76 2022-06-23 thomas break;
5657 e78dc838 2020-12-04 stsp view->focus_child = 1;
5658 1e37a5c2 2019-05-12 jcs } else
5659 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5660 1e37a5c2 2019-05-12 jcs if (err)
5661 e5a0f69f 2018-08-18 stsp break;
5662 1e37a5c2 2019-05-12 jcs break;
5663 1e37a5c2 2019-05-12 jcs }
5664 70f17a53 2022-06-13 thomas case CTRL('d'):
5665 23427b14 2022-06-23 thomas case 'd':
5666 70f17a53 2022-06-13 thomas nscroll /= 2;
5667 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5668 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5669 ea025d1d 2020-02-22 naddy case CTRL('f'):
5670 1c5e5faa 2022-06-23 thomas case 'f':
5671 1e37a5c2 2019-05-12 jcs case ' ':
5672 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5673 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5674 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5675 07b0611c 2022-06-23 thomas view->count = 0;
5676 e5a0f69f 2018-08-18 stsp break;
5677 1e37a5c2 2019-05-12 jcs }
5678 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5679 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5680 70f17a53 2022-06-13 thomas s->selected_line +=
5681 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5682 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5683 1e37a5c2 2019-05-12 jcs }
5684 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5685 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5686 1e37a5c2 2019-05-12 jcs else
5687 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5688 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5689 1e37a5c2 2019-05-12 jcs break;
5690 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5691 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5692 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5693 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5694 1e37a5c2 2019-05-12 jcs }
5695 1e37a5c2 2019-05-12 jcs break;
5696 1e37a5c2 2019-05-12 jcs default:
5697 07b0611c 2022-06-23 thomas view->count = 0;
5698 1e37a5c2 2019-05-12 jcs break;
5699 a70480e0 2018-06-23 stsp }
5700 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5701 adf4c9e0 2022-07-03 thomas }
5702 adf4c9e0 2022-07-03 thomas
5703 adf4c9e0 2022-07-03 thomas static const struct got_error *
5704 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
5705 adf4c9e0 2022-07-03 thomas {
5706 adf4c9e0 2022-07-03 thomas const struct got_error *err;
5707 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
5708 adf4c9e0 2022-07-03 thomas
5709 adf4c9e0 2022-07-03 thomas view->count = 0;
5710 adf4c9e0 2022-07-03 thomas s->done = 1;
5711 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
5712 adf4c9e0 2022-07-03 thomas s->done = 0;
5713 adf4c9e0 2022-07-03 thomas if (err)
5714 adf4c9e0 2022-07-03 thomas return err;
5715 adf4c9e0 2022-07-03 thomas return run_blame(view);
5716 a70480e0 2018-06-23 stsp }
5717 a70480e0 2018-06-23 stsp
5718 a70480e0 2018-06-23 stsp static const struct got_error *
5719 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5720 9f7d7167 2018-04-29 stsp {
5721 a70480e0 2018-06-23 stsp const struct got_error *error;
5722 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5723 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5724 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5725 0587e10c 2020-07-23 stsp char *link_target = NULL;
5726 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5727 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5728 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5729 a70480e0 2018-06-23 stsp int ch;
5730 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5731 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5732 a70480e0 2018-06-23 stsp
5733 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5734 a70480e0 2018-06-23 stsp switch (ch) {
5735 a70480e0 2018-06-23 stsp case 'c':
5736 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5737 a70480e0 2018-06-23 stsp break;
5738 69069811 2018-08-02 stsp case 'r':
5739 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5740 69069811 2018-08-02 stsp if (repo_path == NULL)
5741 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5742 9ba1d308 2019-10-21 stsp optarg);
5743 69069811 2018-08-02 stsp break;
5744 a70480e0 2018-06-23 stsp default:
5745 17020d27 2019-03-07 stsp usage_blame();
5746 a70480e0 2018-06-23 stsp /* NOTREACHED */
5747 a70480e0 2018-06-23 stsp }
5748 a70480e0 2018-06-23 stsp }
5749 a70480e0 2018-06-23 stsp
5750 a70480e0 2018-06-23 stsp argc -= optind;
5751 a70480e0 2018-06-23 stsp argv += optind;
5752 a70480e0 2018-06-23 stsp
5753 f135c941 2020-02-20 stsp if (argc != 1)
5754 a70480e0 2018-06-23 stsp usage_blame();
5755 6962eb72 2020-02-20 stsp
5756 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5757 7cd52833 2022-06-23 thomas if (error != NULL)
5758 7cd52833 2022-06-23 thomas goto done;
5759 7cd52833 2022-06-23 thomas
5760 69069811 2018-08-02 stsp if (repo_path == NULL) {
5761 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5762 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5763 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5764 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5765 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5766 c156c7a4 2020-12-18 stsp goto done;
5767 f135c941 2020-02-20 stsp if (worktree)
5768 eb41ed75 2019-02-05 stsp repo_path =
5769 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5770 f135c941 2020-02-20 stsp else
5771 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5772 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5773 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5774 c156c7a4 2020-12-18 stsp goto done;
5775 c156c7a4 2020-12-18 stsp }
5776 f135c941 2020-02-20 stsp }
5777 a915003a 2019-02-05 stsp
5778 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5779 c02c541e 2019-03-29 stsp if (error != NULL)
5780 8e94dd5b 2019-01-04 stsp goto done;
5781 69069811 2018-08-02 stsp
5782 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5783 f135c941 2020-02-20 stsp worktree);
5784 c02c541e 2019-03-29 stsp if (error)
5785 92205607 2019-01-04 stsp goto done;
5786 69069811 2018-08-02 stsp
5787 f135c941 2020-02-20 stsp init_curses();
5788 f135c941 2020-02-20 stsp
5789 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5790 51a10b52 2020-12-26 stsp if (error)
5791 51a10b52 2020-12-26 stsp goto done;
5792 51a10b52 2020-12-26 stsp
5793 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5794 eb41ed75 2019-02-05 stsp if (error)
5795 69069811 2018-08-02 stsp goto done;
5796 a70480e0 2018-06-23 stsp
5797 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5798 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5799 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5800 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5801 a70480e0 2018-06-23 stsp if (error != NULL)
5802 66b4983c 2018-06-23 stsp goto done;
5803 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5804 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5805 a70480e0 2018-06-23 stsp } else {
5806 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5807 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5808 a70480e0 2018-06-23 stsp }
5809 a19e88aa 2018-06-23 stsp if (error != NULL)
5810 8b473291 2019-02-21 stsp goto done;
5811 8b473291 2019-02-21 stsp
5812 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5813 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5814 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5815 e1cd8fed 2018-08-01 stsp goto done;
5816 e1cd8fed 2018-08-01 stsp }
5817 0587e10c 2020-07-23 stsp
5818 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5819 945f9229 2022-04-16 thomas if (error)
5820 945f9229 2022-04-16 thomas goto done;
5821 945f9229 2022-04-16 thomas
5822 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5823 945f9229 2022-04-16 thomas commit, repo);
5824 7cbe629d 2018-08-04 stsp if (error)
5825 7cbe629d 2018-08-04 stsp goto done;
5826 0587e10c 2020-07-23 stsp
5827 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5828 78756c87 2020-11-24 stsp commit_id, repo);
5829 0587e10c 2020-07-23 stsp if (error)
5830 0587e10c 2020-07-23 stsp goto done;
5831 12314ad4 2019-08-31 stsp if (worktree) {
5832 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5833 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5834 12314ad4 2019-08-31 stsp worktree = NULL;
5835 12314ad4 2019-08-31 stsp }
5836 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5837 a70480e0 2018-06-23 stsp done:
5838 69069811 2018-08-02 stsp free(repo_path);
5839 f135c941 2020-02-20 stsp free(in_repo_path);
5840 0587e10c 2020-07-23 stsp free(link_target);
5841 69069811 2018-08-02 stsp free(cwd);
5842 a70480e0 2018-06-23 stsp free(commit_id);
5843 945f9229 2022-04-16 thomas if (commit)
5844 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5845 eb41ed75 2019-02-05 stsp if (worktree)
5846 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5847 1d0f4054 2021-06-17 stsp if (repo) {
5848 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5849 1d0f4054 2021-06-17 stsp if (error == NULL)
5850 1d0f4054 2021-06-17 stsp error = close_err;
5851 1d0f4054 2021-06-17 stsp }
5852 7cd52833 2022-06-23 thomas if (pack_fds) {
5853 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5854 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5855 7cd52833 2022-06-23 thomas if (error == NULL)
5856 7cd52833 2022-06-23 thomas error = pack_err;
5857 7cd52833 2022-06-23 thomas }
5858 51a10b52 2020-12-26 stsp tog_free_refs();
5859 a70480e0 2018-06-23 stsp return error;
5860 ffd1d5e5 2018-06-23 stsp }
5861 ffd1d5e5 2018-06-23 stsp
5862 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5863 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5864 ffd1d5e5 2018-06-23 stsp {
5865 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5866 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5867 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5868 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5869 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5870 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5871 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5872 ffd1d5e5 2018-06-23 stsp
5873 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5874 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5875 a5d43cac 2022-07-01 thomas --limit; /* border */
5876 ffd1d5e5 2018-06-23 stsp
5877 f7d12f7e 2018-08-01 stsp werase(view->window);
5878 ffd1d5e5 2018-06-23 stsp
5879 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5880 ffd1d5e5 2018-06-23 stsp return NULL;
5881 ffd1d5e5 2018-06-23 stsp
5882 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5883 f91a2b48 2022-06-23 thomas 0, 0);
5884 ffd1d5e5 2018-06-23 stsp if (err)
5885 ffd1d5e5 2018-06-23 stsp return err;
5886 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5887 a3404814 2018-09-02 stsp wstandout(view->window);
5888 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5889 11b20872 2019-11-08 stsp if (tc)
5890 11b20872 2019-11-08 stsp wattr_on(view->window,
5891 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5892 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5893 11b20872 2019-11-08 stsp if (tc)
5894 11b20872 2019-11-08 stsp wattr_off(view->window,
5895 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5896 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5897 a3404814 2018-09-02 stsp wstandend(view->window);
5898 2550e4c3 2018-07-13 stsp free(wline);
5899 2550e4c3 2018-07-13 stsp wline = NULL;
5900 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5901 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5902 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5903 ffd1d5e5 2018-06-23 stsp return NULL;
5904 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5905 f91a2b48 2022-06-23 thomas 0, 0);
5906 ce52c690 2018-06-23 stsp if (err)
5907 ce52c690 2018-06-23 stsp return err;
5908 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5909 2550e4c3 2018-07-13 stsp free(wline);
5910 2550e4c3 2018-07-13 stsp wline = NULL;
5911 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5912 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5913 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5914 ffd1d5e5 2018-06-23 stsp return NULL;
5915 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5916 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5917 a1eca9bb 2018-06-23 stsp return NULL;
5918 ffd1d5e5 2018-06-23 stsp
5919 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5920 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5921 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5922 0cf4efb1 2018-09-29 stsp if (view->focussed)
5923 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5924 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5925 ffd1d5e5 2018-06-23 stsp }
5926 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5927 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5928 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5929 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5930 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5931 ffd1d5e5 2018-06-23 stsp return NULL;
5932 ffd1d5e5 2018-06-23 stsp n = 1;
5933 ffd1d5e5 2018-06-23 stsp } else {
5934 ffd1d5e5 2018-06-23 stsp n = 0;
5935 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5936 ffd1d5e5 2018-06-23 stsp }
5937 ffd1d5e5 2018-06-23 stsp
5938 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5939 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5940 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5941 848d6979 2019-08-12 stsp const char *modestr = "";
5942 56e0773d 2019-11-28 stsp mode_t mode;
5943 1d13200f 2018-07-12 stsp
5944 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5945 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5946 56e0773d 2019-11-28 stsp
5947 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5948 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5949 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5950 1d13200f 2018-07-12 stsp if (err)
5951 638f9024 2019-05-13 stsp return got_error_from_errno(
5952 230a42bd 2019-05-11 jcs "got_object_id_str");
5953 1d13200f 2018-07-12 stsp }
5954 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5955 63c5ca5d 2019-08-24 stsp modestr = "$";
5956 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5957 0d6c6ee3 2020-05-20 stsp int i;
5958 0d6c6ee3 2020-05-20 stsp
5959 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5960 d86d3b18 2020-12-01 naddy te, s->repo);
5961 0d6c6ee3 2020-05-20 stsp if (err) {
5962 0d6c6ee3 2020-05-20 stsp free(id_str);
5963 0d6c6ee3 2020-05-20 stsp return err;
5964 0d6c6ee3 2020-05-20 stsp }
5965 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5966 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5967 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5968 0d6c6ee3 2020-05-20 stsp }
5969 848d6979 2019-08-12 stsp modestr = "@";
5970 0d6c6ee3 2020-05-20 stsp }
5971 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5972 848d6979 2019-08-12 stsp modestr = "/";
5973 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5974 848d6979 2019-08-12 stsp modestr = "*";
5975 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5976 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5977 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5978 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5979 1d13200f 2018-07-12 stsp free(id_str);
5980 0d6c6ee3 2020-05-20 stsp free(link_target);
5981 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5982 1d13200f 2018-07-12 stsp }
5983 1d13200f 2018-07-12 stsp free(id_str);
5984 0d6c6ee3 2020-05-20 stsp free(link_target);
5985 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5986 f91a2b48 2022-06-23 thomas 0, 0);
5987 ffd1d5e5 2018-06-23 stsp if (err) {
5988 ffd1d5e5 2018-06-23 stsp free(line);
5989 ffd1d5e5 2018-06-23 stsp break;
5990 ffd1d5e5 2018-06-23 stsp }
5991 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5992 0cf4efb1 2018-09-29 stsp if (view->focussed)
5993 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5994 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5995 ffd1d5e5 2018-06-23 stsp }
5996 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5997 f26dddb7 2019-11-08 stsp if (tc)
5998 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5999 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6000 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6001 f26dddb7 2019-11-08 stsp if (tc)
6002 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6003 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6004 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6005 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6006 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6007 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6008 ffd1d5e5 2018-06-23 stsp free(line);
6009 2550e4c3 2018-07-13 stsp free(wline);
6010 2550e4c3 2018-07-13 stsp wline = NULL;
6011 ffd1d5e5 2018-06-23 stsp n++;
6012 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6013 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6014 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6015 ffd1d5e5 2018-06-23 stsp break;
6016 ffd1d5e5 2018-06-23 stsp }
6017 ffd1d5e5 2018-06-23 stsp
6018 ffd1d5e5 2018-06-23 stsp return err;
6019 ffd1d5e5 2018-06-23 stsp }
6020 ffd1d5e5 2018-06-23 stsp
6021 ffd1d5e5 2018-06-23 stsp static void
6022 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6023 ffd1d5e5 2018-06-23 stsp {
6024 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6025 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6026 fa86c4bf 2020-11-29 stsp int i = 0;
6027 ffd1d5e5 2018-06-23 stsp
6028 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6029 ffd1d5e5 2018-06-23 stsp return;
6030 ffd1d5e5 2018-06-23 stsp
6031 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6032 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6033 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6034 fa86c4bf 2020-11-29 stsp if (!isroot)
6035 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6036 fa86c4bf 2020-11-29 stsp break;
6037 fa86c4bf 2020-11-29 stsp }
6038 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6039 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6040 ffd1d5e5 2018-06-23 stsp }
6041 ffd1d5e5 2018-06-23 stsp }
6042 ffd1d5e5 2018-06-23 stsp
6043 a5d43cac 2022-07-01 thomas static const struct got_error *
6044 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6045 ffd1d5e5 2018-06-23 stsp {
6046 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6047 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6048 ffd1d5e5 2018-06-23 stsp int n = 0;
6049 ffd1d5e5 2018-06-23 stsp
6050 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6051 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6052 694d3271 2020-12-01 naddy s->first_displayed_entry);
6053 694d3271 2020-12-01 naddy else
6054 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6055 56e0773d 2019-11-28 stsp
6056 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6057 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6058 a5d43cac 2022-07-01 thomas if (last)
6059 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6060 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6061 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6062 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6063 768394f3 2019-01-24 stsp }
6064 ffd1d5e5 2018-06-23 stsp }
6065 a5d43cac 2022-07-01 thomas
6066 a5d43cac 2022-07-01 thomas return NULL;
6067 ffd1d5e5 2018-06-23 stsp }
6068 ffd1d5e5 2018-06-23 stsp
6069 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6070 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6071 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6072 ffd1d5e5 2018-06-23 stsp {
6073 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6074 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6075 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6076 ffd1d5e5 2018-06-23 stsp
6077 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6078 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6079 56e0773d 2019-11-28 stsp + 1 /* slash */;
6080 ce52c690 2018-06-23 stsp if (te)
6081 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6082 ce52c690 2018-06-23 stsp
6083 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6084 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6085 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6086 ffd1d5e5 2018-06-23 stsp
6087 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6088 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6089 d9765a41 2018-06-23 stsp while (pt) {
6090 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6091 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6092 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6093 cb2ebc8a 2018-06-23 stsp goto done;
6094 cb2ebc8a 2018-06-23 stsp }
6095 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6096 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6097 cb2ebc8a 2018-06-23 stsp goto done;
6098 cb2ebc8a 2018-06-23 stsp }
6099 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6100 ffd1d5e5 2018-06-23 stsp }
6101 ce52c690 2018-06-23 stsp if (te) {
6102 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6103 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6104 ce52c690 2018-06-23 stsp goto done;
6105 ce52c690 2018-06-23 stsp }
6106 cb2ebc8a 2018-06-23 stsp }
6107 ce52c690 2018-06-23 stsp done:
6108 ce52c690 2018-06-23 stsp if (err) {
6109 ce52c690 2018-06-23 stsp free(*path);
6110 ce52c690 2018-06-23 stsp *path = NULL;
6111 ce52c690 2018-06-23 stsp }
6112 ce52c690 2018-06-23 stsp return err;
6113 ce52c690 2018-06-23 stsp }
6114 ce52c690 2018-06-23 stsp
6115 ce52c690 2018-06-23 stsp static const struct got_error *
6116 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6117 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6118 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6119 ce52c690 2018-06-23 stsp {
6120 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6121 ce52c690 2018-06-23 stsp char *path;
6122 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6123 a0de39f3 2019-08-09 stsp
6124 a0de39f3 2019-08-09 stsp *new_view = NULL;
6125 69efd4c4 2018-07-18 stsp
6126 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6127 ce52c690 2018-06-23 stsp if (err)
6128 ce52c690 2018-06-23 stsp return err;
6129 ffd1d5e5 2018-06-23 stsp
6130 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6131 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6132 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6133 83ce39e3 2019-08-12 stsp goto done;
6134 83ce39e3 2019-08-12 stsp }
6135 cdf1ee82 2018-08-01 stsp
6136 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6137 e5a0f69f 2018-08-18 stsp if (err) {
6138 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6139 fc06ba56 2019-08-22 stsp err = NULL;
6140 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6141 e5a0f69f 2018-08-18 stsp } else
6142 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6143 83ce39e3 2019-08-12 stsp done:
6144 83ce39e3 2019-08-12 stsp free(path);
6145 69efd4c4 2018-07-18 stsp return err;
6146 69efd4c4 2018-07-18 stsp }
6147 69efd4c4 2018-07-18 stsp
6148 69efd4c4 2018-07-18 stsp static const struct got_error *
6149 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6150 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6151 69efd4c4 2018-07-18 stsp {
6152 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6153 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6154 69efd4c4 2018-07-18 stsp char *path;
6155 a0de39f3 2019-08-09 stsp
6156 a0de39f3 2019-08-09 stsp *new_view = NULL;
6157 69efd4c4 2018-07-18 stsp
6158 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6159 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6160 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6161 e5a0f69f 2018-08-18 stsp
6162 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6163 69efd4c4 2018-07-18 stsp if (err)
6164 69efd4c4 2018-07-18 stsp return err;
6165 69efd4c4 2018-07-18 stsp
6166 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6167 4e97c21c 2020-12-06 stsp path, 0);
6168 ba4f502b 2018-08-04 stsp if (err)
6169 e5a0f69f 2018-08-18 stsp view_close(log_view);
6170 e5a0f69f 2018-08-18 stsp else
6171 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6172 cb2ebc8a 2018-06-23 stsp free(path);
6173 cb2ebc8a 2018-06-23 stsp return err;
6174 ffd1d5e5 2018-06-23 stsp }
6175 ffd1d5e5 2018-06-23 stsp
6176 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6177 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6178 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6179 ffd1d5e5 2018-06-23 stsp {
6180 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6181 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6182 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6183 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6184 ffd1d5e5 2018-06-23 stsp
6185 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6186 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6187 bc573f3b 2021-07-10 stsp
6188 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6189 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6190 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6191 ffd1d5e5 2018-06-23 stsp
6192 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6193 bc573f3b 2021-07-10 stsp if (err)
6194 bc573f3b 2021-07-10 stsp goto done;
6195 bc573f3b 2021-07-10 stsp
6196 bc573f3b 2021-07-10 stsp /*
6197 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6198 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6199 bc573f3b 2021-07-10 stsp * closed on demand.
6200 bc573f3b 2021-07-10 stsp */
6201 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6202 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6203 bc573f3b 2021-07-10 stsp if (err)
6204 bc573f3b 2021-07-10 stsp goto done;
6205 bc573f3b 2021-07-10 stsp s->tree = s->root;
6206 bc573f3b 2021-07-10 stsp
6207 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6208 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6209 ffd1d5e5 2018-06-23 stsp goto done;
6210 ffd1d5e5 2018-06-23 stsp
6211 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6212 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6213 ffd1d5e5 2018-06-23 stsp goto done;
6214 ffd1d5e5 2018-06-23 stsp }
6215 ffd1d5e5 2018-06-23 stsp
6216 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6217 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6218 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6219 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6220 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6221 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6222 9cd7cbd1 2020-12-07 stsp goto done;
6223 9cd7cbd1 2020-12-07 stsp }
6224 9cd7cbd1 2020-12-07 stsp }
6225 fb2756b9 2018-08-04 stsp s->repo = repo;
6226 c0b01bdb 2019-11-08 stsp
6227 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6228 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6229 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6230 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6231 c0b01bdb 2019-11-08 stsp if (err)
6232 c0b01bdb 2019-11-08 stsp goto done;
6233 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6234 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6235 bc573f3b 2021-07-10 stsp if (err)
6236 c0b01bdb 2019-11-08 stsp goto done;
6237 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6238 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6239 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6240 bc573f3b 2021-07-10 stsp if (err)
6241 c0b01bdb 2019-11-08 stsp goto done;
6242 e5a0f69f 2018-08-18 stsp
6243 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6244 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6245 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6246 bc573f3b 2021-07-10 stsp if (err)
6247 11b20872 2019-11-08 stsp goto done;
6248 11b20872 2019-11-08 stsp
6249 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6250 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6251 bc573f3b 2021-07-10 stsp if (err)
6252 c0b01bdb 2019-11-08 stsp goto done;
6253 c0b01bdb 2019-11-08 stsp }
6254 c0b01bdb 2019-11-08 stsp
6255 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6256 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6257 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6258 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6259 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6260 ad80ab7b 2018-08-04 stsp done:
6261 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6262 bc573f3b 2021-07-10 stsp if (commit)
6263 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6264 bc573f3b 2021-07-10 stsp if (err)
6265 bc573f3b 2021-07-10 stsp close_tree_view(view);
6266 ad80ab7b 2018-08-04 stsp return err;
6267 ad80ab7b 2018-08-04 stsp }
6268 ad80ab7b 2018-08-04 stsp
6269 e5a0f69f 2018-08-18 stsp static const struct got_error *
6270 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6271 ad80ab7b 2018-08-04 stsp {
6272 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6273 ad80ab7b 2018-08-04 stsp
6274 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6275 fb2756b9 2018-08-04 stsp free(s->tree_label);
6276 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6277 6484ec90 2018-09-29 stsp free(s->commit_id);
6278 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6279 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6280 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6281 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6282 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6283 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6284 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6285 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6286 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6287 ad80ab7b 2018-08-04 stsp free(parent);
6288 ad80ab7b 2018-08-04 stsp
6289 ad80ab7b 2018-08-04 stsp }
6290 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6291 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6292 bc573f3b 2021-07-10 stsp if (s->root)
6293 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6294 7c32bd05 2019-06-22 stsp return NULL;
6295 7c32bd05 2019-06-22 stsp }
6296 7c32bd05 2019-06-22 stsp
6297 7c32bd05 2019-06-22 stsp static const struct got_error *
6298 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6299 7c32bd05 2019-06-22 stsp {
6300 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6301 7c32bd05 2019-06-22 stsp
6302 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6303 7c32bd05 2019-06-22 stsp return NULL;
6304 7c32bd05 2019-06-22 stsp }
6305 7c32bd05 2019-06-22 stsp
6306 7c32bd05 2019-06-22 stsp static int
6307 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6308 7c32bd05 2019-06-22 stsp {
6309 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6310 7c32bd05 2019-06-22 stsp
6311 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6312 56e0773d 2019-11-28 stsp 0) == 0;
6313 7c32bd05 2019-06-22 stsp }
6314 7c32bd05 2019-06-22 stsp
6315 7c32bd05 2019-06-22 stsp static const struct got_error *
6316 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6317 7c32bd05 2019-06-22 stsp {
6318 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6319 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6320 7c32bd05 2019-06-22 stsp
6321 7c32bd05 2019-06-22 stsp if (!view->searching) {
6322 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6323 7c32bd05 2019-06-22 stsp return NULL;
6324 7c32bd05 2019-06-22 stsp }
6325 7c32bd05 2019-06-22 stsp
6326 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6327 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6328 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6329 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6330 56e0773d 2019-11-28 stsp s->selected_entry);
6331 7c32bd05 2019-06-22 stsp else
6332 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6333 56e0773d 2019-11-28 stsp } else {
6334 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6335 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6336 56e0773d 2019-11-28 stsp else
6337 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6338 56e0773d 2019-11-28 stsp s->selected_entry);
6339 7c32bd05 2019-06-22 stsp }
6340 7c32bd05 2019-06-22 stsp } else {
6341 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6342 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6343 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6344 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6345 56e0773d 2019-11-28 stsp else
6346 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6347 7c32bd05 2019-06-22 stsp }
6348 7c32bd05 2019-06-22 stsp
6349 7c32bd05 2019-06-22 stsp while (1) {
6350 56e0773d 2019-11-28 stsp if (te == NULL) {
6351 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6352 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6353 ac66afb8 2019-06-24 stsp return NULL;
6354 ac66afb8 2019-06-24 stsp }
6355 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6356 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6357 56e0773d 2019-11-28 stsp else
6358 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6359 7c32bd05 2019-06-22 stsp }
6360 7c32bd05 2019-06-22 stsp
6361 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6362 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6363 56e0773d 2019-11-28 stsp s->matched_entry = te;
6364 7c32bd05 2019-06-22 stsp break;
6365 7c32bd05 2019-06-22 stsp }
6366 7c32bd05 2019-06-22 stsp
6367 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6368 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6369 56e0773d 2019-11-28 stsp else
6370 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6371 7c32bd05 2019-06-22 stsp }
6372 e5a0f69f 2018-08-18 stsp
6373 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6374 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6375 7c32bd05 2019-06-22 stsp s->selected = 0;
6376 7c32bd05 2019-06-22 stsp }
6377 7c32bd05 2019-06-22 stsp
6378 e5a0f69f 2018-08-18 stsp return NULL;
6379 ad80ab7b 2018-08-04 stsp }
6380 ad80ab7b 2018-08-04 stsp
6381 ad80ab7b 2018-08-04 stsp static const struct got_error *
6382 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6383 ad80ab7b 2018-08-04 stsp {
6384 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6385 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6386 e5a0f69f 2018-08-18 stsp char *parent_path;
6387 ad80ab7b 2018-08-04 stsp
6388 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6389 e5a0f69f 2018-08-18 stsp if (err)
6390 e5a0f69f 2018-08-18 stsp return err;
6391 ffd1d5e5 2018-06-23 stsp
6392 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6393 e5a0f69f 2018-08-18 stsp free(parent_path);
6394 669b5ffa 2018-10-07 stsp
6395 a5d43cac 2022-07-01 thomas view_border(view);
6396 e5a0f69f 2018-08-18 stsp return err;
6397 e5a0f69f 2018-08-18 stsp }
6398 ce52c690 2018-06-23 stsp
6399 e5a0f69f 2018-08-18 stsp static const struct got_error *
6400 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6401 e5a0f69f 2018-08-18 stsp {
6402 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6403 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6404 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6405 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6406 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6407 ffd1d5e5 2018-06-23 stsp
6408 e5a0f69f 2018-08-18 stsp switch (ch) {
6409 1e37a5c2 2019-05-12 jcs case 'i':
6410 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6411 07b0611c 2022-06-23 thomas view->count = 0;
6412 1e37a5c2 2019-05-12 jcs break;
6413 1e37a5c2 2019-05-12 jcs case 'l':
6414 07b0611c 2022-06-23 thomas view->count = 0;
6415 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6416 ffd1d5e5 2018-06-23 stsp break;
6417 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6418 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6419 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6420 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6421 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6422 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6423 444d5325 2022-07-03 thomas if (err)
6424 444d5325 2022-07-03 thomas break;
6425 444d5325 2022-07-03 thomas }
6426 e78dc838 2020-12-04 stsp view->focussed = 0;
6427 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6428 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6429 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6430 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6431 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6432 1e37a5c2 2019-05-12 jcs if (err)
6433 1e37a5c2 2019-05-12 jcs return err;
6434 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6435 40236d76 2022-06-23 thomas if (err)
6436 40236d76 2022-06-23 thomas return err;
6437 e78dc838 2020-12-04 stsp view->focus_child = 1;
6438 1e37a5c2 2019-05-12 jcs } else
6439 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6440 152c1c93 2020-11-29 stsp break;
6441 152c1c93 2020-11-29 stsp case 'r':
6442 07b0611c 2022-06-23 thomas view->count = 0;
6443 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6444 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6445 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6446 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6447 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6448 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6449 152c1c93 2020-11-29 stsp if (err) {
6450 152c1c93 2020-11-29 stsp view_close(ref_view);
6451 152c1c93 2020-11-29 stsp return err;
6452 152c1c93 2020-11-29 stsp }
6453 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6454 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6455 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6456 444d5325 2022-07-03 thomas if (err)
6457 444d5325 2022-07-03 thomas break;
6458 444d5325 2022-07-03 thomas }
6459 e78dc838 2020-12-04 stsp view->focussed = 0;
6460 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6461 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6462 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6463 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6464 152c1c93 2020-11-29 stsp err = view_close_child(view);
6465 152c1c93 2020-11-29 stsp if (err)
6466 152c1c93 2020-11-29 stsp return err;
6467 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6468 40236d76 2022-06-23 thomas if (err)
6469 40236d76 2022-06-23 thomas return err;
6470 e78dc838 2020-12-04 stsp view->focus_child = 1;
6471 152c1c93 2020-11-29 stsp } else
6472 152c1c93 2020-11-29 stsp *new_view = ref_view;
6473 e4526bf5 2021-09-03 naddy break;
6474 e4526bf5 2021-09-03 naddy case 'g':
6475 e4526bf5 2021-09-03 naddy case KEY_HOME:
6476 e4526bf5 2021-09-03 naddy s->selected = 0;
6477 07b0611c 2022-06-23 thomas view->count = 0;
6478 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6479 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6480 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6481 e4526bf5 2021-09-03 naddy else
6482 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6483 1e37a5c2 2019-05-12 jcs break;
6484 e4526bf5 2021-09-03 naddy case 'G':
6485 a5d43cac 2022-07-01 thomas case KEY_END: {
6486 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6487 a5d43cac 2022-07-01 thomas
6488 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6489 a5d43cac 2022-07-01 thomas --eos; /* border */
6490 e4526bf5 2021-09-03 naddy s->selected = 0;
6491 07b0611c 2022-06-23 thomas view->count = 0;
6492 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6493 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6494 e4526bf5 2021-09-03 naddy if (te == NULL) {
6495 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6496 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6497 e4526bf5 2021-09-03 naddy n++;
6498 e4526bf5 2021-09-03 naddy }
6499 e4526bf5 2021-09-03 naddy break;
6500 e4526bf5 2021-09-03 naddy }
6501 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6502 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6503 e4526bf5 2021-09-03 naddy }
6504 e4526bf5 2021-09-03 naddy if (n > 0)
6505 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6506 e4526bf5 2021-09-03 naddy break;
6507 a5d43cac 2022-07-01 thomas }
6508 1e37a5c2 2019-05-12 jcs case 'k':
6509 1e37a5c2 2019-05-12 jcs case KEY_UP:
6510 f7140bf5 2021-10-17 thomas case CTRL('p'):
6511 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6512 1e37a5c2 2019-05-12 jcs s->selected--;
6513 fa86c4bf 2020-11-29 stsp break;
6514 1e37a5c2 2019-05-12 jcs }
6515 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6516 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6517 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6518 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6519 07b0611c 2022-06-23 thomas view->count = 0;
6520 1e37a5c2 2019-05-12 jcs break;
6521 70f17a53 2022-06-13 thomas case CTRL('u'):
6522 23427b14 2022-06-23 thomas case 'u':
6523 70f17a53 2022-06-13 thomas nscroll /= 2;
6524 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6525 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6526 ea025d1d 2020-02-22 naddy case CTRL('b'):
6527 1c5e5faa 2022-06-23 thomas case 'b':
6528 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6529 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6530 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6531 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6532 fa86c4bf 2020-11-29 stsp } else {
6533 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6534 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6535 fa86c4bf 2020-11-29 stsp }
6536 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6537 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6538 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6539 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6540 07b0611c 2022-06-23 thomas view->count = 0;
6541 1e37a5c2 2019-05-12 jcs break;
6542 1e37a5c2 2019-05-12 jcs case 'j':
6543 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6544 f7140bf5 2021-10-17 thomas case CTRL('n'):
6545 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6546 1e37a5c2 2019-05-12 jcs s->selected++;
6547 1e37a5c2 2019-05-12 jcs break;
6548 1e37a5c2 2019-05-12 jcs }
6549 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6550 07b0611c 2022-06-23 thomas == NULL) {
6551 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6552 07b0611c 2022-06-23 thomas view->count = 0;
6553 1e37a5c2 2019-05-12 jcs break;
6554 07b0611c 2022-06-23 thomas }
6555 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6556 1e37a5c2 2019-05-12 jcs break;
6557 70f17a53 2022-06-13 thomas case CTRL('d'):
6558 23427b14 2022-06-23 thomas case 'd':
6559 70f17a53 2022-06-13 thomas nscroll /= 2;
6560 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6561 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6562 ea025d1d 2020-02-22 naddy case CTRL('f'):
6563 1c5e5faa 2022-06-23 thomas case 'f':
6564 4c2d69cb 2022-06-23 thomas case ' ':
6565 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6566 1e37a5c2 2019-05-12 jcs == NULL) {
6567 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6568 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6569 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6570 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6571 07b0611c 2022-06-23 thomas else
6572 07b0611c 2022-06-23 thomas view->count = 0;
6573 1e37a5c2 2019-05-12 jcs break;
6574 1e37a5c2 2019-05-12 jcs }
6575 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6576 1e37a5c2 2019-05-12 jcs break;
6577 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6578 1e37a5c2 2019-05-12 jcs case '\r':
6579 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6580 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6581 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6582 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6583 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6584 07b0611c 2022-06-23 thomas view->count = 0;
6585 1e37a5c2 2019-05-12 jcs break;
6586 07b0611c 2022-06-23 thomas }
6587 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6588 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6589 1e37a5c2 2019-05-12 jcs entry);
6590 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6591 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6592 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6593 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6594 1e37a5c2 2019-05-12 jcs s->selected_entry =
6595 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6596 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6597 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6598 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6599 a5d43cac 2022-07-01 thomas if (err)
6600 a5d43cac 2022-07-01 thomas break;
6601 a5d43cac 2022-07-01 thomas }
6602 1e37a5c2 2019-05-12 jcs free(parent);
6603 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6604 56e0773d 2019-11-28 stsp s->selected_entry))) {
6605 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6606 07b0611c 2022-06-23 thomas view->count = 0;
6607 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6608 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6609 1e37a5c2 2019-05-12 jcs if (err)
6610 1e37a5c2 2019-05-12 jcs break;
6611 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6612 941e9f74 2019-05-21 stsp if (err) {
6613 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6614 1e37a5c2 2019-05-12 jcs break;
6615 1e37a5c2 2019-05-12 jcs }
6616 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6617 56e0773d 2019-11-28 stsp s->selected_entry))) {
6618 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6619 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
6620 1e37a5c2 2019-05-12 jcs
6621 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
6622 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
6623 a5d43cac 2022-07-01 thomas
6624 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
6625 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6626 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6627 1e37a5c2 2019-05-12 jcs if (err)
6628 1e37a5c2 2019-05-12 jcs break;
6629 a5d43cac 2022-07-01 thomas
6630 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6631 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6632 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6633 a5d43cac 2022-07-01 thomas if (err)
6634 a5d43cac 2022-07-01 thomas break;
6635 a5d43cac 2022-07-01 thomas }
6636 a5d43cac 2022-07-01 thomas
6637 07b0611c 2022-06-23 thomas view->count = 0;
6638 e78dc838 2020-12-04 stsp view->focussed = 0;
6639 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6640 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
6641 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
6642 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6643 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6644 669b5ffa 2018-10-07 stsp if (err)
6645 669b5ffa 2018-10-07 stsp return err;
6646 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
6647 40236d76 2022-06-23 thomas if (err)
6648 40236d76 2022-06-23 thomas return err;
6649 e78dc838 2020-12-04 stsp view->focus_child = 1;
6650 669b5ffa 2018-10-07 stsp } else
6651 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6652 1e37a5c2 2019-05-12 jcs }
6653 1e37a5c2 2019-05-12 jcs break;
6654 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6655 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6656 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6657 07b0611c 2022-06-23 thomas view->count = 0;
6658 1e37a5c2 2019-05-12 jcs break;
6659 1e37a5c2 2019-05-12 jcs default:
6660 07b0611c 2022-06-23 thomas view->count = 0;
6661 1e37a5c2 2019-05-12 jcs break;
6662 ffd1d5e5 2018-06-23 stsp }
6663 e5a0f69f 2018-08-18 stsp
6664 ffd1d5e5 2018-06-23 stsp return err;
6665 9f7d7167 2018-04-29 stsp }
6666 9f7d7167 2018-04-29 stsp
6667 ffd1d5e5 2018-06-23 stsp __dead static void
6668 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6669 ffd1d5e5 2018-06-23 stsp {
6670 ffd1d5e5 2018-06-23 stsp endwin();
6671 91198554 2022-06-23 thomas fprintf(stderr,
6672 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6673 ffd1d5e5 2018-06-23 stsp getprogname());
6674 ffd1d5e5 2018-06-23 stsp exit(1);
6675 ffd1d5e5 2018-06-23 stsp }
6676 ffd1d5e5 2018-06-23 stsp
6677 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6678 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6679 ffd1d5e5 2018-06-23 stsp {
6680 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6681 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6682 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6683 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6684 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6685 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6686 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6687 4e97c21c 2020-12-06 stsp char *label = NULL;
6688 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6689 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6690 ffd1d5e5 2018-06-23 stsp int ch;
6691 5221c383 2018-08-01 stsp struct tog_view *view;
6692 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6693 70ac5f84 2019-03-28 stsp
6694 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6695 ffd1d5e5 2018-06-23 stsp switch (ch) {
6696 ffd1d5e5 2018-06-23 stsp case 'c':
6697 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6698 74283ab8 2020-02-07 stsp break;
6699 74283ab8 2020-02-07 stsp case 'r':
6700 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6701 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6702 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6703 74283ab8 2020-02-07 stsp optarg);
6704 ffd1d5e5 2018-06-23 stsp break;
6705 ffd1d5e5 2018-06-23 stsp default:
6706 e99e2d15 2020-11-24 naddy usage_tree();
6707 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6708 ffd1d5e5 2018-06-23 stsp }
6709 ffd1d5e5 2018-06-23 stsp }
6710 ffd1d5e5 2018-06-23 stsp
6711 ffd1d5e5 2018-06-23 stsp argc -= optind;
6712 ffd1d5e5 2018-06-23 stsp argv += optind;
6713 ffd1d5e5 2018-06-23 stsp
6714 55cccc34 2020-02-20 stsp if (argc > 1)
6715 e99e2d15 2020-11-24 naddy usage_tree();
6716 7cd52833 2022-06-23 thomas
6717 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6718 7cd52833 2022-06-23 thomas if (error != NULL)
6719 7cd52833 2022-06-23 thomas goto done;
6720 74283ab8 2020-02-07 stsp
6721 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6722 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6723 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6724 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6725 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6726 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6727 c156c7a4 2020-12-18 stsp goto done;
6728 55cccc34 2020-02-20 stsp if (worktree)
6729 52185f70 2019-02-05 stsp repo_path =
6730 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6731 55cccc34 2020-02-20 stsp else
6732 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6733 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6734 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6735 c156c7a4 2020-12-18 stsp goto done;
6736 c156c7a4 2020-12-18 stsp }
6737 55cccc34 2020-02-20 stsp }
6738 a915003a 2019-02-05 stsp
6739 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6740 c02c541e 2019-03-29 stsp if (error != NULL)
6741 52185f70 2019-02-05 stsp goto done;
6742 d188b9a6 2019-01-04 stsp
6743 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6744 55cccc34 2020-02-20 stsp repo, worktree);
6745 55cccc34 2020-02-20 stsp if (error)
6746 55cccc34 2020-02-20 stsp goto done;
6747 55cccc34 2020-02-20 stsp
6748 55cccc34 2020-02-20 stsp init_curses();
6749 55cccc34 2020-02-20 stsp
6750 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6751 c02c541e 2019-03-29 stsp if (error)
6752 52185f70 2019-02-05 stsp goto done;
6753 ffd1d5e5 2018-06-23 stsp
6754 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6755 51a10b52 2020-12-26 stsp if (error)
6756 51a10b52 2020-12-26 stsp goto done;
6757 51a10b52 2020-12-26 stsp
6758 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6759 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6760 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6761 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6762 4e97c21c 2020-12-06 stsp if (error)
6763 4e97c21c 2020-12-06 stsp goto done;
6764 4e97c21c 2020-12-06 stsp head_ref_name = label;
6765 4e97c21c 2020-12-06 stsp } else {
6766 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6767 4e97c21c 2020-12-06 stsp if (error == NULL)
6768 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6769 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6770 4e97c21c 2020-12-06 stsp goto done;
6771 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6772 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6773 4e97c21c 2020-12-06 stsp if (error)
6774 4e97c21c 2020-12-06 stsp goto done;
6775 4e97c21c 2020-12-06 stsp }
6776 ffd1d5e5 2018-06-23 stsp
6777 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6778 945f9229 2022-04-16 thomas if (error)
6779 945f9229 2022-04-16 thomas goto done;
6780 945f9229 2022-04-16 thomas
6781 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6782 5221c383 2018-08-01 stsp if (view == NULL) {
6783 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6784 5221c383 2018-08-01 stsp goto done;
6785 5221c383 2018-08-01 stsp }
6786 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6787 ad80ab7b 2018-08-04 stsp if (error)
6788 ad80ab7b 2018-08-04 stsp goto done;
6789 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6790 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
6791 d91faf3b 2020-12-01 naddy in_repo_path);
6792 55cccc34 2020-02-20 stsp if (error)
6793 55cccc34 2020-02-20 stsp goto done;
6794 55cccc34 2020-02-20 stsp }
6795 55cccc34 2020-02-20 stsp
6796 55cccc34 2020-02-20 stsp if (worktree) {
6797 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6798 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6799 55cccc34 2020-02-20 stsp worktree = NULL;
6800 55cccc34 2020-02-20 stsp }
6801 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6802 ffd1d5e5 2018-06-23 stsp done:
6803 52185f70 2019-02-05 stsp free(repo_path);
6804 e4a0e26d 2020-02-20 stsp free(cwd);
6805 ffd1d5e5 2018-06-23 stsp free(commit_id);
6806 4e97c21c 2020-12-06 stsp free(label);
6807 486cd271 2020-12-06 stsp if (ref)
6808 486cd271 2020-12-06 stsp got_ref_close(ref);
6809 1d0f4054 2021-06-17 stsp if (repo) {
6810 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6811 1d0f4054 2021-06-17 stsp if (error == NULL)
6812 1d0f4054 2021-06-17 stsp error = close_err;
6813 1d0f4054 2021-06-17 stsp }
6814 7cd52833 2022-06-23 thomas if (pack_fds) {
6815 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6816 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6817 7cd52833 2022-06-23 thomas if (error == NULL)
6818 7cd52833 2022-06-23 thomas error = pack_err;
6819 7cd52833 2022-06-23 thomas }
6820 51a10b52 2020-12-26 stsp tog_free_refs();
6821 ffd1d5e5 2018-06-23 stsp return error;
6822 6458efa5 2020-11-24 stsp }
6823 6458efa5 2020-11-24 stsp
6824 6458efa5 2020-11-24 stsp static const struct got_error *
6825 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6826 6458efa5 2020-11-24 stsp {
6827 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6828 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6829 6458efa5 2020-11-24 stsp
6830 6458efa5 2020-11-24 stsp s->nrefs = 0;
6831 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6832 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
6833 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
6834 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
6835 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
6836 6458efa5 2020-11-24 stsp continue;
6837 6458efa5 2020-11-24 stsp
6838 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6839 6458efa5 2020-11-24 stsp if (re == NULL)
6840 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6841 6458efa5 2020-11-24 stsp
6842 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6843 8924d611 2020-12-26 stsp if (re->ref == NULL)
6844 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6845 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6846 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6847 6458efa5 2020-11-24 stsp }
6848 6458efa5 2020-11-24 stsp
6849 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6850 6458efa5 2020-11-24 stsp return NULL;
6851 6458efa5 2020-11-24 stsp }
6852 6458efa5 2020-11-24 stsp
6853 ef20f542 2022-06-26 thomas static void
6854 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6855 6458efa5 2020-11-24 stsp {
6856 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6857 6458efa5 2020-11-24 stsp
6858 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6859 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6860 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6861 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6862 6458efa5 2020-11-24 stsp free(re);
6863 6458efa5 2020-11-24 stsp }
6864 6458efa5 2020-11-24 stsp }
6865 6458efa5 2020-11-24 stsp
6866 6458efa5 2020-11-24 stsp static const struct got_error *
6867 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6868 6458efa5 2020-11-24 stsp {
6869 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6870 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6871 6458efa5 2020-11-24 stsp
6872 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6873 6458efa5 2020-11-24 stsp s->repo = repo;
6874 6458efa5 2020-11-24 stsp
6875 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6876 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6877 6458efa5 2020-11-24 stsp
6878 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6879 6458efa5 2020-11-24 stsp if (err)
6880 6458efa5 2020-11-24 stsp return err;
6881 34ba6917 2020-11-30 stsp
6882 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6883 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6884 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6885 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6886 6458efa5 2020-11-24 stsp if (err)
6887 6458efa5 2020-11-24 stsp goto done;
6888 6458efa5 2020-11-24 stsp
6889 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6890 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6891 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6892 6458efa5 2020-11-24 stsp if (err)
6893 6458efa5 2020-11-24 stsp goto done;
6894 6458efa5 2020-11-24 stsp
6895 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6896 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6897 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6898 2183bbf6 2022-01-23 thomas if (err)
6899 2183bbf6 2022-01-23 thomas goto done;
6900 2183bbf6 2022-01-23 thomas
6901 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
6902 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
6903 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
6904 6458efa5 2020-11-24 stsp if (err)
6905 6458efa5 2020-11-24 stsp goto done;
6906 6458efa5 2020-11-24 stsp }
6907 6458efa5 2020-11-24 stsp
6908 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6909 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6910 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6911 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6912 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6913 6458efa5 2020-11-24 stsp done:
6914 6458efa5 2020-11-24 stsp if (err)
6915 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6916 6458efa5 2020-11-24 stsp return err;
6917 6458efa5 2020-11-24 stsp }
6918 6458efa5 2020-11-24 stsp
6919 6458efa5 2020-11-24 stsp static const struct got_error *
6920 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6921 6458efa5 2020-11-24 stsp {
6922 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6923 6458efa5 2020-11-24 stsp
6924 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6925 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6926 6458efa5 2020-11-24 stsp
6927 6458efa5 2020-11-24 stsp return NULL;
6928 9f7d7167 2018-04-29 stsp }
6929 ce5b7c56 2019-07-09 stsp
6930 6458efa5 2020-11-24 stsp static const struct got_error *
6931 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6932 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6933 6458efa5 2020-11-24 stsp {
6934 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6935 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6936 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6937 6458efa5 2020-11-24 stsp int obj_type;
6938 6458efa5 2020-11-24 stsp
6939 c42c9805 2020-11-24 stsp *commit_id = NULL;
6940 6458efa5 2020-11-24 stsp
6941 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6942 6458efa5 2020-11-24 stsp if (err)
6943 6458efa5 2020-11-24 stsp return err;
6944 6458efa5 2020-11-24 stsp
6945 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6946 6458efa5 2020-11-24 stsp if (err)
6947 6458efa5 2020-11-24 stsp goto done;
6948 6458efa5 2020-11-24 stsp
6949 6458efa5 2020-11-24 stsp switch (obj_type) {
6950 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6951 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6952 6458efa5 2020-11-24 stsp break;
6953 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6954 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6955 6458efa5 2020-11-24 stsp if (err)
6956 6458efa5 2020-11-24 stsp goto done;
6957 c42c9805 2020-11-24 stsp free(obj_id);
6958 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6959 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6960 c42c9805 2020-11-24 stsp if (err)
6961 6458efa5 2020-11-24 stsp goto done;
6962 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6963 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6964 c42c9805 2020-11-24 stsp goto done;
6965 c42c9805 2020-11-24 stsp }
6966 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6967 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6968 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6969 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6970 c42c9805 2020-11-24 stsp goto done;
6971 c42c9805 2020-11-24 stsp }
6972 6458efa5 2020-11-24 stsp break;
6973 6458efa5 2020-11-24 stsp default:
6974 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6975 c42c9805 2020-11-24 stsp break;
6976 6458efa5 2020-11-24 stsp }
6977 6458efa5 2020-11-24 stsp
6978 c42c9805 2020-11-24 stsp done:
6979 c42c9805 2020-11-24 stsp if (tag)
6980 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6981 c42c9805 2020-11-24 stsp if (err) {
6982 c42c9805 2020-11-24 stsp free(*commit_id);
6983 c42c9805 2020-11-24 stsp *commit_id = NULL;
6984 c42c9805 2020-11-24 stsp }
6985 c42c9805 2020-11-24 stsp return err;
6986 c42c9805 2020-11-24 stsp }
6987 c42c9805 2020-11-24 stsp
6988 c42c9805 2020-11-24 stsp static const struct got_error *
6989 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6990 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6991 c42c9805 2020-11-24 stsp {
6992 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6993 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6994 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6995 c42c9805 2020-11-24 stsp
6996 c42c9805 2020-11-24 stsp *new_view = NULL;
6997 c42c9805 2020-11-24 stsp
6998 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6999 c42c9805 2020-11-24 stsp if (err) {
7000 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7001 c42c9805 2020-11-24 stsp return err;
7002 c42c9805 2020-11-24 stsp else
7003 c42c9805 2020-11-24 stsp return NULL;
7004 c42c9805 2020-11-24 stsp }
7005 c42c9805 2020-11-24 stsp
7006 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7007 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7008 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7009 6458efa5 2020-11-24 stsp goto done;
7010 6458efa5 2020-11-24 stsp }
7011 6458efa5 2020-11-24 stsp
7012 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7013 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7014 6458efa5 2020-11-24 stsp done:
7015 6458efa5 2020-11-24 stsp if (err)
7016 6458efa5 2020-11-24 stsp view_close(log_view);
7017 6458efa5 2020-11-24 stsp else
7018 6458efa5 2020-11-24 stsp *new_view = log_view;
7019 c42c9805 2020-11-24 stsp free(commit_id);
7020 6458efa5 2020-11-24 stsp return err;
7021 6458efa5 2020-11-24 stsp }
7022 6458efa5 2020-11-24 stsp
7023 ce5b7c56 2019-07-09 stsp static void
7024 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7025 6458efa5 2020-11-24 stsp {
7026 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7027 34ba6917 2020-11-30 stsp int i = 0;
7028 6458efa5 2020-11-24 stsp
7029 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7030 6458efa5 2020-11-24 stsp return;
7031 6458efa5 2020-11-24 stsp
7032 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7033 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7034 34ba6917 2020-11-30 stsp if (re == NULL)
7035 34ba6917 2020-11-30 stsp break;
7036 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7037 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7038 6458efa5 2020-11-24 stsp }
7039 6458efa5 2020-11-24 stsp }
7040 6458efa5 2020-11-24 stsp
7041 a5d43cac 2022-07-01 thomas static const struct got_error *
7042 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7043 6458efa5 2020-11-24 stsp {
7044 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7045 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7046 6458efa5 2020-11-24 stsp int n = 0;
7047 6458efa5 2020-11-24 stsp
7048 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7049 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7050 6458efa5 2020-11-24 stsp else
7051 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7052 6458efa5 2020-11-24 stsp
7053 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7054 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7055 a5d43cac 2022-07-01 thomas if (last)
7056 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7057 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7058 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7059 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7060 6458efa5 2020-11-24 stsp }
7061 6458efa5 2020-11-24 stsp }
7062 a5d43cac 2022-07-01 thomas
7063 a5d43cac 2022-07-01 thomas return NULL;
7064 6458efa5 2020-11-24 stsp }
7065 6458efa5 2020-11-24 stsp
7066 6458efa5 2020-11-24 stsp static const struct got_error *
7067 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7068 6458efa5 2020-11-24 stsp {
7069 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7070 6458efa5 2020-11-24 stsp
7071 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7072 6458efa5 2020-11-24 stsp return NULL;
7073 6458efa5 2020-11-24 stsp }
7074 6458efa5 2020-11-24 stsp
7075 6458efa5 2020-11-24 stsp static int
7076 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7077 6458efa5 2020-11-24 stsp {
7078 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7079 6458efa5 2020-11-24 stsp
7080 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7081 6458efa5 2020-11-24 stsp 0) == 0;
7082 6458efa5 2020-11-24 stsp }
7083 6458efa5 2020-11-24 stsp
7084 6458efa5 2020-11-24 stsp static const struct got_error *
7085 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7086 6458efa5 2020-11-24 stsp {
7087 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7088 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7089 6458efa5 2020-11-24 stsp
7090 6458efa5 2020-11-24 stsp if (!view->searching) {
7091 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7092 6458efa5 2020-11-24 stsp return NULL;
7093 6458efa5 2020-11-24 stsp }
7094 6458efa5 2020-11-24 stsp
7095 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7096 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7097 6458efa5 2020-11-24 stsp if (s->selected_entry)
7098 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7099 6458efa5 2020-11-24 stsp else
7100 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7101 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7102 6458efa5 2020-11-24 stsp } else {
7103 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7104 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7105 6458efa5 2020-11-24 stsp else
7106 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7107 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7108 6458efa5 2020-11-24 stsp }
7109 6458efa5 2020-11-24 stsp } else {
7110 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7111 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7112 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7113 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7114 6458efa5 2020-11-24 stsp else
7115 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7116 6458efa5 2020-11-24 stsp }
7117 6458efa5 2020-11-24 stsp
7118 6458efa5 2020-11-24 stsp while (1) {
7119 6458efa5 2020-11-24 stsp if (re == NULL) {
7120 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7121 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7122 6458efa5 2020-11-24 stsp return NULL;
7123 6458efa5 2020-11-24 stsp }
7124 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7125 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7126 6458efa5 2020-11-24 stsp else
7127 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7128 6458efa5 2020-11-24 stsp }
7129 6458efa5 2020-11-24 stsp
7130 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7131 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7132 6458efa5 2020-11-24 stsp s->matched_entry = re;
7133 6458efa5 2020-11-24 stsp break;
7134 6458efa5 2020-11-24 stsp }
7135 6458efa5 2020-11-24 stsp
7136 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7137 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7138 6458efa5 2020-11-24 stsp else
7139 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7140 6458efa5 2020-11-24 stsp }
7141 6458efa5 2020-11-24 stsp
7142 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7143 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7144 6458efa5 2020-11-24 stsp s->selected = 0;
7145 6458efa5 2020-11-24 stsp }
7146 6458efa5 2020-11-24 stsp
7147 6458efa5 2020-11-24 stsp return NULL;
7148 6458efa5 2020-11-24 stsp }
7149 6458efa5 2020-11-24 stsp
7150 6458efa5 2020-11-24 stsp static const struct got_error *
7151 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7152 6458efa5 2020-11-24 stsp {
7153 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7154 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7155 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7156 6458efa5 2020-11-24 stsp char *line = NULL;
7157 6458efa5 2020-11-24 stsp wchar_t *wline;
7158 6458efa5 2020-11-24 stsp struct tog_color *tc;
7159 6458efa5 2020-11-24 stsp int width, n;
7160 6458efa5 2020-11-24 stsp int limit = view->nlines;
7161 6458efa5 2020-11-24 stsp
7162 6458efa5 2020-11-24 stsp werase(view->window);
7163 6458efa5 2020-11-24 stsp
7164 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7165 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7166 a5d43cac 2022-07-01 thomas --limit; /* border */
7167 6458efa5 2020-11-24 stsp
7168 6458efa5 2020-11-24 stsp if (limit == 0)
7169 6458efa5 2020-11-24 stsp return NULL;
7170 6458efa5 2020-11-24 stsp
7171 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7172 6458efa5 2020-11-24 stsp
7173 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7174 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7175 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7176 6458efa5 2020-11-24 stsp
7177 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7178 6458efa5 2020-11-24 stsp if (err) {
7179 6458efa5 2020-11-24 stsp free(line);
7180 6458efa5 2020-11-24 stsp return err;
7181 6458efa5 2020-11-24 stsp }
7182 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7183 6458efa5 2020-11-24 stsp wstandout(view->window);
7184 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7185 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7186 6458efa5 2020-11-24 stsp wstandend(view->window);
7187 6458efa5 2020-11-24 stsp free(wline);
7188 6458efa5 2020-11-24 stsp wline = NULL;
7189 6458efa5 2020-11-24 stsp free(line);
7190 6458efa5 2020-11-24 stsp line = NULL;
7191 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7192 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7193 6458efa5 2020-11-24 stsp if (--limit <= 0)
7194 6458efa5 2020-11-24 stsp return NULL;
7195 6458efa5 2020-11-24 stsp
7196 6458efa5 2020-11-24 stsp n = 0;
7197 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7198 6458efa5 2020-11-24 stsp char *line = NULL;
7199 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7200 6458efa5 2020-11-24 stsp
7201 84227eb1 2022-06-23 thomas if (s->show_date) {
7202 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7203 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7204 84227eb1 2022-06-23 thomas struct got_object_id *id;
7205 84227eb1 2022-06-23 thomas struct tm tm;
7206 84227eb1 2022-06-23 thomas time_t t;
7207 84227eb1 2022-06-23 thomas
7208 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7209 84227eb1 2022-06-23 thomas if (err)
7210 84227eb1 2022-06-23 thomas return err;
7211 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7212 84227eb1 2022-06-23 thomas if (err) {
7213 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7214 84227eb1 2022-06-23 thomas free(id);
7215 84227eb1 2022-06-23 thomas return err;
7216 84227eb1 2022-06-23 thomas }
7217 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7218 84227eb1 2022-06-23 thomas id);
7219 84227eb1 2022-06-23 thomas if (err) {
7220 84227eb1 2022-06-23 thomas free(id);
7221 84227eb1 2022-06-23 thomas return err;
7222 84227eb1 2022-06-23 thomas }
7223 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7224 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7225 84227eb1 2022-06-23 thomas } else {
7226 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7227 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7228 84227eb1 2022-06-23 thomas }
7229 84227eb1 2022-06-23 thomas free(id);
7230 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7231 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7232 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7233 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7234 84227eb1 2022-06-23 thomas }
7235 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7236 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7237 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7238 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7239 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7240 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7241 6458efa5 2020-11-24 stsp struct got_object_id *id;
7242 6458efa5 2020-11-24 stsp char *id_str;
7243 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7244 6458efa5 2020-11-24 stsp if (err)
7245 6458efa5 2020-11-24 stsp return err;
7246 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7247 6458efa5 2020-11-24 stsp if (err) {
7248 6458efa5 2020-11-24 stsp free(id);
7249 6458efa5 2020-11-24 stsp return err;
7250 6458efa5 2020-11-24 stsp }
7251 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7252 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7253 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7254 6458efa5 2020-11-24 stsp free(id);
7255 6458efa5 2020-11-24 stsp free(id_str);
7256 6458efa5 2020-11-24 stsp return err;
7257 6458efa5 2020-11-24 stsp }
7258 6458efa5 2020-11-24 stsp free(id);
7259 6458efa5 2020-11-24 stsp free(id_str);
7260 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7261 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7262 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7263 6458efa5 2020-11-24 stsp
7264 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7265 f91a2b48 2022-06-23 thomas 0, 0);
7266 6458efa5 2020-11-24 stsp if (err) {
7267 6458efa5 2020-11-24 stsp free(line);
7268 6458efa5 2020-11-24 stsp return err;
7269 6458efa5 2020-11-24 stsp }
7270 6458efa5 2020-11-24 stsp if (n == s->selected) {
7271 6458efa5 2020-11-24 stsp if (view->focussed)
7272 6458efa5 2020-11-24 stsp wstandout(view->window);
7273 6458efa5 2020-11-24 stsp s->selected_entry = re;
7274 6458efa5 2020-11-24 stsp }
7275 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7276 6458efa5 2020-11-24 stsp if (tc)
7277 6458efa5 2020-11-24 stsp wattr_on(view->window,
7278 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7279 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7280 6458efa5 2020-11-24 stsp if (tc)
7281 6458efa5 2020-11-24 stsp wattr_off(view->window,
7282 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7283 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7284 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7285 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7286 6458efa5 2020-11-24 stsp wstandend(view->window);
7287 6458efa5 2020-11-24 stsp free(line);
7288 6458efa5 2020-11-24 stsp free(wline);
7289 6458efa5 2020-11-24 stsp wline = NULL;
7290 6458efa5 2020-11-24 stsp n++;
7291 6458efa5 2020-11-24 stsp s->ndisplayed++;
7292 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7293 6458efa5 2020-11-24 stsp
7294 6458efa5 2020-11-24 stsp limit--;
7295 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7296 6458efa5 2020-11-24 stsp }
7297 6458efa5 2020-11-24 stsp
7298 a5d43cac 2022-07-01 thomas view_border(view);
7299 6458efa5 2020-11-24 stsp return err;
7300 6458efa5 2020-11-24 stsp }
7301 6458efa5 2020-11-24 stsp
7302 6458efa5 2020-11-24 stsp static const struct got_error *
7303 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7304 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7305 c42c9805 2020-11-24 stsp {
7306 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7307 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7308 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7309 c42c9805 2020-11-24 stsp
7310 c42c9805 2020-11-24 stsp *new_view = NULL;
7311 c42c9805 2020-11-24 stsp
7312 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7313 c42c9805 2020-11-24 stsp if (err) {
7314 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7315 c42c9805 2020-11-24 stsp return err;
7316 c42c9805 2020-11-24 stsp else
7317 c42c9805 2020-11-24 stsp return NULL;
7318 c42c9805 2020-11-24 stsp }
7319 c42c9805 2020-11-24 stsp
7320 c42c9805 2020-11-24 stsp
7321 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7322 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7323 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7324 c42c9805 2020-11-24 stsp goto done;
7325 c42c9805 2020-11-24 stsp }
7326 c42c9805 2020-11-24 stsp
7327 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7328 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7329 c42c9805 2020-11-24 stsp if (err)
7330 c42c9805 2020-11-24 stsp goto done;
7331 c42c9805 2020-11-24 stsp
7332 c42c9805 2020-11-24 stsp *new_view = tree_view;
7333 c42c9805 2020-11-24 stsp done:
7334 c42c9805 2020-11-24 stsp free(commit_id);
7335 c42c9805 2020-11-24 stsp return err;
7336 c42c9805 2020-11-24 stsp }
7337 c42c9805 2020-11-24 stsp static const struct got_error *
7338 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7339 6458efa5 2020-11-24 stsp {
7340 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7341 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7342 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7343 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7344 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7345 6458efa5 2020-11-24 stsp
7346 6458efa5 2020-11-24 stsp switch (ch) {
7347 6458efa5 2020-11-24 stsp case 'i':
7348 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7349 07b0611c 2022-06-23 thomas view->count = 0;
7350 3bfadbd4 2021-11-20 thomas break;
7351 84227eb1 2022-06-23 thomas case 'm':
7352 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7353 07b0611c 2022-06-23 thomas view->count = 0;
7354 84227eb1 2022-06-23 thomas break;
7355 98182bd0 2021-11-20 thomas case 'o':
7356 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7357 07b0611c 2022-06-23 thomas view->count = 0;
7358 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7359 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7360 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7361 c591440f 2021-11-20 thomas if (err)
7362 c591440f 2021-11-20 thomas break;
7363 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7364 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7365 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7366 3bfadbd4 2021-11-20 thomas if (err)
7367 3bfadbd4 2021-11-20 thomas break;
7368 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7369 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7370 6458efa5 2020-11-24 stsp break;
7371 6458efa5 2020-11-24 stsp case KEY_ENTER:
7372 6458efa5 2020-11-24 stsp case '\r':
7373 07b0611c 2022-06-23 thomas view->count = 0;
7374 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7375 6458efa5 2020-11-24 stsp break;
7376 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7377 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7378 a5d43cac 2022-07-01 thomas
7379 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7380 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7381 a5d43cac 2022-07-01 thomas if (err)
7382 a5d43cac 2022-07-01 thomas break;
7383 a5d43cac 2022-07-01 thomas
7384 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7385 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7386 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7387 a5d43cac 2022-07-01 thomas if (err)
7388 a5d43cac 2022-07-01 thomas break;
7389 a5d43cac 2022-07-01 thomas }
7390 a5d43cac 2022-07-01 thomas
7391 e78dc838 2020-12-04 stsp view->focussed = 0;
7392 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7393 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7394 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7395 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7396 6458efa5 2020-11-24 stsp err = view_close_child(view);
7397 6458efa5 2020-11-24 stsp if (err)
7398 6458efa5 2020-11-24 stsp return err;
7399 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7400 40236d76 2022-06-23 thomas if (err)
7401 40236d76 2022-06-23 thomas return err;
7402 e78dc838 2020-12-04 stsp view->focus_child = 1;
7403 6458efa5 2020-11-24 stsp } else
7404 6458efa5 2020-11-24 stsp *new_view = log_view;
7405 6458efa5 2020-11-24 stsp break;
7406 c42c9805 2020-11-24 stsp case 't':
7407 07b0611c 2022-06-23 thomas view->count = 0;
7408 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7409 c42c9805 2020-11-24 stsp break;
7410 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7411 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7412 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7413 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7414 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7415 c42c9805 2020-11-24 stsp break;
7416 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7417 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7418 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7419 444d5325 2022-07-03 thomas if (err)
7420 444d5325 2022-07-03 thomas break;
7421 444d5325 2022-07-03 thomas }
7422 e78dc838 2020-12-04 stsp view->focussed = 0;
7423 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7424 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7425 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7426 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7427 c42c9805 2020-11-24 stsp err = view_close_child(view);
7428 c42c9805 2020-11-24 stsp if (err)
7429 c42c9805 2020-11-24 stsp return err;
7430 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7431 40236d76 2022-06-23 thomas if (err)
7432 40236d76 2022-06-23 thomas return err;
7433 e78dc838 2020-12-04 stsp view->focus_child = 1;
7434 c42c9805 2020-11-24 stsp } else
7435 c42c9805 2020-11-24 stsp *new_view = tree_view;
7436 c42c9805 2020-11-24 stsp break;
7437 e4526bf5 2021-09-03 naddy case 'g':
7438 e4526bf5 2021-09-03 naddy case KEY_HOME:
7439 e4526bf5 2021-09-03 naddy s->selected = 0;
7440 07b0611c 2022-06-23 thomas view->count = 0;
7441 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7442 e4526bf5 2021-09-03 naddy break;
7443 e4526bf5 2021-09-03 naddy case 'G':
7444 a5d43cac 2022-07-01 thomas case KEY_END: {
7445 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7446 a5d43cac 2022-07-01 thomas
7447 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7448 a5d43cac 2022-07-01 thomas --eos; /* border */
7449 e4526bf5 2021-09-03 naddy s->selected = 0;
7450 07b0611c 2022-06-23 thomas view->count = 0;
7451 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7452 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7453 e4526bf5 2021-09-03 naddy if (re == NULL)
7454 e4526bf5 2021-09-03 naddy break;
7455 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7456 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7457 e4526bf5 2021-09-03 naddy }
7458 e4526bf5 2021-09-03 naddy if (n > 0)
7459 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7460 e4526bf5 2021-09-03 naddy break;
7461 a5d43cac 2022-07-01 thomas }
7462 6458efa5 2020-11-24 stsp case 'k':
7463 6458efa5 2020-11-24 stsp case KEY_UP:
7464 f7140bf5 2021-10-17 thomas case CTRL('p'):
7465 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7466 6458efa5 2020-11-24 stsp s->selected--;
7467 6458efa5 2020-11-24 stsp break;
7468 34ba6917 2020-11-30 stsp }
7469 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7470 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7471 07b0611c 2022-06-23 thomas view->count = 0;
7472 6458efa5 2020-11-24 stsp break;
7473 70f17a53 2022-06-13 thomas case CTRL('u'):
7474 23427b14 2022-06-23 thomas case 'u':
7475 70f17a53 2022-06-13 thomas nscroll /= 2;
7476 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7477 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7478 6458efa5 2020-11-24 stsp case CTRL('b'):
7479 1c5e5faa 2022-06-23 thomas case 'b':
7480 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7481 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7482 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7483 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7484 07b0611c 2022-06-23 thomas view->count = 0;
7485 6458efa5 2020-11-24 stsp break;
7486 6458efa5 2020-11-24 stsp case 'j':
7487 6458efa5 2020-11-24 stsp case KEY_DOWN:
7488 f7140bf5 2021-10-17 thomas case CTRL('n'):
7489 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7490 6458efa5 2020-11-24 stsp s->selected++;
7491 6458efa5 2020-11-24 stsp break;
7492 6458efa5 2020-11-24 stsp }
7493 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7494 6458efa5 2020-11-24 stsp /* can't scroll any further */
7495 07b0611c 2022-06-23 thomas view->count = 0;
7496 6458efa5 2020-11-24 stsp break;
7497 07b0611c 2022-06-23 thomas }
7498 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7499 6458efa5 2020-11-24 stsp break;
7500 70f17a53 2022-06-13 thomas case CTRL('d'):
7501 23427b14 2022-06-23 thomas case 'd':
7502 70f17a53 2022-06-13 thomas nscroll /= 2;
7503 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7504 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7505 6458efa5 2020-11-24 stsp case CTRL('f'):
7506 1c5e5faa 2022-06-23 thomas case 'f':
7507 4c2d69cb 2022-06-23 thomas case ' ':
7508 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7509 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7510 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7511 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7512 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7513 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7514 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7515 07b0611c 2022-06-23 thomas view->count = 0;
7516 6458efa5 2020-11-24 stsp break;
7517 6458efa5 2020-11-24 stsp }
7518 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7519 6458efa5 2020-11-24 stsp break;
7520 6458efa5 2020-11-24 stsp case CTRL('l'):
7521 07b0611c 2022-06-23 thomas view->count = 0;
7522 8924d611 2020-12-26 stsp tog_free_refs();
7523 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7524 8924d611 2020-12-26 stsp if (err)
7525 8924d611 2020-12-26 stsp break;
7526 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7527 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7528 6458efa5 2020-11-24 stsp break;
7529 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7530 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7531 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7532 6458efa5 2020-11-24 stsp break;
7533 6458efa5 2020-11-24 stsp default:
7534 07b0611c 2022-06-23 thomas view->count = 0;
7535 6458efa5 2020-11-24 stsp break;
7536 6458efa5 2020-11-24 stsp }
7537 6458efa5 2020-11-24 stsp
7538 6458efa5 2020-11-24 stsp return err;
7539 6458efa5 2020-11-24 stsp }
7540 6458efa5 2020-11-24 stsp
7541 6458efa5 2020-11-24 stsp __dead static void
7542 6458efa5 2020-11-24 stsp usage_ref(void)
7543 6458efa5 2020-11-24 stsp {
7544 6458efa5 2020-11-24 stsp endwin();
7545 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7546 6458efa5 2020-11-24 stsp getprogname());
7547 6458efa5 2020-11-24 stsp exit(1);
7548 6458efa5 2020-11-24 stsp }
7549 6458efa5 2020-11-24 stsp
7550 6458efa5 2020-11-24 stsp static const struct got_error *
7551 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7552 6458efa5 2020-11-24 stsp {
7553 6458efa5 2020-11-24 stsp const struct got_error *error;
7554 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7555 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7556 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7557 6458efa5 2020-11-24 stsp int ch;
7558 6458efa5 2020-11-24 stsp struct tog_view *view;
7559 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7560 6458efa5 2020-11-24 stsp
7561 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7562 6458efa5 2020-11-24 stsp switch (ch) {
7563 6458efa5 2020-11-24 stsp case 'r':
7564 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7565 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7566 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7567 6458efa5 2020-11-24 stsp optarg);
7568 6458efa5 2020-11-24 stsp break;
7569 6458efa5 2020-11-24 stsp default:
7570 e99e2d15 2020-11-24 naddy usage_ref();
7571 6458efa5 2020-11-24 stsp /* NOTREACHED */
7572 6458efa5 2020-11-24 stsp }
7573 6458efa5 2020-11-24 stsp }
7574 6458efa5 2020-11-24 stsp
7575 6458efa5 2020-11-24 stsp argc -= optind;
7576 6458efa5 2020-11-24 stsp argv += optind;
7577 6458efa5 2020-11-24 stsp
7578 6458efa5 2020-11-24 stsp if (argc > 1)
7579 e99e2d15 2020-11-24 naddy usage_ref();
7580 7cd52833 2022-06-23 thomas
7581 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7582 7cd52833 2022-06-23 thomas if (error != NULL)
7583 7cd52833 2022-06-23 thomas goto done;
7584 6458efa5 2020-11-24 stsp
7585 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7586 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7587 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7588 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7589 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7590 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7591 c156c7a4 2020-12-18 stsp goto done;
7592 6458efa5 2020-11-24 stsp if (worktree)
7593 6458efa5 2020-11-24 stsp repo_path =
7594 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7595 6458efa5 2020-11-24 stsp else
7596 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7597 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7598 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7599 c156c7a4 2020-12-18 stsp goto done;
7600 c156c7a4 2020-12-18 stsp }
7601 6458efa5 2020-11-24 stsp }
7602 6458efa5 2020-11-24 stsp
7603 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7604 6458efa5 2020-11-24 stsp if (error != NULL)
7605 6458efa5 2020-11-24 stsp goto done;
7606 6458efa5 2020-11-24 stsp
7607 6458efa5 2020-11-24 stsp init_curses();
7608 6458efa5 2020-11-24 stsp
7609 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7610 51a10b52 2020-12-26 stsp if (error)
7611 51a10b52 2020-12-26 stsp goto done;
7612 51a10b52 2020-12-26 stsp
7613 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7614 6458efa5 2020-11-24 stsp if (error)
7615 6458efa5 2020-11-24 stsp goto done;
7616 6458efa5 2020-11-24 stsp
7617 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7618 6458efa5 2020-11-24 stsp if (view == NULL) {
7619 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7620 6458efa5 2020-11-24 stsp goto done;
7621 6458efa5 2020-11-24 stsp }
7622 6458efa5 2020-11-24 stsp
7623 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7624 6458efa5 2020-11-24 stsp if (error)
7625 6458efa5 2020-11-24 stsp goto done;
7626 6458efa5 2020-11-24 stsp
7627 6458efa5 2020-11-24 stsp if (worktree) {
7628 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7629 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7630 6458efa5 2020-11-24 stsp worktree = NULL;
7631 6458efa5 2020-11-24 stsp }
7632 6458efa5 2020-11-24 stsp error = view_loop(view);
7633 6458efa5 2020-11-24 stsp done:
7634 6458efa5 2020-11-24 stsp free(repo_path);
7635 6458efa5 2020-11-24 stsp free(cwd);
7636 1d0f4054 2021-06-17 stsp if (repo) {
7637 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7638 1d0f4054 2021-06-17 stsp if (close_err)
7639 1d0f4054 2021-06-17 stsp error = close_err;
7640 1d0f4054 2021-06-17 stsp }
7641 7cd52833 2022-06-23 thomas if (pack_fds) {
7642 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7643 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7644 7cd52833 2022-06-23 thomas if (error == NULL)
7645 7cd52833 2022-06-23 thomas error = pack_err;
7646 7cd52833 2022-06-23 thomas }
7647 51a10b52 2020-12-26 stsp tog_free_refs();
7648 6458efa5 2020-11-24 stsp return error;
7649 6458efa5 2020-11-24 stsp }
7650 6458efa5 2020-11-24 stsp
7651 a5d43cac 2022-07-01 thomas /*
7652 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
7653 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
7654 a5d43cac 2022-07-01 thomas */
7655 6458efa5 2020-11-24 stsp static void
7656 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
7657 a5d43cac 2022-07-01 thomas {
7658 a5d43cac 2022-07-01 thomas switch (view->type) {
7659 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7660 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7661 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
7662 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
7663 a5d43cac 2022-07-01 thomas 1);
7664 a5d43cac 2022-07-01 thomas break;
7665 a5d43cac 2022-07-01 thomas }
7666 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
7667 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
7668 a5d43cac 2022-07-01 thomas else
7669 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
7670 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
7671 a5d43cac 2022-07-01 thomas break;
7672 a5d43cac 2022-07-01 thomas }
7673 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
7674 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
7675 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
7676 a5d43cac 2022-07-01 thomas break;
7677 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
7678 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
7679 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
7680 a5d43cac 2022-07-01 thomas break;
7681 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
7682 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
7683 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
7684 a5d43cac 2022-07-01 thomas break;
7685 a5d43cac 2022-07-01 thomas default:
7686 a5d43cac 2022-07-01 thomas break;
7687 a5d43cac 2022-07-01 thomas }
7688 a5d43cac 2022-07-01 thomas
7689 a5d43cac 2022-07-01 thomas view->offset = 0;
7690 a5d43cac 2022-07-01 thomas }
7691 a5d43cac 2022-07-01 thomas
7692 a5d43cac 2022-07-01 thomas /*
7693 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
7694 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
7695 a5d43cac 2022-07-01 thomas */
7696 a5d43cac 2022-07-01 thomas static const struct got_error *
7697 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
7698 a5d43cac 2022-07-01 thomas {
7699 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
7700 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
7701 a5d43cac 2022-07-01 thomas int *selected = NULL;
7702 a5d43cac 2022-07-01 thomas int header, offset;
7703 a5d43cac 2022-07-01 thomas
7704 a5d43cac 2022-07-01 thomas switch (view->type) {
7705 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7706 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7707 a5d43cac 2022-07-01 thomas header = 3;
7708 a5d43cac 2022-07-01 thomas scrolld = NULL;
7709 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
7710 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
7711 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
7712 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
7713 a5d43cac 2022-07-01 thomas view->offset = offset;
7714 a5d43cac 2022-07-01 thomas }
7715 a5d43cac 2022-07-01 thomas break;
7716 a5d43cac 2022-07-01 thomas }
7717 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
7718 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
7719 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
7720 a5d43cac 2022-07-01 thomas header = 3;
7721 a5d43cac 2022-07-01 thomas selected = &s->selected;
7722 a5d43cac 2022-07-01 thomas break;
7723 a5d43cac 2022-07-01 thomas }
7724 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
7725 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7726 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
7727 a5d43cac 2022-07-01 thomas header = 3;
7728 a5d43cac 2022-07-01 thomas selected = &s->selected;
7729 a5d43cac 2022-07-01 thomas break;
7730 a5d43cac 2022-07-01 thomas }
7731 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
7732 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7733 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
7734 a5d43cac 2022-07-01 thomas header = 5;
7735 a5d43cac 2022-07-01 thomas selected = &s->selected;
7736 a5d43cac 2022-07-01 thomas break;
7737 a5d43cac 2022-07-01 thomas }
7738 a5d43cac 2022-07-01 thomas default:
7739 a5d43cac 2022-07-01 thomas selected = NULL;
7740 a5d43cac 2022-07-01 thomas scrolld = NULL;
7741 a5d43cac 2022-07-01 thomas header = 0;
7742 a5d43cac 2022-07-01 thomas break;
7743 a5d43cac 2022-07-01 thomas }
7744 a5d43cac 2022-07-01 thomas
7745 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
7746 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
7747 a5d43cac 2022-07-01 thomas view->offset = offset;
7748 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
7749 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
7750 a5d43cac 2022-07-01 thomas *selected -= offset;
7751 a5d43cac 2022-07-01 thomas }
7752 a5d43cac 2022-07-01 thomas }
7753 a5d43cac 2022-07-01 thomas
7754 a5d43cac 2022-07-01 thomas return err;
7755 a5d43cac 2022-07-01 thomas }
7756 a5d43cac 2022-07-01 thomas
7757 a5d43cac 2022-07-01 thomas static void
7758 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7759 ce5b7c56 2019-07-09 stsp {
7760 6059809a 2020-12-17 stsp size_t i;
7761 9f7d7167 2018-04-29 stsp
7762 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7763 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7764 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
7765 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7766 ce5b7c56 2019-07-09 stsp }
7767 6879ba42 2020-10-01 naddy fputc('\n', fp);
7768 ce5b7c56 2019-07-09 stsp }
7769 ce5b7c56 2019-07-09 stsp
7770 4ed7e80c 2018-05-20 stsp __dead static void
7771 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7772 9f7d7167 2018-04-29 stsp {
7773 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7774 6879ba42 2020-10-01 naddy
7775 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7776 6879ba42 2020-10-01 naddy getprogname());
7777 6879ba42 2020-10-01 naddy if (hflag) {
7778 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7779 6879ba42 2020-10-01 naddy list_commands(fp);
7780 ee85c5e8 2020-02-29 stsp }
7781 6879ba42 2020-10-01 naddy exit(status);
7782 9f7d7167 2018-04-29 stsp }
7783 9f7d7167 2018-04-29 stsp
7784 c2301be8 2018-04-30 stsp static char **
7785 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7786 c2301be8 2018-04-30 stsp {
7787 ee85c5e8 2020-02-29 stsp va_list ap;
7788 c2301be8 2018-04-30 stsp char **argv;
7789 ee85c5e8 2020-02-29 stsp int i;
7790 c2301be8 2018-04-30 stsp
7791 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7792 ee85c5e8 2020-02-29 stsp
7793 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7794 c2301be8 2018-04-30 stsp if (argv == NULL)
7795 c2301be8 2018-04-30 stsp err(1, "calloc");
7796 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7797 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7798 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7799 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7800 c2301be8 2018-04-30 stsp }
7801 c2301be8 2018-04-30 stsp
7802 ee85c5e8 2020-02-29 stsp va_end(ap);
7803 c2301be8 2018-04-30 stsp return argv;
7804 ee85c5e8 2020-02-29 stsp }
7805 ee85c5e8 2020-02-29 stsp
7806 ee85c5e8 2020-02-29 stsp /*
7807 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7808 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7809 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7810 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7811 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7812 ee85c5e8 2020-02-29 stsp */
7813 ee85c5e8 2020-02-29 stsp static const struct got_error *
7814 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7815 ee85c5e8 2020-02-29 stsp {
7816 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7817 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
7818 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7819 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7820 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7821 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7822 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7823 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7824 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7825 84de9106 2020-12-26 stsp
7826 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7827 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7828 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7829 ee85c5e8 2020-02-29 stsp
7830 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7831 7cd52833 2022-06-23 thomas if (error != NULL)
7832 7cd52833 2022-06-23 thomas goto done;
7833 7cd52833 2022-06-23 thomas
7834 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7835 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7836 ee85c5e8 2020-02-29 stsp goto done;
7837 ee85c5e8 2020-02-29 stsp
7838 ee85c5e8 2020-02-29 stsp if (worktree)
7839 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7840 ee85c5e8 2020-02-29 stsp else
7841 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7842 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7843 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7844 ee85c5e8 2020-02-29 stsp goto done;
7845 ee85c5e8 2020-02-29 stsp }
7846 ee85c5e8 2020-02-29 stsp
7847 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7848 ee85c5e8 2020-02-29 stsp if (error != NULL)
7849 ee85c5e8 2020-02-29 stsp goto done;
7850 ee85c5e8 2020-02-29 stsp
7851 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7852 ee85c5e8 2020-02-29 stsp repo, worktree);
7853 ee85c5e8 2020-02-29 stsp if (error)
7854 ee85c5e8 2020-02-29 stsp goto done;
7855 ee85c5e8 2020-02-29 stsp
7856 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7857 84de9106 2020-12-26 stsp if (error)
7858 84de9106 2020-12-26 stsp goto done;
7859 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7860 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7861 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7862 ee85c5e8 2020-02-29 stsp if (error)
7863 ee85c5e8 2020-02-29 stsp goto done;
7864 ee85c5e8 2020-02-29 stsp
7865 ee85c5e8 2020-02-29 stsp if (worktree) {
7866 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7867 ee85c5e8 2020-02-29 stsp worktree = NULL;
7868 ee85c5e8 2020-02-29 stsp }
7869 ee85c5e8 2020-02-29 stsp
7870 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7871 945f9229 2022-04-16 thomas if (error)
7872 945f9229 2022-04-16 thomas goto done;
7873 945f9229 2022-04-16 thomas
7874 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7875 ee85c5e8 2020-02-29 stsp if (error) {
7876 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7877 ee85c5e8 2020-02-29 stsp goto done;
7878 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7879 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7880 6879ba42 2020-10-01 naddy usage(1, 1);
7881 ee85c5e8 2020-02-29 stsp /* not reached */
7882 ee85c5e8 2020-02-29 stsp }
7883 ee85c5e8 2020-02-29 stsp
7884 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7885 1d0f4054 2021-06-17 stsp if (error == NULL)
7886 1d0f4054 2021-06-17 stsp error = close_err;
7887 ee85c5e8 2020-02-29 stsp repo = NULL;
7888 ee85c5e8 2020-02-29 stsp
7889 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7890 ee85c5e8 2020-02-29 stsp if (error)
7891 ee85c5e8 2020-02-29 stsp goto done;
7892 ee85c5e8 2020-02-29 stsp
7893 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7894 ee85c5e8 2020-02-29 stsp argc = 4;
7895 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7896 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7897 ee85c5e8 2020-02-29 stsp done:
7898 1d0f4054 2021-06-17 stsp if (repo) {
7899 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7900 1d0f4054 2021-06-17 stsp if (error == NULL)
7901 1d0f4054 2021-06-17 stsp error = close_err;
7902 1d0f4054 2021-06-17 stsp }
7903 945f9229 2022-04-16 thomas if (commit)
7904 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7905 ee85c5e8 2020-02-29 stsp if (worktree)
7906 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7907 7cd52833 2022-06-23 thomas if (pack_fds) {
7908 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7909 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7910 7cd52833 2022-06-23 thomas if (error == NULL)
7911 7cd52833 2022-06-23 thomas error = pack_err;
7912 7cd52833 2022-06-23 thomas }
7913 ee85c5e8 2020-02-29 stsp free(id);
7914 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7915 ee85c5e8 2020-02-29 stsp free(commit_id);
7916 ee85c5e8 2020-02-29 stsp free(cwd);
7917 ee85c5e8 2020-02-29 stsp free(repo_path);
7918 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7919 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7920 ee85c5e8 2020-02-29 stsp int i;
7921 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7922 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7923 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7924 ee85c5e8 2020-02-29 stsp }
7925 87670572 2020-12-26 naddy tog_free_refs();
7926 ee85c5e8 2020-02-29 stsp return error;
7927 c2301be8 2018-04-30 stsp }
7928 c2301be8 2018-04-30 stsp
7929 9f7d7167 2018-04-29 stsp int
7930 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7931 9f7d7167 2018-04-29 stsp {
7932 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7933 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
7934 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7935 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7936 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
7937 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7938 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7939 83cd27f8 2020-01-13 stsp };
7940 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
7941 9f7d7167 2018-04-29 stsp
7942 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7943 9f7d7167 2018-04-29 stsp
7944 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7945 9f7d7167 2018-04-29 stsp switch (ch) {
7946 9f7d7167 2018-04-29 stsp case 'h':
7947 9f7d7167 2018-04-29 stsp hflag = 1;
7948 9f7d7167 2018-04-29 stsp break;
7949 53ccebc2 2019-07-30 stsp case 'V':
7950 53ccebc2 2019-07-30 stsp Vflag = 1;
7951 53ccebc2 2019-07-30 stsp break;
7952 9f7d7167 2018-04-29 stsp default:
7953 6879ba42 2020-10-01 naddy usage(hflag, 1);
7954 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7955 9f7d7167 2018-04-29 stsp }
7956 9f7d7167 2018-04-29 stsp }
7957 9f7d7167 2018-04-29 stsp
7958 9f7d7167 2018-04-29 stsp argc -= optind;
7959 9f7d7167 2018-04-29 stsp argv += optind;
7960 9814e6a3 2020-09-27 naddy optind = 1;
7961 c2301be8 2018-04-30 stsp optreset = 1;
7962 9f7d7167 2018-04-29 stsp
7963 53ccebc2 2019-07-30 stsp if (Vflag) {
7964 53ccebc2 2019-07-30 stsp got_version_print_str();
7965 6879ba42 2020-10-01 naddy return 0;
7966 53ccebc2 2019-07-30 stsp }
7967 4010e238 2020-12-04 stsp
7968 4010e238 2020-12-04 stsp #ifndef PROFILE
7969 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7970 4010e238 2020-12-04 stsp NULL) == -1)
7971 4010e238 2020-12-04 stsp err(1, "pledge");
7972 4010e238 2020-12-04 stsp #endif
7973 53ccebc2 2019-07-30 stsp
7974 c2301be8 2018-04-30 stsp if (argc == 0) {
7975 f29d3e89 2018-06-23 stsp if (hflag)
7976 6879ba42 2020-10-01 naddy usage(hflag, 0);
7977 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7978 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7979 c2301be8 2018-04-30 stsp argc = 1;
7980 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7981 c2301be8 2018-04-30 stsp } else {
7982 6059809a 2020-12-17 stsp size_t i;
7983 9f7d7167 2018-04-29 stsp
7984 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7985 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7986 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7987 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7988 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7989 9f7d7167 2018-04-29 stsp break;
7990 9f7d7167 2018-04-29 stsp }
7991 9f7d7167 2018-04-29 stsp }
7992 ee85c5e8 2020-02-29 stsp }
7993 3642c4c6 2019-07-09 stsp
7994 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7995 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
7996 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
7997 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7998 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
7999 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8000 adf4c9e0 2022-07-03 thomas }
8001 adf4c9e0 2022-07-03 thomas
8002 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8003 ee85c5e8 2020-02-29 stsp if (argc != 1)
8004 6879ba42 2020-10-01 naddy usage(0, 1);
8005 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8006 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8007 ee85c5e8 2020-02-29 stsp } else {
8008 ee85c5e8 2020-02-29 stsp if (hflag)
8009 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8010 ee85c5e8 2020-02-29 stsp else
8011 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8012 9f7d7167 2018-04-29 stsp }
8013 9f7d7167 2018-04-29 stsp
8014 9f7d7167 2018-04-29 stsp endwin();
8015 b46c1e04 2020-09-20 naddy putchar('\n');
8016 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8017 a2f4a359 2020-02-28 stsp int i;
8018 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8019 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8020 a2f4a359 2020-02-28 stsp free(cmd_argv);
8021 a2f4a359 2020-02-28 stsp }
8022 a2f4a359 2020-02-28 stsp
8023 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8024 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8025 9f7d7167 2018-04-29 stsp return 0;
8026 9f7d7167 2018-04-29 stsp }