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 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
116 d6b05b5b 2018-08-04 stsp
117 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
118 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
119 ba4f502b 2018-08-04 stsp struct got_object_id *id;
120 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
121 1a76625f 2018-10-22 stsp int idx;
122 ba4f502b 2018-08-04 stsp };
123 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 ba4f502b 2018-08-04 stsp struct commit_queue {
125 ba4f502b 2018-08-04 stsp int ncommits;
126 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
127 6d17833f 2019-11-08 stsp };
128 6d17833f 2019-11-08 stsp
129 f26dddb7 2019-11-08 stsp struct tog_color {
130 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
131 6d17833f 2019-11-08 stsp regex_t regex;
132 6d17833f 2019-11-08 stsp short colorpair;
133 15a087fe 2019-02-21 stsp };
134 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
135 11b20872 2019-11-08 stsp
136 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
138 2183bbf6 2022-01-23 thomas
139 2183bbf6 2022-01-23 thomas static const struct got_error *
140 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 2183bbf6 2022-01-23 thomas struct got_reference* re2)
142 2183bbf6 2022-01-23 thomas {
143 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
144 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
145 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
146 2183bbf6 2022-01-23 thomas
147 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
148 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
151 2183bbf6 2022-01-23 thomas *cmp = -1;
152 2183bbf6 2022-01-23 thomas return NULL;
153 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
154 2183bbf6 2022-01-23 thomas *cmp = 1;
155 2183bbf6 2022-01-23 thomas return NULL;
156 2183bbf6 2022-01-23 thomas }
157 2183bbf6 2022-01-23 thomas
158 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas }
161 51a10b52 2020-12-26 stsp
162 11b20872 2019-11-08 stsp static const struct got_error *
163 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
164 51a10b52 2020-12-26 stsp {
165 51a10b52 2020-12-26 stsp const struct got_error *err;
166 51a10b52 2020-12-26 stsp
167 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
169 3bfadbd4 2021-11-20 thomas repo);
170 51a10b52 2020-12-26 stsp if (err)
171 51a10b52 2020-12-26 stsp return err;
172 51a10b52 2020-12-26 stsp
173 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
174 51a10b52 2020-12-26 stsp repo);
175 51a10b52 2020-12-26 stsp }
176 51a10b52 2020-12-26 stsp
177 51a10b52 2020-12-26 stsp static void
178 51a10b52 2020-12-26 stsp tog_free_refs(void)
179 51a10b52 2020-12-26 stsp {
180 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
181 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
182 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
183 51a10b52 2020-12-26 stsp }
184 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
185 51a10b52 2020-12-26 stsp }
186 51a10b52 2020-12-26 stsp
187 51a10b52 2020-12-26 stsp static const struct got_error *
188 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
189 11b20872 2019-11-08 stsp int idx, short color)
190 11b20872 2019-11-08 stsp {
191 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
192 11b20872 2019-11-08 stsp struct tog_color *tc;
193 11b20872 2019-11-08 stsp int regerr = 0;
194 11b20872 2019-11-08 stsp
195 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
196 11b20872 2019-11-08 stsp return NULL;
197 11b20872 2019-11-08 stsp
198 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
199 11b20872 2019-11-08 stsp
200 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
201 11b20872 2019-11-08 stsp if (tc == NULL)
202 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
203 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
204 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 11b20872 2019-11-08 stsp if (regerr) {
206 11b20872 2019-11-08 stsp static char regerr_msg[512];
207 11b20872 2019-11-08 stsp static char err_msg[512];
208 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
209 11b20872 2019-11-08 stsp sizeof(regerr_msg));
210 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 11b20872 2019-11-08 stsp regerr_msg);
212 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
213 11b20872 2019-11-08 stsp free(tc);
214 11b20872 2019-11-08 stsp return err;
215 11b20872 2019-11-08 stsp }
216 11b20872 2019-11-08 stsp tc->colorpair = idx;
217 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
218 11b20872 2019-11-08 stsp return NULL;
219 11b20872 2019-11-08 stsp }
220 11b20872 2019-11-08 stsp
221 11b20872 2019-11-08 stsp static void
222 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
223 11b20872 2019-11-08 stsp {
224 11b20872 2019-11-08 stsp struct tog_color *tc;
225 11b20872 2019-11-08 stsp
226 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
227 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
228 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
229 11b20872 2019-11-08 stsp regfree(&tc->regex);
230 11b20872 2019-11-08 stsp free(tc);
231 11b20872 2019-11-08 stsp }
232 11b20872 2019-11-08 stsp }
233 11b20872 2019-11-08 stsp
234 11b20872 2019-11-08 stsp struct tog_color *
235 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
236 11b20872 2019-11-08 stsp {
237 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
238 11b20872 2019-11-08 stsp
239 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
240 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
241 11b20872 2019-11-08 stsp return tc;
242 11b20872 2019-11-08 stsp }
243 11b20872 2019-11-08 stsp
244 11b20872 2019-11-08 stsp return NULL;
245 11b20872 2019-11-08 stsp }
246 11b20872 2019-11-08 stsp
247 11b20872 2019-11-08 stsp static int
248 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
249 11b20872 2019-11-08 stsp {
250 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_CYAN;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 11b20872 2019-11-08 stsp return COLOR_YELLOW;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 11b20872 2019-11-08 stsp return COLOR_GREEN;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_CYAN;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 11b20872 2019-11-08 stsp return COLOR_GREEN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 11b20872 2019-11-08 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 11b20872 2019-11-08 stsp return COLOR_YELLOW;
272 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 6458efa5 2020-11-24 stsp return COLOR_GREEN;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
278 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
280 11b20872 2019-11-08 stsp
281 11b20872 2019-11-08 stsp return -1;
282 11b20872 2019-11-08 stsp }
283 11b20872 2019-11-08 stsp
284 11b20872 2019-11-08 stsp static int
285 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
286 11b20872 2019-11-08 stsp {
287 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
288 11b20872 2019-11-08 stsp
289 11b20872 2019-11-08 stsp if (val == NULL)
290 11b20872 2019-11-08 stsp return default_color_value(envvar);
291 15a087fe 2019-02-21 stsp
292 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
293 11b20872 2019-11-08 stsp return COLOR_BLACK;
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
295 11b20872 2019-11-08 stsp return COLOR_RED;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
297 11b20872 2019-11-08 stsp return COLOR_GREEN;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
299 11b20872 2019-11-08 stsp return COLOR_YELLOW;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
301 11b20872 2019-11-08 stsp return COLOR_BLUE;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
303 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
305 11b20872 2019-11-08 stsp return COLOR_CYAN;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
307 11b20872 2019-11-08 stsp return COLOR_WHITE;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
309 11b20872 2019-11-08 stsp return -1;
310 11b20872 2019-11-08 stsp
311 11b20872 2019-11-08 stsp return default_color_value(envvar);
312 11b20872 2019-11-08 stsp }
313 11b20872 2019-11-08 stsp
314 11b20872 2019-11-08 stsp
315 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
316 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
317 3dbaef42 2020-11-24 stsp const char *label1, *label2;
318 15a087fe 2019-02-21 stsp FILE *f;
319 15a087fe 2019-02-21 stsp int first_displayed_line;
320 15a087fe 2019-02-21 stsp int last_displayed_line;
321 15a087fe 2019-02-21 stsp int eof;
322 15a087fe 2019-02-21 stsp int diff_context;
323 3dbaef42 2020-11-24 stsp int ignore_whitespace;
324 64453f7e 2020-11-21 stsp int force_text_diff;
325 15a087fe 2019-02-21 stsp struct got_repository *repo;
326 bddb1296 2019-11-08 stsp struct tog_colors colors;
327 fe621944 2020-11-10 stsp size_t nlines;
328 f44b1f58 2020-02-02 tracey off_t *line_offsets;
329 f44b1f58 2020-02-02 tracey int matched_line;
330 f44b1f58 2020-02-02 tracey int selected_line;
331 15a087fe 2019-02-21 stsp
332 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
333 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
334 b01e7d3b 2018-08-04 stsp };
335 b01e7d3b 2018-08-04 stsp
336 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
337 1a76625f 2018-10-22 stsp
338 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
339 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
340 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
341 1a76625f 2018-10-22 stsp int commits_needed;
342 fb280deb 2021-08-30 stsp int load_all;
343 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
344 1a76625f 2018-10-22 stsp struct commit_queue *commits;
345 1a76625f 2018-10-22 stsp const char *in_repo_path;
346 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
347 1a76625f 2018-10-22 stsp struct got_repository *repo;
348 1a76625f 2018-10-22 stsp int log_complete;
349 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
350 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
351 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
352 13add988 2019-10-15 stsp int *searching;
353 13add988 2019-10-15 stsp int *search_next_done;
354 13add988 2019-10-15 stsp regex_t *regex;
355 1a76625f 2018-10-22 stsp };
356 1a76625f 2018-10-22 stsp
357 1a76625f 2018-10-22 stsp struct tog_log_view_state {
358 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
359 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
360 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
361 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
362 b01e7d3b 2018-08-04 stsp int selected;
363 b01e7d3b 2018-08-04 stsp char *in_repo_path;
364 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
365 b672a97a 2020-01-27 stsp int log_branches;
366 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
367 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp sig_atomic_t quit;
369 1a76625f 2018-10-22 stsp pthread_t thread;
370 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
371 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
372 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
373 11b20872 2019-11-08 stsp struct tog_colors colors;
374 ba4f502b 2018-08-04 stsp };
375 11b20872 2019-11-08 stsp
376 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
377 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
378 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
380 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
381 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
382 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
384 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
385 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
386 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
387 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
388 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
389 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
390 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
391 ba4f502b 2018-08-04 stsp
392 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
393 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
394 e9424729 2018-08-04 stsp int nlines;
395 e9424729 2018-08-04 stsp
396 e9424729 2018-08-04 stsp struct tog_view *view;
397 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
398 e9424729 2018-08-04 stsp int *quit;
399 e9424729 2018-08-04 stsp };
400 e9424729 2018-08-04 stsp
401 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
402 e9424729 2018-08-04 stsp const char *path;
403 e9424729 2018-08-04 stsp struct got_repository *repo;
404 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
405 e9424729 2018-08-04 stsp int *complete;
406 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
407 fc06ba56 2019-08-22 stsp void *cancel_arg;
408 e9424729 2018-08-04 stsp };
409 e9424729 2018-08-04 stsp
410 e9424729 2018-08-04 stsp struct tog_blame {
411 e9424729 2018-08-04 stsp FILE *f;
412 be659d10 2020-11-18 stsp off_t filesize;
413 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
414 6fcac457 2018-11-19 stsp int nlines;
415 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
416 e9424729 2018-08-04 stsp pthread_t thread;
417 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
418 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
419 e9424729 2018-08-04 stsp const char *path;
420 e9424729 2018-08-04 stsp };
421 e9424729 2018-08-04 stsp
422 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
423 7cbe629d 2018-08-04 stsp int first_displayed_line;
424 7cbe629d 2018-08-04 stsp int last_displayed_line;
425 7cbe629d 2018-08-04 stsp int selected_line;
426 7cbe629d 2018-08-04 stsp int blame_complete;
427 e5a0f69f 2018-08-18 stsp int eof;
428 e5a0f69f 2018-08-18 stsp int done;
429 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
430 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
431 e5a0f69f 2018-08-18 stsp char *path;
432 7cbe629d 2018-08-04 stsp struct got_repository *repo;
433 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
434 e9424729 2018-08-04 stsp struct tog_blame blame;
435 6c4c42e0 2019-06-24 stsp int matched_line;
436 11b20872 2019-11-08 stsp struct tog_colors colors;
437 ad80ab7b 2018-08-04 stsp };
438 ad80ab7b 2018-08-04 stsp
439 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
440 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
441 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
442 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
443 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
444 ad80ab7b 2018-08-04 stsp int selected;
445 ad80ab7b 2018-08-04 stsp };
446 ad80ab7b 2018-08-04 stsp
447 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
448 ad80ab7b 2018-08-04 stsp
449 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
450 ad80ab7b 2018-08-04 stsp char *tree_label;
451 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
452 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
453 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
454 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
458 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
459 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
460 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
461 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
462 6458efa5 2020-11-24 stsp struct tog_colors colors;
463 6458efa5 2020-11-24 stsp };
464 6458efa5 2020-11-24 stsp
465 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
466 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
467 6458efa5 2020-11-24 stsp struct got_reference *ref;
468 6458efa5 2020-11-24 stsp int idx;
469 6458efa5 2020-11-24 stsp };
470 6458efa5 2020-11-24 stsp
471 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
472 6458efa5 2020-11-24 stsp
473 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
474 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
475 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
476 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
478 3bfadbd4 2021-11-20 thomas int nrefs, ndisplayed, selected, show_ids, sort_by_date;
479 6458efa5 2020-11-24 stsp struct got_repository *repo;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
481 bddb1296 2019-11-08 stsp struct tog_colors colors;
482 7cbe629d 2018-08-04 stsp };
483 7cbe629d 2018-08-04 stsp
484 669b5ffa 2018-10-07 stsp /*
485 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
486 669b5ffa 2018-10-07 stsp *
487 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
488 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
489 669b5ffa 2018-10-07 stsp * there is enough screen estate.
490 669b5ffa 2018-10-07 stsp *
491 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
492 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
493 669b5ffa 2018-10-07 stsp *
494 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
495 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
496 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
497 669b5ffa 2018-10-07 stsp *
498 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
499 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
500 669b5ffa 2018-10-07 stsp */
501 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
502 669b5ffa 2018-10-07 stsp
503 cc3c9aac 2018-08-01 stsp struct tog_view {
504 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
505 26ed57b2 2018-05-19 stsp WINDOW *window;
506 26ed57b2 2018-05-19 stsp PANEL *panel;
507 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
508 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
509 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
510 9970f7fc 2020-12-03 stsp int dying;
511 669b5ffa 2018-10-07 stsp struct tog_view *parent;
512 669b5ffa 2018-10-07 stsp struct tog_view *child;
513 5dc9f4bc 2018-08-04 stsp
514 e78dc838 2020-12-04 stsp /*
515 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
516 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
517 e78dc838 2020-12-04 stsp * between parent and child.
518 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
519 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
520 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
521 e78dc838 2020-12-04 stsp * situations.
522 e78dc838 2020-12-04 stsp */
523 e78dc838 2020-12-04 stsp int focus_child;
524 e78dc838 2020-12-04 stsp
525 5dc9f4bc 2018-08-04 stsp /* type-specific state */
526 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
527 5dc9f4bc 2018-08-04 stsp union {
528 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
529 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
530 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
531 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
532 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
533 5dc9f4bc 2018-08-04 stsp } state;
534 e5a0f69f 2018-08-18 stsp
535 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
536 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
537 e78dc838 2020-12-04 stsp struct tog_view *, int);
538 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
539 60493ae3 2019-06-20 stsp
540 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
541 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
542 c0c4acc8 2021-01-24 stsp int search_started;
543 60493ae3 2019-06-20 stsp int searching;
544 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
545 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
546 60493ae3 2019-06-20 stsp int search_next_done;
547 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
548 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
549 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
550 1803e47f 2019-06-22 stsp regex_t regex;
551 41605754 2020-11-12 stsp regmatch_t regmatch;
552 cc3c9aac 2018-08-01 stsp };
553 cd0acaa7 2018-05-20 stsp
554 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
555 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
556 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
557 78756c87 2020-11-24 stsp struct got_repository *);
558 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
559 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
560 e78dc838 2020-12-04 stsp struct tog_view *, int);
561 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
562 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
563 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
564 e5a0f69f 2018-08-18 stsp
565 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
566 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
567 78756c87 2020-11-24 stsp const char *, const char *, int);
568 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
569 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
570 e78dc838 2020-12-04 stsp struct tog_view *, int);
571 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
572 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
573 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
574 e5a0f69f 2018-08-18 stsp
575 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
576 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
577 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
579 e78dc838 2020-12-04 stsp struct tog_view *, int);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
581 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
582 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp
584 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
585 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
586 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_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_tree_view(struct tog_view *);
590 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
591 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
592 6458efa5 2020-11-24 stsp
593 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
594 6458efa5 2020-11-24 stsp struct got_repository *);
595 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
596 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
597 e78dc838 2020-12-04 stsp struct tog_view *, int);
598 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
599 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
600 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
601 25791caa 2018-10-24 stsp
602 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
603 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
604 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
605 25791caa 2018-10-24 stsp
606 25791caa 2018-10-24 stsp static void
607 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
608 25791caa 2018-10-24 stsp {
609 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
610 83baff54 2019-08-12 stsp }
611 83baff54 2019-08-12 stsp
612 83baff54 2019-08-12 stsp static void
613 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
614 83baff54 2019-08-12 stsp {
615 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
616 25791caa 2018-10-24 stsp }
617 26ed57b2 2018-05-19 stsp
618 61266923 2020-01-14 stsp static void
619 61266923 2020-01-14 stsp tog_sigcont(int signo)
620 61266923 2020-01-14 stsp {
621 61266923 2020-01-14 stsp tog_sigcont_received = 1;
622 61266923 2020-01-14 stsp }
623 61266923 2020-01-14 stsp
624 e5a0f69f 2018-08-18 stsp static const struct got_error *
625 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
626 ea5e7bb5 2018-08-01 stsp {
627 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
628 e5a0f69f 2018-08-18 stsp
629 669b5ffa 2018-10-07 stsp if (view->child) {
630 669b5ffa 2018-10-07 stsp view_close(view->child);
631 669b5ffa 2018-10-07 stsp view->child = NULL;
632 669b5ffa 2018-10-07 stsp }
633 e5a0f69f 2018-08-18 stsp if (view->close)
634 e5a0f69f 2018-08-18 stsp err = view->close(view);
635 ea5e7bb5 2018-08-01 stsp if (view->panel)
636 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
637 ea5e7bb5 2018-08-01 stsp if (view->window)
638 ea5e7bb5 2018-08-01 stsp delwin(view->window);
639 ea5e7bb5 2018-08-01 stsp free(view);
640 e5a0f69f 2018-08-18 stsp return err;
641 ea5e7bb5 2018-08-01 stsp }
642 ea5e7bb5 2018-08-01 stsp
643 ea5e7bb5 2018-08-01 stsp static struct tog_view *
644 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
645 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
646 ea5e7bb5 2018-08-01 stsp {
647 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
648 ea5e7bb5 2018-08-01 stsp
649 ea5e7bb5 2018-08-01 stsp if (view == NULL)
650 ea5e7bb5 2018-08-01 stsp return NULL;
651 ea5e7bb5 2018-08-01 stsp
652 d6b05b5b 2018-08-04 stsp view->type = type;
653 f7d12f7e 2018-08-01 stsp view->lines = LINES;
654 f7d12f7e 2018-08-01 stsp view->cols = COLS;
655 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
656 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
657 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
658 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
659 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
660 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
661 96a765a8 2018-08-04 stsp view_close(view);
662 ea5e7bb5 2018-08-01 stsp return NULL;
663 ea5e7bb5 2018-08-01 stsp }
664 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
665 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
666 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
667 96a765a8 2018-08-04 stsp view_close(view);
668 ea5e7bb5 2018-08-01 stsp return NULL;
669 ea5e7bb5 2018-08-01 stsp }
670 ea5e7bb5 2018-08-01 stsp
671 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
672 ea5e7bb5 2018-08-01 stsp return view;
673 cdf1ee82 2018-08-01 stsp }
674 cdf1ee82 2018-08-01 stsp
675 0cf4efb1 2018-09-29 stsp static int
676 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
677 0cf4efb1 2018-09-29 stsp {
678 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
679 0cf4efb1 2018-09-29 stsp return 0;
680 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
681 5c60c32a 2018-10-18 stsp }
682 5c60c32a 2018-10-18 stsp
683 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
684 5c60c32a 2018-10-18 stsp
685 5c60c32a 2018-10-18 stsp static const struct got_error *
686 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
687 5c60c32a 2018-10-18 stsp {
688 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
689 5c60c32a 2018-10-18 stsp
690 5c60c32a 2018-10-18 stsp view->begin_y = 0;
691 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
692 5c60c32a 2018-10-18 stsp view->nlines = LINES;
693 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
694 5c60c32a 2018-10-18 stsp view->lines = LINES;
695 5c60c32a 2018-10-18 stsp view->cols = COLS;
696 5c60c32a 2018-10-18 stsp err = view_resize(view);
697 5c60c32a 2018-10-18 stsp if (err)
698 5c60c32a 2018-10-18 stsp return err;
699 5c60c32a 2018-10-18 stsp
700 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
701 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
702 5c60c32a 2018-10-18 stsp
703 5c60c32a 2018-10-18 stsp return NULL;
704 5c60c32a 2018-10-18 stsp }
705 5c60c32a 2018-10-18 stsp
706 5c60c32a 2018-10-18 stsp static const struct got_error *
707 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
708 5c60c32a 2018-10-18 stsp {
709 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
710 5c60c32a 2018-10-18 stsp
711 5c60c32a 2018-10-18 stsp view->begin_x = 0;
712 5c60c32a 2018-10-18 stsp view->begin_y = 0;
713 5c60c32a 2018-10-18 stsp view->nlines = LINES;
714 5c60c32a 2018-10-18 stsp view->ncols = COLS;
715 5c60c32a 2018-10-18 stsp view->lines = LINES;
716 5c60c32a 2018-10-18 stsp view->cols = COLS;
717 5c60c32a 2018-10-18 stsp err = view_resize(view);
718 5c60c32a 2018-10-18 stsp if (err)
719 5c60c32a 2018-10-18 stsp return err;
720 5c60c32a 2018-10-18 stsp
721 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
722 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
723 5c60c32a 2018-10-18 stsp
724 5c60c32a 2018-10-18 stsp return NULL;
725 0cf4efb1 2018-09-29 stsp }
726 0cf4efb1 2018-09-29 stsp
727 5c60c32a 2018-10-18 stsp static int
728 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
729 5c60c32a 2018-10-18 stsp {
730 5c60c32a 2018-10-18 stsp return view->parent == NULL;
731 5c60c32a 2018-10-18 stsp }
732 5c60c32a 2018-10-18 stsp
733 4d8c2215 2018-08-19 stsp static const struct got_error *
734 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
735 f7d12f7e 2018-08-01 stsp {
736 f7d12f7e 2018-08-01 stsp int nlines, ncols;
737 f7d12f7e 2018-08-01 stsp
738 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
739 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
740 0cf4efb1 2018-09-29 stsp else
741 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
742 f7d12f7e 2018-08-01 stsp
743 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
744 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
745 0cf4efb1 2018-09-29 stsp else
746 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
747 f7d12f7e 2018-08-01 stsp
748 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
749 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
750 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
751 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
752 25791caa 2018-10-24 stsp wclear(view->window);
753 f7d12f7e 2018-08-01 stsp
754 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
755 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
756 0cf4efb1 2018-09-29 stsp view->lines = LINES;
757 0cf4efb1 2018-09-29 stsp view->cols = COLS;
758 6d0fee91 2018-08-01 stsp
759 6e3e5d9c 2018-10-18 stsp if (view->child) {
760 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
761 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
762 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
763 5c60c32a 2018-10-18 stsp if (view->child->focussed)
764 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
765 5c60c32a 2018-10-18 stsp else
766 5c60c32a 2018-10-18 stsp show_panel(view->panel);
767 5c60c32a 2018-10-18 stsp } else {
768 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
769 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
770 5c60c32a 2018-10-18 stsp }
771 5c60c32a 2018-10-18 stsp }
772 669b5ffa 2018-10-07 stsp
773 5c60c32a 2018-10-18 stsp return NULL;
774 669b5ffa 2018-10-07 stsp }
775 669b5ffa 2018-10-07 stsp
776 669b5ffa 2018-10-07 stsp static const struct got_error *
777 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
778 669b5ffa 2018-10-07 stsp {
779 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
780 669b5ffa 2018-10-07 stsp
781 669b5ffa 2018-10-07 stsp if (view->child == NULL)
782 669b5ffa 2018-10-07 stsp return NULL;
783 669b5ffa 2018-10-07 stsp
784 669b5ffa 2018-10-07 stsp err = view_close(view->child);
785 669b5ffa 2018-10-07 stsp view->child = NULL;
786 669b5ffa 2018-10-07 stsp return err;
787 669b5ffa 2018-10-07 stsp }
788 669b5ffa 2018-10-07 stsp
789 72a9cb46 2020-12-03 stsp static void
790 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
791 669b5ffa 2018-10-07 stsp {
792 669b5ffa 2018-10-07 stsp view->child = child;
793 669b5ffa 2018-10-07 stsp child->parent = view;
794 bfddd0d9 2018-09-29 stsp }
795 bfddd0d9 2018-09-29 stsp
796 bfddd0d9 2018-09-29 stsp static int
797 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
798 bfddd0d9 2018-09-29 stsp {
799 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
800 34bc9ec9 2019-02-22 stsp }
801 34bc9ec9 2019-02-22 stsp
802 34bc9ec9 2019-02-22 stsp static void
803 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
804 25791caa 2018-10-24 stsp {
805 25791caa 2018-10-24 stsp int cols, lines;
806 25791caa 2018-10-24 stsp struct winsize size;
807 25791caa 2018-10-24 stsp
808 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
809 25791caa 2018-10-24 stsp cols = 80; /* Default */
810 25791caa 2018-10-24 stsp lines = 24;
811 25791caa 2018-10-24 stsp } else {
812 25791caa 2018-10-24 stsp cols = size.ws_col;
813 25791caa 2018-10-24 stsp lines = size.ws_row;
814 25791caa 2018-10-24 stsp }
815 25791caa 2018-10-24 stsp resize_term(lines, cols);
816 2b49a8ae 2019-06-22 stsp }
817 2b49a8ae 2019-06-22 stsp
818 2b49a8ae 2019-06-22 stsp static const struct got_error *
819 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
820 2b49a8ae 2019-06-22 stsp {
821 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
822 2b49a8ae 2019-06-22 stsp char pattern[1024];
823 2b49a8ae 2019-06-22 stsp int ret;
824 c0c4acc8 2021-01-24 stsp
825 c0c4acc8 2021-01-24 stsp if (view->search_started) {
826 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
827 c0c4acc8 2021-01-24 stsp view->searching = 0;
828 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
829 c0c4acc8 2021-01-24 stsp }
830 c0c4acc8 2021-01-24 stsp view->search_started = 0;
831 2b49a8ae 2019-06-22 stsp
832 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
833 2b49a8ae 2019-06-22 stsp return NULL;
834 2b49a8ae 2019-06-22 stsp
835 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
836 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
837 2b49a8ae 2019-06-22 stsp
838 2b49a8ae 2019-06-22 stsp nocbreak();
839 2b49a8ae 2019-06-22 stsp echo();
840 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
841 2b49a8ae 2019-06-22 stsp cbreak();
842 2b49a8ae 2019-06-22 stsp noecho();
843 2b49a8ae 2019-06-22 stsp if (ret == ERR)
844 2b49a8ae 2019-06-22 stsp return NULL;
845 2b49a8ae 2019-06-22 stsp
846 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
847 7c32bd05 2019-06-22 stsp err = view->search_start(view);
848 7c32bd05 2019-06-22 stsp if (err) {
849 7c32bd05 2019-06-22 stsp regfree(&view->regex);
850 7c32bd05 2019-06-22 stsp return err;
851 7c32bd05 2019-06-22 stsp }
852 c0c4acc8 2021-01-24 stsp view->search_started = 1;
853 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
854 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
855 2b49a8ae 2019-06-22 stsp view->search_next(view);
856 2b49a8ae 2019-06-22 stsp }
857 2b49a8ae 2019-06-22 stsp
858 2b49a8ae 2019-06-22 stsp return NULL;
859 0cf4efb1 2018-09-29 stsp }
860 6d0fee91 2018-08-01 stsp
861 0cf4efb1 2018-09-29 stsp static const struct got_error *
862 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
863 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
864 e5a0f69f 2018-08-18 stsp {
865 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
866 669b5ffa 2018-10-07 stsp struct tog_view *v;
867 1a76625f 2018-10-22 stsp int ch, errcode;
868 e5a0f69f 2018-08-18 stsp
869 e5a0f69f 2018-08-18 stsp *new = NULL;
870 8f4ed634 2020-03-26 stsp
871 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
872 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
873 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
874 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
875 e5a0f69f 2018-08-18 stsp
876 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
877 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
878 82954512 2020-02-03 stsp if (errcode)
879 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
880 82954512 2020-02-03 stsp "pthread_mutex_unlock");
881 3da8ef85 2021-09-21 stsp sched_yield();
882 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
883 82954512 2020-02-03 stsp if (errcode)
884 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
885 82954512 2020-02-03 stsp "pthread_mutex_lock");
886 60493ae3 2019-06-20 stsp view->search_next(view);
887 60493ae3 2019-06-20 stsp return NULL;
888 60493ae3 2019-06-20 stsp }
889 60493ae3 2019-06-20 stsp
890 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
891 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
892 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
893 1a76625f 2018-10-22 stsp if (errcode)
894 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
895 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
896 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
897 1a76625f 2018-10-22 stsp if (errcode)
898 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
899 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
900 25791caa 2018-10-24 stsp
901 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
902 25791caa 2018-10-24 stsp tog_resizeterm();
903 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
904 61266923 2020-01-14 stsp tog_sigcont_received = 0;
905 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
906 25791caa 2018-10-24 stsp err = view_resize(v);
907 25791caa 2018-10-24 stsp if (err)
908 25791caa 2018-10-24 stsp return err;
909 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
910 25791caa 2018-10-24 stsp if (err)
911 25791caa 2018-10-24 stsp return err;
912 cdfcfb03 2020-12-06 stsp if (v->child) {
913 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
914 cdfcfb03 2020-12-06 stsp if (err)
915 cdfcfb03 2020-12-06 stsp return err;
916 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
917 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
918 cdfcfb03 2020-12-06 stsp if (err)
919 cdfcfb03 2020-12-06 stsp return err;
920 cdfcfb03 2020-12-06 stsp }
921 25791caa 2018-10-24 stsp }
922 25791caa 2018-10-24 stsp }
923 25791caa 2018-10-24 stsp
924 e5a0f69f 2018-08-18 stsp switch (ch) {
925 1e37a5c2 2019-05-12 jcs case '\t':
926 1e37a5c2 2019-05-12 jcs if (view->child) {
927 e78dc838 2020-12-04 stsp view->focussed = 0;
928 e78dc838 2020-12-04 stsp view->child->focussed = 1;
929 e78dc838 2020-12-04 stsp view->focus_child = 1;
930 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
931 e78dc838 2020-12-04 stsp view->focussed = 0;
932 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
933 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
934 1e37a5c2 2019-05-12 jcs }
935 1e37a5c2 2019-05-12 jcs break;
936 1e37a5c2 2019-05-12 jcs case 'q':
937 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
938 9970f7fc 2020-12-03 stsp view->dying = 1;
939 1e37a5c2 2019-05-12 jcs break;
940 1e37a5c2 2019-05-12 jcs case 'Q':
941 1e37a5c2 2019-05-12 jcs *done = 1;
942 1e37a5c2 2019-05-12 jcs break;
943 1e37a5c2 2019-05-12 jcs case 'f':
944 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
945 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
946 1e37a5c2 2019-05-12 jcs break;
947 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
948 e78dc838 2020-12-04 stsp view->focussed = 0;
949 e78dc838 2020-12-04 stsp view->child->focussed = 1;
950 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
951 1e37a5c2 2019-05-12 jcs } else
952 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
953 1e37a5c2 2019-05-12 jcs if (err)
954 1e37a5c2 2019-05-12 jcs break;
955 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
956 9970f7fc 2020-12-03 stsp KEY_RESIZE);
957 1e37a5c2 2019-05-12 jcs } else {
958 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
959 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
960 e78dc838 2020-12-04 stsp view->focussed = 1;
961 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
962 1e37a5c2 2019-05-12 jcs } else {
963 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
964 669b5ffa 2018-10-07 stsp }
965 1e37a5c2 2019-05-12 jcs if (err)
966 1e37a5c2 2019-05-12 jcs break;
967 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
968 1e37a5c2 2019-05-12 jcs }
969 1e37a5c2 2019-05-12 jcs break;
970 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
971 60493ae3 2019-06-20 stsp break;
972 60493ae3 2019-06-20 stsp case '/':
973 60493ae3 2019-06-20 stsp if (view->search_start)
974 2b49a8ae 2019-06-22 stsp view_search_start(view);
975 60493ae3 2019-06-20 stsp else
976 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
977 1e37a5c2 2019-05-12 jcs break;
978 b1bf1435 2019-06-21 stsp case 'N':
979 60493ae3 2019-06-20 stsp case 'n':
980 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
981 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
982 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
983 60493ae3 2019-06-20 stsp view->search_next_done = 0;
984 60493ae3 2019-06-20 stsp view->search_next(view);
985 60493ae3 2019-06-20 stsp } else
986 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
987 60493ae3 2019-06-20 stsp break;
988 1e37a5c2 2019-05-12 jcs default:
989 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
990 1e37a5c2 2019-05-12 jcs break;
991 e5a0f69f 2018-08-18 stsp }
992 e5a0f69f 2018-08-18 stsp
993 e5a0f69f 2018-08-18 stsp return err;
994 e5a0f69f 2018-08-18 stsp }
995 e5a0f69f 2018-08-18 stsp
996 1a57306a 2018-09-02 stsp void
997 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
998 1a57306a 2018-09-02 stsp {
999 0cf4efb1 2018-09-29 stsp PANEL *panel;
1000 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1001 0cf4efb1 2018-09-29 stsp
1002 669b5ffa 2018-10-07 stsp if (view->parent)
1003 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1004 669b5ffa 2018-10-07 stsp
1005 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1006 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1007 1a57306a 2018-09-02 stsp return;
1008 1a57306a 2018-09-02 stsp
1009 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1010 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1011 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1012 bcbd79e2 2018-08-19 stsp }
1013 bcbd79e2 2018-08-19 stsp
1014 a3404814 2018-09-02 stsp int
1015 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1016 a3404814 2018-09-02 stsp {
1017 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1018 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1019 669b5ffa 2018-10-07 stsp return 0;
1020 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1021 669b5ffa 2018-10-07 stsp return 0;
1022 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1023 a3404814 2018-09-02 stsp return 0;
1024 a3404814 2018-09-02 stsp
1025 669b5ffa 2018-10-07 stsp return view->focussed;
1026 a3404814 2018-09-02 stsp }
1027 a3404814 2018-09-02 stsp
1028 bcbd79e2 2018-08-19 stsp static const struct got_error *
1029 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1030 e5a0f69f 2018-08-18 stsp {
1031 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1032 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1033 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1034 fd823528 2018-10-22 stsp int fast_refresh = 10;
1035 1a76625f 2018-10-22 stsp int done = 0, errcode;
1036 e5a0f69f 2018-08-18 stsp
1037 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1038 1a76625f 2018-10-22 stsp if (errcode)
1039 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1040 1a76625f 2018-10-22 stsp
1041 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1042 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1043 e5a0f69f 2018-08-18 stsp
1044 1004088d 2018-09-29 stsp view->focussed = 1;
1045 878940b7 2018-09-29 stsp err = view->show(view);
1046 0cf4efb1 2018-09-29 stsp if (err)
1047 0cf4efb1 2018-09-29 stsp return err;
1048 0cf4efb1 2018-09-29 stsp update_panels();
1049 0cf4efb1 2018-09-29 stsp doupdate();
1050 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1051 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1052 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1053 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1054 fd823528 2018-10-22 stsp
1055 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1056 e5a0f69f 2018-08-18 stsp if (err)
1057 e5a0f69f 2018-08-18 stsp break;
1058 9970f7fc 2020-12-03 stsp if (view->dying) {
1059 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1060 669b5ffa 2018-10-07 stsp
1061 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1062 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1063 9970f7fc 2020-12-03 stsp entry);
1064 e78dc838 2020-12-04 stsp else if (view->parent)
1065 669b5ffa 2018-10-07 stsp prev = view->parent;
1066 669b5ffa 2018-10-07 stsp
1067 e78dc838 2020-12-04 stsp if (view->parent) {
1068 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1069 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1070 e78dc838 2020-12-04 stsp } else
1071 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1072 669b5ffa 2018-10-07 stsp
1073 9970f7fc 2020-12-03 stsp err = view_close(view);
1074 fb59748f 2020-12-05 stsp if (err)
1075 e5a0f69f 2018-08-18 stsp goto done;
1076 669b5ffa 2018-10-07 stsp
1077 e78dc838 2020-12-04 stsp view = NULL;
1078 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1079 e78dc838 2020-12-04 stsp if (v->focussed)
1080 e78dc838 2020-12-04 stsp break;
1081 0cf4efb1 2018-09-29 stsp }
1082 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1083 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1084 e78dc838 2020-12-04 stsp if (prev)
1085 e78dc838 2020-12-04 stsp view = prev;
1086 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1087 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1088 e78dc838 2020-12-04 stsp tog_view_list_head);
1089 e78dc838 2020-12-04 stsp }
1090 e78dc838 2020-12-04 stsp if (view) {
1091 e78dc838 2020-12-04 stsp if (view->focus_child) {
1092 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1093 e78dc838 2020-12-04 stsp view = view->child;
1094 e78dc838 2020-12-04 stsp } else
1095 e78dc838 2020-12-04 stsp view->focussed = 1;
1096 e78dc838 2020-12-04 stsp }
1097 e78dc838 2020-12-04 stsp }
1098 e5a0f69f 2018-08-18 stsp }
1099 bcbd79e2 2018-08-19 stsp if (new_view) {
1100 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1101 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1102 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1103 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1104 86c66b02 2018-10-18 stsp continue;
1105 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1106 86c66b02 2018-10-18 stsp err = view_close(v);
1107 86c66b02 2018-10-18 stsp if (err)
1108 86c66b02 2018-10-18 stsp goto done;
1109 86c66b02 2018-10-18 stsp break;
1110 86c66b02 2018-10-18 stsp }
1111 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1112 fed7eaa8 2018-10-24 stsp view = new_view;
1113 e78dc838 2020-12-04 stsp }
1114 669b5ffa 2018-10-07 stsp if (view) {
1115 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1116 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1117 e78dc838 2020-12-04 stsp view = view->child;
1118 e78dc838 2020-12-04 stsp } else {
1119 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1120 e78dc838 2020-12-04 stsp view = view->parent;
1121 1a76625f 2018-10-22 stsp }
1122 e78dc838 2020-12-04 stsp show_panel(view->panel);
1123 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1124 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1125 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1126 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1127 669b5ffa 2018-10-07 stsp if (err)
1128 1a76625f 2018-10-22 stsp goto done;
1129 669b5ffa 2018-10-07 stsp }
1130 669b5ffa 2018-10-07 stsp err = view->show(view);
1131 0cf4efb1 2018-09-29 stsp if (err)
1132 1a76625f 2018-10-22 stsp goto done;
1133 669b5ffa 2018-10-07 stsp if (view->child) {
1134 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1135 669b5ffa 2018-10-07 stsp if (err)
1136 1a76625f 2018-10-22 stsp goto done;
1137 669b5ffa 2018-10-07 stsp }
1138 1a76625f 2018-10-22 stsp update_panels();
1139 1a76625f 2018-10-22 stsp doupdate();
1140 0cf4efb1 2018-09-29 stsp }
1141 e5a0f69f 2018-08-18 stsp }
1142 e5a0f69f 2018-08-18 stsp done:
1143 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1144 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1145 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1146 e5a0f69f 2018-08-18 stsp view_close(view);
1147 e5a0f69f 2018-08-18 stsp }
1148 1a76625f 2018-10-22 stsp
1149 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1150 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1151 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1152 1a76625f 2018-10-22 stsp
1153 e5a0f69f 2018-08-18 stsp return err;
1154 ea5e7bb5 2018-08-01 stsp }
1155 ea5e7bb5 2018-08-01 stsp
1156 4ed7e80c 2018-05-20 stsp __dead static void
1157 9f7d7167 2018-04-29 stsp usage_log(void)
1158 9f7d7167 2018-04-29 stsp {
1159 80ddbec8 2018-04-29 stsp endwin();
1160 c70c5802 2018-08-01 stsp fprintf(stderr,
1161 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1162 9f7d7167 2018-04-29 stsp getprogname());
1163 9f7d7167 2018-04-29 stsp exit(1);
1164 80ddbec8 2018-04-29 stsp }
1165 80ddbec8 2018-04-29 stsp
1166 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1167 80ddbec8 2018-04-29 stsp static const struct got_error *
1168 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1169 963b370f 2018-05-20 stsp {
1170 00dfcb92 2018-06-11 stsp char *vis = NULL;
1171 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1172 963b370f 2018-05-20 stsp
1173 963b370f 2018-05-20 stsp *ws = NULL;
1174 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1175 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1176 00dfcb92 2018-06-11 stsp int vislen;
1177 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1178 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1179 00dfcb92 2018-06-11 stsp
1180 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1181 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1182 00dfcb92 2018-06-11 stsp if (err)
1183 00dfcb92 2018-06-11 stsp return err;
1184 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1185 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1186 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1187 a7f50699 2018-06-11 stsp goto done;
1188 a7f50699 2018-06-11 stsp }
1189 00dfcb92 2018-06-11 stsp }
1190 963b370f 2018-05-20 stsp
1191 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1192 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1193 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1194 a7f50699 2018-06-11 stsp goto done;
1195 a7f50699 2018-06-11 stsp }
1196 963b370f 2018-05-20 stsp
1197 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1198 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1199 a7f50699 2018-06-11 stsp done:
1200 00dfcb92 2018-06-11 stsp free(vis);
1201 963b370f 2018-05-20 stsp if (err) {
1202 963b370f 2018-05-20 stsp free(*ws);
1203 963b370f 2018-05-20 stsp *ws = NULL;
1204 963b370f 2018-05-20 stsp *wlen = 0;
1205 963b370f 2018-05-20 stsp }
1206 963b370f 2018-05-20 stsp return err;
1207 963b370f 2018-05-20 stsp }
1208 963b370f 2018-05-20 stsp
1209 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1210 963b370f 2018-05-20 stsp static const struct got_error *
1211 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1212 27a741e5 2019-09-11 stsp int col_tab_align)
1213 963b370f 2018-05-20 stsp {
1214 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1215 963b370f 2018-05-20 stsp int cols = 0;
1216 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1217 963b370f 2018-05-20 stsp size_t wlen;
1218 963b370f 2018-05-20 stsp int i;
1219 963b370f 2018-05-20 stsp
1220 963b370f 2018-05-20 stsp *wlinep = NULL;
1221 b700b5d6 2018-07-10 stsp *widthp = 0;
1222 963b370f 2018-05-20 stsp
1223 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1224 963b370f 2018-05-20 stsp if (err)
1225 963b370f 2018-05-20 stsp return err;
1226 963b370f 2018-05-20 stsp
1227 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1228 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1229 3f670bfb 2020-12-10 stsp wlen--;
1230 3f670bfb 2020-12-10 stsp }
1231 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1232 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1233 3f670bfb 2020-12-10 stsp wlen--;
1234 3f670bfb 2020-12-10 stsp }
1235 3f670bfb 2020-12-10 stsp
1236 963b370f 2018-05-20 stsp i = 0;
1237 27a741e5 2019-09-11 stsp while (i < wlen) {
1238 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1239 27a741e5 2019-09-11 stsp
1240 27a741e5 2019-09-11 stsp if (width == 0) {
1241 b700b5d6 2018-07-10 stsp i++;
1242 27a741e5 2019-09-11 stsp continue;
1243 27a741e5 2019-09-11 stsp }
1244 27a741e5 2019-09-11 stsp
1245 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1246 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1247 27a741e5 2019-09-11 stsp break;
1248 27a741e5 2019-09-11 stsp cols += width;
1249 3c1f04f1 2018-09-13 stsp i++;
1250 27a741e5 2019-09-11 stsp } else if (width == -1) {
1251 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1252 27a741e5 2019-09-11 stsp width = TABSIZE -
1253 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1254 748d5cab 2020-12-14 naddy } else {
1255 748d5cab 2020-12-14 naddy width = 1;
1256 748d5cab 2020-12-14 naddy wline[i] = L'.';
1257 27a741e5 2019-09-11 stsp }
1258 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1259 748d5cab 2020-12-14 naddy break;
1260 748d5cab 2020-12-14 naddy cols += width;
1261 b700b5d6 2018-07-10 stsp i++;
1262 27a741e5 2019-09-11 stsp } else {
1263 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1264 963b370f 2018-05-20 stsp goto done;
1265 963b370f 2018-05-20 stsp }
1266 963b370f 2018-05-20 stsp }
1267 963b370f 2018-05-20 stsp wline[i] = L'\0';
1268 b700b5d6 2018-07-10 stsp if (widthp)
1269 b700b5d6 2018-07-10 stsp *widthp = cols;
1270 963b370f 2018-05-20 stsp done:
1271 963b370f 2018-05-20 stsp if (err)
1272 963b370f 2018-05-20 stsp free(wline);
1273 963b370f 2018-05-20 stsp else
1274 963b370f 2018-05-20 stsp *wlinep = wline;
1275 963b370f 2018-05-20 stsp return err;
1276 963b370f 2018-05-20 stsp }
1277 963b370f 2018-05-20 stsp
1278 8b473291 2019-02-21 stsp static const struct got_error*
1279 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1280 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1281 8b473291 2019-02-21 stsp {
1282 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1283 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1284 8b473291 2019-02-21 stsp char *s;
1285 8b473291 2019-02-21 stsp const char *name;
1286 8b473291 2019-02-21 stsp
1287 8b473291 2019-02-21 stsp *refs_str = NULL;
1288 8b473291 2019-02-21 stsp
1289 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1290 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1291 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1292 52b5abe1 2019-08-13 stsp int cmp;
1293 52b5abe1 2019-08-13 stsp
1294 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1295 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1296 8b473291 2019-02-21 stsp continue;
1297 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1298 8b473291 2019-02-21 stsp name += 5;
1299 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1300 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1301 7143d404 2019-03-12 stsp continue;
1302 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1303 8b473291 2019-02-21 stsp name += 6;
1304 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1305 8b473291 2019-02-21 stsp name += 8;
1306 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1307 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1308 79cc719f 2020-04-24 stsp continue;
1309 79cc719f 2020-04-24 stsp }
1310 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1311 48cae60d 2020-09-22 stsp if (err)
1312 48cae60d 2020-09-22 stsp break;
1313 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1314 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1315 5d844a1e 2019-08-13 stsp if (err) {
1316 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1317 48cae60d 2020-09-22 stsp free(ref_id);
1318 5d844a1e 2019-08-13 stsp break;
1319 48cae60d 2020-09-22 stsp }
1320 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1321 5d844a1e 2019-08-13 stsp err = NULL;
1322 5d844a1e 2019-08-13 stsp tag = NULL;
1323 5d844a1e 2019-08-13 stsp }
1324 52b5abe1 2019-08-13 stsp }
1325 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1326 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1327 48cae60d 2020-09-22 stsp free(ref_id);
1328 52b5abe1 2019-08-13 stsp if (tag)
1329 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1330 52b5abe1 2019-08-13 stsp if (cmp != 0)
1331 52b5abe1 2019-08-13 stsp continue;
1332 8b473291 2019-02-21 stsp s = *refs_str;
1333 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1334 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1335 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1336 8b473291 2019-02-21 stsp free(s);
1337 8b473291 2019-02-21 stsp *refs_str = NULL;
1338 8b473291 2019-02-21 stsp break;
1339 8b473291 2019-02-21 stsp }
1340 8b473291 2019-02-21 stsp free(s);
1341 8b473291 2019-02-21 stsp }
1342 8b473291 2019-02-21 stsp
1343 8b473291 2019-02-21 stsp return err;
1344 8b473291 2019-02-21 stsp }
1345 8b473291 2019-02-21 stsp
1346 963b370f 2018-05-20 stsp static const struct got_error *
1347 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1348 27a741e5 2019-09-11 stsp int col_tab_align)
1349 5813d178 2019-03-09 stsp {
1350 e6b8b890 2020-12-29 naddy char *smallerthan;
1351 5813d178 2019-03-09 stsp
1352 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1353 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1354 5813d178 2019-03-09 stsp author = smallerthan + 1;
1355 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1356 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1357 5813d178 2019-03-09 stsp }
1358 5813d178 2019-03-09 stsp
1359 5813d178 2019-03-09 stsp static const struct got_error *
1360 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1361 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1362 8fdc79fe 2020-12-01 naddy int author_display_cols)
1363 80ddbec8 2018-04-29 stsp {
1364 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1365 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1366 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1367 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1368 5813d178 2019-03-09 stsp char *author = NULL;
1369 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1370 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1371 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1372 bb737323 2018-05-20 stsp int col, limit;
1373 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1374 ccb26ccd 2018-11-05 stsp struct tm tm;
1375 45d799e2 2018-12-23 stsp time_t committer_time;
1376 11b20872 2019-11-08 stsp struct tog_color *tc;
1377 80ddbec8 2018-04-29 stsp
1378 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1379 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1380 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1381 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1382 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1383 b39d25c7 2018-07-10 stsp
1384 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1385 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1386 b39d25c7 2018-07-10 stsp else
1387 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1388 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1389 11b20872 2019-11-08 stsp if (tc)
1390 11b20872 2019-11-08 stsp wattr_on(view->window,
1391 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1392 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1393 11b20872 2019-11-08 stsp if (tc)
1394 11b20872 2019-11-08 stsp wattr_off(view->window,
1395 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1396 27a741e5 2019-09-11 stsp col = limit;
1397 b39d25c7 2018-07-10 stsp if (col > avail)
1398 b39d25c7 2018-07-10 stsp goto done;
1399 6570a66d 2019-11-08 stsp
1400 6570a66d 2019-11-08 stsp if (avail >= 120) {
1401 6570a66d 2019-11-08 stsp char *id_str;
1402 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1403 6570a66d 2019-11-08 stsp if (err)
1404 6570a66d 2019-11-08 stsp goto done;
1405 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1406 11b20872 2019-11-08 stsp if (tc)
1407 11b20872 2019-11-08 stsp wattr_on(view->window,
1408 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1409 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1410 11b20872 2019-11-08 stsp if (tc)
1411 11b20872 2019-11-08 stsp wattr_off(view->window,
1412 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1413 6570a66d 2019-11-08 stsp free(id_str);
1414 6570a66d 2019-11-08 stsp col += 9;
1415 6570a66d 2019-11-08 stsp if (col > avail)
1416 6570a66d 2019-11-08 stsp goto done;
1417 6570a66d 2019-11-08 stsp }
1418 b39d25c7 2018-07-10 stsp
1419 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1420 5813d178 2019-03-09 stsp if (author == NULL) {
1421 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1422 80ddbec8 2018-04-29 stsp goto done;
1423 80ddbec8 2018-04-29 stsp }
1424 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1425 bb737323 2018-05-20 stsp if (err)
1426 bb737323 2018-05-20 stsp goto done;
1427 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1428 11b20872 2019-11-08 stsp if (tc)
1429 11b20872 2019-11-08 stsp wattr_on(view->window,
1430 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1431 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1432 11b20872 2019-11-08 stsp if (tc)
1433 11b20872 2019-11-08 stsp wattr_off(view->window,
1434 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1435 bb737323 2018-05-20 stsp col += author_width;
1436 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1437 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1438 bb737323 2018-05-20 stsp col++;
1439 bb737323 2018-05-20 stsp author_width++;
1440 bb737323 2018-05-20 stsp }
1441 9c2eaf34 2018-05-20 stsp if (col > avail)
1442 9c2eaf34 2018-05-20 stsp goto done;
1443 80ddbec8 2018-04-29 stsp
1444 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1445 5943eee2 2019-08-13 stsp if (err)
1446 6d9fbc00 2018-04-29 stsp goto done;
1447 bb737323 2018-05-20 stsp logmsg = logmsg0;
1448 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1449 bb737323 2018-05-20 stsp logmsg++;
1450 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1451 bb737323 2018-05-20 stsp if (newline)
1452 bb737323 2018-05-20 stsp *newline = '\0';
1453 bb737323 2018-05-20 stsp limit = avail - col;
1454 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1455 bb737323 2018-05-20 stsp if (err)
1456 bb737323 2018-05-20 stsp goto done;
1457 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1458 bb737323 2018-05-20 stsp col += logmsg_width;
1459 27a741e5 2019-09-11 stsp while (col < avail) {
1460 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1461 bb737323 2018-05-20 stsp col++;
1462 881b2d3e 2018-04-30 stsp }
1463 80ddbec8 2018-04-29 stsp done:
1464 80ddbec8 2018-04-29 stsp free(logmsg0);
1465 bb737323 2018-05-20 stsp free(wlogmsg);
1466 5813d178 2019-03-09 stsp free(author);
1467 bb737323 2018-05-20 stsp free(wauthor);
1468 80ddbec8 2018-04-29 stsp free(line);
1469 80ddbec8 2018-04-29 stsp return err;
1470 80ddbec8 2018-04-29 stsp }
1471 26ed57b2 2018-05-19 stsp
1472 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1473 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1474 899d86c2 2018-05-10 stsp struct got_object_id *id)
1475 80ddbec8 2018-04-29 stsp {
1476 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1477 80ddbec8 2018-04-29 stsp
1478 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1479 80ddbec8 2018-04-29 stsp if (entry == NULL)
1480 899d86c2 2018-05-10 stsp return NULL;
1481 99db9666 2018-05-07 stsp
1482 899d86c2 2018-05-10 stsp entry->id = id;
1483 99db9666 2018-05-07 stsp entry->commit = commit;
1484 899d86c2 2018-05-10 stsp return entry;
1485 99db9666 2018-05-07 stsp }
1486 80ddbec8 2018-04-29 stsp
1487 99db9666 2018-05-07 stsp static void
1488 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1489 99db9666 2018-05-07 stsp {
1490 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1491 99db9666 2018-05-07 stsp
1492 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1493 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1494 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1495 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1496 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1497 99db9666 2018-05-07 stsp free(entry);
1498 99db9666 2018-05-07 stsp }
1499 99db9666 2018-05-07 stsp
1500 99db9666 2018-05-07 stsp static void
1501 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1502 99db9666 2018-05-07 stsp {
1503 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1504 99db9666 2018-05-07 stsp pop_commit(commits);
1505 c4972b91 2018-05-07 stsp }
1506 c4972b91 2018-05-07 stsp
1507 c4972b91 2018-05-07 stsp static const struct got_error *
1508 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1509 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1510 13add988 2019-10-15 stsp {
1511 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1512 13add988 2019-10-15 stsp regmatch_t regmatch;
1513 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1514 13add988 2019-10-15 stsp
1515 13add988 2019-10-15 stsp *have_match = 0;
1516 13add988 2019-10-15 stsp
1517 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1518 13add988 2019-10-15 stsp if (err)
1519 13add988 2019-10-15 stsp return err;
1520 13add988 2019-10-15 stsp
1521 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1522 13add988 2019-10-15 stsp if (err)
1523 13add988 2019-10-15 stsp goto done;
1524 13add988 2019-10-15 stsp
1525 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1526 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1527 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1528 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1529 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1530 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1531 13add988 2019-10-15 stsp *have_match = 1;
1532 13add988 2019-10-15 stsp done:
1533 13add988 2019-10-15 stsp free(id_str);
1534 13add988 2019-10-15 stsp free(logmsg);
1535 13add988 2019-10-15 stsp return err;
1536 13add988 2019-10-15 stsp }
1537 13add988 2019-10-15 stsp
1538 13add988 2019-10-15 stsp static const struct got_error *
1539 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1540 c4972b91 2018-05-07 stsp {
1541 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1542 9ba79e04 2018-06-11 stsp
1543 1a76625f 2018-10-22 stsp /*
1544 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1545 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1546 1a76625f 2018-10-22 stsp * while updating the display.
1547 1a76625f 2018-10-22 stsp */
1548 4e0d2870 2020-12-07 naddy do {
1549 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1550 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1551 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1552 1a76625f 2018-10-22 stsp int errcode;
1553 899d86c2 2018-05-10 stsp
1554 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1555 4e0d2870 2020-12-07 naddy NULL, NULL);
1556 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1557 ecb28ae0 2018-07-16 stsp break;
1558 899d86c2 2018-05-10 stsp
1559 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1560 9ba79e04 2018-06-11 stsp if (err)
1561 9ba79e04 2018-06-11 stsp break;
1562 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1563 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1564 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1565 9ba79e04 2018-06-11 stsp break;
1566 9ba79e04 2018-06-11 stsp }
1567 93e45b7c 2018-09-24 stsp
1568 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1569 1a76625f 2018-10-22 stsp if (errcode) {
1570 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1571 13add988 2019-10-15 stsp "pthread_mutex_lock");
1572 1a76625f 2018-10-22 stsp break;
1573 1a76625f 2018-10-22 stsp }
1574 1a76625f 2018-10-22 stsp
1575 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1576 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1577 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1578 1a76625f 2018-10-22 stsp
1579 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1580 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1581 7c1452c1 2020-03-26 stsp int have_match;
1582 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1583 7c1452c1 2020-03-26 stsp if (err)
1584 7c1452c1 2020-03-26 stsp break;
1585 7c1452c1 2020-03-26 stsp if (have_match)
1586 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1587 13add988 2019-10-15 stsp }
1588 13add988 2019-10-15 stsp
1589 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1590 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1591 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1592 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1593 7c1452c1 2020-03-26 stsp if (err)
1594 13add988 2019-10-15 stsp break;
1595 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1596 899d86c2 2018-05-10 stsp
1597 9ba79e04 2018-06-11 stsp return err;
1598 0553a4e3 2018-04-30 stsp }
1599 0553a4e3 2018-04-30 stsp
1600 2b779855 2020-12-05 naddy static void
1601 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1602 2b779855 2020-12-05 naddy {
1603 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1604 2b779855 2020-12-05 naddy int ncommits = 0;
1605 2b779855 2020-12-05 naddy
1606 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1607 2b779855 2020-12-05 naddy while (entry) {
1608 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1609 2b779855 2020-12-05 naddy s->selected_entry = entry;
1610 2b779855 2020-12-05 naddy break;
1611 2b779855 2020-12-05 naddy }
1612 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1613 2b779855 2020-12-05 naddy ncommits++;
1614 2b779855 2020-12-05 naddy }
1615 2b779855 2020-12-05 naddy }
1616 2b779855 2020-12-05 naddy
1617 0553a4e3 2018-04-30 stsp static const struct got_error *
1618 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1619 0553a4e3 2018-04-30 stsp {
1620 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1621 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1622 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1623 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1624 60493ae3 2019-06-20 stsp int width;
1625 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1626 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1627 8b473291 2019-02-21 stsp char *refs_str = NULL;
1628 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1629 11b20872 2019-11-08 stsp struct tog_color *tc;
1630 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1631 0553a4e3 2018-04-30 stsp
1632 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1633 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1634 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1635 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1636 1a76625f 2018-10-22 stsp if (err)
1637 ecb28ae0 2018-07-16 stsp return err;
1638 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1639 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1640 d2075bf3 2020-12-25 stsp if (refs) {
1641 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1642 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1643 d2075bf3 2020-12-25 stsp if (err)
1644 d2075bf3 2020-12-25 stsp goto done;
1645 d2075bf3 2020-12-25 stsp }
1646 867c6645 2018-07-10 stsp }
1647 359bfafd 2019-02-22 stsp
1648 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1649 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1650 1a76625f 2018-10-22 stsp
1651 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1652 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1653 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1654 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1655 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1656 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1657 8f4ed634 2020-03-26 stsp goto done;
1658 8f4ed634 2020-03-26 stsp }
1659 8f4ed634 2020-03-26 stsp } else {
1660 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1661 f9686aa5 2020-03-27 stsp
1662 f9686aa5 2020-03-27 stsp if (view->searching) {
1663 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1664 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1665 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1666 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1667 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1668 f9686aa5 2020-03-27 stsp search_str = "searching...";
1669 f9686aa5 2020-03-27 stsp }
1670 f9686aa5 2020-03-27 stsp
1671 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1672 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1673 f9686aa5 2020-03-27 stsp search_str ? search_str :
1674 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1675 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1676 8f4ed634 2020-03-26 stsp goto done;
1677 8f4ed634 2020-03-26 stsp }
1678 8b473291 2019-02-21 stsp }
1679 1a76625f 2018-10-22 stsp
1680 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1681 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1682 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1683 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1684 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1685 1a76625f 2018-10-22 stsp header = NULL;
1686 1a76625f 2018-10-22 stsp goto done;
1687 1a76625f 2018-10-22 stsp }
1688 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1689 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1690 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1691 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1692 1a76625f 2018-10-22 stsp header = NULL;
1693 1a76625f 2018-10-22 stsp goto done;
1694 ecb28ae0 2018-07-16 stsp }
1695 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1696 1a76625f 2018-10-22 stsp if (err)
1697 1a76625f 2018-10-22 stsp goto done;
1698 867c6645 2018-07-10 stsp
1699 2814baeb 2018-08-01 stsp werase(view->window);
1700 867c6645 2018-07-10 stsp
1701 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1702 a3404814 2018-09-02 stsp wstandout(view->window);
1703 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1704 11b20872 2019-11-08 stsp if (tc)
1705 11b20872 2019-11-08 stsp wattr_on(view->window,
1706 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1707 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1708 11b20872 2019-11-08 stsp if (tc)
1709 11b20872 2019-11-08 stsp wattr_off(view->window,
1710 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1711 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1712 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1713 1a76625f 2018-10-22 stsp width++;
1714 1a76625f 2018-10-22 stsp }
1715 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1716 a3404814 2018-09-02 stsp wstandend(view->window);
1717 ecb28ae0 2018-07-16 stsp free(wline);
1718 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1719 1a76625f 2018-10-22 stsp goto done;
1720 0553a4e3 2018-04-30 stsp
1721 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1722 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1723 5813d178 2019-03-09 stsp ncommits = 0;
1724 5813d178 2019-03-09 stsp while (entry) {
1725 5813d178 2019-03-09 stsp char *author;
1726 5813d178 2019-03-09 stsp wchar_t *wauthor;
1727 5813d178 2019-03-09 stsp int width;
1728 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1729 5813d178 2019-03-09 stsp break;
1730 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1731 5813d178 2019-03-09 stsp if (author == NULL) {
1732 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1733 5813d178 2019-03-09 stsp goto done;
1734 5813d178 2019-03-09 stsp }
1735 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1736 27a741e5 2019-09-11 stsp date_display_cols);
1737 5813d178 2019-03-09 stsp if (author_cols < width)
1738 5813d178 2019-03-09 stsp author_cols = width;
1739 5813d178 2019-03-09 stsp free(wauthor);
1740 5813d178 2019-03-09 stsp free(author);
1741 7ca04879 2019-10-19 stsp ncommits++;
1742 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1743 5813d178 2019-03-09 stsp }
1744 5813d178 2019-03-09 stsp
1745 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1746 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1747 867c6645 2018-07-10 stsp ncommits = 0;
1748 899d86c2 2018-05-10 stsp while (entry) {
1749 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1750 899d86c2 2018-05-10 stsp break;
1751 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1752 2814baeb 2018-08-01 stsp wstandout(view->window);
1753 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1754 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1755 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1756 2814baeb 2018-08-01 stsp wstandend(view->window);
1757 0553a4e3 2018-04-30 stsp if (err)
1758 60493ae3 2019-06-20 stsp goto done;
1759 0553a4e3 2018-04-30 stsp ncommits++;
1760 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1761 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1762 80ddbec8 2018-04-29 stsp }
1763 80ddbec8 2018-04-29 stsp
1764 1a57306a 2018-09-02 stsp view_vborder(view);
1765 dd038bc6 2021-09-21 thomas.ad update_panels();
1766 dd038bc6 2021-09-21 thomas.ad doupdate();
1767 1a76625f 2018-10-22 stsp done:
1768 1a76625f 2018-10-22 stsp free(id_str);
1769 8b473291 2019-02-21 stsp free(refs_str);
1770 1a76625f 2018-10-22 stsp free(ncommits_str);
1771 1a76625f 2018-10-22 stsp free(header);
1772 80ddbec8 2018-04-29 stsp return err;
1773 9f7d7167 2018-04-29 stsp }
1774 07b55e75 2018-05-10 stsp
1775 07b55e75 2018-05-10 stsp static void
1776 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1777 07b55e75 2018-05-10 stsp {
1778 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1779 07b55e75 2018-05-10 stsp int nscrolled = 0;
1780 07b55e75 2018-05-10 stsp
1781 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1782 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1783 07b55e75 2018-05-10 stsp return;
1784 9f7d7167 2018-04-29 stsp
1785 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1786 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1787 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1788 07b55e75 2018-05-10 stsp if (entry) {
1789 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1790 07b55e75 2018-05-10 stsp nscrolled++;
1791 07b55e75 2018-05-10 stsp }
1792 07b55e75 2018-05-10 stsp }
1793 aa075928 2018-05-10 stsp }
1794 aa075928 2018-05-10 stsp
1795 aa075928 2018-05-10 stsp static const struct got_error *
1796 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1797 aa075928 2018-05-10 stsp {
1798 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1799 5e224a3e 2019-02-22 stsp int errcode;
1800 8a42fca8 2019-02-22 stsp
1801 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1802 aa075928 2018-05-10 stsp
1803 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1804 ffe38506 2020-12-01 naddy if (ta->log_complete)
1805 5e224a3e 2019-02-22 stsp break;
1806 b295e71b 2019-02-22 stsp
1807 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1808 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1809 7aafa0d1 2019-02-22 stsp if (errcode)
1810 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1811 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1812 7c1452c1 2020-03-26 stsp
1813 7c1452c1 2020-03-26 stsp /*
1814 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1815 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1816 7c1452c1 2020-03-26 stsp */
1817 7c1452c1 2020-03-26 stsp if (!wait)
1818 7c1452c1 2020-03-26 stsp break;
1819 7c1452c1 2020-03-26 stsp
1820 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1821 ffe38506 2020-12-01 naddy show_log_view(view);
1822 7c1452c1 2020-03-26 stsp update_panels();
1823 7c1452c1 2020-03-26 stsp doupdate();
1824 7c1452c1 2020-03-26 stsp
1825 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1826 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1827 82954512 2020-02-03 stsp if (errcode)
1828 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1829 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1830 82954512 2020-02-03 stsp
1831 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1832 ffe38506 2020-12-01 naddy show_log_view(view);
1833 7c1452c1 2020-03-26 stsp update_panels();
1834 7c1452c1 2020-03-26 stsp doupdate();
1835 5e224a3e 2019-02-22 stsp }
1836 5e224a3e 2019-02-22 stsp
1837 5e224a3e 2019-02-22 stsp return NULL;
1838 5e224a3e 2019-02-22 stsp }
1839 5e224a3e 2019-02-22 stsp
1840 5e224a3e 2019-02-22 stsp static const struct got_error *
1841 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1842 5e224a3e 2019-02-22 stsp {
1843 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1844 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1845 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1846 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1847 5e224a3e 2019-02-22 stsp
1848 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1849 5e224a3e 2019-02-22 stsp return NULL;
1850 5e224a3e 2019-02-22 stsp
1851 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1852 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1853 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1854 08ebd0a9 2019-02-22 stsp /*
1855 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1856 08ebd0a9 2019-02-22 stsp */
1857 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1858 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1859 5e224a3e 2019-02-22 stsp if (err)
1860 5e224a3e 2019-02-22 stsp return err;
1861 7aafa0d1 2019-02-22 stsp }
1862 b295e71b 2019-02-22 stsp
1863 7aafa0d1 2019-02-22 stsp do {
1864 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1865 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1866 88048b54 2019-02-21 stsp break;
1867 88048b54 2019-02-21 stsp
1868 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1869 aa075928 2018-05-10 stsp
1870 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1871 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1872 dd0a52c1 2018-05-20 stsp break;
1873 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1874 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1875 aa075928 2018-05-10 stsp
1876 dd0a52c1 2018-05-20 stsp return err;
1877 07b55e75 2018-05-10 stsp }
1878 4a7f7875 2018-05-10 stsp
1879 cd0acaa7 2018-05-20 stsp static const struct got_error *
1880 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1881 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1882 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1883 cd0acaa7 2018-05-20 stsp {
1884 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1885 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1886 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1887 cd0acaa7 2018-05-20 stsp
1888 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1889 15a94983 2018-12-23 stsp if (diff_view == NULL)
1890 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1891 ea5e7bb5 2018-08-01 stsp
1892 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1893 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1894 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1895 e5a0f69f 2018-08-18 stsp if (err == NULL)
1896 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1897 cd0acaa7 2018-05-20 stsp return err;
1898 4a7f7875 2018-05-10 stsp }
1899 4a7f7875 2018-05-10 stsp
1900 80ddbec8 2018-04-29 stsp static const struct got_error *
1901 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1902 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1903 9343a5fb 2018-06-23 stsp {
1904 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1905 941e9f74 2019-05-21 stsp
1906 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1907 941e9f74 2019-05-21 stsp if (parent == NULL)
1908 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1909 941e9f74 2019-05-21 stsp
1910 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1911 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1912 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1913 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1914 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1915 941e9f74 2019-05-21 stsp s->tree = subtree;
1916 941e9f74 2019-05-21 stsp s->selected = 0;
1917 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1918 941e9f74 2019-05-21 stsp return NULL;
1919 941e9f74 2019-05-21 stsp }
1920 941e9f74 2019-05-21 stsp
1921 941e9f74 2019-05-21 stsp static const struct got_error *
1922 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1923 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1924 941e9f74 2019-05-21 stsp {
1925 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1926 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1927 941e9f74 2019-05-21 stsp const char *p;
1928 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1929 9343a5fb 2018-06-23 stsp
1930 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1931 941e9f74 2019-05-21 stsp p = path;
1932 941e9f74 2019-05-21 stsp while (*p) {
1933 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1934 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1935 56e0773d 2019-11-28 stsp char *te_name;
1936 33cbf02b 2020-01-12 stsp
1937 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1938 33cbf02b 2020-01-12 stsp p++;
1939 941e9f74 2019-05-21 stsp
1940 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1941 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1942 941e9f74 2019-05-21 stsp if (slash == NULL)
1943 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1944 33cbf02b 2020-01-12 stsp else
1945 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1946 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1947 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1948 56e0773d 2019-11-28 stsp break;
1949 941e9f74 2019-05-21 stsp }
1950 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1951 56e0773d 2019-11-28 stsp if (te == NULL) {
1952 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1953 56e0773d 2019-11-28 stsp free(te_name);
1954 941e9f74 2019-05-21 stsp break;
1955 941e9f74 2019-05-21 stsp }
1956 56e0773d 2019-11-28 stsp free(te_name);
1957 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1958 941e9f74 2019-05-21 stsp
1959 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1960 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1961 b03c880f 2019-05-21 stsp
1962 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1963 941e9f74 2019-05-21 stsp if (slash)
1964 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1965 941e9f74 2019-05-21 stsp else
1966 941e9f74 2019-05-21 stsp subpath = strdup(path);
1967 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1968 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1969 941e9f74 2019-05-21 stsp break;
1970 941e9f74 2019-05-21 stsp }
1971 941e9f74 2019-05-21 stsp
1972 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1973 941e9f74 2019-05-21 stsp subpath);
1974 941e9f74 2019-05-21 stsp if (err)
1975 941e9f74 2019-05-21 stsp break;
1976 941e9f74 2019-05-21 stsp
1977 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1978 941e9f74 2019-05-21 stsp free(tree_id);
1979 941e9f74 2019-05-21 stsp if (err)
1980 941e9f74 2019-05-21 stsp break;
1981 941e9f74 2019-05-21 stsp
1982 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1983 941e9f74 2019-05-21 stsp if (err) {
1984 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1985 941e9f74 2019-05-21 stsp break;
1986 941e9f74 2019-05-21 stsp }
1987 941e9f74 2019-05-21 stsp if (slash == NULL)
1988 941e9f74 2019-05-21 stsp break;
1989 941e9f74 2019-05-21 stsp free(subpath);
1990 941e9f74 2019-05-21 stsp subpath = NULL;
1991 941e9f74 2019-05-21 stsp p = slash;
1992 941e9f74 2019-05-21 stsp }
1993 941e9f74 2019-05-21 stsp
1994 941e9f74 2019-05-21 stsp free(subpath);
1995 1a76625f 2018-10-22 stsp return err;
1996 61266923 2020-01-14 stsp }
1997 61266923 2020-01-14 stsp
1998 61266923 2020-01-14 stsp static const struct got_error *
1999 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2000 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2001 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2002 55cccc34 2020-02-20 stsp {
2003 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2004 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2005 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2006 55cccc34 2020-02-20 stsp
2007 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2008 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2009 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2010 55cccc34 2020-02-20 stsp
2011 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2012 bc573f3b 2021-07-10 stsp if (err)
2013 55cccc34 2020-02-20 stsp return err;
2014 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2015 55cccc34 2020-02-20 stsp
2016 55cccc34 2020-02-20 stsp *new_view = tree_view;
2017 55cccc34 2020-02-20 stsp
2018 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2019 55cccc34 2020-02-20 stsp return NULL;
2020 55cccc34 2020-02-20 stsp
2021 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
2022 55cccc34 2020-02-20 stsp }
2023 55cccc34 2020-02-20 stsp
2024 55cccc34 2020-02-20 stsp static const struct got_error *
2025 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2026 61266923 2020-01-14 stsp {
2027 61266923 2020-01-14 stsp sigset_t sigset;
2028 61266923 2020-01-14 stsp int errcode;
2029 61266923 2020-01-14 stsp
2030 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2031 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2032 61266923 2020-01-14 stsp
2033 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
2034 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2035 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2036 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2037 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2038 61266923 2020-01-14 stsp
2039 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2040 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2041 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2042 61266923 2020-01-14 stsp
2043 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2044 61266923 2020-01-14 stsp if (errcode)
2045 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2046 61266923 2020-01-14 stsp
2047 61266923 2020-01-14 stsp return NULL;
2048 1a76625f 2018-10-22 stsp }
2049 1a76625f 2018-10-22 stsp
2050 1a76625f 2018-10-22 stsp static void *
2051 1a76625f 2018-10-22 stsp log_thread(void *arg)
2052 1a76625f 2018-10-22 stsp {
2053 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2054 1a76625f 2018-10-22 stsp int errcode = 0;
2055 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2056 1a76625f 2018-10-22 stsp int done = 0;
2057 61266923 2020-01-14 stsp
2058 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2059 61266923 2020-01-14 stsp if (err)
2060 61266923 2020-01-14 stsp return (void *)err;
2061 1a76625f 2018-10-22 stsp
2062 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2063 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2064 1a76625f 2018-10-22 stsp if (err) {
2065 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2066 1a76625f 2018-10-22 stsp return (void *)err;
2067 1a76625f 2018-10-22 stsp err = NULL;
2068 1a76625f 2018-10-22 stsp done = 1;
2069 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2070 1a76625f 2018-10-22 stsp a->commits_needed--;
2071 1a76625f 2018-10-22 stsp
2072 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2073 3abe8080 2019-04-10 stsp if (errcode) {
2074 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2075 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2076 3abe8080 2019-04-10 stsp break;
2077 3abe8080 2019-04-10 stsp } else if (*a->quit)
2078 1a76625f 2018-10-22 stsp done = 1;
2079 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2080 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2081 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2082 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2083 1a76625f 2018-10-22 stsp }
2084 1a76625f 2018-10-22 stsp
2085 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2086 7c1452c1 2020-03-26 stsp if (errcode) {
2087 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2088 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2089 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2090 7c1452c1 2020-03-26 stsp break;
2091 7c1452c1 2020-03-26 stsp }
2092 7c1452c1 2020-03-26 stsp
2093 1a76625f 2018-10-22 stsp if (done)
2094 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2095 7c1452c1 2020-03-26 stsp else {
2096 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2097 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2098 7c1452c1 2020-03-26 stsp &tog_mutex);
2099 7c1452c1 2020-03-26 stsp if (errcode)
2100 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2101 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2102 21355643 2020-12-06 stsp if (*a->quit)
2103 21355643 2020-12-06 stsp done = 1;
2104 7c1452c1 2020-03-26 stsp }
2105 1a76625f 2018-10-22 stsp }
2106 1a76625f 2018-10-22 stsp
2107 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2108 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2109 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2110 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2111 1a76625f 2018-10-22 stsp }
2112 3abe8080 2019-04-10 stsp a->log_complete = 1;
2113 1a76625f 2018-10-22 stsp return (void *)err;
2114 1a76625f 2018-10-22 stsp }
2115 1a76625f 2018-10-22 stsp
2116 1a76625f 2018-10-22 stsp static const struct got_error *
2117 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2118 1a76625f 2018-10-22 stsp {
2119 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2120 1a76625f 2018-10-22 stsp int errcode;
2121 1a76625f 2018-10-22 stsp
2122 1a76625f 2018-10-22 stsp if (s->thread) {
2123 1a76625f 2018-10-22 stsp s->quit = 1;
2124 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2125 1a76625f 2018-10-22 stsp if (errcode)
2126 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2127 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2128 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2129 1a76625f 2018-10-22 stsp if (errcode)
2130 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2131 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2132 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2133 1a76625f 2018-10-22 stsp if (errcode)
2134 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2135 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2136 1a76625f 2018-10-22 stsp if (errcode)
2137 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2138 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2139 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2140 1a76625f 2018-10-22 stsp }
2141 1a76625f 2018-10-22 stsp
2142 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2143 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2144 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2145 1a76625f 2018-10-22 stsp }
2146 1a76625f 2018-10-22 stsp
2147 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2148 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2149 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2150 1a76625f 2018-10-22 stsp }
2151 1a76625f 2018-10-22 stsp
2152 9343a5fb 2018-06-23 stsp return err;
2153 9343a5fb 2018-06-23 stsp }
2154 9343a5fb 2018-06-23 stsp
2155 9343a5fb 2018-06-23 stsp static const struct got_error *
2156 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2157 1a76625f 2018-10-22 stsp {
2158 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2159 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2160 276b94a1 2020-11-13 naddy int errcode;
2161 1a76625f 2018-10-22 stsp
2162 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2163 276b94a1 2020-11-13 naddy
2164 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2165 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2166 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2167 276b94a1 2020-11-13 naddy
2168 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2169 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2170 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2171 276b94a1 2020-11-13 naddy
2172 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2173 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2174 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2175 1a76625f 2018-10-22 stsp free(s->start_id);
2176 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2177 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2178 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2179 1a76625f 2018-10-22 stsp return err;
2180 1a76625f 2018-10-22 stsp }
2181 1a76625f 2018-10-22 stsp
2182 1a76625f 2018-10-22 stsp static const struct got_error *
2183 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2184 60493ae3 2019-06-20 stsp {
2185 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2186 60493ae3 2019-06-20 stsp
2187 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2188 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2189 60493ae3 2019-06-20 stsp return NULL;
2190 60493ae3 2019-06-20 stsp }
2191 60493ae3 2019-06-20 stsp
2192 60493ae3 2019-06-20 stsp static const struct got_error *
2193 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2194 60493ae3 2019-06-20 stsp {
2195 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2196 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2197 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2198 60493ae3 2019-06-20 stsp
2199 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2200 f9686aa5 2020-03-27 stsp show_log_view(view);
2201 f9686aa5 2020-03-27 stsp update_panels();
2202 f9686aa5 2020-03-27 stsp doupdate();
2203 f9686aa5 2020-03-27 stsp
2204 96e2b566 2019-07-08 stsp if (s->search_entry) {
2205 13add988 2019-10-15 stsp int errcode, ch;
2206 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2207 13add988 2019-10-15 stsp if (errcode)
2208 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2209 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2210 13add988 2019-10-15 stsp ch = wgetch(view->window);
2211 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2212 13add988 2019-10-15 stsp if (errcode)
2213 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2214 13add988 2019-10-15 stsp "pthread_mutex_lock");
2215 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2216 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2217 678cbce5 2019-07-28 stsp return NULL;
2218 678cbce5 2019-07-28 stsp }
2219 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2220 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2221 96e2b566 2019-07-08 stsp else
2222 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2223 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2224 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2225 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2226 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2227 b1bf1435 2019-06-21 stsp else
2228 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2229 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2230 20be8d96 2019-06-21 stsp } else {
2231 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2232 20be8d96 2019-06-21 stsp }
2233 60493ae3 2019-06-20 stsp
2234 60493ae3 2019-06-20 stsp while (1) {
2235 13add988 2019-10-15 stsp int have_match = 0;
2236 13add988 2019-10-15 stsp
2237 60493ae3 2019-06-20 stsp if (entry == NULL) {
2238 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2239 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2240 f9967bca 2020-03-27 stsp view->search_next_done =
2241 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2242 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2243 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2244 f801134a 2019-06-25 stsp return NULL;
2245 60493ae3 2019-06-20 stsp }
2246 96e2b566 2019-07-08 stsp /*
2247 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2248 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2249 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2250 96e2b566 2019-07-08 stsp */
2251 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2252 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2253 60493ae3 2019-06-20 stsp }
2254 60493ae3 2019-06-20 stsp
2255 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2256 13add988 2019-10-15 stsp &view->regex);
2257 5943eee2 2019-08-13 stsp if (err)
2258 13add988 2019-10-15 stsp break;
2259 13add988 2019-10-15 stsp if (have_match) {
2260 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2261 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2262 60493ae3 2019-06-20 stsp break;
2263 60493ae3 2019-06-20 stsp }
2264 13add988 2019-10-15 stsp
2265 96e2b566 2019-07-08 stsp s->search_entry = entry;
2266 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2267 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2268 b1bf1435 2019-06-21 stsp else
2269 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2270 60493ae3 2019-06-20 stsp }
2271 60493ae3 2019-06-20 stsp
2272 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2273 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2274 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2275 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2276 60493ae3 2019-06-20 stsp if (err)
2277 60493ae3 2019-06-20 stsp return err;
2278 ead14cbe 2019-06-21 stsp cur++;
2279 ead14cbe 2019-06-21 stsp }
2280 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2281 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2282 60493ae3 2019-06-20 stsp if (err)
2283 60493ae3 2019-06-20 stsp return err;
2284 ead14cbe 2019-06-21 stsp cur--;
2285 60493ae3 2019-06-20 stsp }
2286 60493ae3 2019-06-20 stsp }
2287 60493ae3 2019-06-20 stsp
2288 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2289 96e2b566 2019-07-08 stsp
2290 60493ae3 2019-06-20 stsp return NULL;
2291 60493ae3 2019-06-20 stsp }
2292 60493ae3 2019-06-20 stsp
2293 60493ae3 2019-06-20 stsp static const struct got_error *
2294 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2295 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2296 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2297 80ddbec8 2018-04-29 stsp {
2298 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2299 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2300 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2301 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2302 1a76625f 2018-10-22 stsp int errcode;
2303 80ddbec8 2018-04-29 stsp
2304 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2305 f135c941 2020-02-20 stsp free(s->in_repo_path);
2306 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2307 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2308 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2309 f135c941 2020-02-20 stsp }
2310 ecb28ae0 2018-07-16 stsp
2311 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2312 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2313 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2314 78756c87 2020-11-24 stsp
2315 fb2756b9 2018-08-04 stsp s->repo = repo;
2316 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2317 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2318 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2319 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2320 9cd7cbd1 2020-12-07 stsp goto done;
2321 9cd7cbd1 2020-12-07 stsp }
2322 9cd7cbd1 2020-12-07 stsp }
2323 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2324 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2325 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2326 5036bf37 2018-09-24 stsp goto done;
2327 5036bf37 2018-09-24 stsp }
2328 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2329 e5a0f69f 2018-08-18 stsp
2330 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2331 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2332 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2333 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2334 11b20872 2019-11-08 stsp if (err)
2335 11b20872 2019-11-08 stsp goto done;
2336 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2337 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2338 11b20872 2019-11-08 stsp if (err) {
2339 11b20872 2019-11-08 stsp free_colors(&s->colors);
2340 11b20872 2019-11-08 stsp goto done;
2341 11b20872 2019-11-08 stsp }
2342 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2343 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2344 11b20872 2019-11-08 stsp if (err) {
2345 11b20872 2019-11-08 stsp free_colors(&s->colors);
2346 11b20872 2019-11-08 stsp goto done;
2347 11b20872 2019-11-08 stsp }
2348 11b20872 2019-11-08 stsp }
2349 11b20872 2019-11-08 stsp
2350 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2351 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2352 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2353 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2354 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2355 1a76625f 2018-10-22 stsp
2356 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2357 1a76625f 2018-10-22 stsp if (err)
2358 1a76625f 2018-10-22 stsp goto done;
2359 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2360 b672a97a 2020-01-27 stsp !s->log_branches);
2361 1a76625f 2018-10-22 stsp if (err)
2362 1a76625f 2018-10-22 stsp goto done;
2363 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2364 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2365 c5b78334 2020-01-12 stsp if (err)
2366 c5b78334 2020-01-12 stsp goto done;
2367 1a76625f 2018-10-22 stsp
2368 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2369 1a76625f 2018-10-22 stsp if (errcode) {
2370 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2371 1a76625f 2018-10-22 stsp goto done;
2372 1a76625f 2018-10-22 stsp }
2373 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2374 7c1452c1 2020-03-26 stsp if (errcode) {
2375 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2376 7c1452c1 2020-03-26 stsp goto done;
2377 7c1452c1 2020-03-26 stsp }
2378 1a76625f 2018-10-22 stsp
2379 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2380 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2381 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2382 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2383 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2384 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2385 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2386 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2387 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2388 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2389 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2390 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2391 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2392 ba4f502b 2018-08-04 stsp done:
2393 1a76625f 2018-10-22 stsp if (err)
2394 1a76625f 2018-10-22 stsp close_log_view(view);
2395 ba4f502b 2018-08-04 stsp return err;
2396 ba4f502b 2018-08-04 stsp }
2397 ba4f502b 2018-08-04 stsp
2398 e5a0f69f 2018-08-18 stsp static const struct got_error *
2399 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2400 ba4f502b 2018-08-04 stsp {
2401 f2f6d207 2020-11-24 stsp const struct got_error *err;
2402 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2403 ba4f502b 2018-08-04 stsp
2404 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2405 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2406 2b380cc8 2018-10-24 stsp &s->thread_args);
2407 2b380cc8 2018-10-24 stsp if (errcode)
2408 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2409 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2410 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2411 f2f6d207 2020-11-24 stsp if (err)
2412 f2f6d207 2020-11-24 stsp return err;
2413 f2f6d207 2020-11-24 stsp }
2414 2b380cc8 2018-10-24 stsp }
2415 2b380cc8 2018-10-24 stsp
2416 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2417 e5a0f69f 2018-08-18 stsp }
2418 04cc582a 2018-08-01 stsp
2419 e5a0f69f 2018-08-18 stsp static const struct got_error *
2420 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2421 e5a0f69f 2018-08-18 stsp {
2422 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2423 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2424 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2425 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2426 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2427 f3bc9f1d 2021-09-05 naddy int begin_x = 0, n;
2428 80ddbec8 2018-04-29 stsp
2429 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2430 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2431 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2432 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2433 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2434 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2435 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2436 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2437 fb280deb 2021-08-30 stsp select_commit(s);
2438 fb280deb 2021-08-30 stsp }
2439 528dedf3 2021-08-30 stsp return NULL;
2440 528dedf3 2021-08-30 stsp }
2441 528dedf3 2021-08-30 stsp
2442 528dedf3 2021-08-30 stsp switch (ch) {
2443 1e37a5c2 2019-05-12 jcs case 'q':
2444 1e37a5c2 2019-05-12 jcs s->quit = 1;
2445 1e37a5c2 2019-05-12 jcs break;
2446 1e37a5c2 2019-05-12 jcs case 'k':
2447 1e37a5c2 2019-05-12 jcs case KEY_UP:
2448 1e37a5c2 2019-05-12 jcs case '<':
2449 1e37a5c2 2019-05-12 jcs case ',':
2450 f7140bf5 2021-10-17 thomas case CTRL('p'):
2451 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2452 1a76625f 2018-10-22 stsp break;
2453 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2454 1e37a5c2 2019-05-12 jcs s->selected--;
2455 1144d21a 2019-06-21 stsp else
2456 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2457 912a3f79 2021-08-30 j select_commit(s);
2458 912a3f79 2021-08-30 j break;
2459 912a3f79 2021-08-30 j case 'g':
2460 912a3f79 2021-08-30 j case KEY_HOME:
2461 912a3f79 2021-08-30 j s->selected = 0;
2462 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2463 2b779855 2020-12-05 naddy select_commit(s);
2464 1e37a5c2 2019-05-12 jcs break;
2465 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2466 a4292ac5 2019-05-12 jcs case CTRL('b'):
2467 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2468 e5a0f69f 2018-08-18 stsp break;
2469 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2470 1e37a5c2 2019-05-12 jcs s->selected = 0;
2471 2b779855 2020-12-05 naddy else
2472 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2473 2b779855 2020-12-05 naddy select_commit(s);
2474 1e37a5c2 2019-05-12 jcs break;
2475 1e37a5c2 2019-05-12 jcs case 'j':
2476 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2477 1e37a5c2 2019-05-12 jcs case '>':
2478 1e37a5c2 2019-05-12 jcs case '.':
2479 f7140bf5 2021-10-17 thomas case CTRL('n'):
2480 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2481 e5a0f69f 2018-08-18 stsp break;
2482 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2483 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2484 1e37a5c2 2019-05-12 jcs s->selected++;
2485 2b779855 2020-12-05 naddy else {
2486 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2487 2b779855 2020-12-05 naddy if (err)
2488 2b779855 2020-12-05 naddy break;
2489 912a3f79 2021-08-30 j }
2490 912a3f79 2021-08-30 j select_commit(s);
2491 912a3f79 2021-08-30 j break;
2492 912a3f79 2021-08-30 j case 'G':
2493 912a3f79 2021-08-30 j case KEY_END: {
2494 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2495 912a3f79 2021-08-30 j * traverse them all. */
2496 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2497 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2498 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2499 80ddbec8 2018-04-29 stsp }
2500 912a3f79 2021-08-30 j
2501 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2502 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2503 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2504 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2505 f3bc9f1d 2021-09-05 naddy break;
2506 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2507 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2508 f3bc9f1d 2021-09-05 naddy }
2509 f3bc9f1d 2021-09-05 naddy if (n > 0)
2510 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2511 2b779855 2020-12-05 naddy select_commit(s);
2512 1e37a5c2 2019-05-12 jcs break;
2513 912a3f79 2021-08-30 j }
2514 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2515 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2516 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2517 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2518 1e37a5c2 2019-05-12 jcs if (first == NULL)
2519 e5a0f69f 2018-08-18 stsp break;
2520 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2521 1cae65b4 2019-09-22 stsp if (err)
2522 1cae65b4 2019-09-22 stsp break;
2523 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2524 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2525 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2526 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2527 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2528 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2529 1e37a5c2 2019-05-12 jcs }
2530 2b779855 2020-12-05 naddy select_commit(s);
2531 1e37a5c2 2019-05-12 jcs break;
2532 1e37a5c2 2019-05-12 jcs }
2533 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2534 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2535 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2536 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2537 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2538 2b779855 2020-12-05 naddy select_commit(s);
2539 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2540 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2541 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2542 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2543 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2544 0bf7f153 2020-12-02 naddy }
2545 1e37a5c2 2019-05-12 jcs break;
2546 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2547 87c7274c 2019-05-12 jcs case ' ':
2548 1e37a5c2 2019-05-12 jcs case '\r':
2549 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2550 e5a0f69f 2018-08-18 stsp break;
2551 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2552 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2553 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2554 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2555 78756c87 2020-11-24 stsp view, s->repo);
2556 1e37a5c2 2019-05-12 jcs if (err)
2557 1e37a5c2 2019-05-12 jcs break;
2558 e78dc838 2020-12-04 stsp view->focussed = 0;
2559 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2560 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2561 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2562 f7013a22 2018-10-24 stsp if (err)
2563 1e37a5c2 2019-05-12 jcs return err;
2564 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2565 e78dc838 2020-12-04 stsp view->focus_child = 1;
2566 1e37a5c2 2019-05-12 jcs } else
2567 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2568 1e37a5c2 2019-05-12 jcs break;
2569 1e37a5c2 2019-05-12 jcs case 't':
2570 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2571 5036bf37 2018-09-24 stsp break;
2572 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2573 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2574 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2575 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2576 4e97c21c 2020-12-06 stsp s->repo);
2577 1e37a5c2 2019-05-12 jcs if (err)
2578 e5a0f69f 2018-08-18 stsp break;
2579 e78dc838 2020-12-04 stsp view->focussed = 0;
2580 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2581 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2582 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2583 1e37a5c2 2019-05-12 jcs if (err)
2584 1e37a5c2 2019-05-12 jcs return err;
2585 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2586 e78dc838 2020-12-04 stsp view->focus_child = 1;
2587 1e37a5c2 2019-05-12 jcs } else
2588 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2589 1e37a5c2 2019-05-12 jcs break;
2590 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2591 21355643 2020-12-06 stsp case CTRL('l'):
2592 21355643 2020-12-06 stsp case 'B':
2593 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2594 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2595 1e37a5c2 2019-05-12 jcs break;
2596 21355643 2020-12-06 stsp err = stop_log_thread(s);
2597 74cfe85e 2020-10-20 stsp if (err)
2598 74cfe85e 2020-10-20 stsp return err;
2599 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2600 21355643 2020-12-06 stsp char *parent_path;
2601 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2602 21355643 2020-12-06 stsp if (err)
2603 21355643 2020-12-06 stsp return err;
2604 21355643 2020-12-06 stsp free(s->in_repo_path);
2605 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2606 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2607 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2608 21355643 2020-12-06 stsp struct got_object_id *start_id;
2609 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2610 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2611 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2612 21355643 2020-12-06 stsp if (err)
2613 21355643 2020-12-06 stsp return err;
2614 21355643 2020-12-06 stsp free(s->start_id);
2615 21355643 2020-12-06 stsp s->start_id = start_id;
2616 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2617 21355643 2020-12-06 stsp } else /* 'B' */
2618 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2619 21355643 2020-12-06 stsp
2620 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2621 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2622 74cfe85e 2020-10-20 stsp if (err)
2623 21355643 2020-12-06 stsp return err;
2624 51a10b52 2020-12-26 stsp tog_free_refs();
2625 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2626 ca51c541 2020-12-07 stsp if (err)
2627 ca51c541 2020-12-07 stsp return err;
2628 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2629 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2630 d01904d4 2019-06-25 stsp if (err)
2631 d01904d4 2019-06-25 stsp return err;
2632 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2633 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2634 b672a97a 2020-01-27 stsp if (err)
2635 b672a97a 2020-01-27 stsp return err;
2636 21355643 2020-12-06 stsp free_commits(&s->commits);
2637 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2638 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2639 21355643 2020-12-06 stsp s->selected_entry = NULL;
2640 21355643 2020-12-06 stsp s->selected = 0;
2641 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2642 21355643 2020-12-06 stsp s->quit = 0;
2643 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2644 d01904d4 2019-06-25 stsp break;
2645 6458efa5 2020-11-24 stsp case 'r':
2646 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2647 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2648 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2649 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2650 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2651 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2652 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2653 6458efa5 2020-11-24 stsp if (err) {
2654 6458efa5 2020-11-24 stsp view_close(ref_view);
2655 6458efa5 2020-11-24 stsp return err;
2656 6458efa5 2020-11-24 stsp }
2657 e78dc838 2020-12-04 stsp view->focussed = 0;
2658 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2659 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2660 6458efa5 2020-11-24 stsp err = view_close_child(view);
2661 6458efa5 2020-11-24 stsp if (err)
2662 6458efa5 2020-11-24 stsp return err;
2663 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2664 e78dc838 2020-12-04 stsp view->focus_child = 1;
2665 6458efa5 2020-11-24 stsp } else
2666 6458efa5 2020-11-24 stsp *new_view = ref_view;
2667 6458efa5 2020-11-24 stsp break;
2668 1e37a5c2 2019-05-12 jcs default:
2669 1e37a5c2 2019-05-12 jcs break;
2670 899d86c2 2018-05-10 stsp }
2671 e5a0f69f 2018-08-18 stsp
2672 80ddbec8 2018-04-29 stsp return err;
2673 80ddbec8 2018-04-29 stsp }
2674 80ddbec8 2018-04-29 stsp
2675 4ed7e80c 2018-05-20 stsp static const struct got_error *
2676 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2677 c2db6724 2019-01-04 stsp {
2678 c2db6724 2019-01-04 stsp const struct got_error *error;
2679 c2db6724 2019-01-04 stsp
2680 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2681 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2682 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2683 37c06ea4 2019-07-15 stsp #endif
2684 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2685 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2686 c2db6724 2019-01-04 stsp
2687 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2688 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2689 c2db6724 2019-01-04 stsp
2690 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2691 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2692 c2db6724 2019-01-04 stsp
2693 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2694 c2db6724 2019-01-04 stsp if (error != NULL)
2695 c2db6724 2019-01-04 stsp return error;
2696 c2db6724 2019-01-04 stsp
2697 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2698 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2699 c2db6724 2019-01-04 stsp
2700 c2db6724 2019-01-04 stsp return NULL;
2701 c2db6724 2019-01-04 stsp }
2702 c2db6724 2019-01-04 stsp
2703 a915003a 2019-02-05 stsp static void
2704 a915003a 2019-02-05 stsp init_curses(void)
2705 a915003a 2019-02-05 stsp {
2706 a915003a 2019-02-05 stsp initscr();
2707 a915003a 2019-02-05 stsp cbreak();
2708 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2709 a915003a 2019-02-05 stsp noecho();
2710 a915003a 2019-02-05 stsp nonl();
2711 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2712 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2713 a915003a 2019-02-05 stsp curs_set(0);
2714 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2715 6d17833f 2019-11-08 stsp start_color();
2716 6d17833f 2019-11-08 stsp use_default_colors();
2717 6d17833f 2019-11-08 stsp }
2718 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2719 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2720 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2721 a915003a 2019-02-05 stsp }
2722 a915003a 2019-02-05 stsp
2723 c2db6724 2019-01-04 stsp static const struct got_error *
2724 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2725 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2726 f135c941 2020-02-20 stsp {
2727 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2728 f135c941 2020-02-20 stsp
2729 f135c941 2020-02-20 stsp if (argc == 0) {
2730 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2731 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2732 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2733 f135c941 2020-02-20 stsp return NULL;
2734 f135c941 2020-02-20 stsp }
2735 f135c941 2020-02-20 stsp
2736 f135c941 2020-02-20 stsp if (worktree) {
2737 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2738 bfd61697 2020-11-14 stsp char *p;
2739 f135c941 2020-02-20 stsp
2740 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2741 f135c941 2020-02-20 stsp if (err)
2742 f135c941 2020-02-20 stsp return err;
2743 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2744 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2745 bfd61697 2020-11-14 stsp p) == -1) {
2746 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2747 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2748 f135c941 2020-02-20 stsp }
2749 f135c941 2020-02-20 stsp free(p);
2750 f135c941 2020-02-20 stsp } else
2751 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2752 f135c941 2020-02-20 stsp
2753 f135c941 2020-02-20 stsp return err;
2754 f135c941 2020-02-20 stsp }
2755 f135c941 2020-02-20 stsp
2756 f135c941 2020-02-20 stsp static const struct got_error *
2757 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2758 9f7d7167 2018-04-29 stsp {
2759 80ddbec8 2018-04-29 stsp const struct got_error *error;
2760 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2761 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2762 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2763 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2764 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2765 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2766 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2767 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2768 04cc582a 2018-08-01 stsp struct tog_view *view;
2769 80ddbec8 2018-04-29 stsp
2770 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2771 80ddbec8 2018-04-29 stsp switch (ch) {
2772 b672a97a 2020-01-27 stsp case 'b':
2773 b672a97a 2020-01-27 stsp log_branches = 1;
2774 b672a97a 2020-01-27 stsp break;
2775 80ddbec8 2018-04-29 stsp case 'c':
2776 80ddbec8 2018-04-29 stsp start_commit = optarg;
2777 80ddbec8 2018-04-29 stsp break;
2778 ecb28ae0 2018-07-16 stsp case 'r':
2779 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2780 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2781 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2782 9ba1d308 2019-10-21 stsp optarg);
2783 ecb28ae0 2018-07-16 stsp break;
2784 80ddbec8 2018-04-29 stsp default:
2785 17020d27 2019-03-07 stsp usage_log();
2786 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2787 80ddbec8 2018-04-29 stsp }
2788 80ddbec8 2018-04-29 stsp }
2789 80ddbec8 2018-04-29 stsp
2790 80ddbec8 2018-04-29 stsp argc -= optind;
2791 80ddbec8 2018-04-29 stsp argv += optind;
2792 80ddbec8 2018-04-29 stsp
2793 f135c941 2020-02-20 stsp if (argc > 1)
2794 f135c941 2020-02-20 stsp usage_log();
2795 963f97a1 2019-03-18 stsp
2796 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2797 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2798 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2799 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2800 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2801 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2802 c156c7a4 2020-12-18 stsp goto done;
2803 a1fbf39a 2019-08-11 stsp if (worktree)
2804 6962eb72 2020-02-20 stsp repo_path =
2805 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2806 a1fbf39a 2019-08-11 stsp else
2807 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2808 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2809 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2810 c156c7a4 2020-12-18 stsp goto done;
2811 c156c7a4 2020-12-18 stsp }
2812 ecb28ae0 2018-07-16 stsp }
2813 ecb28ae0 2018-07-16 stsp
2814 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2815 80ddbec8 2018-04-29 stsp if (error != NULL)
2816 ecb28ae0 2018-07-16 stsp goto done;
2817 80ddbec8 2018-04-29 stsp
2818 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2819 f135c941 2020-02-20 stsp repo, worktree);
2820 f135c941 2020-02-20 stsp if (error)
2821 f135c941 2020-02-20 stsp goto done;
2822 f135c941 2020-02-20 stsp
2823 f135c941 2020-02-20 stsp init_curses();
2824 f135c941 2020-02-20 stsp
2825 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2826 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2827 c02c541e 2019-03-29 stsp if (error)
2828 c02c541e 2019-03-29 stsp goto done;
2829 c02c541e 2019-03-29 stsp
2830 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2831 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2832 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
2833 87670572 2020-12-26 naddy if (error)
2834 87670572 2020-12-26 naddy goto done;
2835 87670572 2020-12-26 naddy }
2836 51a10b52 2020-12-26 stsp
2837 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2838 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2839 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2840 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2841 d8f38dc4 2020-12-05 stsp if (error)
2842 d8f38dc4 2020-12-05 stsp goto done;
2843 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2844 d8f38dc4 2020-12-05 stsp } else {
2845 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2846 d8f38dc4 2020-12-05 stsp if (error == NULL)
2847 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2848 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2849 d8f38dc4 2020-12-05 stsp goto done;
2850 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2851 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2852 d8f38dc4 2020-12-05 stsp if (error)
2853 d8f38dc4 2020-12-05 stsp goto done;
2854 d8f38dc4 2020-12-05 stsp }
2855 8b473291 2019-02-21 stsp
2856 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2857 04cc582a 2018-08-01 stsp if (view == NULL) {
2858 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2859 04cc582a 2018-08-01 stsp goto done;
2860 04cc582a 2018-08-01 stsp }
2861 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2862 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2863 ba4f502b 2018-08-04 stsp if (error)
2864 ba4f502b 2018-08-04 stsp goto done;
2865 2fc00ff4 2019-08-31 stsp if (worktree) {
2866 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2867 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2868 2fc00ff4 2019-08-31 stsp worktree = NULL;
2869 2fc00ff4 2019-08-31 stsp }
2870 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2871 ecb28ae0 2018-07-16 stsp done:
2872 f135c941 2020-02-20 stsp free(in_repo_path);
2873 ecb28ae0 2018-07-16 stsp free(repo_path);
2874 ecb28ae0 2018-07-16 stsp free(cwd);
2875 899d86c2 2018-05-10 stsp free(start_id);
2876 d8f38dc4 2020-12-05 stsp free(label);
2877 d8f38dc4 2020-12-05 stsp if (ref)
2878 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2879 1d0f4054 2021-06-17 stsp if (repo) {
2880 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2881 1d0f4054 2021-06-17 stsp if (error == NULL)
2882 1d0f4054 2021-06-17 stsp error = close_err;
2883 1d0f4054 2021-06-17 stsp }
2884 ec142235 2019-03-07 stsp if (worktree)
2885 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2886 51a10b52 2020-12-26 stsp tog_free_refs();
2887 80ddbec8 2018-04-29 stsp return error;
2888 9f7d7167 2018-04-29 stsp }
2889 9f7d7167 2018-04-29 stsp
2890 4ed7e80c 2018-05-20 stsp __dead static void
2891 9f7d7167 2018-04-29 stsp usage_diff(void)
2892 9f7d7167 2018-04-29 stsp {
2893 80ddbec8 2018-04-29 stsp endwin();
2894 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2895 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2896 9f7d7167 2018-04-29 stsp exit(1);
2897 b304db33 2018-05-20 stsp }
2898 b304db33 2018-05-20 stsp
2899 6d17833f 2019-11-08 stsp static int
2900 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2901 41605754 2020-11-12 stsp regmatch_t *regmatch)
2902 6d17833f 2019-11-08 stsp {
2903 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2904 6d17833f 2019-11-08 stsp }
2905 6d17833f 2019-11-08 stsp
2906 f26dddb7 2019-11-08 stsp struct tog_color *
2907 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2908 6d17833f 2019-11-08 stsp {
2909 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2910 6d17833f 2019-11-08 stsp
2911 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2912 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2913 6d17833f 2019-11-08 stsp return tc;
2914 6d17833f 2019-11-08 stsp }
2915 6d17833f 2019-11-08 stsp
2916 6d17833f 2019-11-08 stsp return NULL;
2917 6d17833f 2019-11-08 stsp }
2918 6d17833f 2019-11-08 stsp
2919 4ed7e80c 2018-05-20 stsp static const struct got_error *
2920 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2921 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2922 41605754 2020-11-12 stsp {
2923 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2924 41605754 2020-11-12 stsp wchar_t *wline;
2925 41605754 2020-11-12 stsp int width;
2926 41605754 2020-11-12 stsp char *s;
2927 41605754 2020-11-12 stsp
2928 41605754 2020-11-12 stsp *wtotal = 0;
2929 41605754 2020-11-12 stsp
2930 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2931 41605754 2020-11-12 stsp if (s == NULL)
2932 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2933 41605754 2020-11-12 stsp
2934 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2935 41605754 2020-11-12 stsp if (err) {
2936 41605754 2020-11-12 stsp free(s);
2937 41605754 2020-11-12 stsp return err;
2938 41605754 2020-11-12 stsp }
2939 41605754 2020-11-12 stsp waddwstr(window, wline);
2940 41605754 2020-11-12 stsp free(wline);
2941 41605754 2020-11-12 stsp free(s);
2942 41605754 2020-11-12 stsp wlimit -= width;
2943 41605754 2020-11-12 stsp *wtotal += width;
2944 41605754 2020-11-12 stsp
2945 41605754 2020-11-12 stsp if (wlimit > 0) {
2946 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2947 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2948 41605754 2020-11-12 stsp if (s == NULL) {
2949 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2950 41605754 2020-11-12 stsp free(s);
2951 41605754 2020-11-12 stsp return err;
2952 41605754 2020-11-12 stsp }
2953 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2954 41605754 2020-11-12 stsp if (err) {
2955 41605754 2020-11-12 stsp free(s);
2956 41605754 2020-11-12 stsp return err;
2957 41605754 2020-11-12 stsp }
2958 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2959 41605754 2020-11-12 stsp waddwstr(window, wline);
2960 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2961 41605754 2020-11-12 stsp free(wline);
2962 41605754 2020-11-12 stsp free(s);
2963 41605754 2020-11-12 stsp wlimit -= width;
2964 41605754 2020-11-12 stsp *wtotal += width;
2965 41605754 2020-11-12 stsp }
2966 41605754 2020-11-12 stsp
2967 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2968 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2969 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2970 41605754 2020-11-12 stsp if (err)
2971 41605754 2020-11-12 stsp return err;
2972 41605754 2020-11-12 stsp waddwstr(window, wline);
2973 41605754 2020-11-12 stsp free(wline);
2974 41605754 2020-11-12 stsp *wtotal += width;
2975 41605754 2020-11-12 stsp }
2976 41605754 2020-11-12 stsp
2977 41605754 2020-11-12 stsp return NULL;
2978 41605754 2020-11-12 stsp }
2979 41605754 2020-11-12 stsp
2980 41605754 2020-11-12 stsp static const struct got_error *
2981 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2982 26ed57b2 2018-05-19 stsp {
2983 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2984 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2985 61e69b96 2018-05-20 stsp const struct got_error *err;
2986 fe621944 2020-11-10 stsp int nprinted = 0;
2987 b304db33 2018-05-20 stsp char *line;
2988 826082fe 2020-12-10 stsp size_t linesize = 0;
2989 826082fe 2020-12-10 stsp ssize_t linelen;
2990 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2991 61e69b96 2018-05-20 stsp wchar_t *wline;
2992 e0b650dd 2018-05-20 stsp int width;
2993 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2994 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2995 fe621944 2020-11-10 stsp off_t line_offset;
2996 26ed57b2 2018-05-19 stsp
2997 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2998 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2999 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3000 fe621944 2020-11-10 stsp
3001 f7d12f7e 2018-08-01 stsp werase(view->window);
3002 a3404814 2018-09-02 stsp
3003 a3404814 2018-09-02 stsp if (header) {
3004 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3005 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3006 135a2da0 2020-11-11 stsp header) == -1)
3007 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3008 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3009 135a2da0 2020-11-11 stsp free(line);
3010 135a2da0 2020-11-11 stsp if (err)
3011 a3404814 2018-09-02 stsp return err;
3012 a3404814 2018-09-02 stsp
3013 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3014 a3404814 2018-09-02 stsp wstandout(view->window);
3015 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3016 e54cc94a 2020-11-11 stsp free(wline);
3017 e54cc94a 2020-11-11 stsp wline = NULL;
3018 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3019 a3404814 2018-09-02 stsp wstandend(view->window);
3020 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3021 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3022 26ed57b2 2018-05-19 stsp
3023 a3404814 2018-09-02 stsp if (max_lines <= 1)
3024 a3404814 2018-09-02 stsp return NULL;
3025 a3404814 2018-09-02 stsp max_lines--;
3026 a3404814 2018-09-02 stsp }
3027 a3404814 2018-09-02 stsp
3028 89f1a395 2020-12-01 naddy s->eof = 0;
3029 826082fe 2020-12-10 stsp line = NULL;
3030 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3031 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3032 826082fe 2020-12-10 stsp if (linelen == -1) {
3033 826082fe 2020-12-10 stsp if (feof(s->f)) {
3034 826082fe 2020-12-10 stsp s->eof = 1;
3035 826082fe 2020-12-10 stsp break;
3036 826082fe 2020-12-10 stsp }
3037 826082fe 2020-12-10 stsp free(line);
3038 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3039 61e69b96 2018-05-20 stsp }
3040 6d17833f 2019-11-08 stsp
3041 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3042 f26dddb7 2019-11-08 stsp if (tc)
3043 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3044 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3045 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3046 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3047 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3048 41605754 2020-11-12 stsp view->window, regmatch);
3049 41605754 2020-11-12 stsp if (err) {
3050 41605754 2020-11-12 stsp free(line);
3051 41605754 2020-11-12 stsp return err;
3052 41605754 2020-11-12 stsp }
3053 41605754 2020-11-12 stsp } else {
3054 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3055 41605754 2020-11-12 stsp if (err) {
3056 41605754 2020-11-12 stsp free(line);
3057 41605754 2020-11-12 stsp return err;
3058 41605754 2020-11-12 stsp }
3059 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3060 41605754 2020-11-12 stsp free(wline);
3061 41605754 2020-11-12 stsp wline = NULL;
3062 41605754 2020-11-12 stsp }
3063 f26dddb7 2019-11-08 stsp if (tc)
3064 6d17833f 2019-11-08 stsp wattr_off(view->window,
3065 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3066 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3067 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3068 fe621944 2020-11-10 stsp nprinted++;
3069 826082fe 2020-12-10 stsp }
3070 826082fe 2020-12-10 stsp free(line);
3071 fe621944 2020-11-10 stsp if (nprinted >= 1)
3072 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3073 89f1a395 2020-12-01 naddy (nprinted - 1);
3074 fe621944 2020-11-10 stsp else
3075 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3076 26ed57b2 2018-05-19 stsp
3077 1a57306a 2018-09-02 stsp view_vborder(view);
3078 c3e9aa98 2019-05-13 jcs
3079 89f1a395 2020-12-01 naddy if (s->eof) {
3080 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3081 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3082 c3e9aa98 2019-05-13 jcs nprinted++;
3083 c3e9aa98 2019-05-13 jcs }
3084 c3e9aa98 2019-05-13 jcs
3085 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3086 c3e9aa98 2019-05-13 jcs if (err) {
3087 c3e9aa98 2019-05-13 jcs return err;
3088 c3e9aa98 2019-05-13 jcs }
3089 26ed57b2 2018-05-19 stsp
3090 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3091 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3092 e54cc94a 2020-11-11 stsp free(wline);
3093 e54cc94a 2020-11-11 stsp wline = NULL;
3094 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3095 c3e9aa98 2019-05-13 jcs }
3096 c3e9aa98 2019-05-13 jcs
3097 26ed57b2 2018-05-19 stsp return NULL;
3098 abd2672a 2018-12-23 stsp }
3099 abd2672a 2018-12-23 stsp
3100 abd2672a 2018-12-23 stsp static char *
3101 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3102 abd2672a 2018-12-23 stsp {
3103 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3104 09867e48 2019-08-13 stsp char *p, *s;
3105 09867e48 2019-08-13 stsp
3106 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3107 09867e48 2019-08-13 stsp if (tm == NULL)
3108 09867e48 2019-08-13 stsp return NULL;
3109 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3110 09867e48 2019-08-13 stsp if (s == NULL)
3111 09867e48 2019-08-13 stsp return NULL;
3112 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3113 abd2672a 2018-12-23 stsp if (p)
3114 abd2672a 2018-12-23 stsp *p = '\0';
3115 abd2672a 2018-12-23 stsp return s;
3116 9f7d7167 2018-04-29 stsp }
3117 9f7d7167 2018-04-29 stsp
3118 4ed7e80c 2018-05-20 stsp static const struct got_error *
3119 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3120 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3121 0208f208 2020-05-05 stsp {
3122 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3123 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3124 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3125 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3126 0208f208 2020-05-05 stsp
3127 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3128 0208f208 2020-05-05 stsp if (qid != NULL) {
3129 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3130 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3131 0208f208 2020-05-05 stsp qid->id);
3132 0208f208 2020-05-05 stsp if (err)
3133 0208f208 2020-05-05 stsp return err;
3134 0208f208 2020-05-05 stsp
3135 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3136 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3137 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3138 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3139 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3140 aa8b5dd0 2021-08-01 stsp }
3141 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3142 0208f208 2020-05-05 stsp
3143 0208f208 2020-05-05 stsp }
3144 0208f208 2020-05-05 stsp
3145 0208f208 2020-05-05 stsp if (tree_id1) {
3146 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3147 0208f208 2020-05-05 stsp if (err)
3148 0208f208 2020-05-05 stsp goto done;
3149 0208f208 2020-05-05 stsp }
3150 0208f208 2020-05-05 stsp
3151 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3152 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3153 0208f208 2020-05-05 stsp if (err)
3154 0208f208 2020-05-05 stsp goto done;
3155 0208f208 2020-05-05 stsp
3156 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3157 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3158 0208f208 2020-05-05 stsp done:
3159 0208f208 2020-05-05 stsp if (tree1)
3160 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3161 0208f208 2020-05-05 stsp if (tree2)
3162 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3163 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3164 0208f208 2020-05-05 stsp return err;
3165 0208f208 2020-05-05 stsp }
3166 0208f208 2020-05-05 stsp
3167 0208f208 2020-05-05 stsp static const struct got_error *
3168 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3169 abd2672a 2018-12-23 stsp {
3170 fe621944 2020-11-10 stsp off_t *p;
3171 fe621944 2020-11-10 stsp
3172 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3173 fe621944 2020-11-10 stsp if (p == NULL)
3174 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3175 fe621944 2020-11-10 stsp *line_offsets = p;
3176 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3177 fe621944 2020-11-10 stsp (*nlines)++;
3178 fe621944 2020-11-10 stsp return NULL;
3179 fe621944 2020-11-10 stsp }
3180 fe621944 2020-11-10 stsp
3181 fe621944 2020-11-10 stsp static const struct got_error *
3182 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3183 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3184 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3185 fe621944 2020-11-10 stsp {
3186 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3187 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3188 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3189 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3190 45d799e2 2018-12-23 stsp time_t committer_time;
3191 45d799e2 2018-12-23 stsp const char *author, *committer;
3192 8b473291 2019-02-21 stsp char *refs_str = NULL;
3193 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3194 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3195 fe621944 2020-11-10 stsp off_t outoff = 0;
3196 fe621944 2020-11-10 stsp int n;
3197 abd2672a 2018-12-23 stsp
3198 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3199 0208f208 2020-05-05 stsp
3200 8b473291 2019-02-21 stsp if (refs) {
3201 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3202 8b473291 2019-02-21 stsp if (err)
3203 8b473291 2019-02-21 stsp return err;
3204 8b473291 2019-02-21 stsp }
3205 8b473291 2019-02-21 stsp
3206 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3207 abd2672a 2018-12-23 stsp if (err)
3208 abd2672a 2018-12-23 stsp return err;
3209 abd2672a 2018-12-23 stsp
3210 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3211 15a94983 2018-12-23 stsp if (err) {
3212 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3213 15a94983 2018-12-23 stsp goto done;
3214 15a94983 2018-12-23 stsp }
3215 abd2672a 2018-12-23 stsp
3216 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3217 fe621944 2020-11-10 stsp if (err)
3218 fe621944 2020-11-10 stsp goto done;
3219 fe621944 2020-11-10 stsp
3220 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3221 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3222 fe621944 2020-11-10 stsp if (n < 0) {
3223 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3224 abd2672a 2018-12-23 stsp goto done;
3225 abd2672a 2018-12-23 stsp }
3226 fe621944 2020-11-10 stsp outoff += n;
3227 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3228 fe621944 2020-11-10 stsp if (err)
3229 fe621944 2020-11-10 stsp goto done;
3230 fe621944 2020-11-10 stsp
3231 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3232 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3233 fe621944 2020-11-10 stsp if (n < 0) {
3234 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3235 abd2672a 2018-12-23 stsp goto done;
3236 abd2672a 2018-12-23 stsp }
3237 fe621944 2020-11-10 stsp outoff += n;
3238 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3239 fe621944 2020-11-10 stsp if (err)
3240 fe621944 2020-11-10 stsp goto done;
3241 fe621944 2020-11-10 stsp
3242 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3243 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3244 fe621944 2020-11-10 stsp if (datestr) {
3245 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3246 fe621944 2020-11-10 stsp if (n < 0) {
3247 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3248 fe621944 2020-11-10 stsp goto done;
3249 fe621944 2020-11-10 stsp }
3250 fe621944 2020-11-10 stsp outoff += n;
3251 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3252 fe621944 2020-11-10 stsp if (err)
3253 fe621944 2020-11-10 stsp goto done;
3254 abd2672a 2018-12-23 stsp }
3255 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3256 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3257 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3258 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3259 fe621944 2020-11-10 stsp if (n < 0) {
3260 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3261 fe621944 2020-11-10 stsp goto done;
3262 fe621944 2020-11-10 stsp }
3263 fe621944 2020-11-10 stsp outoff += n;
3264 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3265 fe621944 2020-11-10 stsp if (err)
3266 fe621944 2020-11-10 stsp goto done;
3267 abd2672a 2018-12-23 stsp }
3268 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3269 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3270 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3271 ac4dc263 2021-09-24 thomas int pn = 1;
3272 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3273 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3274 ac4dc263 2021-09-24 thomas err = got_object_id_str(&id_str, qid->id);
3275 ac4dc263 2021-09-24 thomas if (err)
3276 ac4dc263 2021-09-24 thomas goto done;
3277 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3278 ac4dc263 2021-09-24 thomas if (n < 0) {
3279 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3280 ac4dc263 2021-09-24 thomas goto done;
3281 ac4dc263 2021-09-24 thomas }
3282 ac4dc263 2021-09-24 thomas outoff += n;
3283 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3284 ac4dc263 2021-09-24 thomas if (err)
3285 ac4dc263 2021-09-24 thomas goto done;
3286 ac4dc263 2021-09-24 thomas free(id_str);
3287 ac4dc263 2021-09-24 thomas id_str = NULL;
3288 ac4dc263 2021-09-24 thomas }
3289 ac4dc263 2021-09-24 thomas }
3290 ac4dc263 2021-09-24 thomas
3291 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3292 5943eee2 2019-08-13 stsp if (err)
3293 5943eee2 2019-08-13 stsp goto done;
3294 fe621944 2020-11-10 stsp s = logmsg;
3295 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3296 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3297 fe621944 2020-11-10 stsp if (n < 0) {
3298 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3299 fe621944 2020-11-10 stsp goto done;
3300 fe621944 2020-11-10 stsp }
3301 fe621944 2020-11-10 stsp outoff += n;
3302 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3303 fe621944 2020-11-10 stsp if (err)
3304 fe621944 2020-11-10 stsp goto done;
3305 abd2672a 2018-12-23 stsp }
3306 fe621944 2020-11-10 stsp
3307 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3308 0208f208 2020-05-05 stsp if (err)
3309 0208f208 2020-05-05 stsp goto done;
3310 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3311 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3312 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3313 fe621944 2020-11-10 stsp if (n < 0) {
3314 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3315 fe621944 2020-11-10 stsp goto done;
3316 fe621944 2020-11-10 stsp }
3317 fe621944 2020-11-10 stsp outoff += n;
3318 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3319 fe621944 2020-11-10 stsp if (err)
3320 fe621944 2020-11-10 stsp goto done;
3321 0208f208 2020-05-05 stsp free((char *)pe->path);
3322 0208f208 2020-05-05 stsp free(pe->data);
3323 0208f208 2020-05-05 stsp }
3324 fe621944 2020-11-10 stsp
3325 0208f208 2020-05-05 stsp fputc('\n', outfile);
3326 fe621944 2020-11-10 stsp outoff++;
3327 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3328 abd2672a 2018-12-23 stsp done:
3329 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3330 abd2672a 2018-12-23 stsp free(id_str);
3331 5943eee2 2019-08-13 stsp free(logmsg);
3332 8b473291 2019-02-21 stsp free(refs_str);
3333 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3334 7510f233 2020-08-09 stsp if (err) {
3335 ae6a6978 2020-08-09 stsp free(*line_offsets);
3336 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3337 ae6a6978 2020-08-09 stsp *nlines = 0;
3338 7510f233 2020-08-09 stsp }
3339 fe621944 2020-11-10 stsp return err;
3340 abd2672a 2018-12-23 stsp }
3341 abd2672a 2018-12-23 stsp
3342 abd2672a 2018-12-23 stsp static const struct got_error *
3343 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3344 26ed57b2 2018-05-19 stsp {
3345 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3346 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3347 15a94983 2018-12-23 stsp int obj_type;
3348 fe621944 2020-11-10 stsp
3349 fe621944 2020-11-10 stsp free(s->line_offsets);
3350 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3351 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3352 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3353 fe621944 2020-11-10 stsp s->nlines = 0;
3354 26ed57b2 2018-05-19 stsp
3355 511a516b 2018-05-19 stsp f = got_opentemp();
3356 48ae06ee 2018-10-18 stsp if (f == NULL) {
3357 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3358 48ae06ee 2018-10-18 stsp goto done;
3359 48ae06ee 2018-10-18 stsp }
3360 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3361 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3362 fb43ecf1 2019-02-11 stsp goto done;
3363 fb43ecf1 2019-02-11 stsp }
3364 48ae06ee 2018-10-18 stsp s->f = f;
3365 26ed57b2 2018-05-19 stsp
3366 15a94983 2018-12-23 stsp if (s->id1)
3367 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3368 15a94983 2018-12-23 stsp else
3369 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3370 15a94983 2018-12-23 stsp if (err)
3371 15a94983 2018-12-23 stsp goto done;
3372 15a94983 2018-12-23 stsp
3373 15a94983 2018-12-23 stsp switch (obj_type) {
3374 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3375 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3376 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3377 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3378 26ed57b2 2018-05-19 stsp break;
3379 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3380 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3381 cc8021af 2021-10-12 thomas s->id1, s->id2, NULL, "", "", s->diff_context,
3382 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3383 26ed57b2 2018-05-19 stsp break;
3384 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3385 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3386 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3387 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3388 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3389 abd2672a 2018-12-23 stsp
3390 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3391 abd2672a 2018-12-23 stsp if (err)
3392 3ffacbe1 2020-02-02 tracey goto done;
3393 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3394 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3395 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3396 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3397 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3398 f44b1f58 2020-02-02 tracey if (err)
3399 f44b1f58 2020-02-02 tracey goto done;
3400 f44b1f58 2020-02-02 tracey } else {
3401 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3402 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3403 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3404 fe621944 2020-11-10 stsp err = write_commit_info(
3405 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3406 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3407 f44b1f58 2020-02-02 tracey if (err)
3408 f44b1f58 2020-02-02 tracey goto done;
3409 f5404e4e 2020-02-02 tracey break;
3410 15a087fe 2019-02-21 stsp }
3411 abd2672a 2018-12-23 stsp }
3412 abd2672a 2018-12-23 stsp }
3413 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3414 abd2672a 2018-12-23 stsp
3415 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3416 cc8021af 2021-10-12 thomas s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3417 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3418 26ed57b2 2018-05-19 stsp break;
3419 abd2672a 2018-12-23 stsp }
3420 26ed57b2 2018-05-19 stsp default:
3421 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3422 48ae06ee 2018-10-18 stsp break;
3423 26ed57b2 2018-05-19 stsp }
3424 f44b1f58 2020-02-02 tracey if (err)
3425 f44b1f58 2020-02-02 tracey goto done;
3426 48ae06ee 2018-10-18 stsp done:
3427 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3428 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3429 48ae06ee 2018-10-18 stsp return err;
3430 48ae06ee 2018-10-18 stsp }
3431 26ed57b2 2018-05-19 stsp
3432 f5215bb9 2019-02-22 stsp static void
3433 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3434 f5215bb9 2019-02-22 stsp {
3435 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3436 f5215bb9 2019-02-22 stsp update_panels();
3437 f5215bb9 2019-02-22 stsp doupdate();
3438 f44b1f58 2020-02-02 tracey }
3439 f44b1f58 2020-02-02 tracey
3440 f44b1f58 2020-02-02 tracey static const struct got_error *
3441 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3442 f44b1f58 2020-02-02 tracey {
3443 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3444 f44b1f58 2020-02-02 tracey
3445 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3446 f44b1f58 2020-02-02 tracey return NULL;
3447 f44b1f58 2020-02-02 tracey }
3448 f44b1f58 2020-02-02 tracey
3449 f44b1f58 2020-02-02 tracey static const struct got_error *
3450 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3451 f44b1f58 2020-02-02 tracey {
3452 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3453 f44b1f58 2020-02-02 tracey int lineno;
3454 826082fe 2020-12-10 stsp char *line = NULL;
3455 826082fe 2020-12-10 stsp size_t linesize = 0;
3456 826082fe 2020-12-10 stsp ssize_t linelen;
3457 f44b1f58 2020-02-02 tracey
3458 f44b1f58 2020-02-02 tracey if (!view->searching) {
3459 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3460 f44b1f58 2020-02-02 tracey return NULL;
3461 f44b1f58 2020-02-02 tracey }
3462 f44b1f58 2020-02-02 tracey
3463 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3464 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3465 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3466 f44b1f58 2020-02-02 tracey else
3467 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3468 4b3f9dac 2021-12-17 thomas } else
3469 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3470 f44b1f58 2020-02-02 tracey
3471 f44b1f58 2020-02-02 tracey while (1) {
3472 f44b1f58 2020-02-02 tracey off_t offset;
3473 f44b1f58 2020-02-02 tracey
3474 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3475 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3476 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3477 f44b1f58 2020-02-02 tracey break;
3478 f44b1f58 2020-02-02 tracey }
3479 f44b1f58 2020-02-02 tracey
3480 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3481 f44b1f58 2020-02-02 tracey lineno = 1;
3482 f44b1f58 2020-02-02 tracey else
3483 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3484 f44b1f58 2020-02-02 tracey }
3485 f44b1f58 2020-02-02 tracey
3486 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3487 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3488 f44b1f58 2020-02-02 tracey free(line);
3489 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3490 f44b1f58 2020-02-02 tracey }
3491 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3492 826082fe 2020-12-10 stsp if (linelen != -1 &&
3493 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3494 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3495 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3496 f44b1f58 2020-02-02 tracey break;
3497 f44b1f58 2020-02-02 tracey }
3498 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3499 f44b1f58 2020-02-02 tracey lineno++;
3500 f44b1f58 2020-02-02 tracey else
3501 f44b1f58 2020-02-02 tracey lineno--;
3502 f44b1f58 2020-02-02 tracey }
3503 826082fe 2020-12-10 stsp free(line);
3504 f44b1f58 2020-02-02 tracey
3505 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3506 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3507 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3508 f44b1f58 2020-02-02 tracey }
3509 f44b1f58 2020-02-02 tracey
3510 f44b1f58 2020-02-02 tracey return NULL;
3511 f5215bb9 2019-02-22 stsp }
3512 f5215bb9 2019-02-22 stsp
3513 48ae06ee 2018-10-18 stsp static const struct got_error *
3514 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3515 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3516 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3517 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3518 48ae06ee 2018-10-18 stsp {
3519 48ae06ee 2018-10-18 stsp const struct got_error *err;
3520 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3521 5dc9f4bc 2018-08-04 stsp
3522 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3523 15a94983 2018-12-23 stsp int type1, type2;
3524 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3525 15a94983 2018-12-23 stsp if (err)
3526 15a94983 2018-12-23 stsp return err;
3527 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3528 15a94983 2018-12-23 stsp if (err)
3529 15a94983 2018-12-23 stsp return err;
3530 15a94983 2018-12-23 stsp
3531 15a94983 2018-12-23 stsp if (type1 != type2)
3532 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3533 15a94983 2018-12-23 stsp }
3534 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3535 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3536 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3537 f44b1f58 2020-02-02 tracey s->repo = repo;
3538 f44b1f58 2020-02-02 tracey s->id1 = id1;
3539 f44b1f58 2020-02-02 tracey s->id2 = id2;
3540 3dbaef42 2020-11-24 stsp s->label1 = label1;
3541 3dbaef42 2020-11-24 stsp s->label2 = label2;
3542 48ae06ee 2018-10-18 stsp
3543 15a94983 2018-12-23 stsp if (id1) {
3544 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3545 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3546 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3547 48ae06ee 2018-10-18 stsp } else
3548 5465d566 2020-02-01 tracey s->id1 = NULL;
3549 48ae06ee 2018-10-18 stsp
3550 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3551 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3552 5465d566 2020-02-01 tracey free(s->id1);
3553 5465d566 2020-02-01 tracey s->id1 = NULL;
3554 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3555 48ae06ee 2018-10-18 stsp }
3556 5465d566 2020-02-01 tracey s->f = NULL;
3557 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3558 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3559 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3560 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3561 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3562 5465d566 2020-02-01 tracey s->log_view = log_view;
3563 5465d566 2020-02-01 tracey s->repo = repo;
3564 6d17833f 2019-11-08 stsp
3565 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3566 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3567 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3568 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3569 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3570 6d17833f 2019-11-08 stsp if (err)
3571 6d17833f 2019-11-08 stsp return err;
3572 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3573 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3574 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3575 6d17833f 2019-11-08 stsp if (err) {
3576 5465d566 2020-02-01 tracey free_colors(&s->colors);
3577 6d17833f 2019-11-08 stsp return err;
3578 6d17833f 2019-11-08 stsp }
3579 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3580 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3581 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3582 6d17833f 2019-11-08 stsp if (err) {
3583 5465d566 2020-02-01 tracey free_colors(&s->colors);
3584 6d17833f 2019-11-08 stsp return err;
3585 6d17833f 2019-11-08 stsp }
3586 6d17833f 2019-11-08 stsp
3587 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3588 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3589 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3590 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3591 6d17833f 2019-11-08 stsp if (err) {
3592 5465d566 2020-02-01 tracey free_colors(&s->colors);
3593 6d17833f 2019-11-08 stsp return err;
3594 6d17833f 2019-11-08 stsp }
3595 11b20872 2019-11-08 stsp
3596 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3597 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3598 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3599 11b20872 2019-11-08 stsp if (err) {
3600 5465d566 2020-02-01 tracey free_colors(&s->colors);
3601 11b20872 2019-11-08 stsp return err;
3602 11b20872 2019-11-08 stsp }
3603 11b20872 2019-11-08 stsp
3604 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3605 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3606 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3607 11b20872 2019-11-08 stsp if (err) {
3608 5465d566 2020-02-01 tracey free_colors(&s->colors);
3609 11b20872 2019-11-08 stsp return err;
3610 11b20872 2019-11-08 stsp }
3611 6d17833f 2019-11-08 stsp }
3612 5dc9f4bc 2018-08-04 stsp
3613 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3614 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3615 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3616 f5215bb9 2019-02-22 stsp
3617 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3618 fe621944 2020-11-10 stsp s->nlines = 0;
3619 5465d566 2020-02-01 tracey err = create_diff(s);
3620 48ae06ee 2018-10-18 stsp if (err) {
3621 5465d566 2020-02-01 tracey free(s->id1);
3622 5465d566 2020-02-01 tracey s->id1 = NULL;
3623 5465d566 2020-02-01 tracey free(s->id2);
3624 5465d566 2020-02-01 tracey s->id2 = NULL;
3625 78756c87 2020-11-24 stsp free_colors(&s->colors);
3626 48ae06ee 2018-10-18 stsp return err;
3627 48ae06ee 2018-10-18 stsp }
3628 48ae06ee 2018-10-18 stsp
3629 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3630 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3631 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3632 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3633 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3634 e5a0f69f 2018-08-18 stsp
3635 5dc9f4bc 2018-08-04 stsp return NULL;
3636 5dc9f4bc 2018-08-04 stsp }
3637 5dc9f4bc 2018-08-04 stsp
3638 e5a0f69f 2018-08-18 stsp static const struct got_error *
3639 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3640 5dc9f4bc 2018-08-04 stsp {
3641 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3642 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3643 5465d566 2020-02-01 tracey
3644 5465d566 2020-02-01 tracey free(s->id1);
3645 5465d566 2020-02-01 tracey s->id1 = NULL;
3646 5465d566 2020-02-01 tracey free(s->id2);
3647 5465d566 2020-02-01 tracey s->id2 = NULL;
3648 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3649 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3650 5465d566 2020-02-01 tracey free_colors(&s->colors);
3651 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3652 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3653 fe621944 2020-11-10 stsp s->nlines = 0;
3654 e5a0f69f 2018-08-18 stsp return err;
3655 5dc9f4bc 2018-08-04 stsp }
3656 5dc9f4bc 2018-08-04 stsp
3657 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3658 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3659 5dc9f4bc 2018-08-04 stsp {
3660 a3404814 2018-09-02 stsp const struct got_error *err;
3661 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3662 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3663 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3664 a3404814 2018-09-02 stsp
3665 a3404814 2018-09-02 stsp if (s->id1) {
3666 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3667 a3404814 2018-09-02 stsp if (err)
3668 a3404814 2018-09-02 stsp return err;
3669 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3670 3dbaef42 2020-11-24 stsp } else
3671 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3672 3dbaef42 2020-11-24 stsp
3673 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3674 a3404814 2018-09-02 stsp if (err)
3675 a3404814 2018-09-02 stsp return err;
3676 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3677 26ed57b2 2018-05-19 stsp
3678 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3679 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3680 a3404814 2018-09-02 stsp free(id_str1);
3681 a3404814 2018-09-02 stsp free(id_str2);
3682 a3404814 2018-09-02 stsp return err;
3683 a3404814 2018-09-02 stsp }
3684 a3404814 2018-09-02 stsp free(id_str1);
3685 a3404814 2018-09-02 stsp free(id_str2);
3686 a3404814 2018-09-02 stsp
3687 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3688 267bb3b8 2021-08-01 stsp free(header);
3689 267bb3b8 2021-08-01 stsp return err;
3690 15a087fe 2019-02-21 stsp }
3691 15a087fe 2019-02-21 stsp
3692 15a087fe 2019-02-21 stsp static const struct got_error *
3693 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3694 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3695 15a087fe 2019-02-21 stsp {
3696 d7a04538 2019-02-21 stsp const struct got_error *err;
3697 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3698 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3699 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3700 15a087fe 2019-02-21 stsp
3701 15a087fe 2019-02-21 stsp free(s->id2);
3702 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3703 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3704 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3705 15a087fe 2019-02-21 stsp
3706 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3707 d7a04538 2019-02-21 stsp if (err)
3708 d7a04538 2019-02-21 stsp return err;
3709 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3710 15a087fe 2019-02-21 stsp free(s->id1);
3711 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3712 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3713 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3714 15a087fe 2019-02-21 stsp return NULL;
3715 0cf4efb1 2018-09-29 stsp }
3716 0cf4efb1 2018-09-29 stsp
3717 0cf4efb1 2018-09-29 stsp static const struct got_error *
3718 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3719 e5a0f69f 2018-08-18 stsp {
3720 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3721 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3722 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3723 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3724 826082fe 2020-12-10 stsp char *line = NULL;
3725 826082fe 2020-12-10 stsp size_t linesize = 0;
3726 826082fe 2020-12-10 stsp ssize_t linelen;
3727 e5a0f69f 2018-08-18 stsp int i;
3728 e5a0f69f 2018-08-18 stsp
3729 e5a0f69f 2018-08-18 stsp switch (ch) {
3730 64453f7e 2020-11-21 stsp case 'a':
3731 3dbaef42 2020-11-24 stsp case 'w':
3732 3dbaef42 2020-11-24 stsp if (ch == 'a')
3733 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3734 3dbaef42 2020-11-24 stsp if (ch == 'w')
3735 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3736 64453f7e 2020-11-21 stsp wclear(view->window);
3737 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3738 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3739 aa61903a 2021-12-31 thomas s->matched_line = 0;
3740 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3741 64453f7e 2020-11-21 stsp err = create_diff(s);
3742 912a3f79 2021-08-30 j break;
3743 912a3f79 2021-08-30 j case 'g':
3744 912a3f79 2021-08-30 j case KEY_HOME:
3745 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3746 64453f7e 2020-11-21 stsp break;
3747 912a3f79 2021-08-30 j case 'G':
3748 912a3f79 2021-08-30 j case KEY_END:
3749 912a3f79 2021-08-30 j if (s->eof)
3750 912a3f79 2021-08-30 j break;
3751 912a3f79 2021-08-30 j
3752 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3753 912a3f79 2021-08-30 j s->eof = 1;
3754 912a3f79 2021-08-30 j break;
3755 1e37a5c2 2019-05-12 jcs case 'k':
3756 1e37a5c2 2019-05-12 jcs case KEY_UP:
3757 f7140bf5 2021-10-17 thomas case CTRL('p'):
3758 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3759 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3760 1e37a5c2 2019-05-12 jcs break;
3761 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3762 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3763 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3764 26ed57b2 2018-05-19 stsp break;
3765 1e37a5c2 2019-05-12 jcs i = 0;
3766 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3767 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3768 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3769 1e37a5c2 2019-05-12 jcs break;
3770 1e37a5c2 2019-05-12 jcs case 'j':
3771 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3772 f7140bf5 2021-10-17 thomas case CTRL('n'):
3773 1e37a5c2 2019-05-12 jcs if (!s->eof)
3774 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3775 1e37a5c2 2019-05-12 jcs break;
3776 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3777 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3778 1e37a5c2 2019-05-12 jcs case ' ':
3779 00ba99a7 2019-05-12 jcs if (s->eof)
3780 1e37a5c2 2019-05-12 jcs break;
3781 1e37a5c2 2019-05-12 jcs i = 0;
3782 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3783 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3784 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3785 826082fe 2020-12-10 stsp if (linelen == -1) {
3786 826082fe 2020-12-10 stsp if (feof(s->f)) {
3787 826082fe 2020-12-10 stsp s->eof = 1;
3788 826082fe 2020-12-10 stsp } else
3789 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3790 34bc9ec9 2019-02-22 stsp break;
3791 826082fe 2020-12-10 stsp }
3792 1e37a5c2 2019-05-12 jcs }
3793 826082fe 2020-12-10 stsp free(line);
3794 1e37a5c2 2019-05-12 jcs break;
3795 1e37a5c2 2019-05-12 jcs case '[':
3796 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3797 1e37a5c2 2019-05-12 jcs s->diff_context--;
3798 aa61903a 2021-12-31 thomas s->matched_line = 0;
3799 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3800 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3801 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3802 27829c9e 2020-11-21 stsp s->nlines) {
3803 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3804 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3805 27829c9e 2020-11-21 stsp }
3806 1e37a5c2 2019-05-12 jcs }
3807 1e37a5c2 2019-05-12 jcs break;
3808 1e37a5c2 2019-05-12 jcs case ']':
3809 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3810 1e37a5c2 2019-05-12 jcs s->diff_context++;
3811 aa61903a 2021-12-31 thomas s->matched_line = 0;
3812 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3813 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3814 1e37a5c2 2019-05-12 jcs }
3815 1e37a5c2 2019-05-12 jcs break;
3816 1e37a5c2 2019-05-12 jcs case '<':
3817 1e37a5c2 2019-05-12 jcs case ',':
3818 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3819 48ae06ee 2018-10-18 stsp break;
3820 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3821 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3822 6524637e 2019-02-21 stsp
3823 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3824 1e37a5c2 2019-05-12 jcs if (err)
3825 1e37a5c2 2019-05-12 jcs break;
3826 15a087fe 2019-02-21 stsp
3827 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3828 5a8b5076 2020-12-05 stsp break;
3829 5a8b5076 2020-12-05 stsp
3830 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3831 1e37a5c2 2019-05-12 jcs if (err)
3832 1e37a5c2 2019-05-12 jcs break;
3833 15a087fe 2019-02-21 stsp
3834 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3835 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3836 aa61903a 2021-12-31 thomas s->matched_line = 0;
3837 15a087fe 2019-02-21 stsp
3838 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3839 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3840 1e37a5c2 2019-05-12 jcs break;
3841 1e37a5c2 2019-05-12 jcs case '>':
3842 1e37a5c2 2019-05-12 jcs case '.':
3843 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3844 15a087fe 2019-02-21 stsp break;
3845 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3846 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3847 5e224a3e 2019-02-22 stsp
3848 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3849 1e37a5c2 2019-05-12 jcs if (err)
3850 5a8b5076 2020-12-05 stsp break;
3851 5a8b5076 2020-12-05 stsp
3852 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3853 1e37a5c2 2019-05-12 jcs break;
3854 15a087fe 2019-02-21 stsp
3855 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3856 1e37a5c2 2019-05-12 jcs if (err)
3857 1e37a5c2 2019-05-12 jcs break;
3858 15a087fe 2019-02-21 stsp
3859 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3860 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3861 aa61903a 2021-12-31 thomas s->matched_line = 0;
3862 1e37a5c2 2019-05-12 jcs
3863 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3864 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3865 1e37a5c2 2019-05-12 jcs break;
3866 1e37a5c2 2019-05-12 jcs default:
3867 1e37a5c2 2019-05-12 jcs break;
3868 26ed57b2 2018-05-19 stsp }
3869 e5a0f69f 2018-08-18 stsp
3870 bcbd79e2 2018-08-19 stsp return err;
3871 26ed57b2 2018-05-19 stsp }
3872 26ed57b2 2018-05-19 stsp
3873 4ed7e80c 2018-05-20 stsp static const struct got_error *
3874 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3875 9f7d7167 2018-04-29 stsp {
3876 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3877 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3878 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3879 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3880 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3881 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3882 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3883 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3884 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3885 3dbaef42 2020-11-24 stsp const char *errstr;
3886 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3887 70ac5f84 2019-03-28 stsp
3888 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3889 26ed57b2 2018-05-19 stsp switch (ch) {
3890 64453f7e 2020-11-21 stsp case 'a':
3891 64453f7e 2020-11-21 stsp force_text_diff = 1;
3892 3dbaef42 2020-11-24 stsp break;
3893 3dbaef42 2020-11-24 stsp case 'C':
3894 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3895 3dbaef42 2020-11-24 stsp &errstr);
3896 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3897 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
3898 8f666e67 2022-02-12 thomas errstr, errstr);
3899 64453f7e 2020-11-21 stsp break;
3900 09b5bff8 2020-02-23 naddy case 'r':
3901 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3902 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3903 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3904 09b5bff8 2020-02-23 naddy optarg);
3905 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3906 09b5bff8 2020-02-23 naddy break;
3907 3dbaef42 2020-11-24 stsp case 'w':
3908 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3909 3dbaef42 2020-11-24 stsp break;
3910 26ed57b2 2018-05-19 stsp default:
3911 17020d27 2019-03-07 stsp usage_diff();
3912 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3913 26ed57b2 2018-05-19 stsp }
3914 26ed57b2 2018-05-19 stsp }
3915 26ed57b2 2018-05-19 stsp
3916 26ed57b2 2018-05-19 stsp argc -= optind;
3917 26ed57b2 2018-05-19 stsp argv += optind;
3918 26ed57b2 2018-05-19 stsp
3919 26ed57b2 2018-05-19 stsp if (argc == 0) {
3920 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3921 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3922 15a94983 2018-12-23 stsp id_str1 = argv[0];
3923 15a94983 2018-12-23 stsp id_str2 = argv[1];
3924 26ed57b2 2018-05-19 stsp } else
3925 26ed57b2 2018-05-19 stsp usage_diff();
3926 eb6600df 2019-01-04 stsp
3927 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3928 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3929 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3930 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3931 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3932 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3933 c156c7a4 2020-12-18 stsp goto done;
3934 a273ac94 2020-02-23 naddy if (worktree)
3935 a273ac94 2020-02-23 naddy repo_path =
3936 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3937 a273ac94 2020-02-23 naddy else
3938 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3939 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3940 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3941 c156c7a4 2020-12-18 stsp goto done;
3942 c156c7a4 2020-12-18 stsp }
3943 a273ac94 2020-02-23 naddy }
3944 a273ac94 2020-02-23 naddy
3945 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3946 eb6600df 2019-01-04 stsp if (error)
3947 eb6600df 2019-01-04 stsp goto done;
3948 26ed57b2 2018-05-19 stsp
3949 a273ac94 2020-02-23 naddy init_curses();
3950 a273ac94 2020-02-23 naddy
3951 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3952 51a10b52 2020-12-26 stsp if (error)
3953 51a10b52 2020-12-26 stsp goto done;
3954 51a10b52 2020-12-26 stsp
3955 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3956 26ed57b2 2018-05-19 stsp if (error)
3957 26ed57b2 2018-05-19 stsp goto done;
3958 26ed57b2 2018-05-19 stsp
3959 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3960 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3961 26ed57b2 2018-05-19 stsp if (error)
3962 26ed57b2 2018-05-19 stsp goto done;
3963 26ed57b2 2018-05-19 stsp
3964 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3965 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3966 26ed57b2 2018-05-19 stsp if (error)
3967 26ed57b2 2018-05-19 stsp goto done;
3968 26ed57b2 2018-05-19 stsp
3969 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3970 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3971 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3972 ea5e7bb5 2018-08-01 stsp goto done;
3973 ea5e7bb5 2018-08-01 stsp }
3974 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3975 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3976 5dc9f4bc 2018-08-04 stsp if (error)
3977 5dc9f4bc 2018-08-04 stsp goto done;
3978 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3979 26ed57b2 2018-05-19 stsp done:
3980 3dbaef42 2020-11-24 stsp free(label1);
3981 3dbaef42 2020-11-24 stsp free(label2);
3982 c02c541e 2019-03-29 stsp free(repo_path);
3983 a273ac94 2020-02-23 naddy free(cwd);
3984 1d0f4054 2021-06-17 stsp if (repo) {
3985 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3986 1d0f4054 2021-06-17 stsp if (error == NULL)
3987 1d0f4054 2021-06-17 stsp error = close_err;
3988 1d0f4054 2021-06-17 stsp }
3989 a273ac94 2020-02-23 naddy if (worktree)
3990 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3991 51a10b52 2020-12-26 stsp tog_free_refs();
3992 26ed57b2 2018-05-19 stsp return error;
3993 9f7d7167 2018-04-29 stsp }
3994 9f7d7167 2018-04-29 stsp
3995 4ed7e80c 2018-05-20 stsp __dead static void
3996 9f7d7167 2018-04-29 stsp usage_blame(void)
3997 9f7d7167 2018-04-29 stsp {
3998 80ddbec8 2018-04-29 stsp endwin();
3999 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4000 9f7d7167 2018-04-29 stsp getprogname());
4001 9f7d7167 2018-04-29 stsp exit(1);
4002 9f7d7167 2018-04-29 stsp }
4003 84451b3e 2018-07-10 stsp
4004 84451b3e 2018-07-10 stsp struct tog_blame_line {
4005 84451b3e 2018-07-10 stsp int annotated;
4006 84451b3e 2018-07-10 stsp struct got_object_id *id;
4007 84451b3e 2018-07-10 stsp };
4008 9f7d7167 2018-04-29 stsp
4009 4ed7e80c 2018-05-20 stsp static const struct got_error *
4010 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4011 84451b3e 2018-07-10 stsp {
4012 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4013 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4014 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4015 84451b3e 2018-07-10 stsp const struct got_error *err;
4016 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4017 826082fe 2020-12-10 stsp char *line = NULL;
4018 826082fe 2020-12-10 stsp size_t linesize = 0;
4019 826082fe 2020-12-10 stsp ssize_t linelen;
4020 84451b3e 2018-07-10 stsp wchar_t *wline;
4021 27a741e5 2019-09-11 stsp int width;
4022 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4023 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4024 ab089a2a 2018-07-12 stsp char *id_str;
4025 11b20872 2019-11-08 stsp struct tog_color *tc;
4026 ab089a2a 2018-07-12 stsp
4027 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
4028 ab089a2a 2018-07-12 stsp if (err)
4029 ab089a2a 2018-07-12 stsp return err;
4030 84451b3e 2018-07-10 stsp
4031 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4032 f7d12f7e 2018-08-01 stsp werase(view->window);
4033 84451b3e 2018-07-10 stsp
4034 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4035 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4036 ab089a2a 2018-07-12 stsp free(id_str);
4037 ab089a2a 2018-07-12 stsp return err;
4038 ab089a2a 2018-07-12 stsp }
4039 ab089a2a 2018-07-12 stsp
4040 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4041 ab089a2a 2018-07-12 stsp free(line);
4042 2550e4c3 2018-07-13 stsp line = NULL;
4043 1cae65b4 2019-09-22 stsp if (err)
4044 1cae65b4 2019-09-22 stsp return err;
4045 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4046 a3404814 2018-09-02 stsp wstandout(view->window);
4047 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4048 11b20872 2019-11-08 stsp if (tc)
4049 11b20872 2019-11-08 stsp wattr_on(view->window,
4050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4051 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4052 11b20872 2019-11-08 stsp if (tc)
4053 11b20872 2019-11-08 stsp wattr_off(view->window,
4054 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4055 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4056 a3404814 2018-09-02 stsp wstandend(view->window);
4057 2550e4c3 2018-07-13 stsp free(wline);
4058 2550e4c3 2018-07-13 stsp wline = NULL;
4059 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4060 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4061 ab089a2a 2018-07-12 stsp
4062 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4063 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4064 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4065 ab089a2a 2018-07-12 stsp free(id_str);
4066 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4067 ab089a2a 2018-07-12 stsp }
4068 ab089a2a 2018-07-12 stsp free(id_str);
4069 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4070 3f60a8ef 2018-07-10 stsp free(line);
4071 2550e4c3 2018-07-13 stsp line = NULL;
4072 3f60a8ef 2018-07-10 stsp if (err)
4073 3f60a8ef 2018-07-10 stsp return err;
4074 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4075 2550e4c3 2018-07-13 stsp free(wline);
4076 2550e4c3 2018-07-13 stsp wline = NULL;
4077 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4078 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4079 3f60a8ef 2018-07-10 stsp
4080 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4081 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4082 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4083 826082fe 2020-12-10 stsp if (linelen == -1) {
4084 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4085 826082fe 2020-12-10 stsp s->eof = 1;
4086 826082fe 2020-12-10 stsp break;
4087 826082fe 2020-12-10 stsp }
4088 84451b3e 2018-07-10 stsp free(line);
4089 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4090 84451b3e 2018-07-10 stsp }
4091 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4092 826082fe 2020-12-10 stsp continue;
4093 84451b3e 2018-07-10 stsp
4094 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4095 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4096 b700b5d6 2018-07-10 stsp
4097 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4098 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4099 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4100 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4101 27a741e5 2019-09-11 stsp !(view->focussed &&
4102 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4103 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4104 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4105 8d0fe45a 2019-08-12 stsp char *id_str;
4106 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4107 8d0fe45a 2019-08-12 stsp if (err) {
4108 8d0fe45a 2019-08-12 stsp free(line);
4109 8d0fe45a 2019-08-12 stsp return err;
4110 8d0fe45a 2019-08-12 stsp }
4111 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4112 11b20872 2019-11-08 stsp if (tc)
4113 11b20872 2019-11-08 stsp wattr_on(view->window,
4114 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4115 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4116 11b20872 2019-11-08 stsp if (tc)
4117 11b20872 2019-11-08 stsp wattr_off(view->window,
4118 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4119 8d0fe45a 2019-08-12 stsp free(id_str);
4120 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4121 8d0fe45a 2019-08-12 stsp } else {
4122 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4123 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4124 84451b3e 2018-07-10 stsp }
4125 ee41ec32 2018-07-10 stsp } else {
4126 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4127 ee41ec32 2018-07-10 stsp prev_id = NULL;
4128 ee41ec32 2018-07-10 stsp }
4129 84451b3e 2018-07-10 stsp
4130 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4131 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4132 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4133 27a741e5 2019-09-11 stsp
4134 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4135 41605754 2020-11-12 stsp width = 9;
4136 41605754 2020-11-12 stsp wline = wcsdup(L"");
4137 41605754 2020-11-12 stsp if (wline == NULL) {
4138 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4139 41605754 2020-11-12 stsp free(line);
4140 41605754 2020-11-12 stsp return err;
4141 41605754 2020-11-12 stsp }
4142 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4143 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4144 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4145 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4146 41605754 2020-11-12 stsp view->window, regmatch);
4147 41605754 2020-11-12 stsp if (err) {
4148 41605754 2020-11-12 stsp free(line);
4149 41605754 2020-11-12 stsp return err;
4150 41605754 2020-11-12 stsp }
4151 41605754 2020-11-12 stsp width += 9;
4152 41605754 2020-11-12 stsp } else {
4153 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4154 41605754 2020-11-12 stsp view->ncols - 9, 9);
4155 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4156 41605754 2020-11-12 stsp free(wline);
4157 41605754 2020-11-12 stsp wline = NULL;
4158 41605754 2020-11-12 stsp width += 9;
4159 41605754 2020-11-12 stsp }
4160 41605754 2020-11-12 stsp
4161 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4162 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4163 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4164 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4165 84451b3e 2018-07-10 stsp }
4166 826082fe 2020-12-10 stsp free(line);
4167 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4168 84451b3e 2018-07-10 stsp
4169 1a57306a 2018-09-02 stsp view_vborder(view);
4170 84451b3e 2018-07-10 stsp
4171 84451b3e 2018-07-10 stsp return NULL;
4172 84451b3e 2018-07-10 stsp }
4173 84451b3e 2018-07-10 stsp
4174 84451b3e 2018-07-10 stsp static const struct got_error *
4175 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4176 84451b3e 2018-07-10 stsp {
4177 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4178 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4179 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4180 1a76625f 2018-10-22 stsp int errcode;
4181 84451b3e 2018-07-10 stsp
4182 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4183 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4184 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4185 84451b3e 2018-07-10 stsp
4186 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4187 1a76625f 2018-10-22 stsp if (errcode)
4188 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4189 84451b3e 2018-07-10 stsp
4190 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4191 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4192 d68a0a7d 2018-07-10 stsp goto done;
4193 d68a0a7d 2018-07-10 stsp }
4194 d68a0a7d 2018-07-10 stsp
4195 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4196 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4197 d68a0a7d 2018-07-10 stsp
4198 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4199 d68a0a7d 2018-07-10 stsp if (line->annotated)
4200 d68a0a7d 2018-07-10 stsp goto done;
4201 d68a0a7d 2018-07-10 stsp
4202 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4203 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4204 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4205 84451b3e 2018-07-10 stsp goto done;
4206 84451b3e 2018-07-10 stsp }
4207 84451b3e 2018-07-10 stsp line->annotated = 1;
4208 84451b3e 2018-07-10 stsp done:
4209 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4210 1a76625f 2018-10-22 stsp if (errcode)
4211 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4212 84451b3e 2018-07-10 stsp return err;
4213 84451b3e 2018-07-10 stsp }
4214 84451b3e 2018-07-10 stsp
4215 84451b3e 2018-07-10 stsp static void *
4216 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4217 84451b3e 2018-07-10 stsp {
4218 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4219 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4220 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4221 1a76625f 2018-10-22 stsp int errcode;
4222 18430de3 2018-07-10 stsp
4223 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4224 61266923 2020-01-14 stsp if (err)
4225 61266923 2020-01-14 stsp return (void *)err;
4226 61266923 2020-01-14 stsp
4227 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4228 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4229 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4230 fc06ba56 2019-08-22 stsp err = NULL;
4231 18430de3 2018-07-10 stsp
4232 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4233 1a76625f 2018-10-22 stsp if (errcode)
4234 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4235 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4236 18430de3 2018-07-10 stsp
4237 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4238 1d0f4054 2021-06-17 stsp if (err == NULL)
4239 1d0f4054 2021-06-17 stsp err = close_err;
4240 c9beca56 2018-07-22 stsp ta->repo = NULL;
4241 c9beca56 2018-07-22 stsp *ta->complete = 1;
4242 18430de3 2018-07-10 stsp
4243 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4244 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4245 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4246 18430de3 2018-07-10 stsp
4247 18430de3 2018-07-10 stsp return (void *)err;
4248 84451b3e 2018-07-10 stsp }
4249 84451b3e 2018-07-10 stsp
4250 245d91c1 2018-07-12 stsp static struct got_object_id *
4251 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4252 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4253 245d91c1 2018-07-12 stsp {
4254 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4255 8d0fe45a 2019-08-12 stsp
4256 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4257 8d0fe45a 2019-08-12 stsp return NULL;
4258 b880a918 2018-07-10 stsp
4259 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4260 245d91c1 2018-07-12 stsp if (!line->annotated)
4261 245d91c1 2018-07-12 stsp return NULL;
4262 245d91c1 2018-07-12 stsp
4263 245d91c1 2018-07-12 stsp return line->id;
4264 b880a918 2018-07-10 stsp }
4265 245d91c1 2018-07-12 stsp
4266 b880a918 2018-07-10 stsp static const struct got_error *
4267 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4268 a70480e0 2018-06-23 stsp {
4269 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4270 245d91c1 2018-07-12 stsp int i;
4271 245d91c1 2018-07-12 stsp
4272 245d91c1 2018-07-12 stsp if (blame->thread) {
4273 1a76625f 2018-10-22 stsp int errcode;
4274 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4275 1a76625f 2018-10-22 stsp if (errcode)
4276 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4277 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4278 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4279 1a76625f 2018-10-22 stsp if (errcode)
4280 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4281 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4282 1a76625f 2018-10-22 stsp if (errcode)
4283 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4284 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4285 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4286 245d91c1 2018-07-12 stsp err = NULL;
4287 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4288 245d91c1 2018-07-12 stsp }
4289 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4290 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4291 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4292 1d0f4054 2021-06-17 stsp if (err == NULL)
4293 1d0f4054 2021-06-17 stsp err = close_err;
4294 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4295 245d91c1 2018-07-12 stsp }
4296 245d91c1 2018-07-12 stsp if (blame->f) {
4297 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4298 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4299 245d91c1 2018-07-12 stsp blame->f = NULL;
4300 245d91c1 2018-07-12 stsp }
4301 57670559 2018-12-23 stsp if (blame->lines) {
4302 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4303 57670559 2018-12-23 stsp free(blame->lines[i].id);
4304 57670559 2018-12-23 stsp free(blame->lines);
4305 57670559 2018-12-23 stsp blame->lines = NULL;
4306 57670559 2018-12-23 stsp }
4307 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4308 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4309 245d91c1 2018-07-12 stsp
4310 245d91c1 2018-07-12 stsp return err;
4311 245d91c1 2018-07-12 stsp }
4312 245d91c1 2018-07-12 stsp
4313 245d91c1 2018-07-12 stsp static const struct got_error *
4314 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4315 fc06ba56 2019-08-22 stsp {
4316 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4317 fc06ba56 2019-08-22 stsp int *done = arg;
4318 fc06ba56 2019-08-22 stsp int errcode;
4319 fc06ba56 2019-08-22 stsp
4320 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4321 fc06ba56 2019-08-22 stsp if (errcode)
4322 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4323 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4324 fc06ba56 2019-08-22 stsp
4325 fc06ba56 2019-08-22 stsp if (*done)
4326 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4327 fc06ba56 2019-08-22 stsp
4328 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4329 fc06ba56 2019-08-22 stsp if (errcode)
4330 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4331 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4332 fc06ba56 2019-08-22 stsp
4333 fc06ba56 2019-08-22 stsp return err;
4334 fc06ba56 2019-08-22 stsp }
4335 fc06ba56 2019-08-22 stsp
4336 fc06ba56 2019-08-22 stsp static const struct got_error *
4337 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4338 245d91c1 2018-07-12 stsp {
4339 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4340 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4341 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4342 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4343 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4344 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4345 15a94983 2018-12-23 stsp int obj_type;
4346 a70480e0 2018-06-23 stsp
4347 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4348 a5388363 2020-12-01 naddy s->path);
4349 27d434c2 2018-09-15 stsp if (err)
4350 15a94983 2018-12-23 stsp return err;
4351 27d434c2 2018-09-15 stsp
4352 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4353 84451b3e 2018-07-10 stsp if (err)
4354 84451b3e 2018-07-10 stsp goto done;
4355 27d434c2 2018-09-15 stsp
4356 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4357 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4358 84451b3e 2018-07-10 stsp goto done;
4359 84451b3e 2018-07-10 stsp }
4360 a70480e0 2018-06-23 stsp
4361 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4362 a70480e0 2018-06-23 stsp if (err)
4363 a70480e0 2018-06-23 stsp goto done;
4364 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4365 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4366 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4367 84451b3e 2018-07-10 stsp goto done;
4368 84451b3e 2018-07-10 stsp }
4369 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4370 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4371 1fddf795 2021-01-20 stsp if (err)
4372 1fddf795 2021-01-20 stsp goto done;
4373 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4374 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4375 84451b3e 2018-07-10 stsp goto done;
4376 1fddf795 2021-01-20 stsp }
4377 b02560ec 2019-08-19 stsp
4378 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4379 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4380 b02560ec 2019-08-19 stsp blame->nlines--;
4381 a70480e0 2018-06-23 stsp
4382 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4383 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4384 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4385 84451b3e 2018-07-10 stsp goto done;
4386 84451b3e 2018-07-10 stsp }
4387 a70480e0 2018-06-23 stsp
4388 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4389 bd24772e 2018-07-11 stsp if (err)
4390 bd24772e 2018-07-11 stsp goto done;
4391 bd24772e 2018-07-11 stsp
4392 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4393 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4394 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4395 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4396 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4397 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4398 245d91c1 2018-07-12 stsp goto done;
4399 245d91c1 2018-07-12 stsp }
4400 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4401 245d91c1 2018-07-12 stsp
4402 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4403 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4404 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4405 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4406 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4407 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4408 a5388363 2020-12-01 naddy s->blame_complete = 0;
4409 f5a09613 2020-12-13 naddy
4410 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4411 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4412 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4413 f5a09613 2020-12-13 naddy s->selected_line = 1;
4414 f5a09613 2020-12-13 naddy }
4415 aa61903a 2021-12-31 thomas s->matched_line = 0;
4416 245d91c1 2018-07-12 stsp
4417 245d91c1 2018-07-12 stsp done:
4418 245d91c1 2018-07-12 stsp if (blob)
4419 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4420 27d434c2 2018-09-15 stsp free(obj_id);
4421 245d91c1 2018-07-12 stsp if (err)
4422 245d91c1 2018-07-12 stsp stop_blame(blame);
4423 245d91c1 2018-07-12 stsp return err;
4424 245d91c1 2018-07-12 stsp }
4425 245d91c1 2018-07-12 stsp
4426 245d91c1 2018-07-12 stsp static const struct got_error *
4427 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4428 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4429 245d91c1 2018-07-12 stsp {
4430 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4431 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4432 dbc6a6b6 2018-07-12 stsp
4433 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4434 245d91c1 2018-07-12 stsp
4435 c4843652 2019-08-12 stsp s->path = strdup(path);
4436 c4843652 2019-08-12 stsp if (s->path == NULL)
4437 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4438 c4843652 2019-08-12 stsp
4439 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4440 c4843652 2019-08-12 stsp if (err) {
4441 c4843652 2019-08-12 stsp free(s->path);
4442 7cbe629d 2018-08-04 stsp return err;
4443 c4843652 2019-08-12 stsp }
4444 245d91c1 2018-07-12 stsp
4445 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4446 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4447 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4448 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4449 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4450 fb2756b9 2018-08-04 stsp s->repo = repo;
4451 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4452 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4453 7cbe629d 2018-08-04 stsp
4454 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4455 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4456 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4457 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4458 11b20872 2019-11-08 stsp if (err)
4459 11b20872 2019-11-08 stsp return err;
4460 11b20872 2019-11-08 stsp }
4461 11b20872 2019-11-08 stsp
4462 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4463 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4464 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4465 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4466 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4467 e5a0f69f 2018-08-18 stsp
4468 a5388363 2020-12-01 naddy return run_blame(view);
4469 7cbe629d 2018-08-04 stsp }
4470 7cbe629d 2018-08-04 stsp
4471 e5a0f69f 2018-08-18 stsp static const struct got_error *
4472 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4473 7cbe629d 2018-08-04 stsp {
4474 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4475 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4476 7cbe629d 2018-08-04 stsp
4477 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4478 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4479 e5a0f69f 2018-08-18 stsp
4480 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4481 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4482 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4483 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4484 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4485 7cbe629d 2018-08-04 stsp }
4486 e5a0f69f 2018-08-18 stsp
4487 e5a0f69f 2018-08-18 stsp free(s->path);
4488 11b20872 2019-11-08 stsp free_colors(&s->colors);
4489 e5a0f69f 2018-08-18 stsp
4490 e5a0f69f 2018-08-18 stsp return err;
4491 7cbe629d 2018-08-04 stsp }
4492 7cbe629d 2018-08-04 stsp
4493 7cbe629d 2018-08-04 stsp static const struct got_error *
4494 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4495 6c4c42e0 2019-06-24 stsp {
4496 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4497 6c4c42e0 2019-06-24 stsp
4498 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4499 6c4c42e0 2019-06-24 stsp return NULL;
4500 6c4c42e0 2019-06-24 stsp }
4501 6c4c42e0 2019-06-24 stsp
4502 6c4c42e0 2019-06-24 stsp static const struct got_error *
4503 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4504 6c4c42e0 2019-06-24 stsp {
4505 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4506 6c4c42e0 2019-06-24 stsp int lineno;
4507 826082fe 2020-12-10 stsp char *line = NULL;
4508 826082fe 2020-12-10 stsp size_t linesize = 0;
4509 826082fe 2020-12-10 stsp ssize_t linelen;
4510 6c4c42e0 2019-06-24 stsp
4511 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4512 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4513 6c4c42e0 2019-06-24 stsp return NULL;
4514 6c4c42e0 2019-06-24 stsp }
4515 6c4c42e0 2019-06-24 stsp
4516 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4517 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4518 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4519 6c4c42e0 2019-06-24 stsp else
4520 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4521 4b3f9dac 2021-12-17 thomas } else
4522 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4523 6c4c42e0 2019-06-24 stsp
4524 6c4c42e0 2019-06-24 stsp while (1) {
4525 6c4c42e0 2019-06-24 stsp off_t offset;
4526 6c4c42e0 2019-06-24 stsp
4527 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4528 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4529 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4530 6c4c42e0 2019-06-24 stsp break;
4531 6c4c42e0 2019-06-24 stsp }
4532 2246482e 2019-06-25 stsp
4533 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4534 6c4c42e0 2019-06-24 stsp lineno = 1;
4535 6c4c42e0 2019-06-24 stsp else
4536 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4537 6c4c42e0 2019-06-24 stsp }
4538 6c4c42e0 2019-06-24 stsp
4539 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4540 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4541 6c4c42e0 2019-06-24 stsp free(line);
4542 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4543 6c4c42e0 2019-06-24 stsp }
4544 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4545 826082fe 2020-12-10 stsp if (linelen != -1 &&
4546 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4547 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4548 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4549 6c4c42e0 2019-06-24 stsp break;
4550 6c4c42e0 2019-06-24 stsp }
4551 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4552 6c4c42e0 2019-06-24 stsp lineno++;
4553 6c4c42e0 2019-06-24 stsp else
4554 6c4c42e0 2019-06-24 stsp lineno--;
4555 6c4c42e0 2019-06-24 stsp }
4556 826082fe 2020-12-10 stsp free(line);
4557 6c4c42e0 2019-06-24 stsp
4558 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4559 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4560 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4561 6c4c42e0 2019-06-24 stsp }
4562 6c4c42e0 2019-06-24 stsp
4563 6c4c42e0 2019-06-24 stsp return NULL;
4564 6c4c42e0 2019-06-24 stsp }
4565 6c4c42e0 2019-06-24 stsp
4566 6c4c42e0 2019-06-24 stsp static const struct got_error *
4567 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4568 7cbe629d 2018-08-04 stsp {
4569 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4570 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4571 2b380cc8 2018-10-24 stsp int errcode;
4572 2b380cc8 2018-10-24 stsp
4573 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4574 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4575 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4576 2b380cc8 2018-10-24 stsp if (errcode)
4577 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4578 51fe7530 2019-08-19 stsp
4579 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4580 2b380cc8 2018-10-24 stsp }
4581 e5a0f69f 2018-08-18 stsp
4582 51fe7530 2019-08-19 stsp if (s->blame_complete)
4583 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4584 51fe7530 2019-08-19 stsp
4585 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4586 e5a0f69f 2018-08-18 stsp
4587 669b5ffa 2018-10-07 stsp view_vborder(view);
4588 e5a0f69f 2018-08-18 stsp return err;
4589 e5a0f69f 2018-08-18 stsp }
4590 e5a0f69f 2018-08-18 stsp
4591 e5a0f69f 2018-08-18 stsp static const struct got_error *
4592 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4593 e5a0f69f 2018-08-18 stsp {
4594 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4595 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4596 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4597 669b5ffa 2018-10-07 stsp int begin_x = 0;
4598 7cbe629d 2018-08-04 stsp
4599 e5a0f69f 2018-08-18 stsp switch (ch) {
4600 1e37a5c2 2019-05-12 jcs case 'q':
4601 1e37a5c2 2019-05-12 jcs s->done = 1;
4602 4deef56f 2021-09-02 naddy break;
4603 4deef56f 2021-09-02 naddy case 'g':
4604 4deef56f 2021-09-02 naddy case KEY_HOME:
4605 4deef56f 2021-09-02 naddy s->selected_line = 1;
4606 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4607 4deef56f 2021-09-02 naddy break;
4608 4deef56f 2021-09-02 naddy case 'G':
4609 4deef56f 2021-09-02 naddy case KEY_END:
4610 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4611 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4612 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4613 4deef56f 2021-09-02 naddy } else {
4614 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4615 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4616 4deef56f 2021-09-02 naddy (view->nlines - 3);
4617 4deef56f 2021-09-02 naddy }
4618 1e37a5c2 2019-05-12 jcs break;
4619 1e37a5c2 2019-05-12 jcs case 'k':
4620 1e37a5c2 2019-05-12 jcs case KEY_UP:
4621 f7140bf5 2021-10-17 thomas case CTRL('p'):
4622 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4623 1e37a5c2 2019-05-12 jcs s->selected_line--;
4624 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4625 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4626 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4627 1e37a5c2 2019-05-12 jcs break;
4628 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4629 ea025d1d 2020-02-22 naddy case CTRL('b'):
4630 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4631 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4632 e5a0f69f 2018-08-18 stsp break;
4633 1e37a5c2 2019-05-12 jcs }
4634 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4635 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4636 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4637 1e37a5c2 2019-05-12 jcs else
4638 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4639 1e37a5c2 2019-05-12 jcs break;
4640 1e37a5c2 2019-05-12 jcs case 'j':
4641 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4642 f7140bf5 2021-10-17 thomas case CTRL('n'):
4643 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4644 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4645 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4646 1e37a5c2 2019-05-12 jcs s->selected_line++;
4647 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4648 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4649 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4650 1e37a5c2 2019-05-12 jcs break;
4651 1e37a5c2 2019-05-12 jcs case 'b':
4652 1e37a5c2 2019-05-12 jcs case 'p': {
4653 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4654 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4655 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4656 1e37a5c2 2019-05-12 jcs if (id == NULL)
4657 e5a0f69f 2018-08-18 stsp break;
4658 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4659 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4660 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4661 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4662 1e37a5c2 2019-05-12 jcs int obj_type;
4663 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4664 1e37a5c2 2019-05-12 jcs s->repo, id);
4665 e5a0f69f 2018-08-18 stsp if (err)
4666 e5a0f69f 2018-08-18 stsp break;
4667 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4668 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4669 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4670 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4671 e5a0f69f 2018-08-18 stsp break;
4672 e5a0f69f 2018-08-18 stsp }
4673 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4674 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4675 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4676 e5a0f69f 2018-08-18 stsp if (err) {
4677 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4678 1e37a5c2 2019-05-12 jcs err = NULL;
4679 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4680 e5a0f69f 2018-08-18 stsp break;
4681 e5a0f69f 2018-08-18 stsp }
4682 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4683 1e37a5c2 2019-05-12 jcs blob_id);
4684 1e37a5c2 2019-05-12 jcs free(blob_id);
4685 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4686 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4687 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4688 e5a0f69f 2018-08-18 stsp break;
4689 1e37a5c2 2019-05-12 jcs }
4690 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4691 1e37a5c2 2019-05-12 jcs pid->id);
4692 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4693 1e37a5c2 2019-05-12 jcs } else {
4694 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4695 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4696 1e37a5c2 2019-05-12 jcs break;
4697 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4698 1e37a5c2 2019-05-12 jcs id);
4699 1e37a5c2 2019-05-12 jcs }
4700 1e37a5c2 2019-05-12 jcs if (err)
4701 e5a0f69f 2018-08-18 stsp break;
4702 1e37a5c2 2019-05-12 jcs s->done = 1;
4703 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4704 1e37a5c2 2019-05-12 jcs s->done = 0;
4705 1e37a5c2 2019-05-12 jcs if (thread_err)
4706 1e37a5c2 2019-05-12 jcs break;
4707 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4708 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4709 a5388363 2020-12-01 naddy err = run_blame(view);
4710 1e37a5c2 2019-05-12 jcs if (err)
4711 1e37a5c2 2019-05-12 jcs break;
4712 1e37a5c2 2019-05-12 jcs break;
4713 1e37a5c2 2019-05-12 jcs }
4714 1e37a5c2 2019-05-12 jcs case 'B': {
4715 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4716 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4717 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4718 1e37a5c2 2019-05-12 jcs break;
4719 1e37a5c2 2019-05-12 jcs s->done = 1;
4720 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4721 1e37a5c2 2019-05-12 jcs s->done = 0;
4722 1e37a5c2 2019-05-12 jcs if (thread_err)
4723 1e37a5c2 2019-05-12 jcs break;
4724 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4725 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4726 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4727 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4728 a5388363 2020-12-01 naddy err = run_blame(view);
4729 1e37a5c2 2019-05-12 jcs if (err)
4730 1e37a5c2 2019-05-12 jcs break;
4731 1e37a5c2 2019-05-12 jcs break;
4732 1e37a5c2 2019-05-12 jcs }
4733 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4734 1e37a5c2 2019-05-12 jcs case '\r': {
4735 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4736 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4737 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4738 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4739 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4740 1e37a5c2 2019-05-12 jcs if (id == NULL)
4741 1e37a5c2 2019-05-12 jcs break;
4742 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4743 1e37a5c2 2019-05-12 jcs if (err)
4744 1e37a5c2 2019-05-12 jcs break;
4745 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4746 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4747 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4748 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4749 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4750 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4751 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4752 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4753 1e37a5c2 2019-05-12 jcs break;
4754 15a94983 2018-12-23 stsp }
4755 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4756 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4757 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4758 1e37a5c2 2019-05-12 jcs if (err) {
4759 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4760 1e37a5c2 2019-05-12 jcs break;
4761 1e37a5c2 2019-05-12 jcs }
4762 e78dc838 2020-12-04 stsp view->focussed = 0;
4763 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4764 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4765 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4766 1e37a5c2 2019-05-12 jcs if (err)
4767 34bc9ec9 2019-02-22 stsp break;
4768 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4769 e78dc838 2020-12-04 stsp view->focus_child = 1;
4770 1e37a5c2 2019-05-12 jcs } else
4771 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4772 1e37a5c2 2019-05-12 jcs if (err)
4773 e5a0f69f 2018-08-18 stsp break;
4774 1e37a5c2 2019-05-12 jcs break;
4775 1e37a5c2 2019-05-12 jcs }
4776 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4777 ea025d1d 2020-02-22 naddy case CTRL('f'):
4778 1e37a5c2 2019-05-12 jcs case ' ':
4779 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4780 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4781 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4782 e5a0f69f 2018-08-18 stsp break;
4783 1e37a5c2 2019-05-12 jcs }
4784 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4785 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4786 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4787 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4788 e5a0f69f 2018-08-18 stsp break;
4789 1e37a5c2 2019-05-12 jcs }
4790 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4791 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4792 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4793 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4794 1e37a5c2 2019-05-12 jcs else
4795 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4796 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4797 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4798 1e37a5c2 2019-05-12 jcs break;
4799 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4800 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4801 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4802 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4803 1e37a5c2 2019-05-12 jcs }
4804 1e37a5c2 2019-05-12 jcs break;
4805 1e37a5c2 2019-05-12 jcs default:
4806 1e37a5c2 2019-05-12 jcs break;
4807 a70480e0 2018-06-23 stsp }
4808 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4809 a70480e0 2018-06-23 stsp }
4810 a70480e0 2018-06-23 stsp
4811 a70480e0 2018-06-23 stsp static const struct got_error *
4812 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4813 9f7d7167 2018-04-29 stsp {
4814 a70480e0 2018-06-23 stsp const struct got_error *error;
4815 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4816 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4817 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4818 0587e10c 2020-07-23 stsp char *link_target = NULL;
4819 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4820 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4821 a70480e0 2018-06-23 stsp int ch;
4822 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4823 a70480e0 2018-06-23 stsp
4824 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4825 a70480e0 2018-06-23 stsp switch (ch) {
4826 a70480e0 2018-06-23 stsp case 'c':
4827 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4828 a70480e0 2018-06-23 stsp break;
4829 69069811 2018-08-02 stsp case 'r':
4830 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4831 69069811 2018-08-02 stsp if (repo_path == NULL)
4832 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4833 9ba1d308 2019-10-21 stsp optarg);
4834 69069811 2018-08-02 stsp break;
4835 a70480e0 2018-06-23 stsp default:
4836 17020d27 2019-03-07 stsp usage_blame();
4837 a70480e0 2018-06-23 stsp /* NOTREACHED */
4838 a70480e0 2018-06-23 stsp }
4839 a70480e0 2018-06-23 stsp }
4840 a70480e0 2018-06-23 stsp
4841 a70480e0 2018-06-23 stsp argc -= optind;
4842 a70480e0 2018-06-23 stsp argv += optind;
4843 a70480e0 2018-06-23 stsp
4844 f135c941 2020-02-20 stsp if (argc != 1)
4845 a70480e0 2018-06-23 stsp usage_blame();
4846 6962eb72 2020-02-20 stsp
4847 69069811 2018-08-02 stsp if (repo_path == NULL) {
4848 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4849 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4850 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4851 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4852 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4853 c156c7a4 2020-12-18 stsp goto done;
4854 f135c941 2020-02-20 stsp if (worktree)
4855 eb41ed75 2019-02-05 stsp repo_path =
4856 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4857 f135c941 2020-02-20 stsp else
4858 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4859 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4860 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4861 c156c7a4 2020-12-18 stsp goto done;
4862 c156c7a4 2020-12-18 stsp }
4863 f135c941 2020-02-20 stsp }
4864 a915003a 2019-02-05 stsp
4865 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4866 c02c541e 2019-03-29 stsp if (error != NULL)
4867 8e94dd5b 2019-01-04 stsp goto done;
4868 69069811 2018-08-02 stsp
4869 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4870 f135c941 2020-02-20 stsp worktree);
4871 c02c541e 2019-03-29 stsp if (error)
4872 92205607 2019-01-04 stsp goto done;
4873 69069811 2018-08-02 stsp
4874 f135c941 2020-02-20 stsp init_curses();
4875 f135c941 2020-02-20 stsp
4876 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4877 51a10b52 2020-12-26 stsp if (error)
4878 51a10b52 2020-12-26 stsp goto done;
4879 51a10b52 2020-12-26 stsp
4880 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4881 eb41ed75 2019-02-05 stsp if (error)
4882 69069811 2018-08-02 stsp goto done;
4883 a70480e0 2018-06-23 stsp
4884 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4885 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4886 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4887 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4888 a70480e0 2018-06-23 stsp if (error != NULL)
4889 66b4983c 2018-06-23 stsp goto done;
4890 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4891 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4892 a70480e0 2018-06-23 stsp } else {
4893 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4894 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4895 a70480e0 2018-06-23 stsp }
4896 a19e88aa 2018-06-23 stsp if (error != NULL)
4897 8b473291 2019-02-21 stsp goto done;
4898 8b473291 2019-02-21 stsp
4899 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4900 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4901 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4902 e1cd8fed 2018-08-01 stsp goto done;
4903 e1cd8fed 2018-08-01 stsp }
4904 0587e10c 2020-07-23 stsp
4905 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4906 0587e10c 2020-07-23 stsp commit_id, repo);
4907 7cbe629d 2018-08-04 stsp if (error)
4908 7cbe629d 2018-08-04 stsp goto done;
4909 0587e10c 2020-07-23 stsp
4910 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4911 78756c87 2020-11-24 stsp commit_id, repo);
4912 0587e10c 2020-07-23 stsp if (error)
4913 0587e10c 2020-07-23 stsp goto done;
4914 12314ad4 2019-08-31 stsp if (worktree) {
4915 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4916 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4917 12314ad4 2019-08-31 stsp worktree = NULL;
4918 12314ad4 2019-08-31 stsp }
4919 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4920 a70480e0 2018-06-23 stsp done:
4921 69069811 2018-08-02 stsp free(repo_path);
4922 f135c941 2020-02-20 stsp free(in_repo_path);
4923 0587e10c 2020-07-23 stsp free(link_target);
4924 69069811 2018-08-02 stsp free(cwd);
4925 a70480e0 2018-06-23 stsp free(commit_id);
4926 eb41ed75 2019-02-05 stsp if (worktree)
4927 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4928 1d0f4054 2021-06-17 stsp if (repo) {
4929 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4930 1d0f4054 2021-06-17 stsp if (error == NULL)
4931 1d0f4054 2021-06-17 stsp error = close_err;
4932 1d0f4054 2021-06-17 stsp }
4933 51a10b52 2020-12-26 stsp tog_free_refs();
4934 a70480e0 2018-06-23 stsp return error;
4935 ffd1d5e5 2018-06-23 stsp }
4936 ffd1d5e5 2018-06-23 stsp
4937 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4938 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4939 ffd1d5e5 2018-06-23 stsp {
4940 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4941 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4942 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4943 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4944 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4945 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4946 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4947 ffd1d5e5 2018-06-23 stsp
4948 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4949 ffd1d5e5 2018-06-23 stsp
4950 f7d12f7e 2018-08-01 stsp werase(view->window);
4951 ffd1d5e5 2018-06-23 stsp
4952 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4953 ffd1d5e5 2018-06-23 stsp return NULL;
4954 ffd1d5e5 2018-06-23 stsp
4955 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4956 ffd1d5e5 2018-06-23 stsp if (err)
4957 ffd1d5e5 2018-06-23 stsp return err;
4958 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4959 a3404814 2018-09-02 stsp wstandout(view->window);
4960 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4961 11b20872 2019-11-08 stsp if (tc)
4962 11b20872 2019-11-08 stsp wattr_on(view->window,
4963 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4964 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4965 11b20872 2019-11-08 stsp if (tc)
4966 11b20872 2019-11-08 stsp wattr_off(view->window,
4967 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4968 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4969 a3404814 2018-09-02 stsp wstandend(view->window);
4970 2550e4c3 2018-07-13 stsp free(wline);
4971 2550e4c3 2018-07-13 stsp wline = NULL;
4972 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4973 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4974 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4975 ffd1d5e5 2018-06-23 stsp return NULL;
4976 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4977 ce52c690 2018-06-23 stsp if (err)
4978 ce52c690 2018-06-23 stsp return err;
4979 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4980 2550e4c3 2018-07-13 stsp free(wline);
4981 2550e4c3 2018-07-13 stsp wline = NULL;
4982 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4983 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4984 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4985 ffd1d5e5 2018-06-23 stsp return NULL;
4986 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4987 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4988 a1eca9bb 2018-06-23 stsp return NULL;
4989 ffd1d5e5 2018-06-23 stsp
4990 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4991 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4992 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4993 0cf4efb1 2018-09-29 stsp if (view->focussed)
4994 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4995 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4996 ffd1d5e5 2018-06-23 stsp }
4997 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4998 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4999 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5000 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5001 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5002 ffd1d5e5 2018-06-23 stsp return NULL;
5003 ffd1d5e5 2018-06-23 stsp n = 1;
5004 ffd1d5e5 2018-06-23 stsp } else {
5005 ffd1d5e5 2018-06-23 stsp n = 0;
5006 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5007 ffd1d5e5 2018-06-23 stsp }
5008 ffd1d5e5 2018-06-23 stsp
5009 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5010 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5011 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5012 848d6979 2019-08-12 stsp const char *modestr = "";
5013 56e0773d 2019-11-28 stsp mode_t mode;
5014 1d13200f 2018-07-12 stsp
5015 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5016 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5017 56e0773d 2019-11-28 stsp
5018 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5019 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5020 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5021 1d13200f 2018-07-12 stsp if (err)
5022 638f9024 2019-05-13 stsp return got_error_from_errno(
5023 230a42bd 2019-05-11 jcs "got_object_id_str");
5024 1d13200f 2018-07-12 stsp }
5025 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5026 63c5ca5d 2019-08-24 stsp modestr = "$";
5027 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5028 0d6c6ee3 2020-05-20 stsp int i;
5029 0d6c6ee3 2020-05-20 stsp
5030 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5031 d86d3b18 2020-12-01 naddy te, s->repo);
5032 0d6c6ee3 2020-05-20 stsp if (err) {
5033 0d6c6ee3 2020-05-20 stsp free(id_str);
5034 0d6c6ee3 2020-05-20 stsp return err;
5035 0d6c6ee3 2020-05-20 stsp }
5036 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5037 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5038 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5039 0d6c6ee3 2020-05-20 stsp }
5040 848d6979 2019-08-12 stsp modestr = "@";
5041 0d6c6ee3 2020-05-20 stsp }
5042 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5043 848d6979 2019-08-12 stsp modestr = "/";
5044 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5045 848d6979 2019-08-12 stsp modestr = "*";
5046 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5047 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5048 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5049 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5050 1d13200f 2018-07-12 stsp free(id_str);
5051 0d6c6ee3 2020-05-20 stsp free(link_target);
5052 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5053 1d13200f 2018-07-12 stsp }
5054 1d13200f 2018-07-12 stsp free(id_str);
5055 0d6c6ee3 2020-05-20 stsp free(link_target);
5056 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5057 ffd1d5e5 2018-06-23 stsp if (err) {
5058 ffd1d5e5 2018-06-23 stsp free(line);
5059 ffd1d5e5 2018-06-23 stsp break;
5060 ffd1d5e5 2018-06-23 stsp }
5061 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5062 0cf4efb1 2018-09-29 stsp if (view->focussed)
5063 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5064 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5065 ffd1d5e5 2018-06-23 stsp }
5066 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5067 f26dddb7 2019-11-08 stsp if (tc)
5068 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5069 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5070 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5071 f26dddb7 2019-11-08 stsp if (tc)
5072 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5073 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5074 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5075 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5076 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5077 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5078 ffd1d5e5 2018-06-23 stsp free(line);
5079 2550e4c3 2018-07-13 stsp free(wline);
5080 2550e4c3 2018-07-13 stsp wline = NULL;
5081 ffd1d5e5 2018-06-23 stsp n++;
5082 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5083 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5084 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5085 ffd1d5e5 2018-06-23 stsp break;
5086 ffd1d5e5 2018-06-23 stsp }
5087 ffd1d5e5 2018-06-23 stsp
5088 ffd1d5e5 2018-06-23 stsp return err;
5089 ffd1d5e5 2018-06-23 stsp }
5090 ffd1d5e5 2018-06-23 stsp
5091 ffd1d5e5 2018-06-23 stsp static void
5092 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5093 ffd1d5e5 2018-06-23 stsp {
5094 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5095 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5096 fa86c4bf 2020-11-29 stsp int i = 0;
5097 ffd1d5e5 2018-06-23 stsp
5098 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5099 ffd1d5e5 2018-06-23 stsp return;
5100 ffd1d5e5 2018-06-23 stsp
5101 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5102 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5103 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5104 fa86c4bf 2020-11-29 stsp if (!isroot)
5105 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5106 fa86c4bf 2020-11-29 stsp break;
5107 fa86c4bf 2020-11-29 stsp }
5108 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5109 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5110 ffd1d5e5 2018-06-23 stsp }
5111 ffd1d5e5 2018-06-23 stsp }
5112 ffd1d5e5 2018-06-23 stsp
5113 fa86c4bf 2020-11-29 stsp static void
5114 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5115 ffd1d5e5 2018-06-23 stsp {
5116 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5117 ffd1d5e5 2018-06-23 stsp int n = 0;
5118 ffd1d5e5 2018-06-23 stsp
5119 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5120 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5121 694d3271 2020-12-01 naddy s->first_displayed_entry);
5122 694d3271 2020-12-01 naddy else
5123 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5124 56e0773d 2019-11-28 stsp
5125 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5126 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5127 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5128 768394f3 2019-01-24 stsp if (last) {
5129 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5130 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5131 768394f3 2019-01-24 stsp }
5132 ffd1d5e5 2018-06-23 stsp }
5133 ffd1d5e5 2018-06-23 stsp }
5134 ffd1d5e5 2018-06-23 stsp
5135 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5136 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5137 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5138 ffd1d5e5 2018-06-23 stsp {
5139 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5140 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5141 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5142 ffd1d5e5 2018-06-23 stsp
5143 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5144 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5145 56e0773d 2019-11-28 stsp + 1 /* slash */;
5146 ce52c690 2018-06-23 stsp if (te)
5147 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5148 ce52c690 2018-06-23 stsp
5149 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5150 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5151 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5152 ffd1d5e5 2018-06-23 stsp
5153 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5154 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5155 d9765a41 2018-06-23 stsp while (pt) {
5156 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5157 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5158 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5159 cb2ebc8a 2018-06-23 stsp goto done;
5160 cb2ebc8a 2018-06-23 stsp }
5161 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5162 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5163 cb2ebc8a 2018-06-23 stsp goto done;
5164 cb2ebc8a 2018-06-23 stsp }
5165 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5166 ffd1d5e5 2018-06-23 stsp }
5167 ce52c690 2018-06-23 stsp if (te) {
5168 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5169 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5170 ce52c690 2018-06-23 stsp goto done;
5171 ce52c690 2018-06-23 stsp }
5172 cb2ebc8a 2018-06-23 stsp }
5173 ce52c690 2018-06-23 stsp done:
5174 ce52c690 2018-06-23 stsp if (err) {
5175 ce52c690 2018-06-23 stsp free(*path);
5176 ce52c690 2018-06-23 stsp *path = NULL;
5177 ce52c690 2018-06-23 stsp }
5178 ce52c690 2018-06-23 stsp return err;
5179 ce52c690 2018-06-23 stsp }
5180 ce52c690 2018-06-23 stsp
5181 ce52c690 2018-06-23 stsp static const struct got_error *
5182 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5183 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5184 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5185 ce52c690 2018-06-23 stsp {
5186 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5187 ce52c690 2018-06-23 stsp char *path;
5188 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5189 a0de39f3 2019-08-09 stsp
5190 a0de39f3 2019-08-09 stsp *new_view = NULL;
5191 69efd4c4 2018-07-18 stsp
5192 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5193 ce52c690 2018-06-23 stsp if (err)
5194 ce52c690 2018-06-23 stsp return err;
5195 ffd1d5e5 2018-06-23 stsp
5196 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5197 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5198 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5199 83ce39e3 2019-08-12 stsp goto done;
5200 83ce39e3 2019-08-12 stsp }
5201 cdf1ee82 2018-08-01 stsp
5202 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5203 e5a0f69f 2018-08-18 stsp if (err) {
5204 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5205 fc06ba56 2019-08-22 stsp err = NULL;
5206 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5207 e5a0f69f 2018-08-18 stsp } else
5208 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5209 83ce39e3 2019-08-12 stsp done:
5210 83ce39e3 2019-08-12 stsp free(path);
5211 69efd4c4 2018-07-18 stsp return err;
5212 69efd4c4 2018-07-18 stsp }
5213 69efd4c4 2018-07-18 stsp
5214 69efd4c4 2018-07-18 stsp static const struct got_error *
5215 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5216 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5217 69efd4c4 2018-07-18 stsp {
5218 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5219 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5220 69efd4c4 2018-07-18 stsp char *path;
5221 a0de39f3 2019-08-09 stsp
5222 a0de39f3 2019-08-09 stsp *new_view = NULL;
5223 69efd4c4 2018-07-18 stsp
5224 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5225 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5226 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5227 e5a0f69f 2018-08-18 stsp
5228 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5229 69efd4c4 2018-07-18 stsp if (err)
5230 69efd4c4 2018-07-18 stsp return err;
5231 69efd4c4 2018-07-18 stsp
5232 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5233 4e97c21c 2020-12-06 stsp path, 0);
5234 ba4f502b 2018-08-04 stsp if (err)
5235 e5a0f69f 2018-08-18 stsp view_close(log_view);
5236 e5a0f69f 2018-08-18 stsp else
5237 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5238 cb2ebc8a 2018-06-23 stsp free(path);
5239 cb2ebc8a 2018-06-23 stsp return err;
5240 ffd1d5e5 2018-06-23 stsp }
5241 ffd1d5e5 2018-06-23 stsp
5242 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5243 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5244 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5245 ffd1d5e5 2018-06-23 stsp {
5246 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5247 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5248 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5249 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5250 ffd1d5e5 2018-06-23 stsp
5251 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5252 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5253 bc573f3b 2021-07-10 stsp
5254 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5255 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5256 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5257 ffd1d5e5 2018-06-23 stsp
5258 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5259 bc573f3b 2021-07-10 stsp if (err)
5260 bc573f3b 2021-07-10 stsp goto done;
5261 bc573f3b 2021-07-10 stsp
5262 bc573f3b 2021-07-10 stsp /*
5263 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5264 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5265 bc573f3b 2021-07-10 stsp * closed on demand.
5266 bc573f3b 2021-07-10 stsp */
5267 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5268 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5269 bc573f3b 2021-07-10 stsp if (err)
5270 bc573f3b 2021-07-10 stsp goto done;
5271 bc573f3b 2021-07-10 stsp s->tree = s->root;
5272 bc573f3b 2021-07-10 stsp
5273 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5274 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5275 ffd1d5e5 2018-06-23 stsp goto done;
5276 ffd1d5e5 2018-06-23 stsp
5277 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5278 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5279 ffd1d5e5 2018-06-23 stsp goto done;
5280 ffd1d5e5 2018-06-23 stsp }
5281 ffd1d5e5 2018-06-23 stsp
5282 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5283 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5284 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5285 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5286 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5287 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5288 9cd7cbd1 2020-12-07 stsp goto done;
5289 9cd7cbd1 2020-12-07 stsp }
5290 9cd7cbd1 2020-12-07 stsp }
5291 fb2756b9 2018-08-04 stsp s->repo = repo;
5292 c0b01bdb 2019-11-08 stsp
5293 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5294 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5295 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5296 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5297 c0b01bdb 2019-11-08 stsp if (err)
5298 c0b01bdb 2019-11-08 stsp goto done;
5299 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5300 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5301 bc573f3b 2021-07-10 stsp if (err)
5302 c0b01bdb 2019-11-08 stsp goto done;
5303 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5304 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5305 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5306 bc573f3b 2021-07-10 stsp if (err)
5307 c0b01bdb 2019-11-08 stsp goto done;
5308 e5a0f69f 2018-08-18 stsp
5309 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5310 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5311 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5312 bc573f3b 2021-07-10 stsp if (err)
5313 11b20872 2019-11-08 stsp goto done;
5314 11b20872 2019-11-08 stsp
5315 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5316 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5317 bc573f3b 2021-07-10 stsp if (err)
5318 c0b01bdb 2019-11-08 stsp goto done;
5319 c0b01bdb 2019-11-08 stsp }
5320 c0b01bdb 2019-11-08 stsp
5321 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5322 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5323 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5324 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5325 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5326 ad80ab7b 2018-08-04 stsp done:
5327 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5328 bc573f3b 2021-07-10 stsp if (commit)
5329 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5330 bc573f3b 2021-07-10 stsp if (err)
5331 bc573f3b 2021-07-10 stsp close_tree_view(view);
5332 ad80ab7b 2018-08-04 stsp return err;
5333 ad80ab7b 2018-08-04 stsp }
5334 ad80ab7b 2018-08-04 stsp
5335 e5a0f69f 2018-08-18 stsp static const struct got_error *
5336 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5337 ad80ab7b 2018-08-04 stsp {
5338 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5339 ad80ab7b 2018-08-04 stsp
5340 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5341 fb2756b9 2018-08-04 stsp free(s->tree_label);
5342 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5343 6484ec90 2018-09-29 stsp free(s->commit_id);
5344 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5345 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5346 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5347 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5348 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5349 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5350 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5351 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5352 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5353 ad80ab7b 2018-08-04 stsp free(parent);
5354 ad80ab7b 2018-08-04 stsp
5355 ad80ab7b 2018-08-04 stsp }
5356 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5357 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5358 bc573f3b 2021-07-10 stsp if (s->root)
5359 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5360 7c32bd05 2019-06-22 stsp return NULL;
5361 7c32bd05 2019-06-22 stsp }
5362 7c32bd05 2019-06-22 stsp
5363 7c32bd05 2019-06-22 stsp static const struct got_error *
5364 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5365 7c32bd05 2019-06-22 stsp {
5366 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5367 7c32bd05 2019-06-22 stsp
5368 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5369 7c32bd05 2019-06-22 stsp return NULL;
5370 7c32bd05 2019-06-22 stsp }
5371 7c32bd05 2019-06-22 stsp
5372 7c32bd05 2019-06-22 stsp static int
5373 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5374 7c32bd05 2019-06-22 stsp {
5375 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5376 7c32bd05 2019-06-22 stsp
5377 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5378 56e0773d 2019-11-28 stsp 0) == 0;
5379 7c32bd05 2019-06-22 stsp }
5380 7c32bd05 2019-06-22 stsp
5381 7c32bd05 2019-06-22 stsp static const struct got_error *
5382 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5383 7c32bd05 2019-06-22 stsp {
5384 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5385 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5386 7c32bd05 2019-06-22 stsp
5387 7c32bd05 2019-06-22 stsp if (!view->searching) {
5388 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5389 7c32bd05 2019-06-22 stsp return NULL;
5390 7c32bd05 2019-06-22 stsp }
5391 7c32bd05 2019-06-22 stsp
5392 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5393 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5394 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5395 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5396 56e0773d 2019-11-28 stsp s->selected_entry);
5397 7c32bd05 2019-06-22 stsp else
5398 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5399 56e0773d 2019-11-28 stsp } else {
5400 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5401 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5402 56e0773d 2019-11-28 stsp else
5403 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5404 56e0773d 2019-11-28 stsp s->selected_entry);
5405 7c32bd05 2019-06-22 stsp }
5406 7c32bd05 2019-06-22 stsp } else {
5407 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5408 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5409 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5410 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5411 56e0773d 2019-11-28 stsp else
5412 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5413 7c32bd05 2019-06-22 stsp }
5414 7c32bd05 2019-06-22 stsp
5415 7c32bd05 2019-06-22 stsp while (1) {
5416 56e0773d 2019-11-28 stsp if (te == NULL) {
5417 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5418 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5419 ac66afb8 2019-06-24 stsp return NULL;
5420 ac66afb8 2019-06-24 stsp }
5421 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5422 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5423 56e0773d 2019-11-28 stsp else
5424 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5425 7c32bd05 2019-06-22 stsp }
5426 7c32bd05 2019-06-22 stsp
5427 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5428 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5429 56e0773d 2019-11-28 stsp s->matched_entry = te;
5430 7c32bd05 2019-06-22 stsp break;
5431 7c32bd05 2019-06-22 stsp }
5432 7c32bd05 2019-06-22 stsp
5433 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5434 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5435 56e0773d 2019-11-28 stsp else
5436 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5437 7c32bd05 2019-06-22 stsp }
5438 e5a0f69f 2018-08-18 stsp
5439 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5440 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5441 7c32bd05 2019-06-22 stsp s->selected = 0;
5442 7c32bd05 2019-06-22 stsp }
5443 7c32bd05 2019-06-22 stsp
5444 e5a0f69f 2018-08-18 stsp return NULL;
5445 ad80ab7b 2018-08-04 stsp }
5446 ad80ab7b 2018-08-04 stsp
5447 ad80ab7b 2018-08-04 stsp static const struct got_error *
5448 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5449 ad80ab7b 2018-08-04 stsp {
5450 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5451 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5452 e5a0f69f 2018-08-18 stsp char *parent_path;
5453 ad80ab7b 2018-08-04 stsp
5454 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5455 e5a0f69f 2018-08-18 stsp if (err)
5456 e5a0f69f 2018-08-18 stsp return err;
5457 ffd1d5e5 2018-06-23 stsp
5458 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5459 e5a0f69f 2018-08-18 stsp free(parent_path);
5460 669b5ffa 2018-10-07 stsp
5461 669b5ffa 2018-10-07 stsp view_vborder(view);
5462 e5a0f69f 2018-08-18 stsp return err;
5463 e5a0f69f 2018-08-18 stsp }
5464 ce52c690 2018-06-23 stsp
5465 e5a0f69f 2018-08-18 stsp static const struct got_error *
5466 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5467 e5a0f69f 2018-08-18 stsp {
5468 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5469 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5470 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5471 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5472 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
5473 ffd1d5e5 2018-06-23 stsp
5474 e5a0f69f 2018-08-18 stsp switch (ch) {
5475 1e37a5c2 2019-05-12 jcs case 'i':
5476 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5477 1e37a5c2 2019-05-12 jcs break;
5478 1e37a5c2 2019-05-12 jcs case 'l':
5479 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5480 ffd1d5e5 2018-06-23 stsp break;
5481 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5482 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5483 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5484 e78dc838 2020-12-04 stsp view->focussed = 0;
5485 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5486 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5487 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5488 1e37a5c2 2019-05-12 jcs if (err)
5489 1e37a5c2 2019-05-12 jcs return err;
5490 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5491 e78dc838 2020-12-04 stsp view->focus_child = 1;
5492 1e37a5c2 2019-05-12 jcs } else
5493 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5494 152c1c93 2020-11-29 stsp break;
5495 152c1c93 2020-11-29 stsp case 'r':
5496 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5497 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5498 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5499 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5500 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5501 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5502 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5503 152c1c93 2020-11-29 stsp if (err) {
5504 152c1c93 2020-11-29 stsp view_close(ref_view);
5505 152c1c93 2020-11-29 stsp return err;
5506 152c1c93 2020-11-29 stsp }
5507 e78dc838 2020-12-04 stsp view->focussed = 0;
5508 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5509 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5510 152c1c93 2020-11-29 stsp err = view_close_child(view);
5511 152c1c93 2020-11-29 stsp if (err)
5512 152c1c93 2020-11-29 stsp return err;
5513 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5514 e78dc838 2020-12-04 stsp view->focus_child = 1;
5515 152c1c93 2020-11-29 stsp } else
5516 152c1c93 2020-11-29 stsp *new_view = ref_view;
5517 e4526bf5 2021-09-03 naddy break;
5518 e4526bf5 2021-09-03 naddy case 'g':
5519 e4526bf5 2021-09-03 naddy case KEY_HOME:
5520 e4526bf5 2021-09-03 naddy s->selected = 0;
5521 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5522 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5523 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5524 e4526bf5 2021-09-03 naddy else
5525 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5526 1e37a5c2 2019-05-12 jcs break;
5527 e4526bf5 2021-09-03 naddy case 'G':
5528 e4526bf5 2021-09-03 naddy case KEY_END:
5529 e4526bf5 2021-09-03 naddy s->selected = 0;
5530 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5531 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5532 e4526bf5 2021-09-03 naddy if (te == NULL) {
5533 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5534 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5535 e4526bf5 2021-09-03 naddy n++;
5536 e4526bf5 2021-09-03 naddy }
5537 e4526bf5 2021-09-03 naddy break;
5538 e4526bf5 2021-09-03 naddy }
5539 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5540 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5541 e4526bf5 2021-09-03 naddy }
5542 e4526bf5 2021-09-03 naddy if (n > 0)
5543 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5544 e4526bf5 2021-09-03 naddy break;
5545 1e37a5c2 2019-05-12 jcs case 'k':
5546 1e37a5c2 2019-05-12 jcs case KEY_UP:
5547 f7140bf5 2021-10-17 thomas case CTRL('p'):
5548 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5549 1e37a5c2 2019-05-12 jcs s->selected--;
5550 fa86c4bf 2020-11-29 stsp break;
5551 1e37a5c2 2019-05-12 jcs }
5552 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5553 1e37a5c2 2019-05-12 jcs break;
5554 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5555 ea025d1d 2020-02-22 naddy case CTRL('b'):
5556 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5557 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5558 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5559 fa86c4bf 2020-11-29 stsp s->selected = 0;
5560 fa86c4bf 2020-11-29 stsp } else {
5561 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5562 fa86c4bf 2020-11-29 stsp s->selected = 0;
5563 fa86c4bf 2020-11-29 stsp }
5564 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5565 1e37a5c2 2019-05-12 jcs break;
5566 1e37a5c2 2019-05-12 jcs case 'j':
5567 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5568 f7140bf5 2021-10-17 thomas case CTRL('n'):
5569 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5570 1e37a5c2 2019-05-12 jcs s->selected++;
5571 1e37a5c2 2019-05-12 jcs break;
5572 1e37a5c2 2019-05-12 jcs }
5573 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5574 56e0773d 2019-11-28 stsp == NULL)
5575 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5576 1e37a5c2 2019-05-12 jcs break;
5577 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5578 1e37a5c2 2019-05-12 jcs break;
5579 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5580 ea025d1d 2020-02-22 naddy case CTRL('f'):
5581 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5582 1e37a5c2 2019-05-12 jcs == NULL) {
5583 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5584 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5585 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5586 1e37a5c2 2019-05-12 jcs break;
5587 1e37a5c2 2019-05-12 jcs }
5588 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5589 1e37a5c2 2019-05-12 jcs break;
5590 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5591 1e37a5c2 2019-05-12 jcs case '\r':
5592 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5593 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5594 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5595 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5596 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5597 1e37a5c2 2019-05-12 jcs break;
5598 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5599 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5600 1e37a5c2 2019-05-12 jcs entry);
5601 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5602 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5603 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5604 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5605 1e37a5c2 2019-05-12 jcs s->selected_entry =
5606 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5607 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5608 1e37a5c2 2019-05-12 jcs free(parent);
5609 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5610 56e0773d 2019-11-28 stsp s->selected_entry))) {
5611 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5612 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5613 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5614 1e37a5c2 2019-05-12 jcs if (err)
5615 1e37a5c2 2019-05-12 jcs break;
5616 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5617 941e9f74 2019-05-21 stsp if (err) {
5618 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5619 1e37a5c2 2019-05-12 jcs break;
5620 1e37a5c2 2019-05-12 jcs }
5621 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5622 56e0773d 2019-11-28 stsp s->selected_entry))) {
5623 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5624 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5625 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5626 1e37a5c2 2019-05-12 jcs
5627 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5628 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5629 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5630 1e37a5c2 2019-05-12 jcs if (err)
5631 1e37a5c2 2019-05-12 jcs break;
5632 e78dc838 2020-12-04 stsp view->focussed = 0;
5633 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5634 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5635 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5636 669b5ffa 2018-10-07 stsp if (err)
5637 669b5ffa 2018-10-07 stsp return err;
5638 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5639 e78dc838 2020-12-04 stsp view->focus_child = 1;
5640 669b5ffa 2018-10-07 stsp } else
5641 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5642 1e37a5c2 2019-05-12 jcs }
5643 1e37a5c2 2019-05-12 jcs break;
5644 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5645 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5646 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5647 1e37a5c2 2019-05-12 jcs break;
5648 1e37a5c2 2019-05-12 jcs default:
5649 1e37a5c2 2019-05-12 jcs break;
5650 ffd1d5e5 2018-06-23 stsp }
5651 e5a0f69f 2018-08-18 stsp
5652 ffd1d5e5 2018-06-23 stsp return err;
5653 9f7d7167 2018-04-29 stsp }
5654 9f7d7167 2018-04-29 stsp
5655 ffd1d5e5 2018-06-23 stsp __dead static void
5656 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5657 ffd1d5e5 2018-06-23 stsp {
5658 ffd1d5e5 2018-06-23 stsp endwin();
5659 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5660 ffd1d5e5 2018-06-23 stsp getprogname());
5661 ffd1d5e5 2018-06-23 stsp exit(1);
5662 ffd1d5e5 2018-06-23 stsp }
5663 ffd1d5e5 2018-06-23 stsp
5664 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5665 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5666 ffd1d5e5 2018-06-23 stsp {
5667 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5668 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5669 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5670 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5671 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5672 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5673 4e97c21c 2020-12-06 stsp char *label = NULL;
5674 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5675 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5676 ffd1d5e5 2018-06-23 stsp int ch;
5677 5221c383 2018-08-01 stsp struct tog_view *view;
5678 70ac5f84 2019-03-28 stsp
5679 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5680 ffd1d5e5 2018-06-23 stsp switch (ch) {
5681 ffd1d5e5 2018-06-23 stsp case 'c':
5682 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5683 74283ab8 2020-02-07 stsp break;
5684 74283ab8 2020-02-07 stsp case 'r':
5685 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5686 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5687 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5688 74283ab8 2020-02-07 stsp optarg);
5689 ffd1d5e5 2018-06-23 stsp break;
5690 ffd1d5e5 2018-06-23 stsp default:
5691 e99e2d15 2020-11-24 naddy usage_tree();
5692 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5693 ffd1d5e5 2018-06-23 stsp }
5694 ffd1d5e5 2018-06-23 stsp }
5695 ffd1d5e5 2018-06-23 stsp
5696 ffd1d5e5 2018-06-23 stsp argc -= optind;
5697 ffd1d5e5 2018-06-23 stsp argv += optind;
5698 ffd1d5e5 2018-06-23 stsp
5699 55cccc34 2020-02-20 stsp if (argc > 1)
5700 e99e2d15 2020-11-24 naddy usage_tree();
5701 74283ab8 2020-02-07 stsp
5702 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5703 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5704 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5705 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5706 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5707 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5708 c156c7a4 2020-12-18 stsp goto done;
5709 55cccc34 2020-02-20 stsp if (worktree)
5710 52185f70 2019-02-05 stsp repo_path =
5711 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5712 55cccc34 2020-02-20 stsp else
5713 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5714 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5715 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5716 c156c7a4 2020-12-18 stsp goto done;
5717 c156c7a4 2020-12-18 stsp }
5718 55cccc34 2020-02-20 stsp }
5719 a915003a 2019-02-05 stsp
5720 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5721 c02c541e 2019-03-29 stsp if (error != NULL)
5722 52185f70 2019-02-05 stsp goto done;
5723 d188b9a6 2019-01-04 stsp
5724 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5725 55cccc34 2020-02-20 stsp repo, worktree);
5726 55cccc34 2020-02-20 stsp if (error)
5727 55cccc34 2020-02-20 stsp goto done;
5728 55cccc34 2020-02-20 stsp
5729 55cccc34 2020-02-20 stsp init_curses();
5730 55cccc34 2020-02-20 stsp
5731 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5732 c02c541e 2019-03-29 stsp if (error)
5733 52185f70 2019-02-05 stsp goto done;
5734 ffd1d5e5 2018-06-23 stsp
5735 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5736 51a10b52 2020-12-26 stsp if (error)
5737 51a10b52 2020-12-26 stsp goto done;
5738 51a10b52 2020-12-26 stsp
5739 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5740 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5741 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5742 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5743 4e97c21c 2020-12-06 stsp if (error)
5744 4e97c21c 2020-12-06 stsp goto done;
5745 4e97c21c 2020-12-06 stsp head_ref_name = label;
5746 4e97c21c 2020-12-06 stsp } else {
5747 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5748 4e97c21c 2020-12-06 stsp if (error == NULL)
5749 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5750 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5751 4e97c21c 2020-12-06 stsp goto done;
5752 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5753 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5754 4e97c21c 2020-12-06 stsp if (error)
5755 4e97c21c 2020-12-06 stsp goto done;
5756 4e97c21c 2020-12-06 stsp }
5757 ffd1d5e5 2018-06-23 stsp
5758 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5759 5221c383 2018-08-01 stsp if (view == NULL) {
5760 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5761 5221c383 2018-08-01 stsp goto done;
5762 5221c383 2018-08-01 stsp }
5763 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5764 ad80ab7b 2018-08-04 stsp if (error)
5765 ad80ab7b 2018-08-04 stsp goto done;
5766 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5767 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5768 d91faf3b 2020-12-01 naddy in_repo_path);
5769 55cccc34 2020-02-20 stsp if (error)
5770 55cccc34 2020-02-20 stsp goto done;
5771 55cccc34 2020-02-20 stsp }
5772 55cccc34 2020-02-20 stsp
5773 55cccc34 2020-02-20 stsp if (worktree) {
5774 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5775 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5776 55cccc34 2020-02-20 stsp worktree = NULL;
5777 55cccc34 2020-02-20 stsp }
5778 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5779 ffd1d5e5 2018-06-23 stsp done:
5780 52185f70 2019-02-05 stsp free(repo_path);
5781 e4a0e26d 2020-02-20 stsp free(cwd);
5782 ffd1d5e5 2018-06-23 stsp free(commit_id);
5783 4e97c21c 2020-12-06 stsp free(label);
5784 486cd271 2020-12-06 stsp if (ref)
5785 486cd271 2020-12-06 stsp got_ref_close(ref);
5786 1d0f4054 2021-06-17 stsp if (repo) {
5787 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5788 1d0f4054 2021-06-17 stsp if (error == NULL)
5789 1d0f4054 2021-06-17 stsp error = close_err;
5790 1d0f4054 2021-06-17 stsp }
5791 51a10b52 2020-12-26 stsp tog_free_refs();
5792 ffd1d5e5 2018-06-23 stsp return error;
5793 6458efa5 2020-11-24 stsp }
5794 6458efa5 2020-11-24 stsp
5795 6458efa5 2020-11-24 stsp static const struct got_error *
5796 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5797 6458efa5 2020-11-24 stsp {
5798 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5799 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5800 6458efa5 2020-11-24 stsp
5801 6458efa5 2020-11-24 stsp s->nrefs = 0;
5802 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5803 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
5804 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
5805 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
5806 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
5807 6458efa5 2020-11-24 stsp continue;
5808 6458efa5 2020-11-24 stsp
5809 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5810 6458efa5 2020-11-24 stsp if (re == NULL)
5811 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5812 6458efa5 2020-11-24 stsp
5813 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5814 8924d611 2020-12-26 stsp if (re->ref == NULL)
5815 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5816 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5817 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5818 6458efa5 2020-11-24 stsp }
5819 6458efa5 2020-11-24 stsp
5820 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5821 6458efa5 2020-11-24 stsp return NULL;
5822 6458efa5 2020-11-24 stsp }
5823 6458efa5 2020-11-24 stsp
5824 6458efa5 2020-11-24 stsp void
5825 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5826 6458efa5 2020-11-24 stsp {
5827 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5828 6458efa5 2020-11-24 stsp
5829 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5830 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5831 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5832 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5833 6458efa5 2020-11-24 stsp free(re);
5834 6458efa5 2020-11-24 stsp }
5835 6458efa5 2020-11-24 stsp }
5836 6458efa5 2020-11-24 stsp
5837 6458efa5 2020-11-24 stsp static const struct got_error *
5838 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5839 6458efa5 2020-11-24 stsp {
5840 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5841 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5842 6458efa5 2020-11-24 stsp
5843 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5844 6458efa5 2020-11-24 stsp s->repo = repo;
5845 6458efa5 2020-11-24 stsp
5846 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5847 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5848 6458efa5 2020-11-24 stsp
5849 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5850 6458efa5 2020-11-24 stsp if (err)
5851 6458efa5 2020-11-24 stsp return err;
5852 34ba6917 2020-11-30 stsp
5853 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5854 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5855 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5856 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5857 6458efa5 2020-11-24 stsp if (err)
5858 6458efa5 2020-11-24 stsp goto done;
5859 6458efa5 2020-11-24 stsp
5860 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5861 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5862 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5863 6458efa5 2020-11-24 stsp if (err)
5864 6458efa5 2020-11-24 stsp goto done;
5865 6458efa5 2020-11-24 stsp
5866 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5867 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5868 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5869 2183bbf6 2022-01-23 thomas if (err)
5870 2183bbf6 2022-01-23 thomas goto done;
5871 2183bbf6 2022-01-23 thomas
5872 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
5873 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
5874 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
5875 6458efa5 2020-11-24 stsp if (err)
5876 6458efa5 2020-11-24 stsp goto done;
5877 6458efa5 2020-11-24 stsp }
5878 6458efa5 2020-11-24 stsp
5879 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5880 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5881 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5882 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5883 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5884 6458efa5 2020-11-24 stsp done:
5885 6458efa5 2020-11-24 stsp if (err)
5886 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5887 6458efa5 2020-11-24 stsp return err;
5888 6458efa5 2020-11-24 stsp }
5889 6458efa5 2020-11-24 stsp
5890 6458efa5 2020-11-24 stsp static const struct got_error *
5891 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5892 6458efa5 2020-11-24 stsp {
5893 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5894 6458efa5 2020-11-24 stsp
5895 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5896 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5897 6458efa5 2020-11-24 stsp
5898 6458efa5 2020-11-24 stsp return NULL;
5899 9f7d7167 2018-04-29 stsp }
5900 ce5b7c56 2019-07-09 stsp
5901 6458efa5 2020-11-24 stsp static const struct got_error *
5902 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5903 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5904 6458efa5 2020-11-24 stsp {
5905 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5906 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5907 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5908 6458efa5 2020-11-24 stsp int obj_type;
5909 6458efa5 2020-11-24 stsp
5910 c42c9805 2020-11-24 stsp *commit_id = NULL;
5911 6458efa5 2020-11-24 stsp
5912 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5913 6458efa5 2020-11-24 stsp if (err)
5914 6458efa5 2020-11-24 stsp return err;
5915 6458efa5 2020-11-24 stsp
5916 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5917 6458efa5 2020-11-24 stsp if (err)
5918 6458efa5 2020-11-24 stsp goto done;
5919 6458efa5 2020-11-24 stsp
5920 6458efa5 2020-11-24 stsp switch (obj_type) {
5921 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5922 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5923 6458efa5 2020-11-24 stsp break;
5924 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5925 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5926 6458efa5 2020-11-24 stsp if (err)
5927 6458efa5 2020-11-24 stsp goto done;
5928 c42c9805 2020-11-24 stsp free(obj_id);
5929 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5930 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5931 c42c9805 2020-11-24 stsp if (err)
5932 6458efa5 2020-11-24 stsp goto done;
5933 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5934 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5935 c42c9805 2020-11-24 stsp goto done;
5936 c42c9805 2020-11-24 stsp }
5937 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5938 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5939 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5940 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5941 c42c9805 2020-11-24 stsp goto done;
5942 c42c9805 2020-11-24 stsp }
5943 6458efa5 2020-11-24 stsp break;
5944 6458efa5 2020-11-24 stsp default:
5945 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5946 c42c9805 2020-11-24 stsp break;
5947 6458efa5 2020-11-24 stsp }
5948 6458efa5 2020-11-24 stsp
5949 c42c9805 2020-11-24 stsp done:
5950 c42c9805 2020-11-24 stsp if (tag)
5951 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5952 c42c9805 2020-11-24 stsp if (err) {
5953 c42c9805 2020-11-24 stsp free(*commit_id);
5954 c42c9805 2020-11-24 stsp *commit_id = NULL;
5955 c42c9805 2020-11-24 stsp }
5956 c42c9805 2020-11-24 stsp return err;
5957 c42c9805 2020-11-24 stsp }
5958 c42c9805 2020-11-24 stsp
5959 c42c9805 2020-11-24 stsp static const struct got_error *
5960 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5961 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5962 c42c9805 2020-11-24 stsp {
5963 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5964 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5965 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5966 c42c9805 2020-11-24 stsp
5967 c42c9805 2020-11-24 stsp *new_view = NULL;
5968 c42c9805 2020-11-24 stsp
5969 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5970 c42c9805 2020-11-24 stsp if (err) {
5971 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5972 c42c9805 2020-11-24 stsp return err;
5973 c42c9805 2020-11-24 stsp else
5974 c42c9805 2020-11-24 stsp return NULL;
5975 c42c9805 2020-11-24 stsp }
5976 c42c9805 2020-11-24 stsp
5977 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5978 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5979 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5980 6458efa5 2020-11-24 stsp goto done;
5981 6458efa5 2020-11-24 stsp }
5982 6458efa5 2020-11-24 stsp
5983 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5984 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5985 6458efa5 2020-11-24 stsp done:
5986 6458efa5 2020-11-24 stsp if (err)
5987 6458efa5 2020-11-24 stsp view_close(log_view);
5988 6458efa5 2020-11-24 stsp else
5989 6458efa5 2020-11-24 stsp *new_view = log_view;
5990 c42c9805 2020-11-24 stsp free(commit_id);
5991 6458efa5 2020-11-24 stsp return err;
5992 6458efa5 2020-11-24 stsp }
5993 6458efa5 2020-11-24 stsp
5994 ce5b7c56 2019-07-09 stsp static void
5995 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5996 6458efa5 2020-11-24 stsp {
5997 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5998 34ba6917 2020-11-30 stsp int i = 0;
5999 6458efa5 2020-11-24 stsp
6000 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6001 6458efa5 2020-11-24 stsp return;
6002 6458efa5 2020-11-24 stsp
6003 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6004 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6005 34ba6917 2020-11-30 stsp if (re == NULL)
6006 34ba6917 2020-11-30 stsp break;
6007 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6008 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6009 6458efa5 2020-11-24 stsp }
6010 6458efa5 2020-11-24 stsp }
6011 6458efa5 2020-11-24 stsp
6012 34ba6917 2020-11-30 stsp static void
6013 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6014 6458efa5 2020-11-24 stsp {
6015 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6016 6458efa5 2020-11-24 stsp int n = 0;
6017 6458efa5 2020-11-24 stsp
6018 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6019 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6020 6458efa5 2020-11-24 stsp else
6021 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6022 6458efa5 2020-11-24 stsp
6023 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6024 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6025 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6026 6458efa5 2020-11-24 stsp if (last) {
6027 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6028 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6029 6458efa5 2020-11-24 stsp }
6030 6458efa5 2020-11-24 stsp }
6031 6458efa5 2020-11-24 stsp }
6032 6458efa5 2020-11-24 stsp
6033 6458efa5 2020-11-24 stsp static const struct got_error *
6034 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6035 6458efa5 2020-11-24 stsp {
6036 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6037 6458efa5 2020-11-24 stsp
6038 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6039 6458efa5 2020-11-24 stsp return NULL;
6040 6458efa5 2020-11-24 stsp }
6041 6458efa5 2020-11-24 stsp
6042 6458efa5 2020-11-24 stsp static int
6043 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6044 6458efa5 2020-11-24 stsp {
6045 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6046 6458efa5 2020-11-24 stsp
6047 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6048 6458efa5 2020-11-24 stsp 0) == 0;
6049 6458efa5 2020-11-24 stsp }
6050 6458efa5 2020-11-24 stsp
6051 6458efa5 2020-11-24 stsp static const struct got_error *
6052 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6053 6458efa5 2020-11-24 stsp {
6054 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6055 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6056 6458efa5 2020-11-24 stsp
6057 6458efa5 2020-11-24 stsp if (!view->searching) {
6058 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6059 6458efa5 2020-11-24 stsp return NULL;
6060 6458efa5 2020-11-24 stsp }
6061 6458efa5 2020-11-24 stsp
6062 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6063 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6064 6458efa5 2020-11-24 stsp if (s->selected_entry)
6065 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6066 6458efa5 2020-11-24 stsp else
6067 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6068 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6069 6458efa5 2020-11-24 stsp } else {
6070 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6071 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6072 6458efa5 2020-11-24 stsp else
6073 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6074 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6075 6458efa5 2020-11-24 stsp }
6076 6458efa5 2020-11-24 stsp } else {
6077 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6078 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6079 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6080 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6081 6458efa5 2020-11-24 stsp else
6082 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6083 6458efa5 2020-11-24 stsp }
6084 6458efa5 2020-11-24 stsp
6085 6458efa5 2020-11-24 stsp while (1) {
6086 6458efa5 2020-11-24 stsp if (re == NULL) {
6087 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6088 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6089 6458efa5 2020-11-24 stsp return NULL;
6090 6458efa5 2020-11-24 stsp }
6091 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6092 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6093 6458efa5 2020-11-24 stsp else
6094 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6095 6458efa5 2020-11-24 stsp }
6096 6458efa5 2020-11-24 stsp
6097 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6098 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6099 6458efa5 2020-11-24 stsp s->matched_entry = re;
6100 6458efa5 2020-11-24 stsp break;
6101 6458efa5 2020-11-24 stsp }
6102 6458efa5 2020-11-24 stsp
6103 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6104 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6105 6458efa5 2020-11-24 stsp else
6106 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6107 6458efa5 2020-11-24 stsp }
6108 6458efa5 2020-11-24 stsp
6109 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6110 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6111 6458efa5 2020-11-24 stsp s->selected = 0;
6112 6458efa5 2020-11-24 stsp }
6113 6458efa5 2020-11-24 stsp
6114 6458efa5 2020-11-24 stsp return NULL;
6115 6458efa5 2020-11-24 stsp }
6116 6458efa5 2020-11-24 stsp
6117 6458efa5 2020-11-24 stsp static const struct got_error *
6118 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6119 6458efa5 2020-11-24 stsp {
6120 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6121 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6122 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6123 6458efa5 2020-11-24 stsp char *line = NULL;
6124 6458efa5 2020-11-24 stsp wchar_t *wline;
6125 6458efa5 2020-11-24 stsp struct tog_color *tc;
6126 6458efa5 2020-11-24 stsp int width, n;
6127 6458efa5 2020-11-24 stsp int limit = view->nlines;
6128 6458efa5 2020-11-24 stsp
6129 6458efa5 2020-11-24 stsp werase(view->window);
6130 6458efa5 2020-11-24 stsp
6131 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6132 6458efa5 2020-11-24 stsp
6133 6458efa5 2020-11-24 stsp if (limit == 0)
6134 6458efa5 2020-11-24 stsp return NULL;
6135 6458efa5 2020-11-24 stsp
6136 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6137 6458efa5 2020-11-24 stsp
6138 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6139 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6140 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6141 6458efa5 2020-11-24 stsp
6142 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6143 6458efa5 2020-11-24 stsp if (err) {
6144 6458efa5 2020-11-24 stsp free(line);
6145 6458efa5 2020-11-24 stsp return err;
6146 6458efa5 2020-11-24 stsp }
6147 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6148 6458efa5 2020-11-24 stsp wstandout(view->window);
6149 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6150 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6151 6458efa5 2020-11-24 stsp wstandend(view->window);
6152 6458efa5 2020-11-24 stsp free(wline);
6153 6458efa5 2020-11-24 stsp wline = NULL;
6154 6458efa5 2020-11-24 stsp free(line);
6155 6458efa5 2020-11-24 stsp line = NULL;
6156 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6157 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6158 6458efa5 2020-11-24 stsp if (--limit <= 0)
6159 6458efa5 2020-11-24 stsp return NULL;
6160 6458efa5 2020-11-24 stsp
6161 6458efa5 2020-11-24 stsp n = 0;
6162 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6163 6458efa5 2020-11-24 stsp char *line = NULL;
6164 6458efa5 2020-11-24 stsp
6165 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6166 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6167 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6168 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6169 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6170 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6171 6458efa5 2020-11-24 stsp struct got_object_id *id;
6172 6458efa5 2020-11-24 stsp char *id_str;
6173 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6174 6458efa5 2020-11-24 stsp if (err)
6175 6458efa5 2020-11-24 stsp return err;
6176 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6177 6458efa5 2020-11-24 stsp if (err) {
6178 6458efa5 2020-11-24 stsp free(id);
6179 6458efa5 2020-11-24 stsp return err;
6180 6458efa5 2020-11-24 stsp }
6181 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6182 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6183 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6184 6458efa5 2020-11-24 stsp free(id);
6185 6458efa5 2020-11-24 stsp free(id_str);
6186 6458efa5 2020-11-24 stsp return err;
6187 6458efa5 2020-11-24 stsp }
6188 6458efa5 2020-11-24 stsp free(id);
6189 6458efa5 2020-11-24 stsp free(id_str);
6190 6458efa5 2020-11-24 stsp } else {
6191 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6192 6458efa5 2020-11-24 stsp if (line == NULL)
6193 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6194 6458efa5 2020-11-24 stsp }
6195 6458efa5 2020-11-24 stsp
6196 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6197 6458efa5 2020-11-24 stsp if (err) {
6198 6458efa5 2020-11-24 stsp free(line);
6199 6458efa5 2020-11-24 stsp return err;
6200 6458efa5 2020-11-24 stsp }
6201 6458efa5 2020-11-24 stsp if (n == s->selected) {
6202 6458efa5 2020-11-24 stsp if (view->focussed)
6203 6458efa5 2020-11-24 stsp wstandout(view->window);
6204 6458efa5 2020-11-24 stsp s->selected_entry = re;
6205 6458efa5 2020-11-24 stsp }
6206 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6207 6458efa5 2020-11-24 stsp if (tc)
6208 6458efa5 2020-11-24 stsp wattr_on(view->window,
6209 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6210 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6211 6458efa5 2020-11-24 stsp if (tc)
6212 6458efa5 2020-11-24 stsp wattr_off(view->window,
6213 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6214 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6215 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6216 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6217 6458efa5 2020-11-24 stsp wstandend(view->window);
6218 6458efa5 2020-11-24 stsp free(line);
6219 6458efa5 2020-11-24 stsp free(wline);
6220 6458efa5 2020-11-24 stsp wline = NULL;
6221 6458efa5 2020-11-24 stsp n++;
6222 6458efa5 2020-11-24 stsp s->ndisplayed++;
6223 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6224 6458efa5 2020-11-24 stsp
6225 6458efa5 2020-11-24 stsp limit--;
6226 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6227 6458efa5 2020-11-24 stsp }
6228 6458efa5 2020-11-24 stsp
6229 6458efa5 2020-11-24 stsp view_vborder(view);
6230 6458efa5 2020-11-24 stsp return err;
6231 6458efa5 2020-11-24 stsp }
6232 6458efa5 2020-11-24 stsp
6233 6458efa5 2020-11-24 stsp static const struct got_error *
6234 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6235 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6236 c42c9805 2020-11-24 stsp {
6237 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6238 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6239 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6240 c42c9805 2020-11-24 stsp
6241 c42c9805 2020-11-24 stsp *new_view = NULL;
6242 c42c9805 2020-11-24 stsp
6243 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6244 c42c9805 2020-11-24 stsp if (err) {
6245 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6246 c42c9805 2020-11-24 stsp return err;
6247 c42c9805 2020-11-24 stsp else
6248 c42c9805 2020-11-24 stsp return NULL;
6249 c42c9805 2020-11-24 stsp }
6250 c42c9805 2020-11-24 stsp
6251 c42c9805 2020-11-24 stsp
6252 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6253 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6254 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6255 c42c9805 2020-11-24 stsp goto done;
6256 c42c9805 2020-11-24 stsp }
6257 c42c9805 2020-11-24 stsp
6258 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6259 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6260 c42c9805 2020-11-24 stsp if (err)
6261 c42c9805 2020-11-24 stsp goto done;
6262 c42c9805 2020-11-24 stsp
6263 c42c9805 2020-11-24 stsp *new_view = tree_view;
6264 c42c9805 2020-11-24 stsp done:
6265 c42c9805 2020-11-24 stsp free(commit_id);
6266 c42c9805 2020-11-24 stsp return err;
6267 c42c9805 2020-11-24 stsp }
6268 c42c9805 2020-11-24 stsp static const struct got_error *
6269 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6270 6458efa5 2020-11-24 stsp {
6271 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6272 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6273 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6274 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6275 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
6276 6458efa5 2020-11-24 stsp
6277 6458efa5 2020-11-24 stsp switch (ch) {
6278 6458efa5 2020-11-24 stsp case 'i':
6279 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6280 3bfadbd4 2021-11-20 thomas break;
6281 98182bd0 2021-11-20 thomas case 'o':
6282 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6283 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6284 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6285 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6286 c591440f 2021-11-20 thomas if (err)
6287 c591440f 2021-11-20 thomas break;
6288 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6289 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6290 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6291 3bfadbd4 2021-11-20 thomas if (err)
6292 3bfadbd4 2021-11-20 thomas break;
6293 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6294 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6295 6458efa5 2020-11-24 stsp break;
6296 6458efa5 2020-11-24 stsp case KEY_ENTER:
6297 6458efa5 2020-11-24 stsp case '\r':
6298 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6299 6458efa5 2020-11-24 stsp break;
6300 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6301 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6302 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6303 6458efa5 2020-11-24 stsp s->repo);
6304 e78dc838 2020-12-04 stsp view->focussed = 0;
6305 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6306 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6307 6458efa5 2020-11-24 stsp err = view_close_child(view);
6308 6458efa5 2020-11-24 stsp if (err)
6309 6458efa5 2020-11-24 stsp return err;
6310 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6311 e78dc838 2020-12-04 stsp view->focus_child = 1;
6312 6458efa5 2020-11-24 stsp } else
6313 6458efa5 2020-11-24 stsp *new_view = log_view;
6314 6458efa5 2020-11-24 stsp break;
6315 c42c9805 2020-11-24 stsp case 't':
6316 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6317 c42c9805 2020-11-24 stsp break;
6318 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6319 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6320 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6321 c42c9805 2020-11-24 stsp s->repo);
6322 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6323 c42c9805 2020-11-24 stsp break;
6324 e78dc838 2020-12-04 stsp view->focussed = 0;
6325 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6326 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6327 c42c9805 2020-11-24 stsp err = view_close_child(view);
6328 c42c9805 2020-11-24 stsp if (err)
6329 c42c9805 2020-11-24 stsp return err;
6330 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6331 e78dc838 2020-12-04 stsp view->focus_child = 1;
6332 c42c9805 2020-11-24 stsp } else
6333 c42c9805 2020-11-24 stsp *new_view = tree_view;
6334 c42c9805 2020-11-24 stsp break;
6335 e4526bf5 2021-09-03 naddy case 'g':
6336 e4526bf5 2021-09-03 naddy case KEY_HOME:
6337 e4526bf5 2021-09-03 naddy s->selected = 0;
6338 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6339 e4526bf5 2021-09-03 naddy break;
6340 e4526bf5 2021-09-03 naddy case 'G':
6341 e4526bf5 2021-09-03 naddy case KEY_END:
6342 e4526bf5 2021-09-03 naddy s->selected = 0;
6343 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6344 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6345 e4526bf5 2021-09-03 naddy if (re == NULL)
6346 e4526bf5 2021-09-03 naddy break;
6347 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6348 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6349 e4526bf5 2021-09-03 naddy }
6350 e4526bf5 2021-09-03 naddy if (n > 0)
6351 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6352 e4526bf5 2021-09-03 naddy break;
6353 6458efa5 2020-11-24 stsp case 'k':
6354 6458efa5 2020-11-24 stsp case KEY_UP:
6355 f7140bf5 2021-10-17 thomas case CTRL('p'):
6356 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6357 6458efa5 2020-11-24 stsp s->selected--;
6358 6458efa5 2020-11-24 stsp break;
6359 34ba6917 2020-11-30 stsp }
6360 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6361 6458efa5 2020-11-24 stsp break;
6362 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6363 6458efa5 2020-11-24 stsp case CTRL('b'):
6364 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6365 34ba6917 2020-11-30 stsp s->selected = 0;
6366 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6367 6458efa5 2020-11-24 stsp break;
6368 6458efa5 2020-11-24 stsp case 'j':
6369 6458efa5 2020-11-24 stsp case KEY_DOWN:
6370 f7140bf5 2021-10-17 thomas case CTRL('n'):
6371 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6372 6458efa5 2020-11-24 stsp s->selected++;
6373 6458efa5 2020-11-24 stsp break;
6374 6458efa5 2020-11-24 stsp }
6375 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6376 6458efa5 2020-11-24 stsp /* can't scroll any further */
6377 6458efa5 2020-11-24 stsp break;
6378 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6379 6458efa5 2020-11-24 stsp break;
6380 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6381 6458efa5 2020-11-24 stsp case CTRL('f'):
6382 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6383 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6384 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6385 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6386 6458efa5 2020-11-24 stsp break;
6387 6458efa5 2020-11-24 stsp }
6388 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6389 6458efa5 2020-11-24 stsp break;
6390 6458efa5 2020-11-24 stsp case CTRL('l'):
6391 8924d611 2020-12-26 stsp tog_free_refs();
6392 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6393 8924d611 2020-12-26 stsp if (err)
6394 8924d611 2020-12-26 stsp break;
6395 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6396 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6397 6458efa5 2020-11-24 stsp break;
6398 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6399 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6400 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6401 6458efa5 2020-11-24 stsp break;
6402 6458efa5 2020-11-24 stsp default:
6403 6458efa5 2020-11-24 stsp break;
6404 6458efa5 2020-11-24 stsp }
6405 6458efa5 2020-11-24 stsp
6406 6458efa5 2020-11-24 stsp return err;
6407 6458efa5 2020-11-24 stsp }
6408 6458efa5 2020-11-24 stsp
6409 6458efa5 2020-11-24 stsp __dead static void
6410 6458efa5 2020-11-24 stsp usage_ref(void)
6411 6458efa5 2020-11-24 stsp {
6412 6458efa5 2020-11-24 stsp endwin();
6413 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6414 6458efa5 2020-11-24 stsp getprogname());
6415 6458efa5 2020-11-24 stsp exit(1);
6416 6458efa5 2020-11-24 stsp }
6417 6458efa5 2020-11-24 stsp
6418 6458efa5 2020-11-24 stsp static const struct got_error *
6419 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6420 6458efa5 2020-11-24 stsp {
6421 6458efa5 2020-11-24 stsp const struct got_error *error;
6422 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6423 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6424 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6425 6458efa5 2020-11-24 stsp int ch;
6426 6458efa5 2020-11-24 stsp struct tog_view *view;
6427 6458efa5 2020-11-24 stsp
6428 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6429 6458efa5 2020-11-24 stsp switch (ch) {
6430 6458efa5 2020-11-24 stsp case 'r':
6431 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6432 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6433 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6434 6458efa5 2020-11-24 stsp optarg);
6435 6458efa5 2020-11-24 stsp break;
6436 6458efa5 2020-11-24 stsp default:
6437 e99e2d15 2020-11-24 naddy usage_ref();
6438 6458efa5 2020-11-24 stsp /* NOTREACHED */
6439 6458efa5 2020-11-24 stsp }
6440 6458efa5 2020-11-24 stsp }
6441 6458efa5 2020-11-24 stsp
6442 6458efa5 2020-11-24 stsp argc -= optind;
6443 6458efa5 2020-11-24 stsp argv += optind;
6444 6458efa5 2020-11-24 stsp
6445 6458efa5 2020-11-24 stsp if (argc > 1)
6446 e99e2d15 2020-11-24 naddy usage_ref();
6447 6458efa5 2020-11-24 stsp
6448 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6449 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6450 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6451 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6452 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6453 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6454 c156c7a4 2020-12-18 stsp goto done;
6455 6458efa5 2020-11-24 stsp if (worktree)
6456 6458efa5 2020-11-24 stsp repo_path =
6457 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6458 6458efa5 2020-11-24 stsp else
6459 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6460 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6461 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6462 c156c7a4 2020-12-18 stsp goto done;
6463 c156c7a4 2020-12-18 stsp }
6464 6458efa5 2020-11-24 stsp }
6465 6458efa5 2020-11-24 stsp
6466 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6467 6458efa5 2020-11-24 stsp if (error != NULL)
6468 6458efa5 2020-11-24 stsp goto done;
6469 6458efa5 2020-11-24 stsp
6470 6458efa5 2020-11-24 stsp init_curses();
6471 6458efa5 2020-11-24 stsp
6472 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6473 51a10b52 2020-12-26 stsp if (error)
6474 51a10b52 2020-12-26 stsp goto done;
6475 51a10b52 2020-12-26 stsp
6476 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6477 6458efa5 2020-11-24 stsp if (error)
6478 6458efa5 2020-11-24 stsp goto done;
6479 6458efa5 2020-11-24 stsp
6480 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6481 6458efa5 2020-11-24 stsp if (view == NULL) {
6482 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6483 6458efa5 2020-11-24 stsp goto done;
6484 6458efa5 2020-11-24 stsp }
6485 6458efa5 2020-11-24 stsp
6486 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6487 6458efa5 2020-11-24 stsp if (error)
6488 6458efa5 2020-11-24 stsp goto done;
6489 6458efa5 2020-11-24 stsp
6490 6458efa5 2020-11-24 stsp if (worktree) {
6491 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6492 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6493 6458efa5 2020-11-24 stsp worktree = NULL;
6494 6458efa5 2020-11-24 stsp }
6495 6458efa5 2020-11-24 stsp error = view_loop(view);
6496 6458efa5 2020-11-24 stsp done:
6497 6458efa5 2020-11-24 stsp free(repo_path);
6498 6458efa5 2020-11-24 stsp free(cwd);
6499 1d0f4054 2021-06-17 stsp if (repo) {
6500 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6501 1d0f4054 2021-06-17 stsp if (close_err)
6502 1d0f4054 2021-06-17 stsp error = close_err;
6503 1d0f4054 2021-06-17 stsp }
6504 51a10b52 2020-12-26 stsp tog_free_refs();
6505 6458efa5 2020-11-24 stsp return error;
6506 6458efa5 2020-11-24 stsp }
6507 6458efa5 2020-11-24 stsp
6508 6458efa5 2020-11-24 stsp static void
6509 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6510 ce5b7c56 2019-07-09 stsp {
6511 6059809a 2020-12-17 stsp size_t i;
6512 9f7d7167 2018-04-29 stsp
6513 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6514 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6515 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6516 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6517 ce5b7c56 2019-07-09 stsp }
6518 6879ba42 2020-10-01 naddy fputc('\n', fp);
6519 ce5b7c56 2019-07-09 stsp }
6520 ce5b7c56 2019-07-09 stsp
6521 4ed7e80c 2018-05-20 stsp __dead static void
6522 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6523 9f7d7167 2018-04-29 stsp {
6524 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6525 6879ba42 2020-10-01 naddy
6526 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6527 6879ba42 2020-10-01 naddy getprogname());
6528 6879ba42 2020-10-01 naddy if (hflag) {
6529 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6530 6879ba42 2020-10-01 naddy list_commands(fp);
6531 ee85c5e8 2020-02-29 stsp }
6532 6879ba42 2020-10-01 naddy exit(status);
6533 9f7d7167 2018-04-29 stsp }
6534 9f7d7167 2018-04-29 stsp
6535 c2301be8 2018-04-30 stsp static char **
6536 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6537 c2301be8 2018-04-30 stsp {
6538 ee85c5e8 2020-02-29 stsp va_list ap;
6539 c2301be8 2018-04-30 stsp char **argv;
6540 ee85c5e8 2020-02-29 stsp int i;
6541 c2301be8 2018-04-30 stsp
6542 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6543 ee85c5e8 2020-02-29 stsp
6544 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6545 c2301be8 2018-04-30 stsp if (argv == NULL)
6546 c2301be8 2018-04-30 stsp err(1, "calloc");
6547 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6548 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6549 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6550 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6551 c2301be8 2018-04-30 stsp }
6552 c2301be8 2018-04-30 stsp
6553 ee85c5e8 2020-02-29 stsp va_end(ap);
6554 c2301be8 2018-04-30 stsp return argv;
6555 ee85c5e8 2020-02-29 stsp }
6556 ee85c5e8 2020-02-29 stsp
6557 ee85c5e8 2020-02-29 stsp /*
6558 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6559 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6560 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6561 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6562 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6563 ee85c5e8 2020-02-29 stsp */
6564 ee85c5e8 2020-02-29 stsp static const struct got_error *
6565 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6566 ee85c5e8 2020-02-29 stsp {
6567 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6568 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6569 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6570 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6571 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6572 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6573 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6574 84de9106 2020-12-26 stsp
6575 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6576 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6577 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6578 ee85c5e8 2020-02-29 stsp
6579 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6580 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6581 ee85c5e8 2020-02-29 stsp goto done;
6582 ee85c5e8 2020-02-29 stsp
6583 ee85c5e8 2020-02-29 stsp if (worktree)
6584 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6585 ee85c5e8 2020-02-29 stsp else
6586 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6587 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6588 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6589 ee85c5e8 2020-02-29 stsp goto done;
6590 ee85c5e8 2020-02-29 stsp }
6591 ee85c5e8 2020-02-29 stsp
6592 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6593 ee85c5e8 2020-02-29 stsp if (error != NULL)
6594 ee85c5e8 2020-02-29 stsp goto done;
6595 ee85c5e8 2020-02-29 stsp
6596 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6597 ee85c5e8 2020-02-29 stsp repo, worktree);
6598 ee85c5e8 2020-02-29 stsp if (error)
6599 ee85c5e8 2020-02-29 stsp goto done;
6600 ee85c5e8 2020-02-29 stsp
6601 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6602 84de9106 2020-12-26 stsp if (error)
6603 84de9106 2020-12-26 stsp goto done;
6604 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6605 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6606 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6607 ee85c5e8 2020-02-29 stsp if (error)
6608 ee85c5e8 2020-02-29 stsp goto done;
6609 ee85c5e8 2020-02-29 stsp
6610 ee85c5e8 2020-02-29 stsp if (worktree) {
6611 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6612 ee85c5e8 2020-02-29 stsp worktree = NULL;
6613 ee85c5e8 2020-02-29 stsp }
6614 ee85c5e8 2020-02-29 stsp
6615 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6616 ee85c5e8 2020-02-29 stsp if (error) {
6617 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6618 ee85c5e8 2020-02-29 stsp goto done;
6619 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6620 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6621 6879ba42 2020-10-01 naddy usage(1, 1);
6622 ee85c5e8 2020-02-29 stsp /* not reached */
6623 ee85c5e8 2020-02-29 stsp }
6624 ee85c5e8 2020-02-29 stsp
6625 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6626 1d0f4054 2021-06-17 stsp if (error == NULL)
6627 1d0f4054 2021-06-17 stsp error = close_err;
6628 ee85c5e8 2020-02-29 stsp repo = NULL;
6629 ee85c5e8 2020-02-29 stsp
6630 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6631 ee85c5e8 2020-02-29 stsp if (error)
6632 ee85c5e8 2020-02-29 stsp goto done;
6633 ee85c5e8 2020-02-29 stsp
6634 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6635 ee85c5e8 2020-02-29 stsp argc = 4;
6636 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6637 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6638 ee85c5e8 2020-02-29 stsp done:
6639 1d0f4054 2021-06-17 stsp if (repo) {
6640 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6641 1d0f4054 2021-06-17 stsp if (error == NULL)
6642 1d0f4054 2021-06-17 stsp error = close_err;
6643 1d0f4054 2021-06-17 stsp }
6644 ee85c5e8 2020-02-29 stsp if (worktree)
6645 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6646 ee85c5e8 2020-02-29 stsp free(id);
6647 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6648 ee85c5e8 2020-02-29 stsp free(commit_id);
6649 ee85c5e8 2020-02-29 stsp free(cwd);
6650 ee85c5e8 2020-02-29 stsp free(repo_path);
6651 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6652 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6653 ee85c5e8 2020-02-29 stsp int i;
6654 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6655 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6656 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6657 ee85c5e8 2020-02-29 stsp }
6658 87670572 2020-12-26 naddy tog_free_refs();
6659 ee85c5e8 2020-02-29 stsp return error;
6660 c2301be8 2018-04-30 stsp }
6661 c2301be8 2018-04-30 stsp
6662 9f7d7167 2018-04-29 stsp int
6663 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6664 9f7d7167 2018-04-29 stsp {
6665 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6666 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6667 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6668 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6669 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
6670 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6671 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6672 83cd27f8 2020-01-13 stsp };
6673 9f7d7167 2018-04-29 stsp
6674 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6675 9f7d7167 2018-04-29 stsp
6676 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6677 9f7d7167 2018-04-29 stsp switch (ch) {
6678 9f7d7167 2018-04-29 stsp case 'h':
6679 9f7d7167 2018-04-29 stsp hflag = 1;
6680 9f7d7167 2018-04-29 stsp break;
6681 53ccebc2 2019-07-30 stsp case 'V':
6682 53ccebc2 2019-07-30 stsp Vflag = 1;
6683 53ccebc2 2019-07-30 stsp break;
6684 9f7d7167 2018-04-29 stsp default:
6685 6879ba42 2020-10-01 naddy usage(hflag, 1);
6686 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6687 9f7d7167 2018-04-29 stsp }
6688 9f7d7167 2018-04-29 stsp }
6689 9f7d7167 2018-04-29 stsp
6690 9f7d7167 2018-04-29 stsp argc -= optind;
6691 9f7d7167 2018-04-29 stsp argv += optind;
6692 9814e6a3 2020-09-27 naddy optind = 1;
6693 c2301be8 2018-04-30 stsp optreset = 1;
6694 9f7d7167 2018-04-29 stsp
6695 53ccebc2 2019-07-30 stsp if (Vflag) {
6696 53ccebc2 2019-07-30 stsp got_version_print_str();
6697 6879ba42 2020-10-01 naddy return 0;
6698 53ccebc2 2019-07-30 stsp }
6699 4010e238 2020-12-04 stsp
6700 4010e238 2020-12-04 stsp #ifndef PROFILE
6701 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6702 4010e238 2020-12-04 stsp NULL) == -1)
6703 4010e238 2020-12-04 stsp err(1, "pledge");
6704 4010e238 2020-12-04 stsp #endif
6705 53ccebc2 2019-07-30 stsp
6706 c2301be8 2018-04-30 stsp if (argc == 0) {
6707 f29d3e89 2018-06-23 stsp if (hflag)
6708 6879ba42 2020-10-01 naddy usage(hflag, 0);
6709 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6710 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6711 c2301be8 2018-04-30 stsp argc = 1;
6712 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6713 c2301be8 2018-04-30 stsp } else {
6714 6059809a 2020-12-17 stsp size_t i;
6715 9f7d7167 2018-04-29 stsp
6716 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6717 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6718 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6719 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6720 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6721 9f7d7167 2018-04-29 stsp break;
6722 9f7d7167 2018-04-29 stsp }
6723 9f7d7167 2018-04-29 stsp }
6724 ee85c5e8 2020-02-29 stsp }
6725 3642c4c6 2019-07-09 stsp
6726 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6727 ee85c5e8 2020-02-29 stsp if (argc != 1)
6728 6879ba42 2020-10-01 naddy usage(0, 1);
6729 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6730 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6731 ee85c5e8 2020-02-29 stsp } else {
6732 ee85c5e8 2020-02-29 stsp if (hflag)
6733 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6734 ee85c5e8 2020-02-29 stsp else
6735 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6736 9f7d7167 2018-04-29 stsp }
6737 9f7d7167 2018-04-29 stsp
6738 9f7d7167 2018-04-29 stsp endwin();
6739 b46c1e04 2020-09-20 naddy putchar('\n');
6740 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6741 a2f4a359 2020-02-28 stsp int i;
6742 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6743 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6744 a2f4a359 2020-02-28 stsp free(cmd_argv);
6745 a2f4a359 2020-02-28 stsp }
6746 a2f4a359 2020-02-28 stsp
6747 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6748 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6749 9f7d7167 2018-04-29 stsp return 0;
6750 9f7d7167 2018-04-29 stsp }